Skip to main content

Keycloak Configuration

The ORT Server uses Keycloak for authentication and for user management. To ensure that the ORT Server can be used with a Keycloak instance, the following configuration steps are required.

info

The authentication flows used in this document are officially supported by the ORT Server. There is no guarantee that other authentication flows will work correctly, and they are not tested. If you need support for an authentication flow that is not listed here, please open an issue.

Clients​

It is recommended to create the following clients in Keycloak. While a single client could be used for all purposes, separating clients allows a more flexible and secure configuration. An example for this configuration can be seen in the Docker Compose setup.

ORT Server Backend​

This client is used for authentication by the ORT Server backend to access the Keycloak Admin API. It is recommended to use the "Service account roles" flow in Keycloak which corresponds to the "Client credentials" flow in OAuth2. This allows the ORT Server to authenticate with Keycloak without the need for a user account.

  • Default client ID: ort-server-backend
  • Client authentication: Enabled
  • Authentication flow: Service account roles
  • Client scopes: Default, plus kc-admin-api (required since Keycloak 26.8.2 to access the Admin REST API)
  • Service account roles: default-roles-<realm-name>, admin

Required configuration for the core application:

Environment variableDescription
KEYCLOAK_BASE_URLThe base URL of the Keycloak instance.
KEYCLOAK_REALMThe Keycloak realm to use.
KEYCLOAK_CLIENT_IDThe client ID of this client.
KEYCLOAK_API_USERSet to an empty string to use the service account instead of a user account.
KEYCLOAK_API_PASSWORD (secret keycloak.apiSecret)The client secret of this client.

Alternatively, the client can be configured to use the "Direct access grants" flow, which corresponds to the "Resource owner password credentials" flow in OAuth2. This allows the ORT Server to authenticate with Keycloak using a user account, but requires the user to have the admin role. For this, the KEYCLOAK_API_USER must be set to the username instead of an empty string, and the KEYCLOAK_API_PASSWORD must be set to the password of the user.

ORT Server API​

This client is not used for authentication but for the audience claim in the access tokens issued by Keycloak. The ORT Server API validates that the audience claim in the access token matches the client ID of this client.

  • Default client ID: ort-server-api
  • Client authentication: Enabled
  • Authentication flow: None
  • Client scopes: None

Required configuration for the core application:

Environment variableDescription
JWT_ISSUERThe expected issuer claim in the access tokens issued by Keycloak. This is usually the base URL of the Keycloak instance followed by /realms/<realm-name>.
JWT_URIThe URI where Keycloak exposes the public keys for token validation. This is usually the base URL of the Keycloak instance followed by /realms/<realm-name>/protocol/openid-connect/certs.
JWT_AUDIENCEThe expected audience claim in the access tokens issued by Keycloak. This must be set to the client ID of this client.
JWT_REALMThe Keycloak realm to use. This is used for token validation and must match the realm used in the KEYCLOAK_REALM variable.

Client scope​

It is recommended to create a client scope ort-server-api-access to easily include the correct audience claim in the access tokens issued by Keycloak for other clients. The client scope needs the following protocol mapper:

  • Mapper type: Audience
  • Included client audience: ort-server-api
  • Add to access token: On

ORT Server UI​

