fix(sts): give AssumeRole's identity-based policies the sts:RoleSessionName and sts:ExternalId keys (#8922)

With IAM enforcement on, the caller's identity-based policy, permissions boundary, session
policy and SCPs were evaluated for sts:AssumeRole without the request's sts:RoleSessionName
and sts:ExternalId, which the role's trust policy already sees, so a condition on either never
matched. The Service Authorization Reference lists both among the condition keys of
AssumeRole. STS now joins the generated request condition key table, which reads them from the
AssumeRole form; an AssumeRole without ExternalId names no sts:ExternalId. AssumeRoleWithSAML
carries no session name member, so it is listed as not reading it from the request.

sts:SourceIdentity, sts:TransitiveTagKeys and the session tag keys are not read: Floci does not
model source identity or session tags, as the STS page already says.

Closes #8921
This commit is contained in:
Jeremy Chapeau authored and GitHub committed 2026-11-19 17:17:17 -04:00
1 parent 76f9481bb1
commit 3a9f529158
8 files changed
+200 -3

No files matched your search

@@ -236,6 +236,17 @@ class IamConditionContextResolverTest {
formRequest("Action=GetClusterCredentials")));
}
@Test
void anAssumeRoleRequestCarriesItsSessionNameAndExternalId() {
assertEquals(Map.of("sts:RoleSessionName", List.of("alice"), "sts:ExternalId", List.of("partner-42")),
resolver.resolve("sts", "sts:AssumeRole", formRequest(
"Action=AssumeRole&RoleArn=arn%3Aaws%3Aiam%3A%3A111111111111%3Arole%2Fr"
+ "&RoleSessionName=alice&ExternalId=partner-42")));
assertEquals(Map.of("sts:RoleSessionName", List.of("alice")), resolver.resolve("sts", "sts:AssumeRole",
formRequest("Action=AssumeRole&RoleSessionName=alice")), "no ExternalId, no sts:ExternalId");
assertNull(resolver.resolve("sts", "sts:GetCallerIdentity", formRequest("Action=GetCallerIdentity")));
}
@Test
void logStreamTagsUseRawParentNameAndRequestRegion() throws Exception {
ContainerRequestContext request = mock(ContainerRequestContext.class);
@@ -0,0 +1,118 @@
package io.floci.aws.services.iam;
import io.floci.aws.testing.EnforcementFixtures;
import io.floci.aws.testing.EnforcementFixtures.UserCredentials;
import io.floci.aws.testing.IamEnforcementProfile;
import io.floci.aws.testing.PartitionCleanup;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import java.net.URI;
import java.util.UUID;
import static io.floci.aws.testing.EnforcementFixtures.auth;
import static io.restassured.RestAssured.given;
/**
* With {@code iam.enforcement-enabled=true}, the caller's identity policy for {@code sts:AssumeRole}
* is evaluated with the {@code sts:RoleSessionName} and {@code sts:ExternalId} the request carries,
* which the Service Authorization Reference lists among the condition keys of {@code AssumeRole}.
* Every role here trusts its account, so the identity policy decides.
*/
@QuarkusTest
@TestProfile(IamEnforcementProfile.class)
class StsAssumeRoleIdentityConditionKeysIntegrationTest {
private static final String ACCOUNT = "111111111111";
@TestHTTPResource
URI baseUri;
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
@Test
void anIdentityPolicyRequiringAnExternalIdAllowsOnlyThatExternalId() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
String roleArn = createRole("external-id-" + suffix);
UserCredentials caller = callerWithPolicy("external-id-caller-" + suffix, """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"sts:AssumeRole","Resource":"%s",
"Condition":{"StringEquals":{"sts:ExternalId":"partner-42"}}}]}""".formatted(roleArn));
assumeRole(caller, roleArn, "s1", "partner-42").then().statusCode(200);
assumeRole(caller, roleArn, "s2", "partner-43").then().statusCode(403);
assumeRole(caller, roleArn, "s3", null).then().statusCode(403);
}
@Test
void anIdentityPolicyRequiringTheUserNameAsSessionNameAllowsOnlyThatName() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
String roleArn = createRole("session-name-" + suffix);
String user = "session-caller-" + suffix;
UserCredentials caller = callerWithPolicy(user, """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"sts:AssumeRole","Resource":"%s",
"Condition":{"StringEquals":{"sts:RoleSessionName":"${aws:username}"}}}]}""".formatted(roleArn));
assumeRole(caller, roleArn, user, null).then().statusCode(200);
assumeRole(caller, roleArn, "someone-else", null).then().statusCode(403);
}
@Test
void aDenyOnASessionNamePatternRefusesOnlyThoseNames() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
String roleArn = createRole("session-deny-" + suffix);
UserCredentials caller = callerWithPolicy("session-deny-caller-" + suffix, """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"sts:AssumeRole","Resource":"%s"},
{"Effect":"Deny","Action":"sts:AssumeRole","Resource":"*",
"Condition":{"StringLike":{"sts:RoleSessionName":"admin-*"}}}]}""".formatted(roleArn));
assumeRole(caller, roleArn, "admin-1", null).then().statusCode(403);
assumeRole(caller, roleArn, "batch-1", null).then().statusCode(200);
}
@Test
void anIdentityPolicyWithoutConditionAllowsWithOrWithoutAnExternalId() {
String suffix = UUID.randomUUID().toString().substring(0, 8);
String roleArn = createRole("no-condition-" + suffix);
UserCredentials caller = callerWithPolicy("no-condition-caller-" + suffix, """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"sts:AssumeRole","Resource":"%s"}]}""".formatted(roleArn));
assumeRole(caller, roleArn, "s1", null).then().statusCode(200);
assumeRole(caller, roleArn, "s2", "partner-42").then().statusCode(200);
}
private UserCredentials callerWithPolicy(String user, String policy) {
String iamAuth = auth(ACCOUNT, "iam");
UserCredentials caller = EnforcementFixtures.createUser(cleanup, baseUri.getPort(), iamAuth, user);
EnforcementFixtures.putUserPolicy(baseUri.getPort(), iamAuth, user, "assume", policy);
return caller;
}
private String createRole(String role) {
return EnforcementFixtures.createRole(cleanup, baseUri.getPort(), ACCOUNT, "/", role, """
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"iam:ListUsers","Resource":"*"}]}""");
}
private Response assumeRole(UserCredentials caller, String roleArn, String sessionName, String externalId) {
RequestSpecification request = given().port(baseUri.getPort())
.contentType("application/x-www-form-urlencoded")
.header("Authorization", auth(caller.accessKeyId(), "sts"))
.formParam("Action", "AssumeRole")
.formParam("RoleArn", roleArn)
.formParam("RoleSessionName", sessionName);
if (externalId != null) {
request = request.formParam("ExternalId", externalId);
}
return request.when().post("/");
}
}