fix(elasticache): store the user Engine instead of hardcoding redis (#2331)

CreateUser dropped the Engine parameter and userXml emitted a literal
redis, so a user created with Engine=valkey read back as redis on
CreateUser, DescribeUsers, ModifyUser and DeleteUser - the issue's
aws_elasticache_user resource plans a diff on every run and never
converges.

Store the engine on ElastiCacheUser, defaulting a missing value to
redis to keep the previous behavior for callers that omit it. Accept
Engine on ModifyUser too, which AWS models as an optional parameter,
validating it before the first setter so a rejected request leaves
the user untouched - the storage backends hand back the live stored
object, so a mutation applied before validation would survive the
400. Honor the documented Engine filter on DescribeUsers, which means
something now that users can differ by engine. Values are validated
redis/valkey case-insensitively, matching the handler's existing
memcached check. MinimumEngineVersion stays at 6.0, the only value
AWS documents for the element. Users persisted before the engine
field existed deserialize with the redis default.

Closes #2290
This commit is contained in:
soreavis authored and GitHub committed 2026-08-15 18:16:57 -04:00
1 parent 20470b7f4a
commit 7a4fc45fdf
7 files changed
+423 -13

No files matched your search

@@ -158,6 +158,7 @@ public class ElastiCacheQueryHandler {
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()) {
return AwsQueryResponse.error("InvalidParameterValue", "UserId is required.", AwsNamespaces.EC, 400);
@@ -178,7 +179,7 @@ public class ElastiCacheQueryHandler {
}
try {
ElastiCacheUser user = service.createUser(userId, userName, authMode, passwords, accessString);
ElastiCacheUser user = service.createUser(userId, userName, authMode, passwords, accessString, engine);
return Response.ok(AwsQueryResponse.envelope("CreateUser", AwsNamespaces.EC, userXml(user))).build();
} catch (AwsException e) {
return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.EC, e.getHttpStatus());
@@ -187,8 +188,9 @@ public class ElastiCacheQueryHandler {
private Response handleDescribeUsers(MultivaluedMap<String, String> params) {
String filterId = params.getFirst("UserId");
String filterEngine = params.getFirst("Engine");
try {
Collection<ElastiCacheUser> users = service.listUsers(filterId);
Collection<ElastiCacheUser> users = service.listUsers(filterId, filterEngine);
var xml = new XmlBuilder().start("Users");
for (ElastiCacheUser u : users) {
xml.start("member").raw(userXml(u)).end("member");
@@ -202,12 +204,13 @@ public class ElastiCacheQueryHandler {
private Response handleModifyUser(MultivaluedMap<String, String> params) {
String userId = params.getFirst("UserId");
String engine = params.getFirst("Engine");
if (userId == null || userId.isBlank()) {
return AwsQueryResponse.error("InvalidParameterValue", "UserId is required.", AwsNamespaces.EC, 400);
}
List<String> passwords = extractMemberList(params, "AuthenticationMode.Passwords.member.");
try {
ElastiCacheUser user = service.modifyUser(userId, passwords.isEmpty() ? null : passwords);
ElastiCacheUser user = service.modifyUser(userId, passwords.isEmpty() ? null : passwords, 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());
@@ -384,7 +387,8 @@ public class ElastiCacheQueryHandler {
.elem("Type", authType)
.elem("PasswordCount", (long) pwCount)
.end("Authentication")
.elem("Engine", "redis")
.elem("Engine", u.getEngine())
// MinimumEngineVersion: the only value AWS documents; no valkey-specific one is published.
.elem("MinimumEngineVersion", "6.0")
.start("UserGroupIds").end("UserGroupIds")
.elem("ARN", AwsArnUtils.Arn.of("elasticache", regionResolver.getDefaultRegion(), regionResolver.getAccountId(), "user:" + u.getUserId()).toString())
@@ -20,6 +20,7 @@ import org.jboss.logging.Logger;
import java.time.Instant;
import java.util.Collection;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@@ -183,17 +184,20 @@ public class ElastiCacheService {
}
public ElastiCacheUser createUser(String userId, String userName, AuthMode authMode,
List<String> passwords, String accessString) {
List<String> passwords, String accessString, String engine) {
if (users.get(userId).isPresent()) {
throw new AwsException("UserAlreadyExistsFault",
"User " + userId + " already exists.", 400);
}
// Engine is required on AWS, but Floci's CreateUser accepted requests without
// it, so a missing value keeps the previous implicit redis.
String normalizedEngine = (engine == null || engine.isBlank()) ? "redis" : normalizeEngine(engine);
ElastiCacheUser user = new ElastiCacheUser(
userId, userName, authMode,
passwords != null ? passwords : List.of(),
accessString != null ? accessString : "on ~* +@all",
"active", Instant.now());
normalizedEngine, "active", Instant.now());
users.put(userId, user);
LOG.infov("ElastiCache user {0} created with authMode={1}", userId, authMode);
@@ -205,21 +209,33 @@ public class ElastiCacheService {
new AwsException("UserNotFoundFault", "User " + userId + " not found.", 404));
}
public Collection<ElastiCacheUser> listUsers(String filterUserId) {
public Collection<ElastiCacheUser> listUsers(String filterUserId, String filterEngine) {
// UserId wins when both filters are sent; AWS documents no interaction between them.
if (filterUserId != null && !filterUserId.isBlank()) {
return users.get(filterUserId)
.map(List::of)
.orElseThrow(() -> new AwsException("UserNotFoundFault",
"User " + filterUserId + " not found.", 404));
}
if (filterEngine != null && !filterEngine.isBlank()) {
return users.scan(k -> true).stream()
.filter(u -> filterEngine.equalsIgnoreCase(u.getEngine()))
.toList();
}
return users.scan(k -> true);
}
public ElastiCacheUser modifyUser(String userId, List<String> passwords) {
public ElastiCacheUser modifyUser(String userId, List<String> passwords, 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 (normalizedEngine != null) {
user.setEngine(normalizedEngine);
}
users.put(userId, user);
return user;
}
@@ -267,6 +283,16 @@ public class ElastiCacheService {
.anyMatch(u -> u.getPasswords() != null && u.getPasswords().contains(password));
}
// AWS allows only redis and valkey.
private static String normalizeEngine(String engine) {
String normalized = engine.toLowerCase(Locale.ROOT);
if (!"redis".equals(normalized) && !"valkey".equals(normalized)) {
throw new AwsException("InvalidParameterValue",
"Engine must be 'redis' or 'valkey'.", 400);
}
return normalized;
}
private String resolveEndpointHost() {
return config.hostname().orElse("localhost");
}
@@ -13,19 +13,22 @@ public class ElastiCacheUser {
private AuthMode authMode;
private List<String> passwords;
private String accessString;
// "redis" or "valkey"; the initializer keeps users persisted before this field existed on redis.
private String engine = "redis";
private String status;
private Instant createdAt;
public ElastiCacheUser() {}
public ElastiCacheUser(String userId, String userName, AuthMode authMode,
List<String> passwords, String accessString,
List<String> passwords, String accessString, String engine,
String status, Instant createdAt) {
this.userId = userId;
this.userName = userName;
this.authMode = authMode;
this.passwords = passwords;
this.accessString = accessString;
this.engine = engine;
this.status = status;
this.createdAt = createdAt;
}
@@ -45,6 +48,9 @@ public class ElastiCacheUser {
public String getAccessString() { return accessString; }
public void setAccessString(String accessString) { this.accessString = accessString; }
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; }