KEYCLOAK-15087 : Reduce get client and get roles calls in realm create

This commit is contained in:
Pratik Somanagoudar 2020-08-09 12:56:50 -04:00 committed by Pedro Igor
parent 6231b7c904
commit f486e97c18
2 changed files with 21 additions and 25 deletions

View file

@ -326,8 +326,9 @@ public class RepresentationToModel {
} }
} }
Map<String, ClientModel> createdClients = new HashMap<>();
if (rep.getClients() != null) { if (rep.getClients() != null) {
createClients(session, rep, newRealm, mappedFlows); createdClients = createClients(session, rep, newRealm, mappedFlows);
} }
importRoles(rep.getRoles(), newRealm); importRoles(rep.getRoles(), newRealm);
@ -342,20 +343,19 @@ public class RepresentationToModel {
if (rep.getClients() != null) { if (rep.getClients() != null) {
for (ClientRepresentation resourceRep : rep.getClients()) { for (ClientRepresentation resourceRep : rep.getClients()) {
if (resourceRep.getDefaultRoles() != null) { if (resourceRep.getDefaultRoles() != null) {
ClientModel clientModel = newRealm.getClientByClientId(resourceRep.getClientId()); ClientModel clientModel = createdClients.computeIfAbsent(resourceRep.getClientId(), k -> newRealm.getClientByClientId(resourceRep.getClientId()));
clientModel.updateDefaultRoles(resourceRep.getDefaultRoles()); clientModel.updateDefaultRoles(resourceRep.getDefaultRoles());
createdClients.put(clientModel.getClientId(), clientModel);
} }
} }
} }
// Now that all possible roles and clients are created, create scope mappings // Now that all possible roles and clients are created, create scope mappings
//Map<String, ClientModel> appMap = newRealm.getClientNameMap();
if (rep.getClientScopeMappings() != null) { if (rep.getClientScopeMappings() != null) {
for (Map.Entry<String, List<ScopeMappingRepresentation>> entry : rep.getClientScopeMappings().entrySet()) { for (Map.Entry<String, List<ScopeMappingRepresentation>> entry : rep.getClientScopeMappings().entrySet()) {
ClientModel app = newRealm.getClientByClientId(entry.getKey()); ClientModel app = createdClients.computeIfAbsent(entry.getKey(), k -> newRealm.getClientByClientId(entry.getKey()));
if (app == null) { if (app == null) {
throw new RuntimeException("Unable to find client role mappings for client: " + entry.getKey()); throw new RuntimeException("Unable to find client role mappings for client: " + entry.getKey());
} }
@ -364,17 +364,19 @@ public class RepresentationToModel {
} }
if (rep.getScopeMappings() != null) { if (rep.getScopeMappings() != null) {
Map<String, RoleModel> roleModelMap = newRealm.getRolesStream().collect(Collectors.toMap(RoleModel::getId, Function.identity()));
for (ScopeMappingRepresentation scope : rep.getScopeMappings()) { for (ScopeMappingRepresentation scope : rep.getScopeMappings()) {
ScopeContainerModel scopeContainer = getScopeContainerHavingScope(newRealm, scope); ScopeContainerModel scopeContainer = getScopeContainerHavingScope(newRealm, scope);
for (String roleString : scope.getRoles()) { for (String roleString : scope.getRoles()) {
RoleModel role = newRealm.getRole(roleString.trim()); final String roleStringTrimmed = roleString.trim();
RoleModel role = roleModelMap.computeIfAbsent(roleStringTrimmed, k -> newRealm.getRole(roleStringTrimmed));
if (role == null) { if (role == null) {
role = newRealm.addRole(roleString.trim()); role = newRealm.addRole(roleString);
roleModelMap.put(role.getId(), role);
} }
scopeContainer.addScopeMapping(role); scopeContainer.addScopeMapping(role);
} }
} }
} }
@ -412,14 +414,13 @@ public class RepresentationToModel {
if (rep.getUsers() != null) { if (rep.getUsers() != null) {
for (UserRepresentation userRep : rep.getUsers()) { for (UserRepresentation userRep : rep.getUsers()) {
UserModel user = createUser(session, newRealm, userRep); createUser(session, newRealm, userRep);
} }
} }
if (rep.getFederatedUsers() != null) { if (rep.getFederatedUsers() != null) {
for (UserRepresentation userRep : rep.getFederatedUsers()) { for (UserRepresentation userRep : rep.getFederatedUsers()) {
importFederatedUser(session, newRealm, userRep); importFederatedUser(session, newRealm, userRep);
} }
} }

View file

@ -303,11 +303,11 @@ public class RealmManager {
String adminRealmId = Config.getAdminRealm(); String adminRealmId = Config.getAdminRealm();
RealmModel adminRealm = model.getRealm(adminRealmId); RealmModel adminRealm = model.getRealm(adminRealmId);
ClientModel masterApp = adminRealm.getClientByClientId(KeycloakModelUtils.getMasterRealmAdminApplicationClientId(realm.getName())); ClientModel masterApp = adminRealm.getClientByClientId(KeycloakModelUtils.getMasterRealmAdminApplicationClientId(realm.getName()));
if (masterApp != null) { if (masterApp == null) {
realm.setMasterAdminClient(masterApp);
} else {
createMasterAdminManagement(realm); createMasterAdminManagement(realm);
return;
} }
realm.setMasterAdminClient(masterApp);
} }
private void createMasterAdminManagement(RealmModel realm) { private void createMasterAdminManagement(RealmModel realm) {
@ -521,22 +521,19 @@ public class RealmManager {
if (!hasRealmAdminManagementClient(rep)) setupRealmAdminManagement(realm); if (!hasRealmAdminManagementClient(rep)) setupRealmAdminManagement(realm);
if (!hasAccountManagementClient(rep)) setupAccountManagement(realm); if (!hasAccountManagementClient(rep)) setupAccountManagement(realm);
boolean postponeImpersonationSetup = false; boolean postponeImpersonationSetup = hasRealmAdminManagementClient(rep);
if (hasRealmAdminManagementClient(rep)) { if (!postponeImpersonationSetup) {
postponeImpersonationSetup = true;
} else {
setupImpersonationService(realm); setupImpersonationService(realm);
} }
if (!hasBrokerClient(rep)) setupBrokerService(realm); if (!hasBrokerClient(rep)) setupBrokerService(realm);
if (!hasAdminConsoleClient(rep)) setupAdminConsole(realm); if (!hasAdminConsoleClient(rep)) setupAdminConsole(realm);
boolean postponeAdminCliSetup = false; boolean postponeAdminCliSetup = false;
if (!hasAdminCliClient(rep)) { if (!hasAdminCliClient(rep)) {
if (hasRealmAdminManagementClient(rep)) { postponeAdminCliSetup = hasRealmAdminManagementClient(rep);
postponeAdminCliSetup = true;
} else { if(!postponeAdminCliSetup) {
setupAdminCli(realm); setupAdminCli(realm);
} }
} }
@ -550,7 +547,6 @@ public class RealmManager {
} }
RepresentationToModel.importRealm(session, rep, realm, skipUserDependent); RepresentationToModel.importRealm(session, rep, realm, skipUserDependent);
List<ClientRepresentation> clients = rep.getClients();
setupClientServiceAccountsAndAuthorizationOnImport(rep, skipUserDependent); setupClientServiceAccountsAndAuthorizationOnImport(rep, skipUserDependent);
@ -570,8 +566,7 @@ public class RealmManager {
// I need to postpone impersonation because it needs "realm-management" client and its roles set // I need to postpone impersonation because it needs "realm-management" client and its roles set
if (postponeImpersonationSetup) { if (postponeImpersonationSetup) {
setupImpersonationService(realm); setupImpersonationService(realm);
String realmAdminClientId = getRealmAdminClientId(realm); }
}
if (postponeAdminCliSetup) { if (postponeAdminCliSetup) {
setupAdminCli(realm); setupAdminCli(realm);