mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(elasticache): restore groups and clusters of every account (#5244)
* fix(elasticache): restore groups and clusters of every account restorePersistedRuntime scanned the replication group and cache cluster stores with scan(k -> true), which is scoped to the calling account. At boot there is no request context, so only the default account's records were re-provisioned: groups and clusters of any other account kept a stale status with no data plane, and their proxy ports were never reserved, so the next create could be handed the same port. Scan every account with scanAllAccountEntries and carry the owning account through the restore path, so each write-back, and the deleted-while-restoring check, works on the owner's record instead of copying it into the default account. Unprefixed group and cache cluster records from before account prefixing are moved under the default account first, as get() does on first read, so they are restored once and a delete does not leave a legacy copy to come back on the next boot. The proxy authenticates on its own thread, outside any request, so a validator that looked the group or cluster up by id alone read the default account. The validator now carries the owning account captured when the proxy is registered, and reads the group, cluster, user groups and users for that account. The set of records holding their port is keyed by account and id, so deleting one of two same-named groups in different accounts no longer leaks the other's port. * fix(elasticache): read a group's auth mode under its owning account The proxy's authMode() callback still looked the group up by id alone. ModifyReplicationGroup does not restart the proxy, so that callback is how it learns a group dropped its last user group. On the proxy thread the read landed in the default account, so another account's proxy kept its starting mode, or picked up a same-named default-account group's mode. Read it with getForAccount, like the other callbacks. Moves the cross-account restore and proxy tests into ElastiCacheServiceTest and adds one for the auth-mode read. * fix(elasticache): check a proxy's IAM tokens against the cache's own account The auth proxy validates IAM tokens on its own thread, outside any request, so the long-term access key lookup fell back to the default account: a replication group or serverless cache of any other account rejected tokens signed with its own account's keys, restored or not. The proxy now passes the owning account its authenticator already captures, and SigV4RequestValidator resolves the key with findSecretKeyInAnyAccount, accepting it only when that account holds it. Temporary session keys are found whichever account issued them, as before. Callers that pass no account, RDS and MemoryDB among them, keep the existing lookup. * test(compat): poll SDK waiters once a second and let ECS test tasks exit on SIGTERM The slowest sdk-test-java classes spent most of their time sleeping rather than testing. SDK waiters sleep out the published AWS cadence between checks: 6 s for ECS TasksStopped, 15 s for ServicesStable and 30 s for the CloudFormation and EKS waiters, where Floci has usually settled within one. TestFixtures.waiterOverride(timeout) keeps each call's timeout, and against Floci checks once a second with an attempt limit to match; against real AWS the waiter keeps its own cadence. The ECS, EKS and CloudFormation waiters in these classes now use it. The capacity-provider service test and the one-shot task's explicit-stop test ran a bare sleep as the container's PID 1, which has no default SIGTERM handler, so every stop sat out the whole stopTimeout grace period, four times per case for deleteService. They now trap TERM, as the ECS and EKS Docker integration tests already do. * test(ec2): let the volume test's instance containers exit on SIGTERM Ec2VolumeBlockDeviceDockerIntegrationTest ran its instance and cluster-node containers with a bare "sleep 3600" as PID 1, which has no default SIGTERM handler, so every stop sat out the whole grace period: the 30 s Ec2ContainerManager.stop gives a container when the root-device test stops its instance, and the 2 s of the cleanup stop after every test. Both containers now trap TERM, as the ECS and EKS Docker integration tests already do. --------- Co-authored-by: Priamo <pgermosen@gmail.com>
This commit is contained in:
1 parent
80719fc62a
commit
05e1bebe1e
16 files changed
+588
-128
No files matched your search
+5
-2
@@ -161,7 +161,9 @@ class Ec2VolumeBlockDeviceDockerIntegrationTest {
|
||||
ContainerSpec spec = containerBuilder.newContainer("alpine:3.21")
|
||||
.withName(containerName)
|
||||
.withPrivileged(true)
|
||||
.withEntrypoint(List.of("sleep", "3600"))
|
||||
// PID 1 gets no default SIGTERM handler, so a bare "sleep 3600" sits out the whole
|
||||
// grace period of every stop: 30 seconds when stopInstances stops it, 2 in cleanup.
|
||||
.withEntrypoint(List.of("sh", "-c", "trap 'exit 0' TERM; sleep 3600 & wait"))
|
||||
.build();
|
||||
|
||||
String containerId = lifecycleManager.createAndStart(spec).containerId();
|
||||
@@ -337,7 +339,8 @@ class Ec2VolumeBlockDeviceDockerIntegrationTest {
|
||||
ContainerSpec spec = containerBuilder.newContainer("alpine:3.21")
|
||||
.withName(containerName)
|
||||
.withPrivileged(true)
|
||||
.withEntrypoint(List.of("sleep", "3600"))
|
||||
// As in createRunningTestInstance: lets cleanup's stop return at once.
|
||||
.withEntrypoint(List.of("sh", "-c", "trap 'exit 0' TERM; sleep 3600 & wait"))
|
||||
.build();
|
||||
|
||||
String containerId = lifecycleManager.createAndStart(spec).containerId();
|
||||
|
||||
+1
@@ -357,6 +357,7 @@ class ElastiCacheServerlessServiceTest {
|
||||
validator.capture(), eq("account-cache"));
|
||||
assertTrue(validator.getValue().validatePassword("alice", "owner-secret"));
|
||||
assertFalse(validator.getValue().validatePassword("alice", "password-a"));
|
||||
assertEquals("111111111111", validator.getValue().ownerAccountId());
|
||||
AccountAwareStorageBackend<CacheCluster> backing = factory.create("elasticache", "elasticache-redis-clusters.json", null);
|
||||
assertTrue(backing.getForAccount("000000000000", original.getBackingCacheClusterId()).isEmpty());
|
||||
assertTrue(backing.getForAccount("111111111111", original.getBackingCacheClusterId()).isPresent());
|
||||
|
||||
+248
@@ -24,6 +24,7 @@ import io.github.hectorvent.floci.services.elasticache.model.Endpoint;
|
||||
import io.github.hectorvent.floci.services.elasticache.model.ReplicationGroup;
|
||||
import io.github.hectorvent.floci.services.elasticache.model.ReplicationGroupSettings;
|
||||
import io.github.hectorvent.floci.services.elasticache.model.ReplicationGroupStatus;
|
||||
import io.github.hectorvent.floci.services.elasticache.proxy.ElastiCacheAuthProxy;
|
||||
import io.github.hectorvent.floci.services.elasticache.proxy.ElastiCacheProxyManager;
|
||||
import io.github.hectorvent.floci.services.kms.KmsService;
|
||||
import io.github.hectorvent.floci.services.kms.model.KmsKey;
|
||||
@@ -970,6 +971,253 @@ class ElastiCacheServiceTest {
|
||||
"A restored group's port must be reserved again so new groups cannot take it");
|
||||
}
|
||||
|
||||
@Test
|
||||
void restorePersistedRuntimeRestoresEveryAccountUnderItsOwnAccount() {
|
||||
String other = "111111111111";
|
||||
StorageFactory storageFactory = sharedStorageFactory();
|
||||
ElastiCacheContainerManager beforeRestart = mock(ElastiCacheContainerManager.class);
|
||||
stubSingleNodeContainer(beforeRestart);
|
||||
ElastiCacheService before = serviceWith(storageFactory, beforeRestart,
|
||||
mock(ElastiCacheProxyManager.class), mock(ValkeyClusterFormation.class));
|
||||
before.createReplicationGroup("grp", "test", AuthMode.NO_AUTH, null, "us-east-1");
|
||||
before.createCacheCluster(cacheClusterRequest("cc", "redis", 1));
|
||||
// Move both records to a non-default account, as a request under that account would have stored them.
|
||||
AccountAwareStorageBackend<ReplicationGroup> groups =
|
||||
storageFactory.<ReplicationGroup>create("elasticache", "elasticache-groups.json", null);
|
||||
AccountAwareStorageBackend<CacheCluster> clusters =
|
||||
storageFactory.<CacheCluster>create("elasticache", "elasticache-redis-clusters.json", null);
|
||||
groups.putForAccount(other, "grp", groups.getForAccount("000000000000", "grp").orElseThrow());
|
||||
groups.deleteForAccount("000000000000", "grp");
|
||||
clusters.putForAccount(other, "cc", clusters.getForAccount("000000000000", "cc").orElseThrow());
|
||||
clusters.deleteForAccount("000000000000", "cc");
|
||||
|
||||
ElastiCacheContainerManager restartedContainers = mock(ElastiCacheContainerManager.class);
|
||||
stubSingleNodeContainer(restartedContainers);
|
||||
ElastiCacheService restarted = serviceWith(storageFactory, restartedContainers,
|
||||
mock(ElastiCacheProxyManager.class), mock(ValkeyClusterFormation.class));
|
||||
|
||||
restarted.restorePersistedRuntime().join();
|
||||
|
||||
verify(restartedContainers).tryStart(eq("grp"), anyString(), any());
|
||||
verify(restartedContainers).tryStart(eq("cc"), anyString(), any());
|
||||
assertEquals(ReplicationGroupStatus.AVAILABLE,
|
||||
groups.getForAccount(other, "grp").orElseThrow().getStatus());
|
||||
assertEquals(CacheClusterStatus.AVAILABLE,
|
||||
clusters.getForAccount(other, "cc").orElseThrow().getCacheClusterStatus());
|
||||
assertTrue(groups.getForAccount("000000000000", "grp").isEmpty(),
|
||||
"A restore must not copy another account's group into the default account");
|
||||
assertTrue(clusters.getForAccount("000000000000", "cc").isEmpty(),
|
||||
"A restore must not copy another account's cluster into the default account");
|
||||
|
||||
ReplicationGroup next =
|
||||
restarted.createReplicationGroup("grp2", "test", AuthMode.NO_AUTH, null, "us-east-1");
|
||||
assertEquals(16381, next.getProxyPort(),
|
||||
"Both restored records' ports (16379, 16380) must be reserved again");
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoreMigratesUnprefixedLegacyRecordsInsteadOfRestoringThemEveryBoot() {
|
||||
InMemoryStorage<String, Object> rawGroups = new InMemoryStorage<>();
|
||||
InMemoryStorage<String, Object> rawClusters = new InMemoryStorage<>();
|
||||
StorageFactory storageFactory = mock(StorageFactory.class);
|
||||
Map<String, Object> backends = new ConcurrentHashMap<>();
|
||||
backends.put("elasticache-groups.json", new AccountAwareStorageBackend<>(rawGroups, null, "000000000000"));
|
||||
backends.put("elasticache-redis-clusters.json",
|
||||
new AccountAwareStorageBackend<>(rawClusters, null, "000000000000"));
|
||||
when(storageFactory.create(anyString(), anyString(), any())).thenAnswer(inv ->
|
||||
backends.computeIfAbsent(inv.getArgument(1, String.class),
|
||||
key -> AccountAwareStorageBackend.inMemory("000000000000")));
|
||||
ElastiCacheContainerManager beforeRestart = mock(ElastiCacheContainerManager.class);
|
||||
stubSingleNodeContainer(beforeRestart);
|
||||
ElastiCacheService before = serviceWith(storageFactory, beforeRestart,
|
||||
mock(ElastiCacheProxyManager.class), mock(ValkeyClusterFormation.class));
|
||||
before.createReplicationGroup("grp", "test", AuthMode.NO_AUTH, null, "us-east-1");
|
||||
before.createCacheCluster(cacheClusterRequest("cc", "redis", 1));
|
||||
// Rewrite both records under the unprefixed key data from before account prefixing used.
|
||||
rawGroups.put("grp", rawGroups.get("000000000000/grp").orElseThrow());
|
||||
rawGroups.delete("000000000000/grp");
|
||||
rawClusters.put("cc", rawClusters.get("000000000000/cc").orElseThrow());
|
||||
rawClusters.delete("000000000000/cc");
|
||||
|
||||
for (int boot = 1; boot <= 2; boot++) {
|
||||
ElastiCacheContainerManager containers = mock(ElastiCacheContainerManager.class);
|
||||
stubSingleNodeContainer(containers);
|
||||
serviceWith(storageFactory, containers, mock(ElastiCacheProxyManager.class),
|
||||
mock(ValkeyClusterFormation.class)).restorePersistedRuntime().join();
|
||||
verify(containers, times(1)).tryStart(eq("grp"), anyString(), any());
|
||||
verify(containers, times(1)).tryStart(eq("cc"), anyString(), any());
|
||||
}
|
||||
assertEquals(Set.of("000000000000/grp"), rawGroups.keys(),
|
||||
"The legacy key must be migrated, not left beside the restored copy");
|
||||
assertEquals(Set.of("000000000000/cc"), rawClusters.keys(),
|
||||
"The legacy key must be migrated, not left beside the restored copy");
|
||||
}
|
||||
|
||||
/**
|
||||
* Storage whose implicit account is whatever {@code account} holds, as a request's context
|
||||
* sets it, so a test can act as another account and then fall back to the default one, the
|
||||
* way the proxy thread does.
|
||||
*/
|
||||
private static StorageFactory storageFollowing(AtomicReference<String> account) {
|
||||
RequestContext requestContext = mock(RequestContext.class);
|
||||
when(requestContext.getAccountId()).thenAnswer(inv -> account.get());
|
||||
@SuppressWarnings("unchecked")
|
||||
Instance<RequestContext> requestContextInstance = mock(Instance.class);
|
||||
when(requestContextInstance.get()).thenReturn(requestContext);
|
||||
StorageFactory storageFactory = mock(StorageFactory.class);
|
||||
Map<String, Object> backends = new ConcurrentHashMap<>();
|
||||
when(storageFactory.create(anyString(), anyString(), any())).thenAnswer(inv ->
|
||||
backends.computeIfAbsent(inv.getArgument(1, String.class), key ->
|
||||
new AccountAwareStorageBackend<>(new InMemoryStorage<>(), requestContextInstance,
|
||||
"000000000000")));
|
||||
return storageFactory;
|
||||
}
|
||||
|
||||
private static void seedPasswordGroup(StorageFactory storageFactory, String accountId, int port) {
|
||||
ReplicationGroup group = new ReplicationGroup();
|
||||
group.setReplicationGroupId("grp");
|
||||
group.setStatus(ReplicationGroupStatus.AVAILABLE);
|
||||
group.setProxyPort(port);
|
||||
group.setAuthMode(AuthMode.PASSWORD);
|
||||
group.setAuthToken("secret-token");
|
||||
storageFactory.<ReplicationGroup>create("elasticache", "elasticache-groups.json", null)
|
||||
.putForAccount(accountId, "grp", group);
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoredGroupOfAnotherAccountAcceptsItsPassword() {
|
||||
StorageFactory storageFactory = storageFollowing(new AtomicReference<>("000000000000"));
|
||||
ElastiCacheService svc = serviceWith(storageFactory, containerManager, proxyManager, clusterFormation);
|
||||
seedPasswordGroup(storageFactory, "111111111111", 16379);
|
||||
|
||||
svc.restorePersistedRuntime().join();
|
||||
|
||||
ArgumentCaptor<ElastiCacheAuthProxy.PasswordValidator> validator =
|
||||
ArgumentCaptor.forClass(ElastiCacheAuthProxy.PasswordValidator.class);
|
||||
verify(proxyManager).startProxy(eq("grp"), eq(AuthMode.PASSWORD), eq(16379), anyString(), anyInt(),
|
||||
validator.capture());
|
||||
assertTrue(validator.getValue().validatePassword(null, "secret-token"));
|
||||
assertEquals("111111111111", validator.getValue().ownerAccountId(),
|
||||
"IAM tokens must be checked against the restored group's own account");
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletingSameNamedRestoredGroupsOfTwoAccountsReleasesBothPorts() {
|
||||
AtomicReference<String> account = new AtomicReference<>("000000000000");
|
||||
StorageFactory storageFactory = storageFollowing(account);
|
||||
ElastiCacheService svc = serviceWith(storageFactory, containerManager, proxyManager, clusterFormation);
|
||||
seedPasswordGroup(storageFactory, "000000000000", 16379);
|
||||
seedPasswordGroup(storageFactory, "111111111111", 16380);
|
||||
svc.restorePersistedRuntime().join();
|
||||
|
||||
svc.deleteReplicationGroup("grp");
|
||||
account.set("111111111111");
|
||||
svc.deleteReplicationGroup("grp");
|
||||
|
||||
assertEquals(16379, svc.createReplicationGroup("new1", "test", AuthMode.NO_AUTH, null,
|
||||
"us-east-1").getProxyPort());
|
||||
assertEquals(16380, svc.createReplicationGroup("new2", "test", AuthMode.NO_AUTH, null,
|
||||
"us-east-1").getProxyPort());
|
||||
}
|
||||
|
||||
@Test
|
||||
void restoredCacheClusterOfAnotherAccountAcceptsItsPassword() {
|
||||
StorageFactory storageFactory = storageFollowing(new AtomicReference<>("000000000000"));
|
||||
ElastiCacheService svc = serviceWith(storageFactory, containerManager, proxyManager, clusterFormation);
|
||||
CacheCluster cluster = new CacheCluster();
|
||||
cluster.setCacheClusterId("cc");
|
||||
cluster.setCacheClusterStatus(CacheClusterStatus.AVAILABLE);
|
||||
cluster.setConfigurationEndpoint(new Endpoint("localhost", 16379));
|
||||
cluster.setAuthMode(AuthMode.PASSWORD);
|
||||
cluster.setAuthToken("secret-token");
|
||||
storageFactory.<CacheCluster>create("elasticache", "elasticache-redis-clusters.json", null)
|
||||
.putForAccount("111111111111", "cc", cluster);
|
||||
|
||||
svc.restorePersistedRuntime().join();
|
||||
|
||||
ArgumentCaptor<ElastiCacheAuthProxy.PasswordValidator> validator =
|
||||
ArgumentCaptor.forClass(ElastiCacheAuthProxy.PasswordValidator.class);
|
||||
verify(proxyManager).startProxy(eq("cc"), eq(AuthMode.PASSWORD), eq(16379), anyString(), anyInt(),
|
||||
validator.capture());
|
||||
assertTrue(validator.getValue().validatePassword(null, "secret-token"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void groupCreatedUnderAnotherAccountAcceptsItsPasswordOutsideTheRequest() {
|
||||
AtomicReference<String> account = new AtomicReference<>("111111111111");
|
||||
ElastiCacheService svc = serviceWith(storageFollowing(account), containerManager, proxyManager,
|
||||
clusterFormation);
|
||||
svc.createReplicationGroup("grp", "test", AuthMode.PASSWORD, "secret-token", "us-east-1");
|
||||
ArgumentCaptor<ElastiCacheAuthProxy.PasswordValidator> validator =
|
||||
ArgumentCaptor.forClass(ElastiCacheAuthProxy.PasswordValidator.class);
|
||||
verify(proxyManager).startProxy(eq("grp"), eq(AuthMode.PASSWORD), anyInt(), anyString(), anyInt(),
|
||||
validator.capture());
|
||||
|
||||
// The proxy authenticates on its own thread, where the store falls back to the default account.
|
||||
account.set("000000000000");
|
||||
assertTrue(validator.getValue().validatePassword(null, "secret-token"));
|
||||
assertEquals("111111111111", validator.getValue().ownerAccountId());
|
||||
}
|
||||
|
||||
@Test
|
||||
void userGroupMemberOfAnotherAccountAuthenticatesOutsideTheRequest() {
|
||||
AtomicReference<String> account = new AtomicReference<>("111111111111");
|
||||
ElastiCacheService svc = serviceWith(storageFollowing(account), containerManager, proxyManager,
|
||||
clusterFormation);
|
||||
svc.createReplicationGroup("grp", "test", AuthMode.PASSWORD, null, "us-east-1");
|
||||
svc.createUser("default-user-id", "default", AuthMode.PASSWORD, List.of("default-pass"),
|
||||
"on ~* +@all", null);
|
||||
svc.createUser("app-user-id", "app", AuthMode.PASSWORD, List.of("app-pass"), "on ~* +@all", null);
|
||||
svc.createUserGroup("app-group", "redis", List.of("default-user-id", "app-user-id"), "us-east-1");
|
||||
svc.modifyReplicationGroup("grp", List.of("app-group"), null);
|
||||
ArgumentCaptor<ElastiCacheAuthProxy.PasswordValidator> validator =
|
||||
ArgumentCaptor.forClass(ElastiCacheAuthProxy.PasswordValidator.class);
|
||||
verify(proxyManager).startProxy(eq("grp"), any(), anyInt(), anyString(), anyInt(), validator.capture());
|
||||
|
||||
// The proxy authenticates on its own thread, where the store falls back to the default account.
|
||||
account.set("000000000000");
|
||||
assertTrue(validator.getValue().validatePassword("app", "app-pass"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void groupOfAnotherAccountDroppingItsLastUserGroupChangesTheProxyAuthModeOutsideTheRequest() {
|
||||
AtomicReference<String> account = new AtomicReference<>("111111111111");
|
||||
ElastiCacheService svc = serviceWith(storageFollowing(account), containerManager, proxyManager,
|
||||
clusterFormation);
|
||||
svc.createReplicationGroup("grp", "test", AuthMode.PASSWORD, "group-token", "us-east-1");
|
||||
svc.createUser("default-user-id", "default", AuthMode.PASSWORD, List.of("default-pass"),
|
||||
"on ~* +@all", null);
|
||||
svc.createUserGroup("redis-users", "redis", List.of("default-user-id"), "us-east-1");
|
||||
svc.modifyReplicationGroup("grp", List.of("redis-users"), null, false, true,
|
||||
ReplicationGroupSettings.unchanged());
|
||||
svc.modifyReplicationGroup("grp", null, List.of("redis-users"));
|
||||
ArgumentCaptor<ElastiCacheAuthProxy.PasswordValidator> validator =
|
||||
ArgumentCaptor.forClass(ElastiCacheAuthProxy.PasswordValidator.class);
|
||||
verify(proxyManager).startProxy(eq("grp"), eq(AuthMode.PASSWORD), anyInt(), anyString(), anyInt(),
|
||||
validator.capture());
|
||||
|
||||
// ModifyReplicationGroup does not restart the proxy, which reads the mode on its own
|
||||
// thread, where the store falls back to the default account.
|
||||
account.set("000000000000");
|
||||
assertEquals(AuthMode.NO_AUTH, validator.getValue().authMode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void cacheClusterCreatedUnderAnotherAccountAcceptsItsPasswordOutsideTheRequest() {
|
||||
AtomicReference<String> account = new AtomicReference<>("111111111111");
|
||||
ElastiCacheService svc = serviceWith(storageFollowing(account), containerManager, proxyManager,
|
||||
clusterFormation);
|
||||
svc.createCacheCluster(cacheClusterRequest("cc", "redis", null, AuthMode.PASSWORD, "secret-token", null));
|
||||
ArgumentCaptor<ElastiCacheAuthProxy.PasswordValidator> validator =
|
||||
ArgumentCaptor.forClass(ElastiCacheAuthProxy.PasswordValidator.class);
|
||||
verify(proxyManager).startProxy(eq("cc"), eq(AuthMode.PASSWORD), anyInt(), anyString(), anyInt(),
|
||||
validator.capture());
|
||||
|
||||
// The proxy authenticates on its own thread, where the store falls back to the default account.
|
||||
account.set("000000000000");
|
||||
assertTrue(validator.getValue().validatePassword(null, "secret-token"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void singleNodeRestoreFailureReportsCreateFailedAndReleasesThePort() {
|
||||
StorageFactory storageFactory = storageWithSingleNodeGroup("grp");
|
||||
|
||||
+56
-5
@@ -68,8 +68,8 @@ class ElastiCacheAuthProxyTest {
|
||||
@Test
|
||||
void groupWithPasswordAuthModeAuthenticatesIamUserWhenAssociated() {
|
||||
SigV4Validator sigV4Validator = mock(SigV4Validator.class);
|
||||
when(sigV4Validator.validate("valid-sigv4", "grp-2", "iam-user")).thenReturn(true);
|
||||
when(sigV4Validator.validate("bad-sigv4", "grp-2", "iam-user")).thenReturn(false);
|
||||
when(sigV4Validator.validate("valid-sigv4", "grp-2", "iam-user", null)).thenReturn(true);
|
||||
when(sigV4Validator.validate("bad-sigv4", "grp-2", "iam-user", null)).thenReturn(false);
|
||||
|
||||
ElastiCacheAuthProxy.PasswordValidator passwordValidator = new ElastiCacheAuthProxy.PasswordValidator() {
|
||||
@Override
|
||||
@@ -157,7 +157,7 @@ class ElastiCacheAuthProxyTest {
|
||||
@Test
|
||||
void groupWithMembersNormalizesEmptyUsernameToDefaultForIamValidation() {
|
||||
SigV4Validator sigV4Validator = mock(SigV4Validator.class);
|
||||
when(sigV4Validator.validate("valid-sigv4", "grp-empty", "default")).thenReturn(true);
|
||||
when(sigV4Validator.validate("valid-sigv4", "grp-empty", "default", null)).thenReturn(true);
|
||||
|
||||
ElastiCacheAuthProxy.PasswordValidator passwordValidator = new ElastiCacheAuthProxy.PasswordValidator() {
|
||||
@Override
|
||||
@@ -186,7 +186,7 @@ class ElastiCacheAuthProxyTest {
|
||||
@Test
|
||||
void groupWithNoMembersRequiresDefaultUserForSingleArgIamAuth() {
|
||||
SigV4Validator sigV4Validator = mock(SigV4Validator.class);
|
||||
when(sigV4Validator.validate("valid-sigv4", "grp-legacy-iam", "default")).thenReturn(true);
|
||||
when(sigV4Validator.validate("valid-sigv4", "grp-legacy-iam", "default", null)).thenReturn(true);
|
||||
|
||||
ElastiCacheAuthProxy.PasswordValidator passwordValidator = new ElastiCacheAuthProxy.PasswordValidator() {
|
||||
@Override
|
||||
@@ -210,7 +210,7 @@ class ElastiCacheAuthProxyTest {
|
||||
@Test
|
||||
void groupWithNoMembersPreservesLegacyIamAndAuthTokenBehaviour() {
|
||||
SigV4Validator sigV4Validator = mock(SigV4Validator.class);
|
||||
when(sigV4Validator.validate("valid-sigv4", "grp-4", "anyone")).thenReturn(true);
|
||||
when(sigV4Validator.validate("valid-sigv4", "grp-4", "anyone", null)).thenReturn(true);
|
||||
|
||||
ElastiCacheAuthProxy.PasswordValidator passwordValidator = new ElastiCacheAuthProxy.PasswordValidator() {
|
||||
@Override
|
||||
@@ -421,4 +421,55 @@ class ElastiCacheAuthProxyTest {
|
||||
assertTrue(proxy.authenticate("app-user", "secret-123"),
|
||||
"Configured password user must be able to authenticate");
|
||||
}
|
||||
|
||||
@Test
|
||||
void iamTokenIsCheckedAgainstTheAccountThatOwnsTheCache() {
|
||||
SigV4Validator sigV4Validator = mock(SigV4Validator.class);
|
||||
when(sigV4Validator.validate("valid-sigv4", "grp-owned", "iam-user", "111111111111")).thenReturn(true);
|
||||
when(sigV4Validator.validate("valid-sigv4", "grp-owned", "default", "111111111111")).thenReturn(true);
|
||||
ElastiCacheAuthProxy.PasswordValidator passwordValidator = new ElastiCacheAuthProxy.PasswordValidator() {
|
||||
@Override
|
||||
public boolean validatePassword(String username, String password) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasMembers() {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthMode memberAuthMode(String username) {
|
||||
return AuthMode.IAM;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String ownerAccountId() {
|
||||
return "111111111111";
|
||||
}
|
||||
};
|
||||
|
||||
ElastiCacheAuthProxy proxy = new ElastiCacheAuthProxy("grp-owned", AuthMode.IAM, "127.0.0.1", 6379,
|
||||
passwordValidator, sigV4Validator);
|
||||
|
||||
assertTrue(proxy.authenticate("iam-user", "valid-sigv4"));
|
||||
assertTrue(proxy.authenticate(null, "valid-sigv4"));
|
||||
|
||||
when(sigV4Validator.validate("valid-sigv4", "grp-owned", "anyone", "111111111111")).thenReturn(true);
|
||||
ElastiCacheAuthProxy.PasswordValidator withoutMembers = new ElastiCacheAuthProxy.PasswordValidator() {
|
||||
@Override
|
||||
public boolean validatePassword(String username, String password) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String ownerAccountId() {
|
||||
return "111111111111";
|
||||
}
|
||||
};
|
||||
ElastiCacheAuthProxy legacyProxy = new ElastiCacheAuthProxy("grp-owned", AuthMode.IAM, "127.0.0.1", 6379,
|
||||
withoutMembers, sigV4Validator);
|
||||
|
||||
assertTrue(legacyProxy.authenticate("anyone", "valid-sigv4"));
|
||||
}
|
||||
}
|
||||
+27
@@ -118,6 +118,31 @@ class SigV4ValidatorTest {
|
||||
assertFalse(validator.validate(token, "cache-cluster-01", "default"));
|
||||
}
|
||||
|
||||
/**
|
||||
* The proxy validates on its own thread, where the implicit account is the default one. A key
|
||||
* held by the account that owns the cache is still found, and one held by another account is
|
||||
* not.
|
||||
*/
|
||||
@Test
|
||||
void validateFindsTheOwningAccountsKeyOutsideARequest() throws Exception {
|
||||
IamService iamService = IamServiceTestHelper.iamServiceWithAccessKeyInAccount(
|
||||
"AKIDCACHE", "secret-cache", "111111111111", "000000000000");
|
||||
|
||||
SigV4Validator validator = new SigV4Validator(iamService);
|
||||
String token = SigV4TokenTestHelper.createElastiCacheToken(
|
||||
"cache-cluster-01",
|
||||
"default",
|
||||
"AKIDCACHE",
|
||||
"secret-cache",
|
||||
Instant.now().minusSeconds(60),
|
||||
900
|
||||
);
|
||||
|
||||
assertTrue(validator.validate(token, "cache-cluster-01", "default", "111111111111"));
|
||||
assertFalse(validator.validate(token, "cache-cluster-01", "default", "222222222222"));
|
||||
assertFalse(validator.validate(token, "cache-cluster-01", "default", "000000000000"));
|
||||
}
|
||||
|
||||
/**
|
||||
* A bare 12-digit access key ID that isn't registered in IAM must be rejected like any
|
||||
* other unknown key, not resolved to the well-known "test" secret. That fallback would let
|
||||
@@ -245,6 +270,8 @@ class SigV4ValidatorTest {
|
||||
Instant.now().minusSeconds(60), 900, "session-token");
|
||||
|
||||
assertTrue(validator.validate(token, "cache-cluster-01", "default"));
|
||||
assertTrue(validator.validate(token, "cache-cluster-01", "default", "111111111111"),
|
||||
"A session is found whichever account issued it, with or without an owner to check");
|
||||
}
|
||||
|
||||
/** A Lambda execution role's session token carries {@code +}, {@code /} and {@code =}. */
|
||||
|
||||
Reference in new issue
Block a user