realm components import/export

This commit is contained in:
Bill Burke 2016-08-09 15:06:29 -04:00
parent 1633f062d5
commit 530870f05e
7 changed files with 177 additions and 5 deletions

View file

@ -0,0 +1,44 @@
/*
* 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.admin.client.resource;
import org.jboss.resteasy.spi.NotFoundException;
import org.keycloak.representations.idm.ComponentRepresentation;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
public interface ComponentResource {
@GET
public ComponentRepresentation toRepresentation();
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public void update(ComponentRepresentation rep);
@DELETE
public void remove();
}

View file

@ -0,0 +1,57 @@
/*
* 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.admin.client.resource;
import org.keycloak.representations.idm.ComponentRepresentation;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
/**
* @author <a href="mailto:bill@burkecentral.com">Bill Burke</a>
* @version $Revision: 1 $
*/
public interface ComponentsResource {
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<ComponentRepresentation> query();
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<ComponentRepresentation> query(@QueryParam("parent") String parent);
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<ComponentRepresentation> query(@QueryParam("parent") String parent, @QueryParam("type") String type);
@POST
@Consumes(MediaType.APPLICATION_JSON)
Response add(ComponentRepresentation rep);
@Path("{id}")
ComponentResource component(@PathParam("id") String id);
}

View file

@ -38,7 +38,7 @@ public interface GroupsResource {
@GET
@NoCache
@Produces(MediaType.APPLICATION_JSON)
public List<GroupRepresentation> groups();
List<GroupRepresentation> groups();
/**
* create or add a top level realm groupSet or create child. This will update the group and set the parent if it exists. Create it and set the parent
@ -48,9 +48,9 @@ public interface GroupsResource {
*/
@POST
@Consumes(MediaType.APPLICATION_JSON)
public Response add(GroupRepresentation rep);
Response add(GroupRepresentation rep);
@Path("{id}")
public GroupResource group(@PathParam("id") String id);
GroupResource group(@PathParam("id") String id);
}

View file

@ -193,4 +193,7 @@ public interface RealmResource {
@DELETE
void deleteSession(@PathParam("session") String sessionId);
@Path("components")
ComponentsResource components();
}

View file

@ -763,6 +763,7 @@ public class ModelToRepresentation {
rep.setName(component.getName());
rep.setProviderId(component.getProviderId());
rep.setProviderType(component.getProviderType());
rep.setParentId(component.getParentId());
rep.setConfig(component.getConfig());
return rep;
}

View file

@ -42,6 +42,7 @@ import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
@ -82,8 +83,15 @@ public class ComponentResource {
@Produces(MediaType.APPLICATION_JSON)
public List<ComponentRepresentation> getComponents(@QueryParam("parent") String parent, @QueryParam("type") String type) {
auth.requireManage();
if (parent == null) parent = realm.getId();
List<ComponentModel> components = realm.getComponents(parent, type);
List<ComponentModel> components = Collections.EMPTY_LIST;
if (parent == null) {
components = realm.getComponents();
} else if (type == null) {
components = realm.getComponents(parent);
} else {
components = realm.getComponents(parent, type);
}
List<ComponentRepresentation> reps = new LinkedList<>();
for (ComponentModel component : components) {
ComponentRepresentation rep = ModelToRepresentation.toRepresentation(component);

View file

@ -19,10 +19,12 @@ package org.keycloak.testsuite.exportimport;
import org.junit.Assert;
import org.junit.Test;
import org.keycloak.common.util.MultivaluedHashMap;
import org.keycloak.exportimport.ExportImportConfig;
import org.keycloak.exportimport.dir.DirExportProvider;
import org.keycloak.exportimport.dir.DirExportProviderFactory;
import org.keycloak.exportimport.singlefile.SingleFileExportProviderFactory;
import org.keycloak.representations.idm.ComponentRepresentation;
import org.keycloak.representations.idm.RealmRepresentation;
import java.io.File;
@ -138,6 +140,63 @@ public class ExportImportTest extends AbstractExportImportTest {
ExportImportUtil.assertDataImportedInRealm(adminClient, testingClient, testRealmRealm.toRepresentation());
}
@Test
public void testComponentExportImport() throws Throwable {
RealmRepresentation realmRep = new RealmRepresentation();
realmRep.setRealm("component-realm");
adminClient.realms().create(realmRep);
Assert.assertEquals(4, adminClient.realms().findAll().size());
RealmResource realm = adminClient.realm("component-realm");
realmRep = realm.toRepresentation();
ComponentRepresentation component = new ComponentRepresentation();
component.setProviderId("dummy");
component.setProviderType("dummyType");
component.setName("dummy-name");
component.setParentId(realmRep.getId());
component.setConfig(new MultivaluedHashMap<>());
component.getConfig().add("name", "value");
realm.components().add(component);
ExportImportConfig.setProvider(SingleFileExportProviderFactory.PROVIDER_ID);
String targetFilePath = getExportImportTestDirectory() + File.separator + "singleFile-realm.json";
ExportImportConfig.setFile(targetFilePath);
ExportImportConfig.setAction(ExportImportConfig.ACTION_EXPORT);
ExportImportConfig.setRealmName("component-realm");
testingClient.testing().runExport();
// Delete some realm (and some data in admin realm)
adminClient.realm("component-realm").remove();
Assert.assertEquals(3, adminClient.realms().findAll().size());
// Configure import
ExportImportConfig.setAction(ExportImportConfig.ACTION_IMPORT);
testingClient.testing().runImport();
realmRep = realm.toRepresentation();
List<ComponentRepresentation> components = realm.components().query();
Assert.assertEquals(1, components.size());
component = components.get(0);
Assert.assertEquals("dummy-name", component.getName());
Assert.assertEquals("dummyType", component.getProviderType());
Assert.assertEquals("dummy", component.getProviderId());
Assert.assertEquals(realmRep.getId(), component.getParentId());
Assert.assertEquals(1, component.getConfig().size());
Assert.assertEquals("value", component.getConfig().getFirst("name"));
adminClient.realm("component-realm").remove();
}
private void removeRealm(String realmName) {
adminClient.realm(realmName).remove();