From 788ddf751f0ad56943a8db236ea5d3d5606b22fe Mon Sep 17 00:00:00 2001 From: Stuart Lang Date: Fri, 22 May 2026 02:36:28 +0100 Subject: [PATCH] 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. --- .../floci/services/iam/StsQueryHandler.java | 15 +++--- .../services/iam/IamIntegrationTest.java | 54 +++++++++++++++++++ 2 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/iam/StsQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/iam/StsQueryHandler.java index e3a25a74dc..c68e02b12f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iam/StsQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/iam/StsQueryHandler.java @@ -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 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 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(); diff --git a/src/test/java/io/github/hectorvent/floci/services/iam/IamIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/iam/IamIntegrationTest.java index 18b93cdf6f..740d31ac74 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iam/IamIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iam/IamIntegrationTest.java @@ -52,6 +52,24 @@ class IamIntegrationTest { @Test @Order(2) + void stsGetCallerIdentityHonoursTwelveDigitAccessKey() { + given() + .formParam("Action", "GetCallerIdentity") + .header("Authorization", + "AWS4-HMAC-SHA256 Credential=123456789012/20260227/us-east-1/sts/aws4_request") + .when() + .post("/") + .then() + .statusCode(200) + .contentType("application/xml") + .body("GetCallerIdentityResponse.GetCallerIdentityResult.UserId", equalTo("123456789012")) + .body("GetCallerIdentityResponse.GetCallerIdentityResult.Account", equalTo("123456789012")) + .body("GetCallerIdentityResponse.GetCallerIdentityResult.Arn", + equalTo("arn:aws:iam::123456789012:root")); + } + + @Test + @Order(3) void stsAssumeRole() { given() .formParam("Action", "AssumeRole") @@ -73,6 +91,42 @@ class IamIntegrationTest { containsString("assumed-role/TestRole/test-session")); } + @Test + @Order(4) + void stsAssumeRoleHonoursTwelveDigitAccessKey() { + given() + .formParam("Action", "AssumeRole") + .formParam("RoleArn", "arn:aws:iam::123456789012:role/TestRole") + .formParam("RoleSessionName", "tenant-session") + .formParam("DurationSeconds", "3600") + .header("Authorization", + "AWS4-HMAC-SHA256 Credential=123456789012/20260227/us-east-1/sts/aws4_request") + .when() + .post("/") + .then() + .statusCode(200) + .body("AssumeRoleResponse.AssumeRoleResult.AssumedRoleUser.Arn", + equalTo("arn:aws:sts::123456789012:assumed-role/TestRole/tenant-session")); + } + + @Test + @Order(6) + void stsAssumeRoleUsesAccountFromRoleArnForCrossAccount() { + given() + .formParam("Action", "AssumeRole") + .formParam("RoleArn", "arn:aws:iam::222222222222:role/CrossAccountRole") + .formParam("RoleSessionName", "cross-session") + .formParam("DurationSeconds", "3600") + .header("Authorization", + "AWS4-HMAC-SHA256 Credential=123456789012/20260227/us-east-1/sts/aws4_request") + .when() + .post("/") + .then() + .statusCode(200) + .body("AssumeRoleResponse.AssumeRoleResult.AssumedRoleUser.Arn", + equalTo("arn:aws:sts::222222222222:assumed-role/CrossAccountRole/cross-session")); + } + // ========================================================================= // AWS Managed Policies (seeded at startup) // =========================================================================