fix(iam): return UserName from GetAccessKeyLastUsed (#5579)

* fix(iam): return UserName from GetAccessKeyLastUsed

GetAccessKeyLastUsed omitted UserName for every key, so Terraform's
aws_iam_access_key import failed with "fetching IAM Access Key (...)
username via GetAccessKeyLastUsed: empty response". Resolve the owner
of a stored key and emit it; an unknown key still omits the element.

* test(iam): give the GetAccessKeyLastUsed owner test a unique @Order

* fix(iam): do not let another account claim a legacy key through GetAccessKeyLastUsed

* fix(iam): never migrate a legacy key on the GetAccessKeyLastUsed read path

findAccessKeyOwnerInThisAccount adopted an unprefixed legacy key into the
calling account whenever a user of the same name existed there, so another
account with its own `worker` user could read the owner's name and move the
key out of the default account. Read the scoped key, else peek at legacy
keys in place via scanAllAccountEntries, which attributes them to the
default account, as resourcesInCurrentAccount already does. Nothing moves.

The tests now model accounts through a request context over one shared
store instead of two backends with different default accounts.

* perf(iam): look up a legacy access key directly instead of scanning every account

findAccessKeyOwnerInThisAccount fell back to scanAllAccountEntries on every
miss, so an unknown key walked the whole key store. Add
AccountAwareStorageBackend.getLegacyForAccount, a read-only point lookup of an
unprefixed entry that, like scanAllAccountEntries, attributes it to the
default account only.

* fix(iam): resolve GetAccessKeyLastUsed's policy resource without migrating a legacy key

The enforcement filter resolves the key's owner before the handler runs, and it
went through the migrating lookup, so another account could still pull a
pre-multi-account key into its own partition just by asking about it. Use the
same non-migrating lookup the handler uses, so the policy check and the
response agree on who owns the key.
This commit is contained in:
Allen Sanborn authored and GitHub committed 2026-10-14 00:22:35 -04:00
1 parent 5a90be1049
commit 372d3d11d5
8 files changed
+150 -9

No files matched your search

@@ -307,6 +307,16 @@ public class AccountAwareStorageBackend<V> implements StorageBackend<String, V>
return delegate.get(accountId + "/" + key);
}
/**
* An unprefixed pre-multi-account entry, read in place (never migrated). Such entries belong
* to {@code defaultAccountId}, as in {@link #scanAllAccountEntries}, so any other account
* gets empty.
*/
public Optional<V> getLegacyForAccount(String accountId, String key) {
return defaultAccountId.equals(accountId) && !hasAccountPrefix(key)
? delegate.get(key) : Optional.empty();
}
/**
* Returns an explicitly scoped entry, or safely migrates a matching legacy unscoped entry.
*
@@ -1955,12 +1955,14 @@ public class IamQueryHandler {
private Response handleGetAccessKeyLastUsed(MultivaluedMap<String, String> params) {
// Access-key usage is not modeled, so return the IAM API's documented
// "never used" shape: an AccessKeyLastUsed with ServiceName=N/A and Region=N/A
// and NO LastUsedDate. UserName is optional and its type is non-empty
// (length 1-128); since the emulator exposes no AccessKeyId→UserName lookup,
// the element is omitted rather than emitted empty — an empty string could be
// rejected by a strict client, and callers that need the owner already have it
// from the preceding ListAccessKeys call.
String result = new XmlBuilder()
// and NO LastUsedDate. UserName names the key's owner; Terraform's
// aws_iam_access_key import reads it and fails on "empty response" without it.
// Its type is non-empty (length 1-128), so for a key the emulator does not
// hold the element is omitted rather than emitted empty.
XmlBuilder xml = new XmlBuilder();
iamService.findAccessKeyOwnerInThisAccount(getParam(params, "AccessKeyId"))
.ifPresent(userName -> xml.elem("UserName", userName));
String result = xml
.start("AccessKeyLastUsed")
.elem("ServiceName", "N/A")
.elem("Region", "N/A")
@@ -777,6 +777,25 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
return accessKeys.get(accessKeyId).map(AccessKey::getUserName);
}
/**
* The owner of a key in the calling account, for a read that is not the owner's own request.
* Unlike {@link #findUserNameByAccessKeyId}, this never migrates: an unprefixed
* pre-multi-account key belongs to the default account and is only read in place, so another
* account asking (even one with a same-named user) neither learns the owner nor moves the key.
*/
public Optional<String> findAccessKeyOwnerInThisAccount(String accessKeyId) {
if (accessKeyId == null) {
return Optional.empty();
}
if (!(accessKeys instanceof AccountAwareStorageBackend<AccessKey> aware)) {
return findUserNameByAccessKeyId(accessKeyId);
}
String accountId = aware.accountId();
return aware.getForAccount(accountId, accessKeyId)
.or(() -> aware.getLegacyForAccount(accountId, accessKeyId))
.map(AccessKey::getUserName);
}
public void deleteUser(String userName) {
synchronized (resourceNameLock) {
IamUser user = getUser(userName);
@@ -443,7 +443,7 @@ public class ResourceArnBuilder {
if (accessKeyId == null || accessKeyId.isBlank() || iamService == null) {
return List.of("*");
}
return iamService.get().findUserNameByAccessKeyId(accessKeyId)
return iamService.get().findAccessKeyOwnerInThisAccount(accessKeyId)
.flatMap(userName -> storedIamEntity("user", userName, accountId))
.map(stored -> List.of(stored.arn()))
.orElse(List.of("*"));