Skip to content

Fixes #32960: Sync SSO roles from the provider's id_token at the OIDC callback - #32961

Merged
mohityadav766 merged 2 commits into
mainfrom
fix/32960-oidc-role-sync
Sep 10, 2026
Merged

mohityadav766 merged 2 commits into
mainfrom
fix/32960-oidc-role-sync

Conversation

@mohityadav766

Copy link
Copy Markdown
Member

Describe your changes:

Fixes #32960

Role sync from a Keycloak custom-OIDC token stops working on 2.0.0 with useRolesFromProvider: true and a confidential client: new SSO users get none of the roles in their token, and users whose roles were revoked in Keycloak keep their old ones.

Root cause. useRolesFromProvider has always worked by reading the roles claim off the bearer token. Through 1.10.6 that bearer was the provider's id_token — AuthenticationCodeFlowHandler.sendRedirectWithToken redirected with id_token=credentials.getIdToken().getParsedString(), so JwtFilter saw Keycloak's roles on every request and UserResource#getCurrentLoggedInUser synced them.

Since #26314 (session management, in 2.0.0, not in 1.10.6) the callback mints an OpenMetadata session JWT from getRoleListFromUser(user) — the roles already in OM's database. The provider's roles are read at the callback and dropped, and the sync downstream compares OM against itself, so it never detects a change.

I applied the provider's roles at the callback, before the session token is minted, since that is the last point where the id_token claims are in hand.

Making revocation work required dropping the non-empty guard in reSyncUserRolesFromToken. That guard was load-bearing — without it, an empty role set on a deployment that never configured a roles claim would wipe every user's roles on login. So getUserRolesFromClaims now returns null for "the provider said nothing about roles" (feature off, bot, or claim absent) and a possibly-empty set for "these are the roles", and only the latter is treated as authoritative. getRolesFromAuthorizationToken reads the raw record component rather than getUserRoles(), which flattened null into an empty set.

Two related defects in the same claim read, fixed in the same pass since all three copies route through it:

  • A role emitted as a scalar string ("roles": "DataSteward") rather than a one-element array was read as no roles — Claim.asList() returns null for a scalar. Now uses the existing SecurityUtil.getClaimAsList.
  • TestLoginService.resolveRoles — the SSO test dialog customers use to verify their roles claim — had the same bug, so it would report "no roles" for exactly the configuration login would grant.

Admin remains promotion-only. Demoting on a missing admin role would lock out anyone who is an admin via adminPrincipals rather than via the provider; that is called out in the javadoc.

Type of change:

  • Bug fix

High-level design:

N/A — small change (55 lines of production code across 5 files, all on the existing role-sync path).

Tests:

Use cases covered

  • New SSO user logging in via Keycloak confidential OIDC is granted the roles in their roles claim
  • A user whose roles were revoked in Keycloak (claim present, empty) loses them in OpenMetadata on next login
  • A user whose roles changed in Keycloak has the stale ones replaced
  • A deployment with useRolesFromProvider: true but no roles claim keeps its users' roles untouched (this is the case the removed guard used to protect)
  • useRolesFromProvider: false ignores the claim entirely
  • The roles claim is matched case-insensitively, as the callback's claims map already is
  • A provider sending Admin promotes the user; a provider omitting it does not demote an adminPrincipals admin
  • Roles in the token that do not exist in OpenMetadata are skipped rather than failing the login
  • Bot tokens never pick up provider roles
  • A scalar roles claim is honoured, on both the login path and in the SSO test dialog

Unit tests

  • I added unit tests for the new/changed logic.

Files updated:

  • openmetadata-service/src/test/java/org/openmetadata/service/security/AuthenticationCodeFlowHandlerTest.java — 7 tests on the callback sync (the regression itself)
  • openmetadata-service/src/test/java/org/openmetadata/service/security/JwtFilterTest.java — 6 tests on the claim → security-context signal, including the null-vs-empty distinction
  • openmetadata-service/src/test/java/org/openmetadata/service/util/UserUtilTest.java — 7 tests on the sync semantics matrix
  • openmetadata-service/src/test/java/org/openmetadata/service/security/auth/TestLoginServiceTest.java — 2 tests on the SSO test dialog's claim read

I verified the new tests actually catch the regression rather than just passing: reverting the three production files and re-running produces 14 failures/errors across the three classes, with the rest of the suites still green.

$ mvn -pl openmetadata-service test -Dtest='org.openmetadata.service.security.**.*Test,org.openmetadata.service.util.*Test'
classes=91 tests=1241 failures=0 errors=0 skipped=0

Backend integration tests

  • Not applicable — no API endpoints added or changed. The behaviour lives in the OIDC callback servlet, which needs a live identity provider to exercise end to end; there is no OIDC IT harness in openmetadata-integration-tests/ today.

Ingestion integration tests

  • Not applicable (no ingestion changes).

Playwright (UI) tests

  • Not applicable (no UI changes).

Manual testing performed

Not performed — reproducing requires a Keycloak confidential-client deployment with a top-level roles claim mapper. The reporting customer's environment is the natural place to confirm; the behaviour is pinned by the unit tests above and by the 1.10.6-vs-2.0.0 diff in sendRedirectWithToken that identifies the regression.

