mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
feat(elasticache): apply a changed access string to open sessions (#7101)
* feat(elasticache): apply a changed access string to open sessions ModifyUser with AccessString or AppendAccessString was installed on the backend only at the user's next authentication, so a connection that was already open kept its old permissions. The service now asks the proxy of every replication group that holds the user to install the new rules on the backend user the open sessions run as, through a short administrator connection. Nothing is sent before the backend is set up or for a user nobody has authenticated as, and an access string with a selector is refused as it is at login. * fix(elasticache): propagate live ACL updates to serverless and evaluate rules under lock
This commit is contained in:
1 parent
4d99aa9639
commit
3f53b2685d
10 files changed
+491
-23
No files matched your search
+36
-6
@@ -1654,7 +1654,7 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
String id = original.getCacheClusterId();
|
||||
String account = original.getOwnerAccountId();
|
||||
return new ElastiCacheAuthProxy.PasswordValidator() {
|
||||
private ElastiCacheUser member(String username) {
|
||||
private ElastiCacheUser member(String username, boolean requireEnabled) {
|
||||
String name = username == null || username.isEmpty() ? "default" : username;
|
||||
synchronized (users) {
|
||||
CacheCluster cluster = cacheClusters.getForAccount(account, id).orElse(null);
|
||||
@@ -1666,14 +1666,14 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
return null;
|
||||
}
|
||||
return group.getUserIds().stream().map(userId -> users.getForAccount(account, userId).orElse(null))
|
||||
.filter(user -> user != null && name.equals(user.getUserName()) && user.isEnabled())
|
||||
.filter(user -> user != null && name.equals(user.getUserName()) && (!requireEnabled || user.isEnabled()))
|
||||
.findFirst().orElse(null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean validatePassword(String username, String password) {
|
||||
ElastiCacheUser user = member(username);
|
||||
ElastiCacheUser user = member(username, true);
|
||||
return user != null && (user.getAuthMode() == AuthMode.NO_AUTH
|
||||
|| user.getAuthMode() == AuthMode.PASSWORD && user.getPasswords().contains(password));
|
||||
}
|
||||
@@ -1686,7 +1686,7 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
|
||||
@Override
|
||||
public AuthMode memberAuthMode(String username) {
|
||||
ElastiCacheUser user = member(username);
|
||||
ElastiCacheUser user = member(username, true);
|
||||
return user == null ? null : user.getAuthMode();
|
||||
}
|
||||
|
||||
@@ -1697,7 +1697,7 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
|
||||
@Override
|
||||
public String accessString(String username) {
|
||||
ElastiCacheUser user = member(username);
|
||||
ElastiCacheUser user = member(username, false);
|
||||
return user == null ? null : user.getAccessString();
|
||||
}
|
||||
|
||||
@@ -2141,6 +2141,36 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
// 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);
|
||||
ElastiCacheUser modified = applyUserModification(userId, user, authMode, passwords, accessString,
|
||||
appendAccessString, normalizedEngine);
|
||||
if (accessString != null || appendAccessString != null) {
|
||||
reapplyAccessString(userId, modified);
|
||||
}
|
||||
return modified;
|
||||
}
|
||||
|
||||
/** Lets the open sessions of every cache the user can sign in to pick up its new access string. */
|
||||
private void reapplyAccessString(String userId, ElastiCacheUser user) {
|
||||
for (ReplicationGroup group : groups.scan(k -> true)) {
|
||||
if (effectiveUserIds(groups.accountId(), group).contains(userId)) {
|
||||
proxyManager.refreshUser(group.getReplicationGroupId(), user.getUserName());
|
||||
}
|
||||
}
|
||||
for (CacheCluster cluster : cacheClusters.scan(k -> true)) {
|
||||
if (cluster.getServerlessCacheName() != null && cluster.getUserGroupId() != null) {
|
||||
String account = cluster.getOwnerAccountId() != null ? cluster.getOwnerAccountId() : cacheClusters.accountId();
|
||||
ElastiCacheUserGroup group = userGroups.getForAccount(account,
|
||||
normalizeUserGroupId(cluster.getUserGroupId())).orElse(null);
|
||||
if (group != null && group.getUserIds().contains(userId)) {
|
||||
proxyManager.refreshUser(cluster.getCacheClusterId(), user.getUserName());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private ElastiCacheUser applyUserModification(String userId, ElastiCacheUser user, AuthMode authMode,
|
||||
List<String> passwords, String accessString,
|
||||
String appendAccessString, String normalizedEngine) {
|
||||
synchronized (userGroupLock) {
|
||||
String nextEngine = normalizedEngine != null ? normalizedEngine : user.getEngine();
|
||||
AuthMode nextAuthMode = authMode != null ? authMode : user.getAuthMode();
|
||||
@@ -2563,7 +2593,7 @@ public class ElastiCacheService implements ResourceProvider {
|
||||
}
|
||||
return effectiveUserIds(accountId, group).stream()
|
||||
.map(id -> users.getForAccount(accountId, id).orElse(null))
|
||||
.filter(user -> user != null && user.isEnabled() && username.equals(user.getUserName()))
|
||||
.filter(user -> user != null && username.equals(user.getUserName()))
|
||||
.map(ElastiCacheUser::getAccessString).findFirst().orElse(null);
|
||||
}
|
||||
|
||||
|
||||
+8
@@ -86,6 +86,14 @@ public abstract class AbstractRedisAuthProxy {
|
||||
this.handshakeTimeoutMillis = handshakeTimeoutMillis;
|
||||
}
|
||||
|
||||
protected final String backendHost() {
|
||||
return backendHost;
|
||||
}
|
||||
|
||||
protected final int backendPort() {
|
||||
return backendPort;
|
||||
}
|
||||
|
||||
/** Whether the {@code AUTH} command is required before bridging to the backend. */
|
||||
protected abstract boolean authRequired();
|
||||
|
||||
|
||||
+15
-4
@@ -96,11 +96,22 @@ public class ElastiCacheAuthProxy extends AbstractRedisAuthProxy {
|
||||
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,
|
||||
() -> members ? passwordValidator.accessString(name) : "on ~* &* +@all");
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies the user's current access string to the backend user that its open sessions run as. A
|
||||
* failure is logged and the sessions keep their old permissions until the user authenticates again.
|
||||
*/
|
||||
public void refreshUser(String username) {
|
||||
String name = username == null || username.isEmpty() ? "default" : username;
|
||||
try {
|
||||
backendAcl.reapply(backendHost(), backendPort(), name, () -> passwordValidator.accessString(name));
|
||||
} catch (IOException e) {
|
||||
LOG.warnv(e, "Could not apply the new permissions of user {0} to the open sessions of group {1}",
|
||||
name, groupId);
|
||||
}
|
||||
backendAcl.authenticate(backend, name, access);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
+70
-10
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.elasticache.proxy;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
@@ -10,9 +11,13 @@ import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/** Keeps frontend passwords and IAM tokens out of the backend's private ACL credentials. */
|
||||
final class ElastiCacheBackendAcl {
|
||||
private static final int CONNECT_TIMEOUT_MILLIS = 5000;
|
||||
private static final int COMMAND_TIMEOUT_MILLIS = 10000;
|
||||
|
||||
private final String administrator = "floci-admin-" + UUID.randomUUID();
|
||||
private final String administratorPassword = UUID.randomUUID().toString();
|
||||
private final Map<String, String> passwords = new LinkedHashMap<>();
|
||||
@@ -22,13 +27,14 @@ final class ElastiCacheBackendAcl {
|
||||
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");
|
||||
synchronized void authenticate(Socket backend, String username, Supplier<String> accessStringSupplier)
|
||||
throws IOException {
|
||||
String accessString = accessStringSupplier.get();
|
||||
if (accessString == null) {
|
||||
throw new IOException("Cache user is no longer associated with this resource");
|
||||
}
|
||||
backend.setSoTimeout(10000);
|
||||
List<String> accessRules = accessRules(accessString);
|
||||
backend.setSoTimeout(COMMAND_TIMEOUT_MILLIS);
|
||||
if (!initialized) {
|
||||
command(backend, List.of("ACL", "SETUSER", administrator, "reset", "on",
|
||||
">" + administratorPassword, "~*", "&*", "+@all"));
|
||||
@@ -38,8 +44,64 @@ final class ElastiCacheBackendAcl {
|
||||
} else {
|
||||
command(backend, List.of("AUTH", administrator, administratorPassword));
|
||||
}
|
||||
String backendName = "default".equals(username) ? "floci-default-" + administrator : username;
|
||||
String backendName = backendName(username);
|
||||
String password = passwords.computeIfAbsent(backendName, ignored -> UUID.randomUUID().toString());
|
||||
command(backend, userRules(backendName, accessRules, password));
|
||||
command(backend, List.of("AUTH", backendName, password));
|
||||
backend.setSoTimeout(0);
|
||||
}
|
||||
|
||||
void authenticate(Socket backend, String username, String accessString) throws IOException {
|
||||
authenticate(backend, username, () -> accessString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Installs a changed access string on the backend user that sessions of {@code username} already run
|
||||
* as, so it applies to their open connections and not only to the next login. Nothing is done while
|
||||
* the backend has not been set up, or for a user nobody has authenticated as yet: the next login
|
||||
* installs the current rules.
|
||||
*/
|
||||
synchronized void reapply(String backendHost, int backendPort, String username,
|
||||
Supplier<String> accessStringSupplier) throws IOException {
|
||||
if (!initialized) {
|
||||
return;
|
||||
}
|
||||
String backendName = backendName(username);
|
||||
String password = passwords.get(backendName);
|
||||
if (password == null) {
|
||||
return;
|
||||
}
|
||||
String accessString = accessStringSupplier.get();
|
||||
if (accessString == null) {
|
||||
return;
|
||||
}
|
||||
List<String> accessRules = accessRules(accessString);
|
||||
try (Socket backend = new Socket()) {
|
||||
backend.connect(new InetSocketAddress(backendHost, backendPort), CONNECT_TIMEOUT_MILLIS);
|
||||
backend.setSoTimeout(COMMAND_TIMEOUT_MILLIS);
|
||||
command(backend, List.of("AUTH", administrator, administratorPassword));
|
||||
command(backend, userRules(backendName, accessRules, password));
|
||||
}
|
||||
}
|
||||
|
||||
void reapply(String backendHost, int backendPort, String username, String accessString) throws IOException {
|
||||
reapply(backendHost, backendPort, username, () -> accessString);
|
||||
}
|
||||
|
||||
private String backendName(String username) {
|
||||
return "default".equals(username) ? "floci-default-" + administrator : username;
|
||||
}
|
||||
|
||||
private static List<String> accessRules(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");
|
||||
}
|
||||
return accessRules;
|
||||
}
|
||||
|
||||
private static List<String> userRules(String backendName, List<String> accessRules, String password) {
|
||||
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
|
||||
@@ -47,9 +109,7 @@ final class ElastiCacheBackendAcl {
|
||||
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);
|
||||
return rules;
|
||||
}
|
||||
|
||||
private static void command(Socket socket, List<String> arguments) throws IOException {
|
||||
|
||||
+8
@@ -64,6 +64,14 @@ public class ElastiCacheProxyManager {
|
||||
}
|
||||
}
|
||||
|
||||
/** Applies a changed access string of {@code username} to the open sessions of the group's proxy. */
|
||||
public void refreshUser(String groupId, String username) {
|
||||
ElastiCacheAuthProxy proxy = proxies.get(groupId);
|
||||
if (proxy != null) {
|
||||
proxy.refreshUser(username);
|
||||
}
|
||||
}
|
||||
|
||||
public void stopAll() {
|
||||
proxies.values().forEach(ElastiCacheAuthProxy::stop);
|
||||
proxies.clear();
|
||||
|
||||
Reference in new issue
Block a user