Skip to content

Commit

Permalink
Add configuration to disable bootstrap of admin account
Browse files Browse the repository at this point in the history
  • Loading branch information
marioschlipf committed Nov 5, 2024
1 parent 6221e6d commit f2510aa
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public abstract class ExtendableKeycloakContainer<SELF extends ExtendableKeycloa
private List<File> providerLibsLocations;
private List<String> customCommandParts;

private boolean bootstrapAdmin = true;

/**
* Create a KeycloakContainer with default image and version tag
*/
Expand Down Expand Up @@ -164,8 +166,11 @@ protected void configure() {
withEnv("KC_FEATURES_DISABLED", String.join(",", featuresDisabled));
}

withEnv("KC_BOOTSTRAP_ADMIN_USERNAME", adminUsername);
withEnv("KC_BOOTSTRAP_ADMIN_PASSWORD", adminPassword);
if(bootstrapAdmin) {
withEnv("KC_BOOTSTRAP_ADMIN_USERNAME", adminUsername);
withEnv("KC_BOOTSTRAP_ADMIN_PASSWORD", adminPassword);
}

withEnv("JAVA_OPTS_KC_HEAP", "-XX:InitialRAMPercentage=%d -XX:MaxRAMPercentage=%d".formatted(initialRamPercentage, maxRamPercentage));

if (useTls && isNotBlank(tlsCertificateFilename)) {
Expand Down Expand Up @@ -515,6 +520,16 @@ private SELF withDebug(int hostPort, boolean suspend) {
return self();
}

/** Disable default bootstrapping of the keycloak admin. Useful when realms are imported. */
public SELF withoutBootstrapAdmin() {
this.bootstrapAdmin = false;
return self();
}

/**
* Returns the keycloak admin. Note that this may not return a functioning admin client
* if the master realm including users were imported.
*/
public Keycloak getKeycloakAdminClient() {
if (useTls) {
return Keycloak.getInstance(getAuthServerUrl(), MASTER_REALM, getAdminUsername(), getAdminPassword(), ADMIN_CLI_CLIENT, buildSslContext());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dasniko.testcontainers.keycloak;

import io.restassured.response.ValidatableResponse;
import jakarta.ws.rs.NotAuthorizedException;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
Expand All @@ -23,6 +24,7 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.startsWith;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

Expand All @@ -32,6 +34,7 @@
public class KeycloakContainerTest {

public static final String TEST_REALM_JSON = "/test-realm.json";
public static final String MASTER_REALM_USERS_JSON = "/master-realm.json";

@Test
public void shouldStartKeycloak() {
Expand Down Expand Up @@ -91,6 +94,22 @@ public void shouldImportMultipleRealms() {
}
}

@Test
public void shouldImportMasterRealmAdmin() {
try (KeycloakContainer keycloak = new KeycloakContainer()
.withoutBootstrapAdmin()
.withRealmImportFiles(MASTER_REALM_USERS_JSON)) {
keycloak.start();

// Throws because we have imported a different admin user with different password
assertThrows(NotAuthorizedException.class, () -> keycloak.getKeycloakAdminClient().tokenManager().getAccessToken());

// Set password from imported realm, see json file
keycloak.withAdminPassword("password");
keycloak.getKeycloakAdminClient().tokenManager().getAccessToken();
}
}

@Test
public void shouldReturnServerInfo() {
try (KeycloakContainer keycloak = new KeycloakContainer()) {
Expand Down
23 changes: 23 additions & 0 deletions src/test/resources/master-realm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"realm": "master",
"enabled": true,
"users": [
{
"username": "admin",
"firstName": "Example",
"lastName": "User",
"email": "example@keycloak.org",
"enabled": true,
"credentials": [
{
"type": "password",
"value": "password"
}
],
"realmRoles": [
"admin",
"default-roles-master"
]
}
]
}

0 comments on commit f2510aa

Please sign in to comment.