mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
perf(iam): find an access key across accounts without scanning every key (#7716)
Every request whose access key was not 12 digits walked all stored IAM keys to find the owning account. 100 keys added about 17 KB to each request of every service. A new store lookup does one get per owning account and returns the same entries as the scan. It returns all of them because the same id can be Inactive in one account and Active in another, and it does not migrate legacy entries.
This commit is contained in:
1 parent
d6aed932a1
commit
206b602439
4 files changed
+116
-2
No files matched your search
@@ -45,7 +45,7 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
|
||||
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<String> ownerAccounts = ConcurrentHashMap.newKeySet();
|
||||
@@ -584,6 +584,27 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
|
||||
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<AccountEntry<V>> findAllAccountEntries(String key) {
|
||||
if (!ownersComplete) {
|
||||
startTrackingOwners();
|
||||
}
|
||||
List<AccountEntry<V>> 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);
|
||||
|
||||
@@ -4599,7 +4599,7 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
|
||||
|| !(accessKeys instanceof AccountAwareStorageBackend<AccessKey> aware)) {
|
||||
return List.of();
|
||||
}
|
||||
return aware.scanAllAccountEntries(accessKeyId::equals);
|
||||
return aware.findAllAccountEntries(accessKeyId);
|
||||
}
|
||||
|
||||
private Optional<String> activeAccessKeySecret(String accessKeyId) {
|
||||
|
||||
@@ -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<String, String> 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<String> storage =
|
||||
new AccountAwareStorageBackend<>(raw, null, "000000000000");
|
||||
|
||||
List<AccountAwareStorageBackend.AccountEntry<String>> 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<String, String> raw = new InMemoryStorage<>();
|
||||
raw.put("222222222222/AKIAKEY", "owned");
|
||||
AccountAwareStorageBackend<String> storage =
|
||||
new AccountAwareStorageBackend<>(raw, null, "000000000000");
|
||||
|
||||
assertTrue(storage.findAllAccountEntries("222222222222/AKIAKEY").isEmpty());
|
||||
assertTrue(storage.scanAllAccountEntries("222222222222/AKIAKEY"::equals).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void findAllAccountEntriesDoesNotMigrateLegacyEntries() {
|
||||
InMemoryStorage<String, String> raw = new InMemoryStorage<>();
|
||||
raw.put("AKIAKEY", "legacy");
|
||||
AccountAwareStorageBackend<String> 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<String, String> raw = spy(new InMemoryStorage<String, String>());
|
||||
AccountAwareStorageBackend<String> 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<String, String> raw = spy(new InMemoryStorage<String, String>());
|
||||
|
||||
@@ -2448,6 +2448,42 @@ class IamServiceTest {
|
||||
Mockito.verify(accessKeys, Mockito.never()).scanAllAccountEntries(Mockito.any());
|
||||
}
|
||||
|
||||
@Test
|
||||
void theActiveCopyOfADuplicateAccessKeyIdPicksTheAccount() {
|
||||
AccountAwareStorageBackend<AccessKey> 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<AccessKey> 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<AccessKey> 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
|
||||
|
||||
Reference in new issue
Block a user