Files
ops-docs/scripts/sync-oidc-module.sh
T
inference-bot 23d2fc0eb2 Shared code: single-source the OIDC client; record the guest-state consolidation
- scripts/shared/oidc_client.py is the canonical OIDC client module; the
  dashboard and panel ship byte-identical copies (pinned by
  tests/test_oidc_sync.py in each repo). sync-oidc-module.sh re-copies +
  re-pins; check-oidc-sync.py is the pre-deploy gate;
  scripts/shared/test_oidc_client.py is the behaviour harness.
- maintenance/shared-code-and-guest-state.md records the guest-state
  single-derivation design (portal serves state; panel/dashboard are pure
  shims), the deploy record with rollback tags, and the CI follow-up.
- app-inventory.md: the three apps' tags refreshed to the 20261111 deploys;
  the stale 'admin panel = local/coop...' registry-path note corrected
  against the live API (all three live under code/).
2026-11-11 05:24:57 +00:00

109 lines
4.1 KiB
Bash
Executable File

#!/usr/bin/env bash
# Sync the shared OIDC client module to both thin apps, and re-pin them.
#
# ops-docs/scripts/sync-oidc-module.sh
#
# The canonical module is scripts/shared/oidc_client.py in THIS repo. The two
# consumer repos (member-dashboard, admin-panel) ship BYTE-IDENTICAL copies at
# app/oidc.py so their login never depends on another app being up. Editing a
# copy by hand is a dead end: this script overwrites it, and the repo's
# tests/test_oidc_sync.py goes red on any drift from the pinned revision.
#
# What this does:
# 1. copies the canonical module over both repos' app/oidc.py
# 2. regenerates each repo's tests/test_oidc_sync.py with the CURRENT
# canonical sha256 (the pin CI checks, since CI has no ops-docs checkout)
# 3. py_compiles every touched file
# 4. runs check-oidc-sync.py as the gate — a red check leaves the working
# tree dirty so nothing can be silently committed half-synced
#
# After it passes: commit all three repos (ops-docs + both apps).
set -euo pipefail
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPOS="$(dirname "$(dirname "$HERE")")" # ops-docs/scripts -> ops-docs -> ~/repos
CANONICAL="$HERE/shared/oidc_client.py"
[ -f "$CANONICAL" ] || { echo "canonical module missing: $CANONICAL" >&2; exit 1; }
SHA="$(sha256sum "$CANONICAL" | cut -d' ' -f1)"
echo "canonical sha256: $SHA"
gen_pin_test() { # <repo> <cookie-literal-configure-call>
local repo="$1" configure="$2"
local dir="$REPOS/$repo"
mkdir -p "$dir/tests"
cat > "$dir/tests/test_oidc_sync.py" <<EOF
#!/usr/bin/env python3
"""oidc.py sync pin — the repo copy is byte-identical to the canonical module.
WHY THIS EXISTS
app/oidc.py is a byte-identical copy of the canonical module
(co-op/ops-docs repo: scripts/shared/oidc_client.py) — the single source for
the OIDC client the thin apps ship copies of, so their login never depends
on another app being up. This test pins the copy to the canonical revision
recorded below: ANY hand edit to app/oidc.py breaks the pin and goes red.
To change the module, edit the canonical file and run
ops-docs/scripts/sync-oidc-module.sh, which re-copies and re-pins both
repos in one step (check-oidc-sync.py is the pre-deploy gate).
Generated by sync-oidc-module.sh — do not edit by hand.
Run: python3 tests/test_oidc_sync.py
"""
import hashlib
import sys
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
CANONICAL_SHA256 = "$SHA"
EXPECTED_CONFIGURE = '''$configure'''
failures = []
copy = ROOT / "app" / "oidc.py"
if not copy.exists():
failures.append("app/oidc.py missing")
else:
got = hashlib.sha256(copy.read_bytes()).hexdigest()
if got == CANONICAL_SHA256:
print(f"ok - app/oidc.py matches the pinned canonical revision ({got[:16]}…)")
else:
failures.append(
"app/oidc.py DRIFTED from the pinned canonical revision\\n"
f" pinned {CANONICAL_SHA256}\\n"
f" actual {got}\\n"
" fix: edit the canonical module (ops-docs: scripts/shared/oidc_client.py) "
"and run ops-docs/scripts/sync-oidc-module.sh — never hand-edit the copy.")
main_src = (ROOT / "app" / "main.py")
if not main_src.exists():
failures.append("app/main.py missing")
elif EXPECTED_CONFIGURE not in main_src.read_text():
failures.append(
"app/main.py does not configure this app's oidc identity:\\n"
f" expected literal: {EXPECTED_CONFIGURE}")
if failures:
print()
print("FAILED: " + ", ".join(failures))
sys.exit(1)
print("oidc sync pin: OK")
EOF
echo "regenerated $repo/tests/test_oidc_sync.py"
}
gen_pin_test member-dashboard \
'oidc.configure("member-dashboard", "member_oidc_session", "member-dashboard-oidc-session")'
gen_pin_test admin-panel \
'oidc.configure("admin-panel", "admin_oidc_session", "admin-panel-oidc-session")'
for repo in member-dashboard admin-panel; do
cp "$CANONICAL" "$REPOS/$repo/app/oidc.py"
echo "copied canonical -> $repo/app/oidc.py"
python3 -m py_compile "$REPOS/$repo/app/oidc.py" "$REPOS/$repo/tests/test_oidc_sync.py"
done
echo "py_compile: OK (both copies + both pin tests)"
python3 "$HERE/check-oidc-sync.py"