Search in sources :

Example 1 with PartialImportRepresentation

use of org.keycloak.representations.idm.PartialImportRepresentation in project keycloak by keycloak.

the class PermissionsTest method realms.

@Test
public void realms() throws Exception {
    // Check returned realms
    invoke((RealmResource realm) -> {
        clients.get("master-none").realms().findAll();
    }, clients.get("none"), false);
    invoke((RealmResource realm) -> {
        clients.get("none").realms().findAll();
    }, clients.get("none"), false);
    Assert.assertNames(clients.get("master-admin").realms().findAll(), "master", REALM_NAME, "realm2");
    Assert.assertNames(clients.get(AdminRoles.REALM_ADMIN).realms().findAll(), REALM_NAME);
    Assert.assertNames(clients.get("REALM2").realms().findAll(), "realm2");
    // Check realm only contains name if missing view realm permission
    List<RealmRepresentation> realms = clients.get(AdminRoles.VIEW_USERS).realms().findAll();
    Assert.assertNames(realms, REALM_NAME);
    assertGettersEmpty(realms.get(0));
    realms = clients.get(AdminRoles.VIEW_REALM).realms().findAll();
    Assert.assertNames(realms, REALM_NAME);
    assertNotNull(realms.get(0).getAccessTokenLifespan());
    // Check the same when access with users from 'master' realm
    realms = clients.get("master-" + AdminRoles.VIEW_USERS).realms().findAll();
    Assert.assertNames(realms, REALM_NAME);
    assertGettersEmpty(realms.get(0));
    realms = clients.get("master-" + AdminRoles.VIEW_REALM).realms().findAll();
    Assert.assertNames(realms, REALM_NAME);
    assertNotNull(realms.get(0).getAccessTokenLifespan());
    // Create realm
    invoke(new Invocation() {

        public void invoke(RealmResource realm) {
            clients.get("master-admin").realms().create(RealmBuilder.create().name("master").build());
        }
    }, adminClient, true);
    invoke(new Invocation() {

        public void invoke(RealmResource realm) {
            clients.get("master-" + AdminRoles.MANAGE_USERS).realms().create(RealmBuilder.create().name("master").build());
        }
    }, adminClient, false);
    invoke(new Invocation() {

        public void invoke(RealmResource realm) {
            clients.get(AdminRoles.REALM_ADMIN).realms().create(RealmBuilder.create().name("master").build());
        }
    }, adminClient, false);
    // Get realm
    invoke(new Invocation() {

        public void invoke(RealmResource realm) {
            realm.toRepresentation();
        }
    }, Resource.REALM, false, true);
    assertGettersEmpty(clients.get(AdminRoles.QUERY_REALMS).realm(REALM_NAME).toRepresentation());
    // this should pass given that users granted with "query" roles are allowed to access the realm with limited access
    for (String role : AdminRoles.ALL_QUERY_ROLES) {
        invoke(realm -> clients.get(role).realms().realm(REALM_NAME).toRepresentation(), clients.get(role), true);
    }
    invoke(new Invocation() {

        public void invoke(RealmResource realm) {
            realm.update(new RealmRepresentation());
        }
    }, Resource.REALM, true);
    invoke(new Invocation() {

        public void invoke(RealmResource realm) {
            realm.pushRevocation();
        }
    }, Resource.REALM, true);
    invoke(new Invocation() {

        public void invoke(RealmResource realm) {
            realm.deleteSession("nosuch");
        }
    }, Resource.USER, true);
    invoke(new Invocation() {

        public void invoke(RealmResource realm) {
            realm.getClientSessionStats();
        }
    }, Resource.REALM, false);
    invoke(new Invocation() {

        public void invoke(RealmResource realm) {
            realm.getDefaultGroups();
        }
    }, Resource.REALM, false);
    invoke(new Invocation() {

        public void invoke(RealmResource realm) {
            realm.addDefaultGroup("nosuch");
        }
    }, Resource.REALM, true);
    invoke(new Invocation() {

        public void invoke(RealmResource realm) {
            realm.removeDefaultGroup("nosuch");
        }
    }, Resource.REALM, true);
    GroupRepresentation newGroup = new GroupRepresentation();
    newGroup.setName("sample");
    adminClient.realm(REALM_NAME).groups().add(newGroup);
    GroupRepresentation group = adminClient.realms().realm(REALM_NAME).getGroupByPath("sample");
    invoke(new Invocation() {

        public void invoke(RealmResource realm) {
            realm.getGroupByPath("sample");
        }
    }, Resource.USER, false);
    adminClient.realms().realm(REALM_NAME).groups().group(group.getId()).remove();
    invoke(new InvocationWithResponse() {

        public void invoke(RealmResource realm, AtomicReference<Response> response) {
            response.set(realm.testLDAPConnection("nosuch", "nosuch", "nosuch", "nosuch", "nosuch", "nosuch"));
        }
    }, Resource.REALM, true);
    invoke(new InvocationWithResponse() {

        public void invoke(RealmResource realm, AtomicReference<Response> response) {
            response.set(realm.partialImport(new PartialImportRepresentation()));
        }
    }, Resource.REALM, true);
    invoke(new Invocation() {

        public void invoke(RealmResource realm) {
            realm.clearRealmCache();
        }
    }, Resource.REALM, true);
    invoke(new Invocation() {

        public void invoke(RealmResource realm) {
            realm.clearUserCache();
        }
    }, Resource.REALM, true);
    // Delete realm
    invoke(new Invocation() {

        public void invoke(RealmResource realm) {
            clients.get("master-admin").realms().realm("nosuch").remove();
        }
    }, adminClient, true);
    invoke(new Invocation() {

        public void invoke(RealmResource realm) {
            clients.get("REALM2").realms().realm(REALM_NAME).remove();
        }
    }, adminClient, false);
    invoke(new Invocation() {

        public void invoke(RealmResource realm) {
            clients.get(AdminRoles.MANAGE_USERS).realms().realm(REALM_NAME).remove();
        }
    }, adminClient, false);
    invoke(new Invocation() {

        public void invoke(RealmResource realm) {
            clients.get(AdminRoles.REALM_ADMIN).realms().realm(REALM_NAME).remove();
        }
    }, adminClient, true);
    // Revert realm removal
    recreatePermissionRealm();
}
Also used : Response(javax.ws.rs.core.Response) PartialImportRepresentation(org.keycloak.representations.idm.PartialImportRepresentation) GroupRepresentation(org.keycloak.representations.idm.GroupRepresentation) RealmResource(org.keycloak.admin.client.resource.RealmResource) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) AbstractKeycloakTest(org.keycloak.testsuite.AbstractKeycloakTest) Test(org.junit.Test)

