fix(sts): honour 12-digit access key as account ID in STS responses (#983)

* fix(sts): honour 12-digit access key as account ID

GetCallerIdentity, AssumeRole, AssumeRoleWithWebIdentity,
AssumeRoleWithSAML, and GetFederationToken now resolve the account ID
from the request-scoped RegionResolver instead of the static IAM
default, so ARNs match the per-account namespace used by SNS, SQS, S3,
etc. Closes #981.

* fix(sts): derive AssumedRoleUser account from RoleArn

AssumeRole, AssumeRoleWithWebIdentity, and AssumeRoleWithSAML now build
AssumedRoleUser.Arn using the account ID parsed from the supplied
RoleArn, falling back to the caller's account only when the ARN is
missing or unparseable. This matches AWS cross-account assume-role
behaviour: the resulting ARN belongs to the target role's account, not
the caller's.
This commit is contained in:
Stuart Lang authored and GitHub committed 2026-05-21 20:36:28 -05:00
1 parent ff40a2fc0a
commit 788ddf751f
2 files changed
+63 -6

No files matched your search

@@ -4,6 +4,7 @@ import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsNamespaces;
import io.github.hectorvent.floci.core.common.AwsQueryController;
import io.github.hectorvent.floci.core.common.AwsQueryResponse;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.XmlBuilder;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
@@ -27,10 +28,12 @@ public class StsQueryHandler {
private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
private final IamService iamService;
private final RegionResolver regionResolver;
@Inject
public StsQueryHandler(IamService iamService) {
public StsQueryHandler(IamService iamService, RegionResolver regionResolver) {
this.iamService = iamService;
this.regionResolver = regionResolver;
}
public Response handle(String action, MultivaluedMap<String, String> params) {
@@ -66,7 +69,7 @@ public class StsQueryHandler {
String roleName = roleArn != null && roleArn.contains("/")
? roleArn.substring(roleArn.lastIndexOf('/') + 1)
: "UnknownRole";
String accountId = iamService.getAccountId();
String accountId = AwsArnUtils.accountOrDefault(roleArn, regionResolver.getAccountId());
String assumedRoleArn = AwsArnUtils.Arn.of("sts", "", accountId, "assumed-role/" + roleName + "/" + sessionName).toString();
String assumedRoleId = "AROA" + randomId(16) + ":" + sessionName;
@@ -86,7 +89,7 @@ public class StsQueryHandler {
}
private Response handleGetCallerIdentity(MultivaluedMap<String, String> params) {
String accountId = iamService.getAccountId();
String accountId = regionResolver.getAccountId();
String result = new XmlBuilder()
.elem("UserId", accountId)
.elem("Account", accountId)
@@ -122,7 +125,7 @@ public class StsQueryHandler {
Instant expiration = Instant.now().plusSeconds(durationSeconds);
String roleName = roleArn.contains("/") ? roleArn.substring(roleArn.lastIndexOf('/') + 1) : "UnknownRole";
String accountId = iamService.getAccountId();
String accountId = AwsArnUtils.accountOrDefault(roleArn, regionResolver.getAccountId());
String assumedRoleArn = AwsArnUtils.Arn.of("sts", "", accountId, "assumed-role/" + roleName + "/" + sessionName).toString();
String assumedRoleId = "AROA" + randomId(16) + ":" + sessionName;
String provider = providerId != null && !providerId.isBlank() ? providerId : "accounts.google.com";
@@ -159,7 +162,7 @@ public class StsQueryHandler {
Instant expiration = Instant.now().plusSeconds(durationSeconds);
String roleName = roleArn.contains("/") ? roleArn.substring(roleArn.lastIndexOf('/') + 1) : "UnknownRole";
String accountId = iamService.getAccountId();
String accountId = AwsArnUtils.accountOrDefault(roleArn, regionResolver.getAccountId());
String assumedRoleArn = AwsArnUtils.Arn.of("sts", "", accountId, "assumed-role/" + roleName + "/" + sessionName).toString();
String assumedRoleId = "AROA" + randomId(16) + ":" + sessionName;
@@ -193,7 +196,7 @@ public class StsQueryHandler {
String secretKey = randomSecret(40);
String sessionToken = randomSecret(200);
Instant expiration = Instant.now().plusSeconds(durationSeconds);
String accountId = iamService.getAccountId();
String accountId = regionResolver.getAccountId();
String federatedUserId = accountId + ":" + name;
String federatedUserArn = AwsArnUtils.Arn.of("sts", "", accountId, "federated-user/" + name).toString();