Skip to content

Commit

Permalink
Merge pull request quarkusio#36508 from sberyozkin/remove_custom_keyc…
Browse files Browse the repository at this point in the history
…loak_test_container

Remove custom Keycloak image code from the keycloak-authorization tests
  • Loading branch information
sberyozkin authored Dec 15, 2023
2 parents e72026f + 1dc33fb commit bbef09f
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,20 @@ public class DevServicesConfig {
@ConfigItem
public Optional<List<String>> realmPath;

/**
* Aliases to additional class or file system resources which will be used to initialize Keycloak.
* Each map entry represents a mapping between an alias and a class or file system resource path.
*/
@ConfigItem
public Map<String, String> resourceAliases;
/**
* Additional class or file system resources which will be used to initialize Keycloak.
* Each map entry represents a mapping between a class or file system resource path alias and the Keycloak container
* location.
*/
@ConfigItem
public Map<String, String> resourceMappings;

/**
* The JAVA_OPTS passed to the keycloak JVM
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.keycloak.representations.idm.RolesRepresentation;
import org.keycloak.representations.idm.UserRepresentation;
import org.keycloak.util.JsonSerialization;
import org.testcontainers.containers.BindMode;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.wait.strategy.Wait;
import org.testcontainers.utility.DockerImageName;
Expand Down Expand Up @@ -362,6 +363,7 @@ private RunningDevService startContainer(DockerStatusBuildItem dockerStatusBuild
capturedDevServicesConfiguration.port,
useSharedNetwork,
capturedDevServicesConfiguration.realmPath.orElse(List.of()),
resourcesMap(),
capturedDevServicesConfiguration.serviceName,
capturedDevServicesConfiguration.shared,
capturedDevServicesConfiguration.javaOpts,
Expand Down Expand Up @@ -399,6 +401,17 @@ private RunningDevService startContainer(DockerStatusBuildItem dockerStatusBuild
.orElseGet(defaultKeycloakContainerSupplier);
}

private Map<String, String> resourcesMap() {
Map<String, String> resources = new HashMap<>();
for (Map.Entry<String, String> aliasEntry : capturedDevServicesConfiguration.resourceAliases.entrySet()) {
if (capturedDevServicesConfiguration.resourceMappings.containsKey(aliasEntry.getKey())) {
resources.put(aliasEntry.getValue(),
capturedDevServicesConfiguration.resourceMappings.get(aliasEntry.getKey()));
}
}
return resources;
}

private static boolean isKeycloakX(DockerImageName dockerImageName) {
return capturedDevServicesConfiguration.keycloakXImage.isPresent()
? capturedDevServicesConfiguration.keycloakXImage.get()
Expand All @@ -414,6 +427,7 @@ private static class QuarkusOidcContainer extends GenericContainer<QuarkusOidcCo
private final OptionalInt fixedExposedPort;
private final boolean useSharedNetwork;
private final List<String> realmPaths;
private final Map<String, String> resources;
private final String containerLabelValue;
private final Optional<String> javaOpts;
private final boolean sharedContainer;
Expand All @@ -425,13 +439,14 @@ private static class QuarkusOidcContainer extends GenericContainer<QuarkusOidcCo
private final List<String> errors;

public QuarkusOidcContainer(DockerImageName dockerImageName, OptionalInt fixedExposedPort, boolean useSharedNetwork,
List<String> realmPaths, String containerLabelValue,
List<String> realmPaths, Map<String, String> resources, String containerLabelValue,
boolean sharedContainer, Optional<String> javaOpts, Optional<String> startCommand, boolean showLogs,
List<String> errors) {
super(dockerImageName);

this.useSharedNetwork = useSharedNetwork;
this.realmPaths = realmPaths;
this.resources = resources;
this.containerLabelValue = containerLabelValue;
this.sharedContainer = sharedContainer;
this.javaOpts = javaOpts;
Expand Down Expand Up @@ -510,6 +525,9 @@ protected void configure() {
}

}
for (Map.Entry<String, String> resource : resources.entrySet()) {
mapResource(resource.getKey(), resource.getValue());
}

if (showLogs) {
super.withLogConsumer(t -> {
Expand All @@ -520,6 +538,17 @@ protected void configure() {
LOG.infof("Using %s powered Keycloak distribution", keycloakX ? "Quarkus" : "WildFly");
}

private void mapResource(String resourcePath, String mappedResource) {
if (Thread.currentThread().getContextClassLoader().getResource(resourcePath) != null) {
withClasspathResourceMapping(resourcePath, mappedResource, BindMode.READ_ONLY);
} else if (Files.exists(Paths.get(resourcePath))) {
withFileSystemBind(resourcePath, mappedResource, BindMode.READ_ONLY);
} else {
errors.add(String.format("%s resource is not available", resourcePath));
LOG.errorf("Realm %s resource is not available", resourcePath);
}
}

private Integer findRandomPort() {
try (ServerSocket socket = new ServerSocket(0)) {
return socket.getLocalPort();
Expand Down
68 changes: 27 additions & 41 deletions integration-tests/keycloak-authorization/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
<name>Quarkus - Integration Tests - Keycloak Policy Enforcer</name>
<description>Module that contains Keycloak Policy Enforcer related tests</description>

<properties>
<keycloak.url>http://localhost:8180/auth</keycloak.url>
<nashorn-core.version>15.3</nashorn-core.version>
</properties>

<dependencies>
<dependency>
<groupId>io.quarkus</groupId>
Expand All @@ -38,8 +33,8 @@

<!-- test dependencies -->
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-test-keycloak-server</artifactId>
<scope>test</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -116,37 +111,29 @@
<artifactId>htmlunit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.openjdk.nashorn</groupId>
<artifactId>nashorn-core</artifactId>
<version>${nashorn-core.version}</version>
</dependency>
</dependencies>

<build>
<testResources>
<testResource>
<directory>src/test/resources</directory>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</testResource>
</testResources>
</resource>
<resource>
<directory>src/main/resources/policies</directory>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
<systemPropertyVariables>
<keycloak.image.version>${keycloak.image.version}</keycloak.image.version>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<skip>true</skip>
<systemPropertyVariables>
<keycloak.image.version>${keycloak.image.version}</keycloak.image.version>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
Expand All @@ -162,19 +149,24 @@
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<artifactId>maven-jar-plugin</artifactId>
<executions>
<execution>
<id>copy-dependencies-quarkus</id>
<phase>process-test-resources</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.testOutputDirectory}</outputDirectory>
<includeArtifactIds>nashorn-core,asm,asm-util,asm-commons</includeArtifactIds>
</configuration>
</execution>
<execution>
<phase>process-resources</phase>
<goals>
<goal>jar</goal>
</goals>
<configuration>
<classifier>policies</classifier>
<includes>
<include>*.js</include>
<include>META-INF/*</include>
</includes>
<excludes>
<exclude>/policies</exclude>
</excludes>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
Expand All @@ -194,18 +186,12 @@
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<systemPropertyVariables>
<keycloak.version>${keycloak.version}</keycloak.version>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<configuration>
<skip>false</skip>
<systemPropertyVariables>
<keycloak.version>${keycloak.version}</keycloak.version>
</systemPropertyVariables>
</configuration>
</plugin>
<plugin>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.keycloak.admin.client.Keycloak;
import org.keycloak.representations.idm.ClientRepresentation;
import org.keycloak.representations.idm.CredentialRepresentation;
Expand All @@ -22,9 +21,6 @@
@Path("/admin-client")
public class AdminClientResource {

@ConfigProperty(name = "admin-url")
String url;

@Inject
Keycloak keycloak;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
quarkus.keycloak.devservices.create-realm=false
quarkus.keycloak.devservices.resource-aliases.policies=${project.build.directory}/${project.build.finalName}-policies.jar
quarkus.keycloak.devservices.resource-mappings.policies=/opt/keycloak/providers/policies.jar
# Enable Policy Enforcement
quarkus.keycloak.policy-enforcer.enable=true

# Default Tenant
# Configuration file
quarkus.oidc.auth-server-url=${quarkus.oidc.auth-server-url}
quarkus.oidc.client-id=quarkus-app
quarkus.oidc.credentials.secret=secret

Expand Down Expand Up @@ -110,4 +112,4 @@ quarkus.keycloak.api-permission-webapp.policy-enforcer.paths.1.claim-information
admin-url=${keycloak.url}

# Configure Keycloak Admin Client
quarkus.keycloak.admin-client.server-url=${admin-url}
quarkus.keycloak.admin-client.server-url=${admin-url}
Loading

0 comments on commit bbef09f

Please sign in to comment.