-
Notifications
You must be signed in to change notification settings - Fork 88
/
start.java
382 lines (331 loc) · 17.6 KB
/
start.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.stream.Collectors;
/**
* Controller script to start the Keycloak environment.
*
* <h2>Run Keycloak with http</h2>
* <pre>{@code
* java start.java
* }</pre>
*
* <h2>Run Keycloak with https</h2>
* <pre>{@code
* java start.java --https
* }</pre>
*
* <h2>Run Keycloak with https and openldap</h2>
* <pre>{@code
* java start.java --https --openldap
* }</pre>
*
* <h2>Run Keycloak with https, openldap and postgres database</h2>
* <pre>{@code
* java start.java --https --openldap --database=postgres
* }</pre>
*/
class start {
static final String HELP_CMD = "--help";
static final String VERBOSE_OPT = "--verbose";
static final String CI_OPT = "--ci";
static final String HTTP_OPT = "--http";
static final String HTTPS_OPT = "--https";
static final String PROVISION_OPT = "--provision";
static final String OPENLDAP_OPT = "--openldap";
static final String OPA_OPT = "--opa";
static final String KEYCLOAK_OPT = "--keycloak=keycloak";
static final String POSTGRES_OPT = "--database=postgres";
static final String NATS_OPT = "--messaging=nats";
static final String ORACLE_OPT = "--database=oracle";
static final String MSSQL_OPT = "--database=mssql";
static final String MYSQL_OPT = "--database=mysql";
static final String GRAYLOG_OPT = "--logging=graylog";
static final String GRAFANA_OPT = "--grafana";
static final String PROMETHEUS_OPT = "--metrics=prometheus";
static final String EXTENSIONS_OPT = "--extensions=";
static final String EXTENSIONS_OPT_CLASSES = "classes";
static final String EXTENSIONS_OPT_JAR = "jar";
static final String DETACH_OPT = "--detach";
static final String TRACING_OPT = "--tracing";
static final String DOCKER_HOST_OPT = "--docker-host=";
public static void main(String[] args) throws Exception {
var argList = Arrays.asList(args);
var useKeycloakx = !argList.contains(KEYCLOAK_OPT); // --keycloak=keycloakx is implied by default
var useHttp = !argList.contains(HTTP_OPT + "=false"); // --http is implied by default
var useHttps = argList.contains(HTTPS_OPT) || argList.contains(HTTPS_OPT + "=true");
var useProvision = !argList.contains(PROVISION_OPT + "=false");
var useOpenLdap = argList.contains(OPENLDAP_OPT) || argList.contains(OPENLDAP_OPT + "=true");
var usePostgres = argList.contains(POSTGRES_OPT);
var useOpa = argList.contains(OPA_OPT);
var useMssql = argList.contains(MSSQL_OPT);
var useMysql = argList.contains(MYSQL_OPT);
var useOracle = argList.contains(ORACLE_OPT);
var useDatabase = usePostgres || useMysql || useMssql || useOracle;
var useGraylog = argList.contains(GRAYLOG_OPT);
var useGrafana = argList.contains(GRAFANA_OPT);
var usePrometheus = argList.contains(PROMETHEUS_OPT);
var extension = argList.stream().filter(s -> s.startsWith(EXTENSIONS_OPT)).map(s -> s.substring(s.indexOf("=") + 1)).findFirst().orElse(EXTENSIONS_OPT_CLASSES);
var ci = argList.stream().filter(s -> s.startsWith(CI_OPT)).map(s -> s.substring(s.indexOf("=") + 1)).findFirst().orElse(null);
var useDetach = argList.contains(DETACH_OPT);
var verbose = argList.contains(VERBOSE_OPT);
var useTracing = argList.contains(TRACING_OPT);
var dockerHost = argList.stream().filter(s -> s.startsWith(DOCKER_HOST_OPT)).map(s -> s.substring(s.indexOf("=") + 1)).findFirst();
var useNats = argList.contains(NATS_OPT);
var showHelp = argList.contains(HELP_CMD);
if (showHelp) {
showHelp();
System.exit(0);
return;
}
if (useDatabase && !(useMysql ^ usePostgres ^ useMssql ^ useOracle)) {
System.out.println("Invalid database configuration detected. Only one --database parameter is allowed!");
showHelp();
System.exit(-1);
}
// Keycloak
createFolderIfMissing("deployments/local/dev/run/keycloak/logs");
createFolderIfMissing("deployments/local/dev/run/keycloak/data");
createFolderIfMissing("deployments/local/dev/run/keycloak/perf");
// Keycloak-X
createFolderIfMissing("deployments/local/dev/run/keycloakx/logs");
createFolderIfMissing("deployments/local/dev/run/keycloakx/data");
createFolderIfMissing("deployments/local/dev/run/keycloakx/perf");
System.out.println("### Starting Keycloak Environment with HTTP" + (useHttps ? "S" : ""));
System.out.printf("# Keycloak: %s%n", useHttps ? "https://id.acme.test:8443/auth" : "http://localhost:8080/auth");
System.out.printf("# MailHog: %s%n", "http://localhost:1080");
if (useOpenLdap) {
System.out.printf("# PhpMyLdapAdmin: %s%n", "http://localhost:17080");
}
var envFiles = new ArrayList<String>();
var requiresBuild = true;
var commandLine = new ArrayList<String>();
commandLine.add("docker");
commandLine.add("compose");
envFiles.add("keycloak.env");
envFiles.add("deployments/local/dev/keycloak-common.env");
commandLine.add("--file");
commandLine.add("deployments/local/dev/docker-compose.yml");
commandLine.add("--file");
if (useKeycloakx) {
commandLine.add("deployments/local/dev/docker-compose-keycloakx.yml");
} else {
commandLine.add("deployments/local/dev/docker-compose-keycloak.yml");
}
if ("github".equals(ci)) {
commandLine.add("--file");
commandLine.add("deployments/local/dev/docker-compose-ci-github.yml");
}
if (useHttp) {
envFiles.add("deployments/local/dev/keycloak-http.env");
}
if (useHttps) {
envFiles.remove("deployments/local/dev/keycloak-http.env");
commandLine.add("--file");
commandLine.add("deployments/local/dev/docker-compose-tls.yml");
envFiles.add("deployments/local/dev/keycloak-tls.env");
}
if (useOpenLdap) {
commandLine.add("--file");
commandLine.add("deployments/local/dev/docker-compose-openldap.yml");
envFiles.add("deployments/local/dev/keycloak-openldap.env");
}
if (useOpa) {
commandLine.add("--file");
commandLine.add("deployments/local/dev/docker-compose-opa.yml");
}
// if (EXTENSIONS_OPT_CLASSES.equals(extension)) {
// commandLine.add("--file");
// commandLine.add("deployments/local/dev/docker-compose-extensions-classes.yml");
// } else if (EXTENSIONS_OPT_JAR.equals(extension)) {
// commandLine.add("--file");
// commandLine.add("deployments/local/dev/docker-compose-extensions-jar.yml");
// } else {
// System.err.printf("Unkown extension include option %s, valid ones are %s and %s%n", extension, EXTENSIONS_OPT_CLASSES, EXTENSIONS_OPT_JAR);
// System.exit(-1);
// }
if (usePostgres) {
commandLine.add("--file");
commandLine.add("deployments/local/dev/docker-compose-postgres.yml");
createFolderIfMissing("deployments/local/dev/run/postgres/data/");
requiresBuild = true;
} else if (useMysql) {
commandLine.add("--file");
commandLine.add("deployments/local/dev/docker-compose-mysql.yml");
createFolderIfMissing("deployments/local/dev/run/mysql/data/");
requiresBuild = true;
} else if (useMssql) {
commandLine.add("--file");
commandLine.add("deployments/local/dev/docker-compose-mssql.yml");
createFolderIfMissing("deployments/local/dev/run/mssql/data/");
requiresBuild = true;
} else if (useOracle) {
commandLine.add("--file");
commandLine.add("deployments/local/dev/docker-compose-oracle.yml");
createFolderIfMissing("deployments/local/dev/run/oracle/data/");
requiresBuild = true;
}
if (useGraylog) {
commandLine.add("--file");
commandLine.add("deployments/local/dev/docker-compose-graylog.yml");
createFolderIfMissing("deployments/local/dev/run/graylog/data/mongodb");
requiresBuild = true;
}
if (useGrafana) {
commandLine.add("--file");
commandLine.add("deployments/local/dev/docker-compose-grafana.yml");
createFolderIfMissing("deployments/local/dev/run/grafana");
}
if (usePrometheus) {
commandLine.add("--file");
commandLine.add("deployments/local/dev/docker-compose-prometheus.yml");
createFolderIfMissing("deployments/local/dev/run/prometheus");
}
if (useProvision) {
commandLine.add("--file");
commandLine.add("deployments/local/dev/docker-compose-provisioning.yml");
envFiles.add("deployments/local/dev/keycloak-provisioning.env");
}
if (useNats) {
commandLine.add("--file");
commandLine.add("deployments/local/dev/docker-compose-nats.yml");
}
if (useTracing) {
commandLine.add("--file");
commandLine.add("deployments/local/dev/docker-compose-tracing.yml");
if (useHttps) {
commandLine.add("--file");
commandLine.add("deployments/local/dev/docker-compose-tracing-tls.yml");
var certPath = Path.of("config/stage/dev/tls/acme.test+1.pem");
if (certPath.toFile().exists()) {
var targetPath = Path.of("deployments/local/dev/otel-collector").resolve(certPath.getFileName());
System.out.printf("Copy cert files for otel-collector from %s to %s%n", certPath, targetPath);
Files.copy(certPath, targetPath, StandardCopyOption.REPLACE_EXISTING);
}
var keyPath = Path.of("config/stage/dev/tls/acme.test+1-key.pem");
if (keyPath.toFile().exists()) {
var targetPath = Path.of("deployments/local/dev/otel-collector").resolve(keyPath.getFileName());
System.out.printf("Copy cert files for otel-collector from %s to %s%n", keyPath, targetPath);
Files.copy(keyPath, targetPath, StandardCopyOption.REPLACE_EXISTING);
}
}
envFiles.add("deployments/local/dev/keycloak-tracing.env");
}
if (Files.exists(Path.of("local.env"))) {
System.out.println("Adding local.env");
envFiles.add("local.env");
}
//-BEGIN env vars
StringBuilder envVariables = new StringBuilder();
for (String envFile : envFiles) {
envVariables.append(Files.readString(Paths.get(envFile))).append("\n");
}
if (useHttps) {
// add quotes around path in case of spaces in path
envVariables.append("CA_ROOT_CERT=\"" + getRootCALocation() + "/rootCA.pem\"");
envVariables.append("\n");
}
if (useHttp && useKeycloakx) {
Path certPath = Path.of("config/stage/dev/tls/acme.test+1.pem");
if (certPath.toFile().exists()) {
Path targetPath = Path.of("deployments/local/dev/keycloakx").resolve(certPath.getFileName());
System.out.printf("Copy cert file for truststore import from %s to %s%n", certPath, targetPath);
Files.copy(certPath, targetPath, StandardCopyOption.REPLACE_EXISTING);
}
}
if (dockerHost.isPresent()) {
envVariables.append(String.format("DOCKER_HOST_IP=\"%s\"", dockerHost.get()));
}
if (!envVariables.toString().isBlank()) {
String generatedEnvFile = "generated.env.tmp";
Files.writeString(Paths.get(generatedEnvFile), envVariables.toString());
commandLine.add("--env-file");
commandLine.add(generatedEnvFile);
}
//-END env vars
commandLine.add("up");
if (useDetach) {
commandLine.add("--detach");
}
if (requiresBuild) {
commandLine.add("--build");
}
commandLine.add("--remove-orphans");
if (verbose) {
System.out.printf("Generated command: %n```%n%s%n```%n",
commandLine.stream().collect(Collectors.joining(" \\\n")));
}
System.exit(runCommandAndWait(commandLine));
}
private static void showHelp() {
System.out.println("Keycloak Environment starter");
System.out.printf("%n%s supports the following options: %n", "start.java");
System.out.println("");
System.out.printf(" %s: %s%n", HTTP_OPT, "enables HTTP support.");
System.out.printf(" %s: %s%n", HTTPS_OPT, "enables HTTPS support. (Optional) Implies --http. If not provided, plain HTTP is used");
System.out.printf(" %s: %s%n", PROVISION_OPT, "enables provisioning via keycloak-config-cli.");
System.out.printf(" %s: %s%n", OPENLDAP_OPT, "enables OpenLDAP support. (Optional)");
System.out.printf(" %s: %s%n", POSTGRES_OPT, "enables PostgreSQL database support. (Optional) If no other database is provided, H2 database is used");
System.out.printf(" %s: %s%n", MYSQL_OPT, "enables MySQL database support. (Optional) If no other database is provided, H2 database is used");
System.out.printf(" %s: %s%n", ORACLE_OPT, "enables Oracle database support. (Optional) If no other database is provided, H2 database is used");
System.out.printf(" %s: %s%n", GRAYLOG_OPT, "enables Graylog database support. (Optional)");
System.out.printf(" %s: %s%n", EXTENSIONS_OPT, "choose dynamic extensions extension based on \"classes\" or static based on \"jar\"");
System.out.printf(" %s: %s%n", DETACH_OPT, "Detached mode: Run containers in the background and prints the container name.. (Optional)");
System.out.printf(" %s: %s%n", VERBOSE_OPT, "Shows debug information, such as the generated command");
System.out.printf(" %s: %s%n", DOCKER_HOST_OPT, "Allows configuring of a non-default IP for reaching the docker host from inside the containers, " +
"which is used for name resolution. This is useful for using WiFi on ICE trains, which use the same network as docker by default. This causes the wifi to not work correctly.");
System.out.printf(" %s: %s%n", TRACING_OPT, "enables tracing with open-telemetry. Injects the otel agent into Keycloak, starts an otel-collector and jaeger container");
System.out.printf(" %s: %s%n", PROMETHEUS_OPT, "enables metrics collection to prometheus. Starts a prometheus metrics container");
System.out.printf(" %s: %s%n", NATS_OPT, "enables nats message broker");
System.out.printf("%n%s supports the following commands: %n", "start.java");
System.out.println("");
System.out.printf(" %s: %s%n", HELP_CMD, "Shows this help message");
System.out.printf("%n Usage examples: %n");
System.out.println("");
System.out.printf(" %s %s%n", "java start.java", "# Start Keycloak Environment with http");
System.out.printf(" %s %s%n", "java start.java --https", "# Start Keycloak Environment with https");
System.out.printf(" %s %s%n", "java start.java --https --verbose", "# Start Keycloak Environment with https and print command");
System.out.printf(" %s %s%n", "java start.java --provision=false", "# Start Keycloak Environment without provisioning");
System.out.printf(" %s %s%n", "java start.java --https --database=postgres", "# Start Keycloak Environment with PostgreSQL database");
System.out.printf(" %s %s%n", "java start.java --https --openldap --database=postgres", "# Start Keycloak Environment with PostgreSQL database and OpenLDAP");
System.out.printf(" %s %s%n", "java start.java --extensions=classes", "# Start Keycloak with extensions mounted from classes folder. Use --extensions=jar to mount the jar file into the container");
System.out.printf(" %s %s%n", "java start.java --docker-host=172.19.0.1", "# Configure a non-default IP for the docker host.");
}
private static int runCommandAndWait(ArrayList<String> commandLine) {
var pb = new ProcessBuilder(commandLine);
pb.directory(new File("."));
// disable docker compose menu in shell
pb.environment().put("COMPOSE_MENU", "false");
pb.inheritIO();
try {
var process = pb.start();
return process.waitFor();
} catch (Exception ex) {
System.err.printf("Could not run command: %s.", commandLine);
ex.printStackTrace();
return 1;
}
}
private static void createFolderIfMissing(String folderPath) {
var folder = new File(folderPath);
if (!folder.exists()) {
System.out.printf("Creating missing %s folder at %s success:%s%n"
, folderPath, folder.getAbsolutePath(), folder.mkdirs());
}
}
private static String getRootCALocation() throws IOException {
Runtime rt = Runtime.getRuntime();
String[] mkcertCommand = {"mkcert", "-CAROOT"};
Process proc = rt.exec(mkcertCommand);
BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
return stdInput.readLine().replace('\\','/');
}
}