Skip to content

Commit f2510aa

Browse files
committed
Add configuration to disable bootstrap of admin account
1 parent 6221e6d commit f2510aa

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

src/main/java/dasniko/testcontainers/keycloak/ExtendableKeycloakContainer.java

+17-2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ public abstract class ExtendableKeycloakContainer<SELF extends ExtendableKeycloa
125125
private List<File> providerLibsLocations;
126126
private List<String> customCommandParts;
127127

128+
private boolean bootstrapAdmin = true;
129+
128130
/**
129131
* Create a KeycloakContainer with default image and version tag
130132
*/
@@ -164,8 +166,11 @@ protected void configure() {
164166
withEnv("KC_FEATURES_DISABLED", String.join(",", featuresDisabled));
165167
}
166168

167-
withEnv("KC_BOOTSTRAP_ADMIN_USERNAME", adminUsername);
168-
withEnv("KC_BOOTSTRAP_ADMIN_PASSWORD", adminPassword);
169+
if(bootstrapAdmin) {
170+
withEnv("KC_BOOTSTRAP_ADMIN_USERNAME", adminUsername);
171+
withEnv("KC_BOOTSTRAP_ADMIN_PASSWORD", adminPassword);
172+
}
173+
169174
withEnv("JAVA_OPTS_KC_HEAP", "-XX:InitialRAMPercentage=%d -XX:MaxRAMPercentage=%d".formatted(initialRamPercentage, maxRamPercentage));
170175

171176
if (useTls && isNotBlank(tlsCertificateFilename)) {
@@ -515,6 +520,16 @@ private SELF withDebug(int hostPort, boolean suspend) {
515520
return self();
516521
}
517522

523+
/** Disable default bootstrapping of the keycloak admin. Useful when realms are imported. */
524+
public SELF withoutBootstrapAdmin() {
525+
this.bootstrapAdmin = false;
526+
return self();
527+
}
528+
529+
/**
530+
* Returns the keycloak admin. Note that this may not return a functioning admin client
531+
* if the master realm including users were imported.
532+
*/
518533
public Keycloak getKeycloakAdminClient() {
519534
if (useTls) {
520535
return Keycloak.getInstance(getAuthServerUrl(), MASTER_REALM, getAdminUsername(), getAdminPassword(), ADMIN_CLI_CLIENT, buildSslContext());

src/test/java/dasniko/testcontainers/keycloak/KeycloakContainerTest.java

+19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dasniko.testcontainers.keycloak;
22

33
import io.restassured.response.ValidatableResponse;
4+
import jakarta.ws.rs.NotAuthorizedException;
45
import org.junit.jupiter.api.Test;
56
import org.junit.jupiter.params.ParameterizedTest;
67
import org.junit.jupiter.params.provider.ValueSource;
@@ -23,6 +24,7 @@
2324
import static org.hamcrest.Matchers.is;
2425
import static org.hamcrest.Matchers.notNullValue;
2526
import static org.hamcrest.Matchers.startsWith;
27+
import static org.junit.jupiter.api.Assertions.assertThrows;
2628
import static org.junit.jupiter.api.Assertions.assertTrue;
2729
import static org.junit.jupiter.api.Assertions.fail;
2830

@@ -32,6 +34,7 @@
3234
public class KeycloakContainerTest {
3335

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

3639
@Test
3740
public void shouldStartKeycloak() {
@@ -91,6 +94,22 @@ public void shouldImportMultipleRealms() {
9194
}
9295
}
9396

97+
@Test
98+
public void shouldImportMasterRealmAdmin() {
99+
try (KeycloakContainer keycloak = new KeycloakContainer()
100+
.withoutBootstrapAdmin()
101+
.withRealmImportFiles(MASTER_REALM_USERS_JSON)) {
102+
keycloak.start();
103+
104+
// Throws because we have imported a different admin user with different password
105+
assertThrows(NotAuthorizedException.class, () -> keycloak.getKeycloakAdminClient().tokenManager().getAccessToken());
106+
107+
// Set password from imported realm, see json file
108+
keycloak.withAdminPassword("password");
109+
keycloak.getKeycloakAdminClient().tokenManager().getAccessToken();
110+
}
111+
}
112+
94113
@Test
95114
public void shouldReturnServerInfo() {
96115
try (KeycloakContainer keycloak = new KeycloakContainer()) {

src/test/resources/master-realm.json

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"realm": "master",
3+
"enabled": true,
4+
"users": [
5+
{
6+
"username": "admin",
7+
"firstName": "Example",
8+
"lastName": "User",
9+
"email": "example@keycloak.org",
10+
"enabled": true,
11+
"credentials": [
12+
{
13+
"type": "password",
14+
"value": "password"
15+
}
16+
],
17+
"realmRoles": [
18+
"admin",
19+
"default-roles-master"
20+
]
21+
}
22+
]
23+
}

0 commit comments

Comments
 (0)