This client is used for authentication by the ORT Server UI. Currently, the UI only supports the "Standard flow" which corresponds to the "Authorization code" flow in OAuth2.

  • Default client ID: ort-server-ui
  • Client authentication: Disabled
  • Authentication flow: Standard flow
  • PKCE method: S256
  • Client scopes: basic, email, profile, ort-server-api-access
  • Valid redirect URIs: The URL where the UI is hosted followed by /*, for example http://localhost:8082/*.
  • Web origins: Use + to allow all valid redirect URIs.

The remaining default scopes are not required and can be removed to minimize the token size.

Required configuration for the ui application:

Environment variableDescription
UI_AUTHORITYThe URL of the Keycloak realm to use for authentication. This is usually the base URL of the Keycloak instance followed by /realms/<realm-name>.
UI_CLIENT_IDThe client ID of this client.

ORT Server CLI​

This client is used for authentication by the ORT Server CLI. The client supports both:

  • "Direct access grants" (Resource Owner Password Credentials) for the CLI

  • "Standard flow" (Authorization Code with PKCE) for the UI's token generation page

  • Default client ID: ort-server-cli

  • Client authentication: Disabled

  • Authentication flow: Direct access grants, Standard flow

  • PKCE method: S256

  • Client scopes: basic, email, profile, ort-server-api-access, offline_access

  • Valid redirect URIs: <ORT_SERVER_UI_URL>/profile/token/callback

  • Web origins: Use + to allow the UI origin of the valid redirect URIs.

The remaining default scopes are not required and can be removed to minimize the token size. For the UI-based token generation flow, the UI requests the offline_access scope explicitly to receive an offline token.

Required configuration for the core application:

Environment variableDescription
CLI_KEYCLOAK_BASE_URLThe base URL of the Keycloak instance.
CLI_KEYCLOAK_REALMThe Keycloak realm to use.
CLI_KEYCLOAK_CLIENT_IDThe client ID of this client.

With this configuration, the CLI can auto-detect the necessary information to authenticate with Keycloak via the API.

Additional clients​

If additional applications need to authenticate with Keycloak to access the ORT Server API, it is recommended to create separate clients for each application and include the ort-server-api-access client scope to ensure that the correct audience claim is included in the access tokens issued by Keycloak. For testing purposes, it is also possible to use the ort-server-cli client, as it uses the "Direct access grants" flow which allows authentication with a user account.

Synchronizing users​

Users can be deleted or renamed in Keycloak without the ORT Server being informed. The maintenance task sync-keycloak-users brings the users and role assignments stored by the ORT Server in line with Keycloak. Each run of the task:

  • Stores every user of the realm with their username and full name. The full name is the first and last name joined by a space.
  • Marks stored users that no longer exist in Keycloak as deleted. Their username and full name are kept, so that the runs they started still show who started them.
  • Removes all role assignments of usernames that no longer exist in Keycloak, including superuser roles. A user who was renamed in Keycloak therefore loses the roles assigned under the old username, and needs them assigned again.
  • Fills in the Keycloak ID of the user in role assignments that have none. Role assignments made before the ORT Server stored user IDs have none.
  • Removes role assignments stored with the ID of another user than the one who has the username now. This happens if a user with roles is deleted in Keycloak and another user is later created with the same username: the new user does not get the roles of the deleted user.
  • Records the time of the run, if it completed successfully.

Users who are disabled in Keycloak are treated like all other users: they are stored and keep their roles.

The task connects to Keycloak with the ORT Server Backend client and uses the same KEYCLOAK_* environment variables as the core application. It needs no further permissions. It requests the users from Keycloak in pages of 5000 users; to change this number, set the USER_SYNC_PAGE_SIZE environment variable.

Scheduling​

Run the task regularly, for example once an hour. Two runs must not overlap. The Helm chart creates a Kubernetes cron job for each entry in tasks.configs and does not start a new run while the previous one is still running:

tasks:
configs:
- name: sync-keycloak-users
schedule: '0 * * * *'
maxDeletedUsers: 5
maxDeletedShare: 0.1

To run the task in Docker Compose, see Run Maintenance Tasks.

Safety checks​

If Keycloak is misconfigured, it may return too few users, and the task would remove the roles of users who still exist. To prevent this, the task checks the list of users from Keycloak before it changes anything, and stops without changes if:

  1. Keycloak returns no users, but the ORT Server has stored users or role assignments. This usually means that the task connects to the wrong realm.
  2. More stored users would be marked deleted than the limit allows.
  3. More usernames would lose their role assignments than the limit allows. This counts the usernames that no longer exist in Keycloak and the usernames with role assignments of another user. This check applies only after the task has completed successfully once. The first run is expected to remove many role assignments, because older versions of the ORT Server did not remove the role assignments of deleted users.

The limit is either maxDeletedUsers or the share maxDeletedShare of the stored users, whichever is larger. With the default values, the limit is 5 users for up to 50 stored users, and 20 users for 200 stored users. Check 3 computes the limit in the same way from the number of usernames with role assignments, counting each username once.

Environment variableDefaultDescription
USER_SYNC_MAX_DELETED_USERS5The number of users that a single run may remove in any case.
USER_SYNC_MAX_DELETED_SHARE0.1The share of all stored users that a single run may remove, as a number between 0 and 1. 0.1 means 10 %.

Before the task marks a user as deleted or changes the role assignments of a username, it asks Keycloak about this user once more. Only users that Keycloak reports as not existing are marked as deleted or lose all their roles. The user ID that the task fills in, or compares role assignments with, always comes from this answer, and not from the list of all users. After these answers, the task applies check 3 again. If Keycloak cannot be reached, or any of its answers is an error, the task stops without changes.

When a check stops the task, the run fails with an error that contains the numbers that exceeded the limit. To make a bigger cleanup than the limits allow, raise the limits for a single run of the task. The task does not provide a setting to skip the checks.

Errors​

A run that fails, or that a safety check stops, ends with exit code 1, so Kubernetes shows its job as failed. The termination message of the pod names the reason, for example the check that stopped the run and the numbers that exceeded the limit. kubectl describe pod shows it as the message of the terminated container. The last line of the log of the task names the reason as well.

The Helm chart does not retry a failed run; the next run starts at the next scheduled time. To run the task at once after fixing the cause, create a job from its cron job, for example kubectl create job --from=cronjob/<cron job name> <job name>. kubectl get cronjobs lists the names of the cron jobs.

In addition, each run writes an entry with the status success or failure to the job status log. Entries of successful runs also include the number of stored users, new users, users marked as deleted, removed role assignments, and role assignments that got a user ID filled in.

Upgrading​

A later release of the ORT Server will look up roles by user ID and will not start while any role assignment has no user ID. Run the task right after upgrading to a release that stores user IDs in role assignments, and make sure that it completes successfully at least once before upgrading to the release that looks up roles by user ID.

Token lifespan​

To configure the lifespan of access and refresh tokens, or how long an offline token is valid, see the following sections of the Keycloak documentation: