fix(iam): give a role session's aws:PrincipalArn as its role's ARN under enforcement (#4810)

Follow-up from the review of #4716.

The enforcement filter put the assumed-role session ARN
(arn:aws:sts::<account>:assumed-role/<role>/<session>) in aws:PrincipalArn for a
caller using role-session credentials. AWS puts the ARN of the role that was
assumed there, path included ("For IAM roles, the request context returns the
ARN of the role, not the ARN of the user that assumed the role", IAM User Guide,
global condition keys). So an identity or resource policy keyed on the role's
ARN never matched its sessions: an Allow scoped that way refused them and a Deny
written that way never fired, while a condition naming the session ARN, which
AWS says not to use for this key, matched.

The filter now reads both values from one lookup of the key, through a new
IamService.resolveCallerArns: the caller ARN, still the session ARN for a role
session, and the principal ARN, the role ARN the session recorded when it was
issued (#4716). A user's key and the account-root principal give the same value
for both. One lookup matters: two could straddle the session's expiry, answer
the first and not the second, and leave a fallback to fill aws:PrincipalArn
with the session ARN, so a Deny keyed on the role's ARN would miss.
resolveCallerArn and resolvePrincipalArn now read through it, and the STS trust
check, which made the same two lookups, uses it too. Both places the filter
fills the key (the request's own authorization and authorizeAdditionalResource)
take it from there. The session ARN stays the caller's principal for matching a
resource policy's Principal, which is a separate change.

docs/services/iam.md describes the key for role sessions and drops the caveat
about the session name, which no longer applies to it.

The integration test covers both paths: IAM calls whose policy is keyed on the
role's ARN, and an SSM GetParameter on a Secrets Manager reference, whose
secretsmanager:GetSecretValue check goes through authorizeAdditionalResource.
The SDK compatibility suite's IamEnforcementTest gets the same IAM checks with
AWS SDK clients; like the rest of that class it runs only against a Floci with
IAM enforcement on.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-10-01 07:50:19 -05:00
1 parent 0a54d0708e
commit cf04f433b6
8 files changed
+260 -40

No files matched your search

@@ -59,6 +59,7 @@ class IamEnforcementTest {
// ── Resource names ─────────────────────────────────────────────────────────
private static final String USER = "iam-enf-test-user";
private static final String ROLE = "iam-enf-test-role";
private static final String PATH_ROLE = "iam-enf-test-path-role";
private static final String POLICY_NAME = "iam-enf-allow-s3list";
private static final String TRUST_POLICY = """
@@ -162,6 +163,11 @@ class IamEnforcementTest {
try { iam.deleteAccessKey(DeleteAccessKeyRequest.builder()
.userName(USER).accessKeyId(userAccessKeyId).build()); } catch (Exception ignored) {}
try { iam.deleteRole(DeleteRoleRequest.builder().roleName(ROLE).build()); } catch (Exception ignored) {}
cleanupResource("delete principal-arn policy from role " + PATH_ROLE,
() -> iam.deleteRolePolicy(DeleteRolePolicyRequest.builder()
.roleName(PATH_ROLE).policyName("principal-arn").build()));
cleanupResource("delete role " + PATH_ROLE,
() -> iam.deleteRole(DeleteRoleRequest.builder().roleName(PATH_ROLE).build()));
try { iam.deletePolicy(DeletePolicyRequest.builder().policyArn(allowPolicyArn).build()); } catch (Exception ignored) {}
try { iam.deleteUser(DeleteUserRequest.builder().userName(USER).build()); } catch (Exception ignored) {}
iam.close();
@@ -505,6 +511,58 @@ class IamEnforcementTest {
}
}
@Test
@Order(13)
@DisplayName("a role session's aws:PrincipalArn is its role's ARN, path included")
void roleSessionPrincipalArnIsTheRolesArnWithItsPath() {
assumeEnforcementEnabled();
String pathRoleArn = iam.createRole(CreateRoleRequest.builder()
.roleName(PATH_ROLE)
.path("/team/")
.assumeRolePolicyDocument(TRUST_POLICY)
.build()).role().arn();
AssumeRoleResponse assumed = sts.assumeRole(AssumeRoleRequest.builder()
.roleArn(pathRoleArn)
.roleSessionName("principal-arn")
.build());
String sessionArn = assumed.assumedRoleUser().arn();
// AWS reports the role's ARN in aws:PrincipalArn, not the session's: a condition on the role
// matches, one on the session ARN does not, and a Deny keyed on the role fires.
iam.putRolePolicy(PutRolePolicyRequest.builder()
.roleName(PATH_ROLE)
.policyName("principal-arn")
.policyDocument("""
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"iam:ListUsers","Resource":"*",
"Condition":{"ArnEquals":{"aws:PrincipalArn":"%1$s"}}},
{"Effect":"Allow","Action":"iam:ListRoles","Resource":"*",
"Condition":{"ArnEquals":{"aws:PrincipalArn":"%2$s"}}},
{"Effect":"Allow","Action":"iam:ListGroups","Resource":"*"},
{"Effect":"Deny","Action":"iam:ListGroups","Resource":"*",
"Condition":{"ArnEquals":{"aws:PrincipalArn":"%1$s"}}}
]}""".formatted(pathRoleArn, sessionArn))
.build());
try (IamClient session = IamClient.builder()
.endpointOverride(TestFixtures.endpoint())
.region(Region.US_EAST_1)
.credentialsProvider(StaticCredentialsProvider.create(AwsSessionCredentials.create(
assumed.credentials().accessKeyId(),
assumed.credentials().secretAccessKey(),
assumed.credentials().sessionToken())))
.build()) {
assertThatCode(session::listUsers).doesNotThrowAnyException();
assertThatThrownBy(session::listRoles)
.isInstanceOf(IamException.class)
.extracting(e -> ((IamException) e).statusCode())
.isEqualTo(403);
assertThatThrownBy(session::listGroups)
.isInstanceOf(IamException.class)
.extracting(e -> ((IamException) e).statusCode())
.isEqualTo(403);
}
}
private static StaticCredentialsProvider userCredentials() {
return StaticCredentialsProvider.create(AwsBasicCredentials.create(userAccessKeyId, userSecretKey));
}