Sub topic 5
Anatomy of a policy document
This chapter assumes you know what an IAM policy is and which policy types exist. It also assumes you can read JSON. Here you will read a policy document element by element, so you can write one and spot the usual mistakes.
The shape of a policy
A policy is a JSON document. It has an optional top-level Id, a Version, and a Statement. Each statement is a separate permission rule. AWS evaluates the statements independently and combines the results.
Here is the policy this chapter dissects. It lets an identity list one bucket and read the objects in it.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListTheBucket",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::amzn-s3-demo-bucket"
},
{
"Sid": "ReadObjects",
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*"
}
]
}Version
Version is the policy language version, not a revision number of your policy. Always use "2012-10-17". The older value 2008-10-17 is still accepted, but it does not support policy variables such as ${aws:username}. If you omit Version, AWS treats the policy as the old version. A variable in such a policy is then treated as literal text, and the statement quietly stops matching what you intended.
Statement
Statement holds either one statement object or an array of them. Use an array even for a single statement, because it makes later additions easier and matches what the console generates.
Each statement can contain these elements:
| Element | Required | Purpose |
|---|---|---|
| `Sid` | No | A label for the statement. It must be unique within the policy. |
| `Effect` | Yes | `Allow` or `Deny`. |
| `Principal` | Only in resource-based and trust policies | Who the statement applies to. |
| `Action` or `NotAction` | Yes (one of them) | The API operations covered. |
| `Resource` or `NotResource` | Yes in identity-based policies | The objects the actions apply to. |
| `Condition` | No | Extra tests that must be true for the statement to apply. |
Effect
Effect is either Allow or Deny. Everything is denied by default, so an Allow opens something up. A Deny is an explicit refusal that overrides any Allow anywhere. The full evaluation order is covered in a later chapter.
Action
An action is written as service:Operation, for example s3:GetObject or ec2:StopInstances. It matches the name of an API operation, although some actions are permissions with no matching API call.
You can use wildcards. * matches any run of characters, and ? matches a single character.
s3:Get*matchess3:GetObject,s3:GetBucketPolicy, and every other action starting withs3:Get.iam:*AccessKey*matches every action withAccessKeyin its name.*alone matches every action in every service.
Action also accepts a list of strings. Prefer explicit lists over broad wildcards, because a new action added to a service later can match a wildcard you wrote years ago.
Resource
Resource lists the Amazon Resource Names (ARNs) the statement covers. An ARN has this form:
arn:partition:service:region:account-id:resourceSome services leave fields empty. S3 bucket ARNs have no region or account ID, which is why the example reads arn:aws:s3:::amzn-s3-demo-bucket.
The example shows an important point: bucket-level and object-level actions use different resource ARNs. s3:ListBucket acts on the bucket, so its resource is the bucket ARN. s3:GetObject acts on objects, so its resource is bucket-arn/*. Putting both actions on one ARN is the most common S3 policy mistake. It produces a policy that is valid JSON but does not grant the access you wanted.
Principal
Identity-based policies, which are attached to a user, group, or role, never contain Principal. The identity the policy is attached to is the principal. Resource-based policies and role trust policies do contain it, because they must say who is being granted access. Those uses appear in later chapters.
Condition
Condition limits when a statement applies. It is a map from a condition operator to one or more key-value tests, for example "Bool": { "aws:MultiFactorAuthPresent": "true" }. All operators in the block must be true for the statement to apply. Conditions get their own chapter, so here you only need to recognize where the block sits: at the end of the statement.
NotAction and NotResource
NotAction matches everything except the listed actions. NotResource matches everything except the listed resources. They are easy to misread.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"NotAction": "iam:*",
"Resource": "*"
}
]
}This policy does not deny IAM actions. It allows every action in every other service. It also allows actions that did not exist when you wrote it. If you want to block IAM, you must add a separate Deny. Use NotAction mostly in Deny statements, where "deny everything except these" is a safe and common guardrail.
Common errors
- Missing or wrong
Version. Policy variables stop working. Always write2012-10-17. - Mismatched action and resource. A bucket ARN on
s3:GetObject, or an object ARN ons3:ListBucket, grants nothing. Principalin an identity-based policy. IAM rejects the policy when you save it.- Duplicate
Sidvalues. The policy fails validation. - Overly broad wildcards.
"Action": "*"with"Resource": "*"grants full administrator access. - Misusing
NotActionwithAllow. It grants far more than it appears to. - Exceeding size limits. Policies have length limits that depend on type. A customer managed policy can be at most 6,144 characters, and inline policies have lower limits depending on whether they are attached to a user, group, or role. Whitespace is not counted.
Checking a policy from the command line
You can validate a document without attaching it. Save the policy as policy.json, then run:
aws accessanalyzer validate-policy \
--policy-document file://policy.json \
--policy-type IDENTITY_POLICYThe response lists findings with a type (ERROR, SECURITY_WARNING, WARNING, or SUGGESTION) and a message explaining the problem.
Exercise
A policy contains one statement with "Effect": "Allow", "Action": ["s3:ListBucket", "s3:GetObject"], and "Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*". A user with only this policy can read objects but gets AccessDenied when listing the bucket. Why, and how do you fix it?
Show answer
s3:ListBucket acts on the bucket itself, and its resource is the bucket ARN, arn:aws:s3:::amzn-s3-demo-bucket. The ARN with / matches only objects, so the list call is not covered and is implicitly denied. Fix it by splitting the policy into two statements: s3:ListBucket on the bucket ARN, and s3:GetObject on the bucket ARN followed by /.
Exercise
What does a statement with "Effect": "Allow", "NotAction": "iam:", and "Resource": "" allow, and does it prevent IAM actions?
Show answer
It allows every action in every service except IAM actions. It does not prevent IAM actions. Another policy can still allow them. To block IAM you need an explicit Deny statement.