From 404941fbdcc28247e51876b5fe6e9b305d1b20cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Vav=C5=99=C3=ADk?= Date: Sun, 9 Oct 2022 01:07:57 +0200 Subject: [PATCH] Use fixed ports in Dev Svc for Keycloak on shared network & user request fixes: #27763 --- .../KeycloakDevServicesProcessor.java | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/extensions/oidc/deployment/src/main/java/io/quarkus/oidc/deployment/devservices/keycloak/KeycloakDevServicesProcessor.java b/extensions/oidc/deployment/src/main/java/io/quarkus/oidc/deployment/devservices/keycloak/KeycloakDevServicesProcessor.java index dbdc5dbd8ec3c..34a1a4cb0918e 100644 --- a/extensions/oidc/deployment/src/main/java/io/quarkus/oidc/deployment/devservices/keycloak/KeycloakDevServicesProcessor.java +++ b/extensions/oidc/deployment/src/main/java/io/quarkus/oidc/deployment/devservices/keycloak/KeycloakDevServicesProcessor.java @@ -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 configs = prepareConfiguration(keycloakBuildItemBuildProducer, internalUrl, hostUrl, @@ -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 @@ -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); } @@ -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;