Sub topic 11
Narrowing trust with conditions
This chapter assumes you can read IAM JSON policies and have already written a basic role trust policy. You should know that an assume-role call needs an Allow in the role's trust policy and an Allow in the caller's identity policy. Here you add Condition blocks to the trust policy so that "this account can assume the role" becomes "only these callers, in these circumstances, can assume the role".
Why account-level trust is too broad
The basic trust policy names another account's root as the principal:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111111111111:root" },
"Action": "sts:AssumeRole"
}
]
}This does not mean only the root user can assume the role. It delegates the decision to account 111111111111. Any user or role there that holds sts:AssumeRole permission for this role can assume it. If someone in that account later gets a broad sts:AssumeRole on *, they can reach your role too.
Conditions let the role's owner add checks that the other account's administrators cannot override. They are evaluated against the request context, and the trust policy is satisfied only if the principal matches and every condition in the statement is true.
Condition basics in a trust policy
A condition has an operator, a condition key, and one or more values. Several keys under one operator are combined with AND. Several values for one key are combined with OR.
The condition keys most useful in trust policies are listed here.
| Key | What it checks |
|---|---|
| `aws:PrincipalOrgID` | The ID of the AWS Organization that owns the caller's account |
| `aws:PrincipalOrgPaths` | The organizational unit path of the caller's account |
| `aws:PrincipalArn` | The ARN of the calling user or role |
| `aws:MultiFactorAuthPresent` | Whether the caller's credentials were issued after MFA |
| `aws:MultiFactorAuthAge` | Seconds since MFA authentication |
| `sts:ExternalId` | The external ID the caller supplied |
| `sts:RoleSessionName` | The session name the caller supplied |
| `aws:SourceIp` | The caller's IP address, which is only meaningful for calls made directly from that address |
Limiting trust to an AWS Organization
If you want every account in your organization to be able to assume a role, you do not want to list each account. Use aws:PrincipalOrgID and set the principal to a wildcard:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "*" },
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": { "aws:PrincipalOrgID": "o-exampleorgid" }
}
}
]
}New accounts that join the organization are covered automatically, and accounts that leave are cut off.
Even with this trust policy, nothing is granted yet to individual users. Each caller still needs sts:AssumeRole on the role ARN in their own account's identity policy, as described in the earlier chapter on caller policies. The organization condition only opens the trust side.
Limiting trust to specific callers
The account root principal can be paired with aws:PrincipalArn to name particular roles or users while still trusting the account:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111111111111:root" },
"Action": "sts:AssumeRole",
"Condition": {
"ArnLike": {
"aws:PrincipalArn": "arn:aws:iam::111111111111:role/audit-*"
}
}
}
]
}Only roles in account 111111111111 whose names begin with audit- can assume this role. You could instead put a specific role ARN directly in Principal. The trade-off is this: when IAM stores a role ARN in a trust policy, it converts it to a unique principal ID. If the named role is deleted and recreated with the same name, the new role has a different ID, and the trust policy no longer matches it. A condition on aws:PrincipalArn compares the ARN string at request time, so it survives recreation. Prefer the account root plus aws:PrincipalArn when the other account may rebuild its roles, for example through infrastructure-as-code.
You can also constrain the session name, which is useful for attributing activity in logs:
"Condition": {
"StringLike": { "sts:RoleSessionName": "audit-*" }
}A caller who passes --role-session-name tmp is denied.
Requiring MFA
For human access to a sensitive role, require that the caller authenticated with a multi-factor authentication (MFA) device:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::111111111111:root" },
"Action": "sts:AssumeRole",
"Condition": {
"Bool": { "aws:MultiFactorAuthPresent": "true" },
"NumericLessThan": { "aws:MultiFactorAuthAge": "3600" }
}
}
]
}The second condition requires the MFA authentication to be less than an hour old. A user whose credentials are plain long-term access keys has no MFA context. For such a request the aws:MultiFactorAuthPresent key is absent, the Bool test fails, and the Allow statement does not apply.
To satisfy the condition from the AWS CLI, the profile must tell the CLI which MFA device to use. The CLI then prompts for a code and passes it to AssumeRole:
[profile auditor-mfa]
role_arn = arn:aws:iam::222222222222:role/ReadOnlyAuditor
source_profile = default
mfa_serial = arn:aws:iam::111111111111:mfa/alice
region = us-east-1aws sts get-caller-identity --profile auditor-mfaWhen you call aws sts assume-role directly, pass --serial-number and --token-code instead.
Choosing between `Bool` and `BoolIfExists`
Operators have a variant with the suffix IfExists. It treats a missing key as a match. This is useful in Deny statements and risky in Allow statements.
- In an Allow statement, use
Bool. A missing MFA key then fails the condition, which is what you want. - In a Deny statement that blocks callers without MFA, use
BoolIfExistswith"false". A plainBoolcheck on a missing key does not match, so callers using long-term keys would slip past the Deny.
A deny pattern looks like this when placed in a permissions or guardrail policy:
{
"Effect": "Deny",
"Action": "*",
"Resource": "*",
"Condition": {
"BoolIfExists": { "aws:MultiFactorAuthPresent": "false" }
}
}Combining conditions
Conditions in a statement are all required. You can therefore layer them. A role for a third-party vendor might require both an external ID and a session-name prefix. A role for staff might require organization membership and MFA:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "*" },
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": { "aws:PrincipalOrgID": "o-exampleorgid" },
"Bool": { "aws:MultiFactorAuthPresent": "true" }
}
}
]
}To express OR across different keys, write separate statements. Do not try to combine them in one Condition block.
Common mistakes
- Wrong operator for wildcards.
StringEqualstreats*literally. UseStringLikefor session names andArnLikefor ARNs when you use wildcards. - Assuming a condition replaces the caller's permission. The caller's identity policy must still allow
sts:AssumeRole. - Using
aws:SourceIpfor calls made through AWS services. When a service calls on your behalf, the source address is the service's, not your user's. - Testing only the allowed path. Always test one request that should fail, such as a caller from another organization or without MFA, and confirm
AccessDenied. - Using the wrong organization ID. The ID starts with
o-. The root ID (r-) and an OU ID (ou-) are different values and do not work withaws:PrincipalOrgID.
Verifying a condition works
After you change a trust policy, call the role and inspect the result:
aws sts get-caller-identity --profile auditor-mfaAn ARN that looks like arn:aws:sts::222222222222:assumed-role/ReadOnlyAuditor/... confirms the assumption. If it is denied, check CloudTrail in the role's account for the AssumeRole event. The errorMessage field says whether the failure came from the trust policy or from the caller's side.
Exercise
A role in account 222222222222 has a trust policy with Principal set to the root of account 111111111111 and the condition "Bool": { "aws:MultiFactorAuthPresent": "true" }. A user in 111111111111 has the correct sts:AssumeRole permission but runs aws sts assume-role using long-term access keys and no --serial-number or --token-code. What happens, and what two changes would let the user succeed?
Show answer
The call is denied. Long-term keys without MFA give no aws:MultiFactorAuthPresent value, so the Bool condition is not met and the Allow statement does not apply. The user can succeed by passing --serial-number and --token-code with a current MFA code, or by using a CLI profile with mfa_serial set so the CLI prompts for the code. (Removing the MFA condition would also work, but it weakens the policy.)
Exercise
Why might you choose "Principal": { "AWS": "arn:aws:iam::111111111111:root" } with an aws:PrincipalArn condition over naming a specific role ARN directly in Principal?
Show answer
A role ARN in Principal is stored as the role's unique ID, so if the role is deleted and recreated with the same name, the trust policy stops matching it. An aws:PrincipalArn condition compares the ARN string at request time, so it keeps working after recreation.