From 415ea0ea6fac087068f57cd67f7d52d5e9d9ae76 Mon Sep 17 00:00:00 2001 From: Davide Bigotti Date: Thu, 28 Mar 2024 10:26:32 +0100 Subject: [PATCH 1/2] feat: allow connection to host network --- .../deployment/DevServicesLocalStackProcessor.java | 5 +++++ .../LocalStackDevServicesBuildTimeConfig.java | 13 +++++++++++++ 2 files changed, 18 insertions(+) diff --git a/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/DevServicesLocalStackProcessor.java b/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/DevServicesLocalStackProcessor.java index a19a98ec4..9ba024e76 100644 --- a/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/DevServicesLocalStackProcessor.java +++ b/common/deployment/src/main/java/io/quarkus/amazon/common/deployment/DevServicesLocalStackProcessor.java @@ -14,6 +14,7 @@ import java.util.stream.Stream; import org.jboss.logging.Logger; +import org.testcontainers.Testcontainers; import org.testcontainers.containers.BindMode; import org.testcontainers.containers.localstack.LocalStackContainer; import org.testcontainers.containers.localstack.LocalStackContainer.EnabledService; @@ -304,6 +305,10 @@ public String getSecretKey() { .toArray(EnabledService[]::new)) .withLabel(DEV_SERVICE_LABEL, devServiceName); + if (localStackDevServicesBuildTimeConfig.accessToHost()) { + Testcontainers.exposeHostPorts(localStackDevServicesBuildTimeConfig.hostPort()); + } + localStackDevServicesBuildTimeConfig.port().ifPresent( port -> container.setPortBindings(Collections.singletonList("%s:%s".formatted(port, PORT)))); diff --git a/common/runtime/src/main/java/io/quarkus/amazon/common/runtime/LocalStackDevServicesBuildTimeConfig.java b/common/runtime/src/main/java/io/quarkus/amazon/common/runtime/LocalStackDevServicesBuildTimeConfig.java index e7bd794c5..c295c4a2d 100644 --- a/common/runtime/src/main/java/io/quarkus/amazon/common/runtime/LocalStackDevServicesBuildTimeConfig.java +++ b/common/runtime/src/main/java/io/quarkus/amazon/common/runtime/LocalStackDevServicesBuildTimeConfig.java @@ -48,4 +48,17 @@ public interface LocalStackDevServicesBuildTimeConfig { * Optional fixed port localstack will listen to. */ Optional port(); + + /** + * Indicates if the container would make a connection to the host network. + */ + @WithDefault("false") + boolean accessToHost(); + + /** + * port which the application listen to. + */ + @WithDefault("${quarkus.http.port:8080}") + int hostPort(); + } From f7acfee083d1bc06d1f9d2c74adf1dd293d640b9 Mon Sep 17 00:00:00 2001 From: Davide Bigotti Date: Thu, 28 Mar 2024 11:40:36 +0100 Subject: [PATCH 2/2] doc: add host networking section --- docs/modules/ROOT/pages/dev-services.adoc | 18 ++++++++++ ...-stack-dev-services-build-time-config.adoc | 34 +++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/docs/modules/ROOT/pages/dev-services.adoc b/docs/modules/ROOT/pages/dev-services.adoc index c5cc3e8f6..090a472b0 100644 --- a/docs/modules/ROOT/pages/dev-services.adoc +++ b/docs/modules/ROOT/pages/dev-services.adoc @@ -61,6 +61,24 @@ The key is the name of the service to enable and must be a valid LocalStack serv Some extensions support additional configuration to be applied at startup. Refer to the extension documentation. +== Connect to host network + +Dev Services for Amazon Services can support communication from the localstack container to the host network. + +If the container needs to communicate with the application running in dev mode, the property `quarkus.http.host` must be set to make the application listen on `0.0.0.0`. +Otherwise, network traffic must be forwarded somehow to `127.0.0.1` which is the default listening address. + +By default, the host exposes port `8080` or the one set via `quarkus.http.port` property to the LocalStack container. +In case the exposed port is incorrectly evaluated, it can be manually forced by specifying the desired value via `quarkus.aws.devservices.localstack.host-port`. + +[source, properties] +---- +quarkus.http.host=0.0.0.0 +quarkus.aws.devservices.localstack.access-to-host=true +---- + +Connection to host network DOES NOT support `quarkus.http.port=0` + == Cognito The Cognito extension is not using LocalStack but Moto. diff --git a/docs/modules/ROOT/pages/includes/quarkus-aws-devservices-localstack-local-stack-dev-services-build-time-config.adoc b/docs/modules/ROOT/pages/includes/quarkus-aws-devservices-localstack-local-stack-dev-services-build-time-config.adoc index 24338c7d4..e4e012e7e 100644 --- a/docs/modules/ROOT/pages/includes/quarkus-aws-devservices-localstack-local-stack-dev-services-build-time-config.adoc +++ b/docs/modules/ROOT/pages/includes/quarkus-aws-devservices-localstack-local-stack-dev-services-build-time-config.adoc @@ -95,6 +95,40 @@ endif::add-copy-button-to-env-var[] | +a|icon:lock[title=Fixed at build time] [[quarkus-aws-devservices-localstack-local-stack-dev-services-build-time-config_quarkus-aws-devservices-localstack-access-to-host]]`link:#quarkus-aws-devservices-localstack-local-stack-dev-services-build-time-config_quarkus-aws-devservices-localstack-access-to-host[quarkus.aws.devservices.localstack.access-to-host]` + + +[.description] +-- +Indicates if the container would make a connection to the host network. + +ifdef::add-copy-button-to-env-var[] +Environment variable: env_var_with_copy_button:+++QUARKUS_AWS_DEVSERVICES_LOCALSTACK_ACCESS_TO_HOST+++[] +endif::add-copy-button-to-env-var[] +ifndef::add-copy-button-to-env-var[] +Environment variable: `+++QUARKUS_AWS_DEVSERVICES_LOCALSTACK_ACCESS_TO_HOST+++` +endif::add-copy-button-to-env-var[] +--|boolean +|`false` + + +a|icon:lock[title=Fixed at build time] [[quarkus-aws-devservices-localstack-local-stack-dev-services-build-time-config_quarkus-aws-devservices-localstack-host-port]]`link:#quarkus-aws-devservices-localstack-local-stack-dev-services-build-time-config_quarkus-aws-devservices-localstack-host-port[quarkus.aws.devservices.localstack.host-port]` + + +[.description] +-- +port which the application listen to. + +ifdef::add-copy-button-to-env-var[] +Environment variable: env_var_with_copy_button:+++QUARKUS_AWS_DEVSERVICES_LOCALSTACK_HOST_PORT+++[] +endif::add-copy-button-to-env-var[] +ifndef::add-copy-button-to-env-var[] +Environment variable: `+++QUARKUS_AWS_DEVSERVICES_LOCALSTACK_HOST_PORT+++` +endif::add-copy-button-to-env-var[] +--|int +|`${quarkus.http.port:8080}` + + a|icon:lock[title=Fixed at build time] [[quarkus-aws-devservices-localstack-local-stack-dev-services-build-time-config_quarkus-aws-devservices-localstack-container-properties-container-properties]]`link:#quarkus-aws-devservices-localstack-local-stack-dev-services-build-time-config_quarkus-aws-devservices-localstack-container-properties-container-properties[quarkus.aws.devservices.localstack.container-properties]`