UI screen recording / screenshots:

Not applicable.

Checklist:

  • I have read the CONTRIBUTING document.
  • My PR title is Fixes <issue-number>: <short explanation>
  • My PR is linked to a GitHub issue via Fixes #<issue-number> above.
  • I have commented on my code, particularly in hard-to-understand areas.
  • For JSON Schema changes: not applicable — no schema changes.
  • For UI changes: not applicable.
  • I have added tests (unit / integration / Playwright as applicable) and listed them above.

Bug fix

  • I have added a test that covers the exact scenario we are fixing. The issue number is commented in AuthenticationCodeFlowHandlerTest for future reference.

Note for reviewers

The useRolesFromProvider flag reads the literal roles claim (JWTTokenGenerator.ROLES_CLAIM); there is no jwtRoleClaimMapping counterpart to the existing jwtTeamClaimMapping. Keycloak users need a mapper that puts roles at the top level rather than under realm_access.roles — that was equally true on 1.10.6, so this PR restores parity rather than changing the contract. Adding a configurable role claim name would be a reasonable follow-up.

Separately: SAML (SamlAuthServletHandler) also mints its token from database roles and has never read a roles claim, so useRolesFromProvider does not work there either. That is a pre-existing gap rather than a 2.0 regression, so I left it out of this PR.

🤖 Generated with Claude Code

… callback

Since #26314 the OIDC confidential-client callback hands the browser an
OpenMetadata-issued session JWT instead of the provider's raw id_token. That
token carries the roles already stored in OpenMetadata, so the
useRolesFromProvider sync in JwtFilter/UserResource ends up comparing the
database against itself: new SSO users are never granted the roles in their
Keycloak token, and users whose roles were revoked in Keycloak keep theirs.

Apply the provider's roles at the callback, which is the last point where the
id_token claims are in hand, before the session token is minted.

Making revocation work meant dropping the non-empty guard in
reSyncUserRolesFromToken. That guard was load-bearing: without it an empty role
set from a deployment that never configured a roles claim would wipe every
user's roles. getUserRolesFromClaims now returns null for "the provider said
nothing about roles" and a possibly-empty set for "these are the roles", and
the sync treats only the latter as authoritative.

Also fixes the same claim read in the two other places it is duplicated: a role
emitted as a scalar string rather than a one-element array was read as no roles
at all, both on the login path and in the SSO test dialog customers use to
verify their roles claim.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@mohityadav766
mohityadav766 requested a review from a team as a code owner September 8, 2026 13:40
@github-actions

github-actions Bot commented Sep 8, 2026

Copy link
Copy Markdown
Contributor

❌ PR checklist incomplete

This PR cannot be merged until the following are addressed on its linked issue:

The fields live on the linked issue in the Shipping project (open the issue → right sidebar → Projects). After you set them, re-run this check (or push a commit) — issue/project changes do not re-trigger it automatically.

Maintainers can bypass this check by adding the skip-pr-checks label.

@github-actions github-actions Bot added backend safe to test Add this label to run secure Github workflows on PRs labels Sep 8, 2026
Comment on lines +1039 to +1046
private void syncRolesFromProvider(User user, Map<String, Object> claims) {
if (!Boolean.TRUE.equals(authorizerConfiguration.getUseRolesFromProvider())
|| !claims.containsKey(ROLES_CLAIM)) {
return;
}
UserUtil.reSyncUserRolesFromToken(
null, user, new HashSet<>(getClaimAsList(claims.get(ROLES_CLAIM))));
}

@gitar-bot gitar-bot Bot Sep 8, 2026 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Edge Case: Present-but-null roles claim collapses to "revoke all roles"

When the id_token carries a roles claim whose value is JSON null, claims.containsKey(ROLES_CLAIM) is true, so getClaimAsList(null) returns an empty list and reSyncUserRolesFromToken treats it as the authoritative empty set — revoking every role. This is the same case the PR's null-vs-empty distinction is meant to protect against ("the provider said nothing"), yet a null-valued claim is indistinguishable from a real empty revocation. Same behavior exists on the JwtFilter path via an auth0 NullClaim. Impact is low (requires a provider/mapper emitting an explicit null), but consider treating a null value as "no info" by checking the value is non-null before syncing.

Treat a null-valued roles claim as "no info" rather than an empty revocation.:

private void syncRolesFromProvider(User user, Map<String, Object> claims) {
  if (!Boolean.TRUE.equals(authorizerConfiguration.getUseRolesFromProvider())
      || claims.get(ROLES_CLAIM) == null) {
    return;
  }
  UserUtil.reSyncUserRolesFromToken(
      null, user, new HashSet<>(getClaimAsList(claims.get(ROLES_CLAIM))));
}

Was this helpful? React with 👍 / 👎

@mohityadav766 mohityadav766 self-assigned this Sep 8, 2026

This branch was previously deployed

1 inactive deployment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backend safe to test Add this label to run secure Github workflows on PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Role assignment/revocation from Keycloak custom-OIDC stops syncing to OpenMetadata

3 participants