mirror of
https://github.com/floci-io/floci.git
synced 2026-11-30 00:45:17 -05:00
fix(elasticache): enforce user permissions in backend ACL sessions (#6608)
* fix(elasticache): enforce user permissions in backend ACL sessions * fix(elasticache): block replication commands for ACL users * fix(elasticache): deny persistence and migration commands
This commit is contained in:
1 parent
d58ce5c31c
commit
3890378728
8 files changed
+409
-7
No files matched your search
@@ -1695,6 +1695,12 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
return TransitEncryption.REQUIRED;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String accessString(String username) {
|
||||
ElastiCacheUser user = member(username);
|
||||
return user == null ? null : user.getAccessString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String ownerAccountId() {
|
||||
return account;
|
||||
@@ -2549,6 +2555,23 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
.map(ReplicationGroup::transitEncryption).orElse(TransitEncryption.REQUIRED);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String accessString(String username) {
|
||||
ReplicationGroup group = groups.getForAccount(accountId, groupId).orElse(null);
|
||||
if (group == null) {
|
||||
return null;
|
||||
}
|
||||
return effectiveUserIds(accountId, group).stream()
|
||||
.map(id -> users.getForAccount(accountId, id).orElse(null))
|
||||
.filter(user -> user != null && user.isEnabled() && username.equals(user.getUserName()))
|
||||
.map(ElastiCacheUser::getAccessString).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean clusterMode() {
|
||||
return groups.getForAccount(accountId, groupId).map(ReplicationGroup::isClusterEnabled).orElse(false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String ownerAccountId() {
|
||||
return accountId;
|
||||
|
||||
+12
-6
@@ -94,6 +94,10 @@ public abstract class AbstractRedisAuthProxy {
|
||||
/** Closes a socket, tolerating errors. Implementations may add their own logging. */
|
||||
protected abstract void closeQuietly(Socket socket);
|
||||
|
||||
/** Applies the validated frontend identity to a newly connected backend. */
|
||||
protected void initializeBackend(Socket backend, String username) throws IOException {
|
||||
}
|
||||
|
||||
/** Establishes the client transport before reading credentials. */
|
||||
protected ClientTransport clientTransport(Socket client) throws IOException {
|
||||
return ClientTransport.unencrypted(client);
|
||||
@@ -205,7 +209,7 @@ public abstract class AbstractRedisAuthProxy {
|
||||
}
|
||||
continue;
|
||||
}
|
||||
bridgeCommand(client, input, cmd);
|
||||
bridgeCommand(client, input, cmd, null);
|
||||
return;
|
||||
}
|
||||
if (authRequired()) {
|
||||
@@ -214,7 +218,7 @@ public abstract class AbstractRedisAuthProxy {
|
||||
return;
|
||||
}
|
||||
|
||||
bridgeCommand(client, input, cmd);
|
||||
bridgeCommand(client, input, cmd, null);
|
||||
return;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
@@ -252,8 +256,7 @@ public abstract class AbstractRedisAuthProxy {
|
||||
return;
|
||||
}
|
||||
|
||||
writeResponse(client, OK_RESPONSE);
|
||||
bridgeCommand(client, input, null);
|
||||
bridgeCommand(client, input, null, username);
|
||||
}
|
||||
|
||||
private boolean handleHello(Socket client, InputStream input, String[] cmd) throws IOException {
|
||||
@@ -318,7 +321,7 @@ public abstract class AbstractRedisAuthProxy {
|
||||
return false;
|
||||
}
|
||||
|
||||
bridgeCommand(client, input, forwarded.toArray(String[]::new));
|
||||
bridgeCommand(client, input, forwarded.toArray(String[]::new), username);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -339,12 +342,15 @@ public abstract class AbstractRedisAuthProxy {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void bridgeCommand(Socket client, InputStream input, String[] command) throws IOException {
|
||||
private void bridgeCommand(Socket client, InputStream input, String[] command, String username) throws IOException {
|
||||
Socket backend = new Socket(backendHost, backendPort);
|
||||
try {
|
||||
backend.setTcpNoDelay(true);
|
||||
initializeBackend(backend, username);
|
||||
if (command != null) {
|
||||
resendCommand(command, backend.getOutputStream());
|
||||
} else {
|
||||
writeResponse(client, OK_RESPONSE);
|
||||
}
|
||||
client.setSoTimeout(0);
|
||||
handshakingClients.remove(client);
|
||||
|
||||
+28
@@ -30,6 +30,7 @@ public class ElastiCacheAuthProxy extends AbstractRedisAuthProxy {
|
||||
private final PasswordValidator passwordValidator;
|
||||
private final SigV4Validator sigV4Validator;
|
||||
private final Supplier<SSLContext> tlsContext;
|
||||
private final ElastiCacheBackendAcl backendAcl = new ElastiCacheBackendAcl();
|
||||
|
||||
public ElastiCacheAuthProxy(String groupId, AuthMode authMode,
|
||||
String backendHost, int backendPort,
|
||||
@@ -83,6 +84,25 @@ public class ElastiCacheAuthProxy extends AbstractRedisAuthProxy {
|
||||
return encrypted;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void initializeBackend(Socket backend, String username) throws IOException {
|
||||
boolean members = passwordValidator.hasMembers();
|
||||
if (!members && !backendAcl.initialized()) {
|
||||
return;
|
||||
}
|
||||
// Disabling default without configuring replication credentials breaks cluster nodes.
|
||||
// Until that path is implemented, fail closed rather than ignore the user's ACL.
|
||||
if (passwordValidator.clusterMode()) {
|
||||
throw new IOException("Cluster-mode user ACL transport is not supported yet");
|
||||
}
|
||||
String name = username == null || username.isEmpty() ? "default" : username;
|
||||
String access = members ? passwordValidator.accessString(name) : "on ~* &* +@all";
|
||||
if (access == null) {
|
||||
throw new IOException("Cache user is no longer associated with this resource");
|
||||
}
|
||||
backendAcl.authenticate(backend, name, access);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean authRequired() {
|
||||
if (passwordValidator.hasMembers()) {
|
||||
@@ -168,6 +188,14 @@ public class ElastiCacheAuthProxy extends AbstractRedisAuthProxy {
|
||||
return TransitEncryption.NONE;
|
||||
}
|
||||
|
||||
default String accessString(String username) {
|
||||
return null;
|
||||
}
|
||||
|
||||
default boolean clusterMode() {
|
||||
return false;
|
||||
}
|
||||
|
||||
default boolean hasMembers() {
|
||||
return false;
|
||||
}
|
||||
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
package io.github.hectorvent.floci.services.elasticache.proxy;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/** Keeps frontend passwords and IAM tokens out of the backend's private ACL credentials. */
|
||||
final class ElastiCacheBackendAcl {
|
||||
private final String administrator = "floci-admin-" + UUID.randomUUID();
|
||||
private final String administratorPassword = UUID.randomUUID().toString();
|
||||
private final Map<String, String> passwords = new LinkedHashMap<>();
|
||||
private volatile boolean initialized;
|
||||
|
||||
boolean initialized() {
|
||||
return initialized;
|
||||
}
|
||||
|
||||
synchronized void authenticate(Socket backend, String username, String accessString) throws IOException {
|
||||
List<String> accessRules = List.of(accessString.trim().split("\\s+"));
|
||||
if (accessRules.stream().anyMatch(rule -> rule.startsWith("("))) {
|
||||
// Selectors can grant permissions independently of the root command restrictions below.
|
||||
throw new IOException("Cache ACL selectors are not supported yet");
|
||||
}
|
||||
backend.setSoTimeout(10000);
|
||||
if (!initialized) {
|
||||
command(backend, List.of("ACL", "SETUSER", administrator, "reset", "on",
|
||||
">" + administratorPassword, "~*", "&*", "+@all"));
|
||||
command(backend, List.of("AUTH", administrator, administratorPassword));
|
||||
command(backend, List.of("ACL", "SETUSER", "default", "reset", "off"));
|
||||
initialized = true;
|
||||
} else {
|
||||
command(backend, List.of("AUTH", administrator, administratorPassword));
|
||||
}
|
||||
String backendName = "default".equals(username) ? "floci-default-" + administrator : username;
|
||||
String password = passwords.computeIfAbsent(backendName, ignored -> UUID.randomUUID().toString());
|
||||
List<String> rules = new ArrayList<>(List.of("ACL", "SETUSER", backendName, "reset"));
|
||||
rules.addAll(accessRules);
|
||||
// Authentication rules are owned by the proxy. A supplied nopass or password token
|
||||
// must never make an alternate backend identity publicly accessible.
|
||||
rules.addAll(List.of("resetpass", ">" + password, "-acl", "-config", "-debug", "-module",
|
||||
"-replicaof", "-slaveof", "-shutdown", "-monitor", "-slowlog", "-sync", "-psync",
|
||||
"-save", "-bgsave", "-bgrewriteaof", "-migrate"));
|
||||
command(backend, rules);
|
||||
command(backend, List.of("AUTH", backendName, password));
|
||||
backend.setSoTimeout(0);
|
||||
}
|
||||
|
||||
private static void command(Socket socket, List<String> arguments) throws IOException {
|
||||
OutputStream out = socket.getOutputStream();
|
||||
out.write(("*" + arguments.size() + "\r\n").getBytes(StandardCharsets.US_ASCII));
|
||||
for (String argument : arguments) {
|
||||
byte[] bytes = argument.getBytes(StandardCharsets.UTF_8);
|
||||
out.write(("$" + bytes.length + "\r\n").getBytes(StandardCharsets.US_ASCII));
|
||||
out.write(bytes);
|
||||
out.write("\r\n".getBytes(StandardCharsets.US_ASCII));
|
||||
}
|
||||
out.flush();
|
||||
InputStream in = socket.getInputStream();
|
||||
StringBuilder response = new StringBuilder();
|
||||
int value;
|
||||
while ((value = in.read()) != -1 && value != '\r' && response.length() < 4096) {
|
||||
response.append((char) value);
|
||||
}
|
||||
if (value != '\r' || in.read() != '\n' || !"+OK".contentEquals(response)) {
|
||||
// Never include server responses, access strings or credentials in this error.
|
||||
throw new IOException("Could not establish the backend cache user's permissions");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user