Repository navigation
Restore realm context after importing users from dir - #52892
Conversation
There was a problem hiding this comment.
🟢 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.
|
I accidentally added a merge commit when updating the branch. I have removed it now. |
There was a problem hiding this comment.
🟢 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
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>
There was a problem hiding this comment.
@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.
|
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. |
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>
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>
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>
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>
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.importUsersreplaced 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.importRealmthen tried to access the detached session during the migration and so failed withLazyInitializationException.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.