Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fixed ports in Dev Service for Keycloak on shared network & user request #28458

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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