mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(elasticache): add user groups and associate them with replication groups (#4435)
CreateUserGroup, DescribeUserGroups, ModifyUserGroup and DeleteUserGroup returned UnsupportedOperation, and ModifyReplicationGroup read UserGroupIdsToAdd as user ids, so RBAC could not be set up the way AWS and Terraform (aws_elasticache_user_group, user_group_ids) do it. User groups are now stored resources. CreateReplicationGroup takes UserGroupIds and ModifyReplicationGroup adds and removes user groups; the proxy authenticates the members of a replication group's user groups, read at AUTH time so a ModifyUserGroup applies at once, each by its own authentication mode. A replication group with a user group associated admits only its members, even while the group is empty. An id in UserGroupIdsToAdd that names no user group but names a user still associates that user directly, so existing setups keep working. A cache uses either its AUTH token or user groups, as on AWS: while a user group is attached the token is not accepted and AuthTokenEnabled reads false. ModifyReplicationGroup takes AuthTokenUpdateStrategy DELETE, which removes the token and is only allowed together with a user group, and RemoveUserGroups. Removing the last user group leaves the cache without access control, still encrypted in transit; a token that was never deleted applies again. The proxy reads the cache's auth mode per connection, so these changes take effect without a restart. Validation follows the RBAC rules: a redis user group needs a user named default (DefaultUserRequired), members cannot share a user name (DuplicateUserName), valkey users only join valkey groups, a valkey group cannot hold a no-password user or serve a redis cache, association needs in-transit encryption, and a user group in use cannot be deleted (InvalidUserGroupState). ModifyUser refuses an engine or authentication change that a user group holding the user would reject. Deleting a user removes it from its user groups.
This commit is contained in:
1 parent
da53fc1c05
commit
0848fa6a48
13 files changed
+1256
-88
No files matched your search
+81
-4
@@ -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();
|
||||
|
||||
|
||||
Reference in new issue
Block a user