8000 Restore realm context after importing users from dir by Haeata-Ash · Pull Request #52892 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content

Restore realm context after importing users from dir - #52892

Merged
shawkins merged 1 commit into
keycloak:mainfrom
Haeata-Ash:main
Oct 14, 2026
Merged

shawkins merged 1 commit into
keycloak:mainfrom
Haeata-Ash:main

Conversation

@Haeata-Ash
@Haeata-Ash Haeata-Ash commented Sep 17, 2026 •
Copy link
Copy Markdown
Contributor

Closes #52870

When importing a realm from a directory that contained both a realm and user json file, json migrations older than 24.0.0 log an error and fail silently. The unmanagedAttributePolicy was not being ENALBED and the hmac-generated-hs512 key was not being created.

It failed because DirImportProvider.importUsers replaced the session's current realm with one loaded in the batch user-import (#37926). The new session was subsequently detached at the end of the user import. RealmManager.importRealm then tried to access the detached session during the migration and so failed with LazyInitializationException.

The previous realm is now restored after the users are imported. The fix was tested by checking that unmanagedAttributePolicy is correctly ENABLED when a realm and user json file are imported and the realm file has a version lower than 24.0.0.

Added ExportImportTest#testDirImportMigrationWithUsersFile.

@Haeata-Ash
Haeata-Ash requested a review from a team as a code owner September 17, 2026 10:27
Copilot AI balanced review requested due to automatic review settings September 17, 2026 10:27
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

The focused context-restoration fix is protected by a regression test reproducing the reported migration scenario.

Pull request overview

Restores the realm context after batched directory user imports, preventing migration failures caused by detached realm models.

Changes:

  • Saves and restores the previous realm context during user import.
  • Adds regression coverage for realm migration with a separate users file.
  • Adds legacy realm and user import fixtures.
File summaries
File Description
model/storage-services/src/main/java/org/keycloak/exportimport/dir/DirImportProvider.java Restores the prior realm context after batched user import.
tests/base/src/test/java/org/keycloak/tests/exportimport/ExportImportTest.java Verifies user import and migration completion.
tests/base/src/test/resources/org/keycloak/tests/exportimport/dir-import-migration-realm.json Provides a pre-24.0.0 realm fixture.
tests/base/src/test/resources/org/keycloak/tests/exportimport/dir-import-migration-users-0.json Provides the associated user fixture.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 0
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Copilot AI review requested due to automatic review settings September 18, 2026 02:44
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

The context restoration is correctly scoped with finally, and the regression test covers the reported failure path.

Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@Haeata-Ash
Copy link
Copy Markdown
Contributor Author

I accidentally added a merge commit when updating the branch. I have removed it now.

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

The focused fix correctly preserves the managed realm context and includes regression coverage for the reported failure.

Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 0 new
  • Review effort level: Balanced

@mposolda mposolda assigned mposolda and unassigned mposolda Sep 21, 2026
Batch importing of users would detach the session's realm at the end,
so JSON migrations (MigrateTo24_0_0) failed with LazyInitializationException
when a users file was present.

Closes keycloak#52870

Signed-off-by: Haeata <haeata.ash@pm.me>
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 review overview

🟢 Approval recommended

The realm context is safely restored in a finally block and the reported regression is covered by an integration test.

Review effort: Balanced
Findings: None

@vmuzikar
vmuzikar requested a review from shawkins October 5, 2026 13:26
@shawkins shawkins 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.

@Haeata-Ash the change looks good, thank you for the fix.

@mposolda do you want to have a look as well?

I think we can probably clean things up a bit wrt realm being associated with the session context later.

@Haeata-Ash
Copy link
Copy Markdown
Contributor Author

Awesome, let me know if there is anything you need me to do before its merged in!

@shawkins
Copy link
Copy Markdown
Contributor

Awesome, let me know if there is anything you need me to do before its merged in!

Should be good, merging now. Since the fix is very targeted I'll look into creating backports.

@shawkins
shawkins merged commit b3032aa into keycloak:main Oct 14, 2026
94 checks passed
stianst pushed a commit that referenced this pull request Oct 15, 2026
Batch importing of users would detach the session's realm at the end,
so JSON migrations (MigrateTo24_0_0) failed with LazyInitializationException
when a users file was present.

Closes #52870


(cherry picked from commit b3032aa)

Signed-off-by: Haeata <haeata.ash@pm.me>
Co-authored-by: Haeata-Ash <57158452+Haeata-Ash@users.noreply.github.com>
stianst pushed a commit that referenced this pull request Oct 15, 2026
Batch importing of users would detach the session's realm at the end,
so JSON migrations (MigrateTo24_0_0) failed with LazyInitializationException
when a users file was present.

Closes #52870


(cherry picked from commit b3032aa)

Signed-off-by: Haeata <haeata.ash@pm.me>
Co-authored-by: Haeata-Ash <57158452+Haeata-Ash@users.noreply.github.com>
PadzerButch pushed a commit to PadzerButch/keycloak that referenced this pull request Oct 22, 2026
Batch importing of users would detach the session's realm at the end,
so JSON migrations (MigrateTo24_0_0) failed with LazyInitializationException
when a users file was present.

Closes keycloak#52870

Signed-off-by: Haeata <haeata.ash@pm.me>
PadzerButch pushed a commit to PadzerButch/keycloak that referenced this pull request Oct 23, 2026
Batch importing of users would detach the session's realm at the end,
so JSON migrations (MigrateTo24_0_0) failed with LazyInitializationException
when a users file was present.

Closes keycloak#52870

Signed-off-by: Haeata <haeata.ash@pm.me>
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: JSON representation migration fails with LazyInitializationException when a users file is present (regression in 26.3)

4 participants

0