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);
session.realms().getMigrationModel().setStoredVersion(model.getModelVersion());

List<RealmRepresentation> realmReps = new ArrayList<RealmRepresentation>();
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);
}

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

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import org.keycloak.exportimport.ExportImportConfig;

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

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
*
Expand All @@ -41,15 +54,15 @@ public class ImportUtils {
* @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)
*/
public static RealmModel importRealm(KeycloakSession session, RealmRepresentation rep, Strategy strategy) {
public static void importRealm(KeycloakSession session, RealmRepresentation rep, Strategy strategy) {
String realmName = rep.getRealm();
RealmProvider model = session.realms();
RealmModel realm = model.getRealmByName(realmName);

if (realm != null) {
if (strategy == Strategy.IGNORE_EXISTING) {
logger.infof("Realm '%s' already exists. Import skipped", realmName);
return realm;
return;
} else {
logger.infof("Realm '%s' already exists. Removing it before import", realmName);
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);
}

return realm;
return;
}

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 {
Map<String, RealmRepresentation> realmReps = getRealmsFromStream(mapper, is);
for (RealmRepresentation realmRep : realmReps.values()) {
importRealm(session, realmRep, strategy);
}
importRealms(session, realmReps.values(), strategy);
}

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
protected void runExportImportTask(KeycloakSession session) throws IOException {
for (RealmRepresentation realmRep : realmReps.values()) {
ImportUtils.importRealm(session, realmRep, strategy);
}
ImportUtils.importRealms(session, realmReps.values(), strategy);
}

});
Expand Down

0 comments on commit f980362

Please sign in to comment.