|
1 | | -/* |
2 | | - * Copyright 2017-2021 original authors |
3 | | - * |
4 | | - * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | | - * you may not use this file except in compliance with the License. |
6 | | - * You may obtain a copy of the License at |
7 | | - * |
8 | | - * https://www.apache.org/licenses/LICENSE-2.0 |
9 | | - * |
10 | | - * Unless required by applicable law or agreed to in writing, software |
11 | | - * distributed under the License is distributed on an "AS IS" BASIS, |
12 | | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | | - * See the License for the specific language governing permissions and |
14 | | - * limitations under the License. |
15 | | - */ |
16 | 1 | package io.micronaut.security.testutils |
17 | 2 |
|
18 | 3 | import dasniko.testcontainers.keycloak.KeycloakContainer |
19 | 4 | import org.testcontainers.Testcontainers |
20 | | -import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy |
21 | | - |
22 | | -import java.time.Duration |
23 | 5 |
|
24 | 6 | class Keycloak { |
25 | 7 | static final String SYS_TESTCONTAINERS = "testcontainers" |
@@ -48,33 +30,58 @@ class Keycloak { |
48 | 30 | Integer.valueOf(issuer.substring(issuer.indexOf("localhost:") + "localhost:".length(), issuer.indexOf("/realms"))) |
49 | 31 | } |
50 | 32 |
|
| 33 | + private static exec(String... parts) { |
| 34 | + println "Running command: " + parts.join(" ") |
| 35 | + def result = keycloak.execInContainer(parts) |
| 36 | + println "OUT: $result.stdout" |
| 37 | + println "ERR: $result.stderr" |
| 38 | + println "EXIT: $result.exitCode" |
| 39 | + assert result.exitCode == 0 |
| 40 | + } |
| 41 | + |
51 | 42 | static void init() { |
52 | 43 | if (keycloak == null) { |
53 | 44 |
|
54 | 45 | keycloak = new KeycloakContainer() |
55 | 46 | .withAdminUsername("admin") |
56 | 47 | .withAdminPassword("admin") |
57 | 48 | .withExposedPorts(8080) |
58 | | - .withEnv(Map.of( |
59 | | - "KEYCLOAK_USER", "user", |
60 | | - "KEYCLOAK_PASSWORD", "password", |
61 | | - "DB_VENDOR", "H2") |
62 | | - ) |
63 | | - .waitingFor(new LogMessageWaitStrategy().withRegEx(".*powered by Quarkus.*").withStartupTimeout(Duration.ofMinutes(5))) |
| 49 | + .withContextPath("/auth") |
| 50 | + .withEnv(DB_VENDOR: "H2") |
64 | 51 |
|
65 | 52 | keycloak.start() |
66 | | - keycloak.execInContainer("/opt/keycloak/bin/kcreg.sh config credentials " + |
67 | | - "--server http://localhost:8080/auth " + |
68 | | - "--realm master --user user --password password" |
69 | | - .split(" ")) |
70 | | - keycloak.execInContainer("/opt/keycloak/bin/kcreg.sh " + |
71 | | - "create -s clientId=$CLIENT_ID " + |
72 | | - "-s redirectUris=[\"http://${TestContainersUtils.host}*\"] " + |
73 | | - "-s secret=$clientSecret" |
74 | | - .split(" ")) |
| 53 | + // Login |
| 54 | + exec("/opt/keycloak/bin/kcadm.sh", |
| 55 | + "config", "credentials", |
| 56 | + "--server", "http://localhost:8080/auth", |
| 57 | + "--realm", "master", |
| 58 | + "--user", "admin", |
| 59 | + "--password", "admin") |
| 60 | + // Add user |
| 61 | + exec("/opt/keycloak/bin/kcadm.sh", |
| 62 | + "create", "users", |
| 63 | + "-s", "username=user", |
| 64 | + "-s", "enabled=true", |
| 65 | + "-o", |
| 66 | + "--fields", "id,username") |
| 67 | + // Set user password |
| 68 | + exec("/opt/keycloak/bin/kcadm.sh", |
| 69 | + "set-password", |
| 70 | + "--username", "user", |
| 71 | + "--new-password", "password") |
| 72 | + // Add client |
| 73 | + exec("/opt/keycloak/bin/kcreg.sh", |
| 74 | + "create", |
| 75 | + "--server", "http://localhost:8080/auth", |
| 76 | + "--realm", "master", |
| 77 | + "--user", "admin", |
| 78 | + "--password", "admin", |
| 79 | + "-s", "clientId=$CLIENT_ID", |
| 80 | + "-s", "redirectUris=[\"http://${redirectUriHost}*\"]", |
| 81 | + "-s", "secret=$clientSecret") |
75 | 82 | int port = keycloak.getMappedPort(8080) |
76 | 83 | Testcontainers.exposeHostPorts(port) |
77 | | - issuer = "http://" + getHost() + ":" + port + "/realms/master" |
| 84 | + issuer = "http://$host:$port/auth/realms/master" |
78 | 85 | } |
79 | 86 | } |
80 | 87 |
|
|
0 commit comments