Search in sources :

Example 6 with ConfigData

use of org.keycloak.client.admin.cli.config.ConfigData in project keycloak by keycloak.

the class SetPasswordCmd method process.

public CommandResult process(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
    if (args != null && args.size() > 0) {
        throw new IllegalArgumentException("Invalid option: " + args.get(0));
    }
    if (userid == null && username == null) {
        throw new IllegalArgumentException("No user specified. Use --username or --userid to specify user");
    }
    if (userid != null && username != null) {
        throw new IllegalArgumentException("Options --userid and --username are mutually exclusive");
    }
    if (pass == null) {
        pass = readSecret("Enter password: ", commandInvocation);
    }
    ConfigData config = loadConfig();
    config = copyWithServerInfo(config);
    setupTruststore(config, commandInvocation);
    String auth = null;
    config = ensureAuthInfo(config, commandInvocation);
    config = copyWithServerInfo(config);
    if (credentialsAvailable(config)) {
        auth = ensureToken(config);
    }
    auth = auth != null ? "Bearer " + auth : null;
    final String server = config.getServerUrl();
    final String realm = getTargetRealm(config);
    final String adminRoot = adminRestRoot != null ? adminRestRoot : composeAdminRoot(server);
    // if username is specified resolve id
    if (username != null) {
        userid = getIdFromUsername(adminRoot, realm, auth, username);
    }
    resetUserPassword(adminRoot, realm, auth, userid, pass, temporary);
    return CommandResult.SUCCESS;
}
Also used : ConfigData(org.keycloak.client.admin.cli.config.ConfigData)

Example 7 with ConfigData

use of org.keycloak.client.admin.cli.config.ConfigData in project keycloak by keycloak.

the class GetRolesCmd method process.

