component export/import

This commit is contained in:
Bill Burke 2016-08-09 12:25:04 -04:00
parent f838c697d1
commit ff703f935f
10 changed files with 180 additions and 2 deletions

View file

@ -0,0 +1,72 @@
/*
* Copyright 2016 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.keycloak.representations.idm;
import org.keycloak.common.util.MultivaluedHashMap;
/**
* @author <a href="mailto:mposolda@redhat.com">Marek Posolda</a>
*/
public class ComponentExportRepresentation {
private String id;
private String name;
private String providerId;
private MultivaluedHashMap<String, ComponentExportRepresentation> subComponents = new MultivaluedHashMap<>();
private MultivaluedHashMap<String, String> config = new MultivaluedHashMap<>();
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getProviderId() {
return providerId;
}
public void setProviderId(String providerId) {
this.providerId = providerId;
}
public MultivaluedHashMap<String, String> getConfig() {
return config;
}
public void setConfig(MultivaluedHashMap<String, String> config) {
this.config = config;
}
public MultivaluedHashMap<String, ComponentExportRepresentation> getSubComponents() {
return subComponents;
}
public void setSubComponents(MultivaluedHashMap<String, ComponentExportRepresentation> subComponents) {
this.subComponents = subComponents;
}
}

View file

@ -18,6 +18,7 @@
package org.keycloak.representations.idm; package org.keycloak.representations.idm;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import org.keycloak.common.util.MultivaluedHashMap;
import java.util.*; import java.util.*;
@ -109,6 +110,7 @@ public class RealmRepresentation {
private List<IdentityProviderRepresentation> identityProviders; private List<IdentityProviderRepresentation> identityProviders;
private List<IdentityProviderMapperRepresentation> identityProviderMappers; private List<IdentityProviderMapperRepresentation> identityProviderMappers;
private List<ProtocolMapperRepresentation> protocolMappers; private List<ProtocolMapperRepresentation> protocolMappers;
private MultivaluedHashMap<String, ComponentExportRepresentation> components;
protected Boolean internationalizationEnabled; protected Boolean internationalizationEnabled;
protected Set<String> supportedLocales; protected Set<String> supportedLocales;
protected String defaultLocale; protected String defaultLocale;
@ -849,6 +851,14 @@ public class RealmRepresentation {
this.clientTemplates = clientTemplates; this.clientTemplates = clientTemplates;
} }
public MultivaluedHashMap<String, ComponentExportRepresentation> getComponents() {
return components;
}
public void setComponents(MultivaluedHashMap<String, ComponentExportRepresentation> components) {
this.components = components;
}
@JsonIgnore @JsonIgnore
public boolean isIdentityFederationEnabled() { public boolean isIdentityFederationEnabled() {
return identityProviders != null && !identityProviders.isEmpty(); return identityProviders != null && !identityProviders.isEmpty();

View file

@ -1438,7 +1438,15 @@ public class RealmAdapter implements RealmModel {
@Override @Override
public List<ComponentModel> getComponents(String parentId, String providerType) { public List<ComponentModel> getComponents(String parentId, String providerType) {
if (isUpdated()) return updated.getComponents(parentId, providerType); if (isUpdated()) return updated.getComponents(parentId, providerType);
List<ComponentModel> components = cached.getComponentsByParent().getList(parentId + providerType); List<ComponentModel> components = cached.getComponentsByParentAndType().getList(parentId + providerType);
if (components == null) return Collections.EMPTY_LIST;
return Collections.unmodifiableList(components);
}
@Override
public List<ComponentModel> getComponents(String parentId) {
if (isUpdated()) return updated.getComponents(parentId);
List<ComponentModel> components = cached.getComponentsByParent().getList(parentId);
if (components == null) return Collections.EMPTY_LIST; if (components == null) return Collections.EMPTY_LIST;
return Collections.unmodifiableList(components); return Collections.unmodifiableList(components);
} }

View file

@ -107,6 +107,7 @@ public class CachedRealm extends AbstractRevisioned {
protected List<RequiredCredentialModel> requiredCredentials; protected List<RequiredCredentialModel> requiredCredentials;
protected List<UserFederationProviderModel> userFederationProviders; protected List<UserFederationProviderModel> userFederationProviders;
protected MultivaluedHashMap<String, ComponentModel> componentsByParent = new MultivaluedHashMap<>(); protected MultivaluedHashMap<String, ComponentModel> componentsByParent = new MultivaluedHashMap<>();
protected MultivaluedHashMap<String, ComponentModel> componentsByParentAndType = new MultivaluedHashMap<>();
protected Map<String, ComponentModel> components = new HashMap<>(); protected Map<String, ComponentModel> components = new HashMap<>();
protected MultivaluedHashMap<String, UserFederationMapperModel> userFederationMappers = new MultivaluedHashMap<String, UserFederationMapperModel>(); protected MultivaluedHashMap<String, UserFederationMapperModel> userFederationMappers = new MultivaluedHashMap<String, UserFederationMapperModel>();
protected Set<UserFederationMapperModel> userFederationMapperSet; protected Set<UserFederationMapperModel> userFederationMapperSet;
@ -275,7 +276,10 @@ public class CachedRealm extends AbstractRevisioned {
clientAuthenticationFlow = model.getClientAuthenticationFlow(); clientAuthenticationFlow = model.getClientAuthenticationFlow();
for (ComponentModel component : model.getComponents()) { for (ComponentModel component : model.getComponents()) {
componentsByParent.add(component.getParentId() + component.getProviderType(), component); componentsByParentAndType.add(component.getParentId() + component.getProviderType(), component);
}
for (ComponentModel component : model.getComponents()) {
componentsByParent.add(component.getParentId(), component);
} }
for (ComponentModel component : model.getComponents()) { for (ComponentModel component : model.getComponents()) {
components.put(component.getId(), component); components.put(component.getId(), component);
@ -608,6 +612,10 @@ public class CachedRealm extends AbstractRevisioned {
return componentsByParent; return componentsByParent;
} }
public MultivaluedHashMap<String, ComponentModel> getComponentsByParentAndType() {
return componentsByParentAndType;
}
public Map<String, ComponentModel> getComponents() { public Map<String, ComponentModel> getComponents() {
return components; return components;
} }

View file

@ -2195,6 +2195,21 @@ public class RealmAdapter implements RealmModel, JpaModel<RealmEntity> {
return rtn; return rtn;
} }
@Override
public List<ComponentModel> getComponents(String parentId) {
TypedQuery<ComponentEntity> query = em.createNamedQuery("getComponentsByParent", ComponentEntity.class)
.setParameter("realm", realm)
.setParameter("parentId", parentId);
List<ComponentEntity> results = query.getResultList();
List<ComponentModel> rtn = new LinkedList<>();
for (ComponentEntity c : results) {
ComponentModel model = entityToModel(c);
rtn.add(model);
}
return rtn;
}
protected ComponentModel entityToModel(ComponentEntity c) { protected ComponentModel entityToModel(ComponentEntity c) {
ComponentModel model = new ComponentModel(); ComponentModel model = new ComponentModel();
model.setId(c.getId()); model.setId(c.getId());

View file

@ -44,6 +44,7 @@ import java.util.Map;
@NamedQueries({ @NamedQueries({
@NamedQuery(name="getComponents", query="select attr from ComponentEntity attr where attr.realm = :realm"), @NamedQuery(name="getComponents", query="select attr from ComponentEntity attr where attr.realm = :realm"),
@NamedQuery(name="getComponentsByParentAndType", query="select attr from ComponentEntity attr where attr.realm = :realm and attr.providerType = :providerType and attr.parentId = :parentId"), @NamedQuery(name="getComponentsByParentAndType", query="select attr from ComponentEntity attr where attr.realm = :realm and attr.providerType = :providerType and attr.parentId = :parentId"),
@NamedQuery(name="getComponentByParent", query="select attr from ComponentEntity attr where attr.realm = :realm and attr.parentId = :parentId"),
@NamedQuery(name="getComponentIdsByParent", query="select attr.id from ComponentEntity attr where attr.realm = :realm and attr.parentId = :parentId"), @NamedQuery(name="getComponentIdsByParent", query="select attr.id from ComponentEntity attr where attr.realm = :realm and attr.parentId = :parentId"),
@NamedQuery(name="deleteComponentByRealm", query="delete from ComponentEntity c where c.realm = :realm"), @NamedQuery(name="deleteComponentByRealm", query="delete from ComponentEntity c where c.realm = :realm"),
@NamedQuery(name="deleteComponentByParent", query="delete from ComponentEntity c where c.parentId = :parentId") @NamedQuery(name="deleteComponentByParent", query="delete from ComponentEntity c where c.parentId = :parentId")

View file

@ -2135,6 +2135,19 @@ public class RealmAdapter extends AbstractMongoAdapter<MongoRealmEntity> impleme
return results; return results;
} }
@Override
public List<ComponentModel> getComponents(String parentId) {
List<ComponentModel> results = new LinkedList<>();
for (ComponentEntity entity : realm.getComponentEntities()) {
if (entity.getParentId().equals(parentId)) {
ComponentModel model = entityToModel(entity);
results.add(model);
}
}
return results;
}
protected ComponentModel entityToModel(ComponentEntity entity) { protected ComponentModel entityToModel(ComponentEntity entity) {
ComponentModel model = new ComponentModel(); ComponentModel model = new ComponentModel();
model.setId(entity.getId()); model.setId(entity.getId());

View file

@ -291,6 +291,9 @@ public interface RealmModel extends RoleContainerModel {
void removeComponent(ComponentModel component); void removeComponent(ComponentModel component);
void removeComponents(String parentId); void removeComponents(String parentId);
List<ComponentModel> getComponents(String parentId, String providerType); List<ComponentModel> getComponents(String parentId, String providerType);
List<ComponentModel> getComponents(String parentId);
List<ComponentModel> getComponents(); List<ComponentModel> getComponents();
ComponentModel getComponent(String id); ComponentModel getComponent(String id);

View file

@ -20,6 +20,7 @@ package org.keycloak.models.utils;
import org.keycloak.authorization.AuthorizationProvider; import org.keycloak.authorization.AuthorizationProvider;
import org.keycloak.authorization.model.ResourceServer; import org.keycloak.authorization.model.ResourceServer;
import org.keycloak.authorization.store.ResourceServerStore; import org.keycloak.authorization.store.ResourceServerStore;
import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.component.ComponentModel; import org.keycloak.component.ComponentModel;
import org.keycloak.hash.Pbkdf2PasswordHashProvider; import org.keycloak.hash.Pbkdf2PasswordHashProvider;
import org.keycloak.migration.migrators.MigrationUtils; import org.keycloak.migration.migrators.MigrationUtils;
@ -63,6 +64,7 @@ import org.keycloak.representations.idm.AuthenticatorConfigRepresentation;
import org.keycloak.representations.idm.ClaimRepresentation; import org.keycloak.representations.idm.ClaimRepresentation;
import org.keycloak.representations.idm.ClientRepresentation; import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.ClientTemplateRepresentation; import org.keycloak.representations.idm.ClientTemplateRepresentation;
import org.keycloak.representations.idm.ComponentExportRepresentation;
import org.keycloak.representations.idm.ComponentRepresentation; import org.keycloak.representations.idm.ComponentRepresentation;
import org.keycloak.representations.idm.CredentialRepresentation; import org.keycloak.representations.idm.CredentialRepresentation;
import org.keycloak.representations.idm.FederatedIdentityRepresentation; import org.keycloak.representations.idm.FederatedIdentityRepresentation;
@ -285,6 +287,12 @@ public class RepresentationToModel {
newRealm.setBrowserSecurityHeaders(BrowserSecurityHeaders.defaultHeaders); newRealm.setBrowserSecurityHeaders(BrowserSecurityHeaders.defaultHeaders);
} }
if (rep.getComponents() != null) {
MultivaluedHashMap<String, ComponentExportRepresentation> components = rep.getComponents();
String parentId = newRealm.getId();
importComponents(newRealm, components, parentId);
}
List<UserFederationProviderModel> providerModels = null; List<UserFederationProviderModel> providerModels = null;
if (rep.getUserFederationProviders() != null) { if (rep.getUserFederationProviders() != null) {
providerModels = convertFederationProviders(rep.getUserFederationProviders()); providerModels = convertFederationProviders(rep.getUserFederationProviders());
@ -346,6 +354,25 @@ public class RepresentationToModel {
} }
} }
protected static void importComponents(RealmModel newRealm, MultivaluedHashMap<String, ComponentExportRepresentation> components, String parentId) {
for (Map.Entry<String, List<ComponentExportRepresentation>> entry : components.entrySet()) {
String providerType = entry.getKey();
for (ComponentExportRepresentation compRep : entry.getValue()) {
ComponentModel component = new ComponentModel();
component.setId(compRep.getId());
component.setName(compRep.getName());
component.setConfig(compRep.getConfig());
component.setProviderType(providerType);
component.setProviderId(compRep.getProviderId());
component.setParentId(parentId);
component = newRealm.addComponentModel(component);
if (compRep.getSubComponents() != null) {
importComponents(newRealm, compRep.getSubComponents(), component.getId());
}
}
}
}
public static void importRoles(RolesRepresentation realmRoles, RealmModel realm) { public static void importRoles(RolesRepresentation realmRoles, RealmModel realm) {
if (realmRoles == null) return; if (realmRoles == null) return;

View file

@ -24,6 +24,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.SerializationFeature;
import org.keycloak.common.Version; import org.keycloak.common.Version;
import org.keycloak.common.util.Base64; import org.keycloak.common.util.Base64;
import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.component.ComponentModel;
import org.keycloak.models.*; import org.keycloak.models.*;
import org.keycloak.models.utils.ModelToRepresentation; import org.keycloak.models.utils.ModelToRepresentation;
import org.keycloak.representations.idm.*; import org.keycloak.representations.idm.*;
@ -178,9 +180,28 @@ public class ExportUtils {
} }
} }
// components
MultivaluedHashMap<String, ComponentExportRepresentation> components = exportComponents(realm, realm.getId());
rep.setComponents(components);
return rep; return rep;
} }
public static MultivaluedHashMap<String, ComponentExportRepresentation> exportComponents(RealmModel realm, String parentId) {
List<ComponentModel> componentList = realm.getComponents(parentId);
MultivaluedHashMap<String, ComponentExportRepresentation> components = new MultivaluedHashMap<>();
for (ComponentModel component : componentList) {
ComponentExportRepresentation compRep = new ComponentExportRepresentation();
compRep.setId(component.getId());
compRep.setProviderId(component.getProviderId());
compRep.setConfig(component.getConfig());
compRep.setName(component.getName());
compRep.setSubComponents(exportComponents(realm, component.getId()));
components.add(component.getProviderType(), compRep);
}
return components;
}
/** /**
* Full export of application including claims and secret * Full export of application including claims and secret
* @param client * @param client