T13.7: dry run for the Okta sync, OKTA_SYNC_DRY_RUN (D22)
This commit is contained in:
@@ -84,6 +84,12 @@ OKTA_REDIRECT_URI=CHANGE_ME
|
||||
# once a minute and one of them wins each cycle, so raising the worker count does
|
||||
# not raise the call rate to Okta.
|
||||
# OKTA_SYNC_INTERVAL_SECONDS=900
|
||||
#
|
||||
# Dry run (D22). Set to true for the first deploy. The sync then does everything
|
||||
# except the change: it logs who it WOULD disable and writes nothing, and it writes
|
||||
# no audit rows. Read the result with: docker compose logs api | grep "Okta sync"
|
||||
# Remove it (or set it to false) to go live. Off by default.
|
||||
# OKTA_SYNC_DRY_RUN=true
|
||||
|
||||
# ── Email notifications (optional) ─────────────────────────────────────────────
|
||||
# WP-assignment emails are OFF by default and are turned on from the Admin
|
||||
|
||||
@@ -148,7 +148,16 @@ def describe() -> str:
|
||||
return "Okta sync DISABLED - no OKTA_ORG_URL and OKTA_ISSUER has no host. Deprovisioning is not detected."
|
||||
if not base.startswith("https://"):
|
||||
return f"Okta sync DISABLED - org URL {base!r} is not https. The token is never sent over http."
|
||||
return f"Okta sync configured - org {base}, identity claim {okta_auth.IDENTITY_CLAIM!r}"
|
||||
mode = "DRY RUN, no account is changed" if dry_run() else "live"
|
||||
return f"Okta sync configured ({mode}) - org {base}, identity claim {okta_auth.IDENTITY_CLAIM!r}"
|
||||
|
||||
|
||||
def dry_run() -> bool:
|
||||
"""OKTA_SYNC_DRY_RUN (D22). When it is 1, true, yes or on, the sync does its whole
|
||||
job except the change: it logs who it WOULD disable and writes nothing. Off by
|
||||
default, so a deploy that does not set it behaves as before. Read at each run,
|
||||
so the value in force is the one the worker started with."""
|
||||
return os.getenv("OKTA_SYNC_DRY_RUN", "").strip().lower() in {"1", "true", "yes", "on"}
|
||||
|
||||
|
||||
def _profile_attribute() -> str:
|
||||
@@ -269,6 +278,8 @@ class SyncResult:
|
||||
okta_users: int = 0 # size of the Okta list
|
||||
checked: int = 0 # active local accounts looked at
|
||||
disabled: "list[str]" = field(default_factory=list) # usernames disabled this run
|
||||
# (in a dry run: the ones it WOULD disable)
|
||||
dry_run: bool = False # True = nothing was changed on purpose (D22)
|
||||
|
||||
|
||||
def _gen_id(prefix: str) -> str:
|
||||
@@ -277,7 +288,8 @@ def _gen_id(prefix: str) -> str:
|
||||
return f"{prefix}_{uuid.uuid4().hex[:12]}"
|
||||
|
||||
|
||||
def run_sync(db: Session, fetch: Optional[Callable[[], "list[OktaUser]"]] = None) -> SyncResult:
|
||||
def run_sync(db: Session, fetch: Optional[Callable[[], "list[OktaUser]"]] = None,
|
||||
dry: Optional[bool] = None) -> SyncResult:
|
||||
"""Compare the Okta user list to local accounts and disable the ones Okta no
|
||||
longer vouches for.
|
||||
|
||||
@@ -307,9 +319,16 @@ def run_sync(db: Session, fetch: Optional[Callable[[], "list[OktaUser]"]] = None
|
||||
identity twice with different statuses, the account is disabled only when
|
||||
every one of them is a disabling status.
|
||||
|
||||
`fetch` is for tests. In production it is None and fetch_users() is used.
|
||||
DRY RUN (D22, OKTA_SYNC_DRY_RUN): every step above runs, including the fetch and
|
||||
both guards, but the account changes and the audit rows are skipped. The run logs
|
||||
who it would disable. Use it for the first deploy, to see the match count and the
|
||||
list before any account is touched.
|
||||
|
||||
`fetch` and `dry` are for tests. In production fetch is None and fetch_users() is
|
||||
used, and dry is None so the environment decides.
|
||||
"""
|
||||
fetch = fetch or fetch_users
|
||||
dry = dry_run() if dry is None else dry
|
||||
|
||||
# 1. The fetch. Nothing below runs unless it returned a list.
|
||||
try:
|
||||
@@ -353,20 +372,29 @@ def run_sync(db: Session, fetch: Optional[Callable[[], "list[OktaUser]"]] = None
|
||||
|
||||
planned = [(local.username, status) for local, status in plan]
|
||||
checked = len(locals_)
|
||||
for local, status in plan:
|
||||
local.is_active = False
|
||||
db.add(models.AuditLog(
|
||||
id=_gen_id("ev"), actor=SYNC_ACTOR, action=SYNC_ACTION,
|
||||
entity_type="user", entity_id=local.id, project_id=None,
|
||||
summary=local.username[:400],
|
||||
detail={"okta_status": status, "via": "okta_sync"},
|
||||
))
|
||||
db.commit()
|
||||
if not dry:
|
||||
for local, status in plan:
|
||||
local.is_active = False
|
||||
db.add(models.AuditLog(
|
||||
id=_gen_id("ev"), actor=SYNC_ACTOR, action=SYNC_ACTION,
|
||||
entity_type="user", entity_id=local.id, project_id=None,
|
||||
summary=local.username[:400],
|
||||
detail={"okta_status": status, "via": "okta_sync"},
|
||||
))
|
||||
db.commit()
|
||||
except Exception as exc:
|
||||
db.rollback()
|
||||
log.error("Okta sync FAILED, nothing was kept: unexpected %s", type(exc).__name__)
|
||||
return SyncResult(ok=False, error=f"unexpected {type(exc).__name__}")
|
||||
|
||||
if dry:
|
||||
for name, status in planned:
|
||||
log.warning("Okta sync DRY RUN would disable %r (Okta status: %s)", name, status)
|
||||
log.info("Okta sync DRY RUN done, no account was changed: %d Okta user(s), %d active "
|
||||
"local account(s) checked, %d matched, %d would be disabled",
|
||||
len(okta_users), checked, matched, len(planned))
|
||||
return SyncResult(ok=True, okta_users=len(okta_users), checked=checked,
|
||||
disabled=[name for name, _ in planned], dry_run=True)
|
||||
for name, status in planned:
|
||||
log.warning("Okta sync disabled %r (Okta status: %s)", name, status)
|
||||
log.info("Okta sync done: %d Okta user(s), %d active local account(s) checked, %d disabled",
|
||||
|
||||
Reference in New Issue
Block a user