Sub topic 3
Reading a policy document
This chapter assumes you know what an IAM user, group, and role are, can read JSON, and have seen an ARN (Amazon Resource Name) and an API action such as s3:GetObject. Here you learn to read any IAM policy one element at a time. Later chapters cover how AWS combines policies.
The shape of a policy
An IAM policy is a JSON document with a version and a list of statements. Each statement is a single permission rule.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ReadWriteReports",
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": "arn:aws:s3:::example-reports-bucket/*"
}
]
}This is an identity-based policy, one you attach to a user, group, or role. Read each statement as a sentence: "Allow these actions on these resources, if these conditions hold, for this principal."
| Element | Required | Answers the question |
|---|---|---|
| `Version` | Yes, in practice | Which policy language rules apply? |
| `Statement` | Yes | What are the rules? |
| `Sid` | No | What do I call this statement? |
| `Effect` | Yes | Allow or Deny? |
| `Principal` | Resource policies and trust policies only | Who does this apply to? |
| `Action` | Yes (or `NotAction`) | Which API operations? |
| `Resource` | Identity policies: yes. Resource policies: optional in some cases | Which objects? |
| `Condition` | No | Under what circumstances? |
The diagram below shows the same structure as a tree.
Version
Version selects the policy language, not the date you wrote the 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}. With the old version, AWS treats those variables as literal text, so the statement quietly matches nothing.
If you leave Version out, AWS treats the policy as the old version. That is a common source of variable-related surprises.
Statement and Sid
Statement is either one object or an array of objects. Use an array even for a single statement, so adding a second one later does not change the structure.
Sid (statement ID) is a label for humans. It has no effect on evaluation. In a policy that supports it, such as a bucket policy, each Sid must be unique within the policy. Use descriptive values like DenyInsecureTransport, because the Sid is what you search for when you debug.
Effect
Effect is either Allow or Deny, with a capital first letter. Nothing else is valid. How these two interact is the subject of the next chapter. For now, keep one point in mind: a statement that does not match a request has no effect on it, whether it says Allow or Deny.
Action and NotAction
Action lists the operations the statement covers. Each one has the form service:Operation, for example s3:GetObject or ec2:DescribeInstances. You can use these forms:
- A single string or a list of strings:
"s3:GetObject"or["s3:GetObject", "s3:PutObject"]. - Wildcards:
s3:Get*matchess3:GetObject,s3:GetBucketPolicy, and every other action starting withGet.s3:*matches every S3 action. - Names are not case-sensitive, so
S3:getobjectworks. Writing them in the documented case keeps policies readable.
NotAction matches every action except those listed. Combined with Allow, it does not deny the listed actions. It only fails to grant them. Combined with Deny, it blocks everything except the listed actions. Because the effect is hard to see at a glance, use NotAction only when you can explain why a plain Action list will not do.
Wildcards in Action are the usual way policies grow too broad. "Action": "*" with "Resource": "*" grants administrator access.
Resource and NotResource
Resource lists the objects the statement covers, written as ARNs. Not every action supports every kind of resource. The service authorization reference for each service lists which resource types each action applies to. Two rules cause most mistakes.
First, S3 uses different ARNs for a bucket and the objects in it:
arn:aws:s3:::example-reports-bucketis the bucket itself.arn:aws:s3:::example-reports-bucket/*is every object in the bucket.
Bucket-level actions such as s3:ListBucket need the first ARN. Object-level actions such as s3:GetObject need the second. A policy that grants s3:ListBucket on the /* ARN does nothing, and the ListBucket call is still denied.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListTheBucket",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::example-reports-bucket"
},
{
"Sid": "ReadObjects",
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example-reports-bucket/*"
}
]
}Second, some actions do not support resource-level restrictions. ec2:DescribeInstances is one example. For those, the only usable value is "Resource": "*". If you name a specific ARN, the statement never matches those actions.
NotResource is the counterpart of NotAction. It matches every resource except those listed. It has the same cautions.
Principal
Principal names who the statement applies to. Whether you may use it depends on where the policy lives.
- In an identity-based policy, leave it out. The principal is whoever the policy is attached to, so adding
Principalmakes the policy invalid. - In a resource-based policy, such as an S3 bucket policy, it is required. The resource has no "attached to" identity, so you name the principal.
- In a role's trust policy, it names who may assume the role.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowReportReaderRoleFromOtherAccount",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::222233334444:role/ReportReader" },
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example-reports-bucket/*"
}
]
}The AWS key holds IAM entities and accounts. Other keys include Service (for example "lambda.amazonaws.com") and Federated. The value "*" means everyone, including anonymous callers, so treat it with great care. Naming an account root, arn:aws:iam::222233334444:root, means "that account", delegating the decision to that account's own IAM policies. It does not mean only the root user.
Always write role principals as full ARNs. If you write a user or role ARN and that identity is later deleted and recreated, the policy stores an internal ID in place of the ARN and no longer matches the new identity. Delete and recreate carefully.
NotPrincipal exists but is rarely a good choice. In an Allow statement it is not supported, and in a Deny statement it is easy to get wrong. Prefer a Deny with a condition such as aws:PrincipalArn.
Condition
Condition adds tests that must all be true for the statement to match. It has three parts: an operator, a condition key, and a value.
{
"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" } }
}
]
}Read it from the inside out: the key aws:SecureTransport is true when the request used TLS. The operator Bool compares it with "false". When the test passes, the statement applies, so the Deny takes effect for every non-TLS request to that bucket. This policy also lists both bucket ARNs in a list, which Resource accepts.
Two details to remember here. Several keys under one operator must all be true (AND), while several values for one key need only one match (OR). A condition on a key that is missing from the request is treated as not matching, which is why the operators have IfExists and Null variants. The full set of operators and keys gets its own chapter.
Common errors when reading and writing policies
- Using the old
Version, so policy variables are not substituted. - Putting
s3:ListBucketagainst the object ARN, ors3:GetObjectagainst the bucket ARN. - Adding
Principalto an identity-based policy, which makes the policy invalid. - Leaving
Principalout of a bucket policy, which makes it invalid. - Assuming
NotActionwithAllowdenies the listed actions. - Reading a statement as if its
Sidmattered for evaluation. It does not. - Using a wildcard that is broader than intended, such as
s3:*when onlys3:Get*was needed.
Checking a policy before you use it
You do not have to guess whether a document is well formed. The IAM console policy editor validates syntax and warns about problems. IAM Access Analyzer can also validate a policy and flag security warnings and suggestions. Use these before you attach a policy, especially one that includes a Principal.
Exercise
A teammate writes an identity-based policy with "Effect": "Allow", "Action": "s3:ListBucket", and "Resource": "arn:aws:s3:::example-reports-bucket/*", and also adds a Principal element naming a role. List the two problems and how to fix each.
Show answer
First, Principal is not allowed in an identity-based policy, because the principal is whoever the policy is attached to. Remove it and attach the policy to the role instead. Second, s3:ListBucket applies to the bucket, not to its objects, so the resource must be arn:aws:s3:::example-reports-bucket without /*.
Exercise
A statement has "Effect": "Deny", "Action": "s3:*", and the condition { "Bool": { "aws:SecureTransport": "false" } }. Does this statement match a request that arrives over HTTPS?
Show answer
No. Over HTTPS, aws:SecureTransport is true, so the Bool test against "false" fails. The condition is not met, the statement does not apply to that request, and the Deny has no effect on it.