use of org.keycloak.client.admin.cli.config.FileConfigHandler in project keycloak by keycloak.
the class AbstractAuthOptionsCmd method processGlobalOptions.
protected void processGlobalOptions() {
super.processGlobalOptions();
if (config != null && noconfig) {
throw new RuntimeException("Options --config and --no-config are mutually exclusive");
}
if (!noconfig) {
setConfigFile(config != null ? config : ConfigUtil.DEFAULT_CONFIG_FILE_PATH);
ConfigUtil.setHandler(new FileConfigHandler());
} else {
InMemoryConfigHandler handler = new InMemoryConfigHandler();
ConfigData data = new ConfigData();
initConfigData(data);
handler.setConfigData(data);
ConfigUtil.setHandler(handler);
}
}
use of org.keycloak.client.admin.cli.config.FileConfigHandler in project keycloak by keycloak.
the class KcAdmSessionTest method test.
@Test
public void test() throws IOException {
FileConfigHandler handler = initCustomConfigFile();
try (TempFileResource configFile = new TempFileResource(handler.getConfigFile())) {
// login as admin
loginAsUser(configFile.getFile(), serverUrl, "master", "admin", "admin");
// create realm
KcAdmExec exe = execute("create realms --config '" + configFile.getName() + "' -s realm=demorealm -s enabled=true");
assertExitCodeAndStreamSizes(exe, 0, 0, 1);
Assert.assertTrue(exe.stderrLines().get(0).startsWith("Created "));
// create user
exe = execute("create users --config '" + configFile.getName() + "' -r demorealm -s username=testuser -s enabled=true -i");
assertExitCodeAndStreamSizes(exe, 0, 1, 0);
String userId = exe.stdoutLines().get(0);
// add realm admin capabilities to user
exe = execute("add-roles --config '" + configFile.getName() + "' -r demorealm --uusername testuser --cclientid realm-management --rolename realm-admin");
assertExitCodeAndStreamSizes(exe, 0, 0, 0);
// set password for the user
exe = execute("set-password --config '" + configFile.getName() + "' -r demorealm --username testuser -p password");
assertExitCodeAndStdErrSize(exe, 0, 0);
// login as testuser
loginAsUser(configFile.getFile(), serverUrl, "demorealm", "testuser", "password");
// get realm roles
exe = execute("get-roles --config '" + configFile.getName() + "'");
assertExitCodeAndStdErrSize(exe, 0, 0);
List<ObjectNode> roles = loadJson(exe.stdout(), LIST_OF_JSON);
Assert.assertThat("expected three realm roles available", roles.size(), equalTo(3));
// create realm role
exe = execute("create roles --config '" + configFile.getName() + "' -s name=testrole -s 'description=Test role' -o");
assertExitCodeAndStdErrSize(exe, 0, 0);
ObjectNode role = loadJson(exe.stdout(), ObjectNode.class);
Assert.assertEquals("testrole", role.get("name").asText());
String roleId = role.get("id").asText();
// get realm roles again
exe = execute("get-roles --config '" + configFile.getName() + "'");
assertExitCodeAndStdErrSize(exe, 0, 0);
roles = loadJson(exe.stdout(), LIST_OF_JSON);
Assert.assertThat("expected four realm roles available", roles.size(), equalTo(4));
// create client
exe = execute("create clients --config '" + configFile.getName() + "' -s clientId=testclient -i");
assertExitCodeAndStreamSizes(exe, 0, 1, 0);
String idOfClient = exe.stdoutLines().get(0);
// create client role
exe = execute("create clients/" + idOfClient + "/roles --config '" + configFile.getName() + "' -s name=clientrole -s 'description=Test client role'");
assertExitCodeAndStreamSizes(exe, 0, 0, 1);
Assert.assertTrue(exe.stderrLines().get(0).startsWith("Created "));
// make sure client role has been created
exe = execute("get-roles --config '" + configFile.getName() + "' --cclientid testclient");
assertExitCodeAndStdErrSize(exe, 0, 0);
roles = loadJson(exe.stdout(), LIST_OF_JSON);
Assert.assertThat("expected one role", roles.size(), equalTo(1));
Assert.assertEquals("clientrole", roles.get(0).get("name").asText());
// add created role to user - we are realm admin so we can add role to ourself
exe = execute("add-roles --config '" + configFile.getName() + "' --uusername testuser --cclientid testclient --rolename clientrole");
assertExitCodeAndStreamSizes(exe, 0, 0, 0);
// make sure the roles have been added
exe = execute("get-roles --config '" + configFile.getName() + "' --uusername testuser --all");
assertExitCodeAndStdErrSize(exe, 0, 0);
ObjectNode node = loadJson(exe.stdout(), ObjectNode.class);
Assert.assertNotNull(node.get("realmMappings"));
List<String> realmMappings = StreamSupport.stream(node.get("realmMappings").spliterator(), false).map(o -> o.get("name").asText()).sorted().collect(Collectors.toList());
Assert.assertEquals(Arrays.asList("default-roles-demorealm"), realmMappings);
ObjectNode clientRoles = (ObjectNode) node.get("clientMappings");
// List<String> fields = asSortedList(clientRoles.fieldNames());
List<String> fields = StreamSupport.stream(clientRoles.spliterator(), false).map(o -> o.get("client").asText()).sorted().collect(Collectors.toList());
Assert.assertEquals(Arrays.asList("realm-management", "testclient"), fields);
realmMappings = StreamSupport.stream(clientRoles.get("realm-management").get("mappings").spliterator(), false).map(o -> o.get("name").asText()).sorted().collect(Collectors.toList());
Assert.assertEquals(Arrays.asList("realm-admin"), realmMappings);
realmMappings = StreamSupport.stream(clientRoles.get("testclient").get("mappings").spliterator(), false).map(o -> o.get("name").asText()).sorted().collect(Collectors.toList());
Assert.assertEquals(Arrays.asList("clientrole"), realmMappings);
// add a realm role to the user
exe = execute("add-roles --config '" + configFile.getName() + "' --uusername testuser --rolename testrole");
assertExitCodeAndStreamSizes(exe, 0, 0, 0);
// get all roles for the user again
exe = execute("get-roles --config '" + configFile.getName() + "' --uusername testuser --all");
assertExitCodeAndStdErrSize(exe, 0, 0);
node = loadJson(exe.stdout(), ObjectNode.class);
Assert.assertNotNull(node.get("realmMappings"));
realmMappings = StreamSupport.stream(node.get("realmMappings").spliterator(), false).map(o -> o.get("name").asText()).sorted().collect(Collectors.toList());
Assert.assertEquals(Arrays.asList("default-roles-demorealm", "testrole"), realmMappings);
// create a group
exe = execute("create groups --config '" + configFile.getName() + "' -s name=TestUsers -i");
assertExitCodeAndStdErrSize(exe, 0, 0);
String groupId = exe.stdoutLines().get(0);
// create a sub-group
exe = execute("create groups/" + groupId + "/children --config '" + configFile.getName() + "' -s name=TestPowerUsers -i");
assertExitCodeAndStdErrSize(exe, 0, 0);
String subGroupId = exe.stdoutLines().get(0);
// add testuser to TestPowerUsers
exe = execute("update users/" + userId + "/groups/" + subGroupId + " --config '" + configFile.getName() + "' -s realm=demorealm -s userId=" + userId + " -s groupId=" + subGroupId + " -n");
assertExitCodeAndStreamSizes(exe, 0, 0, 0);
// delete everything
exe = execute("delete groups/" + subGroupId + " --config '" + configFile.getName() + "'");
assertExitCodeAndStreamSizes(exe, 0, 0, 0);
exe = execute("delete groups/" + groupId + " --config '" + configFile.getName() + "'");
assertExitCodeAndStreamSizes(exe, 0, 0, 0);
exe = execute("delete clients/" + idOfClient + " --config '" + configFile.getName() + "'");
assertExitCodeAndStreamSizes(exe, 0, 0, 0);
exe = execute("delete roles/testrole --config '" + configFile.getName() + "'");
assertExitCodeAndStreamSizes(exe, 0, 0, 0);
exe = execute("delete users/" + userId + " --config '" + configFile.getName() + "'");
assertExitCodeAndStreamSizes(exe, 0, 0, 0);
// delete realm as well - using initial master realm session still saved in config file
exe = execute("delete realms/demorealm --config '" + configFile.getName() + "' --realm master");
assertExitCodeAndStreamSizes(exe, 0, 0, 0);
}
}
use of org.keycloak.client.admin.cli.config.FileConfigHandler in project keycloak by keycloak.
the class AbstractAdmCliTest method initCustomConfigFile.
FileConfigHandler initCustomConfigFile() {
String filename = UUID.randomUUID().toString() + ".config";
File cfgFile = new File(WORK_DIR + "/" + filename);
FileConfigHandler handler = new FileConfigHandler();
handler.setConfigFile(cfgFile.getAbsolutePath());
return handler;
}
use of org.keycloak.client.admin.cli.config.FileConfigHandler in project keycloak by keycloak.
the class KcAdmCreateTest method testCreateIDPWithoutSyncMode.
@Test
public void testCreateIDPWithoutSyncMode() throws IOException {
final String realm = "test";
final RealmResource realmResource = adminClient.realm(realm);
FileConfigHandler handler = initCustomConfigFile();
try (TempFileResource configFile = new TempFileResource(handler.getConfigFile())) {
loginAsUser(configFile.getFile(), serverUrl, realm, "user1", "userpass");
final File idpJson = new File("target/test-classes/cli/idp-keycloak-without-sync-mode.json");
KcAdmExec exe = execute("create identity-provider/instances/ -r " + realm + " -f " + idpJson.getAbsolutePath() + " --config " + configFile.getFile());
assertExitCodeAndStdErrSize(exe, 0, 1);
}
// If the sync mode is not present on creating the idp, it will never be added automatically. However, the model will always assume "LEGACY", so no errors should occur.
Assert.assertNull(realmResource.identityProviders().get("idpAlias").toRepresentation().getConfig().get(IdentityProviderModel.SYNC_MODE));
}
use of org.keycloak.client.admin.cli.config.FileConfigHandler in project keycloak by keycloak.
the class KcAdmUpdateTest method testUpdateIDPWithoutInternalId.
@Test
public void testUpdateIDPWithoutInternalId() throws IOException {
final String realm = "test";
final RealmResource realmResource = adminClient.realm(realm);
IdentityProviderRepresentation identityProvider = IdentityProviderBuilder.create().providerId(SAMLIdentityProviderFactory.PROVIDER_ID).alias("idpAlias").displayName("SAML").setAttribute(SAMLIdentityProviderConfig.SINGLE_SIGN_ON_SERVICE_URL, "https://saml.idp/saml").setAttribute(SAMLIdentityProviderConfig.SINGLE_LOGOUT_SERVICE_URL, "https://saml.idp/saml").setAttribute(SAMLIdentityProviderConfig.NAME_ID_POLICY_FORMAT, "urn:oasis:names:tc:SAML:1.1:nameid-format:emailAddress").setAttribute(SAMLIdentityProviderConfig.POST_BINDING_RESPONSE, "false").setAttribute(SAMLIdentityProviderConfig.POST_BINDING_AUTHN_REQUEST, "false").setAttribute(SAMLIdentityProviderConfig.BACKCHANNEL_SUPPORTED, "false").build();
try (Closeable ipc = new IdentityProviderCreator(realmResource, identityProvider)) {
FileConfigHandler handler = initCustomConfigFile();
try (TempFileResource configFile = new TempFileResource(handler.getConfigFile())) {
loginAsUser(configFile.getFile(), serverUrl, realm, "user1", "userpass");
KcAdmExec exe = execute("get identity-provider/instances/idpAlias -r " + realm + " --config " + configFile.getFile());
assertExitCodeAndStdErrSize(exe, 0, 0);
final File idpJson = new File("target/test-classes/cli/idp-keycloak-9167.json");
exe = execute("update identity-provider/instances/idpAlias -r " + realm + " -f " + idpJson.getAbsolutePath() + " --config " + configFile.getFile());
assertExitCodeAndStdErrSize(exe, 0, 0);
}
Assert.assertThat(realmResource.identityProviders().get("idpAlias").toRepresentation().getDisplayName(), is(equalTo("SAML_UPDATED")));
}
}
Aggregations