DocsAPI Reference
Admin Guide

Directory lifecycle sync

Suspend Tokamak access, API keys included, when an account is disabled or deleted in Keycloak, and restore it when the account comes back.


When someone leaves, the directory is where their account is switched off. Directory lifecycle sync makes Tokamak follow it. Auth regularly reads Keycloak for every account it issued:

  • Disabled or deleted in Keycloak: the Tokamak account is suspended. API keys, CLI keys and sessions are refused from the next request, and refresh tokens are revoked.
  • Enabled again: the suspension is lifted and the same API keys work again, without being re-issued.

Keycloak federates LDAP, Active Directory, Google and others, so disabling an account in any directory Keycloak mirrors works the same way.

Turn it on

Set TOKAMAK_AUTH_IDP_SYNC_ENABLED=true on tokamak-auth. It needs the Keycloak settings, KEYCLOAK_ISSUER and the service client with view-users, which password sign-in already uses. Auth refuses to start if the sync is on and any of these is missing. The first run is 20 seconds after start, then every TOKAMAK_AUTH_IDP_SYNC_INTERVAL (default 5 minutes). Access ends within about one interval of the change in the directory.

The Keycloak admin console user details for dana@corp.test with the account switched to Disabled
Disabling an account in the Keycloak admin console. Test account on a local deployment. Open full size ↗

On a local test deployment with a 30-second interval, an API key of the account above behaved like this:

05:24:22  [email protected] disabled in Keycloak
05:24:23  API key  GET /v1/models -> 200
05:24:29  API key  GET /v1/models -> 401     # next sync run: suspended
...
05:26:06  [email protected] enabled in Keycloak
05:26:25  API key  GET /v1/models -> 401
05:26:31  API key  GET /v1/models -> 200     # next sync run: restored, same key

What it will and will not do

SituationWhat the sync does
Account disabled or deleted in KeycloakSuspends it, recording the sync as the suspender
Account the sync suspended is enabled againLifts the suspension
Account suspended by a platform administratorNever touches it, whatever Keycloak says
Administrator suspends an account the sync suspendedThe administrator now owns the suspension; the sync will not lift it
Administrator re-activates an account that is still disabled in KeycloakThe next run suspends it again. Fix the account in the directory
Agent and service accounts, other issuersSkipped

An outage changes nothing. A network error, 5xx, 429, a 401 or 403 from the admin API, or a 404 that is not Keycloak's own "user not found" stops the run before any write, recorded as directory_unavailable.

A mass suspension waits for you. If one run would suspend more than TOKAMAK_AUTH_IDP_SYNC_MAX_SUSPENSIONS_PER_RUN (25) accounts, or more than TOKAMAK_AUTH_IDP_SYNC_MAX_SUSPENSION_PERCENT (10%) of the accounts checked, it suspends nobody. It records guard_tripped and logs an error; restorations still apply. After you have looked, run it once with the override:

# $SERVICE_TOKEN: a service token with audience tokamak-auth
curl -X POST https://<auth>/api/auth/internal/idp-sync/run \
  -H "Authorization: Bearer $SERVICE_TOKEN" \
  -H 'Content-Type: application/json' \
  -d '{"allow_mass_suspension": true}'

Only one replica runs the sync at a time, and Keycloak reads are paced by TOKAMAK_AUTH_IDP_SYNC_REQUESTS_PER_SECOND.

Watch it

GET /api/auth/internal/idp-sync/status (service token, audience tokamak-auth) returns the settings in effect, the last 20 runs with their outcome and counts, and the accounts the sync currently holds suspended. Each change is written to the auth audit log with the system as the actor, action user.idp_suspended or user.idp_restored, and request_id idp-sync-run:<id>.

Metrics: tokamak_auth_idp_sync_runs_total{outcome}, tokamak_auth_idp_sync_users_total{result}, tokamak_auth_idp_sync_last_run_timestamp_seconds{outcome}, tokamak_auth_idp_sync_last_success_timestamp_seconds and tokamak_auth_idp_sync_planned_suspensions. Alert on a stale last success and on any guard_tripped run.

Settings

VariableDefaultDescription
TOKAMAK_AUTH_IDP_SYNC_ENABLEDfalseTurn the sync on
TOKAMAK_AUTH_IDP_SYNC_INTERVAL5mTime between runs; minimum 30s
TOKAMAK_AUTH_IDP_SYNC_BATCH_SIZE100Accounts read per page
TOKAMAK_AUTH_IDP_SYNC_REQUESTS_PER_SECOND20Keycloak read rate; 0 is unthrottled
TOKAMAK_AUTH_IDP_SYNC_MAX_SUSPENSIONS_PER_RUN25Absolute guard; 0 turns it off
TOKAMAK_AUTH_IDP_SYNC_MAX_SUSPENSION_PERCENT10Percentage guard; 0 turns it off
TOKAMAK_AUTH_IDP_SYNC_RUN_TIMEOUT2hTime budget for one run. A run reads every account before writing, so this must cover accounts ÷ requests per second (144,000 at the defaults). A run that runs out changes nothing

Limits

  • Accounts that sign in with Google, Microsoft or an organization's single sign-on are suspended only when their Keycloak account is disabled or deleted. Keycloak does not learn when the upstream account is disabled; disable it in Keycloak too.
  • A gateway delegation token already issued lives until it expires (about a minute); the next request is refused.
  • An account deleted and recreated in the same realm becomes a new Tokamak account, with no memberships or keys, and the old one stays suspended. Add the person back to their organizations and revoke the old account's keys.
  • Not shown in the platform console yet; use the status endpoint and metrics.
  • SCIM provisioning (account and group changes pushed by the directory) is planned, not available.

On this page