Aller au contenu principal

authz

Role: the single authorization decision point. It answers Check, CheckMany, LookupResources, EffectivePermissions, SubjectRoles, LookupSubjects and AuthenticateAPIKey over internal RPC, and exposes a REST API for roles, grants, assignments, API keys, login history and organization / site settings. MongoDB is the source of truth; Permify evaluates relationship checks from tuples that authz derives from MongoDB. See the authorization model.

Limits: it never sees resource data. Callers pass the target ARN and its parents (site, project) and authz turns them into contextual tuples, so other services never write tuples. It does not authenticate users (the gateway does, see identity); it only validates raw API keys for the gateway.

Decisions​

StepBehaviour
Unknown permissionREASON_UNKNOWN_PERMISSION (not in pkg/catalog)
Service principalkind=service with an ID is always allowed (Lookup returns all, EffectivePermissions the whole catalogue)
Target resolutionARN + parents → Permify entity, permission and contextual tuples; an unresolvable target is denied
Holdersuser → the user; API key → the key, plus its owner when user-owned (both must be allowed: key grants ∩ owner permissions). Revoked, expired or unknown keys hold nothing
Default roleDEFAULT_ROLE_FOR_USERS (global role name) added as a contextual membership for every user holder; lookup cached 30 s
Gateallowed but Elevated without admin mode → REASON_ADMIN_MODE_REQUIRED; Sensitive without step-up (ACR == STEP_UP_ACR and auth_time within STEP_UP_MAX_AGE) → REASON_STEP_UP_REQUIRED

LookupResources and EffectivePermissions are computed from the MongoDB grants (role grants, default role, key grants), not from Permify. For API keys the key scope is intersected with the owner scope. LookupResources returns an empty scope when the gate would refuse the permission. SubjectRoles lists the global and project roles of a user (assignments, Keycloak-synced roles, default role) or of an API key (its own assignments only); events uses it for role invitations. LookupSubjects (service principals only) lists the users whose roles grant a permission on a resource, from the same grants; notifications uses it to resolve recipients.

The REST handlers use the same authorizer in process (no RPC round trip).

Permify synchronisation​

  • At startup: create the tenant (10 retries, 1 s apart), write the schema generated from the catalogue, seed, then reconcile.
  • Incremental writes failing are logged and schedule a reconciliation (MongoDB already holds the change).
  • Reconciliation reads every tuple of every schema entity type and applies the diff with the desired state (all non-deleted roles' grants, active assignments, active keys' grants). A mutex serialises it with writes.
  • Checks pass the latest snap token and the schema version (depth 20).

Background jobs​

  • Seed (idempotent, never overwrites): organization settings, sites paris and strasbourg, system roles Bureau, Trésorier, Coordinateur, Lab Manager, Membre, Invité (French data). Bureau maps to Keycloak role intranet-admin, Trésorier to intranet-treasurer. In development the dev admin user (00000000-0000-4000-8000-000000000001) is assigned Bureau.
  • Keycloak sync (only when KEYCLOAK_URL is set), every KEYCLOAK_SYNC_INTERVAL: for each role with a keycloak_role, assigns missing members and removes stale keycloak-sourced assignments (manual ones are kept). When KEYCLOAK_LOGIN_EVENTS is on, imports LOGIN / LOGIN_ERROR admin events since a watermark (first run: last 24 h). The service account needs view-users and view-events.

Dependencies​

  • Services: none called. Callers: gateway and every service through authzclient (see internal RPC).
  • Infrastructure: MongoDB (replica set, transactions), Permify (gRPC), Kafka, Keycloak admin API (optional).
  • Libraries: catalog, arn, principal, eventstore, outbox, audit, cloudevent, kafkax, mongox, httpx, problem, ids, config, logging, telemetry (see libraries).

Inputs and outputs​

