diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElastiCachePortSharingTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElastiCachePortSharingTest.java new file mode 100644 index 0000000000..d4869b9eba --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElastiCachePortSharingTest.java @@ -0,0 +1,140 @@ +package com.floci.test; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.services.elasticache.ElastiCacheClient; +import software.amazon.awssdk.services.elasticache.model.CreateReplicationGroupRequest; +import software.amazon.awssdk.services.elasticache.model.CreateReplicationGroupResponse; +import software.amazon.awssdk.services.elasticache.model.DeleteReplicationGroupRequest; +import software.amazon.awssdk.services.elasticache.model.DescribeCacheClustersRequest; +import software.amazon.awssdk.services.elasticache.model.DescribeCacheClustersResponse; +import software.amazon.awssdk.services.elasticache.model.DescribeReplicationGroupsRequest; +import software.amazon.awssdk.services.elasticache.model.DescribeReplicationGroupsResponse; +import software.amazon.awssdk.services.elasticache.model.Endpoint; +import software.amazon.awssdk.services.elasticache.model.ReplicationGroup; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * Two caches both asking for 6379, read back through the real AWS SDK. + * + *

On AWS, Port belongs to a cache's own endpoint, so 6379 is shared routinely and two + * replication groups on it coexist. Floci puts every cache's proxy on one host, where the port is + * the only thing that tells them apart, so the second cache is served on a port of its own and + * that is the port it reports. + * + *

What the SDK adds over the handcrafted-HTTP tests in {@code src/test}: those assert on the + * XML a describe returns, and a response can be well formed and still not populate the model a + * caller actually reads. This one goes through the generated client, so it fails if the + * substituted port does not deserialise into {@code PrimaryEndpoint.Port} on the create response, + * on the describe, and on the member cluster the terraform provider follows. + * + *

