diff --git a/services/src/main/java/org/keycloak/exportimport/util/ImportUtils.java b/services/src/main/java/org/keycloak/exportimport/util/ImportUtils.java index 2ee2e11b3f..769cabb52c 100755 --- a/services/src/main/java/org/keycloak/exportimport/util/ImportUtils.java +++ b/services/src/main/java/org/keycloak/exportimport/util/ImportUtils.java @@ -33,6 +33,7 @@ import org.keycloak.models.utils.RealmImporter; import org.keycloak.models.utils.RepresentationToModel; import org.keycloak.representations.idm.RealmRepresentation; import org.keycloak.representations.idm.UserRepresentation; +import org.keycloak.services.managers.RealmManager; import java.io.IOException; import java.io.InputStream; @@ -46,10 +47,14 @@ public class ImportUtils { private static final Logger logger = Logger.getLogger(ImportUtils.class); public static void importRealms(KeycloakSession session, Collection realms, Strategy strategy) { + boolean masterImported = false; + // Import admin realm first for (RealmRepresentation realm : realms) { if (Config.getAdminRealm().equals(realm.getRealm())) { - importRealm(session, realm, strategy); + if (importRealm(session, realm, strategy)) { + masterImported = true; + } } } @@ -58,6 +63,16 @@ public class ImportUtils { importRealm(session, realm, strategy); } } + + // If master was imported, we may need to re-create realm management clients + if (masterImported) { + for (RealmModel realm : session.realms().getRealms()) { + if (realm.getMasterAdminClient() == null) { + logger.infof("Re-created management client in master realm for realm '%s'", realm.getName()); + new RealmManager(session).setupMasterAdminManagement(realm); + } + } + } } /** diff --git a/services/src/main/java/org/keycloak/services/managers/RealmManager.java b/services/src/main/java/org/keycloak/services/managers/RealmManager.java index b46a0e877b..c9a3b1ebca 100755 --- a/services/src/main/java/org/keycloak/services/managers/RealmManager.java +++ b/services/src/main/java/org/keycloak/services/managers/RealmManager.java @@ -267,7 +267,7 @@ public class RealmManager implements RealmImporter { } - private void setupMasterAdminManagement(RealmModel realm) { + public void setupMasterAdminManagement(RealmModel realm) { // Need to refresh masterApp for current realm String adminRealmId = Config.getAdminRealm(); RealmModel adminRealm = model.getRealm(adminRealmId);