Skip to content

armgit / floci

Public Imported from GitHub
0

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

fix(elasticache): scope user auth to groups, use StorageFactory, throw NotFoundFault (#367)

TiM66141049b53b
diff --git a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
index 054b4e533a..4ad166c5ac 100644
--- a/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
+++ b/src/main/java/io/github/hectorvent/floci/config/EmulatorConfig.java
@@ -81,6 +81,7 @@ public interface EmulatorConfig {
OpenSearchStorageConfig opensearch();
AppConfigStorageConfig appconfig();
AppConfigDataStorageConfig appconfigdata();
+ ElastiCacheStorageConfig elasticache();
}
interface SsmStorageConfig {
@@ -168,6 +169,13 @@ public interface EmulatorConfig {
long flushIntervalMs();
}
+ interface ElastiCacheStorageConfig {
+ Optional<String> mode();
+
+ @WithDefault("5000")
+ long flushIntervalMs();
+ }
+
interface WalConfig {
@WithDefault("30000")
long compactionIntervalMs();
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.java
index 72cc5b98b4..fc9319ccf4 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
@@ -225,7 +225,7 @@ public class AwsQueryController {
*/
private static final Set<String> ELASTICACHE_ACTIONS = Set.of(
"ValidateIamAuthToken",
- "CreateReplicationGroup", "DescribeReplicationGroups", "DeleteReplicationGroup",
+ "CreateReplicationGroup", "DescribeReplicationGroups", "ModifyReplicationGroup", "DeleteReplicationGroup",
"CreateUser", "DescribeUsers", "ModifyUser", "DeleteUser"
);
diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java
index d650880d91..299ade2917 100644
--- a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java
+++ b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java
@@ -69,7 +69,8 @@ public class ResolvedServiceCatalog {
protocols(ServiceProtocol.QUERY),
Set.of(), Set.of("sts"), Set.of(), Set.of()),
descriptor("elasticache", "elasticache", config.services().elasticache().enabled(), true,
- null, null, 5000L, AwsNamespaces.EC, ServiceProtocol.QUERY,
+ "elasticache", storageMode(config.storage().services().elasticache().mode(), config.storage().mode()),
+ config.storage().services().elasticache().flushIntervalMs(), AwsNamespaces.EC, ServiceProtocol.QUERY,
protocols(ServiceProtocol.QUERY),
Set.of(), Set.of("elasticache"), Set.of(), Set.of()),
descriptor("rds", "rds", config.services().rds().enabled(), true,
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 353fd0d4ee..8481886b84 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
@@ -44,6 +44,7 @@ public class ElastiCacheQueryHandler {
case "ValidateIamAuthToken" -> handleValidateIamAuthToken(params);
case "CreateReplicationGroup" -> handleCreateReplicationGroup(params);
case "DescribeReplicationGroups" -> handleDescribeReplicationGroups(params);
+ case "ModifyReplicationGroup" -> handleModifyReplicationGroup(params);
case "DeleteReplicationGroup" -> handleDeleteReplicationGroup(params);
case "CreateUser" -> handleCreateUser(params);
case "DescribeUsers" -> handleDescribeUsers(params);
@@ -117,6 +118,25 @@ public class ElastiCacheQueryHandler {
}
}
+ private Response handleModifyReplicationGroup(MultivaluedMap<String, String> params) {
+ String groupId = params.getFirst("ReplicationGroupId");
+ if (groupId == null || groupId.isBlank()) {
+ return AwsQueryResponse.error("InvalidParameterValue",
+ "ReplicationGroupId is required.", AwsNamespaces.EC, 400);
+ }
+ List<String> userIdsToAdd = extractMemberList(params, "UserGroupIdsToAdd.member.");
+ List<String> userIdsToRemove = extractMemberList(params, "UserGroupIdsToRemove.member.");
+ try {
+ ReplicationGroup group = service.modifyReplicationGroup(groupId,
+ userIdsToAdd.isEmpty() ? null : userIdsToAdd,
+ userIdsToRemove.isEmpty() ? null : userIdsToRemove);
+ String result = replicationGroupXml(group);
+ return Response.ok(AwsQueryResponse.envelope("ModifyReplicationGroup", AwsNamespaces.EC, result)).build();
+ } catch (AwsException e) {
+ return AwsQueryResponse.error(e.getErrorCode(), e.getMessage(), AwsNamespaces.EC, e.getHttpStatus());
+ }
+ }
+
// ── Users ─────────────────────────────────────────────────────────────────
private Response handleCreateUser(MultivaluedMap<String, String> params) {
@@ -138,7 +158,7 @@ public class ElastiCacheQueryHandler {
authMode = AuthMode.IAM;
} else if ("password".equalsIgnoreCase(authModeType)) {
authMode = AuthMode.PASSWORD;
- passwords = extractPasswordList(params, "AuthenticationMode.Passwords.member.");
+ passwords = extractMemberList(params, "AuthenticationMode.Passwords.member.");
} else {
authMode = AuthMode.NO_AUTH;
}
@@ -171,7 +191,7 @@ public class ElastiCacheQueryHandler {
if (userId == null || userId.isBlank()) {
return AwsQueryResponse.error("InvalidParameterValue", "UserId is required.", AwsNamespaces.EC, 400);
}
- List<String> passwords = extractPasswordList(params, "AuthenticationMode.Passwords.member.");
+ List<String> passwords = extractMemberList(params, "AuthenticationMode.Passwords.member.");
try {
ElastiCacheUser user = service.modifyUser(userId, passwords.isEmpty() ? null : passwords);
return Response.ok(AwsQueryResponse.envelope("ModifyUser", AwsNamespaces.EC, userXml(user))).build();
@@ -272,16 +292,16 @@ public class ElastiCacheQueryHandler {
.build();
}
- private static List<String> extractPasswordList(MultivaluedMap<String, String> params, String prefix) {
- List<String> passwords = new ArrayList<>();
+ private static List<String> extractMemberList(MultivaluedMap<String, String> params, String prefix) {
+ List<String> values = new ArrayList<>();
for (int i = 1; ; i++) {
- String pw = params.getFirst(prefix + i);
- if (pw == null) {
+ String value = params.getFirst(prefix + i);
+ if (value == null) {
break;
}
- passwords.add(pw);
+ values.add(value);
}
- return passwords;
+ return values;
}
private static String extractUriHost(String token) {
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 26994f28c2..06379075aa 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
@@ -1,9 +1,10 @@
package io.github.hectorvent.floci.services.elasticache;
+import com.fasterxml.jackson.core.type.TypeReference;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsException;
-import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.core.storage.StorageBackend;
+import io.github.hectorvent.floci.core.storage.StorageFactory;
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerHandle;
import io.github.hectorvent.floci.services.elasticache.container.ElastiCacheContainerManager;
import io.github.hectorvent.floci.services.elasticache.model.AuthMode;
@@ -19,6 +20,7 @@ import org.jboss.logging.Logger;
import java.time.Instant;
import java.util.Collection;
import java.util.List;
+import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
@@ -41,12 +43,15 @@ public class ElastiCacheService {
@Inject
public ElastiCacheService(ElastiCacheContainerManager containerManager,
ElastiCacheProxyManager proxyManager,
+ StorageFactory storageFactory,
EmulatorConfig config) {
this.containerManager = containerManager;
this.proxyManager = proxyManager;
this.config = config;
- this.groups = new InMemoryStorage<>();
- this.users = new InMemoryStorage<>();
+ this.groups = storageFactory.create("elasticache", "elasticache-groups.json",
+ new TypeReference<Map<String, ReplicationGroup>>() {});
+ this.users = storageFactory.create("elasticache", "elasticache-users.json",
+ new TypeReference<Map<String, ElastiCacheUser>>() {});
}
public ReplicationGroup createReplicationGroup(String groupId, String description,
@@ -64,7 +69,8 @@ public class ElastiCacheService {
ElastiCacheContainerHandle handle = containerManager.start(groupId, image);
- Endpoint endpoint = new Endpoint("localhost", proxyPort);
+ String endpointHost = resolveEndpointHost();
+ Endpoint endpoint = new Endpoint(endpointHost, proxyPort);
ReplicationGroup group = new ReplicationGroup(
groupId, description, ReplicationGroupStatus.AVAILABLE,
authMode, endpoint, Instant.now(), proxyPort);
@@ -78,7 +84,7 @@ public class ElastiCacheService {
(username, password) -> validatePassword(groupId, username, password));
groups.put(groupId, group);
- LOG.infov("Replication group {0} created, endpoint=localhost:{1}", groupId, proxyPort);
+ LOG.infov("Replication group {0} created, endpoint={1}:{2}", groupId, endpointHost, proxyPort);
return group;
}
@@ -92,7 +98,8 @@ public class ElastiCacheService {
if (filterGroupId != null && !filterGroupId.isBlank()) {
return groups.get(filterGroupId)
.map(List::of)
- .orElse(List.of());
+ .orElseThrow(() -> new AwsException("ReplicationGroupNotFoundFault",
+ "Replication group " + filterGroupId + " not found.", 404));
}
return groups.scan(k -> true);
}
@@ -117,6 +124,24 @@ public class ElastiCacheService {
LOG.infov("Replication group {0} deleted", groupId);
}
+ public ReplicationGroup modifyReplicationGroup(String groupId, List<String> userIdsToAdd,
+ List<String> userIdsToRemove) {
+ ReplicationGroup group = getReplicationGroup(groupId);
+
+ if (userIdsToAdd != null) {
+ for (String userId : userIdsToAdd) {
+ getUser(userId); // validate user exists
+ group.getAssociatedUserIds().add(userId);
+ }
+ }
+ if (userIdsToRemove != null) {
+ group.getAssociatedUserIds().removeAll(userIdsToRemove);
+ }
+
+ groups.put(groupId, group);
+ return group;
+ }
+
public ElastiCacheUser createUser(String userId, String userName, AuthMode authMode,
List<String> passwords, String accessString) {
if (users.get(userId).isPresent()) {
@@ -144,7 +169,8 @@ public class ElastiCacheService {
if (filterUserId != null && !filterUserId.isBlank()) {
return users.get(filterUserId)
.map(List::of)
- .orElse(List.of());
+ .orElseThrow(() -> new AwsException("UserNotFoundFault",
+ "User " + filterUserId + " not found.", 404));
}
return users.scan(k -> true);
}
@@ -168,28 +194,40 @@ public class ElastiCacheService {
/**
* Validates a Redis AUTH password for the given group.
- * Checks the group-level authToken first, then falls back to per-user passwords.
- * Called by the TCP auth proxy for PASSWORD-mode groups.
+ * Checks the group-level authToken first, then falls back to users associated
+ * with the group. Only users explicitly added via ModifyReplicationGroup are
+ * checked, preventing cross-group credential leakage.
*/
public boolean validatePassword(String groupId, String username, String password) {
+ ReplicationGroup group = groups.get(groupId).orElse(null);
+ if (group == null) {
+ return false;
+ }
+
if (username == null || username.isEmpty()) {
// AUTH password form: check group-level authToken first
- ReplicationGroup group = groups.get(groupId).orElse(null);
- if (group != null && group.getAuthToken() != null
- && password.equals(group.getAuthToken())) {
+ if (group.getAuthToken() != null && password.equals(group.getAuthToken())) {
return true;
}
- // Fall back to any PASSWORD user matching this password
- return users.scan(k -> true).stream()
- .filter(u -> u.getAuthMode() == AuthMode.PASSWORD)
+ // Fall back to PASSWORD users associated with this group
+ Set<String> groupUserIds = group.getAssociatedUserIds();
+ return groupUserIds.stream()
+ .map(id -> users.get(id).orElse(null))
+ .filter(u -> u != null && u.getAuthMode() == AuthMode.PASSWORD)
.anyMatch(u -> u.getPasswords() != null && u.getPasswords().contains(password));
}
- // AUTH username password form: find user by userName
- return users.scan(k -> true).stream()
- .filter(u -> username.equals(u.getUserName()) && u.getAuthMode() == AuthMode.PASSWORD)
+ // AUTH username password form: find user by userName, scoped to group
+ Set<String> groupUserIds = group.getAssociatedUserIds();
+ return groupUserIds.stream()
+ .map(id -> users.get(id).orElse(null))
+ .filter(u -> u != null && username.equals(u.getUserName()) && u.getAuthMode() == AuthMode.PASSWORD)
.anyMatch(u -> u.getPasswords() != null && u.getPasswords().contains(password));
}
+ private String resolveEndpointHost() {
+ return config.hostname().orElse("localhost");
+ }
+
private int allocateProxyPort() {
int base = config.services().elasticache().proxyBasePort();
int max = config.services().elasticache().proxyMaxPort();
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.java
index 44a1ad5084..3effd18ec9 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
@@ -3,6 +3,8 @@ package io.github.hectorvent.floci.services.elasticache.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import java.time.Instant;
+import java.util.HashSet;
+import java.util.Set;
@RegisterForReflection
public class ReplicationGroup {
@@ -15,6 +17,7 @@ public class ReplicationGroup {
private Instant createdAt;
private int proxyPort;
private String authToken; // stored plain-text for PASSWORD auth validation in the proxy
+ private Set<String> associatedUserIds = new HashSet<>();
// Transient fields — not persisted, restored on container restart
private transient String containerId;
@@ -59,6 +62,11 @@ public class ReplicationGroup {
public String getAuthToken() { return authToken; }
public void setAuthToken(String authToken) { this.authToken = authToken; }
+ public Set<String> getAssociatedUserIds() { return associatedUserIds; }
+ public void setAssociatedUserIds(Set<String> associatedUserIds) {
+ this.associatedUserIds = associatedUserIds != null ? associatedUserIds : new HashSet<>();
+ }
+
public String getContainerId() { return containerId; }
public void setContainerId(String containerId) { this.containerId = containerId; }