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

@@ -0,0 +1,142 @@
package io.github.hectorvent.floci.services.iam;
import io.github.hectorvent.floci.testing.PartitionCleanup;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import java.util.Map;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.startsWith;
/**
* {@code GetAccessKeyInfo} over the wire.
*
* <p>The operation answers one member, the account an access key id belongs to, and models no
* errors at all. Its one explicit behaviour is what it does not do: "this operation does not
* indicate the state of the access key. The key might be active, inactive, or deleted", so an
* inactive key still answers its account. A key Floci does not hold is the answer the reference
* leaves open, and {@code docs/services/sts.md} carries that deviation.
*/
@QuarkusTest
class StsAccessKeyInfoIntegrationTest {
private static final String ACCOUNT = "000000000941";
private static final String REGION = "us-east-1";
private static final String RESULT = "GetAccessKeyInfoResponse.GetAccessKeyInfoResult";
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
@Test
void aLongTermKeyAnswersTheAccountThatHoldsIt() {
String userName = user("holder");
String accessKeyId = accessKeyFor(userName);
sts("GetAccessKeyInfo", Map.of("AccessKeyId", accessKeyId)).statusCode(200)
.body(RESULT + ".Account", equalTo(ACCOUNT));
}
@Test
void anInactiveKeyStillAnswersItsAccount() {
// "This operation does not indicate the state of the access key. The key might be active,
// inactive, or deleted", so the status is not a reason to refuse.
String userName = user("inactive");
String accessKeyId = accessKeyFor(userName);
iam("UpdateAccessKey", Map.of("UserName", userName, "AccessKeyId", accessKeyId,
"Status", "Inactive")).statusCode(200);
sts("GetAccessKeyInfo", Map.of("AccessKeyId", accessKeyId)).statusCode(200)
.body(RESULT + ".Account", equalTo(ACCOUNT));
}
@Test
void aSessionKeyAnswersTheAccountItsSessionBelongsTo() {
// "Access key IDs beginning with ASIA are temporary credentials that are created using
// STS operations", and they resolve through the session rather than the key store.
String sessionKeyId = sts("GetSessionToken", Map.of()).statusCode(200)
.body("GetSessionTokenResponse.GetSessionTokenResult.Credentials.AccessKeyId",
startsWith("ASIA"))
.extract().path("GetSessionTokenResponse.GetSessionTokenResult.Credentials.AccessKeyId");
sts("GetAccessKeyInfo", Map.of("AccessKeyId", sessionKeyId)).statusCode(200)
.body(RESULT + ".Account", equalTo(ACCOUNT));
}
@Test
void anIdOutsideTheShapeIsReadBeforeAnyLookup() {
sts("GetAccessKeyInfo", Map.of("AccessKeyId", "AKIASHORT")).statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message",
equalTo("1 validation error detected: Value 'AKIASHORT' at 'accessKeyId' "
+ "failed to satisfy constraint: Member must have length greater "
+ "than or equal to 16"));
sts("GetAccessKeyInfo", Map.of("AccessKeyId", "AKIA-NOT-A-VALID-KEY-ID")).statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message",
containsString("Member must satisfy regular expression pattern"));
// A member sent empty is a value that fails the minimum, where an absent one is the
// refusal that names it null, which is the pair IAM already answers for RoleName here.
sts("GetAccessKeyInfo", Map.of("AccessKeyId", "")).statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message",
equalTo("1 validation error detected: Value '' at 'accessKeyId' failed to "
+ "satisfy constraint: Member must have length greater than or "
+ "equal to 16"));
sts("GetAccessKeyInfo", Map.of()).statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("ValidationError"))
.body("ErrorResponse.Error.Message",
containsString("Value null at 'accessKeyId'"));
}
@Test
void aWellFormedKeyNobodyHoldsIsRefused() {
sts("GetAccessKeyInfo", Map.of("AccessKeyId", "AKIAABSENTKEY0000000")).statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("InvalidParameterValue"));
}
private String user(String label) {
String userName = "access-key-info-" + label;
iam("CreateUser", Map.of("UserName", userName)).statusCode(200);
cleanup.register(() -> iam("DeleteUser", Map.of("UserName", userName)).statusCode(200));
return userName;
}
private String accessKeyFor(String userName) {
String accessKeyId = iam("CreateAccessKey", Map.of("UserName", userName)).statusCode(200)
.extract().path("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.AccessKeyId");
cleanup.register(() -> iam("DeleteAccessKey",
Map.of("UserName", userName, "AccessKeyId", accessKeyId)).statusCode(200));
return accessKeyId;
}
private static ValidatableResponse iam(String action, Map<String, String> params) {
return call(action, "iam", "2010-05-08", params);
}
private static ValidatableResponse sts(String action, Map<String, String> params) {
return call(action, "sts", "2011-06-15", params);
}
private static ValidatableResponse call(String action, String service, String version,
Map<String, String> params) {
RequestSpecification spec = given()
.header("Authorization", "AWS4-HMAC-SHA256 Credential=" + ACCOUNT
+ "/20261019/" + REGION + "/" + service + "/aws4_request, "
+ "SignedHeaders=host, Signature=abc")
.contentType("application/x-www-form-urlencoded")
.formParam("Action", action)
.formParam("Version", version);
params.forEach(spec::formParam);
return spec.when().post("/").then();
}
}
@@ -514,6 +514,102 @@ class StsQueryHandlerTest {
PartitionMatrix.assertGlobalArnIn(partitionCase, arn);
}
@Test
void getAccessKeyInfoAnswersTheAccountThatHoldsTheKey() {
IamService service = mock(IamService.class);
when(service.findAccountForAccessKeyId("AKIAIOSFODNN7EXAMPLE"))
.thenReturn(Optional.of("111122223333"));
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
params.add("AccessKeyId", "AKIAIOSFODNN7EXAMPLE");
Response response = newHandler(service, new RegionResolver(REGION, "000000000000"))
.handle("GetAccessKeyInfo", params);
assertEquals(200, response.getStatus());
String body = (String) response.getEntity();
assertTrue(body.contains("<GetAccessKeyInfoResponse"), body);
assertTrue(body.contains("<Account>111122223333</Account>"), body);
// The lookup is the state-independent one, not the authentication path.
verify(service).findAccountForAccessKeyId("AKIAIOSFODNN7EXAMPLE");
verify(service, never()).resolveAccountId(anyString());
}
@Test
void getAccessKeyInfoRefusesAKeyNobodyHolds() {
// The operation models no error at all, and the page says only that a deleted key "might
// return an error that the key doesn't exist", so this is the published common error
// nearest that sentence rather than a reading.
IamService service = mock(IamService.class);
when(service.findAccountForAccessKeyId(anyString())).thenReturn(Optional.empty());
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
params.add("AccessKeyId", "AKIAABSENTKEY0000000");
Response response = newHandler(service, new RegionResolver(REGION, "000000000000"))
.handle("GetAccessKeyInfo", params);
assertEquals(400, response.getStatus());
assertTrue(response.getEntity().toString().contains("<Code>InvalidParameterValue</Code>"));
}
@ParameterizedTest
@CsvSource({
"-, Value null at 'accessKeyId' failed to satisfy constraint: Member must not be null",
"AKIATOOSHORT, Value 'AKIATOOSHORT' at 'accessKeyId' failed to satisfy constraint: "
+ "Member must have length greater than or equal to 16",
"AKIA-NOT-A-VALID-KEY-ID, Member must satisfy regular expression pattern",
})
void getAccessKeyInfoReadsTheIdsShapeBeforeAnyLookup(String accessKeyId, String expected) {
IamService service = mock(IamService.class);
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
if (!"-".equals(accessKeyId)) {
params.add("AccessKeyId", accessKeyId);
}
Response response = newHandler(service, new RegionResolver(REGION, "000000000000"))
.handle("GetAccessKeyInfo", params);
assertEquals(400, response.getStatus());
// The Query error body escapes the apostrophes the message quotes the member with.
String body = ((String) response.getEntity()).replace("&apos;", "'");
assertTrue(body.contains("<Code>ValidationError</Code>"), body);
assertTrue(body.contains(expected), body);
verifyNoInteractions(service);
}
@ParameterizedTest
@ValueSource(strings = {"", " "})
void getAccessKeyInfoReadsAPresentBlankIdAsAValue(String accessKeyId) {
// Present and empty is a value, not an absence: it fails the minimum and is quoted in the
// message, which is what IAM answers for an empty RoleName over this same wire.
IamService service = mock(IamService.class);
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
params.add("AccessKeyId", accessKeyId);
Response response = newHandler(service, new RegionResolver(REGION, "000000000000"))
.handle("GetAccessKeyInfo", params);
assertEquals(400, response.getStatus());
String body = ((String) response.getEntity()).replace("&apos;", "'");
assertTrue(body.contains("Value '" + accessKeyId + "' at 'accessKeyId' failed to satisfy "
+ "constraint: Member must have length greater than or equal to 16"), body);
verifyNoInteractions(service);
}
@Test
void getAccessKeyInfoRefusesAnIdPastTheMaximum() {
IamService service = mock(IamService.class);
MultivaluedMap<String, String> params = new MultivaluedHashMap<>();
params.add("AccessKeyId", "A".repeat(129));
Response response = newHandler(service, new RegionResolver(REGION, "000000000000"))
.handle("GetAccessKeyInfo", params);
assertEquals(400, response.getStatus());
assertTrue(response.getEntity().toString()
.contains("Member must have length less than or equal to 128"));
verifyNoInteractions(service);
}
@ParameterizedTest
@MethodSource("io.github.hectorvent.floci.testing.PartitionMatrix#cases")
void getFederationTokenMintsTheFederatedUserArnInTheDeploymentsPartition(PartitionCase partitionCase) {