mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -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
+19
@@ -202,6 +202,25 @@ class ElastiCacheServerlessServiceTest {
|
||||
assertFalse(validator.getValue().hasMembers());
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyUserAccessStringIsAppliedToOpenSessionsOfServerlessCache() {
|
||||
ServerlessCache cache = service.createServerlessCache(create("cache", "valkey", "team"));
|
||||
ArgumentCaptor<ElastiCacheAuthProxy.PasswordValidator> validator = ArgumentCaptor.forClass(
|
||||
ElastiCacheAuthProxy.PasswordValidator.class);
|
||||
verify(proxies).startProxy(eq(cache.getBackingCacheClusterId()), any(), anyInt(), anyString(), anyInt(),
|
||||
validator.capture(), eq("cache"));
|
||||
|
||||
runtime.modifyUser("alice", null, null, "on ~allowed:* -@all +get", null, null);
|
||||
verify(proxies).refreshUser(cache.getBackingCacheClusterId(), "alice");
|
||||
assertEquals("on ~allowed:* -@all +get", validator.getValue().accessString("alice"));
|
||||
|
||||
runtime.modifyUser("alice", null, null, "off -@all", null, null);
|
||||
verify(proxies, times(2)).refreshUser(cache.getBackingCacheClusterId(), "alice");
|
||||
assertEquals("off -@all", validator.getValue().accessString("alice"));
|
||||
assertFalse(validator.getValue().validatePassword("alice", "password-a"));
|
||||
assertNull(validator.getValue().memberAuthMode("alice"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void validationAndFailedAllocationLeaveNoAssociationOrRecord() {
|
||||
error("UserGroupNotFound", () -> service.createServerlessCache(create("missing", "valkey", "absent")));
|
||||
|
||||
+79
@@ -1364,6 +1364,85 @@ class ElastiCacheServiceTest {
|
||||
svc.modifyUser("app-user-id", null, null, "on ~allowed:* -@all +get", null, null);
|
||||
account.set("000000000000");
|
||||
assertEquals("on ~allowed:* -@all +get", validator.getValue().accessString("app"));
|
||||
account.set("111111111111");
|
||||
svc.modifyUser("app-user-id", null, null, "off -@all", null, null);
|
||||
account.set("000000000000");
|
||||
assertEquals("off -@all", validator.getValue().accessString("app"));
|
||||
assertFalse(validator.getValue().validatePassword("app", "app-pass"));
|
||||
assertNull(validator.getValue().memberAuthMode("app"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyUserAccessStringIsAppliedToTheOpenSessionsOfTheGroupsThatHoldTheUser() {
|
||||
service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, null, "us-east-1");
|
||||
service.createReplicationGroup("other", "test", AuthMode.PASSWORD, null, "us-east-1");
|
||||
service.createUser("default-user-id", "default", AuthMode.PASSWORD, List.of("default-pass"),
|
||||
"on ~* +@all", null);
|
||||
service.createUser("app-user-id", "app", AuthMode.PASSWORD, List.of("app-pass"), "on ~* +@all", null);
|
||||
service.createUserGroup("app-group", "redis", List.of("default-user-id", "app-user-id"), "us-east-1");
|
||||
service.modifyReplicationGroup("grp", List.of("app-group"), null);
|
||||
|
||||
service.modifyUser("app-user-id", null, null, "on ~allowed:* -@all +get", null, null);
|
||||
|
||||
verify(proxyManager).refreshUser("grp", "app");
|
||||
verify(proxyManager, never()).refreshUser(eq("other"), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyUserAppendedAccessStringIsAppliedToTheOpenSessions() {
|
||||
service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, null, "us-east-1");
|
||||
service.createUser("default-user-id", "default", AuthMode.PASSWORD, List.of("default-pass"),
|
||||
"on ~* +@all", null);
|
||||
service.createUserGroup("app-group", "redis", List.of("default-user-id"), "us-east-1");
|
||||
service.modifyReplicationGroup("grp", List.of("app-group"), null);
|
||||
|
||||
service.modifyUser("default-user-id", null, null, null, "-flushall", null);
|
||||
|
||||
verify(proxyManager).refreshUser("grp", "default");
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyUserWithoutAnAccessStringChangeLeavesTheOpenSessionsAlone() {
|
||||
service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, null, "us-east-1");
|
||||
service.createUser("default-user-id", "default", AuthMode.PASSWORD, List.of("default-pass"),
|
||||
"on ~* +@all", null);
|
||||
service.createUserGroup("app-group", "redis", List.of("default-user-id"), "us-east-1");
|
||||
service.modifyReplicationGroup("grp", List.of("app-group"), null);
|
||||
|
||||
service.modifyUser("default-user-id", AuthMode.PASSWORD, List.of("new-pass"), null, null, null);
|
||||
|
||||
verify(proxyManager, never()).refreshUser(anyString(), anyString());
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyUserDisablingUserAppliesToTheOpenSessions() {
|
||||
service.createReplicationGroup("grp", "test", AuthMode.PASSWORD, null, "us-east-1");
|
||||
service.createUser("default-user-id", "default", AuthMode.PASSWORD, List.of("default-pass"),
|
||||
"on ~* +@all", null);
|
||||
service.createUser("app-user-id", "app", AuthMode.PASSWORD, List.of("app-pass"), "on ~* +@all", null);
|
||||
service.createUserGroup("app-group", "redis", List.of("default-user-id", "app-user-id"), "us-east-1");
|
||||
service.modifyReplicationGroup("grp", List.of("app-group"), null);
|
||||
|
||||
service.modifyUser("app-user-id", null, null, "off -@all", null, null);
|
||||
|
||||
verify(proxyManager).refreshUser("grp", "app");
|
||||
}
|
||||
|
||||
@Test
|
||||
void modifyUserAccessStringIsAppliedToAssociatedServerlessCaches() {
|
||||
ElastiCacheService.CreateCacheClusterRequest req = new ElastiCacheService.CreateCacheClusterRequest(
|
||||
"serverless-backing", "valkey", null, null, 1, null, AuthMode.NO_AUTH, null, null, null,
|
||||
0, null, null, null, null, null, null, false, "us-east-1", Map.of());
|
||||
service.createServerlessBacking(req, "my-serverless", "000000000000", "app-group");
|
||||
|
||||
service.createUser("default-user-id", "default", AuthMode.PASSWORD, List.of("default-pass"),
|
||||
"on ~* +@all", null);
|
||||
service.createUser("app-user-id", "app", AuthMode.PASSWORD, List.of("app-pass"), "on ~* +@all", null);
|
||||
service.createUserGroup("app-group", "valkey", List.of("default-user-id", "app-user-id"), "us-east-1");
|
||||
|
||||
service.modifyUser("app-user-id", null, null, "on ~allowed:* -@all +get", null, null);
|
||||
|
||||
verify(proxyManager).refreshUser("serverless-backing", "app");
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
+20
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.testutil.SigV4TokenTestHelper;
|
||||
import org.junit.jupiter.api.AfterAll;
|
||||
import org.junit.jupiter.api.Assumptions;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestInstance;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
@@ -190,6 +191,25 @@ class ElastiCacheAclProxyIntegrationTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void changedPermissionsApplyToAnAlreadyOpenSession() throws Exception {
|
||||
memberMode.set(AuthMode.PASSWORD);
|
||||
access.set("on ~allowed:* &* -@all +get +ping");
|
||||
try (Socket open = connect(proxy.localPort(), trust)) {
|
||||
assertEquals("+OK", command(open, "AUTH", "subscriber", "synthetic-password"));
|
||||
assertEquals("+PONG", command(open, "PING"));
|
||||
assertTrue(command(open, "SET", "allowed:key", "value").startsWith("-NOPERM"));
|
||||
|
||||
access.set("on ~allowed:* &* -@all +get +set +ping");
|
||||
proxy.refreshUser("subscriber");
|
||||
assertEquals("+OK", command(open, "SET", "allowed:key", "value"));
|
||||
|
||||
access.set("on ~allowed:* &* -@all +get +ping");
|
||||
proxy.refreshUser("subscriber");
|
||||
assertTrue(command(open, "SET", "allowed:key", "value").startsWith("-NOPERM"));
|
||||
}
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
void stopBackend() throws Exception {
|
||||
try {
|
||||
|
||||
+228
@@ -0,0 +1,228 @@
|
||||
package io.github.hectorvent.floci.services.elasticache.proxy;
|
||||
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.ServerSocket;
|
||||
import java.net.Socket;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
class ElastiCacheBackendAclTest {
|
||||
|
||||
private static final int WAIT_MILLIS = 5000;
|
||||
|
||||
private ServerSocket server;
|
||||
private final List<List<String>> commands = new CopyOnWriteArrayList<>();
|
||||
private final List<Socket> accepted = new CopyOnWriteArrayList<>();
|
||||
private volatile boolean refuseNextCommands;
|
||||
private ElastiCacheBackendAcl acl;
|
||||
|
||||
@BeforeEach
|
||||
void startBackend() throws IOException {
|
||||
server = new ServerSocket(0);
|
||||
acl = new ElastiCacheBackendAcl();
|
||||
Thread acceptor = new Thread(this::acceptConnections, "fake-backend");
|
||||
acceptor.setDaemon(true);
|
||||
acceptor.start();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void stopBackend() throws IOException {
|
||||
server.close();
|
||||
for (Socket socket : accepted) {
|
||||
socket.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void reapplyInstallsTheNewRulesOnTheBackendUserThatLoginCreated() throws Exception {
|
||||
login("app", "on ~app:* -@all +get");
|
||||
int afterLogin = commands.size();
|
||||
String sessionPassword = passwordOf("app");
|
||||
|
||||
acl.reapply("localhost", server.getLocalPort(), "app", "on ~app:* -@all +get +set");
|
||||
|
||||
List<List<String>> sent = commands.subList(afterLogin, commands.size());
|
||||
assertEquals(2, sent.size());
|
||||
assertEquals("AUTH", sent.get(0).get(0));
|
||||
assertTrue(sent.get(0).get(1).startsWith("floci-admin-"));
|
||||
assertEquals(List.of("ACL", "SETUSER", "app", "reset", "on", "~app:*", "-@all", "+get", "+set", "resetpass",
|
||||
">" + sessionPassword), sent.get(1).subList(0, 11));
|
||||
assertTrue(sent.get(1).contains("-acl"));
|
||||
assertTrue(sent.get(1).contains("-save"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void defaultUserIsReappliedToTheSameBackendUserLoginUsed() throws Exception {
|
||||
login("default", "on ~* +@all");
|
||||
String backendUser = commands.stream()
|
||||
.filter(c -> c.size() > 2 && "SETUSER".equals(c.get(1)) && c.get(2).startsWith("floci-default-"))
|
||||
.findFirst().orElseThrow().get(2);
|
||||
int afterLogin = commands.size();
|
||||
|
||||
acl.reapply("localhost", server.getLocalPort(), "default", "on ~allowed:* +get");
|
||||
|
||||
assertEquals(backendUser, commands.get(afterLogin + 1).get(2));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nothingIsSentBeforeTheBackendIsSetUp() throws Exception {
|
||||
acl.reapply("localhost", server.getLocalPort(), "app", "on ~* +@all");
|
||||
|
||||
assertEquals(0, accepted.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void nothingIsSentForAUserNobodyHasAuthenticatedAs() throws Exception {
|
||||
login("app", "on ~* +@all");
|
||||
int connections = accepted.size();
|
||||
|
||||
acl.reapply("localhost", server.getLocalPort(), "other", "on ~* +@all");
|
||||
|
||||
assertEquals(connections, accepted.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void selectorsAreRefusedWithoutTouchingTheBackend() throws Exception {
|
||||
login("app", "on ~* +@all");
|
||||
int connections = accepted.size();
|
||||
|
||||
assertThrows(IOException.class,
|
||||
() -> acl.reapply("localhost", server.getLocalPort(), "app", "on ~* -@all (+get ~*)"));
|
||||
|
||||
assertEquals(connections, accepted.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void backendRefusalIsReportedWithoutExposingItsResponse() throws Exception {
|
||||
login("app", "on ~* +@all");
|
||||
refuseNextCommands = true;
|
||||
|
||||
IOException failure = assertThrows(IOException.class,
|
||||
() -> acl.reapply("localhost", server.getLocalPort(), "app", "on ~* +@all"));
|
||||
|
||||
assertEquals("Could not establish the backend cache user's permissions", failure.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void rulesAreReadUnderTheLockSoStaleRulesCannotOverwriteNewerRules() throws Exception {
|
||||
login("app", "on ~app:* -@all +get");
|
||||
int afterLogin = commands.size();
|
||||
|
||||
AtomicReference<String> currentRules = new AtomicReference<>("on ~app:* -@all +get +set");
|
||||
acl.reapply("localhost", server.getLocalPort(), "app", currentRules::get);
|
||||
|
||||
currentRules.set("on ~app:* -@all +get");
|
||||
acl.reapply("localhost", server.getLocalPort(), "app", currentRules::get);
|
||||
|
||||
List<List<String>> sent = commands.subList(afterLogin, commands.size());
|
||||
assertEquals(4, sent.size());
|
||||
assertTrue(sent.get(1).contains("+set"));
|
||||
assertFalse(sent.get(3).contains("+set"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void disabledUserAccessStringIsReappliedToBackend() throws Exception {
|
||||
login("app", "on ~app:* -@all +get");
|
||||
int afterLogin = commands.size();
|
||||
|
||||
acl.reapply("localhost", server.getLocalPort(), "app", "off -@all");
|
||||
|
||||
List<List<String>> sent = commands.subList(afterLogin, commands.size());
|
||||
assertEquals(2, sent.size());
|
||||
assertEquals("ACL", sent.get(1).get(0));
|
||||
assertEquals("SETUSER", sent.get(1).get(1));
|
||||
assertEquals("app", sent.get(1).get(2));
|
||||
assertEquals("reset", sent.get(1).get(3));
|
||||
assertEquals("off", sent.get(1).get(4));
|
||||
assertEquals("-@all", sent.get(1).get(5));
|
||||
}
|
||||
|
||||
private void login(String username, String accessString) throws IOException {
|
||||
try (Socket backend = new Socket("localhost", server.getLocalPort())) {
|
||||
acl.authenticate(backend, username, accessString);
|
||||
}
|
||||
}
|
||||
|
||||
private String passwordOf(String backendUser) {
|
||||
return commands.stream()
|
||||
.filter(c -> c.size() > 2 && "SETUSER".equals(c.get(1)) && backendUser.equals(c.get(2)))
|
||||
.map(c -> c.get(c.indexOf("resetpass") + 1).substring(1))
|
||||
.reduce((first, second) -> second).orElseThrow();
|
||||
}
|
||||
|
||||
private void acceptConnections() {
|
||||
while (!server.isClosed()) {
|
||||
try {
|
||||
Socket socket = server.accept();
|
||||
accepted.add(socket);
|
||||
Thread handler = new Thread(() -> serve(socket), "fake-backend-connection");
|
||||
handler.setDaemon(true);
|
||||
handler.start();
|
||||
} catch (IOException e) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void serve(Socket socket) {
|
||||
try {
|
||||
InputStream in = socket.getInputStream();
|
||||
OutputStream out = socket.getOutputStream();
|
||||
while (true) {
|
||||
List<String> command = readCommand(in);
|
||||
if (command == null) {
|
||||
return;
|
||||
}
|
||||
commands.add(command);
|
||||
out.write((refuseNextCommands ? "-ERR refused\r\n" : "+OK\r\n").getBytes(StandardCharsets.US_ASCII));
|
||||
out.flush();
|
||||
}
|
||||
} catch (IOException expected) {
|
||||
// The test closed the connection, which is how every fake connection ends.
|
||||
}
|
||||
}
|
||||
|
||||
private static List<String> readCommand(InputStream in) throws IOException {
|
||||
String header = readLine(in);
|
||||
if (header == null) {
|
||||
return null;
|
||||
}
|
||||
int count = Integer.parseInt(header.substring(1));
|
||||
List<String> arguments = new ArrayList<>();
|
||||
for (int i = 0; i < count; i++) {
|
||||
int length = Integer.parseInt(readLine(in).substring(1));
|
||||
byte[] bytes = in.readNBytes(length);
|
||||
readLine(in);
|
||||
arguments.add(new String(bytes, StandardCharsets.UTF_8));
|
||||
}
|
||||
return arguments;
|
||||
}
|
||||
|
||||
private static String readLine(InputStream in) throws IOException {
|
||||
StringBuilder line = new StringBuilder();
|
||||
int value;
|
||||
while ((value = in.read()) != -1) {
|
||||
if (value == '\n') {
|
||||
return line.toString();
|
||||
}
|
||||
if (value != '\r') {
|
||||
line.append((char) value);
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user