feat(cloudformation): provision ElastiCache users and user groups (#5422)

* feat(cloudformation): provision ElastiCache users and user groups

Add AWS::ElastiCache::User and AWS::ElastiCache::UserGroup provisioners with authentication mode handling, create-only UserName, membership deltas, engine changes and replacement on rename.

* fix(cloudformation): address review findings for ElastiCache users and user groups

- Require Engine property when provisioning AWS::ElastiCache::User

- Reject invalid and conflicting AuthenticationMode combinations

- Add setUserGroupMembers to atomically update group membership under lock

- Require UserIds property when provisioning AWS::ElastiCache::UserGroup

- Snapshot and restore User and UserGroup on stack update rollback

- Tighten replaced subnet group deletion assertion in integration test

* refactor(elasticache): share user authentication and user group update logic

Move the AuthenticationMode / Passwords / NoPasswordRequired decision into
UserAuthentication so the query handler and the CloudFormation provisioner
no longer duplicate it. Make modifyUserGroup and setUserGroupMembers share
one locked update path, and drop the concurrency test that passed without
the lock.
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-10-12 14:28:27 -04:00
1 parent 669548277f
commit 86b32584f3
11 files changed
+693 -72

No files matched your search

@@ -14,6 +14,8 @@ 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.CacheSubnetGroupNotFoundException;
import software.amazon.awssdk.services.elasticache.model.ElastiCacheException;
import software.amazon.awssdk.services.elasticache.model.UserGroupNotFoundException;
import java.io.IOException;
import java.net.URI;
@@ -24,6 +26,7 @@ import static org.hamcrest.Matchers.containsString;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Provisions the ElastiCache resource types through CloudFormation and checks them against the
@@ -46,10 +49,25 @@ class ElastiCacheCfnIntegrationTest {
"Properties": {"VpcId": {"Ref": "Vpc"}, "CidrBlock": "10.71.0.0/24"}},
"SubnetGroup": {"Type": "AWS::ElastiCache::SubnetGroup",
"Properties": {"CacheSubnetGroupName": "cfn-it-sng", "Description": "example",
"SubnetIds": [{"Ref": "Subnet"}]}}
"SubnetIds": [{"Ref": "Subnet"}]}},
"DefaultUser": {"Type": "AWS::ElastiCache::User",
"Properties": {"UserId": "cfn-it-default", "UserName": "default", "Engine": "redis",
"AccessString": "off -@all", "NoPasswordRequired": true}},
"AppUser": {"Type": "AWS::ElastiCache::User",
"Properties": {"UserId": "cfn-it-app", "UserName": "cfn-it-app", "Engine": "redis",
"AccessString": "on ~* +@all",
"AuthenticationMode": {"Type": "password",
"Passwords": ["example-password-0123456789"]}}},
"UserGroup": {"Type": "AWS::ElastiCache::UserGroup",
"Properties": {"UserGroupId": "cfn-it-users", "Engine": "redis",
"UserIds": [{"Ref": "DefaultUser"}, {"Ref": "AppUser"}]}}
},
"Outputs": {
"SubnetGroupRef": {"Value": {"Ref": "SubnetGroup"}}
"SubnetGroupRef": {"Value": {"Ref": "SubnetGroup"}},
"UserRef": {"Value": {"Ref": "AppUser"}},
"UserArn": {"Value": {"Fn::GetAtt": ["AppUser", "Arn"]}},
"GroupRef": {"Value": {"Ref": "UserGroup"}},
"GroupArn": {"Value": {"Fn::GetAtt": ["UserGroup", "Arn"]}}
}
}
""";
@@ -83,7 +101,18 @@ class ElastiCacheCfnIntegrationTest {
"Properties": {"VpcId": {"Ref": "Vpc"}, "CidrBlock": "10.73.0.0/24"}},
"SubnetGroup": {"Type": "AWS::ElastiCache::SubnetGroup",
"Properties": {"CacheSubnetGroupName": "@@SNG@@", "Description": "example",
"SubnetIds": [{"Ref": "Subnet"}]}}
"SubnetIds": [{"Ref": "Subnet"}]}},
"DefaultUser": {"Type": "AWS::ElastiCache::User",
"Properties": {"UserId": "cfn-it-upd-default", "UserName": "default", "Engine": "redis",
"AccessString": "off -@all", "NoPasswordRequired": true}},
"AppUser": {"Type": "AWS::ElastiCache::User",
"Properties": {"UserId": "cfn-it-upd-app", "UserName": "cfn-it-upd-app", "Engine": "redis",
"AccessString": "on ~* +@all",
"AuthenticationMode": {"Type": "password",
"Passwords": ["example-password-0123456789"]}}},
"UserGroup": {"Type": "AWS::ElastiCache::UserGroup",
"Properties": {"UserGroupId": "cfn-it-upd-users", "Engine": "redis",
"UserIds": @@MEMBERS@@}}
},
"Outputs": {
"SubnetGroupRef": {"Value": {"Ref": "SubnetGroup"}}
@@ -106,35 +135,50 @@ class ElastiCacheCfnIntegrationTest {
}
@Test
void subnetGroupIsCreatedAndRemovedWithTheStack() {
void subnetGroupUsersAndUserGroupAreCreatedAndRemovedWithTheStack() {
cloudFormation(METADATA_STACK, "CreateStack", METADATA_TEMPLATE);
String stacks = describeStacks(METADATA_STACK, "CREATE_COMPLETE");
assertEquals("cfn-it-sng", outputValue(stacks, "SubnetGroupRef"));
assertEquals("cfn-it-app", outputValue(stacks, "UserRef"));
assertEquals("cfn-it-users", outputValue(stacks, "GroupRef"));
assertTrue(outputValue(stacks, "UserArn").endsWith(":user:cfn-it-app"));
assertTrue(outputValue(stacks, "GroupArn").endsWith(":usergroup:cfn-it-users"));
try (ElastiCacheClient client = client()) {
assertEquals(1, client.describeCacheSubnetGroups(r -> r.cacheSubnetGroupName("cfn-it-sng"))
.cacheSubnetGroups().size());
assertEquals(2, client.describeUserGroups(r -> r.userGroupId("cfn-it-users"))
.userGroups().getFirst().userIds().size());
cloudFormation(METADATA_STACK, "DeleteStack", null);
CfnStackWaits.awaitStackDeleted(METADATA_STACK);
assertThrows(UserGroupNotFoundException.class,
() -> client.describeUserGroups(r -> r.userGroupId("cfn-it-users")));
assertThrows(ElastiCacheException.class, () -> client.describeUsers(r -> r.userId("cfn-it-app")));
assertThrows(CacheSubnetGroupNotFoundException.class,
() -> client.describeCacheSubnetGroups(r -> r.cacheSubnetGroupName("cfn-it-sng")));
}
}
@Test
void renamedSubnetGroupIsReplaced() {
void updateChangesGroupMembershipInPlaceAndRenamedSubnetGroupIsReplaced() {
String stack = "elasticache-cfn-update-it";
cloudFormation(stack, "CreateStack", UPDATE_TEMPLATE.replace("@@SNG@@", "cfn-it-upd-sng-a"));
String create = UPDATE_TEMPLATE.replace("@@SNG@@", "cfn-it-upd-sng-a").replace("@@MEMBERS@@",
"[{\"Ref\": \"DefaultUser\"}]");
cloudFormation(stack, "CreateStack", create);
describeStacks(stack, "CREATE_COMPLETE");
cloudFormation(stack, "UpdateStack", UPDATE_TEMPLATE.replace("@@SNG@@", "cfn-it-upd-sng-b"));
String update = UPDATE_TEMPLATE.replace("@@SNG@@", "cfn-it-upd-sng-b").replace("@@MEMBERS@@",
"[{\"Ref\": \"DefaultUser\"}, {\"Ref\": \"AppUser\"}]");
cloudFormation(stack, "UpdateStack", update);
String stacks = describeStacks(stack, "UPDATE_COMPLETE");
assertEquals("cfn-it-upd-sng-b", outputValue(stacks, "SubnetGroupRef"));
try (ElastiCacheClient client = client()) {
assertEquals(2, client.describeUserGroups(r -> r.userGroupId("cfn-it-upd-users"))
.userGroups().getFirst().userIds().size());
assertThrows(CacheSubnetGroupNotFoundException.class,
() -> client.describeCacheSubnetGroups(r -> r.cacheSubnetGroupName("cfn-it-upd-sng-a")));
assertEquals(1, client.describeCacheSubnetGroups(r -> r.cacheSubnetGroupName("cfn-it-upd-sng-b"))
@@ -12,6 +12,8 @@ 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.CacheSubnetGroup;
import io.github.hectorvent.floci.services.elasticache.model.ElastiCacheUser;
import io.github.hectorvent.floci.services.elasticache.model.ElastiCacheUserGroup;
import io.github.hectorvent.floci.services.elasticache.model.Endpoint;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
@@ -19,6 +21,7 @@ import org.mockito.ArgumentCaptor;
import java.time.Instant;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
@@ -253,6 +256,210 @@ class ElastiCacheCfnProvisionerTest {
assertEquals("my-sng", r.getPhysicalId());
}
@Test
void userWithPasswordAuthenticationModeSetsArnAndStatus() throws Exception {
ElastiCacheUser user = new ElastiCacheUser("app", "app", AuthMode.PASSWORD, List.of("pw-0123456789abcd"),
"on ~* +@all", "redis", "active", Instant.now());
user.setArn("arn:aws:elasticache:us-east-1:000000000000:user:app");
when(cache.createUser(eq("app"), eq("app"), eq(AuthMode.PASSWORD), eq(List.of("pw-0123456789abcd")),
eq("on ~* +@all"), eq("redis"))).thenReturn(user);
StackResource r = resource("AWS::ElastiCache::User", "AppUser");
provisioner.provision(r, props("""
{"UserId":"app","UserName":"app","Engine":"redis","AccessString":"on ~* +@all",
"AuthenticationMode":{"Type":"password","Passwords":["pw-0123456789abcd"]}}
"""), ctx(null));
assertEquals("app", r.getPhysicalId());
assertEquals("arn:aws:elasticache:us-east-1:000000000000:user:app", r.getAttributes().get("Arn"));
assertEquals("active", r.getAttributes().get("Status"));
}
@Test
void userWithNoPasswordRequiredUsesNoAuth() throws Exception {
ElastiCacheUser user = new ElastiCacheUser("d", "default", AuthMode.NO_AUTH, List.of(), "off -@all",
"redis", "active", Instant.now());
when(cache.createUser(eq("d"), eq("default"), eq(AuthMode.NO_AUTH), any(), eq("off -@all"), eq("redis")))
.thenReturn(user);
StackResource r = resource("AWS::ElastiCache::User", "Default");
provisioner.provision(r, props("""
{"UserId":"d","UserName":"default","Engine":"redis","AccessString":"off -@all",
"NoPasswordRequired":true}
"""), ctx(null));
assertEquals("d", r.getPhysicalId());
}
@Test
void userRequiresUserIdUserNameAndEngine() throws Exception {
StackResource r = resource("AWS::ElastiCache::User", "Bad");
assertThrows(AwsException.class, () -> provisioner.provision(r, props("{\"Engine\":\"redis\"}"), ctx(null)));
assertThrows(AwsException.class, () -> provisioner.provision(r, props("{\"UserId\":\"u\",\"UserName\":\"u\"}"), ctx(null)));
verify(cache, never()).createUser(any(), any(), any(), any(), any(), any());
}
@Test
void userRejectsPasswordModeWithoutPasswords() {
StackResource r = resource("AWS::ElastiCache::User", "Bad");
AwsException ex = assertThrows(AwsException.class, () -> provisioner.provision(r, props("""
{"UserId":"u","UserName":"u","Engine":"redis",
"AuthenticationMode":{"Type":"password"}}
"""), ctx(null)));
assertEquals("InvalidParameterValue", ex.getErrorCode());
verify(cache, never()).createUser(any(), any(), any(), any(), any(), any());
}
@Test
void userRejectsPasswordsWithNoPasswordRequiredMode() {
StackResource r = resource("AWS::ElastiCache::User", "Bad");
AwsException ex = assertThrows(AwsException.class, () -> provisioner.provision(r, props("""
{"UserId":"u","UserName":"u","Engine":"redis",
"AuthenticationMode":{"Type":"no-password-required","Passwords":["pw"]}}
"""), ctx(null)));
assertEquals("InvalidParameterCombination", ex.getErrorCode());
verify(cache, never()).createUser(any(), any(), any(), any(), any(), any());
}
@Test
void userRejectsPasswordsWithIamAuthenticationMode() {
StackResource r = resource("AWS::ElastiCache::User", "Bad");
AwsException ex = assertThrows(AwsException.class, () -> provisioner.provision(r, props("""
{"UserId":"u","UserName":"u","Engine":"redis",
"AuthenticationMode":{"Type":"iam","Passwords":["pw"]}}
"""), ctx(null)));
assertEquals("InvalidParameterCombination", ex.getErrorCode());
verify(cache, never()).createUser(any(), any(), any(), any(), any(), any());
}
@Test
void userRejectsConflictingNoPasswordRequiredAndPasswords() {
StackResource r = resource("AWS::ElastiCache::User", "Bad");
AwsException ex = assertThrows(AwsException.class, () -> provisioner.provision(r, props("""
{"UserId":"u","UserName":"u","Engine":"redis","NoPasswordRequired":true,"Passwords":["pw"]}
"""), ctx(null)));
assertEquals("InvalidParameterCombination", ex.getErrorCode());
verify(cache, never()).createUser(any(), any(), any(), any(), any(), any());
}
@Test
void userRejectsConflictingNoPasswordRequiredAndAuthenticationModeType() {
StackResource r = resource("AWS::ElastiCache::User", "Bad");
AwsException ex = assertThrows(AwsException.class, () -> provisioner.provision(r, props("""
{"UserId":"u","UserName":"u","Engine":"redis","NoPasswordRequired":true,
"AuthenticationMode":{"Type":"password","Passwords":["pw"]}}
"""), ctx(null)));
assertEquals("InvalidParameterCombination", ex.getErrorCode());
verify(cache, never()).createUser(any(), any(), any(), any(), any(), any());
}
@Test
void userRejectsInvalidAuthenticationModeType() {
StackResource r = resource("AWS::ElastiCache::User", "Bad");
AwsException ex = assertThrows(AwsException.class, () -> provisioner.provision(r, props("""
{"UserId":"u","UserName":"u","Engine":"redis",
"AuthenticationMode":{"Type":"bogus"}}
"""), ctx(null)));
assertEquals("InvalidParameterValue", ex.getErrorCode());
verify(cache, never()).createUser(any(), any(), any(), any(), any(), any());
}
@Test
void userGroupRequiresUserIdsProperty() {
StackResource r = resource("AWS::ElastiCache::UserGroup", "Bad");
AwsException ex = assertThrows(AwsException.class, () -> provisioner.provision(r, props("""
{"UserGroupId":"team","Engine":"valkey"}
"""), ctx(null)));
assertEquals("ValidationException", ex.getErrorCode());
verify(cache, never()).createUserGroup(any(), any(), any(), any());
}
@Test
void userGroupSetsArnAndStatus() throws Exception {
ElastiCacheUserGroup group = new ElastiCacheUserGroup();
group.setUserGroupId("team");
group.setArn("arn:aws:elasticache:us-east-1:000000000000:usergroup:team");
group.setStatus("active");
when(cache.createUserGroup("team", "redis", List.of("d", "app"), "us-east-1")).thenReturn(group);
StackResource r = resource("AWS::ElastiCache::UserGroup", "Group");
provisioner.provision(r, props("""
{"UserGroupId":"team","Engine":"redis","UserIds":["d","app"]}
"""), ctx(null));
assertEquals("team", r.getPhysicalId());
assertEquals("arn:aws:elasticache:us-east-1:000000000000:usergroup:team", r.getAttributes().get("Arn"));
}
@Test
void userGroupUpdateAppliesMembershipDelta() throws Exception {
ElastiCacheUserGroup existing = new ElastiCacheUserGroup();
existing.setUserGroupId("team");
existing.setEngine("redis");
existing.setUserIds(new LinkedHashSet<>(List.of("d", "old")));
existing.setArn("arn");
existing.setStatus("active");
when(cache.getUserGroup("team")).thenReturn(existing);
when(cache.setUserGroupMembers(eq("team"), eq(List.of("d", "new")), eq("redis"))).thenReturn(existing);
StackResource r = resource("AWS::ElastiCache::UserGroup", "Group");
provisioner.provision(r, props("""
{"UserGroupId":"team","Engine":"redis","UserIds":["d","new"]}
"""), ctx("team"));
verify(cache, never()).createUserGroup(any(), any(), any(), any());
verify(cache).setUserGroupMembers(eq("team"), eq(List.of("d", "new")), eq("redis"));
}
@Test
void userNameChangeIsRejectedBecauseItIsCreateOnly() throws Exception {
ElastiCacheUser existing = new ElastiCacheUser("app", "old-name", AuthMode.NO_AUTH, List.of(), "on ~* +@all",
"redis", "active", Instant.now());
when(cache.getUser("app")).thenReturn(existing);
StackResource r = resource("AWS::ElastiCache::User", "AppUser");
assertThrows(AwsException.class, () -> provisioner.provision(r,
props("{\"UserId\":\"app\",\"UserName\":\"new-name\",\"Engine\":\"redis\"}"), ctx("app")));
verify(cache, never()).modifyUser(any(), any(), any(), any(), any(), any());
}
@Test
void userGroupEngineChangeIsAppliedInPlace() throws Exception {
ElastiCacheUserGroup existing = new ElastiCacheUserGroup();
existing.setUserGroupId("team");
existing.setEngine("redis");
existing.setArn("arn");
existing.setStatus("active");
when(cache.getUserGroup("team")).thenReturn(existing);
when(cache.setUserGroupMembers(eq("team"), eq(List.of()), eq("valkey"))).thenReturn(existing);
StackResource r = resource("AWS::ElastiCache::UserGroup", "Group");
provisioner.provision(r, props("{\"UserGroupId\":\"team\",\"Engine\":\"valkey\",\"UserIds\":[]}"), ctx("team"));
verify(cache).setUserGroupMembers("team", List.of(), "valkey");
verify(cache, never()).createUserGroup(any(), any(), any(), any());
}
@Test
void mixedCaseUserGroupIdIsReusedOnUpdateThroughItsLowercaseForm() throws Exception {
ElastiCacheUserGroup existing = new ElastiCacheUserGroup();
existing.setUserGroupId("myusers");
existing.setEngine("redis");
existing.setArn("arn");
existing.setStatus("active");
when(cache.getUserGroup("myusers")).thenReturn(existing);
when(cache.setUserGroupMembers("myusers", List.of(), "redis")).thenReturn(existing);
StackResource r = resource("AWS::ElastiCache::UserGroup", "Group");
provisioner.provision(r, props("{\"UserGroupId\":\"MyUsers\",\"Engine\":\"redis\",\"UserIds\":[]}"),
ctx("myusers"));
verify(cache, never()).createUserGroup(any(), any(), any(), any());
verify(cache).setUserGroupMembers("myusers", List.of(), "redis");
assertEquals("myusers", r.getPhysicalId());
}
@Test
void transitEncryptionDoesNotTurnOnIamAuthentication() throws Exception {
when(cache.createCacheCluster(any())).thenReturn(new CacheCluster("tls", CacheClusterStatus.AVAILABLE,
@@ -349,6 +556,81 @@ class ElastiCacheCfnProvisionerTest {
assertNull(r.getAttributes().get("__FlociCacheSubnetGroupUpdateSnapshot"));
}
@Test
void inPlaceUserUpdateCanBeRolledBackFromSnapshot() throws Exception {
ElastiCacheUser existing = new ElastiCacheUser("app", "app", AuthMode.PASSWORD, List.of("pw-old"),
"on ~* +@all", "redis", "active", Instant.now());
when(cache.getUser("app")).thenReturn(existing);
ElastiCacheUser modified = new ElastiCacheUser("app", "app", AuthMode.PASSWORD, List.of("pw-new"),
"off -@all", "redis", "active", Instant.now());
when(cache.modifyUser(eq("app"), eq(AuthMode.PASSWORD), eq(List.of("pw-new")), eq("off -@all"), any(), eq("redis")))
.thenReturn(modified);
StackResource r = resource("AWS::ElastiCache::User", "AppUser");
provisioner.provision(r, props("""
{"UserId":"app","UserName":"app","Engine":"redis","AccessString":"off -@all",
"AuthenticationMode":{"Type":"password","Passwords":["pw-new"]}}
"""), ctx("app"));
assertTrue(provisioner.rollbackUpdate(r));
verify(cache).modifyUser("app", AuthMode.PASSWORD, List.of("pw-old"), "on ~* +@all", null, "redis");
}
@Test
void inPlaceUserGroupUpdateCanBeRolledBackFromSnapshot() throws Exception {
ElastiCacheUserGroup existing = new ElastiCacheUserGroup();
existing.setUserGroupId("team");
existing.setEngine("redis");
existing.setUserIds(new LinkedHashSet<>(List.of("u1", "u2")));
existing.setArn("arn");
existing.setStatus("active");
when(cache.getUserGroup("team")).thenReturn(existing);
ElastiCacheUserGroup modified = new ElastiCacheUserGroup();
modified.setUserGroupId("team");
modified.setEngine("redis");
modified.setUserIds(new LinkedHashSet<>(List.of("u1", "u3")));
modified.setArn("arn");
modified.setStatus("active");
when(cache.setUserGroupMembers(eq("team"), eq(List.of("u1", "u3")), eq("redis"))).thenReturn(modified);
StackResource r = resource("AWS::ElastiCache::UserGroup", "Group");
provisioner.provision(r, props("""
{"UserGroupId":"team","Engine":"redis","UserIds":["u1","u3"]}
"""), ctx("team"));
assertTrue(provisioner.rollbackUpdate(r));
verify(cache).setUserGroupMembers("team", List.of("u1", "u2"), "redis");
}
@Test
void committedUserAndUserGroupUpdateClearsRollbackSnapshots() {
StackResource rUser = resource("AWS::ElastiCache::User", "User");
rUser.getAttributes().put("__FlociUserUpdateSnapshot", "snapshot-data");
rUser.setStatus("UPDATE_COMPLETE");
provisioner.completeUpdate(rUser);
assertNull(rUser.getAttributes().get("__FlociUserUpdateSnapshot"));
StackResource rGroup = resource("AWS::ElastiCache::UserGroup", "Group");
rGroup.getAttributes().put("__FlociUserGroupUpdateSnapshot", "snapshot-data");
rGroup.setStatus("UPDATE_COMPLETE");
provisioner.completeUpdate(rGroup);
assertNull(rGroup.getAttributes().get("__FlociUserGroupUpdateSnapshot"));
}
@Test
void clearUpdateClearsUserAndUserGroupSnapshots() {
StackResource rUser = resource("AWS::ElastiCache::User", "User");
rUser.getAttributes().put("__FlociUserUpdateSnapshot", "snapshot-data");
provisioner.clearUpdate(rUser);
assertNull(rUser.getAttributes().get("__FlociUserUpdateSnapshot"));
StackResource rGroup = resource("AWS::ElastiCache::UserGroup", "Group");
rGroup.getAttributes().put("__FlociUserGroupUpdateSnapshot", "snapshot-data");
provisioner.clearUpdate(rGroup);
assertNull(rGroup.getAttributes().get("__FlociUserGroupUpdateSnapshot"));
}
@Test
void clusterDeletedOutsideTheStackIsRecreatedOnUpdate() throws Exception {
when(cache.findCacheClusters("app-cache")).thenReturn(List.of());
@@ -380,15 +662,31 @@ class ElastiCacheCfnProvisionerTest {
}
@Test
void deleteToleratesAlreadyDeletedButPropagatesOtherFailures() {
doThrow(new AwsException("CacheSubnetGroupNotFoundFault", "gone", 400))
.when(cache).deleteCacheSubnetGroup("gone");
provisioner.delete("AWS::ElastiCache::SubnetGroup", "gone", "us-east-1");
void renamedUserGroupReplacesAndDeletesTheOldOne() throws Exception {
ElastiCacheUserGroup created = new ElastiCacheUserGroup();
created.setUserGroupId("new-team");
created.setArn("arn");
created.setStatus("active");
when(cache.createUserGroup("new-team", "redis", List.of(), "us-east-1")).thenReturn(created);
StackResource r = resource("AWS::ElastiCache::UserGroup", "Group");
doThrow(new AwsException("CacheSubnetGroupInUse", "busy", 400))
.when(cache).deleteCacheSubnetGroup("busy");
provisioner.provision(r, props("{\"UserGroupId\":\"new-team\",\"Engine\":\"redis\",\"UserIds\":[]}"), ctx("old-team"));
assertTrue(provisioner.hasReplacementUpdate(r));
provisioner.completeUpdate(r);
verify(cache).deleteUserGroup("old-team");
}
@Test
void deleteToleratesAlreadyDeletedButPropagatesOtherFailures() {
doThrow(new AwsException("UserNotFoundFault", "gone", 404))
.when(cache).deleteUser("gone");
provisioner.delete("AWS::ElastiCache::User", "gone", "us-east-1");
doThrow(new AwsException("InvalidUserGroupState", "busy", 400))
.when(cache).deleteUserGroup("busy");
assertThrows(AwsException.class,
() -> provisioner.delete("AWS::ElastiCache::SubnetGroup", "busy", "us-east-1"));
() -> provisioner.delete("AWS::ElastiCache::UserGroup", "busy", "us-east-1"));
}
@Test
@@ -218,6 +218,42 @@ class ElastiCacheServiceTest {
assertThrows(AwsException.class, () -> service.getUserGroup("app-group")).getErrorCode());
}
@Test
void setUserGroupMembersReplacesCompleteMembershipAndEngine() {
service.createUser("default-user-id", "default", AuthMode.PASSWORD,
List.of("default-pass"), "on ~* +@all", null);
service.createUser("app-user-id", "app", AuthMode.PASSWORD,
List.of("app-pass"), "on ~* +@all", null);
service.createUser("extra-user-id", "extra", AuthMode.PASSWORD,
List.of("extra-pass"), "on ~* +@all", null);
service.createUserGroup("set-grp", "redis", List.of("default-user-id", "app-user-id"), "us-east-1");
ElastiCacheUserGroup updated = service.setUserGroupMembers("set-grp",
List.of("default-user-id", "extra-user-id"), "redis");
assertEquals(Set.of("default-user-id", "extra-user-id"), updated.getUserIds());
assertEquals("redis", updated.getEngine());
ElastiCacheUserGroup fetched = service.getUserGroup("set-grp");
assertEquals(Set.of("default-user-id", "extra-user-id"), fetched.getUserIds());
}
@Test
void setUserGroupMembersRejectsInvalidEngineOrMissingDefaultUserForRedis() {
service.createUser("default-user-id", "default", AuthMode.PASSWORD,
List.of("default-pass"), "on ~* +@all", null);
service.createUser("app-user-id", "app", AuthMode.PASSWORD,
List.of("app-pass"), "on ~* +@all", null);
service.createUserGroup("val-grp", "redis", List.of("default-user-id"), "us-east-1");
AwsException exMissing = assertThrows(AwsException.class,
() -> service.setUserGroupMembers("val-grp", List.of("app-user-id"), "redis"));
assertEquals("DefaultUserRequired", exMissing.getErrorCode());
AwsException exEngine = assertThrows(AwsException.class,
() -> service.setUserGroupMembers("val-grp", List.of("default-user-id"), "unknown"));
assertEquals("InvalidParameterValue", exEngine.getErrorCode());
}
@Test
void userGroupNeedsInTransitEncryptionOnTheReplicationGroup() {
service.createReplicationGroup("open", "test", AuthMode.NO_AUTH, null, "us-east-1");
@@ -0,0 +1,60 @@
package io.github.hectorvent.floci.services.elasticache;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.services.elasticache.model.AuthMode;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
class UserAuthenticationTest {
@Test
void nothingGivenResolvesToNull() {
assertNull(UserAuthentication.resolve(null, List.of(), false));
}
@Test
void passwordsWithoutTypeMeanPasswordAuthentication() {
UserAuthentication auth = UserAuthentication.resolve(null, List.of("p"), false);
assertEquals(AuthMode.PASSWORD, auth.mode());
assertEquals(List.of("p"), auth.passwords());
}
@Test
void noPasswordRequiredWithoutTypeMeansNoAuth() {
assertEquals(AuthMode.NO_AUTH, UserAuthentication.resolve(null, List.of(), true).mode());
}
@Test
void typeIsMatchedCaseInsensitively() {
assertEquals(AuthMode.IAM, UserAuthentication.resolve("IAM", List.of(), false).mode());
assertEquals(AuthMode.NO_AUTH, UserAuthentication.resolve("No-Password-Required", List.of(), false).mode());
}
@Test
void passwordTypeWithoutPasswordsIsRejected() {
AwsException e = assertThrows(AwsException.class,
() -> UserAuthentication.resolve("password", List.of(), false));
assertEquals("InvalidParameterValue", e.getErrorCode());
}
@Test
void conflictingInputsAreRejected() {
assertEquals("InvalidParameterCombination", assertThrows(AwsException.class,
() -> UserAuthentication.resolve(null, List.of("p"), true)).getErrorCode());
assertEquals("InvalidParameterCombination", assertThrows(AwsException.class,
() -> UserAuthentication.resolve("password", List.of("p"), true)).getErrorCode());
assertEquals("InvalidParameterCombination", assertThrows(AwsException.class,
() -> UserAuthentication.resolve("iam", List.of("p"), false)).getErrorCode());
}
@Test
void unknownTypeIsRejected() {
assertEquals("InvalidParameterValue", assertThrows(AwsException.class,
() -> UserAuthentication.resolve("kerberos", List.of(), false)).getErrorCode());
}
}