forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
435d0ce
commit d90d2d9
Showing
8 changed files
with
137 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 12 additions & 5 deletions
17
integration-tests/oidc-mtls/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file added
BIN
+2.16 KB
integration-tests/oidc-mtls/src/main/resources/oidc-client-keystore.jks
Binary file not shown.
Binary file added
BIN
+937 Bytes
integration-tests/oidc-mtls/src/main/resources/oidc-client-truststore.jks
Binary file not shown.
Binary file added
BIN
+2.18 KB
integration-tests/oidc-mtls/src/main/resources/oidc-server-keystore.jks
Binary file not shown.
Binary file added
BIN
+925 Bytes
integration-tests/oidc-mtls/src/main/resources/oidc-server-truststore.jks
Binary file not shown.
114 changes: 114 additions & 0 deletions
114
...ests/oidc-mtls/src/test/java/io/quarkus/it/oidc/KeycloakTestResourceLifecycleManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package io.quarkus.it.oidc; | ||
|
||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.util.Map; | ||
|
||
import org.jboss.logging.Logger; | ||
import org.keycloak.representations.AccessTokenResponse; | ||
import org.keycloak.representations.idm.RealmRepresentation; | ||
import org.keycloak.util.JsonSerialization; | ||
import org.testcontainers.containers.BindMode; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.wait.strategy.Wait; | ||
|
||
import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; | ||
import io.restassured.RestAssured; | ||
import io.restassured.specification.RequestSpecification; | ||
|
||
public class KeycloakTestResourceLifecycleManager implements QuarkusTestResourceLifecycleManager { | ||
private static final Logger LOGGER = Logger.getLogger(KeycloakTestResourceLifecycleManager.class); | ||
private GenericContainer<?> keycloak; | ||
|
||
private static String KEYCLOAK_SERVER_URL; | ||
private static String CLIENT_KEYSTORE = "oidc-client-keystore.jks"; | ||
private static String CLIENT_TRUSTSTORE = "oidc-client-truststore.jks"; | ||
|
||
private static String SERVER_KEYSTORE = "oidc-server-keystore.jks"; | ||
private static String SERVER_KEYSTORE_MOUNTED_PATH = "/etc/oidc-server-keystore.jks"; | ||
private static String SERVER_TRUSTSTORE = "oidc-server-truststore.jks"; | ||
private static String SERVER_TRUSTSTORE_MOUNTED_PATH = "/etc/oidc-server-truststore.jks"; | ||
|
||
@SuppressWarnings("resource") | ||
@Override | ||
public Map<String, String> start() { | ||
keycloak = new GenericContainer<>("quay.io/keycloak/keycloak:" + System.getProperty("keycloak.version")) | ||
.withExposedPorts(8080, 8443) | ||
.withEnv("KEYCLOAK_ADMIN", "admin") | ||
.withEnv("KEYCLOAK_ADMIN_PASSWORD", "admin") | ||
.waitingFor(Wait.forLogMessage(".*Keycloak.*started.*", 1)); | ||
|
||
keycloak = keycloak | ||
.withClasspathResourceMapping(SERVER_KEYSTORE, SERVER_KEYSTORE_MOUNTED_PATH, BindMode.READ_ONLY) | ||
.withClasspathResourceMapping(SERVER_TRUSTSTORE, SERVER_TRUSTSTORE_MOUNTED_PATH, BindMode.READ_ONLY) | ||
.withCommand("build --https-client-auth=required") | ||
.withCommand(String.format( | ||
"start --https-client-auth=required --hostname-strict=false" | ||
+ " --https-key-store-file=%s --https-trust-store-file=%s --https-trust-store-password=password", | ||
SERVER_KEYSTORE_MOUNTED_PATH, SERVER_TRUSTSTORE_MOUNTED_PATH)); | ||
keycloak.start(); | ||
LOGGER.info(keycloak.getLogs()); | ||
|
||
KEYCLOAK_SERVER_URL = "https://localhost:" + keycloak.getMappedPort(8443); | ||
|
||
createRealm(); | ||
|
||
return Map.of("quarkus.oidc.auth-server-url", KEYCLOAK_SERVER_URL + "/realms/quarkus"); | ||
} | ||
|
||
private static void createRealm() { | ||
try { | ||
RealmRepresentation realm = loadRealm(); | ||
createRequestSpec().auth().oauth2(getAdminAccessToken()) | ||
.contentType("application/json") | ||
.body(JsonSerialization.writeValueAsBytes(realm)) | ||
.when() | ||
.post(KEYCLOAK_SERVER_URL + "/admin/realms").then() | ||
.statusCode(201); | ||
} catch (Exception e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static RealmRepresentation loadRealm() throws Exception { | ||
URL realmPathUrl = Thread.currentThread().getContextClassLoader().getResource("quarkus-realm.json"); | ||
try (InputStream is = realmPathUrl.openStream()) { | ||
return JsonSerialization.readValue(is, RealmRepresentation.class); | ||
} | ||
} | ||
|
||
private static String getAdminAccessToken() { | ||
return createRequestSpec() | ||
.param("grant_type", "password") | ||
.param("username", "admin") | ||
.param("password", "admin") | ||
.param("client_id", "admin-cli") | ||
.when() | ||
.post(KEYCLOAK_SERVER_URL + "/realms/master/protocol/openid-connect/token") | ||
.as(AccessTokenResponse.class).getToken(); | ||
} | ||
|
||
public static String getAccessToken(String userName) { | ||
return createRequestSpec().param("grant_type", "password") | ||
.param("username", userName) | ||
.param("password", userName) | ||
.param("client_id", "backend-service") | ||
.when() | ||
.post(KEYCLOAK_SERVER_URL + "/realms/quarkus/protocol/openid-connect/token") | ||
.as(AccessTokenResponse.class).getToken(); | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
createRequestSpec().auth().oauth2(getAdminAccessToken()) | ||
.when() | ||
.delete(KEYCLOAK_SERVER_URL + "/admin/realms/quarkus").then().statusCode(204); | ||
|
||
keycloak.stop(); | ||
} | ||
|
||
private static RequestSpecification createRequestSpec() { | ||
return RestAssured.given().trustStore(CLIENT_TRUSTSTORE, "password") | ||
.keyStore(CLIENT_KEYSTORE, "password"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters