From 9dcb1b65ae554b609d783dd1d909bbe0264ce537 Mon Sep 17 00:00:00 2001 From: Hector Ventura Date: Sat, 17 Oct 2026 16:37:15 -0500 Subject: [PATCH] feat(core): translate LocalStack variables in Java instead of the entrypoint (#5851) * feat(core): translate LocalStack variables in Java instead of the entrypoint The LocalStack-to-Floci variable mapping (PERSISTENCE, EDGE_PORT, LOCALSTACK_HOST, GATEWAY_LISTEN, LS_LOG/DEBUG, the Lambda and Docker variables, the DNS suffixes, USE_SSL, CUSTOM_SSL_CERT_PATH) lived in docker/localstack-parity.sh, sourced by the image's entrypoint. A JVM run or a native binary on the host never saw it. LocalStackParity now computes the same mapping and LocalStackParityConfigSource serves it at ordinal 301, just above environment variables, as the exported variables did. A key is produced only when its Floci variable is unset, so an explicit Floci setting still wins; the DNS suffixes are still appended to any configured ones; LOCALSTACK_PARITY=false still turns it all off. TlsConfigSource, which reads its properties before the configuration exists, consults the mapping too. The script, its shell test and every Dockerfile copy of it are removed; the test cases move to LocalStackParityTest. Refs #5767 Signed-off-by: Hector Ventura * fix(core): keep TLS backends on loopback, keep configured DNS suffixes, and pass LocalStack settings to hooks Review follow-ups for the Java LocalStack translation: - Dockerfile.compat took docker-entrypoint.sh from the published native image, which still sourced the removed localstack-parity.sh, so the compat image never became healthy. It now copies the entrypoint from the build context, as it already does healthcheck.sh. - At ordinal 301 the parity source outranked TlsConfigSource's loopback pin (300): with TLS on and GATEWAY_LISTEN set, the backends bound the gateway address while the proxy dialled 127.0.0.1. It now sits at 299, below environment variables and the TLS source and above .env and config/application.yml, where the exported variables used to be. - floci.dns.extra-suffixes is no longer a translated property, which on JVM and native runs replaced suffixes set in application.yml. The five readers append LocalStack's and Floci's suffixes to the configured list through LocalStackParity.withDnsSuffixes instead. - GATEWAY_LISTEN=0.0.0.0:4566 (or [::]:4566, or a list) gives the host to quarkus.http.host and the port to floci.port when EDGE_PORT is unset, instead of a host Floci rejects as malformed. - Init hooks get the translation as FLOCI_*/QUARKUS_* variables again, as they did when the entrypoint exported them. Signed-off-by: Hector Ventura * fix(core): parse LOCALSTACK_HOST and GATEWAY_LISTEN the way LocalStack does Both take LocalStack's host-and-port forms (HostAndPort.parse in its config.py): host:port, host, :port, [v6]:port and [v6]. - LOCALSTACK_HOST is :, localhost.localstack.cloud:4566 by default. Its port went into floci.hostname, so the base URL came out as http://localhost.localstack.cloud:4566:4566. floci.hostname now gets the host alone. - GATEWAY_LISTEN=:4566, LocalStack's default host on 4566, did not match and stopped startup with "Refusing to listen on malformed host". An empty host now keeps Floci's own bind address and takes just the port. Signed-off-by: Hector Ventura --------- Signed-off-by: Hector Ventura --- .dockerignore | 1 - .github/workflows/compatibility.yml | 1 - docker/Dockerfile | 2 +- docker/Dockerfile.compat | 2 +- docker/Dockerfile.jvm-package | 2 +- docker/Dockerfile.native | 2 +- docker/Dockerfile.native-package | 2 +- docker/entrypoint.sh | 4 - docker/localstack-parity.sh | 82 ------- docker/test-localstack-parity.sh | 153 -------------- .../migrate-from-localstack.md | 2 + .../floci/config/LocalStackParity.java | 200 ++++++++++++++++++ .../config/LocalStackParityConfigSource.java | 56 +++++ .../floci/config/TlsCertificateManager.java | 2 +- .../floci/config/TlsConfigSource.java | 5 + .../core/common/dns/EmbeddedDnsServer.java | 3 +- .../inithook/HookScriptExecutor.java | 7 +- .../cloudfront/CloudFrontService.java | 3 +- .../route53/Route53DnsRecordSource.java | 3 +- .../services/s3/S3VirtualHostFilter.java | 3 +- ...lipse.microprofile.config.spi.ConfigSource | 1 + .../floci/config/LocalStackParityTest.java | 186 ++++++++++++++++ 22 files changed, 470 insertions(+), 252 deletions(-) delete mode 100644 docker/localstack-parity.sh delete mode 100755 docker/test-localstack-parity.sh create mode 100644 src/main/java/io/github/hectorvent/floci/config/LocalStackParity.java create mode 100644 src/main/java/io/github/hectorvent/floci/config/LocalStackParityConfigSource.java create mode 100644 src/test/java/io/github/hectorvent/floci/config/LocalStackParityTest.java diff --git a/.dockerignore b/.dockerignore index 83238f2ce0..c0ecd47c6b 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,7 +1,6 @@ * !bin/awslocal !docker/entrypoint.sh -!docker/localstack-parity.sh !docker/healthcheck.sh !pom.xml !src/** diff --git a/.github/workflows/compatibility.yml b/.github/workflows/compatibility.yml index fa6245b01d..0b2214299a 100644 --- a/.github/workflows/compatibility.yml +++ b/.github/workflows/compatibility.yml @@ -11,7 +11,6 @@ on: - 'docker/Dockerfile.native-package' - 'docker/Dockerfile.compat' - 'docker/entrypoint.sh' - - 'docker/localstack-parity.sh' - 'docker/healthcheck.sh' - 'docker/run-docker-tests.sh' - 'docker/test-run-docker-tests.sh' diff --git a/docker/Dockerfile b/docker/Dockerfile index 354de752cc..c1dffcaedc 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -52,7 +52,7 @@ COPY --from=build /build/target/quarkus-app/ quarkus-app/ RUN chown -R 1001:root /app/quarkus-app COPY --chmod=755 docker/entrypoint.sh /usr/local/bin/docker-entrypoint.sh -COPY --chmod=755 docker/localstack-parity.sh docker/healthcheck.sh /usr/local/bin/ +COPY --chmod=755 docker/healthcheck.sh /usr/local/bin/ VOLUME /app/data diff --git a/docker/Dockerfile.compat b/docker/Dockerfile.compat index 104a2ce327..98cc36f83a 100644 --- a/docker/Dockerfile.compat +++ b/docker/Dockerfile.compat @@ -111,7 +111,7 @@ ENV FLOCI_SECURITY_ALLOW_UNSAFE_NETWORK_EXPOSURE=true COPY --from=native --chown=1001:root /app/ /app/ VOLUME /app/data -COPY --from=native /usr/local/bin/docker-entrypoint.sh /usr/local/bin/localstack-parity.sh /usr/local/bin/ +COPY --chmod=755 docker/entrypoint.sh /usr/local/bin/docker-entrypoint.sh # From the build context, not the native image: a published native image may predate the script. COPY --chmod=755 docker/healthcheck.sh /usr/local/bin/ diff --git a/docker/Dockerfile.jvm-package b/docker/Dockerfile.jvm-package index a938c6b803..07ccb810fd 100644 --- a/docker/Dockerfile.jvm-package +++ b/docker/Dockerfile.jvm-package @@ -51,7 +51,7 @@ COPY target/quarkus-app/ quarkus-app/ RUN chown -R 1001:root /app/quarkus-app COPY --chmod=755 docker/entrypoint.sh /usr/local/bin/docker-entrypoint.sh -COPY --chmod=755 docker/localstack-parity.sh docker/healthcheck.sh /usr/local/bin/ +COPY --chmod=755 docker/healthcheck.sh /usr/local/bin/ VOLUME /app/data diff --git a/docker/Dockerfile.native b/docker/Dockerfile.native index 51bf9336a1..7be22b4669 100644 --- a/docker/Dockerfile.native +++ b/docker/Dockerfile.native @@ -50,7 +50,7 @@ ENV FLOCI_STORAGE_PERSISTENT_PATH=/app/data COPY --from=build /usr/lib64/libz.so.1 /usr/lib64/libz.so.1 COPY --chmod=755 --from=build /app/target/*-runner /app/application COPY --chmod=755 docker/entrypoint.sh /usr/local/bin/docker-entrypoint.sh -COPY --chmod=755 docker/localstack-parity.sh docker/healthcheck.sh /usr/local/bin/ +COPY --chmod=755 docker/healthcheck.sh /usr/local/bin/ ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"] # A published port needs 0.0.0.0 inside the container, which Floci only accepts with explicit consent. diff --git a/docker/Dockerfile.native-package b/docker/Dockerfile.native-package index 804fc8b809..7b365f61e8 100644 --- a/docker/Dockerfile.native-package +++ b/docker/Dockerfile.native-package @@ -14,7 +14,7 @@ RUN echo 'floci:x:1001:0:Floci:/app:/sbin/nologin' >> /etc/passwd \ VOLUME /app/data COPY --chmod=755 docker/entrypoint.sh /usr/local/bin/docker-entrypoint.sh -COPY --chmod=755 docker/localstack-parity.sh docker/healthcheck.sh /usr/local/bin/ +COPY --chmod=755 docker/healthcheck.sh /usr/local/bin/ ARG VERSION=latest ENV FLOCI_VERSION=${VERSION} diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index 86a17a103b..773b73f429 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -53,10 +53,6 @@ if [ "$(id -u)" = '0' ]; then fi fi -if [ "${LOCALSTACK_PARITY:-true}" != "false" ]; then - . /usr/local/bin/localstack-parity.sh -fi - # Probe the state dir as the unprivileged user and warn loudly when it is not # writable (read-only or root-owned bind mounts the chown above could not fix). # The container still starts: with the default in-memory storage the dir is diff --git a/docker/localstack-parity.sh b/docker/localstack-parity.sh deleted file mode 100644 index 9c3a1a6e12..0000000000 --- a/docker/localstack-parity.sh +++ /dev/null @@ -1,82 +0,0 @@ -#!/bin/sh -# Maps LocalStack Community environment variables to their Floci equivalents. -# Sourced by entrypoint.sh when LOCALSTACK_PARITY=true. -# Floci vars always win: every mapping uses ${FLOCI_VAR:-} so an -# explicitly-set Floci var is never overwritten. - -# Storage mode — PERSISTENCE=1 / PERSIST_STATE=1 → persistent storage -if [ -n "${PERSISTENCE:-}" ] || [ -n "${PERSIST_STATE:-}" ]; then - _ls_persist="${PERSISTENCE:-${PERSIST_STATE:-}}" - if [ "${_ls_persist}" = "1" ] || [ "${_ls_persist}" = "true" ]; then - export FLOCI_STORAGE_MODE="${FLOCI_STORAGE_MODE:-persistent}" - fi -fi - -# Bind port — EDGE_PORT → FLOCI_PORT -[ -n "${EDGE_PORT:-}" ] && export FLOCI_PORT="${FLOCI_PORT:-${EDGE_PORT}}" - -# Hostname returned in response URLs — LOCALSTACK_HOST / LOCALSTACK_HOSTNAME → FLOCI_HOSTNAME -_ls_host="${LOCALSTACK_HOST:-${LOCALSTACK_HOSTNAME:-}}" -[ -n "${_ls_host}" ] && export FLOCI_HOSTNAME="${FLOCI_HOSTNAME:-${_ls_host}}" - -# Bind address — GATEWAY_LISTEN → QUARKUS_HTTP_HOST. LocalStack listens there with no further -# opt-in, so GATEWAY_LISTEN also gives the consent Floci requires for a non-loopback address. -if [ -n "${GATEWAY_LISTEN:-}" ]; then - export QUARKUS_HTTP_HOST="${QUARKUS_HTTP_HOST:-${GATEWAY_LISTEN}}" - export FLOCI_SECURITY_ALLOW_UNSAFE_NETWORK_EXPOSURE="${FLOCI_SECURITY_ALLOW_UNSAFE_NETWORK_EXPOSURE:-true}" -fi - -# Log level — LS_LOG / DEBUG=1 → QUARKUS_LOG_LEVEL -if [ -n "${LS_LOG:-}" ]; then - export QUARKUS_LOG_LEVEL="${QUARKUS_LOG_LEVEL:-${LS_LOG}}" -elif [ "${DEBUG:-}" = "1" ]; then - export QUARKUS_LOG_LEVEL="${QUARKUS_LOG_LEVEL:-DEBUG}" -fi - -# Lambda — LAMBDA_EXECUTOR is intentionally ignored; Floci always runs Lambda in Docker containers. - -# Lambda Docker network -[ -n "${LAMBDA_DOCKER_NETWORK:-}" ] && \ - export FLOCI_SERVICES_LAMBDA_DOCKER_NETWORK="${FLOCI_SERVICES_LAMBDA_DOCKER_NETWORK:-${LAMBDA_DOCKER_NETWORK}}" - -# Lambda ephemeral containers -if [ "${LAMBDA_REMOVE_CONTAINERS:-}" = "1" ] || [ "${LAMBDA_REMOVE_CONTAINERS:-}" = "true" ]; then - export FLOCI_SERVICES_LAMBDA_EPHEMERAL="${FLOCI_SERVICES_LAMBDA_EPHEMERAL:-true}" -fi - -# LAMBDA_REMOTE_DOCKER — not fully supported. -# Floci's hot-reload is per-function opt-in (S3Bucket=hot-reload), not a global bind-mount mode. -if [ -n "${LAMBDA_REMOTE_DOCKER:-}" ]; then - echo "[floci-parity] WARNING: LAMBDA_REMOTE_DOCKER is not fully supported by Floci." >&2 - echo "[floci-parity] Use S3Bucket=hot-reload per function instead. See https://floci.io/docs/services/lambda" >&2 -fi - -# Docker host -[ -n "${DOCKER_HOST:-}" ] && export FLOCI_DOCKER_DOCKER_HOST="${FLOCI_DOCKER_DOCKER_HOST:-${DOCKER_HOST}}" - -# Docker network — shared across all container-based services (Lambda, RDS, ElastiCache, MSK, OpenSearch, EKS). -# Per-service overrides (e.g. FLOCI_SERVICES_LAMBDA_DOCKER_NETWORK) take precedence when set. -[ -n "${DOCKER_NETWORK:-}" ] && export FLOCI_SERVICES_DOCKER_NETWORK="${FLOCI_SERVICES_DOCKER_NETWORK:-${DOCKER_NETWORK}}" - -# DNS suffixes — register LocalStack and Floci hostname suffixes so that container-to-container -# hostname routing (Function URLs, presigned S3, SQS QueueUrl, etc.) works without manual config. -_parity_suffixes="localhost.localstack.cloud,localhost.floci.io" -if [ -n "${FLOCI_DNS_EXTRA_SUFFIXES:-}" ]; then - export FLOCI_DNS_EXTRA_SUFFIXES="${FLOCI_DNS_EXTRA_SUFFIXES},${_parity_suffixes}" -else - export FLOCI_DNS_EXTRA_SUFFIXES="${_parity_suffixes}" -fi - -# TLS — USE_SSL=1 → FLOCI_TLS_ENABLED=true -if [ "${USE_SSL:-0}" = "1" ]; then - export FLOCI_TLS_ENABLED="${FLOCI_TLS_ENABLED:-true}" -fi - -# TLS — CUSTOM_SSL_CERT_PATH → FLOCI_TLS_CERT_PATH + FLOCI_TLS_KEY_PATH -# LocalStack uses a single combined PEM (cert+key). Floci accepts it in both fields. -if [ -n "${CUSTOM_SSL_CERT_PATH:-}" ]; then - export FLOCI_TLS_CERT_PATH="${FLOCI_TLS_CERT_PATH:-${CUSTOM_SSL_CERT_PATH}}" - export FLOCI_TLS_KEY_PATH="${FLOCI_TLS_KEY_PATH:-${CUSTOM_SSL_CERT_PATH}}" -fi - -# SERVICES: intentionally ignored; Floci starts every service in ~24ms. diff --git a/docker/test-localstack-parity.sh b/docker/test-localstack-parity.sh deleted file mode 100755 index 2f7e6cf547..0000000000 --- a/docker/test-localstack-parity.sh +++ /dev/null @@ -1,153 +0,0 @@ -#!/bin/sh -# Unit tests for localstack-parity.sh. -# Run directly: sh docker/test-localstack-parity.sh -# Exit 0 on success, non-zero on first failure. - -set -eu - -SCRIPT="$(dirname "$0")/localstack-parity.sh" -PASS=0 -FAIL=0 - -# Run the parity script in a subshell with a given environment and print the -# value of a single variable. Arguments: VAR_NAME [ENV_KEY=VALUE ...] -_run() { - var="$1"; shift - env -i "$@" sh -c ". '${SCRIPT}'; printf '%s' \"\${${var}:-}\"" -} - -# Assert that _run produces an expected value. -assert_eq() { - desc="$1"; expected="$2"; actual="$3" - if [ "${actual}" = "${expected}" ]; then - printf '[PASS] %s\n' "${desc}" - PASS=$((PASS + 1)) - else - printf '[FAIL] %s\n expected: %s\n actual: %s\n' "${desc}" "${expected}" "${actual}" - FAIL=$((FAIL + 1)) - fi -} - -# --- PERSISTENCE --- -assert_eq "PERSISTENCE=1 sets FLOCI_STORAGE_MODE=persistent" \ - "persistent" \ - "$(_run FLOCI_STORAGE_MODE PERSISTENCE=1)" - -assert_eq "PERSISTENCE=true sets FLOCI_STORAGE_MODE=persistent" \ - "persistent" \ - "$(_run FLOCI_STORAGE_MODE PERSISTENCE=true)" - -assert_eq "PERSIST_STATE=1 sets FLOCI_STORAGE_MODE=persistent" \ - "persistent" \ - "$(_run FLOCI_STORAGE_MODE PERSIST_STATE=1)" - -assert_eq "FLOCI_STORAGE_MODE wins over PERSISTENCE" \ - "hybrid" \ - "$(_run FLOCI_STORAGE_MODE PERSISTENCE=1 FLOCI_STORAGE_MODE=hybrid)" - -# --- EDGE_PORT --- -assert_eq "EDGE_PORT sets FLOCI_PORT" \ - "4567" \ - "$(_run FLOCI_PORT EDGE_PORT=4567)" - -assert_eq "FLOCI_PORT wins over EDGE_PORT" \ - "4568" \ - "$(_run FLOCI_PORT EDGE_PORT=4567 FLOCI_PORT=4568)" - -# --- LOCALSTACK_HOST / LOCALSTACK_HOSTNAME --- -assert_eq "LOCALSTACK_HOST sets FLOCI_HOSTNAME" \ - "myhost" \ - "$(_run FLOCI_HOSTNAME LOCALSTACK_HOST=myhost)" - -assert_eq "LOCALSTACK_HOSTNAME sets FLOCI_HOSTNAME when LOCALSTACK_HOST unset" \ - "myhost2" \ - "$(_run FLOCI_HOSTNAME LOCALSTACK_HOSTNAME=myhost2)" - -assert_eq "LOCALSTACK_HOST takes priority over LOCALSTACK_HOSTNAME" \ - "primary" \ - "$(_run FLOCI_HOSTNAME LOCALSTACK_HOST=primary LOCALSTACK_HOSTNAME=secondary)" - -assert_eq "FLOCI_HOSTNAME wins over LOCALSTACK_HOST" \ - "explicit" \ - "$(_run FLOCI_HOSTNAME LOCALSTACK_HOST=myhost FLOCI_HOSTNAME=explicit)" - -# --- GATEWAY_LISTEN --- -assert_eq "GATEWAY_LISTEN sets QUARKUS_HTTP_HOST" \ - "0.0.0.0" \ - "$(_run QUARKUS_HTTP_HOST GATEWAY_LISTEN=0.0.0.0)" - -assert_eq "GATEWAY_LISTEN allows a non-loopback address" \ - "true" \ - "$(_run FLOCI_SECURITY_ALLOW_UNSAFE_NETWORK_EXPOSURE GATEWAY_LISTEN=0.0.0.0)" - -assert_eq "FLOCI_SECURITY_ALLOW_UNSAFE_NETWORK_EXPOSURE wins over GATEWAY_LISTEN" \ - "false" \ - "$(_run FLOCI_SECURITY_ALLOW_UNSAFE_NETWORK_EXPOSURE GATEWAY_LISTEN=0.0.0.0 FLOCI_SECURITY_ALLOW_UNSAFE_NETWORK_EXPOSURE=false)" - -assert_eq "no GATEWAY_LISTEN leaves network exposure unallowed" \ - "" \ - "$(_run FLOCI_SECURITY_ALLOW_UNSAFE_NETWORK_EXPOSURE)" - -# --- LOG LEVEL --- -assert_eq "LS_LOG sets QUARKUS_LOG_LEVEL" \ - "WARN" \ - "$(_run QUARKUS_LOG_LEVEL LS_LOG=WARN)" - -assert_eq "DEBUG=1 sets QUARKUS_LOG_LEVEL=DEBUG" \ - "DEBUG" \ - "$(_run QUARKUS_LOG_LEVEL DEBUG=1)" - -assert_eq "LS_LOG takes priority over DEBUG=1" \ - "TRACE" \ - "$(_run QUARKUS_LOG_LEVEL LS_LOG=TRACE DEBUG=1)" - -assert_eq "QUARKUS_LOG_LEVEL wins over LS_LOG" \ - "INFO" \ - "$(_run QUARKUS_LOG_LEVEL LS_LOG=DEBUG QUARKUS_LOG_LEVEL=INFO)" - -# --- LAMBDA --- -assert_eq "LAMBDA_DOCKER_NETWORK sets FLOCI_SERVICES_LAMBDA_DOCKER_NETWORK" \ - "mynet" \ - "$(_run FLOCI_SERVICES_LAMBDA_DOCKER_NETWORK LAMBDA_DOCKER_NETWORK=mynet)" - -assert_eq "FLOCI_SERVICES_LAMBDA_DOCKER_NETWORK wins over LAMBDA_DOCKER_NETWORK" \ - "floci-net" \ - "$(_run FLOCI_SERVICES_LAMBDA_DOCKER_NETWORK LAMBDA_DOCKER_NETWORK=mynet FLOCI_SERVICES_LAMBDA_DOCKER_NETWORK=floci-net)" - -assert_eq "LAMBDA_REMOVE_CONTAINERS=1 sets FLOCI_SERVICES_LAMBDA_EPHEMERAL=true" \ - "true" \ - "$(_run FLOCI_SERVICES_LAMBDA_EPHEMERAL LAMBDA_REMOVE_CONTAINERS=1)" - -assert_eq "LAMBDA_REMOVE_CONTAINERS=true sets FLOCI_SERVICES_LAMBDA_EPHEMERAL=true" \ - "true" \ - "$(_run FLOCI_SERVICES_LAMBDA_EPHEMERAL LAMBDA_REMOVE_CONTAINERS=true)" - -assert_eq "FLOCI_SERVICES_LAMBDA_EPHEMERAL wins over LAMBDA_REMOVE_CONTAINERS" \ - "false" \ - "$(_run FLOCI_SERVICES_LAMBDA_EPHEMERAL LAMBDA_REMOVE_CONTAINERS=1 FLOCI_SERVICES_LAMBDA_EPHEMERAL=false)" - -# --- DOCKER HOST / NETWORK --- -assert_eq "DOCKER_HOST sets FLOCI_DOCKER_DOCKER_HOST" \ - "unix:///var/run/docker.sock" \ - "$(_run FLOCI_DOCKER_DOCKER_HOST DOCKER_HOST=unix:///var/run/docker.sock)" - -assert_eq "DOCKER_NETWORK sets FLOCI_SERVICES_DOCKER_NETWORK" \ - "shared" \ - "$(_run FLOCI_SERVICES_DOCKER_NETWORK DOCKER_NETWORK=shared)" - -assert_eq "FLOCI_SERVICES_DOCKER_NETWORK wins over DOCKER_NETWORK" \ - "override" \ - "$(_run FLOCI_SERVICES_DOCKER_NETWORK DOCKER_NETWORK=shared FLOCI_SERVICES_DOCKER_NETWORK=override)" - -# --- DNS SUFFIXES --- -assert_eq "DNS suffixes set when FLOCI_DNS_EXTRA_SUFFIXES unset" \ - "localhost.localstack.cloud,localhost.floci.io" \ - "$(_run FLOCI_DNS_EXTRA_SUFFIXES)" - -assert_eq "DNS suffixes appended to existing FLOCI_DNS_EXTRA_SUFFIXES" \ - "custom.internal,localhost.localstack.cloud,localhost.floci.io" \ - "$(_run FLOCI_DNS_EXTRA_SUFFIXES FLOCI_DNS_EXTRA_SUFFIXES=custom.internal)" - -# --- Summary --- -printf '\nResults: %d passed, %d failed\n' "${PASS}" "${FAIL}" -[ "${FAIL}" -eq 0 ] diff --git a/docs/getting-started/migrate-from-localstack.md b/docs/getting-started/migrate-from-localstack.md index 93834407f3..8b3e4b1b56 100644 --- a/docs/getting-started/migrate-from-localstack.md +++ b/docs/getting-started/migrate-from-localstack.md @@ -23,6 +23,8 @@ services: Explicitly set Floci variables always win — the translation only fills in values that haven't been set. To disable the translation entirely, set `LOCALSTACK_PARITY=false`. +The translation happens inside Floci, not in the image's entrypoint, so it applies the same way to the Docker image, the JVM build and a native binary run on the host. + Unless parity is disabled, the startup log also ends with a LocalStack-style `Ready.` line (in addition to Floci's own banner), so tooling that watches the container log for LocalStack's readiness message — such as the default wait strategy of Testcontainers' `LocalStackContainer` — works without a custom wait. ## Step-by-step migration diff --git a/src/main/java/io/github/hectorvent/floci/config/LocalStackParity.java b/src/main/java/io/github/hectorvent/floci/config/LocalStackParity.java new file mode 100644 index 0000000000..d2a223df83 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/config/LocalStackParity.java @@ -0,0 +1,200 @@ +package io.github.hectorvent.floci.config; + +import org.jboss.logging.Logger; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Translates LocalStack Community environment variables into the Floci configuration they mean, + * so a LocalStack setup works unchanged in the image and in a binary on the host alike. + * + *

An explicitly set Floci variable always wins: a mapping is produced only when the matching + * {@code FLOCI_*} or {@code QUARKUS_*} environment variable is absent. The DNS suffixes are the + * one exception: LocalStack's and Floci's hostname suffixes are appended to the configured ones, + * wherever those are set ({@link #withDnsSuffixes}). The whole translation is off when + * {@code LOCALSTACK_PARITY=false}. + */ +public final class LocalStackParity { + + private static final Logger LOG = Logger.getLogger(LocalStackParity.class); + + static final List PARITY_SUFFIXES = List.of("localhost.localstack.cloud", "localhost.floci.io"); + + private static final Pattern BRACKETED_IPV6 = Pattern.compile("^\\[([^\\]]+)\\](?::(\\d+))?$"); + + /** One translated setting: the Floci variable it stands for, its property, and its value. */ + record Mapping(String variable, String property, String value) { + } + + /** A host and a port, either of which may be absent (LocalStack then uses its default). */ + record HostAndPort(String host, String port) { + } + + private LocalStackParity() { + } + + public static boolean enabled(Map env) { + return !"false".equals(env.get("LOCALSTACK_PARITY")); + } + + /** The translation for this process's environment, as configuration property names. */ + public static Map forCurrentEnvironment() { + return translate(System.getenv()); + } + + /** + * @return property name to value, for every LocalStack variable present whose Floci + * counterpart is not set; empty when parity is disabled + */ + public static Map translate(Map env) { + Map out = new LinkedHashMap<>(); + for (Mapping mapping : mappings(env)) { + out.put(mapping.property(), mapping.value()); + } + return out; + } + + /** + * The same translation as {@code FLOCI_*}/{@code QUARKUS_*} variables, for child processes such + * as init hooks: the entrypoint script used to export them, so hooks inherited them. Includes + * {@code FLOCI_DNS_EXTRA_SUFFIXES} with the LocalStack suffixes appended, as the script did. + */ + public static Map environment(Map env) { + Map out = new LinkedHashMap<>(); + if (!enabled(env)) { + return out; + } + for (Mapping mapping : mappings(env)) { + out.put(mapping.variable(), mapping.value()); + } + String suffixes = env.get("FLOCI_DNS_EXTRA_SUFFIXES"); + out.put("FLOCI_DNS_EXTRA_SUFFIXES", String.join(",", withDnsSuffixes( + isSet(suffixes) ? Optional.of(List.of(suffixes.split(","))) : Optional.empty(), env).orElseThrow())); + return out; + } + + /** The configured extra DNS suffixes, plus LocalStack's and Floci's when parity is on. */ + public static Optional> withDnsSuffixes(Optional> configured) { + return withDnsSuffixes(configured, System.getenv()); + } + + static Optional> withDnsSuffixes(Optional> configured, Map env) { + if (!enabled(env)) { + return configured; + } + Set suffixes = new LinkedHashSet<>(configured.orElse(List.of())); + suffixes.addAll(PARITY_SUFFIXES); + return Optional.of(List.copyOf(suffixes)); + } + + private static List mappings(Map env) { + List out = new ArrayList<>(); + if (!enabled(env)) { + return out; + } + + String persist = firstSet(env, "PERSISTENCE", "PERSIST_STATE"); + if ("1".equals(persist) || "true".equals(persist)) { + put(out, env, "FLOCI_STORAGE_MODE", "floci.storage.mode", "persistent"); + } + + // LocalStack listens on GATEWAY_LISTEN with no further opt-in, so it also gives the + // consent Floci requires for a non-loopback address. Its first entry may carry a port, + // which stands in for EDGE_PORT when that is unset. + // An empty host (":4566") keeps Floci's own bind address and takes just the port. + String gatewayListen = env.get("GATEWAY_LISTEN"); + HostAndPort gateway = isSet(gatewayListen) + ? parseHostAndPort(gatewayListen.split(",")[0]) + : new HostAndPort(null, null); + String gatewayHost = gateway.host(); + + put(out, env, "FLOCI_PORT", "floci.port", isSet(env.get("EDGE_PORT")) ? env.get("EDGE_PORT") : gateway.port()); + // LOCALSTACK_HOST is : (default localhost.localstack.cloud:4566); Floci's + // hostname is the host alone, its port comes from floci.port. + String localstackHost = firstSet(env, "LOCALSTACK_HOST", "LOCALSTACK_HOSTNAME"); + put(out, env, "FLOCI_HOSTNAME", "floci.hostname", + isSet(localstackHost) ? parseHostAndPort(localstackHost).host() : null); + + if (isSet(gatewayHost)) { + put(out, env, "QUARKUS_HTTP_HOST", "quarkus.http.host", gatewayHost); + put(out, env, "FLOCI_SECURITY_ALLOW_UNSAFE_NETWORK_EXPOSURE", + "floci.security.allow-unsafe-network-exposure", "true"); + } + + String lsLog = env.get("LS_LOG"); + if (isSet(lsLog)) { + put(out, env, "QUARKUS_LOG_LEVEL", "quarkus.log.level", lsLog); + } else if ("1".equals(env.get("DEBUG"))) { + put(out, env, "QUARKUS_LOG_LEVEL", "quarkus.log.level", "DEBUG"); + } + + put(out, env, "FLOCI_SERVICES_LAMBDA_DOCKER_NETWORK", "floci.services.lambda.docker-network", + env.get("LAMBDA_DOCKER_NETWORK")); + String removeContainers = env.get("LAMBDA_REMOVE_CONTAINERS"); + if ("1".equals(removeContainers) || "true".equals(removeContainers)) { + put(out, env, "FLOCI_SERVICES_LAMBDA_EPHEMERAL", "floci.services.lambda.ephemeral", "true"); + } + if (isSet(env.get("LAMBDA_REMOTE_DOCKER"))) { + LOG.warn("LAMBDA_REMOTE_DOCKER is not fully supported by Floci. Use S3Bucket=hot-reload per function" + + " instead. See https://floci.io/docs/services/lambda"); + } + + put(out, env, "FLOCI_DOCKER_DOCKER_HOST", "floci.docker.docker-host", env.get("DOCKER_HOST")); + // Shared by every container-backed service; per-service networks still take precedence. + put(out, env, "FLOCI_SERVICES_DOCKER_NETWORK", "floci.services.docker-network", env.get("DOCKER_NETWORK")); + + if ("1".equals(env.get("USE_SSL"))) { + put(out, env, "FLOCI_TLS_ENABLED", "floci.tls.enabled", "true"); + } + // LocalStack takes one combined PEM (certificate and key); Floci accepts it in both fields. + String customCert = env.get("CUSTOM_SSL_CERT_PATH"); + put(out, env, "FLOCI_TLS_CERT_PATH", "floci.tls.cert-path", customCert); + put(out, env, "FLOCI_TLS_KEY_PATH", "floci.tls.key-path", customCert); + + // LAMBDA_EXECUTOR and SERVICES are intentionally ignored. + return out; + } + + /** + * Parses the forms LocalStack's {@code HostAndPort.parse} accepts: {@code host:port}, + * {@code host}, {@code :port}, {@code [v6]:port} and {@code [v6]}. A missing part is null. + */ + static HostAndPort parseHostAndPort(String input) { + String value = input.trim(); + Matcher ipv6 = BRACKETED_IPV6.matcher(value); + if (ipv6.matches()) { + return new HostAndPort(ipv6.group(1), ipv6.group(2)); + } + int colon = value.indexOf(':'); + if (colon < 0) { + return new HostAndPort(value.isEmpty() ? null : value, null); + } + String host = value.substring(0, colon).trim(); + String port = value.substring(colon + 1).trim(); + return new HostAndPort(host.isEmpty() ? null : host, port.isEmpty() ? null : port); + } + + private static void put(List out, Map env, String flociVariable, + String property, String value) { + if (isSet(value) && !isSet(env.get(flociVariable))) { + out.add(new Mapping(flociVariable, property, value)); + } + } + + private static String firstSet(Map env, String first, String second) { + String value = env.get(first); + return isSet(value) ? value : env.get(second); + } + + private static boolean isSet(String value) { + return value != null && !value.isEmpty(); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/config/LocalStackParityConfigSource.java b/src/main/java/io/github/hectorvent/floci/config/LocalStackParityConfigSource.java new file mode 100644 index 0000000000..2e8299f738 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/config/LocalStackParityConfigSource.java @@ -0,0 +1,56 @@ +package io.github.hectorvent.floci.config; + +import org.eclipse.microprofile.config.spi.ConfigSource; + +import java.util.Map; +import java.util.Set; + +/** + * Serves {@link LocalStackParity}'s translation as configuration. + * + *

The ordinal (299) sits just below environment variables (300), and above {@code .env} (295) + * and {@code config/application.yml} (265), as the {@code FLOCI_*} variables the replaced shell + * script exported did. A translated key is only present when its Floci variable is unset, so it + * never shadows one. Staying below 300 also keeps {@link TlsConfigSource}'s loopback pin of + * {@code quarkus.http.host} in force when TLS is on and {@code GATEWAY_LISTEN} names the public + * address, which {@link NetworkExposureGuard#publicHost} still reads from here. + */ +public class LocalStackParityConfigSource implements ConfigSource { + + static final int ORDINAL = 299; + + private final Map properties; + + public LocalStackParityConfigSource() { + this(LocalStackParity.forCurrentEnvironment()); + } + + LocalStackParityConfigSource(Map properties) { + this.properties = Map.copyOf(properties); + } + + @Override + public Map getProperties() { + return properties; + } + + @Override + public Set getPropertyNames() { + return properties.keySet(); + } + + @Override + public String getValue(String propertyName) { + return properties.get(propertyName); + } + + @Override + public String getName() { + return "floci-localstack-parity"; + } + + @Override + public int getOrdinal() { + return ORDINAL; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/config/TlsCertificateManager.java b/src/main/java/io/github/hectorvent/floci/config/TlsCertificateManager.java index cdc2124538..46d7ae0e4a 100644 --- a/src/main/java/io/github/hectorvent/floci/config/TlsCertificateManager.java +++ b/src/main/java/io/github/hectorvent/floci/config/TlsCertificateManager.java @@ -250,7 +250,7 @@ public class TlsCertificateManager implements Resettable { } catch (RuntimeException e) { LOG.debugv("TLS: floci.base-url {0} has no usable host: {1}", config.baseUrl(), e.getMessage()); } - config.dns().extraSuffixes().ifPresent(list -> list.forEach(s -> suffixes.add(s.toLowerCase(Locale.ROOT)))); + LocalStackParity.withDnsSuffixes(config.dns().extraSuffixes()).ifPresent(list -> list.forEach(s -> suffixes.add(s.toLowerCase(Locale.ROOT)))); String bare = name.startsWith("*.") ? name.substring(2) : name; for (String suffix : suffixes) { if (DomainNames.isWithin(bare, suffix)) { diff --git a/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java b/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java index dfc85f7938..b6f7416a97 100644 --- a/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java +++ b/src/main/java/io/github/hectorvent/floci/config/TlsConfigSource.java @@ -207,6 +207,11 @@ public class TlsConfigSource implements ConfigSource { if (value != null && !value.isBlank()) return value; + // 3. A LocalStack variable that means the same thing (USE_SSL, CUSTOM_SSL_CERT_PATH) + value = LocalStackParity.forCurrentEnvironment().get(key); + if (value != null && !value.isBlank()) + return value; + return defaultValue; } diff --git a/src/main/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServer.java b/src/main/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServer.java index 289a395812..8bb457325a 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServer.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/dns/EmbeddedDnsServer.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.core.common.dns; import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.config.LocalStackParity; import io.github.hectorvent.floci.config.TlsConfigSource; import io.github.hectorvent.floci.core.common.DomainNames; import io.github.hectorvent.floci.core.common.RegionResolver; @@ -157,7 +158,7 @@ public class EmbeddedDnsServer { suffixes.addAll(BUILTIN_SUFFIXES); config.hostname().ifPresent(suffixes::add); - config.dns().extraSuffixes().ifPresent(suffixes::addAll); + LocalStackParity.withDnsSuffixes(config.dns().extraSuffixes()).ifPresent(suffixes::addAll); if (config.dns().spoofAwsEndpoints()) { suffixes.addAll(TlsConfigSource.awsEndpointSuffixes()); if (shouldWarnSpoofInvisibleToTls(true, TlsConfigSource.tlsEnabledVisibleToTls(), diff --git a/src/main/java/io/github/hectorvent/floci/lifecycle/inithook/HookScriptExecutor.java b/src/main/java/io/github/hectorvent/floci/lifecycle/inithook/HookScriptExecutor.java index 9b4740aabe..ec0d83df43 100644 --- a/src/main/java/io/github/hectorvent/floci/lifecycle/inithook/HookScriptExecutor.java +++ b/src/main/java/io/github/hectorvent/floci/lifecycle/inithook/HookScriptExecutor.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.lifecycle.inithook; import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.config.LocalStackParity; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import org.jboss.logging.Logger; @@ -39,7 +40,11 @@ public class HookScriptExecutor { LOG.debugv("Executing hook script {0} via {1}", scriptFileName, command.getFirst()); // Inherit parent I/O so script output is streamed directly and does not block on unconsumed buffers. - final Process process = new ProcessBuilder(command).directory(hookDirectory).inheritIO().start(); + final ProcessBuilder builder = new ProcessBuilder(command).directory(hookDirectory).inheritIO(); + // The entrypoint script used to export the translated LocalStack settings as FLOCI_* + // variables, and hooks read them; the translation now lives in Java, so pass it on. + LocalStackParity.environment(System.getenv()).forEach(builder.environment()::putIfAbsent); + final Process process = builder.start(); run(process, scriptFileName); } diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java index d0a8b83ce1..7619a6bd18 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudfront/CloudFrontService.java @@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.cloudfront; import com.fasterxml.jackson.core.type.TypeReference; import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.config.LocalStackParity; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.CloudFrontEdgeDomain; import io.github.hectorvent.floci.core.common.RegionResolver; @@ -473,7 +474,7 @@ public class CloudFrontService implements ServerCertificateReferenceProvider { endpointHosts.add("localhost"); EmbeddedDnsServer.BUILTIN_SUFFIXES.forEach(endpointHosts::add); config.hostname().ifPresent(endpointHosts::add); - config.dns().extraSuffixes().ifPresent(endpointHosts::addAll); + LocalStackParity.withDnsSuffixes(config.dns().extraSuffixes()).ifPresent(endpointHosts::addAll); return endpointHosts.stream() .map(host -> ".cloudfront." + host.toLowerCase(Locale.ROOT)) .toList(); diff --git a/src/main/java/io/github/hectorvent/floci/services/route53/Route53DnsRecordSource.java b/src/main/java/io/github/hectorvent/floci/services/route53/Route53DnsRecordSource.java index 1ac74c648f..b6b1880904 100644 --- a/src/main/java/io/github/hectorvent/floci/services/route53/Route53DnsRecordSource.java +++ b/src/main/java/io/github/hectorvent/floci/services/route53/Route53DnsRecordSource.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.route53; import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.config.LocalStackParity; import io.github.hectorvent.floci.core.common.DomainNames; import io.github.hectorvent.floci.core.common.dns.DnsAnswer; import io.github.hectorvent.floci.core.common.dns.DnsLookupHelper; @@ -60,7 +61,7 @@ public class Route53DnsRecordSource implements DnsRecordSource { if (config != null) { config.hostname().ifPresent(this.flociSuffixes::add); if (config.dns() != null) { - config.dns().extraSuffixes().ifPresent(this.flociSuffixes::addAll); + LocalStackParity.withDnsSuffixes(config.dns().extraSuffixes()).ifPresent(this.flociSuffixes::addAll); } } } diff --git a/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java b/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java index f6f246bb5a..73e083e207 100644 --- a/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java +++ b/src/main/java/io/github/hectorvent/floci/services/s3/S3VirtualHostFilter.java @@ -1,6 +1,7 @@ package io.github.hectorvent.floci.services.s3; import io.github.hectorvent.floci.config.EmulatorConfig; +import io.github.hectorvent.floci.config.LocalStackParity; import io.github.hectorvent.floci.core.common.AwsPartitions; import io.github.hectorvent.floci.core.common.AwsRegions; import io.github.hectorvent.floci.core.common.RequestHost; @@ -76,7 +77,7 @@ public class S3VirtualHostFilter implements ContainerRequestFilter { .orElseGet(() -> containerDetector.isRunningInContainer() ? EmbeddedDnsServer.DEFAULT_SUFFIX : extractHostnameFromUrl(config.baseUrl())); - this.serviceHostSuffixes = buildServiceHostSuffixes(config.hostname(), config.dns().extraSuffixes()); + this.serviceHostSuffixes = buildServiceHostSuffixes(config.hostname(), LocalStackParity.withDnsSuffixes(config.dns().extraSuffixes())); this.s3ServiceInstance = s3ServiceInstance; } diff --git a/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource b/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource index 52c0f16b32..628e8e2d4c 100644 --- a/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource +++ b/src/main/resources/META-INF/services/org.eclipse.microprofile.config.spi.ConfigSource @@ -1,2 +1,3 @@ io.github.hectorvent.floci.config.TlsConfigSource io.github.hectorvent.floci.config.HostDefaultsConfigSource +io.github.hectorvent.floci.config.LocalStackParityConfigSource diff --git a/src/test/java/io/github/hectorvent/floci/config/LocalStackParityTest.java b/src/test/java/io/github/hectorvent/floci/config/LocalStackParityTest.java new file mode 100644 index 0000000000..e64372cfad --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/config/LocalStackParityTest.java @@ -0,0 +1,186 @@ +package io.github.hectorvent.floci.config; + +import org.junit.jupiter.api.Test; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.aMapWithSize; +import static org.hamcrest.Matchers.allOf; +import static org.hamcrest.Matchers.anEmptyMap; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.greaterThan; +import static org.hamcrest.Matchers.hasKey; +import static org.hamcrest.Matchers.lessThan; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.nullValue; + +/** The cases of the former docker/test-localstack-parity.sh, plus the TLS mappings it did not cover. */ +class LocalStackParityTest { + + private static String value(String property, String... env) { + Map map = new HashMap<>(); + for (int i = 0; i < env.length; i += 2) { + map.put(env[i], env[i + 1]); + } + return LocalStackParity.translate(map).get(property); + } + + @Test + void persistence() { + assertThat(value("floci.storage.mode", "PERSISTENCE", "1"), equalTo("persistent")); + assertThat(value("floci.storage.mode", "PERSISTENCE", "true"), equalTo("persistent")); + assertThat(value("floci.storage.mode", "PERSIST_STATE", "1"), equalTo("persistent")); + assertThat(value("floci.storage.mode", "PERSISTENCE", "0"), nullValue()); + assertThat(value("floci.storage.mode", "PERSISTENCE", "1", "FLOCI_STORAGE_MODE", "hybrid"), nullValue()); + } + + @Test + void edgePort() { + assertThat(value("floci.port", "EDGE_PORT", "4567"), equalTo("4567")); + assertThat(value("floci.port", "EDGE_PORT", "4567", "FLOCI_PORT", "4568"), nullValue()); + } + + @Test + void hostname() { + assertThat(value("floci.hostname", "LOCALSTACK_HOST", "myhost"), equalTo("myhost")); + assertThat(value("floci.hostname", "LOCALSTACK_HOSTNAME", "myhost2"), equalTo("myhost2")); + assertThat(value("floci.hostname", "LOCALSTACK_HOST", "primary", "LOCALSTACK_HOSTNAME", "secondary"), + equalTo("primary")); + assertThat(value("floci.hostname", "LOCALSTACK_HOST", "myhost", "FLOCI_HOSTNAME", "explicit"), nullValue()); + } + + @Test + void gatewayListen() { + assertThat(value("quarkus.http.host", "GATEWAY_LISTEN", "0.0.0.0"), equalTo("0.0.0.0")); + assertThat(value("floci.security.allow-unsafe-network-exposure", "GATEWAY_LISTEN", "0.0.0.0"), equalTo("true")); + assertThat(value("floci.security.allow-unsafe-network-exposure", "GATEWAY_LISTEN", "0.0.0.0", + "FLOCI_SECURITY_ALLOW_UNSAFE_NETWORK_EXPOSURE", "false"), nullValue()); + assertThat(value("floci.security.allow-unsafe-network-exposure"), nullValue()); + } + + @Test + void logLevel() { + assertThat(value("quarkus.log.level", "LS_LOG", "WARN"), equalTo("WARN")); + assertThat(value("quarkus.log.level", "DEBUG", "1"), equalTo("DEBUG")); + assertThat(value("quarkus.log.level", "LS_LOG", "TRACE", "DEBUG", "1"), equalTo("TRACE")); + assertThat(value("quarkus.log.level", "LS_LOG", "DEBUG", "QUARKUS_LOG_LEVEL", "INFO"), nullValue()); + } + + @Test + void lambda() { + assertThat(value("floci.services.lambda.docker-network", "LAMBDA_DOCKER_NETWORK", "mynet"), equalTo("mynet")); + assertThat(value("floci.services.lambda.docker-network", "LAMBDA_DOCKER_NETWORK", "mynet", + "FLOCI_SERVICES_LAMBDA_DOCKER_NETWORK", "floci-net"), nullValue()); + assertThat(value("floci.services.lambda.ephemeral", "LAMBDA_REMOVE_CONTAINERS", "1"), equalTo("true")); + assertThat(value("floci.services.lambda.ephemeral", "LAMBDA_REMOVE_CONTAINERS", "true"), equalTo("true")); + assertThat(value("floci.services.lambda.ephemeral", "LAMBDA_REMOVE_CONTAINERS", "1", + "FLOCI_SERVICES_LAMBDA_EPHEMERAL", "false"), nullValue()); + } + + @Test + void dockerHostAndNetwork() { + assertThat(value("floci.docker.docker-host", "DOCKER_HOST", "unix:///var/run/docker.sock"), + equalTo("unix:///var/run/docker.sock")); + assertThat(value("floci.services.docker-network", "DOCKER_NETWORK", "shared"), equalTo("shared")); + assertThat(value("floci.services.docker-network", "DOCKER_NETWORK", "shared", + "FLOCI_SERVICES_DOCKER_NETWORK", "override"), nullValue()); + } + + @Test + void dnsSuffixesAreAppendedToWhateverIsConfigured() { + // Not a translated property: it would outrank and replace suffixes set in application.yml. + assertThat(value("floci.dns.extra-suffixes"), nullValue()); + assertThat(LocalStackParity.withDnsSuffixes(Optional.empty(), Map.of()), + equalTo(Optional.of(List.of("localhost.localstack.cloud", "localhost.floci.io")))); + assertThat(LocalStackParity.withDnsSuffixes(Optional.of(List.of("custom.internal", "localhost.floci.io")), + Map.of()), + equalTo(Optional.of(List.of("custom.internal", "localhost.floci.io", "localhost.localstack.cloud")))); + assertThat(LocalStackParity.withDnsSuffixes(Optional.of(List.of("custom.internal")), + Map.of("LOCALSTACK_PARITY", "false")), + equalTo(Optional.of(List.of("custom.internal")))); + } + + @Test + void gatewayListenWithAPortGivesTheHostAndThePort() { + assertThat(value("quarkus.http.host", "GATEWAY_LISTEN", "0.0.0.0:4566"), equalTo("0.0.0.0")); + assertThat(value("floci.port", "GATEWAY_LISTEN", "0.0.0.0:4567"), equalTo("4567")); + assertThat(value("quarkus.http.host", "GATEWAY_LISTEN", "[::]:4566,0.0.0.0:443"), equalTo("::")); + assertThat(value("quarkus.http.host", "GATEWAY_LISTEN", "0.0.0.0"), equalTo("0.0.0.0")); + assertThat(value("floci.port", "GATEWAY_LISTEN", "0.0.0.0:4567", "EDGE_PORT", "4570"), equalTo("4570")); + assertThat(value("floci.port", "GATEWAY_LISTEN", "0.0.0.0:4567", "FLOCI_PORT", "4571"), nullValue()); + } + + @Test + void gatewayListenWithOnlyAPortKeepsFlociBindAddress() { + assertThat(value("floci.port", "GATEWAY_LISTEN", ":4566"), equalTo("4566")); + assertThat(value("quarkus.http.host", "GATEWAY_LISTEN", ":4566"), nullValue()); + assertThat(value("floci.security.allow-unsafe-network-exposure", "GATEWAY_LISTEN", ":4566"), nullValue()); + assertThat(value("quarkus.http.host", "GATEWAY_LISTEN", "[::1]"), equalTo("::1")); + } + + @Test + void localstackHostDropsItsPort() { + assertThat(value("floci.hostname", "LOCALSTACK_HOST", "localhost.localstack.cloud:4566"), + equalTo("localhost.localstack.cloud")); + assertThat(value("floci.hostname", "LOCALSTACK_HOSTNAME", "myhost:4567"), equalTo("myhost")); + assertThat(value("floci.hostname", "LOCALSTACK_HOST", ":4566"), nullValue()); + } + + @Test + void hostAndPortFollowLocalStacksForms() { + assertThat(LocalStackParity.parseHostAndPort("0.0.0.0:4566"), + equalTo(new LocalStackParity.HostAndPort("0.0.0.0", "4566"))); + assertThat(LocalStackParity.parseHostAndPort("0.0.0.0"), equalTo(new LocalStackParity.HostAndPort("0.0.0.0", null))); + assertThat(LocalStackParity.parseHostAndPort(":4566"), equalTo(new LocalStackParity.HostAndPort(null, "4566"))); + assertThat(LocalStackParity.parseHostAndPort("[::]:4566"), equalTo(new LocalStackParity.HostAndPort("::", "4566"))); + assertThat(LocalStackParity.parseHostAndPort("[::1]"), equalTo(new LocalStackParity.HostAndPort("::1", null))); + } + + @Test + void hooksGetTheTranslationAsVariables() { + Map env = LocalStackParity.environment(Map.of("EDGE_PORT", "4567", "PERSISTENCE", "1")); + assertThat(env.get("FLOCI_PORT"), equalTo("4567")); + assertThat(env.get("FLOCI_STORAGE_MODE"), equalTo("persistent")); + assertThat(env.get("FLOCI_DNS_EXTRA_SUFFIXES"), equalTo("localhost.localstack.cloud,localhost.floci.io")); + assertThat(LocalStackParity.environment(Map.of("EDGE_PORT", "4567", "FLOCI_PORT", "4568")), + not(hasKey("FLOCI_PORT"))); + assertThat(LocalStackParity.environment(Map.of("LOCALSTACK_PARITY", "false", "EDGE_PORT", "4567")), + anEmptyMap()); + } + + @Test + void tls() { + assertThat(value("floci.tls.enabled", "USE_SSL", "1"), equalTo("true")); + assertThat(value("floci.tls.enabled", "USE_SSL", "0"), nullValue()); + assertThat(value("floci.tls.enabled", "USE_SSL", "1", "FLOCI_TLS_ENABLED", "false"), nullValue()); + assertThat(value("floci.tls.cert-path", "CUSTOM_SSL_CERT_PATH", "/certs/ls.pem"), equalTo("/certs/ls.pem")); + assertThat(value("floci.tls.key-path", "CUSTOM_SSL_CERT_PATH", "/certs/ls.pem"), equalTo("/certs/ls.pem")); + } + + @Test + void disabledTranslatesNothing() { + assertThat(LocalStackParity.translate(Map.of("LOCALSTACK_PARITY", "false", "EDGE_PORT", "4567")), + aMapWithSize(0)); + assertThat(LocalStackParity.translate(Map.of("LOCALSTACK_PARITY", "true", "EDGE_PORT", "4567")), + hasKey("floci.port")); + } + + @Test + void ignoredVariablesProduceNothing() { + Map out = LocalStackParity.translate(Map.of("LAMBDA_EXECUTOR", "local", "SERVICES", "s3")); + assertThat(out.keySet(), equalTo(Set.of())); + assertThat(out, not(hasKey("floci.services.lambda.executor"))); + } + + @Test + void ordinalSitsBelowEnvironmentAndTlsButAboveTheConfigFiles() { + // .env is 295, config/application.yml 265; environment variables and TlsConfigSource are 300. + assertThat(new LocalStackParityConfigSource(Map.of()).getOrdinal(), allOf(greaterThan(295), lessThan(300))); + assertThat(new TlsConfigSource().getOrdinal(), greaterThan(new LocalStackParityConfigSource(Map.of()).getOrdinal())); + } +}