More cleanup after rebase
This commit is contained in:
1 parent
24a492ffe0
commit
c14840a4c0
19 files changed
+183
-108
No files matched your search
+4
-4
@@ -35,10 +35,10 @@ public class ExportImportManager {
|
||||
|
||||
if (realmName == null) {
|
||||
logger.info("Full model export requested");
|
||||
exportProvider.exportModel(session);
|
||||
exportProvider.exportModel(sessionFactory);
|
||||
} else {
|
||||
logger.infof("Export of realm '%s' requested", realmName);
|
||||
exportProvider.exportRealm(session, realmName);
|
||||
exportProvider.exportRealm(sessionFactory, realmName);
|
||||
}
|
||||
logger.info("Export finished successfully");
|
||||
} else {
|
||||
@@ -46,10 +46,10 @@ public class ExportImportManager {
|
||||
Strategy strategy = ExportImportConfig.getStrategy();
|
||||
if (realmName == null) {
|
||||
logger.infof("Full model import requested. Strategy: %s", strategy.toString());
|
||||
importProvider.importModel(session, strategy);
|
||||
importProvider.importModel(sessionFactory, strategy);
|
||||
} else {
|
||||
logger.infof("Import of realm '%s' requested. Strategy: %s", realmName, strategy.toString());
|
||||
importProvider.importRealm(session, realmName, strategy);
|
||||
importProvider.importRealm(sessionFactory, realmName, strategy);
|
||||
}
|
||||
logger.info("Import finished successfully");
|
||||
}
|
||||
|
||||
+2
-2
@@ -11,8 +11,8 @@ import org.keycloak.provider.Provider;
|
||||
*/
|
||||
public interface ExportProvider extends Provider {
|
||||
|
||||
void exportModel(KeycloakSession session) throws IOException;
|
||||
void exportModel(KeycloakSessionFactory factory) throws IOException;
|
||||
|
||||
void exportRealm(KeycloakSession session, String realmName) throws IOException;
|
||||
void exportRealm(KeycloakSessionFactory factory, String realmName) throws IOException;
|
||||
|
||||
}
|
||||
+3
-2
@@ -3,6 +3,7 @@ package org.keycloak.exportimport;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.keycloak.models.KeycloakSession;
|
||||
import org.keycloak.models.KeycloakSessionFactory;
|
||||
import org.keycloak.provider.Provider;
|
||||
|
||||
/**
|
||||
@@ -10,7 +11,7 @@ import org.keycloak.provider.Provider;
|
||||
*/
|
||||
public interface ImportProvider extends Provider {
|
||||
|
||||
void importModel(KeycloakSession session, Strategy strategy) throws IOException;
|
||||
void importModel(KeycloakSessionFactory factory, Strategy strategy) throws IOException;
|
||||
|
||||
void importRealm(KeycloakSession session, String realmName, Strategy strategy) throws IOException;
|
||||
void importRealm(KeycloakSessionFactory factory, String realmName, Strategy strategy) throws IOException;
|
||||
}
|
||||
+3
-1
@@ -2,6 +2,8 @@ package org.keycloak.exportimport.util;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.keycloak.models.KeycloakSession;
|
||||
|
||||
/**
|
||||
* Task to be executed inside transaction
|
||||
*
|
||||
@@ -9,5 +11,5 @@ import java.io.IOException;
|
||||
*/
|
||||
public interface ExportImportJob {
|
||||
|
||||
public void run() throws IOException;
|
||||
public void run(KeycloakSession session) throws IOException;
|
||||
}
|
||||
+7
-5
@@ -4,6 +4,7 @@ import java.io.IOException;
|
||||
|
||||
import org.jboss.logging.Logger;
|
||||
import org.keycloak.models.KeycloakSession;
|
||||
import org.keycloak.models.KeycloakSessionFactory;
|
||||
import org.keycloak.models.KeycloakTransaction;
|
||||
import org.keycloak.models.RealmModel;
|
||||
|
||||
@@ -13,17 +14,17 @@ import org.keycloak.models.RealmModel;
|
||||
public class ExportImportUtils {
|
||||
|
||||
/**
|
||||
* Wrap given runnable job into KeycloakTransaction. Assumption is that session already exists and it doesn't need to be closed when finished
|
||||
* Wrap given runnable job into KeycloakTransaction.
|
||||
*
|
||||
* @param session
|
||||
* @param factory
|
||||
* @param job
|
||||
*/
|
||||
public static void runJobInTransaction(KeycloakSession session, ExportImportJob job) throws IOException {
|
||||
public static void runJobInTransaction(KeycloakSessionFactory factory, ExportImportJob job) throws IOException {
|
||||
KeycloakSession session = factory.create();
|
||||
KeycloakTransaction tx = session.getTransaction();
|
||||
try {
|
||||
tx.begin();
|
||||
|
||||
job.run();
|
||||
job.run(session);
|
||||
|
||||
if (tx.isActive()) {
|
||||
if (tx.getRollbackOnly()) {
|
||||
@@ -36,6 +37,7 @@ public class ExportImportUtils {
|
||||
if (tx.isActive()) {
|
||||
tx.rollback();
|
||||
}
|
||||
session.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+45
-4
@@ -17,11 +17,14 @@ import org.codehaus.jackson.map.ObjectMapper;
|
||||
import org.jboss.logging.Logger;
|
||||
import org.keycloak.Config;
|
||||
import org.keycloak.exportimport.Strategy;
|
||||
import org.keycloak.models.AdminRoles;
|
||||
import org.keycloak.models.ApplicationModel;
|
||||
import org.keycloak.models.KeycloakSession;
|
||||
import org.keycloak.models.ModelProvider;
|
||||
import org.keycloak.models.RealmModel;
|
||||
import org.keycloak.models.RoleModel;
|
||||
import org.keycloak.models.UserModel;
|
||||
import org.keycloak.models.utils.KeycloakModelUtils;
|
||||
import org.keycloak.models.utils.RepresentationToModel;
|
||||
import org.keycloak.representations.idm.RealmRepresentation;
|
||||
import org.keycloak.representations.idm.UserRepresentation;
|
||||
@@ -43,7 +46,7 @@ public class ImportUtils {
|
||||
*/
|
||||
public static RealmModel importRealm(KeycloakSession session, RealmRepresentation rep, Strategy strategy) {
|
||||
String realmName = rep.getRealm();
|
||||
ModelProvider model = session.getModel();
|
||||
ModelProvider model = session.model();
|
||||
RealmModel realm = model.getRealmByName(realmName);
|
||||
|
||||
if (realm != null) {
|
||||
@@ -52,6 +55,9 @@ public class ImportUtils {
|
||||
return realm;
|
||||
} else {
|
||||
logger.infof("Realm '%s' already exists. Removing it before import", realmName);
|
||||
if (Config.getAdminRealm().equals(realm.getId())) {
|
||||
realm.setMasterAdminApp(null);
|
||||
}
|
||||
model.removeRealm(realm.getId());
|
||||
}
|
||||
}
|
||||
@@ -73,13 +79,48 @@ public class ImportUtils {
|
||||
RealmModel adminRealm = realm;
|
||||
for (RealmModel currentRealm : model.getRealms()) {
|
||||
ApplicationModel masterApp = adminRealm.getApplicationByName(ExportImportUtils.getMasterRealmAdminApplicationName(currentRealm));
|
||||
currentRealm.setMasterAdminApp(masterApp);
|
||||
if (masterApp != null) {
|
||||
currentRealm.setMasterAdminApp(masterApp);
|
||||
} else {
|
||||
setupMasterAdminManagement(model, currentRealm);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Need to refresh masterApp for current realm
|
||||
RealmModel adminRealm = model.getRealm(adminRealmId);
|
||||
ApplicationModel masterApp = adminRealm.getApplicationByName(ExportImportUtils.getMasterRealmAdminApplicationName(realm));
|
||||
realm.setMasterAdminApp(masterApp);
|
||||
if (masterApp != null) {
|
||||
realm.setMasterAdminApp(masterApp);
|
||||
} else {
|
||||
setupMasterAdminManagement(model, realm);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: We need method here, so we are able to refresh masterAdmin applications after import. Should be RealmManager moved to model/api instead?
|
||||
public static void setupMasterAdminManagement(ModelProvider model, RealmModel realm) {
|
||||
RealmModel adminRealm;
|
||||
RoleModel adminRole;
|
||||
|
||||
if (realm.getName().equals(Config.getAdminRealm())) {
|
||||
adminRealm = realm;
|
||||
|
||||
adminRole = realm.addRole(AdminRoles.ADMIN);
|
||||
|
||||
RoleModel createRealmRole = realm.addRole(AdminRoles.CREATE_REALM);
|
||||
adminRole.addCompositeRole(createRealmRole);
|
||||
} else {
|
||||
adminRealm = model.getRealmByName(Config.getAdminRealm());
|
||||
adminRole = adminRealm.getRole(AdminRoles.ADMIN);
|
||||
}
|
||||
|
||||
ApplicationModel realmAdminApp = KeycloakModelUtils.createApplication(adminRealm, ExportImportUtils.getMasterRealmAdminApplicationName(realm));
|
||||
realmAdminApp.setBearerOnly(true);
|
||||
realm.setMasterAdminApp(realmAdminApp);
|
||||
|
||||
for (String r : AdminRoles.ALL_REALM_ROLES) {
|
||||
RoleModel role = realmAdminApp.addRole(r);
|
||||
adminRole.addCompositeRole(role);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +160,7 @@ public class ImportUtils {
|
||||
|
||||
// Assuming that it's invoked inside transaction
|
||||
public static void importUsersFromStream(KeycloakSession session, String realmName, ObjectMapper mapper, InputStream is) throws IOException {
|
||||
ModelProvider model = session.getModel();
|
||||
ModelProvider model = session.model();
|
||||
JsonFactory factory = mapper.getJsonFactory();
|
||||
JsonParser parser = factory.createJsonParser(is);
|
||||
try {
|
||||
|
||||
+13
-12
@@ -7,6 +7,7 @@ import org.jboss.logging.Logger;
|
||||
import org.keycloak.exportimport.ExportImportConfig;
|
||||
import org.keycloak.exportimport.ExportProvider;
|
||||
import org.keycloak.models.KeycloakSession;
|
||||
import org.keycloak.models.KeycloakSessionFactory;
|
||||
import org.keycloak.models.RealmModel;
|
||||
import org.keycloak.models.UserModel;
|
||||
import org.keycloak.representations.idm.RealmRepresentation;
|
||||
@@ -19,36 +20,36 @@ public abstract class MultipleStepsExportProvider implements ExportProvider {
|
||||
protected final Logger logger = Logger.getLogger(getClass());
|
||||
|
||||
@Override
|
||||
public void exportModel(final KeycloakSession session) throws IOException {
|
||||
public void exportModel(KeycloakSessionFactory factory) throws IOException {
|
||||
final RealmsHolder holder = new RealmsHolder();
|
||||
|
||||
// Import users into same file with realm
|
||||
ExportImportUtils.runJobInTransaction(session, new ExportImportJob() {
|
||||
ExportImportUtils.runJobInTransaction(factory, new ExportImportJob() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
List<RealmModel> realms = session.getModel().getRealms();
|
||||
public void run(KeycloakSession session) {
|
||||
List<RealmModel> realms = session.model().getRealms();
|
||||
holder.realms = realms;
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
for (RealmModel realm : holder.realms) {
|
||||
exportRealm(session, realm.getName());
|
||||
exportRealm(factory, realm.getName());
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exportRealm(final KeycloakSession session, final String realmName) throws IOException {
|
||||
public void exportRealm(KeycloakSessionFactory factory, final String realmName) throws IOException {
|
||||
final int usersPerFile = ExportImportConfig.getUsersPerFile();
|
||||
final UsersHolder usersHolder = new UsersHolder();
|
||||
final boolean exportUsersIntoSameFile = usersPerFile < 0;
|
||||
|
||||
ExportImportUtils.runJobInTransaction(session, new ExportImportJob() {
|
||||
ExportImportUtils.runJobInTransaction(factory, new ExportImportJob() {
|
||||
|
||||
@Override
|
||||
public void run() throws IOException {
|
||||
RealmModel realm = session.getModel().getRealmByName(realmName);
|
||||
public void run(KeycloakSession session) throws IOException {
|
||||
RealmModel realm = session.model().getRealmByName(realmName);
|
||||
RealmRepresentation rep = ExportUtils.exportRealm(realm, exportUsersIntoSameFile);
|
||||
writeRealm(realmName + "-realm.json", rep);
|
||||
logger.info("Realm '" + realmName + "' - data exported");
|
||||
@@ -76,11 +77,11 @@ public abstract class MultipleStepsExportProvider implements ExportProvider {
|
||||
usersHolder.currentPageEnd = usersHolder.totalCount;
|
||||
}
|
||||
|
||||
ExportImportUtils.runJobInTransaction(session, new ExportImportJob() {
|
||||
ExportImportUtils.runJobInTransaction(factory, new ExportImportJob() {
|
||||
|
||||
@Override
|
||||
public void run() throws IOException {
|
||||
RealmModel realm = session.getModel().getRealmByName(realmName);
|
||||
public void run(KeycloakSession session) throws IOException {
|
||||
RealmModel realm = session.model().getRealmByName(realmName);
|
||||
// TODO: pagination
|
||||
List<UserModel> users = realm.getUsers();
|
||||
usersHolder.users = users.subList(usersHolder.currentPageStart, usersHolder.currentPageEnd);
|
||||
|
||||
Reference in new issue
Block a user