Sub topic 4
What a policy is and which policy types exist
This chapter assumes you know that IAM separates authentication (proving who you are) from authorization (deciding what you may do). It also assumes you can read JSON. You will learn what an IAM policy is, which types exist, and which question each type answers. The next chapter covers the document syntax in detail. A later chapter covers how AWS combines the types into one decision.
What a policy is
A policy is a JSON document that defines permissions. When a principal (a user, role, or federated session) sends a request, AWS checks the policies that apply and allows or denies the request. Policies are not active until they are attached to an identity or a resource, or applied to an account or organization unit.
A policy statement answers four questions:
- Who is acting? This is the principal.
- What action do they want to perform?
- On which resource?
- Under which conditions?
Some policy types state the principal explicitly. Others infer it from where the policy is attached.
Here is a small identity-based policy. Do not worry about every field yet, because the next chapter takes it apart.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "sqs:SendMessage",
"Resource": "arn:aws:sqs:us-east-1:111122223333:orders-queue"
}
]
}Attach this to a role, and any session that uses the role can send messages to that one queue. Everything else stays denied by default.
Grant policies and limit policies
The most useful way to sort the policy types is by what they do. Some policies grant permissions. Others only set an upper limit on what other policies can grant.
- Granting policies can give permissions: identity-based and resource-based policies, plus ACLs.
- Limiting policies never give permissions on their own: permissions boundaries, SCPs, RCPs, and session policies.
A limiting policy is a filter. If an action is not allowed by a granting policy, a limit cannot make it allowed. If an action is allowed by a granting policy but outside the limit, the request fails.
The policy types
Identity-based policies
An identity-based policy is attached to a user, group, or role. It states what that identity may do. It has no Principal element, because the principal is whoever the policy is attached to.
There are three flavors:
- AWS managed policies are created and maintained by AWS, such as
ReadOnlyAccess. They are convenient but often broader than you need. - Customer managed policies are standalone policies you create. You can attach one to many identities and keep versions of it.
- Inline policies are embedded in a single identity and deleted with it.
Resource-based policies
A resource-based policy is attached to a resource, such as an S3 bucket, an SQS queue, a KMS key, or a role's trust policy. It states who may use that resource, so it must include a Principal element. It can name principals in other accounts, which is why resource-based policies are one way to share access across accounts. A later chapter covers this in detail.
A bucket policy is the most common example:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::444455556666:root" },
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*"
}
]
}Permissions boundaries
A permissions boundary is a managed policy you set as the maximum permissions for a user or role. The identity's effective permissions are the overlap of its identity-based policies and the boundary. A boundary on its own grants nothing.
Service control policies (SCPs)
An SCP is an AWS Organizations policy that sets the maximum permissions for the principals in the accounts it applies to. It can apply to an entire organization, an organizational unit, or a single account. SCPs affect IAM users and roles in member accounts, including the account's root user. They do not affect the management account's principals, and they do not affect service-linked roles.
Resource control policies (RCPs)
An RCP is also an Organizations policy. Instead of limiting what principals can do, it limits the permissions available on resources in the accounts it applies to. This lets you enforce a rule on a resource type, such as "S3 buckets cannot be accessed over unencrypted connections", regardless of how each resource's own policy is written. Support is limited to a set of services, so check the current list before relying on it.
Access control lists (ACLs)
An ACL controls which principals in other accounts can access a resource. ACLs are the only policy type that does not use the JSON policy format. They exist mainly for older features, such as S3 object ACLs and some other services. For new designs, AWS recommends resource-based policies instead.
Session policies
A session policy is passed as a parameter when you create a temporary session programmatically, for example when you call AssumeRole. It limits the permissions of that session. The session's effective permissions are the overlap of the role's identity-based policy and the session policy. Like boundaries, session policies cannot add permissions.
Comparing the types
| Policy type | Attached to | Grants or limits | Has `Principal` |
|---|---|---|---|
| Identity-based | User, group, role | Grants | No |
| Resource-based | A resource | Grants | Yes |
| Permissions boundary | User or role | Limits | No |
| SCP | Organization, OU, or account | Limits principals | No |
| RCP | Organization, OU, or account | Limits resources | Yes, usually `*` |
| ACL | A resource | Grants | Not in JSON |
| Session policy | A temporary session | Limits | No |
Choosing the right type
Match the type to the question you are trying to answer:
- "What should this person or workload be able to do?" Use an identity-based policy, normally attached to a group or role rather than directly to a user.
- "Who may use this particular resource, including people outside my account?" Use a resource-based policy.
- "How far may delegated administrators go when they create roles?" Use a permissions boundary.
- "What must never happen in these accounts, regardless of local policies?" Use an SCP, or an RCP if the rule is about resources.
- "How can I hand out a narrower slice of a role for one session?" Use a session policy.
Common mistakes
- Expecting a limit to grant access. An SCP that allows
s3:*does not give anyone S3 access. A principal still needs a granting policy. - Forgetting that SCPs and RCPs do not apply in the management account. Do not rely on them there.
- Using
Principalin an identity-based policy. IAM rejects it, because the principal is implied. - Leaving out
Principalin a resource-based policy. Without it, the policy is not valid. - Reaching for ACLs by default. Most new designs do not need them.
Exercise
A role has an identity-based policy that allows s3:* on all resources. A permissions boundary on the same role allows only ec2:*. Can the role call s3:GetObject? Can it call ec2:StopInstances if the identity-based policy has no EC2 statement?
Show answer
No to both. For s3:GetObject, the identity-based policy allows it, but the boundary does not, so the request fails. For ec2:StopInstances, the boundary allows it, but a boundary never grants permissions. The identity-based policy has no EC2 statement, so nothing grants the action and the request is denied.
Exercise
You want to guarantee that no principal in any account of your organization can delete CloudTrail trails, even if an account administrator writes a policy that allows it. Which policy type do you use, and where do you attach it?
Show answer
A service control policy that denies cloudtrail:DeleteTrail, attached to the organization root or to the relevant organizational unit. Because an SCP limits what member-account principals can be granted, no local identity-based policy can override it. It does not apply to the management account's principals, so protect that account separately.