Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

fix(elasticache): honor SecurityGroupIds on ModifyReplicationGroup (#6467)

Matheus Oliveiraf4cd3110e7aa
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElastiCacheTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElastiCacheTest.java
index 6497c25971..7982e4eafa 100644
--- a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElastiCacheTest.java
+++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElastiCacheTest.java
@@ -18,7 +18,10 @@ import software.amazon.awssdk.services.elasticache.model.CreateUserResponse;
import software.amazon.awssdk.services.elasticache.model.DeleteReplicationGroupRequest;
import software.amazon.awssdk.services.elasticache.model.DeleteUserGroupRequest;
import software.amazon.awssdk.services.elasticache.model.DeleteUserRequest;
+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.DescribeUserGroupsRequest;
import software.amazon.awssdk.services.elasticache.model.DescribeUserGroupsResponse;
import software.amazon.awssdk.services.elasticache.model.DescribeUsersRequest;
@@ -31,6 +34,7 @@ import software.amazon.awssdk.services.elasticache.model.ModifyReplicationGroupR
import software.amazon.awssdk.services.elasticache.model.ModifyUserRequest;
import software.amazon.awssdk.services.elasticache.model.ModifyUserResponse;
import software.amazon.awssdk.services.elasticache.model.ReplicationGroup;
+import software.amazon.awssdk.services.elasticache.model.SecurityGroupMembership;
import software.amazon.awssdk.services.elasticache.model.UserAlreadyExistsException;
import software.amazon.awssdk.services.elasticache.model.UserGroupNotFoundException;
import software.amazon.awssdk.services.elasticache.model.UserNotFoundException;
@@ -410,6 +414,32 @@ class ElastiCacheTest {
@Test
@Order(14)
+ void modifyReplicationGroupSecurityGroupsAreEchoedByMemberClusters() {
+ requireGroup();
+
+ elasticache.modifyReplicationGroup(ModifyReplicationGroupRequest.builder()
+ .replicationGroupId(groupId)
+ .securityGroupIds("sg-compat-1", "sg-compat-2")
+ .build());
+
+ DescribeReplicationGroupsResponse described = elasticache.describeReplicationGroups(
+ DescribeReplicationGroupsRequest.builder()
+ .replicationGroupId(groupId)
+ .build());
+ assertThat(described.replicationGroups().get(0).memberClusters()).hasSize(1);
+
+ DescribeCacheClustersResponse clusters = elasticache.describeCacheClusters(DescribeCacheClustersRequest.builder()
+ .cacheClusterId(described.replicationGroups().get(0).memberClusters().get(0))
+ .build());
+ assertThat(clusters.cacheClusters().get(0).securityGroups())
+ .extracting(SecurityGroupMembership::securityGroupId)
+ .containsExactly("sg-compat-1", "sg-compat-2");
+ assertThat(clusters.cacheClusters().get(0).securityGroups())
+ .allSatisfy(membership -> assertThat(membership.status()).isEqualTo("active"));
+ }
+
+ @Test
+ @Order(15)
void deleteReplicationGroupReleasesPortForReuse() {
requireGroup();
diff --git a/docs/services/elasticache.md b/docs/services/elasticache.md
index bca2c74a70..861c835f70 100644
--- a/docs/services/elasticache.md
+++ b/docs/services/elasticache.md
@@ -183,8 +183,9 @@ request may send them, and the describe will not report them back.
`SecurityGroupIds` supplied to `CreateReplicationGroup` are stored with the group and returned
on every member by `DescribeCacheClusters` as `SecurityGroups` memberships with `Status=active`.
This includes shards and replicas in cluster mode. Groups without stored IDs return no memberships.
-The IDs are control-plane
-metadata, not network rules applied to the backing cache containers.
+`ModifyReplicationGroup` accepts `SecurityGroupIds` and replaces the whole stored list, on the
+group and every member; a modify that names none leaves the memberships untouched. The IDs are
+control-plane metadata, not network rules applied to the backing cache containers.
`CreateReplicationGroup` provisions a real sharded Valkey cluster when the request asks for one:
`NumNodeGroups` greater than 1, a `default.*.cluster.on` parameter group, a custom parameter group
diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheQueryHandler.java
index eb85bce5db..c878df6a17 100644
--- a/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheQueryHandler.java
+++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheQueryHandler.java
@@ -250,7 +250,8 @@ public class ElastiCacheQueryHandler {
userGroupIdsToRemove.isEmpty() ? null : userGroupIdsToRemove,
Boolean.parseBoolean(params.getFirst("RemoveUserGroups")),
"DELETE".equals(params.getFirst("AuthTokenUpdateStrategy")),
- settings);
+ settings,
+ optionalSecurityGroupIds(params));
String result = replicationGroupXml(group);
return Response.ok(AwsQueryResponse.envelope("ModifyReplicationGroup", AwsNamespaces.EC, result)).build();
} catch (AwsException e) {
@@ -684,6 +685,15 @@ public class ElastiCacheQueryHandler {
return securityGroupIds;
}
+ /**
+ * A modify that names no SecurityGroupIds leaves the memberships untouched, so the absent
+ * list must read as null, never as the empty list a replace would mean.
+ */
+ private static List<String> optionalSecurityGroupIds(MultivaluedMap<String, String> params) {
+ List<String> securityGroupIds = parseSecurityGroupIds(params);
+ return securityGroupIds.isEmpty() ? null : securityGroupIds;
+ }
+
private Response handleCreateCacheParameterGroup(MultivaluedMap<String, String> params) {
try {
CacheParameterGroup group = service.createCacheParameterGroup(
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 1a7a7250cd..dc8560ebe6 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
@@ -1754,7 +1754,7 @@ public class ElastiCacheService implements ResourceProvider {
public ReplicationGroup modifyReplicationGroup(String groupId, List<String> userGroupIdsToAdd,
List<String> userGroupIdsToRemove,
ReplicationGroupSettings settings) {
- return modifyReplicationGroup(groupId, userGroupIdsToAdd, userGroupIdsToRemove, false, false, settings);
+ return modifyReplicationGroup(groupId, userGroupIdsToAdd, userGroupIdsToRemove, false, false, settings, null);
}
/**
@@ -1762,11 +1762,14 @@ public class ElastiCacheService implements ResourceProvider {
* {@code deleteAuthToken} is AuthTokenUpdateStrategy DELETE, which AWS allows only when the
* cache moves to user groups. A cache whose last user group goes has no access control, as
* on AWS, unless it kept an auth token by joining user groups without the DELETE.
+ * {@code securityGroupIds} replaces the whole stored list; a null leaves the memberships
+ * untouched, which is what a request that names none means.
*/
public ReplicationGroup modifyReplicationGroup(String groupId, List<String> userGroupIdsToAdd,
List<String> userGroupIdsToRemove,
boolean removeUserGroups, boolean deleteAuthToken,
- ReplicationGroupSettings settings) {
+ ReplicationGroupSettings settings,
+ List<String> securityGroupIds) {
settings.validate();
synchronized (lockFor("rg:" + groupId)) {
ReplicationGroup group = getReplicationGroup(groupId);
@@ -1836,6 +1839,9 @@ public class ElastiCacheService implements ResourceProvider {
if (deleteAuthToken) {
group.setAuthToken(null);
}
+ if (securityGroupIds != null) {
+ group.setSecurityGroupIds(securityGroupIds);
+ }
if (hadUserGroups && nextUserGroupIds.isEmpty()) {
group.setTransitEncryptionEnabled(group.isEncryptedInTransit());
group.setAuthMode(group.getAuthToken() != null ? AuthMode.PASSWORD : AuthMode.NO_AUTH);
diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheMemberSecurityGroupsIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheMemberSecurityGroupsIntegrationTest.java
index 54951c9a7d..4a2ac4f875 100644
--- a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheMemberSecurityGroupsIntegrationTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheMemberSecurityGroupsIntegrationTest.java
@@ -111,6 +111,69 @@ class ElastiCacheMemberSecurityGroupsIntegrationTest {
}
}
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void sdkModifyReplacesSecurityGroupsOnEveryMember(boolean clusterEnabled) {
+ String groupId = groupId();
+ try (ElastiCacheClient client = client()) {
+ try {
+ client.createReplicationGroup(createRequest(groupId, clusterEnabled)
+ .securityGroupIds(SECURITY_GROUP_IDS).build());
+
+ client.modifyReplicationGroup(request -> request.replicationGroupId(groupId)
+ .securityGroupIds(List.of("sg-modified")));
+
+ assertMemberSecurityGroups(client, groupId, clusterEnabled ? 4 : 1, List.of("sg-modified"));
+ } finally {
+ deleteGroup(client, groupId);
+ }
+ }
+ }
+
+ @ParameterizedTest
+ @ValueSource(booleans = {false, true})
+ void modifyThatNamesNoSecurityGroupsKeepsTheMemberships(boolean clusterEnabled) {
+ String groupId = groupId();
+ try (ElastiCacheClient client = client()) {
+ try {
+ client.createReplicationGroup(createRequest(groupId, clusterEnabled)
+ .securityGroupIds(SECURITY_GROUP_IDS).build());
+
+ client.modifyReplicationGroup(request -> request.replicationGroupId(groupId));
+
+ assertMemberSecurityGroups(client, groupId, clusterEnabled ? 4 : 1, SECURITY_GROUP_IDS);
+ } finally {
+ deleteGroup(client, groupId);
+ }
+ }
+ }
+
+ @ParameterizedTest
+ @ValueSource(strings = {"SecurityGroupIds.SecurityGroupId", "SecurityGroupIds.member"})
+ void queryModifyReplacesTheWholeListOnEveryMember(String prefix) {
+ String groupId = groupId();
+ try (ElastiCacheClient client = client()) {
+ try {
+ client.createReplicationGroup(createRequest(groupId, false)
+ .securityGroupIds(SECURITY_GROUP_IDS).build());
+
+ given()
+ .port(endpoint.getPort())
+ .header("Authorization", AUTH_HEADER)
+ .formParam("Action", "ModifyReplicationGroup")
+ .formParam("Version", "2015-02-02")
+ .formParam("ReplicationGroupId", groupId)
+ .formParam(prefix + ".1", "sg-replaced")
+ .when().post("/")
+ .then().statusCode(200);
+
+ assertMemberSecurityGroups(client, groupId, 1, List.of("sg-replaced"));
+ } finally {
+ deleteGroup(client, groupId);
+ }
+ }
+ }
+
private ElastiCacheClient client() {
return ElastiCacheClient.builder().endpointOverride(endpoint).region(Region.US_EAST_1)
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create("test", "test")))
diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheQueryHandlerTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheQueryHandlerTest.java
index 9235f2d266..74beef973c 100644
--- a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheQueryHandlerTest.java
+++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheQueryHandlerTest.java
@@ -455,7 +455,7 @@ class ElastiCacheQueryHandlerTest {
@Test
void modifyReplicationGroup_passesSnapshotSettingsOnly() {
- when(service.modifyReplicationGroup(eq("g1"), isNull(), isNull(), eq(false), eq(false), any()))
+ when(service.modifyReplicationGroup(eq("g1"), isNull(), isNull(), eq(false), eq(false), any(), isNull()))
.thenReturn(group("g1"));
MultivaluedMap<String, String> p = params();
p.add("ReplicationGroupId", "g1");
@@ -466,12 +466,12 @@ class ElastiCacheQueryHandlerTest {
assertEquals(200, handler.handle("ModifyReplicationGroup", p, "us-east-1").getStatus());
verify(service).modifyReplicationGroup("g1", null, null, false, false,
- new ReplicationGroupSettings(null, null, 3, "01:00-02:00"));
+ new ReplicationGroupSettings(null, null, 3, "01:00-02:00"), null);
}
@Test
void modifyReplicationGroup_passesRemoveUserGroupsAndAuthTokenDelete() {
- when(service.modifyReplicationGroup(eq("g1"), any(), isNull(), eq(true), eq(true), any()))
+ when(service.modifyReplicationGroup(eq("g1"), any(), isNull(), eq(true), eq(true), any(), isNull()))
.thenReturn(group("g1"));
MultivaluedMap<String, String> p = params();
p.add("ReplicationGroupId", "g1");
@@ -481,7 +481,21 @@ class ElastiCacheQueryHandlerTest {
assertEquals(200, handler.handle("ModifyReplicationGroup", p, "us-east-1").getStatus());
verify(service).modifyReplicationGroup("g1", List.of("new-users"), null, true, true,
- new ReplicationGroupSettings(null, null, null, null));
+ new ReplicationGroupSettings(null, null, null, null), null);
+ }
+
+ @Test
+ void modifyReplicationGroup_passesNamedSecurityGroupIdsAndUnchangedOtherwise() {
+ when(service.modifyReplicationGroup(eq("g1"), isNull(), isNull(), eq(false), eq(false), any(), any()))
+ .thenReturn(group("g1"));
+ MultivaluedMap<String, String> p = params();
+ p.add("ReplicationGroupId", "g1");
+ p.add("SecurityGroupIds.SecurityGroupId.1", "sg-1");
+ p.add("SecurityGroupIds.SecurityGroupId.2", "sg-2");
+
+ assertEquals(200, handler.handle("ModifyReplicationGroup", p, "us-east-1").getStatus());
+ verify(service).modifyReplicationGroup("g1", null, null, false, false,
+ new ReplicationGroupSettings(null, null, null, null), List.of("sg-1", "sg-2"));
}
@Test
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 d541249bb3..5d83bacf3e 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
@@ -502,12 +502,12 @@ class ElastiCacheServiceTest {
service.createUserGroup("redis-users", "redis", List.of("default-user-id"), "us-east-1");
AwsException refused = assertThrows(AwsException.class, () -> service.modifyReplicationGroup(
- "grp", null, null, false, true, ReplicationGroupSettings.unchanged()));
+ "grp", null, null, false, true, ReplicationGroupSettings.unchanged(), null));
assertEquals("InvalidParameterCombination", refused.getErrorCode());
assertEquals("group-token", service.getReplicationGroup("grp").getAuthToken());
service.modifyReplicationGroup("grp", List.of("redis-users"), null, false, true,
- ReplicationGroupSettings.unchanged());
+ ReplicationGroupSettings.unchanged(), null);
assertNull(service.getReplicationGroup("grp").getAuthToken());
service.modifyReplicationGroup("grp", null, List.of("redis-users"));
@@ -528,7 +528,7 @@ class ElastiCacheServiceTest {
service.createUserGroup("second-users", "redis", List.of("default-user-id"), "us-east-1");
service.modifyReplicationGroup("grp", List.of("first-users", "second-users"), null);
- service.modifyReplicationGroup("grp", null, null, true, false, ReplicationGroupSettings.unchanged());
+ service.modifyReplicationGroup("grp", null, null, true, false, ReplicationGroupSettings.unchanged(), null);
ReplicationGroup group = service.getReplicationGroup("grp");
assertTrue(group.getUserGroupIds().isEmpty());
@@ -537,6 +537,23 @@ class ElastiCacheServiceTest {
assertFalse(service.hasMembers("grp"));
}
+ @Test
+ void modifyReplicationGroupReplacesSecurityGroupsOnlyWhenNamed() {
+ service.createReplicationGroup("grp", "test", AuthMode.NO_AUTH, null, "us-east-1");
+
+ service.modifyReplicationGroup("grp", null, null, false, false,
+ ReplicationGroupSettings.unchanged(), List.of("sg-1", "sg-2"));
+ assertEquals(List.of("sg-1", "sg-2"), service.getReplicationGroup("grp").getSecurityGroupIds());
+
+ service.modifyReplicationGroup("grp", null, null, false, false,
+ ReplicationGroupSettings.unchanged(), null);
+ assertEquals(List.of("sg-1", "sg-2"), service.getReplicationGroup("grp").getSecurityGroupIds());
+
+ service.modifyReplicationGroup("grp", null, null, false, false,
+ ReplicationGroupSettings.unchanged(), List.of("sg-3"));
+ assertEquals(List.of("sg-3"), service.getReplicationGroup("grp").getSecurityGroupIds());
+ }
+
@Test
void aTokenKeptThroughUserGroupsAppliesAgainOnceTheyAreRemoved() {
service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, "group-token", "us-east-1");
@@ -1353,7 +1370,7 @@ class ElastiCacheServiceTest {
"on ~* +@all", null);
svc.createUserGroup("redis-users", "redis", List.of("default-user-id"), "us-east-1");
svc.modifyReplicationGroup("grp", List.of("redis-users"), null, false, true,
- ReplicationGroupSettings.unchanged());
+ ReplicationGroupSettings.unchanged(), null);
svc.modifyReplicationGroup("grp", null, List.of("redis-users"));
ArgumentCaptor<ElastiCacheAuthProxy.PasswordValidator> validator =
ArgumentCaptor.forClass(ElastiCacheAuthProxy.PasswordValidator.class);