fix(iam): default UserName to the calling access key's owner (#1801)

GetUser, ListAccessKeys and DeleteAccessKey treat UserName as optional: the
IAM model documents it as defaulting to the user making the request. Floci
ignored the caller and passed the missing name straight through, so GetUser
fell back to a "root" user that is never seeded and the access-key actions
passed null. Every one of them ended in NoSuchEntity.

Resolve UserName from the access key that signed the request, falling back to
NoSuchEntity (the model's code for these operations, 404, senderFault) when
the key maps to no stored user.

Those errors were also rendered wrong. Query handlers that catch AwsException
build their own error XML, but the ones that don't reach AwsExceptionMapper,
which emits JSON — a JSON error body on the XML wire, which SDK parsers fail
on before they can surface anything useful. AwsQueryController now renders
AwsException as the Query error contract, preserving the declared code and
status, and maps any other handler exception to InternalFailure rather than
letting Quarkus' JSON error page escape.

CloudWatch Metrics is the handler that renders no error XML of its own, so it
is the regression test for the pass-through: SetAlarmState on a missing alarm
must stay 404 ResourceNotFound and not collapse into a 500.

Closes #1753
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-07-11 00:49:06 -05:00
1 parent ad47b39800
commit eb2fa9c25f
5 files changed
+254 -13

No files matched your search

@@ -0,0 +1,66 @@
package io.github.hectorvent.floci.core.common;
import io.github.hectorvent.floci.services.iam.IamService;
import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;
import org.junit.jupiter.api.Test;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
/**
* A handler bug on the Query protocol must render the XML InternalFailure contract, never
* Quarkus's default JSON error page — SDK XML parsers fail on JSON before surfacing anything
* (issue #1753 hit exactly this via an NPE in the IAM handler).
*/
@QuarkusTest
class AwsQueryInternalErrorIntegrationTest {
@InjectMock
IamService iamService;
@Test
void unhandledHandlerExceptionRendersXmlInternalFailure() {
when(iamService.listUsers(any())).thenThrow(new RuntimeException("simulated handler bug"));
given()
.formParam("Action", "ListUsers")
.header("Authorization",
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request")
.when()
.post("/")
.then()
.statusCode(500)
.contentType(containsString("xml"))
.body("ErrorResponse.Error.Type", equalTo("Receiver"))
.body("ErrorResponse.Error.Code", equalTo("InternalFailure"))
.body("ErrorResponse.Error.Message", containsString("simulated handler bug"));
}
/**
* The catch-all must not swallow AwsException. CloudWatch Metrics is the Query handler that
* renders no error XML of its own, so its AwsException reaches the controller: it has to keep
* its declared code and 4xx status and be encoded as XML, not collapse to a 500 InternalFailure
* (nor reach AwsExceptionMapper, which would emit JSON onto the XML wire).
*/
@Test
void awsExceptionFromAHandlerKeepsItsCodeAndStatusAsXml() {
given()
.formParam("Action", "SetAlarmState")
.formParam("AlarmName", "no-such-alarm")
.formParam("StateValue", "ALARM")
.formParam("StateReason", "reason")
.header("Authorization",
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/monitoring/aws4_request")
.when()
.post("/")
.then()
.statusCode(404)
.contentType(containsString("xml"))
.body("ErrorResponse.Error.Type", equalTo("Sender"))
.body("ErrorResponse.Error.Code", equalTo("ResourceNotFound"));
}
}
@@ -767,6 +767,106 @@ class IamIntegrationTest {
// Access Keys
// =========================================================================
private static void createUser(String userName) {
given()
.formParam("Action", "CreateUser")
.formParam("UserName", userName)
.header("Authorization",
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request")
.when()
.post("/")
.then()
.statusCode(200);
}
private static String createAccessKeyFor(String userName) {
return given()
.formParam("Action", "CreateAccessKey")
.formParam("UserName", userName)
.header("Authorization",
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request")
.when()
.post("/")
.then()
.statusCode(200)
.extract()
.path("CreateAccessKeyResponse.CreateAccessKeyResult.AccessKey.AccessKeyId");
}
@Test
@Order(56)
void listAccessKeysWithoutUserNameResolvesCaller() {
// UserName is optional per the IAM model: it defaults to the owner of the access
// key that signed the request (issue #1753: this path used to leak a JSON 500).
createUser("caller-list-user");
String keyId = createAccessKeyFor("caller-list-user");
given()
.formParam("Action", "ListAccessKeys")
.header("Authorization",
"AWS4-HMAC-SHA256 Credential=" + keyId + "/20260227/us-east-1/iam/aws4_request")
.when()
.post("/")
.then()
.statusCode(200)
.contentType(containsString("xml"))
.body("ListAccessKeysResponse.ListAccessKeysResult.AccessKeyMetadata.member.UserName",
equalTo("caller-list-user"));
}
@Test
@Order(57)
void listAccessKeysWithoutUserNameUnknownKeyReturnsNoSuchEntityXml() {
// The conventional 'test' key owns no IAM user: moto parity is the documented
// NoSuchEntity XML error, never a JSON body on the Query wire.
given()
.formParam("Action", "ListAccessKeys")
.header("Authorization",
"AWS4-HMAC-SHA256 Credential=test/20260227/us-east-1/iam/aws4_request")
.when()
.post("/")
.then()
.statusCode(404)
.contentType(containsString("xml"))
.body("ErrorResponse.Error.Code", equalTo("NoSuchEntity"))
.body("ErrorResponse.Error.Message", containsString("test"));
}
@Test
@Order(58)
void getUserWithoutUserNameResolvesCaller() {
createUser("caller-get-user");
String keyId = createAccessKeyFor("caller-get-user");
given()
.formParam("Action", "GetUser")
.header("Authorization",
"AWS4-HMAC-SHA256 Credential=" + keyId + "/20260227/us-east-1/iam/aws4_request")
.when()
.post("/")
.then()
.statusCode(200)
.body("GetUserResponse.GetUserResult.User.UserName", equalTo("caller-get-user"));
}
@Test
@Order(59)
void deleteAccessKeyWithoutUserNameResolvesOwner() {
createUser("caller-del-user");
String keyId = createAccessKeyFor("caller-del-user");
given()
.formParam("Action", "DeleteAccessKey")
.formParam("AccessKeyId", keyId)
.header("Authorization",
"AWS4-HMAC-SHA256 Credential=" + keyId + "/20260227/us-east-1/iam/aws4_request")
.when()
.post("/")
.then()
.statusCode(200)
.body(containsString("DeleteAccessKeyResponse"));
}
@Test
@Order(40)
void createAndListAccessKeys() {
@@ -937,4 +1037,21 @@ class IamIntegrationTest {
.statusCode(400)
.body("ErrorResponse.Error.Code", equalTo("UnsupportedOperation"));
}
@Test
@Order(73)
void getUserWithoutUserNameOnAnUnsignedRequestDoesNotLeakNull() {
// No Authorization header means no access key to resolve the caller from. The action
// still routes to IAM by name, so the error must read as a sentence rather than
// interpolating a null key id onto the wire.
given()
.formParam("Action", "GetUser")
.when()
.post("/")
.then()
.statusCode(404)
.contentType(containsString("xml"))
.body("ErrorResponse.Error.Code", equalTo("NoSuchEntity"))
.body("ErrorResponse.Error.Message", not(containsString("null")));
}
}