Example 2 with PartialImportRepresentation

use of org.keycloak.representations.idm.PartialImportRepresentation in project keycloak by keycloak.

the class PartialImportTest method initAdminEvents.

@Before
public void initAdminEvents() {
    RealmRepresentation realmRep = RealmBuilder.edit(testRealmResource().toRepresentation()).testEventListener().build();
    realmId = realmRep.getId();
    realmRep.setDuplicateEmailsAllowed(false);
    adminClient.realm(realmRep.getRealm()).update(realmRep);
    piRep = new PartialImportRepresentation();
}
Also used : PartialImportRepresentation(org.keycloak.representations.idm.PartialImportRepresentation) RealmRepresentation(org.keycloak.representations.idm.RealmRepresentation) Before(org.junit.Before)

Example 3 with PartialImportRepresentation

use of org.keycloak.representations.idm.PartialImportRepresentation in project keycloak by keycloak.

the class PartialImportTest method testAddClientsOverwriteServiceAccountsWithNoServiceAccounts.

@EnableFeature(value = UPLOAD_SCRIPTS, skipRestart = true)
@Test
public void testAddClientsOverwriteServiceAccountsWithNoServiceAccounts() throws IOException {
    addClients(true);
    setOverwrite();
    PartialImportResults results = doImport();
    assertEquals(NUM_ENTITIES * 2, results.getAdded());
    // check the service accounts are there
    for (int i = 0; i < NUM_ENTITIES; i++) {
        List<UserRepresentation> l = testRealmResource().users().search(ServiceAccountConstants.SERVICE_ACCOUNT_USER_PREFIX + CLIENT_PREFIX + i);
        Assert.assertEquals(1, l.size());
    }
    // re-import without service accounts enabled
    piRep = new PartialImportRepresentation();
    addClients(false);
    setOverwrite();
    results = doImport();
    assertEquals(NUM_ENTITIES, results.getOverwritten());
    // check the service accounts have been removed
    for (int i = 0; i < NUM_ENTITIES; i++) {
        List<UserRepresentation> l = testRealmResource().users().search(ServiceAccountConstants.SERVICE_ACCOUNT_USER_PREFIX + CLIENT_PREFIX + i);
        Assert.assertEquals(0, l.size());
    }
}
Also used : PartialImportRepresentation(org.keycloak.representations.idm.PartialImportRepresentation) PartialImportResults(org.keycloak.partialimport.PartialImportResults) UserRepresentation(org.keycloak.representations.idm.UserRepresentation) AbstractAuthTest(org.keycloak.testsuite.AbstractAuthTest) Test(org.junit.Test) EnableFeature(org.keycloak.testsuite.arquillian.annotation.EnableFeature)

Aggregations

PartialImportRepresentation (org.keycloak.representations.idm.PartialImportRepresentation)3 Test (org.junit.Test)2 RealmRepresentation (org.keycloak.representations.idm.RealmRepresentation)2 Response (javax.ws.rs.core.Response)1 Before (org.junit.Before)1 RealmResource (org.keycloak.admin.client.resource.RealmResource)1 PartialImportResults (org.keycloak.partialimport.PartialImportResults)1 GroupRepresentation (org.keycloak.representations.idm.GroupRepresentation)1 UserRepresentation (org.keycloak.representations.idm.UserRepresentation)1 AbstractAuthTest (org.keycloak.testsuite.AbstractAuthTest)1 AbstractKeycloakTest (org.keycloak.testsuite.AbstractKeycloakTest)1 EnableFeature (org.keycloak.testsuite.arquillian.annotation.EnableFeature)1