mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(elasticache): preserve and mutate resource tags (#6327)
* fix(elasticache): preserve and mutate resource tags * fix(elasticache): complete resource tag mutations * docs(elasticache): sync tag actions * docs(services): update ElastiCache action count * fix(elasticache): use AWS InvalidARN tag error code * fix(elasticache): align resource tag mutations with lifecycle and AWS errors
This commit is contained in:
1 parent
a815434e21
commit
d4f454a1cf
14 files changed
+684
-14
No files matched your search
+49
-11
@@ -191,6 +191,44 @@ class ElastiCacheIntegrationTest {
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void userTagsCanBeAddedListedAndRemoved() {
|
||||
given()
|
||||
.formParam("Action", "AddTagsToResource")
|
||||
.formParam("ResourceName", "arn:aws:elasticache:us-east-1:000000000000:user:" + USER_ID)
|
||||
.formParam("Tags.Tag.1.Key", "environment")
|
||||
.formParam("Tags.Tag.1.Value", "test")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("AddTagsToResourceResponse.AddTagsToResourceResult.TagList.Tag.Key", equalTo("environment"))
|
||||
.body("AddTagsToResourceResponse.AddTagsToResourceResult.TagList.Tag.Value", equalTo("test"));
|
||||
|
||||
given()
|
||||
.formParam("Action", "ListTagsForResource")
|
||||
.formParam("ResourceName", "arn:aws:elasticache:us-east-1:000000000000:user:" + USER_ID)
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("ListTagsForResourceResponse.ListTagsForResourceResult.TagList.Tag.Key", equalTo("environment"));
|
||||
|
||||
given()
|
||||
.formParam("Action", "RemoveTagsFromResource")
|
||||
.formParam("ResourceName", "arn:aws:elasticache:us-east-1:000000000000:user:" + USER_ID)
|
||||
.formParam("TagKeys.member.1", "environment")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("RemoveTagsFromResourceResponse.RemoveTagsFromResourceResult.TagList", notNullValue());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void unassociatedUserIsRejected() throws Exception {
|
||||
// Before associating the user with the group, auth should fail
|
||||
String reply = sendCommand(firstProxyPort, respArray("AUTH", USER_NAME, INITIAL_PASSWORD));
|
||||
@@ -198,7 +236,7 @@ class ElastiCacheIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
@Order(10)
|
||||
void associateUserWithGroup() {
|
||||
given()
|
||||
.formParam("Action", "ModifyReplicationGroup")
|
||||
@@ -213,7 +251,7 @@ class ElastiCacheIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
@Order(11)
|
||||
void describeUsersIncludesCreatedUser() {
|
||||
given()
|
||||
.formParam("Action", "DescribeUsers")
|
||||
@@ -227,7 +265,7 @@ class ElastiCacheIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(11)
|
||||
@Order(12)
|
||||
void crossGroupAuthIsRejected() throws Exception {
|
||||
// Ensure user exists if this test is run in isolation
|
||||
try {
|
||||
@@ -284,7 +322,7 @@ class ElastiCacheIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(12)
|
||||
@Order(13)
|
||||
void userPasswordAuthWorks() throws Exception {
|
||||
try (Socket socket = openSocket(firstProxyPort)) {
|
||||
write(socket, respArray("AUTH", USER_NAME, INITIAL_PASSWORD));
|
||||
@@ -296,7 +334,7 @@ class ElastiCacheIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(13)
|
||||
@Order(14)
|
||||
void modifyUserPasswordInvalidatesOldPasswordAndAcceptsNewPassword() throws Exception {
|
||||
given()
|
||||
.formParam("Action", "ModifyUser")
|
||||
@@ -323,7 +361,7 @@ class ElastiCacheIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(14)
|
||||
@Order(15)
|
||||
void deleteUserRemovesUserFromDescribeUsers() {
|
||||
given()
|
||||
.formParam("Action", "DeleteUser")
|
||||
@@ -346,7 +384,7 @@ class ElastiCacheIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(15)
|
||||
@Order(16)
|
||||
void deleteReplicationGroupReleasesProxyPortForReuse() {
|
||||
given()
|
||||
.formParam("Action", "DeleteReplicationGroup")
|
||||
@@ -389,7 +427,7 @@ class ElastiCacheIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(16)
|
||||
@Order(17)
|
||||
void transitEncryptionReplicationGroupAuthenticatesPasswordAndNoAuthUsers() throws Exception {
|
||||
String transitGroupId = "it-ec-transit-rbac";
|
||||
String passUserId = "it-transit-pass-user";
|
||||
@@ -499,7 +537,7 @@ class ElastiCacheIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(17)
|
||||
@Order(18)
|
||||
void replicationGroupWithNoAuthDefaultMemberAdmitsUnauthenticatedClients() throws Exception {
|
||||
String groupId = "it-ec-def-noauth";
|
||||
String defUserId = "it-def-noauth-uid";
|
||||
@@ -616,7 +654,7 @@ class ElastiCacheIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(18)
|
||||
@Order(19)
|
||||
void replicationGroupWithMembersWithoutDefaultMemberRequiresAuth() throws Exception {
|
||||
String groupId = "it-ec-nodef-members";
|
||||
String passUserId = "it-nodef-pass-uid";
|
||||
@@ -695,7 +733,7 @@ class ElastiCacheIntegrationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(19)
|
||||
@Order(20)
|
||||
void replicationGroupWithDisabledDefaultUserRequiresAuth() throws Exception {
|
||||
String groupId = "it-ec-disabled-def";
|
||||
String defUserId = "it-disabled-def-uid";
|
||||
|
||||
+42
@@ -73,6 +73,48 @@ class ElastiCacheQueryHandlerTest {
|
||||
assertFalse(body.contains("<Marker>"), "Empty list must omit Marker");
|
||||
}
|
||||
|
||||
@Test
|
||||
void addTagsToResource_supportsUserResource() {
|
||||
MultivaluedMap<String, String> p = params();
|
||||
p.add("ResourceName", "arn:aws:elasticache:us-east-1:000000000000:user:sub1");
|
||||
p.add("Tags.Tag.1.Key", "env");
|
||||
p.add("Tags.Tag.1.Value", "test");
|
||||
when(service.addTagsToResource(anyString(), anyMap(), anyString()))
|
||||
.thenReturn(Map.of("env", "test"));
|
||||
|
||||
Response response = handler.handle("AddTagsToResource", p, "us-east-1");
|
||||
|
||||
assertEquals(200, response.getStatus(), (String) response.getEntity());
|
||||
assertTrue(((String) response.getEntity()).contains("<Key>env</Key><Value>test</Value>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void removeTagsFromResource_supportsUserResource() {
|
||||
MultivaluedMap<String, String> p = params();
|
||||
p.add("ResourceName", "arn:aws:elasticache:us-east-1:000000000000:user:sub1");
|
||||
p.add("TagKeys.member.1", "env");
|
||||
when(service.removeTagsFromResource(anyString(), anyList(), anyString()))
|
||||
.thenReturn(Map.of());
|
||||
|
||||
Response response = handler.handle("RemoveTagsFromResource", p, "us-east-1");
|
||||
|
||||
assertEquals(200, response.getStatus(), (String) response.getEntity());
|
||||
assertTrue(((String) response.getEntity()).contains("<TagList></TagList>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void addTagsToResource_rejectsMissingTagKeyBeforeCallingService() {
|
||||
MultivaluedMap<String, String> p = params();
|
||||
p.add("ResourceName", "arn:aws:elasticache:us-east-1:000000000000:user:sub1");
|
||||
p.add("Tags.Tag.1.Value", "test");
|
||||
|
||||
Response response = handler.handle("AddTagsToResource", p, "us-east-1");
|
||||
|
||||
assertEquals(400, response.getStatus());
|
||||
assertTrue(((String) response.getEntity()).contains("InvalidParameterValue"));
|
||||
verifyNoInteractions(service);
|
||||
}
|
||||
|
||||
@Test
|
||||
void describeReplicationGroups_reportsClusterModeTopology() {
|
||||
ReplicationGroup group = new ReplicationGroup("grp", "d", ReplicationGroupStatus.AVAILABLE,
|
||||
|
||||
+18
@@ -468,4 +468,22 @@ class ElastiCacheServerlessServiceTest {
|
||||
error("TagQuotaPerResourceExceeded", () -> service.replaceTags("tag-cache", tooMany));
|
||||
assertEquals(Map.of("env", "dev"), service.getServerlessCache("tag-cache").getTags());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagMutationsUseAwsInvalidArnWireCode() {
|
||||
error("InvalidARN", () -> service.addTagsToResource("not-an-arn", Map.of("env", "test"), "us-east-1"));
|
||||
error("InvalidARN", () -> service.removeTagsFromResource("not-an-arn", List.of("env"), "us-east-1"));
|
||||
String wrongTypeAndRegion = "arn:aws:elasticache:eu-west-1:000000000000:cluster:test";
|
||||
error("InvalidARN", () -> service.addTagsToResource(wrongTypeAndRegion, Map.of("env", "test"), "us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagMutationsKeepUsingTheServerlessCacheArn() {
|
||||
ServerlessCache cache = service.createServerlessCache(create("arn-tags-cache", "valkey", null));
|
||||
|
||||
assertEquals(Map.of("team", "local", "env", "test"),
|
||||
service.addTagsToResource(cache.getArn(), Map.of("env", "test"), "us-east-1"));
|
||||
assertEquals(Map.of("team", "local"),
|
||||
service.removeTagsFromResource(cache.getArn(), List.of("env"), "us-east-1"));
|
||||
}
|
||||
}
|
||||
+141
@@ -17,6 +17,7 @@ import io.github.hectorvent.floci.services.elasticache.container.ValkeyClusterFo
|
||||
import io.github.hectorvent.floci.services.elasticache.model.AuthMode;
|
||||
import io.github.hectorvent.floci.services.elasticache.model.CacheCluster;
|
||||
import io.github.hectorvent.floci.services.elasticache.model.CacheClusterStatus;
|
||||
import io.github.hectorvent.floci.services.elasticache.model.CacheParameterGroup;
|
||||
import io.github.hectorvent.floci.services.elasticache.model.ClusterNode;
|
||||
import io.github.hectorvent.floci.services.elasticache.model.ElastiCacheUser;
|
||||
import io.github.hectorvent.floci.services.elasticache.model.ElastiCacheUserGroup;
|
||||
@@ -33,6 +34,7 @@ import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -41,7 +43,11 @@ import java.util.Set;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
@@ -150,6 +156,141 @@ class ElastiCacheServiceTest {
|
||||
assertEquals(400, ex.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void userAndUserGroupTagsPersistAndCanBeMutatedByArn() {
|
||||
ElastiCacheUser defaultUser = service.createUser("default-user", "default", AuthMode.PASSWORD,
|
||||
List.of("password"), "on ~* +@all", "redis");
|
||||
ElastiCacheUser taggedUser = service.createUser("tagged-user", "tagged", AuthMode.PASSWORD,
|
||||
List.of("password"), "on ~* +@all", "redis", Map.of("owner", "platform"));
|
||||
assertEquals(Map.of("owner", "platform"), taggedUser.getTags());
|
||||
|
||||
ElastiCacheUserGroup taggedGroup = service.createUserGroup("tagged-group", "redis",
|
||||
List.of(defaultUser.getUserId()), "us-east-1", Map.of("team", "cache"));
|
||||
assertEquals(Map.of("team", "cache"), taggedGroup.getTags());
|
||||
|
||||
service.addTagsToResource(taggedUser.getArn(), Map.of("environment", "test"), "us-east-1");
|
||||
assertEquals(Map.of("owner", "platform", "environment", "test"),
|
||||
service.getUser("tagged-user").getTags());
|
||||
service.removeTagsFromResource(taggedUser.getArn(), List.of("owner"), "us-east-1");
|
||||
assertEquals(Map.of("environment", "test"), service.getUser("tagged-user").getTags());
|
||||
|
||||
service.addTagsToResource(taggedGroup.getArn(), Map.of("environment", "test"), "us-east-1");
|
||||
assertEquals(Map.of("team", "cache", "environment", "test"),
|
||||
service.getUserGroup("tagged-group").getTags());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagResourceRejectsSameIdResourceFromAnotherRegion() {
|
||||
ElastiCacheUser user = service.createUser("regional-user", "regional", AuthMode.PASSWORD,
|
||||
List.of("password"), "on ~* +@all", "redis");
|
||||
String foreignRegionArn = user.getArn().replace(":us-east-1:", ":eu-west-1:");
|
||||
|
||||
AwsException error = assertThrows(AwsException.class,
|
||||
() -> service.addTagsToResource(foreignRegionArn, Map.of("env", "test"), "eu-west-1"));
|
||||
|
||||
assertEquals("UserNotFound", error.getErrorCode());
|
||||
assertTrue(service.getUser("regional-user").getTags().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagMutationsUseAwsInvalidArnErrorCode() {
|
||||
AwsException addError = assertThrows(AwsException.class,
|
||||
() -> service.addTagsToResource("not-an-arn", Map.of("env", "test"), "us-east-1"));
|
||||
AwsException removeError = assertThrows(AwsException.class,
|
||||
() -> service.removeTagsFromResource("not-an-arn", List.of("env"), "us-east-1"));
|
||||
|
||||
assertEquals("InvalidARN", addError.getErrorCode());
|
||||
assertEquals("InvalidARN", removeError.getErrorCode());
|
||||
assertEquals(400, addError.getHttpStatus());
|
||||
assertEquals(400, removeError.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagMutationsReportMissingSubnetGroupWithAwsFaultCode() {
|
||||
String arn = "arn:aws:elasticache:us-east-1:000000000000:subnetgroup:missing-tag-subnet";
|
||||
|
||||
AwsException addError = assertThrows(AwsException.class,
|
||||
() -> service.addTagsToResource(arn, Map.of("env", "test"), "us-east-1"));
|
||||
AwsException removeError = assertThrows(AwsException.class,
|
||||
() -> service.removeTagsFromResource(arn, List.of("env"), "us-east-1"));
|
||||
|
||||
assertEquals("CacheSubnetGroupNotFoundFault", addError.getErrorCode());
|
||||
assertEquals("CacheSubnetGroupNotFoundFault", removeError.getErrorCode());
|
||||
assertEquals(400, addError.getHttpStatus());
|
||||
assertEquals(400, removeError.getHttpStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagWritesWaitForParameterGroupDeletion() throws Exception {
|
||||
String name = "tag-delete-race-pg";
|
||||
String arn = "arn:aws:elasticache:us-east-1:000000000000:parametergroup:" + name;
|
||||
service.createCacheParameterGroup(name, "redis7", "tagged", Map.of());
|
||||
|
||||
Field locksField = ElastiCacheService.class.getDeclaredField("parameterGroupLocks");
|
||||
locksField.setAccessible(true);
|
||||
Object monitor = ((ConcurrentHashMap<?, ?>) locksField.get(service)).get(name);
|
||||
ExecutorService pool = Executors.newSingleThreadExecutor();
|
||||
CountDownLatch started = new CountDownLatch(1);
|
||||
try {
|
||||
Future<String> tag = pool.submit(() -> {
|
||||
started.countDown();
|
||||
try {
|
||||
service.addTagsToResource(arn, Map.of("env", "test"), "us-east-1");
|
||||
return "tagged";
|
||||
} catch (AwsException error) {
|
||||
return error.getErrorCode();
|
||||
}
|
||||
});
|
||||
String completedBeforeDelete = null;
|
||||
synchronized (monitor) {
|
||||
assertTrue(started.await(5, TimeUnit.SECONDS));
|
||||
try {
|
||||
completedBeforeDelete = tag.get(1, TimeUnit.SECONDS);
|
||||
} catch (TimeoutException expected) {
|
||||
// A tag writer using the lifecycle lock remains blocked until deletion exits.
|
||||
}
|
||||
service.deleteCacheParameterGroup(name);
|
||||
}
|
||||
assertNull(completedBeforeDelete, "tagging bypassed the parameter-group lifecycle lock");
|
||||
assertEquals("CacheParameterGroupNotFound", tag.get(5, TimeUnit.SECONDS));
|
||||
assertTrue(service.findParameterGroup(name).isEmpty());
|
||||
} finally {
|
||||
pool.shutdownNow();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagResourceDerivesPartitionFromRequestedRegion() {
|
||||
ElastiCacheUserGroup group = service.createUserGroup("gov-group", "valkey", List.of(),
|
||||
"us-gov-west-1");
|
||||
|
||||
service.addTagsToResource(group.getArn(), Map.of("env", "gov"), "us-gov-west-1");
|
||||
|
||||
assertEquals(Map.of("env", "gov"), service.getUserGroup("gov-group").getTags());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagMutationsSupportCacheClustersAndParameterGroups() {
|
||||
CacheCluster cluster = service.createCacheCluster(cacheClusterRequest("tag-cluster", "redis", 1));
|
||||
CacheParameterGroup parameterGroup = service.createCacheParameterGroup(
|
||||
"tag-pg", "redis7", "tagged parameter group", Map.of("owner", "platform"));
|
||||
|
||||
service.addTagsToResource(cluster.getArn(), Map.of("environment", "test"), "us-east-1");
|
||||
service.removeTagsFromResource(cluster.getArn(), List.of("environment"), "us-east-1");
|
||||
assertTrue(service.findCacheClusters("tag-cluster").getFirst().getTags().isEmpty());
|
||||
|
||||
service.addTagsToResource("arn:aws:elasticache:us-east-1:000000000000:parametergroup:tag-pg",
|
||||
Map.of("environment", "test"), "us-east-1");
|
||||
assertEquals(Map.of("owner", "platform", "environment", "test"),
|
||||
service.findParameterGroup(parameterGroup.getName()).orElseThrow().getTags());
|
||||
|
||||
AwsException wrongRegion = assertThrows(AwsException.class, () -> service.addTagsToResource(
|
||||
"arn:aws:elasticache:eu-west-1:000000000000:parametergroup:tag-pg",
|
||||
Map.of("wrong", "region"), "eu-west-1"));
|
||||
assertEquals("CacheParameterGroupNotFound", wrongRegion.getErrorCode());
|
||||
assertFalse(service.findParameterGroup("tag-pg").orElseThrow().getTags().containsKey("wrong"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void singleArgAuthMatchesDefaultUserOnly() {
|
||||
service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, null, "us-east-1");
|
||||
|
||||
+23
@@ -257,4 +257,27 @@ class ElastiCacheSubnetGroupIntegrationTest {
|
||||
.statusCode(400)
|
||||
.body(containsString("Cache Subnet Group " + GROUP + " does not exist"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void missingSubnetGroupTagMutationsUseTheAwsFaultCode() {
|
||||
String arn = "arn:aws:elasticache:us-east-1:000000000000:subnetgroup:missing-tag-sng";
|
||||
|
||||
elasticache("AddTagsToResource")
|
||||
.formParam("ResourceName", arn)
|
||||
.formParam("Tags.Tag.1.Key", "env")
|
||||
.formParam("Tags.Tag.1.Value", "test")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("CacheSubnetGroupNotFoundFault"));
|
||||
|
||||
elasticache("RemoveTagsFromResource")
|
||||
.formParam("ResourceName", arn)
|
||||
.formParam("TagKeys.member.1", "env")
|
||||
.when().post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("CacheSubnetGroupNotFoundFault"));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user