KindNameNotes
HTTPGET /v1/public/settingsanonymous; organization and sites, contact email as image path and spoken FR/EN labels
HTTPGET /v1/public/settings/contact-email.svganonymous; configured contact address as SVG outlines, cached five minutes
HTTP/v1/authz/*permissions catalogue, me/*, roles, project roles, role directory, API keys, logins, assignments, settings: API reference
RPC inauthz.v1.AuthzService/*same port as REST, see internal RPC
Kafka outauthz.role.v1created, updated, grants_replaced, deleted (ID, name, scope, project, Keycloak role, grants)
Kafka outauthz.assignment.v1created, removed (ID, role, subject kind / ID, source); consumed by members (history)
Kafka outauthz.apikey.v1created, rotated, revoked (ID, key_id, prefix, owner, grants, CIDRs, expiry; never the hash); consumed by the gateway
Kafka outaudit.records.v1authz.role.create/update/delete/assign/unassign, authz.apikey.create/rotate/revoke, settings.organization.update, settings.site.manage, settings.site.delete
Kafka ingateway.session.v1created, failed → login history (source gateway, user agent ≤ 200 chars)
Kafka inmembers.member.v1erased → revoke the user's API keys, remove their assignments, delete their login history

Events and audit records go through the transactional outbox (see Kafka and CQRS). The consumer group is authz; malformed events are logged and skipped.

Permissions checked by the REST API​

PermissionUsed for
authz.role.read / create / update / delete / assignglobal roles, grants, assignments
projects.project.manage_rolesevery /projects/{id}/roles route
projects.apikey.manage/projects/{id}/api-keys
authz.apikey.read, authz.apikey.revoke (elevated)all keys, admin revocation
authz.login.read (sensitive)/v1/authz/logins
settings.organization.update, settings.site.manage, settings.site.deletesettings writes

/v1/authz/me/* routes need an authenticated user principal only.

GET /v1/authz/directory/roles?scope=global|project&project_id= lists role cards (id, name, scope, project_id; no grants, no assignments) for pickers such as event invitations. Global roles need an authenticated user only (API keys and services need authz.role.read). Project roles are returned only when the caller holds projects.project.read on the project or is assigned one of its roles; otherwise 403.

Data (MongoDB, database authz)​

CollectionKey fieldsIndexes
rolesname, description, scope (global/project), project_id, keycloak_role, system, grants[] (permission, resource pattern), version, deleted_at (soft delete)(name, project_id), (scope, project_id, _id), keycloak_role
assignmentsrole_id, subject_kind (user/api_key), subject_id, source (manual/keycloak), active, removed_atunique (role_id, subject_kind, subject_id) where active; (subject_kind, subject_id, active); (role_id, active, _id)
api_keysprefix, hash (HMAC-SHA256 with the pepper), owner_kind, owner_id, name, grants[], allowed_cidrs, expires_at, last_used_at (updated at most once a minute), revoked_atunique prefix; (owner_kind, owner_id, _id)
loginsevent_id, user_id, time, ip, user_agent, outcome, reason, source (gateway/keycloak)unique event_id; (user_id, _id)
settingsorganization document, watermark:keycloak_events
sites_id, name, address
eventsevent store (roles, assignments, keys)unique (aggregate_id, version); (type, _id)
outboxpending Kafka messages

API keys have the form atl_<prefix>_<secret> (8-character prefix, 32 random bytes in base32); only the hash and the prefix are stored, the raw key is returned once at creation or rotation.

Owner kinds: user, project and device. Device keys are issued, rotated and revoked only by the devices service principal through /internal/v1/devices/{id}/api-keys (never routed by the gateway) and cannot hold grants: they authenticate device agents, which the devices service authorizes by key owner.

Permify schema​

Generated from the catalogue at startup (internal/schema):

EntityRelations / permissions
user, api_keynone
role, project_rolemember @user @api_key
organization (ID main)per permission: <relation>_grant, permission = grant
siteorganization; permission = grant or organization.<relation>
projectorganization, site; project-defined permissions = grant or site or organization
<service>_<type>organization, site, project (project resource types); per action: <action>_grant, permission = grant or site or organization (or project for project permissions)

Grant subjects are role#member and api_key, plus project_role#member for project-scoped permissions. The catalogue types settings_organization, settings_site and projects_project map to the structural organization, site and project entities.

Configuration​

VariableDefaultDescription
HTTP_ADDR:8081Listen address (REST and ConnectRPC)
PRINCIPAL_PUBLIC_KEYrequiredBase64 Ed25519 key verifying principal tokens
MONGO_URI, MONGO_DATABASEmongodb://localhost:27017/?directConnection=true, authzSource of truth
KAFKA_BROKERSlocalhost:19092Events, audit records, consumed topics
PERMIFY_ADDRlocalhost:3478Permify gRPC endpoint
PERMIFY_TENANTt1Permify tenant (created if missing)
PERMIFY_API_KEYemptyBearer token sent to Permify
AUTHZ_APIKEY_PEPPERdev pepper outside productionHMAC pepper for API key hashes; required in production
DEFAULT_ROLE_FOR_USERSemptyName of a global role implicitly held by every user
STEP_UP_ACR, STEP_UP_MAX_AGEpasskey, 15mStep-up requirement for sensitive permissions
AUTHZ_RECONCILE_INTERVAL5mFull Permify reconciliation period
KEYCLOAK_URLemptyEnables the Keycloak sync
KEYCLOAK_REALMatelierRealm
KEYCLOAK_CLIENT_ID, KEYCLOAK_CLIENT_SECRETemptyService account; required with KEYCLOAK_URL
KEYCLOAK_SYNC_INTERVAL5mRole sync and login import period
KEYCLOAK_LOGIN_EVENTStrueImport Keycloak login events

Intervals must be positive. Plus the common APP_ENV, LOG_LEVEL, METRICS_ADDR, OTEL_EXPORTER_OTLP_ENDPOINT (see environment variables).