KEYCLOAK-2596 Fix import with dir and ignore existing
This commit is contained in:
parent
229eca302f
commit
dc1d0e7f44
2 changed files with 19 additions and 15 deletions
|
@ -35,6 +35,7 @@ import java.io.FilenameFilter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
|
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
|
||||||
|
@ -118,26 +119,29 @@ public class DirImportProvider implements ImportProvider {
|
||||||
// Import realm first
|
// Import realm first
|
||||||
FileInputStream is = new FileInputStream(realmFile);
|
FileInputStream is = new FileInputStream(realmFile);
|
||||||
final RealmRepresentation realmRep = JsonSerialization.readValue(is, RealmRepresentation.class);
|
final RealmRepresentation realmRep = JsonSerialization.readValue(is, RealmRepresentation.class);
|
||||||
|
final AtomicBoolean realmImported = new AtomicBoolean();
|
||||||
|
|
||||||
KeycloakModelUtils.runJobInTransaction(factory, new ExportImportSessionTask() {
|
KeycloakModelUtils.runJobInTransaction(factory, new ExportImportSessionTask() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void runExportImportTask(KeycloakSession session) throws IOException {
|
public void runExportImportTask(KeycloakSession session) throws IOException {
|
||||||
ImportUtils.importRealm(session, realmRep, strategy);
|
boolean imported = ImportUtils.importRealm(session, realmRep, strategy);
|
||||||
|
realmImported.set(imported);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Import users
|
if (realmImported.get()) {
|
||||||
for (File userFile : userFiles) {
|
// Import users
|
||||||
final FileInputStream fis = new FileInputStream(userFile);
|
for (File userFile : userFiles) {
|
||||||
KeycloakModelUtils.runJobInTransaction(factory, new ExportImportSessionTask() {
|
final FileInputStream fis = new FileInputStream(userFile);
|
||||||
|
KeycloakModelUtils.runJobInTransaction(factory, new ExportImportSessionTask() {
|
||||||
@Override
|
@Override
|
||||||
protected void runExportImportTask(KeycloakSession session) throws IOException {
|
protected void runExportImportTask(KeycloakSession session) throws IOException {
|
||||||
ImportUtils.importUsersFromStream(session, realmName, JsonSerialization.mapper, fis);
|
ImportUtils.importUsersFromStream(session, realmName, JsonSerialization.mapper, fis);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -68,7 +68,7 @@ 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 void importRealm(KeycloakSession session, RealmRepresentation rep, Strategy strategy) {
|
public static boolean 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);
|
||||||
|
@ -76,7 +76,7 @@ public class ImportUtils {
|
||||||
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;
|
return false;
|
||||||
} 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())) {
|
||||||
|
@ -91,13 +91,13 @@ public class ImportUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
RealmImporter realmManager = session.getContext().getRealmManager();
|
RealmImporter realmManager = session.getContext().getRealmManager();
|
||||||
realm = realmManager.importRealm(rep);
|
realmManager.importRealm(rep);
|
||||||
|
|
||||||
if (System.getProperty(ExportImportConfig.ACTION) != null) {
|
if (System.getProperty(ExportImportConfig.ACTION) != null) {
|
||||||
logger.infof("Realm '%s' imported", realmName);
|
logger.infof("Realm '%s' imported", realmName);
|
||||||
}
|
}
|
||||||
|
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue