8000 refactor: adding a roles service for v2 by shawkins · Pull Request #55049 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

refactor: adding a roles service for v2 - #55049

Merged
vmuzikar merged 2 commits into
keycloak:mainfrom
shawkins:iss47631
Dec 3, 2026
Merged

vmuzikar merged 2 commits into
keycloak:mainfrom
shawkins:iss47631

Conversation

@shawkins
Copy link
Copy Markdown
Contributor

closes: #47631

Adds a common role service. Since the role classes hold very little state, this is just directly passing the values to the methods. If we want a better way to transfer state, then we could consider adding directly creatable versions of the model classes (like we are already doing for client models) or a service specific representation.

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

The unpaginated by-name endpoint now uses a search overload that adds a database query containing all composite IDs on every request, even when the role cache is warm.

1 open issue
What changed in this PR

Extracts shared role-management logic from the v1 REST resources so the v2 client API can manage roles without depending on v1 resources, supporting #47631.

Changes:

  • Introduces a shared RoleService for role operations and permission checks.
  • Migrates v1 resources and v2 client role reconciliation to the service, keeping admin-event handling with callers.
  • Adds regression coverage for role errors, composite permissions, and v2 client events.
File Description
tests/​base/​src/​test/​java/​org/​keycloak/​tests/​admin/​realm/​RealmRolesCRUDTest.java Tests duplicate names, rename conflicts, and unknown composites.
tests/​base/​src/​test/​java/​org/​keycloak/​tests/​admin/​authz/​fgap/​RoleResourceTypeEvaluationTest.java Tests composite-mapping permissions during role creation.
services/​src/​main/​java/​org/​keycloak/​services/​ServiceException.java Adds shared service errors with suggested HTTP statuses.
services/​src/​main/​java/​org/​keycloak/​services/​roles/​RoleService.java Centralizes role operations and authorization checks.
services/​src/​main/​java/​org/​keycloak/​services/​resources/​admin/​RoleResource.java Adds shared helpers and v1 error translation.
services/​src/​main/​java/​org/​keycloak/​services/​resources/​admin/​RoleContainerResource.java Delegates name-based role operations to the service.
services/​src/​main/​java/​org/​keycloak/​services/​resources/​admin/​RoleByIdResource.java Delegates ID-based role operations to the service.
rest/​admin-v2/​tests/​src/​test/​java/​org/​keycloak/​tests/​admin/​client/​v2/​AdminEventV2Test.java Verifies role changes emit only a client event.
rest/​admin-v2/​services/​src/​main/​java/​org/​keycloak/​services/​RolesService.java Removes the wrapper around v1 role resources.
rest/​admin-v2/​services/​src/​main/​java/​org/​keycloak/​services/​client/​scim/​ClientResourceTypeProvider.java Uses the shared service for declarative client roles.

Review effort: Balanced

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

When search is omitted, the roles-by-ID endpoint paginates an unordered HashSet stream, producing arbitrary pages instead of the name-ordered results expected by existing tests.

1 open issue
1 resolved since last review

Review effort: Balanced

@shawkins
shawkins marked this pull request as ready for review November 11, 2026 17:11
@shawkins
shawkins requested review from a team as code owners November 11, 2026 17:11
@shawkins
shawkins requested review from Pepo48 and vmuzikar and a balanced review from Copilot November 11, 2026 17:11
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

A whitespace-only search now returns all composites when pagination is omitted but remains filtered when pagination is supplied, so the cache optimization changes existing search semantics.

1 open issue
1 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 11, 2026 17:40
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

A nonempty composite-role search without pagination incorrectly returns all visible composites instead of matching roles, breaking the existing search assertion.

1 open issue
1 resolved since last review

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 11, 2026 17:47
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.

🟢 Approval recommended

No active review issues were identified for the shared role-management service refactor.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

Pepo48
Pepo48 previously approved these changes Nov 15, 2026
@Pepo48 Pepo48 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.

Other than the small thing below, it looks fine. Thanks, Steve!

Copilot AI balanced review requested due to automatic review settings November 15, 2026 21:55
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.

🟢 Approval recommended

No active review issues are blocking the shared role-management refactor.

No open issues 🎉

Review effort: Balanced

@vmuzikar vmuzikar 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.

Would be good to document eventing now works differently for v2, i.e. no role events are fired.

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.

🔵 Needs a closer look

Role additions and removals lose admin audit events under default settings because the replacement CLIENT event is gated by a disabled property, and the tests only cover that property enabled.

1 open issue

Review effort: Balanced

@shawkins
shawkins requested a review from vmuzikar November 23, 2026 14:48
Comment on lines 50 to 54
protected void deleteRole(RoleModel role) {
if (!role.getContainer().removeRole(role)) {
throw new NotFoundException("Role not found");
}
}
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.

Just a nit. This doesn't seem to be used anymore?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Removed

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.

The method is still there.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Sorry I should have said the the RealmRolesPartialImport.RoleHelper constructor was removed after looking at this.

Unless there is further refactoring, deleteRole is still used via the RoleHelper.

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

Role additions and removals lose audit events by default because per-role events are suppressed while replacement CLIENT events remain gated off, and the new test masks this regression by explicitly enabling the gate.

2 open issues

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings December 1, 2026 19:16
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.

🔵 Needs a closer look

Role additions and removals lose admin audit events under the default configuration because replacement client events remain disabled, and the test masks this regression by explicitly enabling them.

2 open issues

Review effort: Balanced

Comment on lines +293 to +295
Stream<RoleModel> composites = (StringUtil.isNullOrEmpty(search) && !skip && !limit
? role.getCompositesStream()
: role.getCompositesStream(search, null, null))
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.

One thing I did not notice before but is probably ok...

Before, we always called getCompositesStream(search, ...) which resulted in a JPA call that returned sorted roles. Now, we allow calling getCompositesStream() that doesn't guarantee order. But neither do we on the endpoint, so should be fine...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That is correct - I see it as preferable to use cache. It would be better still if the cache entry were ordered, but I looked into that change and it was too much for this PR.

Copilot AI balanced review requested due to automatic review settings December 2, 2026 22:37
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.

🔵 Needs a closer look

Role additions and removals lose admin audit events under default settings because replacement client events require an opt-in property that the test explicitly enables, masking the regression.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

closes: keycloak#47631

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
Copilot AI balanced review requested due to automatic review settings December 2, 2026 23:12
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.

🔵 Needs a closer look

Switching to the event-free RoleService removes role audit events while replacement client events remain disabled by default, leaving role additions and removals unaudited in normal configurations.

No open issues 🎉

Review effort: Balanced

Closes keycloak#55698

Signed-off-by: Steve Hawkins <shawkins@redhat.com>
Copilot AI balanced review requested due to automatic review settings December 3, 2026 14:34
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.

🟢 Approval recommended

No active review issues are recorded for the shared role-management refactor.

No open issues 🎉

Review effort: Balanced

@shawkins
shawkins commented Dec 3, 2026
Copy link
Copy Markdown
Contributor Author

@vmuzikar to expedite things given that it touches the same code, the eventing issue is now addressed in this PR - with a separate one for 26.8 #55791

@vmuzikar vmuzikar 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.

LGTM, thanks.

@vmuzikar
vmuzikar merged commit 29a5f19 into keycloak:main Dec 3, 2026
97 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Provide API for roles management

4 participants

0