Skip to content

[CIMD] Stale resource audience mapper persists after allowlist tightening - #55212

Open
tnorimat wants to merge 1 commit into
keycloak:mainfrom
Hitachi:ISSUE-55131
Open

tnorimat wants to merge 1 commit into
keycloak:mainfrom
Hitachi:ISSUE-55131

Conversation

@tnorimat

Copy link
Copy Markdown
Contributor

closes #55131

@tnorimat
tnorimat requested a review from a team as a code owner November 16, 2026 02:21
Copilot AI balanced review requested due to automatic review settings November 16, 2026 02:21

Copilot AI 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.

🟡 Changes recommended

Revoked audiences can still appear in tokens from client-credentials and refresh-token grants, and authorization-time cleanup can roll back when later checks fail.

2 open issues
What changed in this PR

This PR addresses stale CIMD resource-audience mappers described in #55131 by reconciling them against the current allowlist during authorization requests.

Changes:

  • Removes stale mappers even when resource is omitted or a still-allowed resource is requested.
  • Removes CIMD-owned audience mappers when Resource Indicators are disabled.
  • Adds regression tests for omitted-resource and alternate-resource authorization requests.
File Description
tests/​base/​src/​test/​java/​org/​keycloak/​tests/​client/​policies/​ClientIdMetadataDocumentTest.java Tests mapper cleanup after allowlist tightening.
services/​src/​main/​java/​org/​keycloak/​protocol/​oauth2/​cimd/​clientpolicy/​executor/​AbstractClientIdMetadataDocumentExecutor.java Reconciles CIMD audience mappers during authorization.

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 16, 2026 04:21

Copilot AI 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.

Copilot AI balanced review requested due to automatic review settings November 16, 2026 05:46

Copilot AI 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.

…ning

closes keycloak#55131

Signed-off-by: Takashi Norimatsu <takashi.norimatsu.ws@hitachi.com>
Copilot AI balanced review requested due to automatic review settings November 16, 2026 06:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Comment on lines +1063 to +1065
List<ClientModel> clients = session.clients().getClientsStream(realm)
.filter(c -> getResourceIndicatorAudienceMappers(c).findAny().isPresent())
.toList();
Comment on lines +1133 to +1136
.flatMap(p -> nullSafe(p.getProfiles()))
.map(allowListsByProfile::get)
.filter(Objects::nonNull)
.flatMap(List::stream)
Comment on lines +1149 to +1151
if (!ClientIdUriSchemeConditionFactory.PROVIDER_ID.equals(condition.getConditionProviderId())) {
// other conditions depend on a request, so they are regarded as possibly satisfied.
continue;
Comment on lines +81 to +83
KeycloakSession session = attributeUpdateEvent.getKeycloakSession();
AbstractClientIdMetadataDocumentExecutor.removeStaleResourceIndicatorAudienceMappers(session,
attributeUpdateEvent.getRealm(), getId(), create(session).getExecutorConfigurationClass());
Comment on lines +1063 to +1065
List<ClientModel> clients = session.clients().getClientsStream(realm)
.filter(c -> getResourceIndicatorAudienceMappers(c).findAny().isPresent())
.toList();
Comment on lines +1100 to +1118
policies = session.clientPolicy().getClientPolicies(realm, false);
} catch (ClientPolicyException e) {
RESOURCE_INDICATOR_LOGGER.warnv("client profiles or policies not loaded, so stale resource indicator audience mappers are not removed: realm = {0}, error = {1}",
realm.getName(), e.getErrorDetail());
return null;
}

Map<String, List<String>> allowListsByProfile = new HashMap<>();
Stream.concat(nullSafe(profiles.getProfiles()), nullSafe(profiles.getGlobalProfiles())).forEach(profile -> {
List<String> allowList = nullSafe(profile.getExecutors())
.filter(e -> executorProviderId.equals(e.getExecutorProviderId()))
.map(e -> (Configuration) JsonSerialization.mapper.convertValue(RawJsonValue.unwrap(JsonNode.class, e.getConfiguration()), configurationClass))
.filter(Objects::nonNull)
.flatMap(c -> nullSafe(c.getResourceIndicatorAllowList()))
.toList();
allowListsByProfile.merge(profile.getName(), allowList, (a, b) -> Stream.concat(a.stream(), b.stream()).toList());
});

List<ClientPolicyRepresentation> enabledPolicies = nullSafe(policies.getPolicies())
Comment on lines +78 to +83
if (event instanceof RealmModel.RealmAttributeUpdateEvent attributeUpdateEvent
&& (Constants.CLIENT_PROFILES.equals(attributeUpdateEvent.getAttributeName())
|| Constants.CLIENT_POLICIES.equals(attributeUpdateEvent.getAttributeName()))) {
KeycloakSession session = attributeUpdateEvent.getKeycloakSession();
AbstractClientIdMetadataDocumentExecutor.removeStaleResourceIndicatorAudienceMappers(session,
attributeUpdateEvent.getRealm(), getId(), create(session).getExecutorConfigurationClass());
Comment on lines +517 to +520
IntrospectionResponse introspectionResponse = oauth.client("test-app", "test-secret").doIntrospectionAccessTokenRequest(tokenResponse.getAccessToken());
Assertions.assertEquals(200, introspectionResponse.getStatusCode());
JsonNode introspectedAudience = introspectionResponse.asJsonNode().get(JsonWebToken.AUD);
Assertions.assertFalse(introspectedAudience != null && introspectedAudience.toString().contains(RESOURCE), "aud = " + introspectedAudience);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[CIMD] Stale resource audience mapper persists after allowlist tightening

2 participants