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

@@ -257,10 +257,30 @@ public class AwsQueryController {
String region = regionResolver.resolveRegion(httpHeaders);
try {
return dispatchToHandler(service, action, formParams, authorization, region);
} catch (AwsException e) {
// Handlers that don't render their own error XML would otherwise reach
// AwsExceptionMapper, which emits JSON — the same JSON-on-the-XML-wire defect.
// The declared code and status must survive; only the encoding changes.
return xmlErrorResponse(e.getErrorCode(), e.getMessage(), e.getHttpStatus(),
e.getHttpStatus() >= 500 ? "Receiver" : "Sender");
} catch (Exception e) {
// A handler bug (e.g. an NPE) must never leak Quarkus's JSON error page onto
// the Query/XML wire — SDK parsers fail before they can surface anything useful.
LOG.errorv(e, "Unhandled error dispatching Query action {0} for service {1}", action, service);
return xmlErrorResponse("InternalFailure",
"Unexpected error: " + e.getMessage(), 500, "Receiver");
}
}
private Response dispatchToHandler(String service, String action,
MultivaluedMap<String, String> formParams,
String authorization, String region) {
return switch (service) {
case "sqs" -> sqsQueryHandler.handle(action, formParams, region);
case "sns" -> snsQueryHandler.handle(action, formParams, region);
case "iam" -> iamQueryHandler.handle(action, formParams);
case "iam" -> iamQueryHandler.handle(action, formParams, authorization);
case "sts" -> stsQueryHandler.handle(action, formParams);
case "elasticache" -> elastiCacheQueryHandler.handle(action, formParams);
case "rds" -> {
@@ -456,10 +476,14 @@ public class AwsQueryController {
}
private Response xmlErrorResponse(String code, String message, int status) {
return xmlErrorResponse(code, message, status, "Sender");
}
private Response xmlErrorResponse(String code, String message, int status, String type) {
String xml = new XmlBuilder()
.start("ErrorResponse")
.start("Error")
.elem("Type", "Sender")
.elem("Type", type)
.elem("Code", code)
.elem("Message", message)
.end("Error")
@@ -35,21 +35,24 @@ public class IamQueryHandler {
private final IamService iamService;
private final IamPolicyEvaluator policyEvaluator;
private final AccountResolver accountResolver;
@Inject
public IamQueryHandler(IamService iamService, IamPolicyEvaluator policyEvaluator) {
public IamQueryHandler(IamService iamService, IamPolicyEvaluator policyEvaluator,
AccountResolver accountResolver) {
this.iamService = iamService;
this.policyEvaluator = policyEvaluator;
this.accountResolver = accountResolver;
}
public Response handle(String action, MultivaluedMap<String, String> params) {
public Response handle(String action, MultivaluedMap<String, String> params, String authorization) {
LOG.debugv("IAM action: {0}", action);
try {
return switch (action) {
// Users
case "CreateUser" -> handleCreateUser(params);
case "GetUser" -> handleGetUser(params);
case "GetUser" -> handleGetUser(params, authorization);
case "DeleteUser" -> handleDeleteUser(params);
case "ListUsers" -> handleListUsers(params);
case "UpdateUser" -> handleUpdateUser(params);
@@ -133,8 +136,8 @@ public class IamQueryHandler {
// Access Keys
case "CreateAccessKey" -> handleCreateAccessKey(params);
case "DeleteAccessKey" -> handleDeleteAccessKey(params);
case "ListAccessKeys" -> handleListAccessKeys(params);
case "DeleteAccessKey" -> handleDeleteAccessKey(params, authorization);
case "ListAccessKeys" -> handleListAccessKeys(params, authorization);
case "UpdateAccessKey" -> handleUpdateAccessKey(params);
case "GetAccessKeyLastUsed" -> handleGetAccessKeyLastUsed(params);
@@ -179,9 +182,28 @@ public class IamQueryHandler {
return Response.ok(AwsQueryResponse.envelope("CreateUser", AwsNamespaces.IAM, result)).build();
}
private Response handleGetUser(MultivaluedMap<String, String> params) {
// UserName is optional on GetUser/ListAccessKeys/DeleteAccessKey: real AWS determines
// it implicitly from the access key that signed the request. Mirrors moto: an access
// key that maps to no stored IAM user is the documented NoSuchEntity error.
private String resolveUserName(MultivaluedMap<String, String> params, String authorization) {
String userName = getParam(params, "UserName");
IamUser user = iamService.getUser(userName != null ? userName : "root");
if (userName != null) {
return userName;
}
String accessKeyId = accountResolver.extractAccessKeyId(authorization);
if (accessKeyId == null) {
throw new AwsException("NoSuchEntity",
"No access key could be determined from the request.", 404);
}
return iamService.findUserNameByAccessKeyId(accessKeyId)
.orElseThrow(() -> new AwsException("NoSuchEntity",
"The Access Key with id " + accessKeyId + " cannot be found.", 404));
}
private Response handleGetUser(MultivaluedMap<String, String> params, String authorization) {
// UserName is optional per the IAM model: it defaults to the user owning the
// access key that signed the request.
IamUser user = iamService.getUser(resolveUserName(params, authorization));
String result = new XmlBuilder().start("User").raw(userXml(user)).end("User").build();
return Response.ok(AwsQueryResponse.envelope("GetUser", AwsNamespaces.IAM, result)).build();
}
@@ -672,13 +694,13 @@ public class IamQueryHandler {
return Response.ok(AwsQueryResponse.envelope("CreateAccessKey", AwsNamespaces.IAM, result)).build();
}
private Response handleDeleteAccessKey(MultivaluedMap<String, String> params) {
iamService.deleteAccessKey(getParam(params, "UserName"), getParam(params, "AccessKeyId"));
private Response handleDeleteAccessKey(MultivaluedMap<String, String> params, String authorization) {
iamService.deleteAccessKey(resolveUserName(params, authorization), getParam(params, "AccessKeyId"));
return Response.ok(AwsQueryResponse.envelopeNoResult("DeleteAccessKey", AwsNamespaces.IAM)).build();
}
private Response handleListAccessKeys(MultivaluedMap<String, String> params) {
List<AccessKey> keys = iamService.listAccessKeys(getParam(params, "UserName"));
private Response handleListAccessKeys(MultivaluedMap<String, String> params, String authorization) {
List<AccessKey> keys = iamService.listAccessKeys(resolveUserName(params, authorization));
var xml = new XmlBuilder().start("AccessKeyMetadata");
for (AccessKey k : keys) {
xml.start("member").raw(accessKeyXml(k, false)).end("member");
@@ -193,11 +193,23 @@ public class IamService implements SessionAccountLookup {
}
public IamUser getUser(String userName) {
if (userName == null) {
throw new AwsException("NoSuchEntity",
"The user with name null cannot be found.", 404);
}
return users.get(userName)
.orElseThrow(() -> new AwsException("NoSuchEntity",
"The user with name " + userName + " cannot be found.", 404));
}
/** The IAM user that owns the given access key id, when it is a real stored key. */
public Optional<String> findUserNameByAccessKeyId(String accessKeyId) {
if (accessKeyId == null) {
return Optional.empty();
}
return accessKeys.get(accessKeyId).map(AccessKey::getUserName);
}
public void deleteUser(String userName) {
IamUser user = getUser(userName);
if (!user.getAttachedPolicyArns().isEmpty()) {