Skip to content

Commit

Permalink
KEYCLOAK-1716 NPE importing with empty database
Browse files Browse the repository at this point in the history
  • Loading branch information
stianst committed Jul 28, 2015
1 parent bf1f867 commit f980362
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 25 deletions.
Expand Up @@ -90,18 +90,7 @@ void readModelFile(KeycloakSession session) {
ImportUtils.importFromStream(session, JsonSerialization.mapper, fis, Strategy.IGNORE_EXISTING); ImportUtils.importFromStream(session, JsonSerialization.mapper, fis, Strategy.IGNORE_EXISTING);
session.realms().getMigrationModel().setStoredVersion(model.getModelVersion()); session.realms().getMigrationModel().setStoredVersion(model.getModelVersion());


List<RealmRepresentation> realmReps = new ArrayList<RealmRepresentation>(); ImportUtils.importRealms(session, model.getRealms(), Strategy.IGNORE_EXISTING);
for (RealmRepresentation realmRep : model.getRealms()) {
if (Config.getAdminRealm().equals(realmRep.getRealm())) {
realmReps.add(0, realmRep);
} else {
realmReps.add(realmRep);
}
}
for (RealmRepresentation realmRep : realmReps) {
ImportUtils.importRealm(session, realmRep, Strategy.IGNORE_EXISTING);
}

} catch (IOException ioe) { } catch (IOException ioe) {
logger.error("Unable to read model file " + kcdata.getAbsolutePath(), ioe); logger.error("Unable to read model file " + kcdata.getAbsolutePath(), ioe);
} finally { } finally {
Expand Down
Expand Up @@ -20,10 +20,8 @@


import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.keycloak.exportimport.ExportImportConfig; import org.keycloak.exportimport.ExportImportConfig;


/** /**
Expand All @@ -33,6 +31,21 @@ public class ImportUtils {


private static final Logger logger = Logger.getLogger(ImportUtils.class); private static final Logger logger = Logger.getLogger(ImportUtils.class);


public static void importRealms(KeycloakSession session, Collection<RealmRepresentation> realms, Strategy strategy) {
// Import admin realm first
for (RealmRepresentation realm : realms) {
if (Config.getAdminRealm().equals(realm.getRealm())) {
importRealm(session, realm, strategy);
}
}

for (RealmRepresentation realm : realms) {
if (!Config.getAdminRealm().equals(realm.getRealm())) {
importRealm(session, realm, strategy);
}
}
}

/** /**
* Fully import realm from representation, save it to model and return model of newly created realm * Fully import realm from representation, save it to model and return model of newly created realm
* *
Expand All @@ -41,15 +54,15 @@ public class ImportUtils {
* @param strategy specifies whether to overwrite or ignore existing realm or user entries * @param strategy specifies whether to overwrite or ignore existing realm or user entries
* @return newly imported realm (or existing realm if ignoreExisting is true and realm of this name already exists) * @return newly imported realm (or existing realm if ignoreExisting is true and realm of this name already exists)
*/ */
public static RealmModel importRealm(KeycloakSession session, RealmRepresentation rep, Strategy strategy) { public static void importRealm(KeycloakSession session, RealmRepresentation rep, Strategy strategy) {
String realmName = rep.getRealm(); String realmName = rep.getRealm();
RealmProvider model = session.realms(); RealmProvider model = session.realms();
RealmModel realm = model.getRealmByName(realmName); RealmModel realm = model.getRealmByName(realmName);


if (realm != null) { if (realm != null) {
if (strategy == Strategy.IGNORE_EXISTING) { if (strategy == Strategy.IGNORE_EXISTING) {
logger.infof("Realm '%s' already exists. Import skipped", realmName); logger.infof("Realm '%s' already exists. Import skipped", realmName);
return realm; return;
} else { } else {
logger.infof("Realm '%s' already exists. Removing it before import", realmName); logger.infof("Realm '%s' already exists. Removing it before import", realmName);
if (Config.getAdminRealm().equals(realm.getId())) { if (Config.getAdminRealm().equals(realm.getId())) {
Expand All @@ -73,7 +86,7 @@ public static RealmModel importRealm(KeycloakSession session, RealmRepresentatio
logger.infof("Realm '%s' imported", realmName); logger.infof("Realm '%s' imported", realmName);
} }


return realm; return;
} }


private static void refreshMasterAdminApps(RealmProvider model, RealmModel realm) { private static void refreshMasterAdminApps(RealmProvider model, RealmModel realm) {
Expand Down Expand Up @@ -145,9 +158,7 @@ public static void setupMasterAdminManagement(RealmProvider model, RealmModel re
*/ */
public static void importFromStream(KeycloakSession session, ObjectMapper mapper, InputStream is, Strategy strategy) throws IOException { public static void importFromStream(KeycloakSession session, ObjectMapper mapper, InputStream is, Strategy strategy) throws IOException {
Map<String, RealmRepresentation> realmReps = getRealmsFromStream(mapper, is); Map<String, RealmRepresentation> realmReps = getRealmsFromStream(mapper, is);
for (RealmRepresentation realmRep : realmReps.values()) { importRealms(session, realmReps.values(), strategy);
importRealm(session, realmRep, strategy);
}
} }


public static Map<String, RealmRepresentation> getRealmsFromStream(ObjectMapper mapper, InputStream is) throws IOException { public static Map<String, RealmRepresentation> getRealmsFromStream(ObjectMapper mapper, InputStream is) throws IOException {
Expand Down
Expand Up @@ -42,9 +42,7 @@ public void importModel(KeycloakSessionFactory factory, final Strategy strategy)


@Override @Override
protected void runExportImportTask(KeycloakSession session) throws IOException { protected void runExportImportTask(KeycloakSession session) throws IOException {
for (RealmRepresentation realmRep : realmReps.values()) { ImportUtils.importRealms(session, realmReps.values(), strategy);
ImportUtils.importRealm(session, realmRep, strategy);
}
} }


}); });
Expand Down

0 comments on commit f980362

Please sign in to comment.