Skip to content

Prevent master access tokens from growing with realm count - #3603

Open
wborn wants to merge 9 commits into
mainfrom
fix/master-access-token-growth
Open

wborn wants to merge 9 commits into
mainfrom
fix/master-access-token-growth

Conversation

@wborn

@wborn wborn commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Description

  • Prevent master superuser access tokens from growing with every realm and exceeding HAProxy’s request limits, which can break the Manager UI’s WebSocket connection.
  • Attach the Keycloak protocol mapper to the openremote client in the master realm on fresh installs and upgrades. The Manager checks for the mapper at startup and installs it only once.
  • Filter out realm-management roles and their audiences while keeping the stock roles scope, realm roles, and application client roles and audiences.
  • Add integration coverage for repeated mapper setup, stable token size after adding realms and refreshing the token, and preservation of custom client roles and mapper-assigned account audiences.
  • Deploy the Keycloak image containing the mapper before starting the updated Manager. Users receive smaller tokens on their next login or refresh. Depends on openremote/keycloak#46.

Fixes #3566

Checklist

  • 1. Acceptance criteria of the linked issue(s) are met
  • 2. Tests are written and all tests pass
  • 3. Changes are manually tested by you and the reviewer
  • 4. Documentation is written or updated

- Limit client roles and audiences emitted by master OpenRemote tokens
- Apply the mapper configuration during clean setup and upgrades
- Verify stable token size and migration retries against Keycloak

Fixes #3566

@ebariaux ebariaux left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Highest finding from Codex.

  • [P1] Preserve non-management client roles on upgrade — core/manager/src/main/java/org/openremote/manager/setup/MasterClientTokenMappers.java:120-121 A master user can have a role such as reports.viewer without any custom protocol mapper, so this deployment passes the customization checks. After migration, both resource_access.reports and the reports audience disappear. That breaks integrations using the token and hides menu entries that check named-client roles in ui/component/or-app/src/or-header.ts:75. Filter the generated realm-management clients, or leave deployments that rely on other client roles unchanged.

After some discussions, it suggests this approach

I’d keep the standard roles scope on master/openremote and exclude only the clients Keycloak creates to manage realms. The current code keeps two named clients, openremote and account. That’s why an unrelated client such as reports disappears. Current mapper configuration

I’d implement the exclusion when Keycloak issues the token. A custom protocol mapper attached to master/openremote would filter the user’s resolved client roles before Keycloak uses them to populate resource_access and aud. Keycloak exposes each realm’s actual management client, so the filter can identify it reliably. A name check like endsWith("-realm") could also match a customer’s application. This approach requires deploying a mapper to Keycloak because the built-in client-role mapper supports one named client or all clients. Keycloak realm model (https://github.com/keycloak/keycloak/blob/26.7.0/server-spi/src/main/java/org/keycloak/models/RealmModel.java#L636) · Client-role mapper (https://github.com/keycloak/keycloak/blob/26.7.0/services/src/main/java/org/keycloak/protocol/oidc/mappers/UserClientRoleMappingMapper.java#L100) · Audience resolver (https://github.com/keycloak/keycloak/blob/26.7.0/services/src/main/java/org/keycloak/protocol/oidc/mappers/AudienceResolveProtocolMapper.java#L130)

The regression test would make the intended behavior concrete. After setup, create a reports client, grant reports.viewer to a master admin, create an acme realm, and request a fresh token:

Token field Expected result
realm_access.roles Still contains admin
resource_access Contains reports with viewer. Omits acme-realm
aud Contains reports. Omits acme-realm

Creating reports after setup proves future integrations work too. If the fix needs to ship entirely in the Manager, the smaller alternative is to make compaction opt-in for existing deployments. That protects existing integrations on upgrade, at the cost of continued token growth until the operator opts in.

The openremote/keycloak image already ships two custom Keycloak providers this way. Its Dockerfile copies the OpenRemote theme provider and the self-registration event listener into /opt/keycloak/providers/, runs kc.sh build, and copies the result into the runtime image. The token mapper can be a third JAR in that same build. OpenRemote Keycloak Dockerfile (https://github.com/openremote/keycloak/blob/main/Dockerfile)

I’d split the work across the two repositories:

  1. In openremote/keycloak, ship the mapper. Build it against the Keycloak version used by the image, register its Java class in META-INF/services/org.keycloak.protocol.ProtocolMapper, and copy its JAR alongside the existing providers before kc.sh build. Publish a new openremote/keycloak image tag. At that point Keycloak knows how to run the mapper. Keycloak protocol mapper registration (https://github.com/keycloak/keycloak/blob/26.7.3/services/src/main/resources/META-INF/services/org.keycloak.protocol.ProtocolMapper)
  2. In openremote/core, enable it for master/openremote. On clean setup or upgrade, the Manager uses the Keycloak Admin API to attach the mapper directly to the openremote client in the master realm. The standard roles scope stays attached. This client-level setting determines where the mapper runs, even though every realm uses the same image. Existing setup entry point

For a customer using our image, deployment is a normal image upgrade. Roll out the new Keycloak tag to all replicas first, then deploy the Manager version that enables the mapper. Compose selects the image through KEYCLOAK_VERSION, and Helm exposes image.tag. The customer needs no separate JAR installation or Admin Console step. Compose configuration · Helm configuration

For customers running their own Keycloak image, I’d have the Manager check whether the server advertises the mapper before attaching it. If it’s missing, the existing tokens keep working with their current claims. The Manager should retry on a later startup so upgrading Keycloak afterward also enables the fix.

With this, 2 other findings would still apply

  • [P2] Detect mappers that change roles without setting claim.name — core/manager/src/main/java/org/openremote/manager/setup/MasterClientTokenMappers.java:211-216
    Keycloak’s hardcoded-role mapper (https://github.com/keycloak/keycloak/blob/26.7.0/services/src/main/java/org/keycloak/protocol/oidc/mappers/HardcodedRole.java) uses role, not claim.name, so this guard misses it. If that mapper supplies a user’s only account.view-profile role, the stock Audience Resolve mapper includes account, but the replacement scope checks the user’s assigned roles and withholds the audience. The role remains in the token while account profile requests fail. Treat role-transforming mappers as customizations before detaching the stock scope.

  • [P2] Undo only Keycloak state created by this test — core/test/src/test/groovy/org/openremote/test/security/IdentityProviderTest.groovy:140-146
    If provisioning leaves roles attached, the first assertion fails before the test adds the scope, but cleanup still detaches it. That can remove the shared client’s only source of role claims and cause unrelated authorization failures in later specs. The fixed-name realms are likewise deleted even if they existed before the feature ran. Track which changes the feature actually made and undo only those, as required for the reused container by AGENTS.md:33.

ebariaux and others added 8 commits November 30, 2026 14:51
- Attach the mapper to the master OpenRemote client when Keycloak provides it
- Keep the stock roles scope and remove the scope rewrite and Flyway migration
- Cover repeated startup configuration, custom client roles, and stable token size after refresh

Relates to #3566
- Explain why the mapper filters realm management roles and audiences
- Describe when the Manager attaches it on fresh installs and upgrades

Relates to #3566
- Verify setup skips configuration when Keycloak lacks the mapper or the master OpenRemote client is missing
- Verify mapper creation reports HTTP 403 and closes the Keycloak response

Relates to #3566
- Define constants for the access token and introspection claim keys
- Serialize the enabled flags as booleans and align the mapper Javadoc with Spotless

Relates to #3566
@wborn
wborn requested a review from ebariaux December 2, 2026 14:38
@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug Error or issue in the system

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Master super user access token grows with every realm and breaks websocket connections

2 participants