Files
ops-docs/maintenance/shared-code-and-guest-state.md
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

97 lines
5.2 KiB
Markdown

# Shared code: guest state and the OIDC client (2026-11-11 consolidation)
The fragile-framework audit found the guest invited/active state logic
triplicated (portal `app/main.py` admin_overview, dashboard page JS, panel page
JS) and `app/oidc.py` near-duplicated between the dashboard and the panel
(21-line diff: logger name, session cookie, session salt, docstring, one dead
line). This note records how both were consolidated and how to change them
safely from here.
## Guest state — the portal is the single derivation point
`member-portal` computes a guest's display state ONCE, in the `/admin/overview`
row (and the same semantics ride the `/broker/guests` trail):
1. invite not yet accepted (Cloudron `inviteAccepted` false AND no username,
from a SEEN user record) → `state: "invited"` — never "active";
2. otherwise → the guests-ledger status (active / converted / expired /
depleted / cancelled by recipient) — the record of record;
3. no ledger row → the membership state (active/inactive) as fallback.
Tested once, in `member-portal/tests/test_guest_invited_state.py`.
The consumers are pure shims — first served field wins, NO derivation from
flags anywhere:
- **admin panel** (`admin-panel-guests-harness.js` asserts this): `state` →
`guest_status` → nothing (an empty pill; a payload with neither field no
longer exists and must not be re-derived). `guest_status` first keeps the
previous payload shape rendering identically during a portal rollback.
- **member dashboard** (`dashboard-guests-harness.js`): trail pill reads
`state || status` (unchanged behaviour; comment updated).
Changing the derivation means changing the PORTAL + its test, then syncing the
consumer comments/harnesses if the vocabulary changes. Never re-add a
derivation to a consumer.
## OIDC client — one canonical module, byte-identical copies
- **Canonical:** `scripts/shared/oidc_client.py` (this repo).
- **Copies:** `member-dashboard/app/oidc.py`, `admin-panel/app/oidc.py` —
byte-identical to the canonical by sha256. Each app calls
`oidc.configure("<app>", "<cookie>", "<salt>")` in `main.py` at import time
with its HISTORICAL cookie name and salt, so existing sessions survive.
- **Why copies, not a portal-served config:** OIDC is each app's LOGIN path;
making it depend on the portal being up would trade a small duplication for
a cross-app outage coupling. Copies keep every app self-contained; the sync
tooling keeps them identical.
- **Change procedure:** edit the canonical → run
`scripts/sync-oidc-module.sh` (re-copies both, re-pins, py_compiles, runs
the check) → commit all three repos. NEVER hand-edit a repo copy:
- each repo's `tests/test_oidc_sync.py` pins the copy to the canonical
revision's sha256 and goes red on any hand edit;
- `scripts/check-oidc-sync.py` is the pre-deploy gate (run before building
either thin app) and verifies copies, pins and configured identities;
- `scripts/shared/test_oidc_client.py` is the steward-host behaviour harness
(stubs authlib/httpx; needs `pip install itsdangerous`): configure(),
lazy-logger attribution, session roundtrip, cross-app salt isolation.
Woodpecker CI currently runs the panel's guard+harnesses and the portal's 12
suites but does NOT run the new pin tests (CI was out of scope for the
consolidation). Adding `python3 tests/test_oidc_sync.py` to both thin apps'
pipelines is a one-line follow-up; until then the pre-deploy
`check-oidc-sync.py` run is the drift gate.
## Deploy record 2026-11-11 (zero downtime)
Order: portal first (source of truth), then dashboard, then panel; each built
from its own repo dir, verified by running image tag + container md5 + code
marker + health + real payload fetch. Chat/gateway untouched throughout.
| App | New tag (deployed) | Rollback tag (previous) |
|---|---|---|
| member-portal | `code/member-portal:20261111-58214be0` | `code/member-portal:20261108-2954fe60` |
| member-dashboard | `code/member-dashboard:20261111-622982a3` | `code/member-dashboard:20261108-1e6e9e48` |
| admin-panel | `code/admin-panel:20261111-80acfaed` | `code/admin-panel:20261108-b23cc67` |
One-command reverts (run from the app's OWN repo dir; verify by container md5
+ marker afterwards, never by exit code):
- portal / panel (pinned manifests): `git revert --no-edit <pin commit>` then
`cloudron update --app <id> --force`.
- dashboard (no pin): `cloudron update --app daf8750e-2683-4104-83ea-f77d1b4601e8
--image registry.inference.coop/code/member-dashboard:20261108-1e6e9e48 --force`.
Rollback compatibility: any single-app rollback renders correctly — the shims
consume whichever fields the other payload shape serves. The only known nuance
(portal rolled back alone while the new panel stays deployed): an ENDED guest
would pill as `inactive` instead of its ledger status until the portal is
restored; no ended guests existed at consolidation time.
Verification evidence 2026-11-11: all 4 live guest rows byte-identical
pre/post (2 invited, 2 active); member stable-field hash identical across
no-deploy fetches (112 active, 1 in_grace, 2 invited); both apps'
`/auth/openid/login` → 302 to the Cloudron provider with correct
client_id/redirect_uri/scope; dashboard + panel `/` → 302 to the login route;
container `oidc.py` md5 identical in both thin apps (`df23e7a8…`).