From 1aab3719129650d2095e7b1ff06d1f7800c38121 Mon Sep 17 00:00:00 2001 From: Stefan Guilhen Date: Thu, 16 May 2024 18:30:03 -0300 Subject: [PATCH] Fix errors when importing realms with the organization feature enabled Closes #29630 Signed-off-by: Stefan Guilhen --- .../exportimport/dir/DirImportProvider.java | 3 ++ .../exportimport/util/ImportUtils.java | 2 ++ .../OrganizationEnabledExportImportTest.java | 33 +++++++++++++++++++ 3 files changed, 38 insertions(+) create mode 100644 testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/organization/admin/OrganizationEnabledExportImportTest.java diff --git a/model/storage-services/src/main/java/org/keycloak/exportimport/dir/DirImportProvider.java b/model/storage-services/src/main/java/org/keycloak/exportimport/dir/DirImportProvider.java index f01c906a11..50765380b2 100755 --- a/model/storage-services/src/main/java/org/keycloak/exportimport/dir/DirImportProvider.java +++ b/model/storage-services/src/main/java/org/keycloak/exportimport/dir/DirImportProvider.java @@ -157,6 +157,7 @@ public class DirImportProvider extends AbstractFileBasedImportProvider { KeycloakModelUtils.runJobInTransaction(factory, new ExportImportSessionTask() { @Override protected void runExportImportTask(KeycloakSession session) throws IOException { + session.getContext().setRealm(session.realms().getRealmByName(realmName)); ImportUtils.importUsersFromStream(session, realmName, JsonSerialization.mapper, fis); logger.infof("Imported users from %s", userFile.getAbsolutePath()); } @@ -168,6 +169,7 @@ public class DirImportProvider extends AbstractFileBasedImportProvider { KeycloakModelUtils.runJobInTransaction(factory, new ExportImportSessionTask() { @Override protected void runExportImportTask(KeycloakSession session) throws IOException { + session.getContext().setRealm(session.realms().getRealmByName(realmName)); ImportUtils.importFederatedUsersFromStream(session, realmName, JsonSerialization.mapper, fis); logger.infof("Imported federated users from %s", userFile.getAbsolutePath()); } @@ -182,6 +184,7 @@ public class DirImportProvider extends AbstractFileBasedImportProvider { @Override public void runExportImportTask(KeycloakSession session) { + session.getContext().setRealm(session.realms().getRealmByName(realmName)); RealmManager realmManager = new RealmManager(session); realmManager.setupClientServiceAccountsAndAuthorizationOnImport(realmRep, false); } diff --git a/model/storage-services/src/main/java/org/keycloak/exportimport/util/ImportUtils.java b/model/storage-services/src/main/java/org/keycloak/exportimport/util/ImportUtils.java index 4ae2c6fe1f..32a6227bf8 100755 --- a/model/storage-services/src/main/java/org/keycloak/exportimport/util/ImportUtils.java +++ b/model/storage-services/src/main/java/org/keycloak/exportimport/util/ImportUtils.java @@ -93,6 +93,7 @@ public class ImportUtils { RealmModel realm = model.getRealmByName(realmName); if (realm != null) { + session.getContext().setRealm(realm); if (strategy == Strategy.IGNORE_EXISTING) { logger.infof("Realm '%s' already exists. Import skipped", realmName); return false; @@ -105,6 +106,7 @@ public class ImportUtils { // TODO: For migration between versions, it should be possible to delete just realm but keep it's users model.removeRealm(realm.getId()); } + session.getContext().setRealm(null); } RealmManager realmManager = new RealmManager(session); diff --git a/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/organization/admin/OrganizationEnabledExportImportTest.java b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/organization/admin/OrganizationEnabledExportImportTest.java new file mode 100644 index 0000000000..f6c16ff876 --- /dev/null +++ b/testsuite/integration-arquillian/tests/base/src/test/java/org/keycloak/testsuite/organization/admin/OrganizationEnabledExportImportTest.java @@ -0,0 +1,33 @@ +/* + * Copyright 2024 Red Hat, Inc. and/or its affiliates + * and other contributors as indicated by the @author tags. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.keycloak.testsuite.organization.admin; + +import org.keycloak.common.Profile; +import org.keycloak.testsuite.arquillian.annotation.EnableFeature; +import org.keycloak.testsuite.exportimport.ExportImportTest; + +/** + * Tests the export/import functionality with the organization feature enabled. + * + * NOTE: When export/import of organizations is implemented and the organization feature is supported, we should either enhance + * this class or the existing ExportImportTest to check org-specific settings. + * + * @author Stefan Guilhen + */ +@EnableFeature(Profile.Feature.ORGANIZATION) +public class OrganizationEnabledExportImportTest extends ExportImportTest { +}