Skip to content

armgit / floci

Public Imported from GitHub
0

Light, fluffy, and always free - The AWS Local Emulator alternative

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

Stuart Lang788ddf751f0a
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<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();
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)
// =========================================================================