Sub topic 7
Narrowing statements with conditions
This chapter assumes you can read an IAM policy statement (Effect, Action, Resource) and know that an explicit deny overrides any allow. Both come from earlier chapters.
What a condition does
A Condition block makes a statement apply only when the request context matches. The context is the set of key-value facts AWS collects for each request. It includes the caller's identity, the source IP address, whether TLS was used, and the tags on the resource. The statement's Effect takes hold only if every condition is true. If a condition is false, AWS treats the statement as if it did not exist.
A condition has three parts: an operator, a condition key, and one or more values.
"Condition": { "Bool": { "aws:SecureTransport": "false" } }Here Bool is the operator, aws:SecureTransport is the key, and "false" is the value to compare against.
A condition on a Deny
This statement denies every request to the bucket that does not use TLS. No Allow in any other policy can override it.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyInsecureTransport",
"Effect": "Deny",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::example-reports-bucket",
"arn:aws:s3:::example-reports-bucket/*"
],
"Condition": { "Bool": { "aws:SecureTransport": "false" } }
}
]
}The statement lists both the bucket ARN and the object ARN pattern. Bucket-level actions such as s3:ListBucket use the first. Object-level actions such as s3:GetObject use the second.
How multiple conditions combine
The combination rules cause most condition mistakes, so learn them early.
| Situation | Logic |
|---|---|
| Several values under one key | OR: any one value can match |
| Several keys under one operator | AND: all keys must match |
| Several operators in one block | AND: all operators must match |
This Allow statement uses two operators and three values:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListReportsFromOffice",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::example-reports-bucket",
"Condition": {
"StringLike": { "s3:prefix": ["reports/*", "archive/*"] },
"IpAddress": { "aws:SourceIp": "203.0.113.0/24" }
}
}
]
}The request must come from 203.0.113.0/24 and ask for a prefix of reports/ or archive/. Failing either test means the Allow does not apply, so the request falls back to implicit deny.
Choosing operators
StringEqualscompares exact text, and case matters.StringLikeallows*and?wildcards.StringEqualstreats*as a literal character, so a wildcard value will silently never match.ArnLikeandArnEqualscompare ARNs.IpAddressandNotIpAddresscompare CIDR ranges.Boolcompares"true"or"false".Nulltests whether a key is present:"true"means absent,"false"means present.
You can prefix an operator with ForAnyValue: or ForAllValues: when the request key holds several values, such as a list of tag keys.
Missing keys
Not every key exists in every request. A key that is absent makes most positive operators evaluate to false. Negated operators such as StringNotEquals evaluate to true when the key is absent.
That difference is dangerous in a Deny statement. Suppose you write a Deny with StringNotEquals on aws:PrincipalTag/team. It also denies any principal that has no team tag at all. This may be what you want, but check it deliberately. To handle absence explicitly, add a Null test.
Where keys come from
Global keys start with aws: and work across services, for example aws:SourceIp, aws:SecureTransport, aws:PrincipalOrgID, and aws:RequestedRegion. Service keys start with the service prefix, such as s3:prefix. A service key only has a value for the actions that supply it. Using s3:prefix on s3:GetObject never matches, because that action does not send the key.
Conditions are allowed in identity policies, resource policies, permission boundaries, session policies, and SCPs.
Exercise
A Deny statement uses "StringNotEquals": { "aws:RequestedRegion": "eu-west-1" } to block other Regions. A call to a global service that supplies no Region value arrives. Is the Deny likely to apply, and which operator would let you check for the missing key explicitly?
Show answer
The Deny is likely to apply, because a negated operator evaluates to true when the key is absent. You can test for absence explicitly with the Null operator, for example "Null": { "aws:RequestedRegion": "false" }, which restricts the Deny to requests where the key is present.