Sub topic 11
Narrowing trust with conditions
This chapter assumes you can read IAM JSON policies, know the difference between a trust policy and an identity policy, and have assumed a role with aws sts assume-role. A trust policy that names an account root trusts every principal in that account that has been granted sts:AssumeRole. Conditions let you narrow that trust without listing each principal.
Where conditions fit
A trust policy is a resource-based policy attached to the role. Its Condition block adds tests that the request must pass, on top of the Principal match. If any condition is false, the statement does not apply and the call fails with AccessDenied.
The caller still needs its own sts:AssumeRole permission in its own account. A condition on the trust policy never replaces that.
Limit trust to an organization
Listing every account that may assume a role does not scale. The aws:PrincipalOrgID condition key matches the AWS Organization that owns the calling principal. The policy below allows any principal from the organization o-exampleorgid.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "*" },
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": { "aws:PrincipalOrgID": "o-exampleorgid" }
}
}
]
}"Principal": {"AWS": "*"} looks alarming, but the condition is what restricts it. Never use * as the principal without a condition that limits the caller. Accounts that join or leave the organization gain or lose access automatically.
Each account in the organization still has to give its own principals sts:AssumeRole permission. The trust policy only says the role is willing to be assumed.
Require MFA
You can require that the caller signed in with multi-factor authentication. The key aws:MultiFactorAuthPresent is true only when the caller's credentials came from an MFA-authenticated session.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111111111111:root" },
"Action": "sts:AssumeRole",
"Condition": {
"Bool": { "aws:MultiFactorAuthPresent": "true" }
}
}
]
}Long-term access keys of an IAM user carry no MFA context, so those calls fail this check. The user must first get an MFA session with aws sts get-session-token, then assume the role using those credentials.
Combine conditions
Several keys inside one Condition block are joined with AND. Every one must be true. Values listed for a single key are joined with OR.
| Goal | Condition key | Typical operator |
|---|---|---|
| Any account in my organization | `aws:PrincipalOrgID` | `StringEquals` |
| Third party with a shared secret | `sts:ExternalId` | `StringEquals` |
| Human with MFA | `aws:MultiFactorAuthPresent` | `Bool` |
| Only a named role in the caller account | `aws:PrincipalArn` | `ArnLike` |
For example, you can require both the organization and MFA in one statement by placing both operators under the same Condition.
Common mistakes
- Using
StringEqualswith a wildcard value. UseStringLikeorArnLikewhen you need wildcards. - Testing
aws:MultiFactorAuthPresentwithBoolfalse to mean "no MFA". The key is absent for long-term keys, so useBoolIfExistswhen you want to deny calls that lack MFA. - Forgetting that an
Allowwith"Principal": {"AWS": "*"}and no condition opens the role to every AWS account that also grants its callers permission. - Assuming a condition replaces the caller-side policy. It does not.
Exercise
A trust policy has Principal {"AWS": "*"} and a single condition, aws:PrincipalOrgID equal to o-exampleorgid. A user in an account inside that organization still gets AccessDenied when calling sts:AssumeRole. Name one likely cause that is not in the trust policy.
Show answer
The user's own identity policy (or a permissions boundary or SCP in the caller's account) does not allow sts:AssumeRole on the role's ARN. The trust policy only permits the assumption. The caller's account must also grant the permission.