fix(elasticache): preserve member security group metadata (#5937)

Keep CreateReplicationGroup SecurityGroupIds in persistent group metadata and report active memberships on every DescribeCacheClusters member. Cover both topologies and Query list spellings with SDK regressions, plus legacy JSON defaults.
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-18 11:13:30 -04:00
1 parent 301ac79c6e
commit 3a44e19e36
7 files changed
+256 -22

No files matched your search

@@ -0,0 +1,174 @@
package io.github.hectorvent.floci.services.elasticache;
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerHandle;
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerManager;
import io.github.hectorvent.floci.services.elasticache.container.ValkeyClusterFormation;
import io.quarkus.test.InjectMock;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.elasticache.ElastiCacheClient;
import software.amazon.awssdk.services.elasticache.model.CacheCluster;
import software.amazon.awssdk.services.elasticache.model.CreateReplicationGroupRequest;
import software.amazon.awssdk.services.elasticache.model.ReplicationGroupNotFoundException;
import software.amazon.awssdk.services.elasticache.model.SecurityGroupMembership;
import java.net.URI;
import java.util.List;
import java.util.UUID;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;
@QuarkusTest
class ElastiCacheMemberSecurityGroupsIntegrationTest {
private static final String AUTH_HEADER =
"AWS4-HMAC-SHA256 Credential=test/20261018/us-east-1/elasticache/aws4_request";
private static final List<String> SECURITY_GROUP_IDS = List.of("sg-0123456789abcdef0", "sg-0fedcba9876543210");
@TestHTTPResource
URI endpoint;
@InjectMock
ElastiCacheContainerManager containerManager;
// Cluster formation talks to the backing Valkey nodes, which are not started by these control-plane tests.
@InjectMock
ValkeyClusterFormation clusterFormation;
@BeforeEach
void stubOnlyBackendProvisioning() {
when(containerManager.tryStart(anyString(), anyString(), anyString()))
.thenAnswer(invocation -> containerHandle(invocation.getArgument(0)));
when(containerManager.start(anyString(), anyString(), anyList(), anyString()))
.thenAnswer(invocation -> containerHandle(invocation.getArgument(0)));
}
@ParameterizedTest
@ValueSource(booleans = {false, true})
void sdkCreatePreservesSecurityGroupsOnEveryMember(boolean clusterEnabled) {
String groupId = groupId();
try (ElastiCacheClient client = client()) {
try {
client.createReplicationGroup(createRequest(groupId, clusterEnabled)
.securityGroupIds(SECURITY_GROUP_IDS).build());
assertMemberSecurityGroups(client, groupId, clusterEnabled ? 4 : 1, SECURITY_GROUP_IDS);
} finally {
deleteGroup(client, groupId);
}
}
}
@ParameterizedTest
@ValueSource(booleans = {false, true})
void omittedSecurityGroupsDoNotFabricateMemberships(boolean clusterEnabled) {
String groupId = groupId();
try (ElastiCacheClient client = client()) {
try {
client.createReplicationGroup(createRequest(groupId, clusterEnabled).build());
assertMemberSecurityGroups(client, groupId, clusterEnabled ? 4 : 1, List.of());
} finally {
deleteGroup(client, groupId);
}
}
}
@ParameterizedTest
@ValueSource(strings = {"SecurityGroupIds.SecurityGroupId", "SecurityGroupIds.member"})
void queryListSpellingsRoundtripThroughTypedSdkMembers(String prefix) {
String groupId = groupId();
try (ElastiCacheClient client = client()) {
try {
given()
.port(endpoint.getPort())
.header("Authorization", AUTH_HEADER)
.formParam("Action", "CreateReplicationGroup")
.formParam("Version", "2015-02-02")
.formParam("ReplicationGroupId", groupId)
.formParam("ReplicationGroupDescription", "member security groups")
.formParam("Engine", "valkey")
.formParam("NumCacheClusters", "2")
.formParam(prefix + ".1", SECURITY_GROUP_IDS.getFirst())
.formParam(prefix + ".2", SECURITY_GROUP_IDS.getLast())
.when().post("/")
.then().statusCode(200);
assertMemberSecurityGroups(client, groupId, 2, SECURITY_GROUP_IDS);
} finally {
deleteGroup(client, groupId);
}
}
}
private ElastiCacheClient client() {
return ElastiCacheClient.builder().endpointOverride(endpoint).region(Region.US_EAST_1)
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
.build();
}
private static CreateReplicationGroupRequest.Builder createRequest(String groupId, boolean clusterEnabled) {
CreateReplicationGroupRequest.Builder request = CreateReplicationGroupRequest.builder()
.replicationGroupId(groupId).replicationGroupDescription("member security groups")
.engine("valkey").clusterMode(clusterEnabled ? "enabled" : "disabled");
if (clusterEnabled) {
request.numNodeGroups(2).replicasPerNodeGroup(1).automaticFailoverEnabled(true);
}
return request;
}
private static void assertMemberSecurityGroups(ElastiCacheClient client, String groupId,
int expectedMembers, List<String> expectedSecurityGroupIds) {
List<String> memberIds = client.describeReplicationGroups(request -> request.replicationGroupId(groupId))
.replicationGroups().getFirst().memberClusters();
assertEquals(expectedMembers, memberIds.size());
for (String memberId : memberIds) {
List<CacheCluster> clusters = client.describeCacheClusters(request -> request
.cacheClusterId(memberId).showCacheNodeInfo(true)).cacheClusters();
assertEquals(1, clusters.size(), memberId);
assertEquals(groupId, clusters.getFirst().replicationGroupId());
assertSecurityGroups(clusters.getFirst(), expectedSecurityGroupIds);
}
List<CacheCluster> listedMembers = client.describeCacheClusters().cacheClusters().stream()
.filter(cluster -> groupId.equals(cluster.replicationGroupId())).toList();
assertEquals(expectedMembers, listedMembers.size());
for (CacheCluster cluster : listedMembers) {
assertSecurityGroups(cluster, expectedSecurityGroupIds);
}
}
private static void assertSecurityGroups(CacheCluster cluster, List<String> expectedIds) {
assertEquals(expectedIds, cluster.securityGroups().stream()
.map(SecurityGroupMembership::securityGroupId).toList(), cluster.cacheClusterId());
for (SecurityGroupMembership membership : cluster.securityGroups()) {
assertEquals("active", membership.status(), cluster.cacheClusterId());
}
}
private static ElastiCacheContainerHandle containerHandle(String memberId) {
return new ElastiCacheContainerHandle("stub-" + memberId, memberId, "127.0.0.1", 16379);
}
private static String groupId() {
return "sg-members-" + UUID.randomUUID().toString().substring(0, 8);
}
private static void deleteGroup(ElastiCacheClient client, String groupId) {
try {
client.deleteReplicationGroup(request -> request.replicationGroupId(groupId));
} catch (ReplicationGroupNotFoundException expected) {
// A failed create may not have stored the group; there is then nothing to clean up.
}
}
}
@@ -273,7 +273,7 @@ class ElastiCacheServiceTest {
ElastiCacheService.CreateReplicationGroupRequest request = new ElastiCacheService.CreateReplicationGroupRequest(
"grp", "test", AuthMode.IAM, null, "us-east-1", "redis", null, null, null, null, null,
null, null, null, null, null, null, ReplicationGroupSettings.defaults(), Map.of(),
List.of("missing-group"));
List.of("missing-group"), null);
AwsException refused = assertThrows(AwsException.class, () -> service.createReplicationGroup(request));
@@ -307,7 +307,7 @@ class ElastiCacheServiceTest {
void anEmptyUserGroupAdmitsNobody() {
service.createReplicationGroup(new ElastiCacheService.CreateReplicationGroupRequest(
"grp", "test", AuthMode.IAM, null, "us-east-1", "valkey", null, null, null, null, null,
null, null, null, null, null, null, ReplicationGroupSettings.defaults(), Map.of(), null));
null, null, null, null, null, null, ReplicationGroupSettings.defaults(), Map.of(), null, null));
service.createUserGroup("empty-users", "valkey", List.of(), "us-east-1");
service.modifyReplicationGroup("grp", List.of("empty-users"), null);
@@ -396,7 +396,7 @@ class ElastiCacheServiceTest {
ElastiCacheService.CreateReplicationGroupRequest request = new ElastiCacheService.CreateReplicationGroupRequest(
"grp", "test", AuthMode.IAM, null, "us-east-1", "redis", null, null, null, null, null,
null, null, null, null, null, null, ReplicationGroupSettings.defaults(), Map.of(),
List.of("reserved"));
List.of("reserved"), null);
assertThrows(RuntimeException.class, () -> service.createReplicationGroup(request));
@@ -647,14 +647,14 @@ class ElastiCacheServiceTest {
return new ElastiCacheService.CreateReplicationGroupRequest(groupId, "test",
AuthMode.NO_AUTH, null, "us-east-1", "valkey", "8.2", "cache.t4g.micro",
"default.valkey8.cluster.on", null, null, numNodeGroups, replicasPerNodeGroup,
null, true, null, null, ReplicationGroupSettings.defaults(), Map.of(), null);
null, true, null, null, ReplicationGroupSettings.defaults(), Map.of(), null, null);
}
private static ElastiCacheService.CreateReplicationGroupRequest singleNodeRequest(
String groupId, Integer port) {
return new ElastiCacheService.CreateReplicationGroupRequest(groupId, "test",
AuthMode.NO_AUTH, null, "us-east-1", null, null, null, null, null, null,
null, null, null, null, null, port, ReplicationGroupSettings.defaults(), Map.of(), null);
null, null, null, null, null, port, ReplicationGroupSettings.defaults(), Map.of(), null, null);
}
// botocore models Port as an optional input on CreateReplicationGroup ("the port number on
@@ -744,7 +744,7 @@ class ElastiCacheServiceTest {
new ElastiCacheService.CreateReplicationGroupRequest("grp", "test",
AuthMode.NO_AUTH, null, "us-east-1", "valkey", "8.2", "cache.t4g.micro",
"default.valkey8.cluster.on", null, null, 2, 1,
null, true, null, 16390, ReplicationGroupSettings.defaults(), Map.of(), null));
null, true, null, 16390, ReplicationGroupSettings.defaults(), Map.of(), null, null));
assertEquals(16390, group.getConfigurationEndpoint().port());
assertEquals(16390, group.getClusterNodes().getFirst().getProxyPort());
@@ -1600,7 +1600,7 @@ class ElastiCacheServiceTest {
return new ElastiCacheService.CreateReplicationGroupRequest(groupId, "test",
AuthMode.NO_AUTH, null, "us-east-1", null, null, null,
parameterGroupName, null, null, null, null,
null, null, null, null, ReplicationGroupSettings.defaults(), Map.of(), null);
null, null, null, null, ReplicationGroupSettings.defaults(), Map.of(), null, null);
}
@Test
@@ -1752,7 +1752,7 @@ class ElastiCacheServiceTest {
svc.createReplicationGroup(new ElastiCacheService.CreateReplicationGroupRequest(
"grp", "test", AuthMode.IAM, null, "us-east-1", "redis", null, null, null, null, null,
null, null, null, null, null, null, ReplicationGroupSettings.defaults(), Map.of(),
List.of("shared-ug")));
List.of("shared-ug"), null));
});
accountA.start();
assertTrue(startedLatch.await(5, TimeUnit.SECONDS), "create never reached container start");
@@ -2450,7 +2450,7 @@ class ElastiCacheServiceTest {
service.createReplicationGroup(new ElastiCacheService.CreateReplicationGroupRequest(
"rg-cluster", "desc", AuthMode.NO_AUTH, null, "us-east-1", null, null, null,
null, "rg-used-group", null, null, null, null, null, null, null,
ReplicationGroupSettings.defaults(), Map.of(), null));
ReplicationGroupSettings.defaults(), Map.of(), null, null));
AwsException ex = assertThrows(AwsException.class, () -> service.deleteCacheSubnetGroup("rg-used-group"));
assertEquals("CacheSubnetGroupInUse", ex.getErrorCode());
@@ -0,0 +1,40 @@
package io.github.hectorvent.floci.services.elasticache;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.services.elasticache.model.ReplicationGroup;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class ReplicationGroupSecurityGroupsTest {
private final ObjectMapper mapper = new ObjectMapper();
@Test
void securityGroupsSurviveThePersistedJsonRoundTrip() throws Exception {
ReplicationGroup group = mapper.readValue("""
{"replicationGroupId":"restored","securityGroupIds":["sg-first","sg-second"]}
""", ReplicationGroup.class);
ReplicationGroup restored = mapper.readValue(mapper.writeValueAsString(group), ReplicationGroup.class);
assertEquals(mapper.readTree("[\"sg-first\",\"sg-second\"]"),
mapper.valueToTree(restored).get("securityGroupIds"));
}
@Test
void legacyRecordsWithoutSecurityGroupsKeepAnEmptyList() throws Exception {
ReplicationGroup group = mapper.readValue("{\"replicationGroupId\":\"legacy\"}", ReplicationGroup.class);
assertEquals(mapper.readTree("[]"), mapper.valueToTree(group).get("securityGroupIds"));
}
@Test
void explicitNullSecurityGroupsNormalizeToAnEmptyList() throws Exception {
ReplicationGroup group = mapper.readValue("""
{"replicationGroupId":"legacy-null","securityGroupIds":null}
""", ReplicationGroup.class);
assertEquals(mapper.readTree("[]"), mapper.valueToTree(group).get("securityGroupIds"));
}
}