public CommandResult process(CommandInvocation commandInvocation) throws CommandException, InterruptedException {
    ConfigData config = loadConfig();
    config = copyWithServerInfo(config);
    setupTruststore(config, commandInvocation);
    String auth = null;
    config = ensureAuthInfo(config, commandInvocation);
    config = copyWithServerInfo(config);
    if (credentialsAvailable(config)) {
        auth = ensureToken(config);
    }
    auth = auth != null ? "Bearer " + auth : null;
    final String server = config.getServerUrl();
    final String realm = getTargetRealm(config);
    final String adminRoot = adminRestRoot != null ? adminRestRoot : composeAdminRoot(server);
    if (isUserSpecified()) {
        if (uid == null) {
            uid = UserOperations.getIdFromUsername(adminRoot, realm, auth, uusername);
        }
        if (isClientSpecified()) {
            // list client roles for a user
            if (cid == null) {
                cid = ClientOperations.getIdFromClientId(adminRoot, realm, auth, cclientid);
            }
            if (available) {
                super.url = composeResourceUrl(adminRoot, realm, "users/" + uid + "/role-mappings/clients/" + cid + "/available");
            } else if (effective) {
                super.url = composeResourceUrl(adminRoot, realm, "users/" + uid + "/role-mappings/clients/" + cid + "/composite");
            } else {
                super.url = composeResourceUrl(adminRoot, realm, "users/" + uid + "/role-mappings/clients/" + cid);
            }
        } else {
            // list realm roles for a user
            if (available) {
                super.url = composeResourceUrl(adminRoot, realm, "users/" + uid + "/role-mappings/realm/available");
            } else if (effective) {
                super.url = composeResourceUrl(adminRoot, realm, "users/" + uid + "/role-mappings/realm/composite");
            } else {
                super.url = composeResourceUrl(adminRoot, realm, "users/" + uid + (all ? "/role-mappings" : "/role-mappings/realm"));
            }
        }
    } else if (isGroupSpecified()) {
        if (gname != null) {
            gid = GroupOperations.getIdFromName(adminRoot, realm, auth, gname);
        } else if (gpath != null) {
            gid = GroupOperations.getIdFromPath(adminRoot, realm, auth, gpath);
        }
        if (isClientSpecified()) {
            // list client roles for a group
            if (cid == null) {
                cid = ClientOperations.getIdFromClientId(adminRoot, realm, auth, cclientid);
            }
            if (available) {
                super.url = composeResourceUrl(adminRoot, realm, "groups/" + gid + "/role-mappings/clients/" + cid + "/available");
            } else if (effective) {
                super.url = composeResourceUrl(adminRoot, realm, "groups/" + gid + "/role-mappings/clients/" + cid + "/composite");
            } else {
                super.url = composeResourceUrl(adminRoot, realm, "groups/" + gid + "/role-mappings/clients/" + cid);
            }
        } else {
            // list realm roles for a group
            if (available) {
                super.url = composeResourceUrl(adminRoot, realm, "groups/" + gid + "/role-mappings/realm/available");
            } else if (effective) {
                super.url = composeResourceUrl(adminRoot, realm, "groups/" + gid + "/role-mappings/realm/composite");
            } else {
                super.url = composeResourceUrl(adminRoot, realm, "groups/" + gid + (all ? "/role-mappings" : "/role-mappings/realm"));
            }
        }
    } else if (isCompositeRoleSpecified()) {
        String uri = rname != null ? "roles/" + rname : "roles-by-id/" + rid;
        if (isClientSpecified()) {
            if (cid == null) {
                cid = ClientOperations.getIdFromClientId(adminRoot, realm, auth, cclientid);
            }
            if (available) {
                throw new IllegalArgumentException("Option --available not supported with composite roles. Try '" + CMD + " get-roles --cid " + cid + "' for full list of client roles for that client");
            }
            if (effective) {
                throw new IllegalArgumentException("Option --effective not supported with composite roles.");
            }
            uri += "/composites/clients/" + cid;
        } else {
            if (available) {
                throw new IllegalArgumentException("Option --available not supported with composite roles. Try '" + CMD + " get-roles' for full list of realm roles");
            }
            if (effective) {
                throw new IllegalArgumentException("Option --effective not supported with composite roles.");
            }
            uri += all ? "/composites" : "/composites/realm";
        }
        super.url = composeResourceUrl(adminRoot, realm, uri);
    } else if (isClientSpecified()) {
        if (cid == null) {
            cid = ClientOperations.getIdFromClientId(adminRoot, realm, auth, cclientid);
        }
        if (isRoleSpecified()) {
            // get specific client role
            if (rolename == null) {
                rolename = RoleOperations.getClientRoleNameFromId(adminRoot, realm, auth, cid, roleid);
            }
            super.url = composeResourceUrl(adminRoot, realm, "clients/" + cid + "/roles/" + rolename);
        } else {
            // list defined client roles
            super.url = composeResourceUrl(adminRoot, realm, "clients/" + cid + "/roles");
        }
    } else {
        if (isRoleSpecified()) {
            // get specific realm role
            if (rolename == null) {
                rolename = RoleOperations.getClientRoleNameFromId(adminRoot, realm, auth, cid, roleid);
            }
            super.url = composeResourceUrl(adminRoot, realm, "roles/" + rolename);
        } else {
            // list defined realm roles
            super.url = composeResourceUrl(adminRoot, realm, "roles");
        }
    }
    return super.process(commandInvocation);
}
Also used : ConfigData(org.keycloak.client.admin.cli.config.ConfigData)

Example 8 with ConfigData

use of org.keycloak.client.admin.cli.config.ConfigData in project keycloak by keycloak.

the class KcAdmTest method testCustomConfigLoginCreateDelete.

@Test
public void testCustomConfigLoginCreateDelete() throws IOException {
    /*
         *  Test user login, create, delete session using a custom config file
         */
    // prepare for loading a config file
    FileConfigHandler handler = initCustomConfigFile();
    try (TempFileResource configFile = new TempFileResource(handler.getConfigFile())) {
        KcAdmExec exe = KcAdmExec.execute("config credentials --server " + serverUrl + " --realm master --user admin --password admin --config '" + configFile.getName() + "'");
        assertExitCodeAndStreamSizes(exe, 0, 0, 1);
        // remember the state of config file
        ConfigData config1 = handler.loadConfig();
        exe = KcAdmExec.execute("create --config '" + configFile.getName() + "' clients -s clientId=test-client -o");
        assertExitCodeAndStdErrSize(exe, 0, 0);
        // check changes to config file
        ConfigData config2 = handler.loadConfig();
        assertFieldsEqualWithExclusions(config1, config2);
        ClientRepresentation client = JsonSerialization.readValue(exe.stdout(), ClientRepresentation.class);
        Assert.assertEquals("clientId", "test-client", client.getClientId());
        exe = KcAdmExec.execute("delete clients/" + client.getId() + " --config '" + configFile.getName() + "'");
        assertExitCodeAndStreamSizes(exe, 0, 0, 0);
        // check changes to config file
        ConfigData config3 = handler.loadConfig();
        assertFieldsEqualWithExclusions(config2, config3);
    }
}
Also used : FileConfigHandler(org.keycloak.client.admin.cli.config.FileConfigHandler) RealmConfigData(org.keycloak.client.admin.cli.config.RealmConfigData) ConfigData(org.keycloak.client.admin.cli.config.ConfigData) KcAdmExec(org.keycloak.testsuite.cli.KcAdmExec) TempFileResource(org.keycloak.testsuite.util.TempFileResource) ClientRepresentation(org.keycloak.representations.idm.ClientRepresentation) Test(org.junit.Test)

