Skip to content

Import organizations before authorization settings during realm import - #55223

Open
ig-suyas wants to merge 3 commits into
keycloak:mainfrom
ig-suyas:fix-55160-org-import-fgap
Open

ig-suyas wants to merge 3 commits into
keycloak:mainfrom
ig-suyas:fix-55160-org-import-fgap

Conversation

@ig-suyas

Copy link
Copy Markdown

Closes #55160

The bug: Importing a realm export fails if it has a fine-grained admin permission (FGAP v2) on one specific organization. The import stops with Resource [<org-id>] does not exist for type [Organizations]. In DefaultExportImportManager.importRealm, the authorization settings of the admin permissions client were imported before the organizations, which came last. When that permission is imported, AdminPermissionsSchema looks up the organization by ID, and it does not exist yet.

The fix: Organizations are now imported right after users and before the authorization settings. They have to come after users because organization members are looked up by username. Nothing else moves. Nothing reacts to organization creation, so the earlier position does not change behavior. The deprecated path that skips user-dependent data (userImport == null) still imports organizations, and its authorization settings are imported after importRealm returns, as before.

Testing: I added OrganizationFgapImportTest. It creates an organization and a VIEW permission on that one organization, exports the realm on the server, deletes the realm, and imports the export again. Then it checks that the organization and the permission (with the organization as its resource) are there. Before the fix it fails with the exact error from the issue; with the fix it passes. OrganizationResourceTypeEvaluationTest (11), OrganizationTest (39) and ImportTest (6) also pass. In ExportImportTest, the directory and single-file export/import round trips pass. Six other tests in that class could not run on my Windows machine because the checkout path has a space, so their JSON fixtures were not found (FileNotFoundException on a %20 path) before any import ran. They do not depend on this change, and CI should cover them.

Fine-grained admin permissions can reference a specific organization,
which is resolved when the admin permissions client's authorization
settings are imported. Organizations were imported last, so such a
permission failed with "Resource [...] does not exist for type
[Organizations]". Organizations are now imported right after users,
and before the authorization settings.

Closes keycloak#55160

Signed-off-by: Naitik Singh <teamopyonick@gmail.com>
Covers an organization with a linked identity provider and a managed
member with a federated identity, referenced by a fine-grained admin
permission, to make sure the new import order keeps these working.

Closes keycloak#55160

Signed-off-by: Naitik Singh <teamopyonick@gmail.com>
@ig-suyas
ig-suyas marked this pull request as ready for review November 16, 2026 17:00
@ig-suyas
ig-suyas requested a review from a team as a code owner November 16, 2026 17:00
Copilot AI balanced review requested due to automatic review settings November 16, 2026 17:01

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 change to import organizations before authorization settings.

No open issues 🎉

What changed in this PR

Fixes #55160 by importing organizations before authorization settings, allowing realm imports to resolve organization-specific fine-grained admin permissions.

Changes:

  • Moves organization import after users but before authorization settings, while preserving organization import for the deprecated null-callback path.
  • Adds export/import regression tests for organization-specific permissions, including an organization linked to an identity provider with a managed member.
File Description
tests/​base/​src/​test/​java/​org/​keycloak/​tests/​organization/​authz/​fgap/​OrganizationFgapImportTest.java Adds round-trip tests for permission resource references, identity-provider links, and managed membership.
model/​storage-private/​src/​main/​java/​org/​keycloak/​storage/​datastore/​DefaultExportImportManager.java Reorders organization import so referenced organizations exist when permissions are imported.

Review effort: Balanced

@martin-kanis martin-kanis self-assigned this Nov 18, 2026
@martin-kanis
martin-kanis self-requested a review November 18, 2026 10:07

@martin-kanis martin-kanis left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@ig-suyas Thanks for the PR. The fix looks good, however I would appreciate some refactoring in the test.

Please see below.

import static org.junit.jupiter.api.Assertions.assertNotNull;

@KeycloakIntegrationTest
public class OrganizationFgapImportTest {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

No need to create yet another class. You can put it into ExportImportFGAPV2Test and only testImportRealmWithPermissionForOrganizationLinkedToIdentityProvider that covers the fix well. No need to add testImportRealmWithPermissionForSpecificOrganization as well.

assertThat(resources, contains(orgId));
}

private String createOrg(String name) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This helper method could be most likely removed and you can leverage

@InjectOrganization(lifecycle = LifeCycle.METHOD)
ManagedOrganization organization;

@ig-suyas

Copy link
Copy Markdown
Author

Thanks @martin-kanis , that makes sense. I'll move the test into ExportImportFGAPV2Test, keep only testImportRealmWithPermissionForOrganizationLinkedToIdentityProvider, remove OrganizationFgapImportTest, and use @InjectOrganization instead of the createOrg helper. I'll push the update shortly.

Keep only the test for an organization linked to an identity provider, inject the organization with @InjectOrganization and remove OrganizationFgapImportTest.

Signed-off-by: Naitik Singh <teamopyonick@gmail.com>
Copilot AI balanced review requested due to automatic review settings November 19, 2026 16:03
@ig-suyas

Copy link
Copy Markdown
Author

Done, pushed in 0072841.

  • The test now lives in ExportImportFGAPV2Test (only the identity-provider case); OrganizationFgapImportTest is removed.
  • The organization comes from @InjectOrganization; the createOrg helper is gone.
  • One addition: the class's @AfterEach removes the "fgap" realm, which the injected-realm test never creates, so that removal now tolerates NotFoundException.

Verified locally: ExportImportFGAPV2Test passes with the fix (4/4). Without the fix, the new test fails with Resource [<id>] does not exist for type [Organizations].

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 fix that imports organizations before permission settings that reference them.

No open issues 🎉

Review effort: Balanced

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.

Realm import fails when FGAP permissions reference a specific organization

3 participants