T13.7: dry run for the Okta sync, OKTA_SYNC_DRY_RUN (D22)

This commit is contained in:
2026-10-16 14:53:05 -07:00
parent 2d4e96d668
commit bcef0e8145
7 changed files with 243 additions and 12 deletions

View File

@@ -0,0 +1,134 @@
#!/usr/bin/env python3
"""Does the Okta sync dry run change nothing? - D22 / T13.7.
With OKTA_SYNC_DRY_RUN on, the sync must fetch, match and apply both guards exactly
as in a live run, report who it WOULD disable, and change no account and write no
audit row. With it off (the default) behavior is as before.
Self-contained: in-memory SQLite, no server, no network, no browser.
Exit 0 all passed, 1 a failure.
"""
import logging
import os
import sys
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, ROOT)
from sqlalchemy import create_engine, select # noqa: E402
from sqlalchemy.orm import sessionmaker # noqa: E402
from sqlalchemy.pool import StaticPool # noqa: E402
from server import models, okta_sync # noqa: E402
from server.okta_sync import OktaSyncError, OktaUser, run_sync # noqa: E402
fails = 0
def chk(label, ok, extra=""):
global fails
print(("PASS " if ok else "FAIL ") + label + (f" [{extra}]" if (extra and not ok) else ""))
if not ok:
fails += 1
engine = create_engine("sqlite://", connect_args={"check_same_thread": False}, poolclass=StaticPool)
models.Base.metadata.create_all(engine)
Session = sessionmaker(bind=engine, autoflush=False, future=True)
def seed(names):
with Session() as db:
db.query(models.AuditLog).delete()
db.query(models.User).delete()
for n in names:
db.add(models.User(id="u_" + n, username=n, is_active=True, email=""))
db.commit()
def state():
with Session() as db:
return ({u.username: u.is_active for u in db.scalars(select(models.User))},
db.query(models.AuditLog).count())
def okta(*pairs):
return lambda: [OktaUser(identity=i, status=s) for i, s in pairs]
class Capture(logging.Handler):
def __init__(self):
super().__init__()
self.lines = []
def emit(self, r):
self.lines.append(r.getMessage())
cap = Capture()
logging.getLogger("wpsuite.okta_sync").addHandler(cap)
# 1. Flag parsing
for v, want in [("", False), ("false", False), ("0", False), ("true", True), ("TRUE", True),
("1", True), ("yes", True), ("on", True), (" on ", True), ("maybe", False)]:
os.environ["OKTA_SYNC_DRY_RUN"] = v
chk(f"OKTA_SYNC_DRY_RUN={v!r} -> {want}", okta_sync.dry_run() is want)
os.environ.pop("OKTA_SYNC_DRY_RUN")
chk("unset -> live", okta_sync.dry_run() is False)
# 2. Dry run reports the plan and changes nothing
seed(["alice", "bob", "carol", "dan"])
before = state()
with Session() as db:
res = run_sync(db, fetch=okta(("alice", "ACTIVE"), ("bob", "SUSPENDED"), ("carol", "DEPROVISIONED")), dry=True)
after = state()
chk("dry run ok", res.ok and res.dry_run)
chk("dry run lists bob, carol and dan (missing)", sorted(res.disabled) == ["bob", "carol", "dan"], str(res.disabled))
chk("dry run changed no account", before == after, f"{before} {after}")
chk("dry run wrote no audit row", after[1] == 0)
chk("log says DRY RUN would disable", any("DRY RUN would disable 'bob'" in l for l in cap.lines))
chk("summary line says no account was changed and counts 3 matched, 3 would be disabled",
any("DRY RUN done, no account was changed" in l and "3 matched" in l and "3 would be disabled" in l
for l in cap.lines))
# 3. The environment decides when no argument is given
os.environ["OKTA_SYNC_DRY_RUN"] = "true"
seed(["alice", "bob"])
with Session() as db:
res = run_sync(db, fetch=okta(("alice", "ACTIVE"), ("bob", "SUSPENDED")))
chk("env on -> dry run", res.dry_run and state()[0]["bob"] is True and state()[1] == 0)
# 4. Guards still apply in a dry run
seed(["alice", "bob"])
with Session() as db:
res = run_sync(db, fetch=okta(("zed", "ACTIVE")))
chk("zero-match guard still refuses in a dry run", (not res.ok) and "probably wrong" in res.error)
def boom():
raise OktaSyncError("down")
with Session() as db:
res = run_sync(db, fetch=boom)
chk("fetch failure still fails closed in a dry run", (not res.ok) and state() == ({"alice": True, "bob": True}, 0))
# 5. Live run (flag off) is unchanged
os.environ.pop("OKTA_SYNC_DRY_RUN")
seed(["alice", "bob"])
with Session() as db:
res = run_sync(db, fetch=okta(("alice", "ACTIVE"), ("bob", "SUSPENDED")))
st = state()
chk("live run disables and logs one audit row", (not res.dry_run) and st[0]["bob"] is False and st[1] == 1)
# 6. Startup line names the mode
os.environ["OKTA_API_TOKEN"] = "tok-secret-9f3a"
os.environ["OKTA_ORG_URL"] = "https://example.okta.com"
os.environ["OKTA_SYNC_DRY_RUN"] = "true"
chk("startup line says DRY RUN", "DRY RUN" in okta_sync.describe())
os.environ.pop("OKTA_SYNC_DRY_RUN")
chk("startup line says live", "(live)" in okta_sync.describe())
chk("startup line never holds the token", "tok-secret-9f3a" not in okta_sync.describe())
print("\nDONE" if fails == 0 else f"\n{fails} FAILED")
sys.exit(1 if fails else 0)