-
Notifications
You must be signed in to change notification settings - Fork 41
/
KeycloakContainer.java
116 lines (94 loc) · 3.33 KB
/
KeycloakContainer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.hiddenswitch.containers;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.containers.wait.strategy.WaitStrategy;
import org.testcontainers.utility.MountableFile;
import java.time.Duration;
public class KeycloakContainer extends GenericContainer<KeycloakContainer> {
private static final int KEYCLOAK_PORT_HTTP = 8080;
private static final String KEYCLOAK_ADMIN_USER = "admin";
private static final String KEYCLOAK_ADMIN_PASSWORD = "admin";
private static final String KEYCLOAK_AUTH_PATH = "/auth";
private String adminUsername = KEYCLOAK_ADMIN_USER;
private String adminPassword = KEYCLOAK_ADMIN_PASSWORD;
private String importFile;
private String tlsCertFilename;
private String tlsKeyFilename;
private boolean useTls = false;
/**
* Create a KeycloakContainer by passing the full docker image name
*/
public KeycloakContainer() {
super("doctorpangloss/keycloak-standalone:latest");
withExposedPorts(KEYCLOAK_PORT_HTTP);
withEnv("LANGUAGE", "en_US.UTF-8");
withEnv("LANG", "en_US.UTF-8");
withEnv("LC_ALL", "en_US.UTF-8");
withReuse(false);
setWaitStrategy(Wait
.forHttp(KEYCLOAK_AUTH_PATH)
.forPort(KEYCLOAK_PORT_HTTP)
.withStartupTimeout(Duration.ofSeconds(60))
);
}
@Override
protected void configure() {
withCommand(
"-c standalone.xml", // don't start infinispan cluster
"-Dkeycloak.profile.feature.upload_scripts=enabled" // enable script uploads
);
withEnv("KEYCLOAK_USER", adminUsername);
withEnv("KEYCLOAK_PASSWORD", adminPassword);
withEnv("DB_SCHEMA", "keycloak");
if (importFile != null) {
String importFileInContainer = "/tmp/" + importFile;
withCopyFileToContainer(MountableFile.forClasspathResource(importFile), importFileInContainer);
withEnv("KEYCLOAK_IMPORT", importFileInContainer);
}
}
public KeycloakContainer withRealmImportFile(String importFile) {
this.importFile = importFile;
return self();
}
public KeycloakContainer withAdminUsername(String adminUsername) {
this.adminUsername = adminUsername;
return self();
}
public KeycloakContainer withAdminPassword(String adminPassword) {
this.adminPassword = adminPassword;
return self();
}
public KeycloakContainer useTls() {
// tls.crt and tls.key are provided with this testcontainer
return useTls("tls.crt", "tls.key");
}
public KeycloakContainer useTls(String tlsCertFilename, String tlsKeyFilename) {
this.tlsCertFilename = tlsCertFilename;
this.tlsKeyFilename = tlsKeyFilename;
this.useTls = true;
return self();
}
public String getAuthServerUrl() {
return String.format("http%s://%s:%s%s", "", getContainerIpAddress(), getMappedPort(KEYCLOAK_PORT_HTTP), KEYCLOAK_AUTH_PATH);
}
public KeycloakContainer withPostgres(String postgresHostPort, String databaseName, String username, String password) {
withEnv("DB_VENDOR", "postgres");
withEnv("DB_ADDR", postgresHostPort);
withEnv("DB_USER", username);
withEnv("DB_PASSWORD", password);
withEnv("DB_DATABASE", databaseName);
return self();
}
public String getAdminUsername() {
return adminUsername;
}
public String getAdminPassword() {
return adminPassword;
}
public int getHttpPort() {
return getMappedPort(KEYCLOAK_PORT_HTTP);
}
private boolean isNotBlank(String s) {
return s != null && !s.trim().isEmpty();
}
}