T13.4: run the Okta sync on a schedule (D18)
A daemon timer thread starts from a new lifespan hook in app.py and calls the T13.3 sync every OKTA_SYNC_INTERVAL_SECONDS (default 900, minimum 300). With no OKTA_API_TOKEN it starts nothing and the startup log says so. The api runs 2 gunicorn workers, so each starts its own timer. A single atomic conditional UPDATE on one app_settings row (key okta_sync) is the gate: exactly one worker wins each cycle, on SQLite and Postgres, with no lock held. The row survives a restart, so a restart inside the interval starts no second run. A fetch is now capped at 120 s in total, so a run is always shorter than the minimum interval and cannot overlap the next. No migration needed. docker-compose.yml passes OKTA_API_TOKEN, OKTA_ORG_URL, OKTA_SYNC_PROFILE_ATTRIBUTE and OKTA_SYNC_INTERVAL_SECONDS to the api service. tests/okta_sync_schedule_check.py: 38 checks including an 8-worker race on a file database (exactly one winner, 16 rounds) and fail-closed through the scheduled path. okta_auth_check.py still 22/22.
This commit is contained in:
@@ -10,6 +10,7 @@ Interactive docs: http://<host>/api/docs
|
||||
"""
|
||||
import base64
|
||||
import csv
|
||||
import contextlib
|
||||
import hashlib
|
||||
import hmac
|
||||
import io
|
||||
@@ -31,8 +32,8 @@ from sqlalchemy import select, delete, func, or_
|
||||
from sqlalchemy.orm import Session
|
||||
from starlette.middleware.sessions import SessionMiddleware
|
||||
|
||||
from .db import Base, engine, get_db
|
||||
from . import models, auth, notify, assets_db, okta_auth, okta_fake
|
||||
from .db import Base, SessionLocal, engine, get_db
|
||||
from . import models, auth, notify, assets_db, okta_auth, okta_fake, okta_sync
|
||||
|
||||
log = logging.getLogger("wpsuite.app")
|
||||
|
||||
@@ -47,7 +48,21 @@ if engine.dialect.name == "sqlite":
|
||||
# Interactive docs are handy in dev but hand an attacker the full API map in prod,
|
||||
# so enable them only on the SQLite dev fallback (production runs on Postgres).
|
||||
_docs_enabled = engine.dialect.name == "sqlite"
|
||||
@contextlib.asynccontextmanager
|
||||
async def _lifespan(_app):
|
||||
"""Start the Okta deprovisioning sync (D18 / T13.4) when the process starts and
|
||||
stop it when the process ends. With no OKTA_API_TOKEN it starts nothing and
|
||||
says so in the startup log."""
|
||||
stop = okta_sync.start_scheduler(SessionLocal)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
if stop is not None:
|
||||
stop.set()
|
||||
|
||||
|
||||
app = FastAPI(
|
||||
lifespan=_lifespan,
|
||||
title="Work Package Suite API",
|
||||
docs_url="/api/docs" if _docs_enabled else None,
|
||||
redoc_url=None,
|
||||
|
||||
Reference in New Issue
Block a user