Self-contained on purpose: its own client, its own uniquely named groups, its own cleanup. It + * shares no fixture with another class and no ordering with one, so nothing it needs can be torn + * down by a test that happens to run first. + */ +@DisplayName("ElastiCache port sharing") +class ElastiCachePortSharingTest { + + /** The Redis default, and the port a module pins when it does not think about it. */ + private static final int SHARED_PORT = 6379; + + private static ElastiCacheClient elasticache; + private static String firstGroupId; + private static String secondGroupId; + + @BeforeAll + static void setup() { + elasticache = TestFixtures.elastiCacheClient(); + firstGroupId = TestFixtures.uniqueName("ec-share-a"); + secondGroupId = TestFixtures.uniqueName("ec-share-b"); + } + + @AfterAll + static void cleanup() { + if (elasticache == null) { + return; + } + for (String groupId : new String[] {firstGroupId, secondGroupId}) { + try { + elasticache.deleteReplicationGroup(DeleteReplicationGroupRequest.builder() + .replicationGroupId(groupId) + .build()); + } catch (Exception ignored) { + // Best effort: a group whose create failed has nothing to delete. + } + } + } + + @Test + void bothCachesArePortedAndEachReportsThePortItIsOn() { + int firstPort = createOnSharedPort(firstGroupId); + + // The create AWS performs and Floci used to answer InvalidParameterValue. + int secondPort = createOnSharedPort(secondGroupId); + + assertThat(secondPort) + .as("the second cache must be served on a port of its own, not the first's") + .isNotEqualTo(firstPort); + + // The whole point of going through the SDK: the substituted port has to survive + // deserialisation into the model, not merely appear in the XML. + assertThat(describedPort(firstGroupId)) + .as("describe must report the first cache on the port its create reported") + .isEqualTo(firstPort); + assertThat(describedPort(secondGroupId)) + .as("describe must report the second cache on the port its create reported") + .isEqualTo(secondPort); + + // DescribeCacheClusters is where terraform-provider-aws reads a member's port back, so + // the two describes must agree or a plan never settles. + assertThat(memberPort(secondGroupId)) + .as("the member cluster must agree with its group's endpoint") + .isEqualTo(secondPort); + } + + private static int createOnSharedPort(String groupId) { + CreateReplicationGroupResponse response = elasticache.createReplicationGroup(CreateReplicationGroupRequest.builder() + .replicationGroupId(groupId) + .replicationGroupDescription("port sharing compat test") + .engine("redis") + .numCacheClusters(1) + .port(SHARED_PORT) + .build()); + + assertThat(response.replicationGroup().replicationGroupId()).isEqualTo(groupId); + Endpoint endpoint = primaryEndpoint(response.replicationGroup()); + assertThat(endpoint) + .as("the create response must carry an endpoint the caller can dial") + .isNotNull(); + assertThat(endpoint.port()) + .as("the port must deserialise into the model, not merely into the XML") + .isNotNull() + .isPositive(); + return endpoint.port(); + } + + private static int describedPort(String groupId) { + DescribeReplicationGroupsResponse response = elasticache.describeReplicationGroups(DescribeReplicationGroupsRequest.builder() + .replicationGroupId(groupId) + .build()); + assertThat(response.replicationGroups()).hasSize(1); + return primaryEndpoint(response.replicationGroups().get(0)).port(); + } + + private static int memberPort(String groupId) { + DescribeCacheClustersResponse response = elasticache.describeCacheClusters(DescribeCacheClustersRequest.builder() + .cacheClusterId(groupId + "-001") + .showCacheNodeInfo(true) + .build()); + assertThat(response.cacheClusters()).hasSize(1); + assertThat(response.cacheClusters().get(0).cacheNodes()).isNotEmpty(); + return response.cacheClusters().get(0).cacheNodes().get(0).endpoint().port(); + } + + private static Endpoint primaryEndpoint(ReplicationGroup group) { + return group.nodeGroups().get(0).primaryEndpoint(); + } +} diff --git a/docs/services/elasticache.md b/docs/services/elasticache.md index d4704ddefc..c8d2becf5f 100644 --- a/docs/services/elasticache.md +++ b/docs/services/elasticache.md @@ -66,6 +66,39 @@ created against a name nothing resolves.) Each record re-reserves its proxy port as it is restored, before Floci serves anything, so a create is never handed a port a surviving cluster or replication group still advertises. +#### Port, when two caches ask for the same one + +On AWS, `Port` belongs to a cache's own endpoint. 6379 is the Redis default, so nearly every +ElastiCache cluster uses it and they coexist: each cache gets a DNS name of its own, and the +port never has to tell them apart. Floci puts every cache's proxy on one host, where a TCP port +is exclusive and the port is the only thing that does tell them apart. + +This section is about `Engine=redis` and `Engine=valkey`, the engines Floci fronts with an auth +proxy. `Engine=memcached` never reaches any of it: `CreateCacheCluster` routes that engine to a +path the `Port` is not passed to, so a pinned port is silently ignored and the cluster reports +the Docker-published port it is actually on. The safety property below holds there for a +different reason, not because the rules do. + +So a `Port` you pass is honored when it is free and inside +`FLOCI_SERVICES_ELASTICACHE_PROXY_BASE_PORT`/`_MAX_PORT`, and otherwise the cache is served on +another port from that range, which is the port its endpoint then reports. A create is never +refused for a port that is merely taken: refusing the second cache on 6379 breaks any module +standing up two Redis clusters, and any suite running one per test in parallel. + +**The port a cache reports is always the port it is actually on.** If you pinned one Floci could +not give you, the endpoint reports the substitute rather than the port you asked for, and the +create logs a warning saying which and why. That is a visible difference: terraform treats the +port as replacement-forcing, so a pinned port that was unavailable shows up as a diff on the +next plan. It is deliberately preferred to the alternative, where a cache reports the port you +pinned while listening somewhere else, because that endpoint names a *different* cache's +listener and a client following it reads the wrong cache's data with no error. + +The only port rejection left is a range check: `Port` must be between 1150 and 65535. That bound +is the one `RdsService.reserveProxyPort` applies to the same argument, not a measurement of +ElastiCache's own limit, which nothing here establishes. It is strictly more permissive than the +proxy-range check it replaced, so the direction it could be wrong in is accepting a port AWS +would refuse, never refusing one AWS accepts. + `SnapshotRetentionLimit`, `SnapshotWindow`, `PreferredMaintenanceWindow`, `PreferredAvailabilityZone`, `SecurityGroupIds`, `NetworkType`, `IpDiscovery` and `AtRestEncryptionEnabled` (output-only on real AWS's CreateCacheCluster; accepted here leniently and echoed) are kept and echoed by `DescribeCacheClusters`, with the same defaults diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.java index c513ce1ca8..71a40c3c0a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.java +++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheService.java @@ -1652,41 +1652,64 @@ public class ElastiCacheService implements ResourceProvider { } /** - * Honors the request's {@code Port} when it is free and inside the proxy range. AWS models - * Port as an optional input on CreateReplicationGroup ("the port number on which each member - * of the replication group accepts connections"), so a caller that pins one and reads back a - * different value sees permanent drift: Terraform treats the port as replacement-forcing. + * Honors the request's {@code Port} when it is free and inside the proxy range, and otherwise + * serves the cache on a port from that range instead of refusing it. * - *

An explicit port is therefore either honored or refused, never quietly changed. - * Substituting one reproduces the very drift honoring it was meant to remove, and the - * substitution could only ever hit a caller who did ask for a port: one who does not care - * passes null and never reaches that branch. Floci multiplexes every group's proxy onto one - * host, so two groups genuinely cannot share a port, and a caller who pinned an unavailable - * one needs to know rather than discover it as drift later. + *

On AWS, Port belongs to a cache's own endpoint. 6379 is the Redis default, so nearly + * every ElastiCache cluster in the world uses it and they coexist: AWS gives each cache a DNS + * name of its own, and the port never has to distinguish them. Floci multiplexes every + * cache's proxy onto one host, where a TCP port is exclusive and the port is the only thing + * that does distinguish them. Refusing the second cache on 6379, which is what this method + * used to do, diverges from AWS on an entirely ordinary request: any module standing up two + * Redis clusters, and any suite running one per test in parallel, failed at the second + * create. * - *

Only an unpinned create falls back through the range below. - * {@code NeptuneService.allocateProxyPort} still substitutes on this path and carries the - * same flaw. + *

Which leaves a choice about the port a caller pinned but cannot have, and only two of + * the three obvious answers are safe. Refusing is the divergence above. Reporting the pinned + * port anyway, while listening on another, is worse than it sounds: the endpoint then names + * a listener belonging to a different cache, so a client that dials it reads someone + * else's data and is told nothing. That is the failure #4094 removed on the restore path, + * where "the old endpoint then reached an unrelated cache instead of failing cleanly". + * + *

So the port a cache reports is always the port it is actually on. A caller that pinned + * an unavailable port sees drift, which terraform surfaces as a replacement-forcing diff on + * the next plan. That is a visible, reversible failure, where the alternative is a silent + * wrong answer, and the create still succeeds. The WARN below names the port that was served + * and why the pinned one could not be. + * + *

A port outside the proxy range is substituted for the same reason rather than refused: + * the range bounds what this emulator may listen on, and never bounded what AWS accepts. The + * only rejection left is a range check, 1150 to 65535, which is the bound + * {@code RdsService.reserveProxyPort} applies to the same argument. Nothing here establishes + * that it is ElastiCache's real lower bound on AWS, so do not read it as one; it is strictly + * more permissive than the proxy-range check it replaced, which bounds the risk to accepting + * a port AWS would refuse rather than refusing one AWS accepts. */ private int allocateProxyPort(Integer requested) { int base = config.services().elasticache().proxyBasePort(); int max = config.services().elasticache().proxyMaxPort(); if (requested != null) { - if (requested < base || requested > max) { - LOG.infov("Rejecting ElastiCache port {0}: outside the proxy range {1}-{2}", - String.valueOf(requested), String.valueOf(base), String.valueOf(max)); + if (requested < 1150 || requested > 65535) { throw new AwsException("InvalidParameterValue", - "Port " + requested + " is outside the port range this emulator serves (" - + base + "-" + max + ").", 400); + "Port must be between 1150 and 65535.", 400); } - if (!usedPorts.add(requested)) { - LOG.infov("Rejecting ElastiCache port {0}: already used by another replication group", - String.valueOf(requested)); - throw new AwsException("InvalidParameterValue", - "Port " + requested + " is already in use by another replication group.", 400); + if (requested >= base && requested <= max && usedPorts.add(requested)) { + return requested; } - return requested; + int substitute = scanForFreePort(base, max); + LOG.warnv("ElastiCache port {0} could not be served: it is {1}. This cache is on {2} " + + "instead, and that is the port its endpoint reports.", + String.valueOf(requested), + requested < base || requested > max + ? "outside the proxy range " + base + "-" + max + : "already in use by another cache here", + String.valueOf(substitute)); + return substitute; } + return scanForFreePort(base, max); + } + + private int scanForFreePort(int base, int max) { for (int port = base; port <= max; port++) { if (usedPorts.add(port)) { return port; diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCachePortSharingIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCachePortSharingIntegrationTest.java new file mode 100644 index 0000000000..18eb585bd9 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCachePortSharingIntegrationTest.java @@ -0,0 +1,351 @@ +package io.github.hectorvent.floci.services.elasticache; + +import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerManager; +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.ValidatableResponse; +import jakarta.inject.Inject; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestMethodOrder; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.net.Socket; +import java.nio.charset.StandardCharsets; +import java.util.List; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.containsString; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Two caches may both ask for 6379, and each is reachable at the endpoint it reports. + * + *

Nothing here requires either cache to land on 6379 itself. Another class in this shared + * Quarkus instance may already hold it, in which case the substitution is the feature working, + * and a test demanding 6379 would fail on correct behaviour before reaching what it exists to + * check. What is asserted is the relationship: two caches asking for one port are served on + * two ports, and each reports the one it is on. + * + *

6379 is the Redis default, so nearly every ElastiCache cluster uses it and on AWS they + * coexist: each cache has a DNS name of its own and the port never distinguishes them. Floci has + * one host, so the port is the only thing that does. It used to refuse the second cache, which + * fails any module standing up two Redis clusters. It now serves the second cache on a port of + * its own and reports that port. + * + *

Two assertions carry the weight, and it is worth being exact about which rules out what. + * {@code Order(2)} compares the two REPORTED ports and requires them to differ. One host port can + * only name one listener, so a build that reported the pinned port on both caches fails there, on + * the control plane, before anything connects. {@code Order(6)} then writes through each cache's + * own advertised endpoint and reads its own value back, which rules out two endpoints reaching + * one cache even when the numbers differ. + * + *

The useful distinction is not control plane against data plane. It is whether an assertion + * constrains the thing a wrong design could get wrong. An assertion about a SINGLE reported value + * cannot: "the API says 6379" is satisfied whatever 6379 actually serves. An assertion about a + * RELATIONSHIP between reported values often can, which is why {@code Order(2)} bites. + */ +@QuarkusTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class ElastiCachePortSharingIntegrationTest { + + private static final String AUTH_HEADER = + "AWS4-HMAC-SHA256 Credential=test/20260412/us-east-1/elasticache/aws4_request"; + + /** The Redis default, and the port every module in the corpus pins. */ + private static final int SHARED_PORT = 6379; + + private static final String FIRST_GROUP = "it-ec-share-a"; + private static final String SECOND_GROUP = "it-ec-share-b"; + /** + * A token per group, because the proxy validates it against the group it was started for + * (see the {@code validatePassword(groupId, ...)} each startProxy is given). That makes the + * token the one thing a cache knows about its own control-plane identity, and it is what + * lets {@code Order(6)} tell "reaches its own cache" from "reaches a distinct cache". + */ + /** What a proxy answers AUTH with another group's token; pinned by ElastiCacheIntegrationTest. */ + private static final String WRONG_TOKEN_ERROR = "invalid username-password pair"; + private static final String FIRST_TOKEN = "token-for-group-a"; + private static final String SECOND_TOKEN = "token-for-group-b"; + + private static final String CREATED_PORT = + "CreateReplicationGroupResponse.CreateReplicationGroupResult.ReplicationGroup" + + ".NodeGroups.NodeGroup.PrimaryEndpoint.Port"; + private static final String DESCRIBED_PORT = + "DescribeReplicationGroupsResponse.DescribeReplicationGroupsResult.ReplicationGroups" + + ".ReplicationGroup.NodeGroups.NodeGroup.PrimaryEndpoint.Port"; + private static final String MEMBER_NODE_PORT = + "DescribeCacheClustersResponse.DescribeCacheClustersResult.CacheClusters.CacheCluster" + + ".CacheNodes.CacheNode.Endpoint.Port"; + + private static int firstPort; + private static int secondPort; + + /** The manager that starts these caches' containers, so the guard is the service's own. */ + @Inject + ElastiCacheContainerManager containerManager; + + @AfterAll + static void cleanup() { + for (String groupId : List.of(FIRST_GROUP, SECOND_GROUP)) { + try { + given() + .formParam("Action", "DeleteReplicationGroup") + .formParam("ReplicationGroupId", groupId) + .header("Authorization", AUTH_HEADER) + .post("/"); + } catch (Exception ignored) { + // Best effort: a group a test never created has nothing to tear down. + } + } + } + + @Test + @Order(1) + void firstGroupIsServedAndReportsWhereItIs() { + firstPort = createGroup(FIRST_GROUP, SHARED_PORT, FIRST_TOKEN) + .statusCode(200) + .extract().xmlPath().getInt(CREATED_PORT); + + assertTrue(firstPort > 0, "The first group must report a port it can be dialled on"); + } + + /** + * The bug: AWS creates this group, and Floci answered InvalidParameterValue. + * + *

Not redundant against {@code Order(6)}, which is the reading this assertion invites. + * Comparing the two REPORTED ports is what rejects a build that reports one port for both + * caches, and it does so before anything connects, so it survives on a runner where + * {@code Order(6)} is skipped for want of Docker. {@code Order(6)} catches what this cannot, + * namely two distinct reported ports that do not reach the caches they name. + */ + @Test + @Order(2) + void secondGroupOnTheSamePortIsCreatedRatherThanRefused() { + secondPort = createGroup(SECOND_GROUP, SHARED_PORT, SECOND_TOKEN) + .statusCode(200) + .extract().xmlPath().getInt(CREATED_PORT); + + assertNotEquals(firstPort, secondPort, + "The second group must be served somewhere of its own, not on the first's port"); + } + + @Test + @Order(3) + void eachGroupKeepsReportingThePortItIsOn() { + assertEquals(firstPort, describeGroup(FIRST_GROUP).extract().xmlPath().getInt(DESCRIBED_PORT)); + assertEquals(secondPort, describeGroup(SECOND_GROUP).extract().xmlPath().getInt(DESCRIBED_PORT)); + } + + /** The terraform provider reads a member's port out of DescribeCacheClusters. */ + @Test + @Order(4) + void memberCacheClustersAgreeWithTheGroupEndpoint() { + assertEquals(secondPort, + describeMember(SECOND_GROUP + "-001").extract().xmlPath().getInt(MEMBER_NODE_PORT)); + } + + /** + * The one range check left. The bound is the one {@code RdsService.reserveProxyPort} applies + * to the same argument; nothing here establishes ElastiCache's real limit on AWS, so the name + * says "accepted" rather than claiming the range is AWS's. + */ + @Test + @Order(5) + void aPortOutsideTheAcceptedRangeIsStillRefused() { + given() + .formParam("Action", "CreateReplicationGroup") + .formParam("ReplicationGroupId", "it-ec-share-bad-port") + .formParam("ReplicationGroupDescription", "port sharing test") + .formParam("Engine", "redis") + .formParam("NumCacheClusters", "1") + .formParam("Port", "80") + .header("Authorization", AUTH_HEADER) + .when() + .post("/") + .then() + .statusCode(400) + .body(containsString("InvalidParameterValue")) + .body(containsString("")); + } + + /** + * Each group's endpoint reaches that group's own cache. + * + *

Docker is required here and only here: this is the one test that needs a container + * behind the proxy. Orders 1 to 5 are control-plane assertions and a group reaches + * {@code available} with no daemon reachable, so gating the whole class on Docker would + * delete the regression test for this fix on a Docker-less runner, silently and green. + * + *

{@code Order(2)} already rejects a build reporting one port for both caches. Writing and + * reading through each endpoint adds the case two distinct reported ports cannot rule out, + * where both endpoints alias onto a single cache. Neither of those, on its own, separates + * "reaches its own cache" from "reaches a distinct cache that is not its own": a consistent + * relabelling would carry both writes and both reads with it and pass. + * + *

The AUTH is what closes that. Each proxy validates the token of the group it was started + * for, so a token is the one thing a cache knows about its control-plane identity. An + * endpoint that reached the other group's proxy would be refused its own group's token, which + * the last two assertions require to be accepted and the other group's to be refused. + */ + @Test + @Order(6) + void eachAdvertisedEndpointReachesItsOwnCache() throws Exception { + Assumptions.assumeTrue(isDockerAvailable(), + "Docker must be available: this assertion needs a real cache behind each proxy"); + + assertEquals("+OK", withAuth(firstPort, FIRST_TOKEN, "SET", "who", "first").trim()); + assertEquals("+OK", withAuth(secondPort, SECOND_TOKEN, "SET", "who", "second").trim()); + + assertEquals("first", bulkString(withAuth(firstPort, FIRST_TOKEN, "GET", "who")), + "The first group's endpoint must reach the first group"); + assertEquals("second", bulkString(withAuth(secondPort, SECOND_TOKEN, "GET", "who")), + "and the second group's endpoint must reach the second group, not the first"); + + // Identity, not just distinctness: each endpoint must refuse the other group's token. + // On the refusal itself rather than on the helper's prefix: a connection that returned + // nothing at all would also fail to start with "+OK", and that must not read as a pass. + assertTrue(withAuth(firstPort, SECOND_TOKEN, "GET", "who").contains(WRONG_TOKEN_ERROR), + "The first group's endpoint must refuse the second group's token, and say so"); + assertTrue(withAuth(secondPort, FIRST_TOKEN, "GET", "who").contains(WRONG_TOKEN_ERROR), + "The second group's endpoint must refuse the first group's token, and say so"); + } + + private static ValidatableResponse createGroup(String groupId, int port, String authToken) { + return given() + .formParam("Action", "CreateReplicationGroup") + .formParam("ReplicationGroupId", groupId) + .formParam("ReplicationGroupDescription", "port sharing test") + .formParam("Engine", "redis") + .formParam("NumCacheClusters", "1") + .formParam("Port", String.valueOf(port)) + .formParam("AuthToken", authToken) + .header("Authorization", AUTH_HEADER) + .when() + .post("/") + .then(); + } + + private static ValidatableResponse describeGroup(String groupId) { + return given() + .formParam("Action", "DescribeReplicationGroups") + .formParam("ReplicationGroupId", groupId) + .header("Authorization", AUTH_HEADER) + .when() + .post("/") + .then() + .statusCode(200); + } + + private static ValidatableResponse describeMember(String cacheClusterId) { + return given() + .formParam("Action", "DescribeCacheClusters") + .formParam("CacheClusterId", cacheClusterId) + .formParam("ShowCacheNodeInfo", "true") + .header("Authorization", AUTH_HEADER) + .when() + .post("/") + .then() + .statusCode(200); + } + + private static String resp(String... args) { + StringBuilder sb = new StringBuilder("*").append(args.length).append("\r\n"); + for (String arg : args) { + sb.append('$').append(arg.length()).append("\r\n").append(arg).append("\r\n"); + } + return sb.toString(); + } + + /** + * AUTHs with {@code token}, then runs one command on the same connection. Returns + * {@code AUTH-REFUSED} plus the reply when the token is not accepted, which is what an + * endpoint reaching a different group's proxy produces. + */ + private static String withAuth(int port, String token, String... command) throws IOException { + try (Socket socket = new Socket("localhost", port)) { + socket.setSoTimeout(10_000); + OutputStream out = socket.getOutputStream(); + InputStream in = socket.getInputStream(); + out.write(resp("AUTH", token).getBytes(StandardCharsets.UTF_8)); + out.flush(); + String authReply = readReply(in); + if (!authReply.startsWith("+OK")) { + return "AUTH-REFUSED: " + authReply.trim(); + } + out.write(resp(command).getBytes(StandardCharsets.UTF_8)); + out.flush(); + return readReply(in); + } + } + + /** + * Reads exactly one RESP reply. A single {@code read()} returns whatever happened to arrive, + * which is usually the whole reply over loopback and is not guaranteed to be: TCP may split it + * anywhere. That turns a working proxy into an intermittently failing test, which is worse + * than no test because it teaches people to re-run. + * + *

Handles the two shapes these commands produce: a single line for {@code +OK}, {@code -ERR} + * and the like, and a bulk string whose first line declares how many bytes follow. + */ + private static String readReply(InputStream in) throws IOException { + String header = readLine(in); + if (header.isEmpty() || header.charAt(0) != '$') { + return header; + } + int length = Integer.parseInt(header.substring(1).trim()); + if (length < 0) { + return header; + } + byte[] body = in.readNBytes(length); + if (body.length < length) { + throw new IOException("Short bulk string: wanted " + length + ", read " + body.length); + } + readLine(in); + return header + new String(body, StandardCharsets.UTF_8) + "\r\n"; + } + + /** One CRLF-terminated line, byte at a time so nothing of the next reply is consumed. */ + private static String readLine(InputStream in) throws IOException { + StringBuilder line = new StringBuilder(); + int b; + while ((b = in.read()) != -1) { + line.append((char) b); + if (line.length() >= 2 + && line.charAt(line.length() - 2) == '\r' + && line.charAt(line.length() - 1) == '\n') { + return line.toString(); + } + } + return line.toString(); + } + + private static String bulkString(String reply) { + String[] lines = reply.split("\r\n"); + return lines.length > 1 ? lines[1] : reply; + } + + /** + * The service's own check, not a second one that happens to agree. + * + *

{@code isDockerReachable} is the method {@link ElastiCacheContainerManager} uses to tell + * a missing daemon from a container that failed for its own reasons, and it pings the + * {@code DockerClient} bean the containers are actually started with. The {@code docker} CLI + * is a different oracle: {@code DockerClientProducer} resolves the daemon from the active + * Docker context, often not the platform default (an OrbStack socket, say), so {@code docker + * info} can fail, or the binary be absent, while Floci's client works. + * + *

This guard protects the only data-plane assertion in the class. One that fired + * spuriously would delete exactly the test the class exists for, silently and green, which is + * the one failure here that no amount of reading the output would catch. + */ + private boolean isDockerAvailable() { + return containerManager.isDockerReachable(); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.java index aabdf9c2e6..5ac86df066 100644 --- a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheServiceTest.java @@ -268,6 +268,7 @@ class ElastiCacheServiceTest { assertEquals(16390, group.getProxyPort(), "A free, in-range requested Port must be the port the group reports"); + assertEquals(16390, group.getConfigurationEndpoint().port()); } @Test @@ -279,31 +280,54 @@ class ElastiCacheServiceTest { } @Test - void requestedPortAlreadyInUseIsRejected() { - // floci multiplexes every group's proxy onto one host, so two groups cannot share a port. - // Substituting a different one would hand back the drift honoring Port exists to remove, - // and it could only ever hit a caller who did pin a port. + void aSecondGroupOnATakenPortIsServedElsewhereRatherThanRefused() { + // AWS gives each cache a DNS name of its own, so 6379 is shared routinely and two groups + // on it coexist. Floci has one host and one port namespace, so the second group cannot + // have the port; refusing it diverged from AWS on an ordinary request, and reporting the + // pinned port while listening elsewhere would point its endpoint at the FIRST group's + // listener. It is served on a port of its own, and reports that port. service.createReplicationGroup(singleNodeRequest("grp1", 16390)); - AwsException thrown = assertThrows(AwsException.class, - () -> service.createReplicationGroup(singleNodeRequest("grp2", 16390))); + ReplicationGroup second = service.createReplicationGroup(singleNodeRequest("grp2", 16390)); - assertEquals("InvalidParameterValue", thrown.getErrorCode()); - assertTrue(thrown.getMessage().contains("16390")); + assertEquals(16379, second.getProxyPort(), + "The second group takes a free port rather than being refused"); + assertEquals(second.getProxyPort(), second.getConfigurationEndpoint().port(), + "and its endpoint reports the port it is actually on, never the other group's"); } @Test - void requestedPortOutsideTheProxyRangeIsRejected() { + void aPortOutsideTheProxyRangeIsServedElsewhereRatherThanRefused() { + // The range bounds what Floci may listen on. It never bounded what AWS accepts. + ReplicationGroup group = service.createReplicationGroup(singleNodeRequest("grp", 9999)); + + assertEquals(16379, group.getProxyPort()); + assertEquals(16379, group.getConfigurationEndpoint().port(), + "An endpoint never advertises a port nothing is listening on"); + } + + // Both arms of the one range check that is left. The bound is RdsService's, applied to the + // same argument; nothing here establishes it is ElastiCache's real AWS limit. + @Test + void aPortBelowTheAcceptedRangeIsRejected() { AwsException thrown = assertThrows(AwsException.class, - () -> service.createReplicationGroup(singleNodeRequest("grp", 9999))); + () -> service.createReplicationGroup(singleNodeRequest("grp", 80))); assertEquals("InvalidParameterValue", thrown.getErrorCode()); - assertTrue(thrown.getMessage().contains("9999")); + assertTrue(thrown.getMessage().contains("1150")); + } + + @Test + void aPortAboveTheAcceptedRangeIsRejected() { + AwsException thrown = assertThrows(AwsException.class, + () -> service.createReplicationGroup(singleNodeRequest("grp", 70000))); + + assertEquals("InvalidParameterValue", thrown.getErrorCode()); + assertTrue(thrown.getMessage().contains("65535")); } @Test void anUnpinnedCreateStillFallsBackWhenTheBasePortIsTaken() { - // The fallback survives for callers that named no port: only an explicit one is refused. service.createReplicationGroup(singleNodeRequest("grp1", 16379)); ReplicationGroup second = service.createReplicationGroup(singleNodeRequest("grp2", null));