Skip to content

Commit

Permalink
Use fixed ports in Dev Svc for Keycloak on shared network & user request
Browse files Browse the repository at this point in the history
  • Loading branch information
michalvavrik authored and tmihalac committed Oct 27, 2022
1 parent 12f6d27 commit 404941f
Showing 1 changed file with 27 additions and 2 deletions.
Expand Up @@ -349,7 +349,9 @@ private RunningDevService startContainer(DockerStatusBuildItem dockerStatusBuild

String internalUrl = startURL(oidcContainer.getHost(), oidcContainer.getPort(), oidcContainer.keycloakX);
String hostUrl = oidcContainer.useSharedNetwork
? startURL("localhost", oidcContainer.fixedExposedPort.getAsInt(), oidcContainer.keycloakX)
// we need to use auto-detected host and port, so it works when docker host != localhost
? startURL(oidcContainer.getSharedNetworkExternalHost(), oidcContainer.getSharedNetworkExternalPort(),
oidcContainer.keycloakX)
: null;

Map<String, String> configs = prepareConfiguration(keycloakBuildItemBuildProducer, internalUrl, hostUrl,
Expand Down Expand Up @@ -406,7 +408,7 @@ public QuarkusOidcContainer(DockerImageName dockerImageName, OptionalInt fixedEx
this.javaOpts = javaOpts;
this.keycloakX = isKeycloakX(dockerImageName);

if (sharedContainer && fixedExposedPort.isEmpty()) {
if (useSharedNetwork && fixedExposedPort.isEmpty()) {
// We need to know the port we are exposing when using the shared network, in order to be able to tell
// Keycloak what the client URL is. This is necessary in order for Keycloak to create the proper 'issuer'
// when creating tokens
Expand Down Expand Up @@ -435,6 +437,11 @@ protected void configure() {

if (fixedExposedPort.isPresent()) {
addFixedExposedPort(fixedExposedPort.getAsInt(), KEYCLOAK_PORT);
if (useSharedNetwork) {
// expose random port for which we are able to ask Testcontainers for the actual mapped port at runtime
// as from the host's perspective Testcontainers actually expose container ports on random host port
addExposedPort(KEYCLOAK_PORT);
}
} else {
addExposedPort(KEYCLOAK_PORT);
}
Expand Down Expand Up @@ -528,6 +535,24 @@ public String getHost() {
return super.getHost();
}

/**
* Host name used for calls from outside of docker when {@code useSharedNetwork} is true.
*
* @return host name
*/
private String getSharedNetworkExternalHost() {
return super.getHost();
}

/**
* Host port used for calls from outside of docker when {@code useSharedNetwork} is true.
*
* @return port
*/
private int getSharedNetworkExternalPort() {
return getFirstMappedPort();
}

public int getPort() {
if (useSharedNetwork) {
return KEYCLOAK_PORT;
Expand Down

0 comments on commit 404941f

Please sign in to comment.