diff --git a/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchService.java b/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchService.java index e3ab611d4c..e9dd12a45f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchService.java +++ b/src/main/java/io/github/hectorvent/floci/services/opensearch/OpenSearchService.java @@ -4,6 +4,7 @@ import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.docker.ContainerStorageHelper; import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.AwsRegions; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; import io.github.hectorvent.floci.core.storage.StorageBackend; @@ -149,10 +150,12 @@ public class OpenSearchService implements ResourceProvider { if (config.services().opensearch().mock()) { domain.setProcessing(false); + domain.setEndpoint(synthesizedEndpoint(domainName, region)); } else { domain.setProcessing(true); if (!domainManager.tryStartDomain(domain)) { domain.setProcessing(false); + domain.setEndpoint(synthesizedEndpoint(domainName, region)); } } @@ -161,6 +164,24 @@ public class OpenSearchService implements ResourceProvider { return domain; } + /** + * The endpoint reported for a domain with no container behind it. Two cases reach this: the + * service is mocked, and no Docker daemon is reachable, which is the only condition under which + * {@link OpenSearchDomainManager#tryStartDomain} returns false rather than rethrowing. + * + *

It reports an AWS-shaped endpoint in place of a blank one, + * {@code search--..es.}, taking the DNS suffix from the + * region's partition rather than assuming the commercial one, with the name suffix derived from the + * domain name so it survives a restart. A domain reporting {@code Processing false} alongside + * {@code Endpoint ""} describes a state AWS never returns, and a client that reads the endpoint + * to address the domain gets an empty string to connect to. + */ + private static String synthesizedEndpoint(String domainName, String region) { + String suffix = Integer.toHexString(domainName.hashCode() & 0x7fffffff); + return "search-" + domainName + "-" + suffix + "." + region + ".es." + + AwsRegions.dnsSuffixFor(region); + } + public Domain describeDomain(String domainName) { return domainStore.get(domainName) .orElseThrow(() -> new AwsException("ResourceNotFoundException", diff --git a/src/test/java/io/github/hectorvent/floci/services/opensearch/OpenSearchDomainEndpointIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/opensearch/OpenSearchDomainEndpointIntegrationTest.java new file mode 100644 index 0000000000..ef5e08fb5b --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/opensearch/OpenSearchDomainEndpointIntegrationTest.java @@ -0,0 +1,90 @@ +package io.github.hectorvent.floci.services.opensearch; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.emptyString; +import static org.hamcrest.Matchers.endsWith; +import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.not; +import static org.hamcrest.Matchers.startsWith; + +/** + * A domain that reports itself no longer processing also reports an endpoint. + * + *

Covers the two cases with no container behind the domain: the service is mocked, and no Docker + * daemon is reachable. Both once reported {@code Processing false} alongside {@code Endpoint ""}, + * which AWS never returns, leaving a client that reads the endpoint with an empty string to address. + */ +@QuarkusTest +class OpenSearchDomainEndpointIntegrationTest { + + private static final String AUTH_HEADER = + "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/es/aws4_request"; + private static final String DOMAIN = "endpoint-probe"; + + @AfterEach + void cleanup() { + given().header("Authorization", AUTH_HEADER) + .when().delete("/2021-01-01/opensearch/domain/" + DOMAIN); + } + + /** The DNS suffix comes from the region's partition, so a China region must not report .com. */ + @Test + void theEndpointTakesItsDnsSuffixFromThePartition() { + String domain = "endpoint-cn-probe"; + try { + given() + .header("Authorization", + "AWS4-HMAC-SHA256 Credential=AKID/20260101/cn-north-1/es/aws4_request") + .contentType("application/json") + .body("{\"DomainName\":\"" + domain + "\",\"EngineVersion\":\"OpenSearch_2.11\"}") + .when() + .post("/2021-01-01/opensearch/domain") + .then() + .statusCode(200) + .body("DomainStatus.Endpoint", endsWith(".cn-north-1.es.amazonaws.com.cn")); + } finally { + given().header("Authorization", + "AWS4-HMAC-SHA256 Credential=AKID/20260101/cn-north-1/es/aws4_request") + .when().delete("/2021-01-01/opensearch/domain/" + domain); + } + } + + @Test + void createAndDescribeReportAnEndpointBesideProcessingFalse() { + given() + .header("Authorization", AUTH_HEADER) + .contentType("application/json") + .body("{\"DomainName\":\"" + DOMAIN + "\",\"EngineVersion\":\"OpenSearch_2.11\"}") + .when() + .post("/2021-01-01/opensearch/domain") + .then() + .statusCode(200) + .body("DomainStatus.Processing", is(false)) + .body("DomainStatus.Endpoint", not(emptyString())) + .body("DomainStatus.Endpoint", startsWith("search-" + DOMAIN + "-")) + .body("DomainStatus.Endpoint", endsWith(".us-east-1.es.amazonaws.com")); + + String first = given() + .header("Authorization", AUTH_HEADER) + .when() + .get("/2021-01-01/opensearch/domain/" + DOMAIN) + .then() + .statusCode(200) + .body("DomainStatus.Processing", is(false)) + .body("DomainStatus.Endpoint", not(emptyString())) + .extract().path("DomainStatus.Endpoint"); + + // The waiter polls, so the value has to be the same every time. + given() + .header("Authorization", AUTH_HEADER) + .when() + .get("/2021-01-01/opensearch/domain/" + DOMAIN) + .then() + .statusCode(200) + .body("DomainStatus.Endpoint", is(first)); + } +}