diff --git a/src/main/java/io/floci/aws/core/storage/AccountAwareStorageBackend.java b/src/main/java/io/floci/aws/core/storage/AccountAwareStorageBackend.java index 8928001b38..2632ead57e 100644 --- a/src/main/java/io/floci/aws/core/storage/AccountAwareStorageBackend.java +++ b/src/main/java/io/floci/aws/core/storage/AccountAwareStorageBackend.java @@ -45,7 +45,7 @@ public class AccountAwareStorageBackend implements StorageBackend private volatile RequestContext requestContextProxy; private final String defaultAccountId; - // Accounts that own keys here, so findAnyAccountEntry does one get per account instead of a scan. + // Accounts that own keys here, so a cross-account lookup does one get per account instead of a scan. // Tracking starts on the first cross-account lookup, so other stores pay one volatile read per write. // Accounts are never removed. A stale one only costs a missed get. private final Set ownerAccounts = ConcurrentHashMap.newKeySet(); @@ -584,6 +584,27 @@ public class AccountAwareStorageBackend implements StorageBackend return Optional.empty(); } + /** + * Returns what {@code scanAllAccountEntries(key::equals)} returns, in no particular order, with one + * get per owning account instead of a scan. Like that scan, this does not migrate legacy entries. + */ + public List> findAllAccountEntries(String key) { + if (!ownersComplete) { + startTrackingOwners(); + } + List> result = new ArrayList<>(); + if (!hasAccountPrefix(key)) { + delegate.get(key).ifPresent(value -> result.add(new AccountEntry<>(defaultAccountId, key, value))); + } + for (String account : ownerAccounts) { + String rawKey = account + "/" + key; + if (hasAccountPrefix(rawKey)) { + delegate.get(rawKey).ifPresent(value -> result.add(new AccountEntry<>(account, key, value))); + } + } + return result; + } + private void trackOwner(String accountId) { if (trackingOwners && !ownerAccounts.contains(accountId)) { ownerAccounts.add(accountId); diff --git a/src/main/java/io/floci/aws/services/iam/IamService.java b/src/main/java/io/floci/aws/services/iam/IamService.java index 0c14ac6aa1..0cdfeb3c1e 100644 --- a/src/main/java/io/floci/aws/services/iam/IamService.java +++ b/src/main/java/io/floci/aws/services/iam/IamService.java @@ -4599,7 +4599,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider { || !(accessKeys instanceof AccountAwareStorageBackend aware)) { return List.of(); } - return aware.scanAllAccountEntries(accessKeyId::equals); + return aware.findAllAccountEntries(accessKeyId); } private Optional activeAccessKeySecret(String accessKeyId) { diff --git a/src/test/java/io/floci/aws/core/storage/AccountAwareStorageBackendTest.java b/src/test/java/io/floci/aws/core/storage/AccountAwareStorageBackendTest.java index f573c1561b..9135b5bc6e 100644 --- a/src/test/java/io/floci/aws/core/storage/AccountAwareStorageBackendTest.java +++ b/src/test/java/io/floci/aws/core/storage/AccountAwareStorageBackendTest.java @@ -15,6 +15,7 @@ import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import java.util.function.Predicate; +import java.util.stream.Collectors; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; @@ -469,6 +470,62 @@ class AccountAwareStorageBackendTest { verify(raw, times(1)).keys(); } + @Test + void findAllAccountEntriesReturnsWhatTheScanReturns() { + InMemoryStorage raw = new InMemoryStorage<>(); + raw.put("111111111111/AKIAKEY", "first"); + raw.put("222222222222/AKIAKEY", "second"); + raw.put("AKIAKEY", "legacy"); + raw.put("333333333333/OTHER", "other"); + raw.put("notanaccount/AKIAKEY", "malformed"); + AccountAwareStorageBackend storage = + new AccountAwareStorageBackend<>(raw, null, "000000000000"); + + List> found = storage.findAllAccountEntries("AKIAKEY"); + + assertEquals(Set.of("111111111111", "222222222222", "000000000000"), + found.stream().map(AccountAwareStorageBackend.AccountEntry::accountId).collect(Collectors.toSet())); + assertEquals(3, found.size()); + assertEquals(Set.copyOf(storage.scanAllAccountEntries("AKIAKEY"::equals)), Set.copyOf(found)); + } + + @Test + void findAllAccountEntriesDoesNotReadAnAccountPrefixedIdAsALegacyKey() { + InMemoryStorage raw = new InMemoryStorage<>(); + raw.put("222222222222/AKIAKEY", "owned"); + AccountAwareStorageBackend storage = + new AccountAwareStorageBackend<>(raw, null, "000000000000"); + + assertTrue(storage.findAllAccountEntries("222222222222/AKIAKEY").isEmpty()); + assertTrue(storage.scanAllAccountEntries("222222222222/AKIAKEY"::equals).isEmpty()); + } + + @Test + void findAllAccountEntriesDoesNotMigrateLegacyEntries() { + InMemoryStorage raw = new InMemoryStorage<>(); + raw.put("AKIAKEY", "legacy"); + AccountAwareStorageBackend storage = + new AccountAwareStorageBackend<>(raw, null, "000000000000"); + + assertEquals(1, storage.findAllAccountEntries("AKIAKEY").size()); + + assertTrue(raw.get("AKIAKEY").isPresent()); + assertEquals(Set.of("AKIAKEY"), raw.keys()); + } + + @Test + void findAllAccountEntriesScansOnceAndSeesLaterWrites() { + InMemoryStorage raw = spy(new InMemoryStorage()); + AccountAwareStorageBackend storage = + new AccountAwareStorageBackend<>(raw, null, "000000000000"); + + assertTrue(storage.findAllAccountEntries("AKIAKEY").isEmpty()); + storage.putForAccount("222222222222", "AKIAKEY", "value"); + + assertEquals("222222222222", storage.findAllAccountEntries("AKIAKEY").get(0).accountId()); + verify(raw, times(1)).keys(); + } + @Test void writeDuringTheInitialScanIsStillTracked() { InMemoryStorage raw = spy(new InMemoryStorage()); diff --git a/src/test/java/io/floci/aws/services/iam/IamServiceTest.java b/src/test/java/io/floci/aws/services/iam/IamServiceTest.java index fd625f719f..e8084ac619 100644 --- a/src/test/java/io/floci/aws/services/iam/IamServiceTest.java +++ b/src/test/java/io/floci/aws/services/iam/IamServiceTest.java @@ -2448,6 +2448,42 @@ class IamServiceTest { Mockito.verify(accessKeys, Mockito.never()).scanAllAccountEntries(Mockito.any()); } + @Test + void theActiveCopyOfADuplicateAccessKeyIdPicksTheAccount() { + AccountAwareStorageBackend accessKeys = new AccountAwareStorageBackend<>( + new InMemoryStorage<>(), null, "000000000000"); + putDuplicateKey(accessKeys, "AKIADUPFIRSTACTIVE", "111122223333", "Active"); + putDuplicateKey(accessKeys, "AKIADUPFIRSTACTIVE", "444455556666", "Inactive"); + putDuplicateKey(accessKeys, "AKIADUPSECONDACTIVE", "111122223333", "Inactive"); + putDuplicateKey(accessKeys, "AKIADUPSECONDACTIVE", "444455556666", "Active"); + + IamService service = iamService(false, accessKeys, new InMemoryStorage<>()); + + assertEquals(Optional.of("111122223333"), service.resolveAccountId("AKIADUPFIRSTACTIVE")); + assertEquals(Optional.of("444455556666"), service.resolveAccountId("AKIADUPSECONDACTIVE")); + } + + private static void putDuplicateKey(AccountAwareStorageBackend accessKeys, String id, + String account, String status) { + AccessKey accessKey = new AccessKey(id, "secret", "dev"); + accessKey.setStatus(status); + accessKeys.putForAccount(account, id, accessKey); + } + + @Test + void resolvingAnAccountDoesNotScanTheAccessKeys() { + AccountAwareStorageBackend accessKeys = Mockito.spy(new AccountAwareStorageBackend<>( + new InMemoryStorage<>(), null, "000000000000")); + AccessKey other = new AccessKey("AKIAOTHERACCOUNTKEY", "secret", "dev"); + accessKeys.putForAccount("111122223333", other.getAccessKeyId(), other); + + IamService service = iamService(false, accessKeys, new InMemoryStorage<>()); + + assertEquals(Optional.of("111122223333"), service.resolveAccountId(other.getAccessKeyId())); + assertTrue(service.resolveAccountId("test").isEmpty()); + Mockito.verify(accessKeys, Mockito.never()).scanAllAccountEntries(Mockito.any()); + } + @Test void findSecretKeyDoesNotResolveAnotherAccountsKey() { // The ElastiCache, MemoryDB and RDS IAM-auth proxies verify through findSecretKey and do