Repository navigation
Conversation
- 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
left a comment
There was a problem hiding this comment.
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.viewerwithout any custom protocol mapper, so this deployment passes the customization checks. After migration, bothresource_access.reportsand thereportsaudience disappear. That breaks integrations using the token and hides menu entries that check named-client roles inui/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:
- In
openremote/keycloak, ship the mapper. Build it against the Keycloak version used by the image, register its Java class inMETA-INF/services/org.keycloak.protocol.ProtocolMapper, and copy its JAR alongside the existing providers beforekc.sh build. Publish a newopenremote/keycloakimage 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) - In
openremote/core, enable it formaster/openremote. On clean setup or upgrade, the Manager uses the Keycloak Admin API to attach the mapper directly to theopenremoteclient in themasterrealm. The standardrolesscope 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.
- 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
|



Description
openremoteclient in themasterrealm on fresh installs and upgrades. The Manager checks for the mapper at startup and installs it only once.rolesscope, realm roles, and application client roles and audiences.Fixes #3566
Checklist