Light, fluffy, and always free - The AWS Local Emulator alternative
feat(elasticache): add user groups and associate them with replication groups (#4435)
Clement0848fa6a4874
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.javaindex b7a5e26473..995af422b2 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@@ -11,19 +11,27 @@ import org.junit.jupiter.api.TestMethodOrder;import software.amazon.awssdk.services.elasticache.ElastiCacheClient;import software.amazon.awssdk.services.elasticache.model.AuthenticationMode;import software.amazon.awssdk.services.elasticache.model.CreateReplicationGroupRequest;+import software.amazon.awssdk.services.elasticache.model.CreateUserGroupRequest;+import software.amazon.awssdk.services.elasticache.model.CreateUserGroupResponse;import software.amazon.awssdk.services.elasticache.model.CreateUserRequest;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.DescribeReplicationGroupsRequest;+import software.amazon.awssdk.services.elasticache.model.DescribeUserGroupsRequest;+import software.amazon.awssdk.services.elasticache.model.DescribeUserGroupsResponse;import software.amazon.awssdk.services.elasticache.model.DescribeUsersRequest;+import software.amazon.awssdk.services.elasticache.model.ElastiCacheException;import software.amazon.awssdk.services.elasticache.model.Endpoint;import software.amazon.awssdk.services.elasticache.model.InputAuthenticationType;+import software.amazon.awssdk.services.elasticache.model.InvalidUserGroupStateException;import software.amazon.awssdk.services.elasticache.model.ModifyReplicationGroupRequest;+import software.amazon.awssdk.services.elasticache.model.ModifyReplicationGroupResponse;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.ElastiCacheException;+import software.amazon.awssdk.services.elasticache.model.UserGroupNotFoundException;import java.io.IOException;import java.io.InputStream;@@ -212,9 +220,9 @@ class ElastiCacheTest {assertThat(rejectReply).isEqualTo("-ERR invalid username-password pair or user is disabled.\r\n");// Associate user with group via ModifyReplicationGroup.- // Known deviation: Floci treats userGroupIdsToAdd as raw user IDs because- // UserGroup resources are not yet implemented. In real AWS, this parameter- // accepts UserGroupIds (which are separate resources containing users).+ // Known deviation: an id that names no user group but names a user associates that+ // user directly, which Floci accepted before it modelled user groups. Real AWS only+ // accepts user group ids here; userGroupMembersAuthenticateThroughTheProxy covers that.var response = elasticache.modifyReplicationGroup(ModifyReplicationGroupRequest.builder().replicationGroupId(groupId).userGroupIdsToAdd(userId)@@ -333,6 +341,75 @@ class ElastiCacheTest {@Test@Order(13)+ void userGroupMembersAuthenticateThroughTheProxy() throws Exception {+ requireGroup();+ String defaultUserId = TestFixtures.uniqueName("ec-ug-default");+ String memberUserId = TestFixtures.uniqueName("ec-ug-member");+ String memberUserName = TestFixtures.uniqueName("ec-ug-member-name");+ String userGroupId = TestFixtures.uniqueName("ec-ug");+ elasticache.createUser(CreateUserRequest.builder()+ .userId(defaultUserId).userName("default").engine("redis")+ .accessString("on ~* +@all")+ .authenticationMode(AuthenticationMode.builder()+ .type(InputAuthenticationType.PASSWORD).passwords("default-password-0123").build())+ .build());+ elasticache.createUser(CreateUserRequest.builder()+ .userId(memberUserId).userName(memberUserName).engine("redis")+ .accessString("on ~* +@all")+ .authenticationMode(AuthenticationMode.builder()+ .type(InputAuthenticationType.PASSWORD).passwords("member-password-0123").build())+ .build());+ try {+ CreateUserGroupResponse created = elasticache.createUserGroup(CreateUserGroupRequest.builder()+ .userGroupId(userGroupId)+ .engine("redis")+ .userIds(defaultUserId, memberUserId)+ .build());+ assertThat(created.status()).isEqualTo("active");+ assertThat(created.userIds()).containsExactlyInAnyOrder(defaultUserId, memberUserId);++ ModifyReplicationGroupResponse associated = elasticache.modifyReplicationGroup(+ ModifyReplicationGroupRequest.builder()+ .replicationGroupId(groupId)+ .userGroupIdsToAdd(userGroupId)+ .build());+ assertThat(associated.replicationGroup().userGroupIds()).contains(userGroupId);+ assertThat(associated.replicationGroup().authTokenEnabled()).isFalse();++ try (Socket socket = openSocket(firstProxyPort)) {+ write(socket, respArray("AUTH", memberUserName, "member-password-0123"));+ assertThat(readLine(socket)).isEqualTo("+OK\r\n");+ }++ DescribeUserGroupsResponse described = elasticache.describeUserGroups(DescribeUserGroupsRequest.builder()+ .userGroupId(userGroupId)+ .build());+ assertThat(described.userGroups().get(0).replicationGroups()).containsExactly(groupId);++ assertThatThrownBy(() -> elasticache.deleteUserGroup(DeleteUserGroupRequest.builder()+ .userGroupId(userGroupId)+ .build()))+ .isInstanceOf(InvalidUserGroupStateException.class);++ elasticache.modifyReplicationGroup(ModifyReplicationGroupRequest.builder()+ .replicationGroupId(groupId)+ .userGroupIdsToRemove(userGroupId)+ .build());+ assertThat(sendCommand(firstProxyPort, respArray("AUTH", memberUserName, "member-password-0123")))+ .isEqualTo("-ERR invalid username-password pair or user is disabled.\r\n");+ } finally {+ try {+ elasticache.deleteUserGroup(DeleteUserGroupRequest.builder().userGroupId(userGroupId).build());+ } catch (UserGroupNotFoundException ignored) {+ // the group was never created+ }+ elasticache.deleteUser(DeleteUserRequest.builder().userId(memberUserId).build());+ elasticache.deleteUser(DeleteUserRequest.builder().userId(defaultUserId).build());+ }+ }++ @Test+ @Order(14)void deleteReplicationGroupReleasesPortForReuse() {requireGroup();diff --git a/docs/services/elasticache.md b/docs/services/elasticache.mdindex c8d2becf5f..61b48ae66e 100644--- a/docs/services/elasticache.md+++ b/docs/services/elasticache.md@@ -12,14 +12,18 @@ Floci manages real Valkey/Redis Docker containers and proxies TCP connections to| Action | Description || --- | --- || `ValidateIamAuthToken` | Validate an IAM auth token (data-plane auth) |-| `CreateReplicationGroup` | Start a new Redis/Valkey cluster; `AtRestEncryptionEnabled`, `KmsKeyId` (resolved to the key ARN), `SnapshotRetentionLimit`, `SnapshotWindow` and `Tags` are kept and returned, with the group `ARN` |+| `CreateReplicationGroup` | Start a new Redis/Valkey cluster; `AtRestEncryptionEnabled`, `KmsKeyId` (resolved to the key ARN), `SnapshotRetentionLimit`, `SnapshotWindow`, `Tags` and `UserGroupIds` are kept and returned, with the group `ARN`. `UserGroupIds` needs `TransitEncryptionEnabled` or an `AuthToken` || `DescribeReplicationGroups` | List clusters and their connection info |-| `ModifyReplicationGroup` | Modify `SnapshotRetentionLimit` and `SnapshotWindow`, and the associated user groups |+| `ModifyReplicationGroup` | Modify `SnapshotRetentionLimit` and `SnapshotWindow`, and the associated user groups (`UserGroupIdsToAdd`, `UserGroupIdsToRemove`, `RemoveUserGroups`). A cache with user groups no longer takes its AUTH token, `AuthTokenUpdateStrategy=DELETE` removes the token when adding a user group, and removing the last user group leaves the cache without access control. Changing the AUTH token (`ROTATE`, `SET`) is not supported || `DeleteReplicationGroup` | Stop and remove a cluster || `CreateUser` | Create a user with password, no-password-required or IAM authentication, set through `AuthenticationMode` or the top-level `Passwords` and `NoPasswordRequired` || `DescribeUsers` | List ElastiCache users || `ModifyUser` | Change a user's passwords and authentication mode, replace its access string with `AccessString` or add to it with `AppendAccessString` || `DeleteUser` | Remove an ElastiCache user |+| `CreateUserGroup` | Create a user group; a `redis` group must contain a user named `default`, a `valkey` group need not |+| `DescribeUserGroups` | List user groups with their users and the replication groups using them |+| `ModifyUserGroup` | Add or remove users, or change the engine; membership changes apply to authentication at once |+| `DeleteUserGroup` | Remove a user group that no replication group uses || `CreateCacheCluster` | Start a Memcached cluster, or a single-node Redis/Valkey one (`NumCacheNodes` must be 1) || `DescribeCacheClusters` | List cache clusters: Memcached, single-node Redis/Valkey, and replication group members || `DeleteCacheCluster` | Stop and remove a cache cluster |diff --git a/docs/services/index.md b/docs/services/index.mdindex 715090351e..03af8a03a7 100644--- a/docs/services/index.md+++ b/docs/services/index.md@@ -60,7 +60,7 @@ Operation counts are exact. For dispatch-table services (Query and JSON 1.1) eac| [Amazon Connect](connect.md) | `/instance`, `/instance/{instanceId}/*`, `/tags/*` | REST JSON | 15 || [Amazon AppIntegrations](appintegrations.md) | `/eventIntegrations/*`, `/dataIntegrations/*`, `/tags/*` | REST JSON | 14 || [Amazon Data Lifecycle Manager](dlm.md) | `/policies`, `/policies/{policyId}`, `/tags/{resourceArn}` | REST JSON | 8 |-| [ElastiCache](elasticache.md) | `POST /` with `Action=` param + TCP proxy | Query + RESP | 22 |+| [ElastiCache](elasticache.md) | `POST /` with `Action=` param + TCP proxy | Query + RESP | 26 || [MemoryDB](memorydb.md) | `POST /` + `X-Amz-Target: AmazonMemoryDB.*` + TCP proxy | JSON 1.1 + RESP | 13 || [RDS](rds.md) | `POST /` with `Action=` param + TCP proxy | Query + wire | 90 || [RDS Data API](rds-data.md) | `/Execute`, `/BeginTransaction`, `/CommitTransaction`, `/RollbackTransaction` | REST JSON | 4 |diff --git a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.javaindex 7947966974..80f184ed15 100644--- a/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java+++ b/src/main/java/io/github/hectorvent/floci/core/common/AwsQueryController.java@@ -533,7 +533,8 @@ public class AwsQueryController {private static final Set<String> ELASTICACHE_ACTIONS = Set.of("ValidateIamAuthToken","CreateReplicationGroup", "DescribeReplicationGroups", "ModifyReplicationGroup", "DeleteReplicationGroup",- "CreateUser", "DescribeUsers", "ModifyUser", "DeleteUser"+ "CreateUser", "DescribeUsers", "ModifyUser", "DeleteUser",+ "CreateUserGroup", "DescribeUserGroups", "ModifyUserGroup", "DeleteUserGroup");private static final Set<String> CLOUDWATCH_ACTIONS = Set.of(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.javaindex 1211f46ba5..8273d671f2 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@@ -14,6 +14,7 @@ import io.github.hectorvent.floci.services.elasticache.model.CacheParameterGroupimport io.github.hectorvent.floci.services.elasticache.model.CacheSubnetGroup;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;import io.github.hectorvent.floci.services.elasticache.model.Endpoint;import io.github.hectorvent.floci.services.elasticache.model.ReplicationGroup;import io.github.hectorvent.floci.services.elasticache.model.ReplicationGroupSettings;@@ -72,6 +73,10 @@ public class ElastiCacheQueryHandler {case "DescribeUsers" -> handleDescribeUsers(params);case "ModifyUser" -> handleModifyUser(params);case "DeleteUser" -> handleDeleteUser(params);+ case "CreateUserGroup" -> handleCreateUserGroup(params, region);+ case "DescribeUserGroups" -> handleDescribeUserGroups(params);+ case "ModifyUserGroup" -> handleModifyUserGroup(params);+ case "DeleteUserGroup" -> handleDeleteUserGroup(params);case "CreateCacheCluster" -> handleCreateCacheCluster(params, region);case "DescribeCacheClusters" -> handleDescribeCacheClusters(params);case "DeleteCacheCluster" -> handleDeleteCacheCluster(params);@@ -133,7 +138,8 @@ public class ElastiCacheQueryHandler {boolParam(params, "MultiAZEnabled"),intParam(params, "Port"),replicationGroupSettings(params),- parseTags(params)));+ parseTags(params),+ extractMemberList(params, "UserGroupIds.member.")));String result = replicationGroupXml(group);return Response.ok(AwsQueryResponse.envelope("CreateReplicationGroup", AwsNamespaces.EC, result)).build();} catch (AwsException e) {@@ -222,15 +228,19 @@ public class ElastiCacheQueryHandler {return AwsQueryResponse.error("InvalidParameterValue","ReplicationGroupId is required.", AwsNamespaces.EC, 400);}- List<String> userIdsToAdd = extractMemberList(params, "UserGroupIdsToAdd.member.");- List<String> userIdsToRemove = extractMemberList(params, "UserGroupIdsToRemove.member.");+ List<String> userGroupIdsToAdd = extractMemberList(params, "UserGroupIdsToAdd.member.");+ List<String> userGroupIdsToRemove = extractMemberList(params, "UserGroupIdsToRemove.member.");try {// AtRestEncryptionEnabled and KmsKeyId are fixed at create and not in the modify shapeReplicationGroupSettings settings = new ReplicationGroupSettings(null, null,optionalInt(params.getFirst("SnapshotRetentionLimit")), params.getFirst("SnapshotWindow"));+ // AuthToken with ROTATE or SET is not modelled yet; DELETE moves the cache to user groupsReplicationGroup group = service.modifyReplicationGroup(groupId,- userIdsToAdd.isEmpty() ? null : userIdsToAdd,- userIdsToRemove.isEmpty() ? null : userIdsToRemove, settings);+ userGroupIdsToAdd.isEmpty() ? null : userGroupIdsToAdd,+ userGroupIdsToRemove.isEmpty() ? null : userGroupIdsToRemove,+ Boolean.parseBoolean(params.getFirst("RemoveUserGroups")),+ "DELETE".equals(params.getFirst("AuthTokenUpdateStrategy")),+ settings);String result = replicationGroupXml(group);return Response.ok(AwsQueryResponse.envelope("ModifyReplicationGroup", AwsNamespaces.EC, result)).build();} catch (AwsException e) {@@ -319,6 +329,78 @@ public class ElastiCacheQueryHandler {}}+ // ── User groups ───────────────────────────────────────────────────────────++ private Response handleCreateUserGroup(MultivaluedMap<String, String> params, String region) {+ try {+ ElastiCacheUserGroup userGroup = service.createUserGroup(params.getFirst("UserGroupId"),+ params.getFirst("Engine"), extractMemberList(params, "UserIds.member."), region);+ return Response.ok(AwsQueryResponse.envelope("CreateUserGroup", AwsNamespaces.EC,+ userGroupXml(userGroup))).build();+ } catch (AwsException e) {+ return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.EC, e.getHttpStatus());+ }+ }++ private Response handleDescribeUserGroups(MultivaluedMap<String, String> params) {+ try {+ Collection<ElastiCacheUserGroup> userGroups = service.listUserGroups(params.getFirst("UserGroupId"));+ XmlBuilder xml = new XmlBuilder().start("UserGroups");+ for (ElastiCacheUserGroup userGroup : userGroups) {+ xml.start("member").raw(userGroupXml(userGroup)).end("member");+ }+ xml.end("UserGroups");+ return Response.ok(AwsQueryResponse.envelope("DescribeUserGroups", AwsNamespaces.EC, xml.build())).build();+ } catch (AwsException e) {+ return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.EC, e.getHttpStatus());+ }+ }++ private Response handleModifyUserGroup(MultivaluedMap<String, String> params) {+ try {+ ElastiCacheUserGroup userGroup = service.modifyUserGroup(params.getFirst("UserGroupId"),+ extractMemberList(params, "UserIdsToAdd.member."),+ extractMemberList(params, "UserIdsToRemove.member."),+ params.getFirst("Engine"));+ return Response.ok(AwsQueryResponse.envelope("ModifyUserGroup", AwsNamespaces.EC,+ userGroupXml(userGroup))).build();+ } catch (AwsException e) {+ return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.EC, e.getHttpStatus());+ }+ }++ private Response handleDeleteUserGroup(MultivaluedMap<String, String> params) {+ try {+ ElastiCacheUserGroup userGroup = service.deleteUserGroup(params.getFirst("UserGroupId"));+ return Response.ok(AwsQueryResponse.envelope("DeleteUserGroup", AwsNamespaces.EC,+ userGroupXml(userGroup))).build();+ } catch (AwsException e) {+ return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.EC, e.getHttpStatus());+ }+ }++ private String userGroupXml(ElastiCacheUserGroup g) {+ XmlBuilder xml = new XmlBuilder()+ .elem("UserGroupId", g.getUserGroupId())+ .elem("Status", g.getStatus())+ .elem("Engine", g.getEngine());+ xml.start("UserIds");+ for (String userId : g.getUserIds()) {+ xml.elem("member", userId);+ }+ xml.end("UserIds");+ // MinimumEngineVersion: the only value AWS documents, as on users.+ xml.elem("MinimumEngineVersion", "6.0");+ xml.start("ReplicationGroups");+ for (String replicationGroupId : service.replicationGroupIdsUsing(g.getUserGroupId())) {+ xml.elem("member", replicationGroupId);+ }+ xml.end("ReplicationGroups");+ xml.start("ServerlessCaches").end("ServerlessCaches");+ xml.elem("ARN", g.getArn());+ return xml.build();+ }+// ── Cache Clusters (Memcached, and single-node Redis/Valkey) ──────────────private Response handleCreateCacheCluster(MultivaluedMap<String, String> params, String region) {@@ -407,7 +489,7 @@ public class ElastiCacheQueryHandler {private String memberCacheClusterXml(ElastiCacheService.MemberCacheCluster member, boolean showNodeInfo) {ReplicationGroup g = member.group();- boolean authTokenEnabled = g.getAuthMode() == AuthMode.PASSWORD;+ boolean authTokenEnabled = authTokenEnabled(g);XmlBuilder xml = new XmlBuilder().start("CacheCluster").elem("CacheClusterId", member.cacheClusterId())@@ -930,12 +1012,17 @@ private Response handleCreateCacheParameterGroup(MultivaluedMap<String, String>* aws_elasticache_replication_group: it reads transit_encryption_enabled back on every plan.*/private static boolean transitEncryptionEnabled(ReplicationGroup g) {- return g.getAuthMode() != AuthMode.NO_AUTH;+ return g.isEncryptedInTransit();+ }++ // a cache uses either AUTH or user groups (RBAC), so attaching a user group turns the token off+ private static boolean authTokenEnabled(ReplicationGroup g) {+ return g.getAuthMode() == AuthMode.PASSWORD && g.getUserGroupIds().isEmpty();}private String replicationGroupXml(ReplicationGroup g) {Endpoint ep = g.getConfigurationEndpoint();- boolean authTokenEnabled = g.getAuthMode() == AuthMode.PASSWORD;+ boolean authTokenEnabled = authTokenEnabled(g);List<ElastiCacheService.MemberCacheCluster> members = service.memberCacheClusters(g);XmlBuilder xml = new XmlBuilder().start("ReplicationGroup")@@ -964,6 +1051,9 @@ private Response handleCreateCacheParameterGroup(MultivaluedMap<String, String>if (g.getArn() != null) {xml.elem("ARN", g.getArn());}+ if (!g.getUserGroupIds().isEmpty()) {+ xml.raw(memberListXml("UserGroupIds", g.getUserGroupIds()));+ }xml.start("MemberClusters");for (ElastiCacheService.MemberCacheCluster member : members) {xml.elem("ClusterId", member.cacheClusterId());@@ -1058,7 +1148,7 @@ private Response handleCreateCacheParameterGroup(MultivaluedMap<String, String>.elem("Engine", u.getEngine())// MinimumEngineVersion: the only value AWS documents; no valkey-specific one is published..elem("MinimumEngineVersion", "6.0")- .start("UserGroupIds").end("UserGroupIds")+ .raw(memberListXml("UserGroupIds", service.userGroupIdsContaining(u.getUserId()))).elem("ARN", userArn(u)).build();}@@ -1128,6 +1218,14 @@ private Response handleCreateCacheParameterGroup(MultivaluedMap<String, String>return new AwsException("InvalidParameterCombination", message, 400);}+ private static String memberListXml(String name, Collection<String> values) {+ XmlBuilder xml = new XmlBuilder().start(name);+ for (String value : values) {+ xml.elem("member", value);+ }+ return xml.end(name).build();+ }+private static List<String> extractMemberList(MultivaluedMap<String, String> params, String prefix) {List<String> values = new ArrayList<>();for (int i = 1; ; i++) {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.javaindex c012bfec4c..91e9bdc71a 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@@ -24,6 +24,7 @@ import io.github.hectorvent.floci.services.elasticache.model.CacheParameterGroupimport io.github.hectorvent.floci.services.elasticache.model.CacheSubnetGroup;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;import io.github.hectorvent.floci.services.elasticache.model.Endpoint;import io.github.hectorvent.floci.services.elasticache.model.ReplicationGroup;import io.github.hectorvent.floci.services.elasticache.model.ReplicationGroupSettings;@@ -42,8 +43,10 @@ import java.net.UnknownHostException;import java.time.Instant;import java.util.ArrayList;import java.util.Collection;+import java.util.HashMap;import java.util.HashSet;import java.util.LinkedHashMap;+import java.util.LinkedHashSet;import java.util.List;import java.util.Locale;import java.util.Map;@@ -72,6 +75,7 @@ public class ElastiCacheService implements ResourceProvider {*/private final StorageBackend<String, CacheCluster> memcachedClusters;private final StorageBackend<String, ElastiCacheUser> users;+ private final AccountAwareStorageBackend<ElastiCacheUserGroup> userGroups;private final AccountAwareStorageBackend<CacheParameterGroup> parameterGroups;private final StorageBackend<String, CacheSubnetGroup> subnetGroups;private final ElastiCacheContainerManager containerManager;@@ -108,6 +112,19 @@ public class ElastiCacheService implements ResourceProvider {* parameter group that no longer exists.*/private final ConcurrentHashMap<String, Integer> reservedParameterGroups = new ConcurrentHashMap<>();+ /**+ * Guards user groups, their members and their association with replication groups, so a+ * delete, a membership change and an association cannot interleave. Taken inside a+ * replication group's lock, never around one.+ */+ private final Object userGroupLock = new Object();+ /**+ * User groups named by in-flight creates, keyed by account and id (see+ * {@link #userGroupReservationKey}) with the number of creates holding each: a+ * replication group is only stored once provisioning finishes, so the stored-groups scan+ * alone would let DeleteUserGroup remove a group that the new cache is about to use.+ */+ private final Map<String, Integer> reservedUserGroups = new HashMap<>();@Injectpublic ElastiCacheService(ElastiCacheContainerManager containerManager,@@ -135,6 +152,8 @@ public class ElastiCacheService implements ResourceProvider {new TypeReference<Map<String, CacheCluster>>() {});this.users = storageFactory.create("elasticache", "elasticache-users.json",new TypeReference<Map<String, ElastiCacheUser>>() {});+ this.userGroups = storageFactory.create("elasticache", "elasticache-user-groups.json",+ new TypeReference<Map<String, ElastiCacheUserGroup>>() {});this.parameterGroups = storageFactory.create("elasticache", "elasticache-parameter-groups.json",new TypeReference<Map<String, CacheParameterGroup>>() {});this.subnetGroups = storageFactory.create("elasticache", "elasticache-subnet-groups.json",@@ -164,7 +183,8 @@ public class ElastiCacheService implements ResourceProvider {Boolean multiAzEnabled,Integer port,ReplicationGroupSettings settings,- Map<String, String> tags) {+ Map<String, String> tags,+ List<String> userGroupIds) {}public ReplicationGroup createReplicationGroup(String groupId, String description,@@ -179,17 +199,59 @@ public class ElastiCacheService implements ResourceProvider {Map<String, String> tags) {return createReplicationGroup(new CreateReplicationGroupRequest(groupId, description,authMode, authToken, region, null, null, null, null, null, null,- null, null, null, null, null, null, settings, tags));+ null, null, null, null, null, null, settings, tags, null));}public ReplicationGroup createReplicationGroup(CreateReplicationGroupRequest request) {- String groupId = request.replicationGroupId();ReplicationGroupSettings settings = request.settings() != null? request.settings() : ReplicationGroupSettings.defaults();settings.validate();// resolved with the other validations, before a port is taken or a container startedReplicationGroupSettings resolvedSettings =settings.withKmsKeyId(resolveKmsKeyArn(settings.kmsKeyId(), request.region()));+ List<String> userGroupReservation = reserveUserGroups(request);+ try {+ return createReplicationGroupWithSettings(request, resolvedSettings);+ } finally {+ releaseUserGroups(userGroupReservation);+ }+ }++ /**+ * Checks the user groups a create names and marks them in use until the create finishes,+ * under {@link #userGroupLock} so the check and the claim cannot straddle a DeleteUserGroup.+ */+ private List<String> reserveUserGroups(CreateReplicationGroupRequest request) {+ if (request.userGroupIds() == null || request.userGroupIds().isEmpty()) {+ return List.of();+ }+ String engine = engineFor(request);+ synchronized (userGroupLock) {+ List<String> reserved = new ArrayList<>();+ for (String userGroupId : request.userGroupIds()) {+ ElastiCacheUserGroup userGroup = getUserGroup(userGroupId);+ requireAssociable(userGroup, request.authMode() != null && request.authMode() != AuthMode.NO_AUTH, engine);+ reserved.add(userGroupReservationKey(userGroup.getUserGroupId()));+ }+ reserved.forEach(key -> reservedUserGroups.merge(key, 1, Integer::sum));+ return reserved;+ }+ }++ private void releaseUserGroups(List<String> reserved) {+ synchronized (userGroupLock) {+ reserved.forEach(key -> reservedUserGroups.computeIfPresent(key, (k, count) -> count > 1 ? count - 1 : null));+ }+ }++ // user groups are stored per account, so two accounts can each hold a group of the same id+ private String userGroupReservationKey(String userGroupId) {+ return userGroups.accountId() + "/" + userGroupId;+ }++ private ReplicationGroup createReplicationGroupWithSettings(CreateReplicationGroupRequest request,+ ReplicationGroupSettings resolvedSettings) {+ String groupId = request.replicationGroupId();// Held until the group is persisted (or the attempt fails) so a concurrent// DeleteCacheParameterGroup sees the dependency before the stored-groups scan can.String parameterGroupReservation = reserveParameterGroup(request.cacheParameterGroupName());@@ -412,9 +474,10 @@ public class ElastiCacheService implements ResourceProvider {private void applyCommonAttributes(ReplicationGroup group, CreateReplicationGroupRequest request,ReplicationGroupSettings resolvedSettings) {- group.setEngine(request.engine() != null && !request.engine().isBlank()- ? normalizeEngine(request.engine())- : defaultEngineForImage());+ group.setEngine(engineFor(request));+ if (request.userGroupIds() != null) {+ request.userGroupIds().forEach(id -> group.getUserGroupIds().add(normalizeUserGroupId(id)));+ }group.setEngineVersion(request.engineVersion() != null && !request.engineVersion().isBlank()? request.engineVersion(): defaultEngineVersion(group.getEngine()));@@ -432,6 +495,12 @@ public class ElastiCacheService implements ResourceProvider {}}+ private String engineFor(CreateReplicationGroupRequest request) {+ return request.engine() != null && !request.engine().isBlank()+ ? normalizeEngine(request.engine())+ : defaultEngineForImage();+ }+private static String defaultEngineVersion(String engine) {return "valkey".equals(engine) ? "8.1" : "7.1";}@@ -1452,50 +1521,98 @@ public class ElastiCacheService implements ResourceProvider {return members;}- public ReplicationGroup modifyReplicationGroup(String groupId, List<String> userIdsToAdd,- List<String> userIdsToRemove) {- return modifyReplicationGroup(groupId, userIdsToAdd, userIdsToRemove,+ public ReplicationGroup modifyReplicationGroup(String groupId, List<String> userGroupIdsToAdd,+ List<String> userGroupIdsToRemove) {+ return modifyReplicationGroup(groupId, userGroupIdsToAdd, userGroupIdsToRemove,ReplicationGroupSettings.unchanged());}- public ReplicationGroup modifyReplicationGroup(String groupId, List<String> userIdsToAdd,- List<String> userIdsToRemove,+ /**+ * Each id in {@code userGroupIdsToAdd} names a user group. An id that names no user group but+ * does name a user associates that user directly, which is how Floci accepted the list before+ * it modelled user groups; AWS itself only takes user group ids here.+ */+ public ReplicationGroup modifyReplicationGroup(String groupId, List<String> userGroupIdsToAdd,+ List<String> userGroupIdsToRemove,+ ReplicationGroupSettings settings) {+ return modifyReplicationGroup(groupId, userGroupIdsToAdd, userGroupIdsToRemove, false, false, settings);+ }++ /**+ * {@code removeUserGroups} drops every user group the cache had before this request, and+ * {@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.+ */+ public ReplicationGroup modifyReplicationGroup(String groupId, List<String> userGroupIdsToAdd,+ List<String> userGroupIdsToRemove,+ boolean removeUserGroups, boolean deleteAuthToken,ReplicationGroupSettings settings) {settings.validate();synchronized (lockFor("rg:" + groupId)) {ReplicationGroup group = getReplicationGroup(groupId);// every check before any change: the store hands out its own object, so a mutation// made before a later refusal would stay visible- Set<String> nextUserIds = new HashSet<>(group.getAssociatedUserIds());- if (userIdsToRemove != null) {- nextUserIds.removeAll(userIdsToRemove);- }- if (userIdsToAdd != null) {- for (String userId : userIdsToAdd) {- getUser(userId);+ synchronized (userGroupLock) {+ Set<String> userGroupsToAdd = new LinkedHashSet<>();+ Set<String> directUsersToAdd = new LinkedHashSet<>();+ if (userGroupIdsToAdd != null) {+ for (String id : userGroupIdsToAdd) {+ Optional<ElastiCacheUserGroup> userGroup = userGroups.get(normalizeUserGroupId(id));+ if (userGroup.isPresent()) {+ requireAssociable(userGroup.get(), group.isEncryptedInTransit(), group.getEngine());+ userGroupsToAdd.add(userGroup.get().getUserGroupId());+ } else if (users.get(id).isPresent()) {+ directUsersToAdd.add(id);+ } else {+ throw userGroupNotFound(id);+ }+ }}- nextUserIds.addAll(userIdsToAdd);- }- Set<String> seenUserNames = new HashSet<>();- for (String userId : nextUserIds) {- ElastiCacheUser u = users.get(userId).orElse(null);- if (u != null && !seenUserNames.add(u.getUserName())) {- throw new AwsException("DuplicateUserNameFault",- "Duplicate user name " + u.getUserName() + " in user group.", 400);+ Set<String> nextUserGroupIds = new LinkedHashSet<>(removeUserGroups ? Set.of() : group.getUserGroupIds());+ nextUserGroupIds.addAll(userGroupsToAdd);+ if (userGroupIdsToRemove != null) {+ userGroupIdsToRemove.forEach(id -> nextUserGroupIds.remove(normalizeUserGroupId(id)));+ }+ if (deleteAuthToken && nextUserGroupIds.isEmpty()) {+ throw new AwsException("InvalidParameterCombination",+ "AuthTokenUpdateStrategy DELETE is allowed only when transitioning to RBAC: "+ + "add a user group in the same request.", 400);}- }- settings.applyTo(group);- if (userIdsToAdd != null) {- group.getAssociatedUserIds().addAll(userIdsToAdd);- }- if (userIdsToRemove != null) {- group.getAssociatedUserIds().removeAll(userIdsToRemove);- }+ Set<String> nextUserIds = new HashSet<>(group.getAssociatedUserIds());+ nextUserIds.addAll(directUsersToAdd);+ if (userGroupIdsToRemove != null) {+ nextUserIds.removeAll(userGroupIdsToRemove);+ }+ Set<String> seenUserNames = new HashSet<>();+ for (String userId : nextUserIds) {+ ElastiCacheUser u = users.get(userId).orElse(null);+ if (u != null && !seenUserNames.add(u.getUserName())) {+ throw new AwsException("DuplicateUserNameFault",+ "Duplicate user name " + u.getUserName() + " in user group.", 400);+ }+ }- groups.put(groupId, group);- return group;+ boolean hadUserGroups = !group.getUserGroupIds().isEmpty();+ settings.applyTo(group);+ group.setUserGroupIds(nextUserGroupIds);+ group.getAssociatedUserIds().addAll(directUsersToAdd);+ if (userGroupIdsToRemove != null) {+ group.getAssociatedUserIds().removeAll(userGroupIdsToRemove);+ }+ if (deleteAuthToken) {+ group.setAuthToken(null);+ }+ if (hadUserGroups && nextUserGroupIds.isEmpty()) {+ group.setTransitEncryptionEnabled(group.isEncryptedInTransit());+ group.setAuthMode(group.getAuthToken() != null ? AuthMode.PASSWORD : AuthMode.NO_AUTH);+ }++ groups.put(groupId, group);+ return group;+ }}}@@ -1582,30 +1699,50 @@ public class ElastiCacheService implements ResourceProvider {// Storage backends hand back the live stored object, so validate everything// before the first setter — a rejected request must not leave changes behind.String normalizedEngine = (engine == null || engine.isBlank()) ? null : normalizeEngine(engine);- if (authMode != null) {- user.setAuthMode(authMode);- user.setPasswords(passwords != null ? passwords : List.of());- }- if (accessString != null) {- user.setAccessString(accessString);- } else if (appendAccessString != null) {- String current = user.getAccessString();- user.setAccessString(current == null || current.isBlank()- ? appendAccessString- : current + " " + appendAccessString);- }- if (normalizedEngine != null) {- user.setEngine(normalizedEngine);+ synchronized (userGroupLock) {+ String nextEngine = normalizedEngine != null ? normalizedEngine : user.getEngine();+ AuthMode nextAuthMode = authMode != null ? authMode : user.getAuthMode();+ if (!nextEngine.equals(user.getEngine()) || nextAuthMode != user.getAuthMode()) {+ // the user must still satisfy every user group that holds it+ for (ElastiCacheUserGroup userGroup : userGroups.scan(k -> true)) {+ if (userGroup.getUserIds().contains(userId)) {+ requireMemberAllowed(userGroup.getEngine(), userId, nextEngine, nextAuthMode);+ }+ }+ }+ if (authMode != null) {+ user.setAuthMode(authMode);+ user.setPasswords(passwords != null ? passwords : List.of());+ }+ if (accessString != null) {+ user.setAccessString(accessString);+ } else if (appendAccessString != null) {+ String current = user.getAccessString();+ user.setAccessString(current == null || current.isBlank()+ ? appendAccessString+ : current + " " + appendAccessString);+ }+ if (normalizedEngine != null) {+ user.setEngine(normalizedEngine);+ }+ users.put(userId, user);+ return user;}- users.put(userId, user);- return user;}public void deleteUser(String userId) {if (users.get(userId).isEmpty()) {throw new AwsException("UserNotFoundFault", "User " + userId + " not found.", 404);}- users.delete(userId);+ synchronized (userGroupLock) {+ users.delete(userId);+ // AWS removes a deleted user from every user group it belongs to+ for (ElastiCacheUserGroup userGroup : userGroups.scan(k -> true)) {+ if (userGroup.getUserIds().remove(userId)) {+ userGroups.put(userGroup.getUserGroupId(), userGroup);+ }+ }+ }for (ReplicationGroup group : groups.scan(k -> true)) {synchronized (lockFor("rg:" + group.getReplicationGroupId())) {if (group.getAssociatedUserIds().remove(userId)) {@@ -1616,6 +1753,178 @@ public class ElastiCacheService implements ResourceProvider {LOG.infov("ElastiCache user {0} deleted", userId);}+ // ── User groups ───────────────────────────────────────────────────────────++ public ElastiCacheUserGroup createUserGroup(String userGroupId, String engine, List<String> userIds,+ String region) {+ String id = normalizeUserGroupId(userGroupId);+ // Engine is required on AWS; a missing value defaults to redis as CreateUser does.+ String normalizedEngine = (engine == null || engine.isBlank()) ? "redis" : normalizeEngine(engine);+ Set<String> members = new LinkedHashSet<>(userIds != null ? userIds : List.of());+ synchronized (userGroupLock) {+ validateUserGroupMembers(normalizedEngine, members);+ if (userGroups.get(id).isPresent()) {+ throw new AwsException("UserGroupAlreadyExists",+ "User group " + id + " already exists.", 400);+ }+ ElastiCacheUserGroup userGroup = new ElastiCacheUserGroup(+ id, normalizedEngine, members, "active", Instant.now());+ userGroup.setArn(regionResolver.buildArn("elasticache", region, "usergroup:" + id));+ userGroups.put(id, userGroup);+ LOG.infov("ElastiCache user group {0} created with {1} users", id, members.size());+ return userGroup;+ }+ }++ public ElastiCacheUserGroup getUserGroup(String userGroupId) {+ return userGroups.get(normalizeUserGroupId(userGroupId)).orElseThrow(() -> userGroupNotFound(userGroupId));+ }++ public Collection<ElastiCacheUserGroup> listUserGroups(String filterUserGroupId) {+ if (filterUserGroupId != null && !filterUserGroupId.isBlank()) {+ return List.of(getUserGroup(filterUserGroupId));+ }+ return userGroups.scan(k -> true);+ }++ public ElastiCacheUserGroup modifyUserGroup(String userGroupId, List<String> userIdsToAdd,+ List<String> userIdsToRemove, String engine) {+ String id = normalizeUserGroupId(userGroupId);+ synchronized (userGroupLock) {+ ElastiCacheUserGroup userGroup = getUserGroup(id);+ // Validate the resulting group before changing the stored object.+ String newEngine = (engine == null || engine.isBlank()) ? userGroup.getEngine() : normalizeEngine(engine);+ Set<String> members = new LinkedHashSet<>(userGroup.getUserIds());+ if (userIdsToAdd != null) {+ members.addAll(userIdsToAdd);+ }+ if (userIdsToRemove != null) {+ members.removeAll(userIdsToRemove);+ }+ validateUserGroupMembers(newEngine, members);+ if (!newEngine.equals(userGroup.getEngine())) {+ for (ReplicationGroup group : replicationGroupsUsing(id)) {+ requireEngineCompatible(newEngine, id, group.getEngine());+ }+ }+ userGroup.setEngine(newEngine);+ userGroup.setUserIds(members);+ userGroups.put(id, userGroup);+ return userGroup;+ }+ }++ public ElastiCacheUserGroup deleteUserGroup(String userGroupId) {+ String id = normalizeUserGroupId(userGroupId);+ synchronized (userGroupLock) {+ ElastiCacheUserGroup userGroup = getUserGroup(id);+ List<String> replicationGroupIds = replicationGroupIdsUsing(id);+ if (!replicationGroupIds.isEmpty() || reservedUserGroups.containsKey(userGroupReservationKey(id))) {+ throw new AwsException("InvalidUserGroupState",+ "User group " + id + " is in use by a replication group. Disassociate it before deleting it.", 400);+ }+ userGroups.delete(id);+ userGroup.setStatus("deleting");+ LOG.infov("ElastiCache user group {0} deleted", id);+ return userGroup;+ }+ }++ public List<String> replicationGroupIdsUsing(String userGroupId) {+ return replicationGroupsUsing(normalizeUserGroupId(userGroupId)).stream()+ .map(ReplicationGroup::getReplicationGroupId)+ .toList();+ }++ public List<String> userGroupIdsContaining(String userId) {+ return userGroups.scan(k -> true).stream()+ .filter(g -> g.getUserIds().contains(userId))+ .map(ElastiCacheUserGroup::getUserGroupId)+ .toList();+ }++ private List<ReplicationGroup> replicationGroupsUsing(String normalizedUserGroupId) {+ return groups.scan(k -> true).stream()+ .filter(g -> g.getUserGroupIds().contains(normalizedUserGroupId))+ .toList();+ }++ /**+ * The users that can authenticate against a replication group: the members of its user+ * groups, read at the time of the check so a ModifyUserGroup applies at once, plus any user+ * associated with it directly.+ */+ private Set<String> effectiveUserIds(ReplicationGroup group) {+ Set<String> userIds = new LinkedHashSet<>(group.getAssociatedUserIds());+ for (String userGroupId : group.getUserGroupIds()) {+ userGroups.get(userGroupId).ifPresent(g -> userIds.addAll(g.getUserIds()));+ }+ return userIds;+ }++ /**+ * A redis user group must contain a user named {@code default}; a valkey one need not, but+ * cannot contain a user without authentication. Valkey users only join valkey user groups,+ * and two members cannot share a user name.+ */+ private void validateUserGroupMembers(String engine, Set<String> userIds) {+ Set<String> userNames = new HashSet<>();+ boolean hasDefaultUser = false;+ for (String userId : userIds) {+ ElastiCacheUser user = getUser(userId);+ requireMemberAllowed(engine, userId, user.getEngine(), user.getAuthMode());+ if (!userNames.add(user.getUserName())) {+ throw new AwsException("DuplicateUserName",+ "More than one user in the user group has the user name " + user.getUserName() + ".", 400);+ }+ hasDefaultUser |= "default".equals(user.getUserName());+ }+ if ("redis".equals(engine) && !hasDefaultUser) {+ throw new AwsException("DefaultUserRequired", "You must add default user to a user group.", 400);+ }+ }++ private static void requireMemberAllowed(String userGroupEngine, String userId, String userEngine,+ AuthMode authMode) {+ if ("redis".equals(userGroupEngine) && "valkey".equals(userEngine)) {+ throw new AwsException("InvalidParameterValue",+ "User " + userId + " uses the valkey engine and can only belong to valkey user groups.", 400);+ }+ if ("valkey".equals(userGroupEngine) && authMode == AuthMode.NO_AUTH) {+ throw new AwsException("InvalidParameterValue",+ "User " + userId + " requires no password and cannot belong to a valkey user group.", 400);+ }+ }++ /** RBAC needs in-transit encryption, and a valkey user group only serves valkey caches. */+ private static void requireAssociable(ElastiCacheUserGroup userGroup, boolean encryptedInTransit, String engine) {+ if (!encryptedInTransit) {+ throw new AwsException("InvalidParameterCombination",+ "User group " + userGroup.getUserGroupId()+ + " can only be associated with a replication group that has in-transit encryption enabled.", 400);+ }+ requireEngineCompatible(userGroup.getEngine(), userGroup.getUserGroupId(), engine);+ }++ private static void requireEngineCompatible(String userGroupEngine, String userGroupId, String replicationGroupEngine) {+ if ("valkey".equals(userGroupEngine) && !"valkey".equals(replicationGroupEngine)) {+ throw new AwsException("InvalidParameterCombination",+ "User group " + userGroupId + " uses the valkey engine and can only be associated with a valkey cache.", 400);+ }+ }++ private static String normalizeUserGroupId(String userGroupId) {+ if (userGroupId == null || userGroupId.isBlank()) {+ throw new AwsException("InvalidParameterValue", "UserGroupId is required.", 400);+ }+ // AWS stores the id as a lowercase string+ return userGroupId.toLowerCase(Locale.ROOT);+ }++ private static AwsException userGroupNotFound(String userGroupId) {+ return new AwsException("UserGroupNotFound", "User group " + userGroupId + " not found.", 404);+ }+/*** Validates a Redis AUTH password for the given group.* Checks the group-level authToken first, then falls back to the "default" user@@ -1630,12 +1939,13 @@ public class ElastiCacheService implements ResourceProvider {}if (username == null || username.isEmpty() || "default".equals(username)) {- // AUTH password form: check group-level authToken first- if (group.getAuthToken() != null && password.equals(group.getAuthToken())) {+ // AUTH password form: the group-level authToken first, unless user groups replaced it+ if (group.getUserGroupIds().isEmpty() && group.getAuthToken() != null+ && password.equals(group.getAuthToken())) {return true;}- // Fall back to the "default" user associated with this group- Set<String> groupUserIds = group.getAssociatedUserIds();+ // Fall back to the "default" user among this group's members+ Set<String> groupUserIds = effectiveUserIds(group);ElastiCacheUser defaultUser = groupUserIds.stream().map(id -> users.get(id).orElse(null)).filter(u -> u != null && "default".equals(u.getUserName()))@@ -1653,7 +1963,7 @@ public class ElastiCacheService implements ResourceProvider {return false;}// AUTH username password form: find user by userName, scoped to group- Set<String> groupUserIds = group.getAssociatedUserIds();+ Set<String> groupUserIds = effectiveUserIds(group);ElastiCacheUser targetUser = groupUserIds.stream().map(id -> users.get(id).orElse(null)).filter(u -> u != null && username.equals(u.getUserName()))@@ -1671,11 +1981,18 @@ public class ElastiCacheService implements ResourceProvider {return false;}+ /**+ * Whether the proxy authenticates each user by its own mode. A user group switches that on+ * even while it has no members, since RBAC then admits nobody rather than every caller.+ */public boolean hasMembers(String groupId) {ReplicationGroup group = groups.get(groupId).orElse(null);if (group == null) {return false;}+ if (!group.getUserGroupIds().isEmpty()) {+ return true;+ }return group.getAssociatedUserIds().stream().anyMatch(id -> users.get(id).isPresent());}@@ -1685,7 +2002,7 @@ public class ElastiCacheService implements ResourceProvider {return null;}String target = (username == null || username.isEmpty()) ? "default" : username;- return group.getAssociatedUserIds().stream()+ return effectiveUserIds(group).stream().map(id -> users.get(id).orElse(null)).filter(u -> u != null && target.equals(u.getUserName()) && u.isEnabled()).map(ElastiCacheUser::getAuthMode)@@ -1709,6 +2026,11 @@ public class ElastiCacheService implements ResourceProvider {public AuthMode memberAuthMode(String username) {return ElastiCacheService.this.memberAuthMode(groupId, username);}++ @Override+ public AuthMode authMode() {+ return groups.get(groupId).map(ReplicationGroup::getAuthMode).orElse(null);+ }};}diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/model/ElastiCacheUserGroup.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/model/ElastiCacheUserGroup.javanew file mode 100644index 0000000000..54f686721a--- /dev/null+++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/model/ElastiCacheUserGroup.java@@ -0,0 +1,50 @@+package io.github.hectorvent.floci.services.elasticache.model;++import io.quarkus.runtime.annotations.RegisterForReflection;++import java.time.Instant;+import java.util.LinkedHashSet;+import java.util.Set;++@RegisterForReflection+public class ElastiCacheUserGroup {++ private String userGroupId;+ // "redis" or "valkey"+ private String engine = "redis";+ private String status;+ private Set<String> userIds = new LinkedHashSet<>();+ private Instant createdAt;+ private String arn;++ public ElastiCacheUserGroup() {}++ public ElastiCacheUserGroup(String userGroupId, String engine, Set<String> userIds,+ String status, Instant createdAt) {+ this.userGroupId = userGroupId;+ this.engine = engine;+ this.userIds = userIds != null ? userIds : new LinkedHashSet<>();+ this.status = status;+ this.createdAt = createdAt;+ }++ public String getUserGroupId() { return userGroupId; }+ public void setUserGroupId(String userGroupId) { this.userGroupId = userGroupId; }++ public String getEngine() { return engine; }+ public void setEngine(String engine) { this.engine = engine; }++ public String getStatus() { return status; }+ public void setStatus(String status) { this.status = status; }++ public Set<String> getUserIds() { return userIds; }+ public void setUserIds(Set<String> userIds) {+ this.userIds = userIds != null ? userIds : new LinkedHashSet<>();+ }++ public Instant getCreatedAt() { return createdAt; }+ public void setCreatedAt(Instant createdAt) { this.createdAt = createdAt; }++ public String getArn() { return arn; }+ public void setArn(String arn) { this.arn = arn; }+}diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/model/ReplicationGroup.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/model/ReplicationGroup.javaindex 92875f9ff5..1d762a40f6 100644--- a/src/main/java/io/github/hectorvent/floci/services/elasticache/model/ReplicationGroup.java+++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/model/ReplicationGroup.java@@ -1,11 +1,13 @@package io.github.hectorvent.floci.services.elasticache.model;+import com.fasterxml.jackson.annotation.JsonIgnore;import io.quarkus.runtime.annotations.RegisterForReflection;import java.time.Instant;import java.util.ArrayList;import java.util.HashSet;import java.util.LinkedHashMap;+import java.util.LinkedHashSet;import java.util.List;import java.util.Map;import java.util.Set;@@ -21,7 +23,14 @@ public class ReplicationGroup {private Instant createdAt;private int proxyPort;private String authToken; // stored plain-text for PASSWORD auth validation in the proxy+ /**+ * Set once the auth mode stops telling: a group whose last user group was removed has no+ * access control but is still encrypted in transit. Null otherwise, see {@link #isEncryptedInTransit}.+ */+ private Boolean transitEncryptionEnabled;+ /** Users associated directly by user id, which Floci accepted before it modelled user groups. */private Set<String> associatedUserIds = new HashSet<>();+ private Set<String> userGroupIds = new LinkedHashSet<>();private String arn;private String region;private boolean atRestEncryptionEnabled;@@ -85,11 +94,27 @@ public class ReplicationGroup {public String getAuthToken() { return authToken; }public void setAuthToken(String authToken) { this.authToken = authToken; }+ public Boolean getTransitEncryptionEnabled() { return transitEncryptionEnabled; }+ public void setTransitEncryptionEnabled(Boolean transitEncryptionEnabled) {+ this.transitEncryptionEnabled = transitEncryptionEnabled;+ }++ /** An auth token or IAM authentication implies encryption in transit, which Floci records as the auth mode. */+ @JsonIgnore+ public boolean isEncryptedInTransit() {+ return transitEncryptionEnabled != null ? transitEncryptionEnabled : authMode != AuthMode.NO_AUTH;+ }+public Set<String> getAssociatedUserIds() { return associatedUserIds; }public void setAssociatedUserIds(Set<String> associatedUserIds) {this.associatedUserIds = associatedUserIds != null ? associatedUserIds : new HashSet<>();}+ public Set<String> getUserGroupIds() { return userGroupIds; }+ public void setUserGroupIds(Set<String> userGroupIds) {+ this.userGroupIds = userGroupIds != null ? userGroupIds : new LinkedHashSet<>();+ }+public String getContainerId() { return containerId; }public void setContainerId(String containerId) { this.containerId = containerId; }diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/ElastiCacheAuthProxy.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/ElastiCacheAuthProxy.javaindex 623d9c1a8c..77bf01b053 100644--- a/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/ElastiCacheAuthProxy.java+++ b/src/main/java/io/github/hectorvent/floci/services/elasticache/proxy/ElastiCacheAuthProxy.java@@ -40,7 +40,7 @@ public class ElastiCacheAuthProxy extends AbstractRedisAuthProxy {}return true;}- return authMode != AuthMode.NO_AUTH;+ return currentAuthMode() != AuthMode.NO_AUTH;}@Override@@ -74,13 +74,19 @@ public class ElastiCacheAuthProxy extends AbstractRedisAuthProxy {return false;}- return switch (authMode) {+ return switch (currentAuthMode()) {case IAM -> sigV4Validator.validate(password, groupId, effectiveUser);case PASSWORD -> passwordValidator.validatePassword(username, password);case NO_AUTH -> true;};}+ // read per connection: removing a cache's last user group changes it while the proxy runs+ private AuthMode currentAuthMode() {+ AuthMode current = passwordValidator.authMode();+ return current != null ? current : authMode;+ }+@Overrideprotected void closeQuietly(Socket s) {try {@@ -111,5 +117,10 @@ public class ElastiCacheAuthProxy extends AbstractRedisAuthProxy {default AuthMode memberAuthMode(String username) {return null;}++ /** The cache's auth mode now, or null to keep the one the proxy started with. */+ default AuthMode authMode() {+ return null;+ }}}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.javaindex 49f468c729..1dc409e913 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@@ -412,7 +412,8 @@ class ElastiCacheQueryHandlerTest {@Testvoid modifyReplicationGroup_passesSnapshotSettingsOnly() {- when(service.modifyReplicationGroup(eq("g1"), isNull(), isNull(), any())).thenReturn(group("g1"));+ when(service.modifyReplicationGroup(eq("g1"), isNull(), isNull(), eq(false), eq(false), any()))+ .thenReturn(group("g1"));MultivaluedMap<String, String> p = params();p.add("ReplicationGroupId", "g1");p.add("SnapshotRetentionLimit", "3");@@ -421,7 +422,39 @@ class ElastiCacheQueryHandlerTest {p.add("KmsKeyId", "alias/other");assertEquals(200, handler.handle("ModifyReplicationGroup", p, "us-east-1").getStatus());- verify(service).modifyReplicationGroup("g1", null, null, new ReplicationGroupSettings(null, null, 3, "01:00-02:00"));+ verify(service).modifyReplicationGroup("g1", null, null, false, false,+ new ReplicationGroupSettings(null, null, 3, "01:00-02:00"));+ }++ @Test+ void modifyReplicationGroup_passesRemoveUserGroupsAndAuthTokenDelete() {+ when(service.modifyReplicationGroup(eq("g1"), any(), isNull(), eq(true), eq(true), any()))+ .thenReturn(group("g1"));+ MultivaluedMap<String, String> p = params();+ p.add("ReplicationGroupId", "g1");+ p.add("UserGroupIdsToAdd.member.1", "new-users");+ p.add("RemoveUserGroups", "true");+ p.add("AuthTokenUpdateStrategy", "DELETE");++ 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));+ }++ @Test+ void describeReplicationGroups_reportsNoAuthTokenOnceAUserGroupIsAttached() {+ ReplicationGroup g = new ReplicationGroup("g1", "d", ReplicationGroupStatus.AVAILABLE,+ AuthMode.PASSWORD, new Endpoint("localhost", 6379), Instant.now(), 6379);+ g.setAuthToken("group-token");+ g.getUserGroupIds().add("app-users");+ when(service.listReplicationGroups("g1")).thenReturn(List.of(g));+ MultivaluedMap<String, String> p = params();+ p.add("ReplicationGroupId", "g1");++ String body = (String) handler.handle("DescribeReplicationGroups", p, "us-east-1").getEntity();++ assertTrue(body.contains("<AuthTokenEnabled>false</AuthTokenEnabled>"), body);+ assertTrue(body.contains("<TransitEncryptionEnabled>true</TransitEncryptionEnabled>"), body);}// ── CreateCacheCluster / DescribeCacheClusters: single-node redis ─────────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.javaindex aa615c6429..bc11ccb930 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@@ -17,6 +17,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.ClusterNode;+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 io.github.hectorvent.floci.services.elasticache.model.ReplicationGroup;import io.github.hectorvent.floci.services.elasticache.model.ReplicationGroupSettings;@@ -33,6 +35,7 @@ import java.time.Instant;import java.util.List;import java.util.Map;import java.util.Optional;+import java.util.Set;import java.util.concurrent.CompletableFuture;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.CountDownLatch;@@ -181,6 +184,249 @@ class ElastiCacheServiceTest {assertFalse(service.validatePassword("grp", "wrong", "other-pass"));}+ @Test+ void userGroupMembersAuthenticateAndMembershipChangesApplyAtOnce() {+ service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, null, "us-east-1");+ 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);+ ElastiCacheUserGroup userGroup = service.createUserGroup("App-Group", "redis",+ List.of("default-user-id", "app-user-id"), "us-east-1");+ assertEquals("app-group", userGroup.getUserGroupId());++ service.modifyReplicationGroup("grp", List.of("App-Group"), null);+ ReplicationGroup group = service.getReplicationGroup("grp");+ assertEquals(Set.of("app-group"), group.getUserGroupIds());+ assertTrue(group.getAssociatedUserIds().isEmpty());+ assertTrue(service.validatePassword("grp", "app", "app-pass"));+ assertTrue(service.validatePassword("grp", null, "default-pass"));++ service.modifyUserGroup("app-group", null, List.of("app-user-id"), null);+ assertFalse(service.validatePassword("grp", "app", "app-pass"));+ service.modifyUserGroup("app-group", List.of("app-user-id"), null, null);+ assertTrue(service.validatePassword("grp", "app", "app-pass"));++ AwsException inUse = assertThrows(AwsException.class, () -> service.deleteUserGroup("app-group"));+ assertEquals("InvalidUserGroupState", inUse.getErrorCode());++ service.modifyReplicationGroup("grp", null, List.of("app-group"));+ assertFalse(service.validatePassword("grp", "app", "app-pass"));+ service.deleteUserGroup("app-group");+ assertEquals("UserGroupNotFound",+ assertThrows(AwsException.class, () -> service.getUserGroup("app-group")).getErrorCode());+ }++ @Test+ void userGroupNeedsInTransitEncryptionOnTheReplicationGroup() {+ service.createReplicationGroup("open", "test", AuthMode.NO_AUTH, null, "us-east-1");+ service.createUser("default-user-id", "default", AuthMode.PASSWORD,+ List.of("default-pass"), "on ~* +@all", null);+ service.createUserGroup("grp-users", "redis", List.of("default-user-id"), "us-east-1");++ AwsException refused = assertThrows(AwsException.class,+ () -> service.modifyReplicationGroup("open", List.of("grp-users"), null));+ assertEquals("InvalidParameterCombination", refused.getErrorCode());+ assertTrue(service.getReplicationGroup("open").getUserGroupIds().isEmpty());+ }++ @Test+ void createReplicationGroupRejectsAnUnknownUserGroupBeforeProvisioning() {+ 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"));++ AwsException refused = assertThrows(AwsException.class, () -> service.createReplicationGroup(request));++ assertEquals("UserGroupNotFound", refused.getErrorCode());+ verify(containerManager, never()).tryStart(anyString(), anyString(), any());+ }++ @Test+ void userGroupMembersDecideEachUsersAuthenticationMode() {+ service.createReplicationGroup("grp", "test", AuthMode.IAM, null, "us-east-1");+ assertFalse(service.hasMembers("grp"),+ "a cache with no users associated keeps the group level authentication mode");++ service.createUser("default-user-id", "default", AuthMode.PASSWORD,+ List.of("default-pass"), "on ~* +@all", null);+ service.createUser("iam-user-id", "iam-app", AuthMode.IAM, List.of(), "on ~* +@all", null);+ service.createUserGroup("iam-users", "redis", List.of("default-user-id", "iam-user-id"), "us-east-1");+ service.modifyReplicationGroup("grp", List.of("iam-users"), null);++ assertTrue(service.hasMembers("grp"));+ assertEquals(AuthMode.IAM, service.memberAuthMode("grp", "iam-app"));+ assertEquals(AuthMode.PASSWORD, service.memberAuthMode("grp", "default"),+ "a password user cannot connect with an IAM token");+ assertNull(service.memberAuthMode("grp", "anyone"));++ service.modifyUserGroup("iam-users", null, List.of("iam-user-id"), null);+ assertNull(service.memberAuthMode("grp", "iam-app"), "removing the member revokes its access");+ }++ @Test+ 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));+ service.createUserGroup("empty-users", "valkey", List.of(), "us-east-1");+ service.modifyReplicationGroup("grp", List.of("empty-users"), null);++ assertTrue(service.hasMembers("grp"));+ assertNull(service.memberAuthMode("grp", "default"));+ assertNull(service.memberAuthMode("grp", "anyone"));+ }++ @Test+ void aCacheOnUserGroupsNoLongerTakesItsAuthToken() {+ service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, "group-token", "us-east-1");+ service.createUser("default-user-id", "default", AuthMode.PASSWORD,+ List.of("default-pass"), "on ~* +@all", null);+ service.createUserGroup("redis-users", "redis", List.of("default-user-id"), "us-east-1");+ assertTrue(service.validatePassword("grp", null, "group-token"));++ service.modifyReplicationGroup("grp", List.of("redis-users"), null);++ assertFalse(service.validatePassword("grp", null, "group-token"));+ assertTrue(service.validatePassword("grp", null, "default-pass"));+ }++ @Test+ void authTokenDeleteIsAllowedOnlyWhenMovingToUserGroups() {+ service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, "group-token", "us-east-1");+ service.createUser("default-user-id", "default", AuthMode.PASSWORD,+ List.of("default-pass"), "on ~* +@all", null);+ 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()));+ assertEquals("InvalidParameterCombination", refused.getErrorCode());+ assertEquals("group-token", service.getReplicationGroup("grp").getAuthToken());++ service.modifyReplicationGroup("grp", List.of("redis-users"), null, false, true,+ ReplicationGroupSettings.unchanged());+ assertNull(service.getReplicationGroup("grp").getAuthToken());++ service.modifyReplicationGroup("grp", null, List.of("redis-users"));+ ReplicationGroup group = service.getReplicationGroup("grp");+ assertEquals(AuthMode.NO_AUTH, group.getAuthMode(), "no user group and no token leaves no access control");+ assertTrue(group.isEncryptedInTransit());++ service.modifyReplicationGroup("grp", List.of("redis-users"), null);+ assertEquals(Set.of("redis-users"), service.getReplicationGroup("grp").getUserGroupIds());+ }++ @Test+ void removeUserGroupsDropsEveryUserGroupAndTheAccessControl() {+ service.createReplicationGroup("grp", "test", AuthMode.IAM, null, "us-east-1");+ service.createUser("default-user-id", "default", AuthMode.PASSWORD,+ List.of("default-pass"), "on ~* +@all", null);+ service.createUserGroup("first-users", "redis", List.of("default-user-id"), "us-east-1");+ 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());++ ReplicationGroup group = service.getReplicationGroup("grp");+ assertTrue(group.getUserGroupIds().isEmpty());+ assertEquals(AuthMode.NO_AUTH, group.getAuthMode());+ assertTrue(group.isEncryptedInTransit());+ assertFalse(service.hasMembers("grp"));+ }++ @Test+ void aTokenKeptThroughUserGroupsAppliesAgainOnceTheyAreRemoved() {+ service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, "group-token", "us-east-1");+ service.createUser("default-user-id", "default", AuthMode.PASSWORD,+ List.of("default-pass"), "on ~* +@all", null);+ service.createUserGroup("redis-users", "redis", List.of("default-user-id"), "us-east-1");+ service.modifyReplicationGroup("grp", List.of("redis-users"), null);++ service.modifyReplicationGroup("grp", null, List.of("redis-users"));++ assertEquals(AuthMode.PASSWORD, service.getReplicationGroup("grp").getAuthMode());+ assertTrue(service.validatePassword("grp", null, "group-token"));+ }++ @Test+ void failedCreateReleasesItsUserGroupReservation() {+ service.createUser("default-user-id", "default", AuthMode.PASSWORD,+ List.of("default-pass"), "on ~* +@all", null);+ service.createUserGroup("reserved", "redis", List.of("default-user-id"), "us-east-1");+ when(containerManager.tryStart(anyString(), anyString(), any())).thenThrow(new RuntimeException("docker down"));+ 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"));++ assertThrows(RuntimeException.class, () -> service.createReplicationGroup(request));++ service.deleteUserGroup("reserved");+ assertEquals("UserGroupNotFound",+ assertThrows(AwsException.class, () -> service.getUserGroup("reserved")).getErrorCode());+ }++ @Test+ void modifyUserCannotMoveAMemberToAnEngineItsUserGroupRejects() {+ service.createUser("default-user-id", "default", AuthMode.PASSWORD,+ List.of("default-pass"), "on ~* +@all", null);+ service.createUserGroup("redis-users", "redis", List.of("default-user-id"), "us-east-1");++ AwsException refused = assertThrows(AwsException.class,+ () -> service.modifyUser("default-user-id", null, null, null, null, "valkey"));++ assertEquals("InvalidParameterValue", refused.getErrorCode());+ assertEquals("redis", service.getUser("default-user-id").getEngine());+ }++ @Test+ void modifyUserCannotDropThePasswordOfAValkeyUserGroupMember() {+ service.createUser("valkey-user-id", "vk", AuthMode.PASSWORD,+ List.of("vk-pass"), "on ~* +@all", "valkey");+ service.createUserGroup("vk-group", "valkey", List.of("valkey-user-id"), "us-east-1");++ AwsException refused = assertThrows(AwsException.class,+ () -> service.modifyUser("valkey-user-id", AuthMode.NO_AUTH, null, null, null, null));++ assertEquals("InvalidParameterValue", refused.getErrorCode());+ ElastiCacheUser user = service.getUser("valkey-user-id");+ assertEquals(AuthMode.PASSWORD, user.getAuthMode());+ assertEquals(List.of("vk-pass"), user.getPasswords());+ }++ @Test+ void userGroupMembershipFollowsAwsRules() {+ service.createUser("default-user-id", "default", AuthMode.PASSWORD,+ List.of("default-pass"), "on ~* +@all", null);+ service.createUser("second-default-id", "default", AuthMode.PASSWORD,+ List.of("other-pass"), "on ~* +@all", null);+ service.createUser("app-user-id", "app", AuthMode.PASSWORD,+ List.of("app-pass"), "on ~* +@all", null);+ service.createUser("valkey-user-id", "vk", AuthMode.PASSWORD,+ List.of("vk-pass"), "on ~* +@all", "valkey");+ service.createUser("open-user-id", "open", AuthMode.NO_AUTH, List.of(), "on ~* +@all", null);++ assertEquals("DefaultUserRequired", assertThrows(AwsException.class,+ () -> service.createUserGroup("no-default", "redis", List.of("app-user-id"), "us-east-1")).getErrorCode());+ assertEquals("DuplicateUserName", assertThrows(AwsException.class,+ () -> service.createUserGroup("two-defaults", "redis",+ List.of("default-user-id", "second-default-id"), "us-east-1")).getErrorCode());+ assertEquals("InvalidParameterValue", assertThrows(AwsException.class,+ () -> service.createUserGroup("mixed", "redis",+ List.of("default-user-id", "valkey-user-id"), "us-east-1")).getErrorCode());+ assertEquals("InvalidParameterValue", assertThrows(AwsException.class,+ () -> service.createUserGroup("vk-open", "valkey", List.of("open-user-id"), "us-east-1")).getErrorCode());++ // a valkey user group needs no default user+ service.createUserGroup("vk-group", "valkey", List.of("valkey-user-id", "app-user-id"), "us-east-1");+ assertEquals("UserGroupAlreadyExists", assertThrows(AwsException.class,+ () -> service.createUserGroup("VK-Group", "valkey", List.of("valkey-user-id"), "us-east-1")).getErrorCode());++ service.deleteUser("app-user-id");+ assertEquals(Set.of("valkey-user-id"), service.getUserGroup("vk-group").getUserIds());+ }+@Testvoid singleArgAuthFallsBackToGroupAuthToken() {service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, "group-token", "us-east-1");@@ -349,14 +595,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, true, null, null, ReplicationGroupSettings.defaults(), Map.of(), 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, port, ReplicationGroupSettings.defaults(), Map.of(), null);}// botocore models Port as an optional input on CreateReplicationGroup ("the port number on@@ -446,7 +692,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, true, null, 16390, ReplicationGroupSettings.defaults(), Map.of(), null));assertEquals(16390, group.getConfigurationEndpoint().port());assertEquals(16390, group.getClusterNodes().getFirst().getProxyPort());@@ -999,10 +1245,10 @@ class ElastiCacheServiceTest {assertEquals("01:00-02:00", service.getReplicationGroup("g1").getSnapshotWindow());// a refusal later in the same request must not leave the earlier part applied: the store- // hands out its own object, so settings applied before the user check would stay visible- AwsException unknownUser = assertThrows(AwsException.class, () -> service.modifyReplicationGroup(- "g1", List.of("no-such-user"), null, new ReplicationGroupSettings(null, null, 9, "03:00-04:00")));- assertEquals("UserNotFoundFault", unknownUser.getErrorCode());+ // hands out its own object, so settings applied before the user group check would stay visible+ AwsException unknownUserGroup = assertThrows(AwsException.class, () -> service.modifyReplicationGroup(+ "g1", List.of("no-such-user-group"), null, new ReplicationGroupSettings(null, null, 9, "03:00-04:00")));+ assertEquals("UserGroupNotFound", unknownUserGroup.getErrorCode());assertEquals(3, service.getReplicationGroup("g1").getSnapshotRetentionLimit());assertEquals("01:00-02:00", service.getReplicationGroup("g1").getSnapshotWindow());}@@ -1055,7 +1301,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, ReplicationGroupSettings.defaults(), Map.of(), null);}@Test@@ -1174,6 +1420,61 @@ class ElastiCacheServiceTest {assertEquals("shared-pg", svc.getReplicationGroup("grp").getCacheParameterGroupName());}+ @Test+ void aUserGroupClaimHeldByOneAccountDoesNotBlockAnotherAccountsDeleteOfItsOwnSameNamedGroup()+ throws InterruptedException {+ ConcurrentHashMap<Thread, String> accountByThread = new ConcurrentHashMap<>();+ RequestContext requestContext = mock(RequestContext.class);+ when(requestContext.getAccountId()).thenAnswer(inv -> accountByThread.get(Thread.currentThread()));+ @SuppressWarnings("unchecked")+ Instance<RequestContext> requestContextInstance = mock(Instance.class);+ when(requestContextInstance.get()).thenReturn(requestContext);+ StorageFactory factory = mock(StorageFactory.class);+ when(factory.create(anyString(), anyString(), any())).thenAnswer(inv ->+ new AccountAwareStorageBackend<>(new InMemoryStorage<>(), requestContextInstance, "000000000000"));+ ElastiCacheService svc = new ElastiCacheService(containerManager, proxyManager, clusterFormation,+ factory, config, mock(Ec2Service.class),+ new RegionResolver("us-east-1", "000000000000"), kmsService, provisioningIds);++ CountDownLatch startedLatch = new CountDownLatch(1);+ CountDownLatch releaseLatch = new CountDownLatch(1);+ when(containerManager.tryStart(anyString(), anyString(), any())).thenAnswer(inv -> {+ startedLatch.countDown();+ assertTrue(releaseLatch.await(5, TimeUnit.SECONDS), "test timed out waiting for release");+ return new ElastiCacheContainerHandle("cid", "grp", "localhost", 6379);+ });++ // Account A: owns shared-ug and is provisioning a replication group that names it.+ Thread accountA = new Thread(() -> {+ accountByThread.put(Thread.currentThread(), "111111111111");+ svc.createUser("default-user-id", "default", AuthMode.PASSWORD,+ List.of("default-pass"), "on ~* +@all", null);+ svc.createUserGroup("shared-ug", "redis", List.of("default-user-id"), "us-east-1");+ 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")));+ });+ accountA.start();+ assertTrue(startedLatch.await(5, TimeUnit.SECONDS), "create never reached container start");++ // Account B: owns an unrelated shared-ug of its own; account A's claim must not hold it.+ accountByThread.put(Thread.currentThread(), "222222222222");+ svc.createUser("default-user-id", "default", AuthMode.PASSWORD,+ List.of("default-pass"), "on ~* +@all", null);+ svc.createUserGroup("shared-ug", "redis", List.of("default-user-id"), "us-east-1");+ svc.deleteUserGroup("shared-ug");++ // Account A's own user group is still held by its in-flight create.+ accountByThread.put(Thread.currentThread(), "111111111111");+ assertEquals("InvalidUserGroupState",+ assertThrows(AwsException.class, () -> svc.deleteUserGroup("shared-ug")).getErrorCode());++ releaseLatch.countDown();+ accountA.join(5000);+ assertEquals(Set.of("shared-ug"), svc.getReplicationGroup("grp").getUserGroupIds());+ }+@Testvoid aFailedCreateReleasesItsClaimOnTheParameterGroup() {service.createCacheParameterGroup("custom-pg", "redis7", "in use", Map.of());diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheUserGroupIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheUserGroupIntegrationTest.javanew file mode 100644index 0000000000..d6194ede8f--- /dev/null+++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheUserGroupIntegrationTest.java@@ -0,0 +1,221 @@+package io.github.hectorvent.floci.services.elasticache;++import io.quarkus.test.junit.QuarkusTest;+import io.restassured.specification.RequestSpecification;+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 static io.restassured.RestAssured.given;+import static org.hamcrest.Matchers.containsString;+import static org.hamcrest.Matchers.equalTo;+import static org.hamcrest.Matchers.hasItems;++/**+ * Control-plane coverage for CreateUserGroup, DescribeUserGroups, ModifyUserGroup and+ * DeleteUserGroup; these never touch a container.+ */+@QuarkusTest+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)+class ElastiCacheUserGroupIntegrationTest {++ private static final String AUTH_HEADER =+ "AWS4-HMAC-SHA256 Credential=test/20260412/us-east-1/elasticache/aws4_request";+ private static final String DEFAULT_USER = "ug-it-default-user";+ private static final String APP_USER = "ug-it-app-user";+ private static final String USER_GROUP = "ug-it-group";+ private static final String CREATE = "CreateUserGroupResponse.CreateUserGroupResult.";+ private static final String MODIFY = "ModifyUserGroupResponse.ModifyUserGroupResult.";+ private static final String DESCRIBE = "DescribeUserGroupsResponse.DescribeUserGroupsResult.UserGroups.member.";++ private static RequestSpecification request(String action) {+ return given()+ .formParam("Action", action)+ .header("Authorization", AUTH_HEADER);+ }++ private static void createUser(String userId, String userName) {+ request("CreateUser")+ .formParam("UserId", userId)+ .formParam("UserName", userName)+ .formParam("Engine", "redis")+ .formParam("AccessString", "on ~* +@all")+ .formParam("AuthenticationMode.Type", "password")+ .formParam("AuthenticationMode.Passwords.member.1", "password-0123456789")+ .when()+ .post("/")+ .then()+ .statusCode(200);+ }++ @Test+ @Order(1)+ void createUserGroupWithoutADefaultUserIsRejected() {+ createUser(DEFAULT_USER, "default");+ createUser(APP_USER, "ug-it-app");++ request("CreateUserGroup")+ .formParam("UserGroupId", "ug-it-no-default")+ .formParam("Engine", "redis")+ .formParam("UserIds.member.1", APP_USER)+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("ErrorResponse.Error.Code", equalTo("DefaultUserRequired"));+ }++ @Test+ @Order(2)+ void createUserGroupStoresTheIdInLowercase() {+ request("CreateUserGroup")+ .formParam("UserGroupId", "UG-IT-Group")+ .formParam("Engine", "redis")+ .formParam("UserIds.member.1", DEFAULT_USER)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(CREATE + "UserGroupId", equalTo(USER_GROUP))+ .body(CREATE + "Status", equalTo("active"))+ .body(CREATE + "Engine", equalTo("redis"))+ .body(CREATE + "UserIds.member", equalTo(DEFAULT_USER))+ .body(CREATE + "MinimumEngineVersion", equalTo("6.0"))+ .body(CREATE + "ARN", containsString(":usergroup:" + USER_GROUP));+ }++ @Test+ @Order(3)+ void userGroupArnCarriesTheRequestRegion() {+ given()+ .formParam("Action", "CreateUserGroup")+ .formParam("UserGroupId", "ug-it-regional")+ .formParam("Engine", "redis")+ .formParam("UserIds.member.1", DEFAULT_USER)+ .header("Authorization",+ "AWS4-HMAC-SHA256 Credential=test/20260412/ap-south-1/elasticache/aws4_request")+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(CREATE + "ARN", containsString(":ap-south-1:"));++ request("DeleteUserGroup")+ .formParam("UserGroupId", "ug-it-regional")+ .when()+ .post("/")+ .then()+ .statusCode(200);+ }++ @Test+ @Order(3)+ void createUserGroupWithATakenIdIsRejected() {+ request("CreateUserGroup")+ .formParam("UserGroupId", USER_GROUP)+ .formParam("Engine", "redis")+ .formParam("UserIds.member.1", DEFAULT_USER)+ .when()+ .post("/")+ .then()+ .statusCode(400)+ .body("ErrorResponse.Error.Code", equalTo("UserGroupAlreadyExists"));+ }++ @Test+ @Order(4)+ void modifyUserGroupAddsAUser() {+ request("ModifyUserGroup")+ .formParam("UserGroupId", USER_GROUP)+ .formParam("UserIdsToAdd.member.1", APP_USER)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(MODIFY + "UserIds.member", hasItems(DEFAULT_USER, APP_USER));+ }++ @Test+ @Order(5)+ void modifyUserGroupRejectsAnUnknownUser() {+ request("ModifyUserGroup")+ .formParam("UserGroupId", USER_GROUP)+ .formParam("UserIdsToAdd.member.1", "ug-it-missing-user")+ .when()+ .post("/")+ .then()+ .statusCode(404);+ }++ @Test+ @Order(6)+ void describeUsersReportsTheUserGroupsAUserBelongsTo() {+ request("DescribeUsers")+ .formParam("UserId", APP_USER)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("DescribeUsersResponse.DescribeUsersResult.Users.member.UserGroupIds.member", equalTo(USER_GROUP));+ }++ @Test+ @Order(7)+ void describeUserGroupsFiltersById() {+ request("DescribeUserGroups")+ .formParam("UserGroupId", USER_GROUP)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(DESCRIBE + "UserGroupId", equalTo(USER_GROUP))+ .body(DESCRIBE + "UserIds.member", hasItems(DEFAULT_USER, APP_USER));+ }++ @Test+ @Order(8)+ void deletingAUserRemovesItFromItsUserGroups() {+ request("DeleteUser")+ .formParam("UserId", APP_USER)+ .when()+ .post("/")+ .then()+ .statusCode(200);++ request("DescribeUserGroups")+ .formParam("UserGroupId", USER_GROUP)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body(DESCRIBE + "UserIds.member", equalTo(DEFAULT_USER));+ }++ @Test+ @Order(9)+ void deleteUserGroupThenDescribeReportsNotFound() {+ request("DeleteUserGroup")+ .formParam("UserGroupId", USER_GROUP)+ .when()+ .post("/")+ .then()+ .statusCode(200)+ .body("DeleteUserGroupResponse.DeleteUserGroupResult.UserGroupId", equalTo(USER_GROUP));++ request("DescribeUserGroups")+ .formParam("UserGroupId", USER_GROUP)+ .when()+ .post("/")+ .then()+ .statusCode(404)+ .body("ErrorResponse.Error.Code", equalTo("UserGroupNotFound"));++ request("DeleteUser")+ .formParam("UserId", DEFAULT_USER)+ .when()+ .post("/")+ .then()+ .statusCode(200);+ }+}diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/proxy/ElastiCacheAuthProxyTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/proxy/ElastiCacheAuthProxyTest.javaindex 53d3dcaa91..2544cfb4c8 100644--- a/src/test/java/io/github/hectorvent/floci/services/elasticache/proxy/ElastiCacheAuthProxyTest.java+++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/proxy/ElastiCacheAuthProxyTest.java@@ -3,6 +3,8 @@ package io.github.hectorvent.floci.services.elasticache.proxy;import io.github.hectorvent.floci.services.elasticache.model.AuthMode;import org.junit.jupiter.api.Test;+import java.util.concurrent.atomic.AtomicReference;+import static org.junit.jupiter.api.Assertions.assertFalse;import static org.junit.jupiter.api.Assertions.assertTrue;import static org.mockito.Mockito.mock;@@ -10,6 +12,29 @@ import static org.mockito.Mockito.when;class ElastiCacheAuthProxyTest {+ @Test+ void authModeIsReadFromTheCacheOnEachConnection() {+ AtomicReference<AuthMode> cacheAuthMode = new AtomicReference<>();+ ElastiCacheAuthProxy.PasswordValidator passwordValidator = new ElastiCacheAuthProxy.PasswordValidator() {+ @Override+ public boolean validatePassword(String username, String password) {+ return false;+ }++ @Override+ public AuthMode authMode() {+ return cacheAuthMode.get();+ }+ };+ ElastiCacheAuthProxy proxy = new ElastiCacheAuthProxy("grp-live", AuthMode.IAM, "127.0.0.1", 6379,+ passwordValidator, mock(SigV4Validator.class));++ assertTrue(proxy.authRequired(), "the mode the proxy started with applies until the cache reports one");++ cacheAuthMode.set(AuthMode.NO_AUTH);+ assertFalse(proxy.authRequired(), "a cache whose last user group was removed has no access control");+ }+@Testvoid groupWithIamAuthModeAuthenticatesPasswordUserWhenAssociated() {SigV4Validator sigV4Validator = mock(SigV4Validator.class);