Example 9 with ConfigData

use of org.keycloak.client.admin.cli.config.ConfigData in project keycloak by keycloak.

the class KcAdmTest method testUserLoginWithCustomConfig.

@Test
public void testUserLoginWithCustomConfig() {
    /*
         *  Test user login using a custom config file
         */
    FileConfigHandler handler = initCustomConfigFile();
    File configFile = new File(handler.getConfigFile());
    try {
        KcAdmExec exe = KcAdmExec.execute("config credentials --server " + serverUrl + " --realm master" + " --user admin --password admin --config '" + configFile.getName() + "'");
        assertExitCodeAndStreamSizes(exe, 0, 0, 1);
        Assert.assertEquals("stderr first line", "Logging into " + serverUrl + " as user admin of realm master", exe.stderrLines().get(0));
        // make sure the config file exists, and has the right content
        ConfigData config = handler.loadConfig();
        Assert.assertEquals("serverUrl", serverUrl, config.getServerUrl());
        Assert.assertEquals("realm", "master", config.getRealm());
        RealmConfigData realmcfg = config.sessionRealmConfigData();
        Assert.assertNotNull("realm config data no null", realmcfg);
        Assert.assertEquals("realm cfg serverUrl", serverUrl, realmcfg.serverUrl());
        Assert.assertEquals("realm cfg realm", "master", realmcfg.realm());
        Assert.assertEquals("client id", "admin-cli", realmcfg.getClientId());
        Assert.assertNotNull("token not null", realmcfg.getToken());
        Assert.assertNotNull("refresh token not null", realmcfg.getRefreshToken());
        Assert.assertNotNull("token expires not null", realmcfg.getExpiresAt());
        Assert.assertNotNull("token expires in future", realmcfg.getExpiresAt() > System.currentTimeMillis());
        Assert.assertNotNull("refresh token expires not null", realmcfg.getRefreshExpiresAt());
        Assert.assertNotNull("refresh token expires in future", realmcfg.getRefreshExpiresAt() > System.currentTimeMillis());
    } finally {
        configFile.delete();
    }
}
Also used : RealmConfigData(org.keycloak.client.admin.cli.config.RealmConfigData) FileConfigHandler(org.keycloak.client.admin.cli.config.FileConfigHandler) RealmConfigData(org.keycloak.client.admin.cli.config.RealmConfigData) ConfigData(org.keycloak.client.admin.cli.config.ConfigData) KcAdmExec(org.keycloak.testsuite.cli.KcAdmExec) File(java.io.File) Test(org.junit.Test)

Example 10 with ConfigData

use of org.keycloak.client.admin.cli.config.ConfigData in project keycloak by keycloak.

the class KcAdmTruststoreTest method testTruststore.

