Repository navigation
Add organization roles - #50606
Add organization roles#50606ggomarighetti wants to merge 6 commits into
Conversation
Design notes and review topicsThese are the main design choices behind the implementation. Some of them are straightforward extensions of existing organization/group patterns, while others are called out because they affect public SPI, storage semantics, or review expectations.
|
feed236 to
771cc26
Compare
|
LGTM - rather helpful, let's get it reviewed by code owners |
|
@ggomarighetti Do you mind if I squash all commits? Rebasing is becoming tricky ... |
|
@pedroigor Sure, no problem. Please go ahead and squash them. Thanks! |
e8b36f0 to
e1aa9da
Compare
Unreported flaky test detectedIf the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR. org.keycloak.testsuite.forms.BruteForceTest#testExceedMaxTemporaryLockouts |
|
@ggomarighetti Added the commit to support assigning org roles to org groups, additional validation/access checks, and tests. Regarding the change to avoid assigning default roles directly to users, feel free to change the outline. From my side, the general idea is to grant the default role to the organization's internal group so that all members of the group/org will automatically inherit the roles from the group. |
There was a problem hiding this comment.
🟡 Changes recommended
A critical export/import defect and multiple moderate authorization, inheritance, and Admin UI regressions remain unresolved.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
Suppressed comments (4)
docs/documentation/upgrading/topics/changes/changes-26_8_0.adoc:222
- This upgrade note documents the new type checks but omits the source-incompatible SPI additions: custom
RoleContainerModelimplementations must now implementgetRealm(), and customRoleProviderimplementations must implement the new generic role methods. Document these required provider changes here so extension authors can upgrade without unexpected compilation failures.
=== Generic role type checks
`RoleModel` now distinguishes realm, client and organization roles through `getType()` and the new `isType(RoleModel.Type)` convenience method.
The existing `RoleModel#isClientRole()` method remains available for compatibility but is deprecated; new code should use `isType(RoleModel.Type.CLIENT)`.
js/apps/admin-ui/src/components/role-mapping/RoleMapping.tsx:218
getRoleMappings()now returns organization mappings for users as well as groups, and the table makes those rows selectable. On a user pagegroupsResourceis undefined, so selecting an organization role reaches this false branch, performs no deletion, and still shows the success alert; either disable/hide these rows outside group mappings or implement deletion through the owning organization role resource.
...(orgRoles.length > 0 && groupsResource
? [
(groupsResource as any).delOrganizationRoleMappings({
id,
roles: orgRoles.map((row) => cleanRole(row.role)),
}),
]
: []),
js/apps/admin-ui/src/components/role-mapping/RoleMapping.tsx:165
- When inherited roles are shown,
mapRolesrenders onlyallEffectiveRoles; that backend response intentionally excludes organization roles, so all direct organization mappings inorgMappingdisappear from the table when the checkbox is unchecked. Merge the direct organization rows into the effective set before mapping.
return [
...mapRoles(
[...clientMapping, ...realmRolesMapping, ...orgMapping],
allEffectiveRoles,
hide,
),
];
server-spi-private/src/main/java/org/keycloak/organization/validation/OrganizationsValidation.java:76
- Organization groups from a different organization pass this branch: once both the group and role are organization-scoped, the method returns without comparing their owners. A direct model-layer
group.grantRole(roleFromOtherOrg)can therefore grant cross-organization access; reject organization roles whose container ID differs fromgroup.getOrganization()before returning.
- Files reviewed: 144/145 changed files
- Comments generated: 4
- Review effort level: Balanced
There was a problem hiding this comment.
🟡 Changes recommended
A critical cross-organization isolation flaw and five moderate correctness, export, token, and Admin UI issues remain unresolved.
Once you've addressed the issues Copilot identified, you can request another Copilot review.
Review details
Suppressed comments (7)
js/apps/admin-ui/src/components/role-mapping/AddRoleMappingModal.tsx:124
AddRoleButtonis reused by user, client-scope, policy, and role-composite screens, but this unconditional option is shown to everymanage-organizationsuser; those callers pass no group resource, so choosing it opens an always-empty picker. Gate this option on an explicit organization-group capability from the caller.
{canViewOrganizationRoles && (
<DropdownItem
data-testid="org-roles"
component="button"
=> {
onFilerTypeChange("orgRoles");
}}
>
{t("organizationRoles")}
</DropdownItem>
)}
js/apps/admin-ui/src/components/role-mapping/RoleMapping.tsx:218
getRoleMappingsnow returns organization mappings for users, but a user-role page has nogroupsResource; selecting such a row makes this condition false, sends no deletion request, and then shows success. These rows must be disabled outside organization-group mappings (or removed through an organization-scoped endpoint).
...(orgRoles.length > 0 && groupsResource
? [
(groupsResource as any).delOrganizationRoleMappings({
id,
roles: orgRoles.map((row) => cleanRole(row.role)),
}),
]
: []),
server-spi-private/src/main/java/org/keycloak/models/utils/ModelToRepresentation.java:255
- Organization role mappings on groups are collected here but never written to
GroupRepresentation, and the import path likewise has no organization-role field. Consequently, exporting and re-importing an organization silently drops all organization roles assigned to its groups; extend the group representation and both export/import paths to preserve this map.
services/src/main/java/org/keycloak/services/resources/admin/RoleMapperResource.java:402 - Unlike the realm-role equivalent, this available-role query does not filter through
canMapRole. Through the generic group endpoint, a caller with group-view access can therefore enumerate organization roles they are not authorized to map.
services/src/main/java/org/keycloak/services/resources/admin/RoleMapperResource.java:443 - This path checks permission to manage the group but never checks permission to map the requested role. The generic group endpoint can therefore assign an organization role without the owning organization's
canMapRoleauthorization; callrequireMapRolebefore granting it.
services/src/main/java/org/keycloak/services/resources/admin/RoleMapperResource.java:478 - Removal has the same authorization gap as addition: group-manage permission alone can remove an organization role mapping through the generic group endpoint. Require mapping permission on the role before deleting the mapping.
services/src/main/java/org/keycloak/utils/RoleResolveUtil.java:117 - Filtering organization roles here is too late for full-scope clients:
TokenManager.getAccesshas already expanded their composites, so realm/client children of an organization role remain in the stream and are emitted into top-levelrealm_access/resource_access. This leaks organization-derived privileges outside the organization claim; exclude organization-role roots before regular token role expansion while retaining their separate organization-claim resolution.
- Files reviewed: 145/146 changed files
- Comments generated: 3
- Review effort level: Balanced
|
@pedroigor assignment of default role through default group pushed. GH Copilot comments and rebase conflicts remains pending. EDIT: GH Copilot comments solved and closed, conflicts remains.. EDIT2: Conflicts solved, branch updated... EDIT3: I'm solving now idp selector conflicts... |
|
@pedroigor refactor pushed, comments solved, branch updated, remaining failed checks it's external doc, not from this PR. |
Add organization-scoped role models, persistence, admin APIs, protocol mappers, admin UI support, and integration coverage. Closes keycloak#40585 Signed-off-by: Guillermo Orue Marighetti <ggomarighetti@gmail.com>
Signed-off-by: Pedro Igor <pigor.craveiro@gmail.com>
Signed-off-by: Pedro Igor <pigor.craveiro@gmail.com>
Signed-off-by: Pedro Igor <pigor.craveiro@gmail.com>
Closes keycloak#40585 Signed-off-by: Guillermo Orue Marighetti <ggomarighetti@gmail.com>
Scope role uniqueness by container type and bound default role names. Respect client role scopes in organization claims and preserve the configured or default membership claim name. Honor listing permissions and independent pagination parameters, and show client identifiers for composite roles. Cover organization role LDAP, federation, export and SAML scenarios in the new test framework while preserving the historical tests. Refs keycloak#40585 Signed-off-by: Guillermo Orue Marighetti <ggomarighetti@gmail.com>
Unreported flaky test detectedIf the flaky tests below are affected by the changes, please review and update the changes accordingly. Otherwise, a maintainer should report the flaky tests prior to merging the PR. org.keycloak.testsuite.cluster.ClientScopeInvalidationClusterTest#crudWithoutFailoverorg.keycloak.testsuite.cluster.ClientScopeInvalidationClusterTest#crudWithFailover |
|
Thanks for your work on organization roles! Is there an estimated timeline for merging this PR, or a target Keycloak release for this feature? |
|
The current aim is to have it ready for KC27. |
|
@ggomarighetti , @pedroigor Thank you for this work. I have pulled down this code for testing and noticed an important behavior that caught my attention. Some of the below content was generated with the help of Claude, but I did test the behavior manually. Organization-derived realm/client roles ignore the selected organizationSetup: A user belongs to Result:
Expected: When the session is bound to one organization, only that organization's org roles (and their composites) should contribute to the token's roles. As it stands, an API that checks top-level Cause: Both role computations call
That method collects direct role mappings and role-mapping groups for every organization ( Docs: Possible fix: When the client session has a selected organization, drop role mappings that come from other organizations before expanding composites. That covers both direct org-role grants and roles mapped to other orgs' groups. Apply it to the token and client-scope role computation. Keep the current behavior when no org is selected. Leave Questions:
See also, proposed patch: |
|
Hi @judsonx, Thanks for the analysis and the proposed patch, and of course for the enthusiasm. To be honest, this PR ended up stalled. I made the last changes Pedro asked for and didn't hear back after that. That's understandable, since the release date we had originally agreed on was getting too close and this PR had grown much bigger than any of us expected. I didn't hear anything more from the Keycloak team, and I also have some personal matters to take care of. So I put the PR on hold until they tell me how they'd like to move forward. One idea I had was to split it into several smaller PRs and track them as tasks or sub-issues under the original issue. That idea was never decided on, so nothing moved forward. I also stopped working on the PR because of all the new features they've been shipping. Every day new issues change the code and cause conflicts with the branch. In the meantime, I've covered this feature with an extension I wrote myself. We'll see what the maintainers want to do with this when things pick back up next year. |
|
Any plans to merge this PR? @ggomarighetti is the extension you mentioned in your last comment open-source? |
Resume
This PR adds Organization Roles as a first-class role type across the Keycloak model, persistence, admin APIs, protocol mapping, export/import, admin client, and Admin UI layers.
Organization roles belong to a single organization within a realm. They are isolated from realm and client roles, can reuse role names across different organizations, and can only be mapped to members of their owning organization. Existing realm and client role behavior is preserved by making role lookups, mappings, composites, authorization checks, and representation conversion aware of the role container/type instead of assuming the previous realm/client-only split.
The implementation introduces the model contracts for organization-owned roles, persists them in the existing role storage with explicit type and organization ownership, and wires the behavior through JPA, storage-private, Infinispan cache, role lookup APIs, composite handling, user role mappings, and organization lifecycle cleanup. Organizations now also have a protected default organization role that is created with the organization, assigned through membership lifecycle, exported/imported with the organization, and protected from direct user-mapping mutations.
The admin REST surface now exposes organization-scoped role management under organizations. It supports role CRUD, users-in-role, available users, composites, effective composites, permission checks, and admin events while keeping generic realm/client role endpoints from accepting organization roles where they should remain scoped to their original containers. Fine-grained admin permissions were updated so classic FGAP and FGAP v2 can evaluate organization role access consistently.
Protocol integration adds OIDC and SAML organization role membership mappers, plus IdP mapper support for hardcoded organization role assignments. Token claims expose organization role membership under the organization claim, while realm/client composites continue to resolve through the normal role resolution and client scope rules.
Export/import support was extended so organization roles, default roles, role mappings, composites, and references survive realm and organization round trips. Upgrade/migration logic initializes the new role type/organization columns and default role relationships for existing data.
The Admin UI now includes organization role management inside the organization detail area. Users can list, create, edit, delete, inspect permissions, assign members, manage composites, and view effective inherited composites for organization roles. The JS admin client and Java admin client were extended with matching organization role resources.
The change is covered across unit, model, cache, REST, protocol mapper, export/import, admin-client, Admin UI, and browser-level tests.
Evidence
Covered by unit, integration, admin-client, and Admin UI tests added with this change.
Reference
Closes #40585