Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(elasticache): honor top-level user passwords and let ModifyUser change auth and access string (#4431)

Clement4001556a3172
diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElastiCacheTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/ElastiCacheTest.java
index d621bba029..b7a5e26473 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
@@ -12,6 +12,7 @@ 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.CreateUserRequest;
+import software.amazon.awssdk.services.elasticache.model.CreateUserResponse;
import software.amazon.awssdk.services.elasticache.model.DeleteReplicationGroupRequest;
import software.amazon.awssdk.services.elasticache.model.DeleteUserRequest;
import software.amazon.awssdk.services.elasticache.model.DescribeReplicationGroupsRequest;
@@ -20,6 +21,7 @@ import software.amazon.awssdk.services.elasticache.model.Endpoint;
import software.amazon.awssdk.services.elasticache.model.InputAuthenticationType;
import software.amazon.awssdk.services.elasticache.model.ModifyReplicationGroupRequest;
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;
@@ -299,6 +301,38 @@ class ElastiCacheTest {
@Test
@Order(12)
+ void topLevelPasswordsAndAccessStringChangesRoundTrip() {
+ String passwordUserId = TestFixtures.uniqueName("ec-pw-user");
+ CreateUserResponse created = elasticache.createUser(CreateUserRequest.builder()
+ .userId(passwordUserId)
+ .userName(TestFixtures.uniqueName("ec-pw-name"))
+ .engine("redis")
+ .accessString("on ~app:* -@all +@read")
+ .passwords("top-level-password-1")
+ .build());
+ assertThat(created.authentication().typeAsString()).isEqualTo("password");
+ assertThat(created.authentication().passwordCount()).isEqualTo(1);
+
+ try {
+ ModifyUserResponse appended = elasticache.modifyUser(ModifyUserRequest.builder()
+ .userId(passwordUserId)
+ .appendAccessString("+@write")
+ .build());
+ assertThat(appended.accessString()).isEqualTo("on ~app:* -@all +@read +@write");
+
+ ModifyUserResponse opened = elasticache.modifyUser(ModifyUserRequest.builder()
+ .userId(passwordUserId)
+ .noPasswordRequired(true)
+ .build());
+ assertThat(opened.authentication().typeAsString()).isEqualTo("no-password-required");
+ assertThat(opened.authentication().passwordCount()).isZero();
+ } finally {
+ elasticache.deleteUser(DeleteUserRequest.builder().userId(passwordUserId).build());
+ }
+ }
+
+ @Test
+ @Order(13)
void deleteReplicationGroupReleasesPortForReuse() {
requireGroup();
diff --git a/docs/services/elasticache.md b/docs/services/elasticache.md
index 2b5d85f2c5..d4704ddefc 100644
--- a/docs/services/elasticache.md
+++ b/docs/services/elasticache.md
@@ -16,9 +16,9 @@ Floci manages real Valkey/Redis Docker containers and proxies TCP connections to
| `DescribeReplicationGroups` | List clusters and their connection info |
| `ModifyReplicationGroup` | Modify `SnapshotRetentionLimit` and `SnapshotWindow`, and the associated user groups |
| `DeleteReplicationGroup` | Stop and remove a cluster |
-| `CreateUser` | Create an ElastiCache IAM user |
+| `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` | Update user access strings |
+| `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 |
| `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 |
diff --git a/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheQueryHandler.java
index 208825b7d8..5bc69a1ef0 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
@@ -31,6 +31,7 @@ import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
/**
@@ -242,7 +243,6 @@ public class ElastiCacheQueryHandler {
String userId = params.getFirst("UserId");
String userName = params.getFirst("UserName");
String accessString = params.getFirst("AccessString");
- String authModeType = params.getFirst("AuthenticationMode.Type");
String engine = params.getFirst("Engine");
if (userId == null || userId.isBlank()) {
@@ -252,18 +252,11 @@ public class ElastiCacheQueryHandler {
return AwsQueryResponse.error("InvalidParameterValue", "UserName is required.", AwsNamespaces.EC, 400);
}
- AuthMode authMode;
- List<String> passwords = new ArrayList<>();
- if ("iam".equalsIgnoreCase(authModeType)) {
- authMode = AuthMode.IAM;
- } else if ("password".equalsIgnoreCase(authModeType)) {
- authMode = AuthMode.PASSWORD;
- passwords = extractMemberList(params, "AuthenticationMode.Passwords.member.");
- } else {
- authMode = AuthMode.NO_AUTH;
- }
-
try {
+ // A request naming no authentication at all keeps Floci's previous implicit no-password-required.
+ UserAuthentication auth = resolveUserAuthentication(params);
+ AuthMode authMode = auth != null ? auth.mode() : AuthMode.NO_AUTH;
+ List<String> passwords = auth != null ? auth.passwords() : List.of();
ElastiCacheUser user = service.createUser(userId, userName, authMode, passwords, accessString, engine);
return Response.ok(AwsQueryResponse.envelope("CreateUser", AwsNamespaces.EC, userXml(user))).build();
} catch (AwsException e) {
@@ -293,9 +286,18 @@ public class ElastiCacheQueryHandler {
if (userId == null || userId.isBlank()) {
return AwsQueryResponse.error("InvalidParameterValue", "UserId is required.", AwsNamespaces.EC, 400);
}
- List<String> passwords = extractMemberList(params, "AuthenticationMode.Passwords.member.");
+ String accessString = params.getFirst("AccessString");
+ String appendAccessString = params.getFirst("AppendAccessString");
+ if (accessString != null && appendAccessString != null) {
+ return AwsQueryResponse.error("InvalidParameterCombination",
+ "AccessString and AppendAccessString cannot be specified together.", AwsNamespaces.EC, 400);
+ }
try {
- ElastiCacheUser user = service.modifyUser(userId, passwords.isEmpty() ? null : passwords, engine);
+ UserAuthentication auth = resolveUserAuthentication(params);
+ ElastiCacheUser user = service.modifyUser(userId,
+ auth != null ? auth.mode() : null,
+ auth != null ? auth.passwords() : null,
+ accessString, appendAccessString, engine);
return Response.ok(AwsQueryResponse.envelope("ModifyUser", AwsNamespaces.EC, userXml(user))).build();
} catch (AwsException e) {
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.EC, e.getHttpStatus());
@@ -1049,6 +1051,59 @@ private Response handleCreateCacheParameterGroup(MultivaluedMap<String, String>
.build();
}
+ private record UserAuthentication(AuthMode mode, List<String> passwords) {}
+
+ /**
+ * Resolves the authentication a CreateUser or ModifyUser request asks for. AWS accepts it
+ * either as an {@code AuthenticationMode} or through the top-level {@code Passwords} and
+ * {@code NoPasswordRequired} members, so passwords given either way mean password
+ * authentication. Returns null when the request names no authentication at all.
+ */
+ private static UserAuthentication resolveUserAuthentication(MultivaluedMap<String, String> params) {
+ String type = params.getFirst("AuthenticationMode.Type");
+ List<String> passwords = extractMemberList(params, "AuthenticationMode.Passwords.member.");
+ if (passwords.isEmpty()) {
+ passwords = extractMemberList(params, "Passwords.member.");
+ }
+ boolean noPasswordRequired = Boolean.parseBoolean(params.getFirst("NoPasswordRequired"));
+
+ if (type == null || type.isBlank()) {
+ if (noPasswordRequired && !passwords.isEmpty()) {
+ throw invalidCombination("NoPasswordRequired cannot be true when Passwords are provided.");
+ }
+ if (!passwords.isEmpty()) {
+ return new UserAuthentication(AuthMode.PASSWORD, passwords);
+ }
+ return noPasswordRequired ? new UserAuthentication(AuthMode.NO_AUTH, List.of()) : null;
+ }
+
+ String normalizedType = type.toLowerCase(Locale.ROOT);
+ if (noPasswordRequired && !"no-password-required".equals(normalizedType)) {
+ throw invalidCombination("NoPasswordRequired cannot be true when AuthenticationMode.Type is " + type + ".");
+ }
+ return switch (normalizedType) {
+ case "password" -> {
+ if (passwords.isEmpty()) {
+ throw new AwsException("InvalidParameterValue",
+ "Passwords are required when AuthenticationMode.Type is password.", 400);
+ }
+ yield new UserAuthentication(AuthMode.PASSWORD, passwords);
+ }
+ case "iam", "no-password-required" -> {
+ if (!passwords.isEmpty()) {
+ throw invalidCombination("Passwords cannot be provided when AuthenticationMode.Type is " + type + ".");
+ }
+ yield new UserAuthentication("iam".equals(normalizedType) ? AuthMode.IAM : AuthMode.NO_AUTH, List.of());
+ }
+ default -> throw new AwsException("InvalidParameterValue",
+ "Invalid AuthenticationMode.Type " + type + ". Valid values are password, no-password-required and iam.", 400);
+ };
+ }
+
+ private static AwsException invalidCombination(String message) {
+ return new AwsException("InvalidParameterCombination", message, 400);
+ }
+
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.java
index 0a83d1c2e4..c513ce1ca8 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
@@ -1548,13 +1548,28 @@ public class ElastiCacheService implements ResourceProvider {
return users.scan(k -> true);
}
- public ElastiCacheUser modifyUser(String userId, List<String> passwords, String engine) {
+ /**
+ * Applies a ModifyUser request. A non-null {@code authMode} switches the user's
+ * authentication and replaces its passwords with {@code passwords}; {@code accessString}
+ * replaces the access string and {@code appendAccessString} adds to it.
+ */
+ public ElastiCacheUser modifyUser(String userId, AuthMode authMode, List<String> passwords,
+ String accessString, String appendAccessString, String engine) {
ElastiCacheUser user = getUser(userId);
// 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 (passwords != null) {
- user.setPasswords(passwords);
+ 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);
diff --git a/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheUserAuthenticationIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheUserAuthenticationIntegrationTest.java
new file mode 100644
index 0000000000..89aadda6b3
--- /dev/null
+++ b/src/test/java/io/github/hectorvent/floci/services/elasticache/ElastiCacheUserAuthenticationIntegrationTest.java
@@ -0,0 +1,188 @@
+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.equalTo;
+
+/**
+ * Control-plane coverage for how CreateUser and ModifyUser resolve a user's authentication:
+ * the top-level Passwords and NoPasswordRequired members as well as AuthenticationMode.
+ */
+@QuarkusTest
+@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
+class ElastiCacheUserAuthenticationIntegrationTest {
+
+ private static final String AUTH_HEADER =
+ "AWS4-HMAC-SHA256 Credential=test/20260412/us-east-1/elasticache/aws4_request";
+ private static final String PASSWORD_USER = "auth-it-password-user";
+ private static final String OPEN_USER = "auth-it-open-user";
+ private static final String CREATE = "CreateUserResponse.CreateUserResult.";
+ private static final String MODIFY = "ModifyUserResponse.ModifyUserResult.";
+ private static final String DESCRIBE = "DescribeUsersResponse.DescribeUsersResult.Users.member.";
+
+ private static RequestSpecification request(String action) {
+ return given()
+ .formParam("Action", action)
+ .header("Authorization", AUTH_HEADER);
+ }
+
+ private static RequestSpecification createUser(String userId) {
+ return request("CreateUser")
+ .formParam("UserId", userId)
+ .formParam("UserName", userId)
+ .formParam("Engine", "valkey")
+ .formParam("AccessString", "on ~* +@all");
+ }
+
+ @Test
+ @Order(1)
+ void createUserWithTopLevelPasswordsUsesPasswordAuthentication() {
+ createUser(PASSWORD_USER)
+ .formParam("Passwords.member.1", "first-password-0123456789")
+ .formParam("Passwords.member.2", "second-password-0123456789")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(CREATE + "Authentication.Type", equalTo("password"))
+ .body(CREATE + "Authentication.PasswordCount", equalTo("2"));
+ }
+
+ @Test
+ @Order(2)
+ void createUserWithNoPasswordRequiredUsesNoPasswordAuthentication() {
+ createUser(OPEN_USER)
+ .formParam("NoPasswordRequired", "true")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(CREATE + "Authentication.Type", equalTo("no-password-required"))
+ .body(CREATE + "Authentication.PasswordCount", equalTo("0"));
+ }
+
+ @Test
+ @Order(3)
+ void createUserRejectsPasswordsTogetherWithNoPasswordRequired() {
+ createUser("auth-it-conflicting-user")
+ .formParam("Passwords.member.1", "first-password-0123456789")
+ .formParam("NoPasswordRequired", "true")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("ErrorResponse.Error.Code", equalTo("InvalidParameterCombination"));
+ }
+
+ @Test
+ @Order(4)
+ void createUserRejectsPasswordModeWithoutPasswords() {
+ createUser("auth-it-passwordless-user")
+ .formParam("AuthenticationMode.Type", "password")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"));
+ }
+
+ @Test
+ @Order(5)
+ void modifyUserWithPasswordsSwitchesANoPasswordUserToPasswordAuthentication() {
+ request("ModifyUser")
+ .formParam("UserId", OPEN_USER)
+ .formParam("Passwords.member.1", "new-password-0123456789")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(MODIFY + "Authentication.Type", equalTo("password"))
+ .body(MODIFY + "Authentication.PasswordCount", equalTo("1"));
+ }
+
+ @Test
+ @Order(6)
+ void modifyUserWithAuthenticationModeSwitchesAPasswordUserToNoPassword() {
+ request("ModifyUser")
+ .formParam("UserId", PASSWORD_USER)
+ .formParam("AuthenticationMode.Type", "no-password-required")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(MODIFY + "Authentication.Type", equalTo("no-password-required"))
+ .body(MODIFY + "Authentication.PasswordCount", equalTo("0"));
+ }
+
+ @Test
+ @Order(7)
+ void modifyUserReplacesTheAccessString() {
+ request("ModifyUser")
+ .formParam("UserId", PASSWORD_USER)
+ .formParam("AccessString", "on ~app:* -@all +@read")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(MODIFY + "AccessString", equalTo("on ~app:* -@all +@read"));
+ }
+
+ @Test
+ @Order(8)
+ void modifyUserAppendsToTheAccessString() {
+ request("ModifyUser")
+ .formParam("UserId", PASSWORD_USER)
+ .formParam("AppendAccessString", "+@write")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(MODIFY + "AccessString", equalTo("on ~app:* -@all +@read +@write"));
+ }
+
+ @Test
+ @Order(9)
+ void modifyUserRejectsAccessStringTogetherWithAppendAccessString() {
+ request("ModifyUser")
+ .formParam("UserId", PASSWORD_USER)
+ .formParam("AccessString", "on ~* +@all")
+ .formParam("AppendAccessString", "+@write")
+ .when()
+ .post("/")
+ .then()
+ .statusCode(400)
+ .body("ErrorResponse.Error.Code", equalTo("InvalidParameterCombination"));
+ }
+
+ @Test
+ @Order(10)
+ void describeUsersReportsTheModifiedAuthentication() {
+ request("DescribeUsers")
+ .formParam("UserId", OPEN_USER)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200)
+ .body(DESCRIBE + "Authentication.Type", equalTo("password"))
+ .body(DESCRIBE + "Authentication.PasswordCount", equalTo("1"));
+ }
+
+ @Test
+ @Order(11)
+ void deleteUsers() {
+ for (String userId : new String[] {PASSWORD_USER, OPEN_USER}) {
+ request("DeleteUser")
+ .formParam("UserId", userId)
+ .when()
+ .post("/")
+ .then()
+ .statusCode(200);
+ }
+ }
+}