@Test
public void testTruststore() throws IOException {
    File truststore = new File("src/test/resources/keystore/keycloak.truststore");
    KcAdmExec exe = execute("config truststore --no-config '" + truststore.getAbsolutePath() + "'");
    assertExitCodeAndStreamSizes(exe, 1, 0, 2);
    Assert.assertEquals("stderr first line", "Unsupported option: --no-config", exe.stderrLines().get(0));
    Assert.assertEquals("try help", "Try '" + OsUtil.CMD + " help config truststore' for more information", exe.stderrLines().get(1));
    // only run this test if ssl protected keycloak server is available
    if (!AUTH_SERVER_SSL_REQUIRED) {
        System.out.println("TEST SKIPPED - This test requires HTTPS. Run with '-Pauth-server-wildfly -Dauth.server.ssl.required=true'");
        return;
    }
    FileConfigHandler handler = initCustomConfigFile();
    try (TempFileResource configFile = new TempFileResource(handler.getConfigFile())) {
        if (runIntermittentlyFailingTests()) {
            // configure truststore
            exe = execute("config truststore --config '" + configFile.getName() + "' '" + truststore.getAbsolutePath() + "'");
            assertExitCodeAndStreamSizes(exe, 0, 0, 0);
            // perform authentication against server - asks for password, then for truststore password
            exe = KcAdmExec.newBuilder().argsLine("config credentials --server " + oauth.AUTH_SERVER_ROOT + " --realm test --user user1" + " --config '" + configFile.getName() + "'").executeAsync();
            exe.waitForStdout("Enter password: ");
            exe.sendToStdin("userpass" + EOL);
            exe.waitForStdout("Enter truststore password: ");
            exe.sendToStdin("secret" + EOL);
            exe.waitCompletion();
            assertExitCodeAndStreamSizes(exe, 0, 2, 1);
            // configure truststore with password
            exe = execute("config truststore --config '" + configFile.getName() + "' --trustpass secret '" + truststore.getAbsolutePath() + "'");
            assertExitCodeAndStreamSizes(exe, 0, 0, 0);
            // perform authentication against server - asks for password, then for truststore password
            exe = KcAdmExec.newBuilder().argsLine("config credentials --server " + oauth.AUTH_SERVER_ROOT + " --realm test --user user1" + " --config '" + configFile.getName() + "'").executeAsync();
            exe.waitForStdout("Enter password: ");
            exe.sendToStdin("userpass" + EOL);
            exe.waitCompletion();
            assertExitCodeAndStreamSizes(exe, 0, 1, 1);
        } else {
            System.out.println("TEST SKIPPED PARTIALLY - This test currently suffers from intermittent failures. Use -Dtest.intermittent=true to run it in full.");
        }
    }
    // configure truststore with password
    exe = execute("config truststore --trustpass secret '" + truststore.getAbsolutePath() + "'");
    assertExitCodeAndStreamSizes(exe, 0, 0, 0);
    // perform authentication against server - asks for password, then for truststore password
    exe = execute("config credentials --server " + serverUrl + " --realm test --user user1 --password userpass");
    assertExitCodeAndStreamSizes(exe, 0, 0, 1);
    exe = execute("config truststore --delete");
    assertExitCodeAndStreamSizes(exe, 0, 0, 0);
    exe = execute("config truststore --delete '" + truststore.getAbsolutePath() + "'");
    assertExitCodeAndStreamSizes(exe, 1, 0, 2);
    Assert.assertEquals("incompatible", "Option --delete is mutually exclusive with specifying a TRUSTSTORE", exe.stderrLines().get(0));
    Assert.assertEquals("try help", "Try '" + CMD + " help config truststore' for more information", exe.stderrLines().get(1));
    exe = execute("config truststore --delete --trustpass secret");
    assertExitCodeAndStreamSizes(exe, 1, 0, 2);
    Assert.assertEquals("no truststore error", "Options --trustpass and --delete are mutually exclusive", exe.stderrLines().get(0));
    Assert.assertEquals("try help", "Try '" + CMD + " help config truststore' for more information", exe.stderrLines().get(1));
    FileConfigHandler cfghandler = new FileConfigHandler();
    cfghandler.setConfigFile(DEFAULT_CONFIG_FILE_PATH);
    ConfigData config = cfghandler.loadConfig();
    Assert.assertNull("truststore null", config.getTruststore());
    Assert.assertNull("trustpass null", config.getTrustpass());
    // perform no-config CRUD test against ssl protected endpoint
    testCRUDWithOnTheFlyAuth(serverUrl, "--user user1 --password userpass", " --truststore '" + truststore.getAbsolutePath() + "' --trustpass secret", "Logging into " + serverUrl + " as user user1 of realm test");
}
Also used : FileConfigHandler(org.keycloak.client.admin.cli.config.FileConfigHandler) ConfigData(org.keycloak.client.admin.cli.config.ConfigData) KcAdmExec(org.keycloak.testsuite.cli.KcAdmExec) File(java.io.File) TempFileResource(org.keycloak.testsuite.util.TempFileResource) Test(org.junit.Test)

Aggregations

ConfigData (org.keycloak.client.admin.cli.config.ConfigData)10 FileConfigHandler (org.keycloak.client.admin.cli.config.FileConfigHandler)4 RealmConfigData (org.keycloak.client.admin.cli.config.RealmConfigData)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 Test (org.junit.Test)3 KcAdmExec (org.keycloak.testsuite.cli.KcAdmExec)3 File (java.io.File)2 ArrayList (java.util.ArrayList)2 HashSet (java.util.HashSet)2 LinkedList (java.util.LinkedList)2 List (java.util.List)2 Set (java.util.Set)2 LocalSearch (org.keycloak.client.admin.cli.operations.LocalSearch)2 TempFileResource (org.keycloak.testsuite.util.TempFileResource)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1