feat(sts): dispatch GetAccessKeyInfo, the account an access key id belongs to (#6121)

GetAccessKeyInfo answered UnsupportedOperation 400. It takes one member and
answers one: "when you pass an access key ID to this operation, it returns the
ID of the AWS account to which the keys belong", and it models no errors at all,
so the whole operation is that lookup plus the shape of its input.

The lookup is not the one authentication uses. resolveAccountId takes only an
active long-term key and a live session, because that is what a request may be
signed with, where this operation is explicit about the opposite: "this
operation does not indicate the state of the access key. The key might be
active, inactive, or deleted". So findAccountForAccessKeyId answers from what is
stored whatever state it is in, a long-term key through the account that holds
it and an ASIA key through the session that minted it, which is the split the
page draws between AKIA as "long-term credentials for an IAM user or the AWS
account root user" and ASIA as "temporary credentials that are created using AWS
STS operations". Both long-term lookups read one scan now, the authentication
one keeping the Active filter over what the new one answers whole, so the guard
and the scan are written once.

The id is read against accessKeyIdType first, 16 to 128 characters of [\w],
which the reference glosses as "any upper- or lowercase letter or digit", so a
malformed id answers for its own shape rather than for a key that could not
exist. The violation is rendered the way the one recorded IAM rendering Floci
already follows renders it, the member in lower camel case and the offending
value quoted beside it; the older STS operations name the parameter as written
instead, which is drift of its own rather than something to copy here. An absent
member and a present empty one are different answers as well: only the absent
one reads as null, where AccessKeyId= is a value that fails the minimum and is
quoted as the empty string it is, which is what IAM answers for an empty
RoleName over this same wire.

One answer is chosen rather than read. The operation models no error for a key
that is not there, and the page says only that "providing a deleted access key
might return an error that the key doesn't exist", so a well-formed id this
emulator never issued answers InvalidParameterValue, the published common error
closest to that sentence. docs/services/sts.md records it as a deviation rather
than a recorded behaviour, since nothing says what AWS answers for an id it
never issued.

Enforcement is untouched on purpose. The "No permissions are required" note
appears on GetCallerIdentity and GetSessionToken and on no other page in the
reference, and sts:GetAccessKeyInfo is in the service authorization reference, so
this one is authorized like any other action rather than exempted the way
GetCallerIdentity is.

Ten tests. Five over the wire: a long-term key answering the account that holds
it, an inactive key still answering it, a session key answering the account its
session belongs to, four shapes refused ahead of any lookup, and a well-formed
id nobody holds. Five on the handler: the account answered and the
authentication path not consulted, the absent, short, long and malformed ids, a
present blank id read as a value rather than an absence, and the refusal for a
key the service does not hold. Docs carry the action, a
section on what it does and does not say about the key, and the matrix row at 9.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-21 18:37:12 -05:00
1 parent f731951c53
commit 4eea01741f
6 files changed
+364 -6

No files matched your search

@@ -4515,18 +4515,49 @@ public class IamService implements SessionAccountLookup, ResourceProvider {
*/
public record OwnedSecretKey(String secretAccessKey, String accountId) {}
/**
* The account that owns an access key id, in whatever state the key is, which is the lookup STS
* GetAccessKeyInfo answers from: "This operation does not indicate the state of the access key.
* The key might be active, inactive, or deleted." {@link #resolveAccountId} is the other
* question, the one authentication asks, and it takes only an active key and a live session.
*/
public Optional<String> findAccountForAccessKeyId(String accessKeyId) {
if (accessKeyId == null || accessKeyId.isBlank()) {
return Optional.empty();
}
if (!isTemporaryAccessKey(accessKeyId)) {
return accessKeyInAnyAccount(accessKeyId)
.map(AccountAwareStorageBackend.AccountEntry::accountId);
}
return storedSessionAnyAccount(accessKeyId)
.map(session -> AwsArnUtils.accountOrDefault(session.getRoleArn(), session.getOriginAccountId()))
.filter(account -> !account.isBlank());
}
/** The long-term key with this id from whichever account holds it, Active or not. */
private Optional<AccountAwareStorageBackend.AccountEntry<AccessKey>> accessKeyInAnyAccount(
String accessKeyId) {
return longTermKeysInAnyAccount(accessKeyId).stream().findFirst();
}
/** The active long-term key with this ID, from whichever account owns it. */
private Optional<AccountAwareStorageBackend.AccountEntry<AccessKey>> activeAccessKeyInAnyAccount(
String accessKeyId) {
if (accessKeyId == null || isTemporaryAccessKey(accessKeyId)
|| !(accessKeys instanceof AccountAwareStorageBackend<AccessKey> aware)) {
return Optional.empty();
}
return aware.scanAllAccountEntries(accessKeyId::equals).stream()
return longTermKeysInAnyAccount(accessKeyId).stream()
.filter(entry -> "Active".equals(entry.value().getStatus()))
.findFirst();
}
/** Every long-term key stored under this id, across accounts, in whatever state. */
private List<AccountAwareStorageBackend.AccountEntry<AccessKey>> longTermKeysInAnyAccount(
String accessKeyId) {
if (accessKeyId == null || isTemporaryAccessKey(accessKeyId)
|| !(accessKeys instanceof AccountAwareStorageBackend<AccessKey> aware)) {
return List.of();
}
return aware.scanAllAccountEntries(accessKeyId::equals);
}
private Optional<String> activeAccessKeySecret(String accessKeyId) {
return accessKeys.get(accessKeyId)
.filter(accessKey -> "Active".equals(accessKey.getStatus()))
@@ -11,6 +11,7 @@ import io.github.hectorvent.floci.core.common.IamConditionContextResolver;
import io.github.hectorvent.floci.core.common.IamEnforcementFilter;
import io.github.hectorvent.floci.core.common.OidcIssuerKeyLookup;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.SmithyValidationErrors;
import io.github.hectorvent.floci.core.common.WebIdentityToken;
import io.github.hectorvent.floci.core.common.WebIdentityTokenVerifier;
import io.github.hectorvent.floci.core.common.XmlBuilder;
@@ -35,6 +36,7 @@ import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Pattern;
/**
* Query-protocol handler for STS (Security Token Service) actions.
@@ -44,6 +46,11 @@ import java.util.concurrent.ThreadLocalRandom;
@ApplicationScoped
public class StsQueryHandler {
/** accessKeyIdType, the only member GetAccessKeyInfo takes: 16 to 128 characters of {@code [\w]}. */
private static final Pattern ACCESS_KEY_ID_PATTERN = Pattern.compile("[\\w]*");
private static final int ACCESS_KEY_ID_MIN_LENGTH = 16;
private static final int ACCESS_KEY_ID_MAX_LENGTH = 128;
private static final Logger LOG = Logger.getLogger(StsQueryHandler.class);
private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
private static final String STS_AUDIENCE = "sts.amazonaws.com"; // partition-literal: web-identity audience; no source outside the commercial partition (P9)
@@ -113,6 +120,7 @@ public class StsQueryHandler {
return switch (action) {
case "AssumeRole" -> handleAssumeRole(params);
case "GetCallerIdentity" -> handleGetCallerIdentity(params);
case "GetAccessKeyInfo" -> handleGetAccessKeyInfo(params);
case "GetSessionToken" -> handleGetSessionToken(params);
case "AssumeRoleWithWebIdentity" -> handleAssumeRoleWithWebIdentity(params);
case "AssumeRoleWithSAML" -> handleAssumeRoleWithSAML(params);
@@ -258,6 +266,70 @@ public class StsQueryHandler {
return Response.ok(AwsQueryResponse.envelope("GetCallerIdentity", AwsNamespaces.STS, result)).build();
}
/**
* The account an access key id belongs to. AWS answers one member and models no errors at all,
* and the page is explicit that the key's state is not part of the answer: "This operation does
* not indicate the state of the access key. The key might be active, inactive, or deleted." So
* the lookup takes the key whatever state it is in, which is why it is not
* {@code resolveAccountId}, the authentication path, that answers here.
*
* <p>A key Floci does not hold is the one answer the reference leaves open. It models no error
* for it and says only that "providing a deleted access key might return an error that the key
* doesn't exist", so this answers {@code InvalidParameterValue}, the published common error
* nearest that sentence, and {@code docs/services/sts.md} records it as the deviation it is.
*/
private Response handleGetAccessKeyInfo(MultivaluedMap<String, String> params) {
Response validation = validateAccessKeyId(params);
if (validation != null) {
return validation;
}
String accessKeyId = getParam(params, "AccessKeyId");
Optional<String> account = iamService.findAccountForAccessKeyId(accessKeyId);
if (account.isEmpty()) {
return AwsQueryResponse.error("InvalidParameterValue",
"The access key " + accessKeyId + " does not exist.", AwsNamespaces.STS, 400);
}
String result = new XmlBuilder().elem("Account", account.get()).build();
return Response.ok(AwsQueryResponse.envelope("GetAccessKeyInfo", AwsNamespaces.STS, result)).build();
}
/**
* accessKeyIdType: required, 16 to 128 characters of {@code [\w]*}, which the reference
* describes as "any upper- or lowercase letter or digit". Read before the lookup, so a
* malformed id answers for its shape rather than for a key that could not exist.
*
* <p>An absent member and a present empty one are different answers: only the absent one is
* "Member must not be null", where {@code AccessKeyId=} is a value that fails the minimum. That
* is what IAM answers for an empty {@code RoleName} on this same wire.
*
* <p>The member is named the way AWS names it in a constraint message, lower camel case, and
* the offending value is quoted beside it, which is what the one recorded rendering Floci
* follows elsewhere does: {@link IamService} reads {@code roleName} that way, and so does this
* handler's own {@code DurationSeconds} check. The older STS operations name the parameter as
* written instead, which is a drift of its own rather than something to copy here.
*/
private Response validateAccessKeyId(MultivaluedMap<String, String> params) {
String accessKeyId = params.getFirst("AccessKeyId");
String constraint;
if (accessKeyId == null) {
constraint = "Member must not be null";
} else if (accessKeyId.length() < ACCESS_KEY_ID_MIN_LENGTH) {
constraint = "Member must have length greater than or equal to " + ACCESS_KEY_ID_MIN_LENGTH;
} else if (accessKeyId.length() > ACCESS_KEY_ID_MAX_LENGTH) {
constraint = "Member must have length less than or equal to " + ACCESS_KEY_ID_MAX_LENGTH;
} else if (!ACCESS_KEY_ID_PATTERN.matcher(accessKeyId).matches()) {
constraint = "Member must satisfy regular expression pattern: "
+ ACCESS_KEY_ID_PATTERN.pattern();
} else {
return null;
}
String value = accessKeyId == null ? "null" : "'" + accessKeyId + "'";
return AwsQueryResponse.error("ValidationError",
SmithyValidationErrors.message(List.of("Value " + value
+ " at 'accessKeyId' failed to satisfy constraint: " + constraint)),
AwsNamespaces.STS, 400);
}
private Response handleGetSessionToken(MultivaluedMap<String, String> params) {
if (IamService.isTemporaryAccessKey(callerAccessKeyId())) {
return AwsQueryResponse.error("AccessDenied", "Cannot call GetSessionToken with session credentials",