#!/usr/bin/env python3 """Behaviour harness for the shared OIDC client module (steward-host only). The module's real imports (authlib, itsdangerous, httpx) are not installed on the steward host, so authlib/httpx are STUBBED and itsdangerous is expected (pip install itsdangerous). Exercises the parts a copy-edit could break: 1. configure() sets the identity; the lazy logger attributes lines to the CONFIGURED name even though it was created before configure() ran. 2. write_session/read_session roundtrip on the configured salt, with the session secret taken from the env (the real derive-from-client-secret path) — and a DIFFERENT salt cannot read the other app's token. 3. tampered/expired tokens read as None (no exception leaks). 4. the pin check: this file's sibling oidc_client.py must be byte-identical to both repos' copies (same guarantee as check-oidc-sync.py, so the harness never validates a drifted copy). Run: python3 scripts/shared/test_oidc_client.py (from the ops-docs repo) """ import hashlib import logging import os import sys import tempfile from pathlib import Path from types import ModuleType HERE = Path(__file__).resolve().parent REPOS = HERE.parent.parent.parent # --- stub authlib + httpx BEFORE importing the module ----------------------- for name in ("authlib", "authlib.jose"): m = ModuleType(name) sys.modules.setdefault(name, m) sys.modules["authlib.jose"].JsonWebToken = object sys.modules["authlib.jose"].JsonWebKey = object httpx = ModuleType("httpx") httpx.AsyncClient = object sys.modules["httpx"] = httpx try: import itsdangerous # noqa: F401 except ImportError: sys.exit("itsdangerous is required: pip install itsdangerous") sys.path.insert(0, str(HERE)) oidc = __import__("oidc_client") failures = [] def check(name, ok, detail=""): print(("PASS " if ok else "FAIL ") + name + (f" [{detail}]" if detail and not ok else "")) if not ok: failures.append(name) # --- 0. the copies this harness validates are the byte-identical ones ------- canon = hashlib.sha256((HERE / "oidc_client.py").read_bytes()).hexdigest() for repo in ("member-dashboard", "admin-panel"): copy = REPOS / repo / "app" / "oidc.py" check(f"sync: {repo}/app/oidc.py byte-identical to canonical", hashlib.sha256(copy.read_bytes()).hexdigest() == canon, str(copy)) # --- 1. configure() + lazy logger ------------------------------------------- pre = oidc.logger.name # read BEFORE configure: sentinel, not a crash oidc.configure("admin-panel", "admin_oidc_session", "admin-panel-oidc-session") records = [] class _Capture(logging.Handler): def emit(self, record): records.append(record) h = _Capture() oidc.logger.addHandler(h) # the proxy resolves the configured logger oidc.logger.setLevel(logging.WARNING) oidc.logger.warning("probe line") check("configure: identity is the app's", oidc.APP_NAME == "admin-panel" and oidc.SESSION_COOKIE == "admin_oidc_session") check("lazy logger: pre-configure name is the loud sentinel", pre == "unset-app", pre) check("lazy logger: lines attribute to the CONFIGURED app", bool(records) and records[-1].name == "admin-panel", records[-1].name if records else "no record") # --- 2/3. session tokens on the configured salt ------------------------------ with tempfile.TemporaryDirectory() as td: os.environ["CLOUDRON_OIDC_CLIENT_SECRET"] = "unit-test-secret" token = oidc.write_session("nathan") check("session: roundtrip returns the identity", oidc.read_session(token) == "nathan") check("session: cookie name survived configure", oidc.SESSION_COOKIE == "admin_oidc_session") tampered = token[:-3] + ("aaa" if token[-3:] != "aaa" else "bbb") check("session: tampered token reads None (no exception)", oidc.read_session(tampered) is None) check("session: empty token reads None", oidc.read_session("") is None) # A different salt (the other app's) must NOT read this token. oidc.configure("member-dashboard", "member_oidc_session", "member-dashboard-oidc-session") check("session: the other app's salt cannot read this app's token", oidc.read_session(token) is None) d_tok = oidc.write_session("someuser") check("session: dashboard roundtrip on its own salt", oidc.read_session(d_tok) == "someuser") check("session: APP_NAME switched with configure", oidc.APP_NAME == "member-dashboard") os.environ.pop("CLOUDRON_OIDC_CLIENT_SECRET") # back to the panel identity, as the panel's main.py leaves it oidc.configure("admin-panel", "admin_oidc_session", "admin-panel-oidc-session") print() print(f"{len(failures)} failure(s)" + (": " + ", ".join(failures) if failures else "")) sys.exit(1 if failures else 0)