mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
fix(elasticache): serve a second cache asking for 6379 instead of refusing it (#4693)
* fix(elasticache): serve a second cache asking for 6379 instead of refusing it
CreateReplicationGroup answered a second cache on port 6379 with
InvalidParameterValue: Port 6379 is already in use by another
replication group.
AWS has no such rule. Port belongs to a cache's own endpoint, and 6379 is
the Redis default, so nearly every ElastiCache cluster uses it and they
coexist: AWS gives each cache 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 tells them apart. Any
module standing up two Redis clusters, and any suite running one per test in
parallel, failed at the second create.
A pinned port that is free and inside the proxy range is still honored. One
that is taken, or outside the range, now yields a port from the range rather
than a rejection, and that port is what the cache's endpoint reports.
The port a cache reports is always the port it is actually on. That is the
whole design, and it rules out the tempting alternative of reporting the
pinned port while listening elsewhere: the endpoint would then name a
DIFFERENT cache's listener, and a client following it reads the wrong
cache's data with no error at all. Measured, on a build that did exactly
that: two no-auth caches pinned to 6379, write through each, then read
through the second's advertised endpoint and get the FIRST cache's value,
from the host and from inside a container both. #4094 removed that same
failure from the restore path last week, where "the old endpoint then
reached an unrelated cache instead of failing cleanly".
What a caller gives up is the pinned port when Floci cannot serve it, which
terraform surfaces as a replacement-forcing diff on the next plan. A visible
difference, against a silent wrong answer: the create still succeeds, which
is what unblocks the modules.
The out-of-range rejection goes for the same reason. That range bounds what
this emulator may listen on and never bounded what AWS accepts. The only
rejection left is AWS's own, 1150 to 65535, as RdsService already applies.
Tests: the two that pinned the old rejections now pin the substitution, and
ElastiCachePortSharingIntegrationTest asserts at the DATA plane, writing
through each cache's own advertised endpoint and reading its own value back.
A control-plane-only test passes against the wrong-endpoint design too,
which is how that design survived review. ElastiCache surface: 214 tests,
0 failures.
Refs: floci-j55a
* test(elasticache): gate Docker per test, tie each endpoint to its own group
Five review points, no behaviour change. The service diff is the 1150 comment.
CORRECTING THE PREVIOUS COMMIT MESSAGE, which cannot be amended without a
force-push. It said a control-plane-only test "passes against the wrong-
endpoint design too, which is how that design got through four rounds". False
about this file: Order(2) compares the two REPORTED ports and requires them to
differ, and the withdrawn design reported 6379 for both, so it fails there
before anything connects. The general point stands, but not as stated. An
assertion about a SINGLE reported value cannot see a wrong-endpoint design,
because "the API says 6379" is satisfied whatever 6379 serves. An assertion
about a RELATIONSHIP between reported values often can. That is the
distinction, not control plane against data plane. Both javadocs said the
wrong thing and both are rewritten.
@BeforeAll gated the whole class on Docker, and only Order(6) needs it: a
group reaches available with no daemon reachable. On a Docker-less runner the
entire regression test for this fix would have vanished, silently and green,
including the control-plane assertion that catches the original bug. The
assumption now sits in Order(6) alone.
Order(6) claimed more than it proved. Writing "first" and "second" through the
two endpoints and reading each back passes under a consistent relabelling,
where each endpoint reaches a distinct cache that is not its own. Each group
now carries its own AuthToken and each proxy validates the token of the group
it was started for, so an endpoint reaching the other group's proxy is refused
its own group's token. The test AUTHs with its own token and requires the
other group's to be refused, which is what makes the method name true. Order(2)
stays: the two catch different things and neither subsumes the other.
Those refusal assertions check the proxy's actual error text, not the helper's
prefix, because a connection returning nothing would also fail to start with
"+OK" and must not read as a pass.
The docs section stated two rules as the service's that are false for
Memcached: CreateCacheCluster routes that engine to a path Port is not passed
to, so a pinned port is ignored and the 1150 check never runs. Scoped to
redis/valkey, with Memcached's behaviour stated. Third time on this work that
a Redis-path rule was written as though it covered the service.
The 1150 bound is no longer called AWS's own, in docs or javadoc. It is the
bound RdsService applies to the same argument; nothing here establishes
ElastiCache's real limit. It is strictly more permissive than the check it
replaced, so the risk is accepting a port AWS would refuse, never refusing one
AWS accepts. The upper arm was untested and now is.
ElastiCache surface: 215 tests, 0 failures, 0 skipped.
Refs: floci-j55a
* test(elasticache): stop the method name claiming the bound is AWS's
Two review points, both about claims rather than behaviour. The only
non-comment change in the diff is the rename.
aPortOutsideTheRangeAwsAcceptsIsStillRefused was the last place in the tree
still asserting that 1150 to 65535 is what AWS accepts, after the previous
commit removed that claim from the docs, the service javadoc and the unit
test. It is now aPortOutsideTheAcceptedRangeIsStillRefused, and its javadoc
says the bound is the one RdsService.reserveProxyPort applies to the same
argument, with nothing here establishing ElastiCache's real limit.
It survived a round that corrected the identical claim in three other places
because a method name does not present itself as prose. It is read far more
often than the body under it and re-read far less, which is true of a display
name, a bead title and a commit subject too.
Order(2) carried a one-line javadoc about the original bug, so a reader
landing there sees no reason it is not redundant against Order(6). It now says
what it catches that Order(6) cannot: comparing the two REPORTED ports is what
rejects a build reporting one port for both caches, it does so before anything
connects, and it still runs on a machine where Order(6) skips for want of
Docker. The non-subsumption was already stated in the class javadoc and in
Order(6)'s, which is not where someone doubting Order(2) is standing.
ElastiCache surface: 215 tests, 0 failures, 0 skipped. The renamed test is
present in the surefire report under its new name.
Refs: floci-j55a
* test(elasticache): SDK coverage for port sharing, and two test defects
Three Greptile findings on #4693, all in the tests. Verified live against
87d663e4e rather than trusting the stale marker: the single-read helper, the
absolute 6379 assertion and the absent SDK coverage were all still there.
SDK VALIDATION. AGENTS.md wants management APIs exercised through an AWS SDK
client, and src/test has no SDK dependency, so it cannot host that; the repo
keeps it in compatibility-tests/sdk-test-java. ElastiCachePortSharingTest goes
there. It proves what the RestAssured assertions cannot: that the substituted
port deserialises into the MODEL a caller reads, on the create response, on
the describe, and on the member cluster terraform follows. A response can be
well formed and still leave PrimaryEndpoint.Port null.
Self-contained by construction, after the shared-fixture trouble on #4598: its
own client, its own uniquely named groups, its own cleanup, no @Order coupling
and no fixture another class can tear down first.
Measured both ways rather than assumed. Against a build with the old refusal
put back, it fails with InvalidParameterValueException "Port 6379 is already
in use by another replication group"; against this branch it passes. An
assertion never seen red is not evidence.
PARTIAL READS. readReply did one in.read(), so a RESP reply split by TCP
misparsed. Theoretical over loopback until Order(6) went from four round trips
to six, and it fails against a WORKING proxy, which makes it a flakiness
source rather than a correctness bug. A flaky test in a regression suite is
worse than no test: it teaches people to re-run. It now reads a line at a time
and honours a bulk string's declared length.
THE FIRST PORT IS NO LONGER ASSUMED FREE. Order(1) required 6379 exactly. If
another class in the shared Quarkus instance holds it, substitution is the
feature working correctly, and the test would have failed on that before
reaching what it exists to check: broken by the code behaving as designed. It
now records whatever port comes back and asserts the relationship, which is
what the class is about anyway.
ElastiCache surface: 215 tests, 0 failures, 0 skipped. SDK suite: the new
class passes against a running emulator.
Refs: floci-j55a
* test(elasticache): guard the data-plane test with the service's own Docker check
The assumption gating Order(6) shelled out to `docker info`. Floci reaches its
daemon through DockerClientProducer, which resolves the endpoint from the
active Docker context rather than the platform default, so on a machine using
an OrbStack socket, or with no docker CLI installed, the guard can fail while
the client that actually starts the containers works.
It now calls ElastiCacheContainerManager.isDockerReachable(), which is the
method the service itself uses to tell a missing daemon from a container that
failed for its own reasons, pinging the same DockerClient bean the caches are
started with. Not a second check that happens to agree: the same one.
This is the failure mode worth spending a commit on. Order(6) is the only
data-plane assertion in the class, and it is the only thing standing between
us and an unverified endpoint-identity property now that the guard sits on it
alone rather than on the whole class. A guard that fires spuriously deletes
exactly the test the class exists for, and leaves a green run behind. Every
other defect on this branch was caught by somebody reading the code; a
spurious skip is invisible to that.
It is also the shape of most of the findings here: the check and the thing it
checks used different mechanisms. The docker CLI is not the connection under
test, a single read() is not a RESP reply, and 6379 is not the port a cache is
necessarily on.
ElastiCache surface: 215 tests, 0 failures, 0 skipped, with Order(6) present
in the surefire XML and no skipped tags.
Refs: floci-j55a
* test(elasticache): name the SDK model types the port test validates
AGENTS.md wants explicit types at call sites so the AWS models under
validation are visible, and three response values in the new SDK test were
var.
Not only style here. The reason that class exists is that a response can be
well formed and still leave PrimaryEndpoint.Port unset in the model a caller
reads, so the model types ARE the subject: var hid
CreateReplicationGroupResponse, DescribeReplicationGroupsResponse and
DescribeCacheClustersResponse at exactly the three places a reader checks what
is being deserialised.
Refs: floci-j55a
This commit is contained in:
1 parent
005ecb3b76
commit
b8aec4c50f
5 files changed
+607
-36
No files matched your search
+140
@@ -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.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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();
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
+47
-24
@@ -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.
|
||||
*
|
||||
* <p>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.
|
||||
* <p>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.
|
||||
*
|
||||
* <p>Only an unpinned create falls back through the range below.
|
||||
* {@code NeptuneService.allocateProxyPort} still substitutes on this path and carries the
|
||||
* same flaw.
|
||||
* <p>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 <em>different</em> 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".
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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;
|
||||
|
||||
+351
@@ -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.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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("<Error>"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Each group's endpoint reaches that group's own cache.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>{@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.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>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.
|
||||
*
|
||||
* <p>{@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.
|
||||
*
|
||||
* <p>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();
|
||||
}
|
||||
}
|
||||
+36
-12
@@ -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));
|
||||
|
||||
Reference in new issue
Block a user