Sub topic 7
Controlling access with IAM, bucket policies and Block Public Access
This chapter assumes you know what an IAM user or role is, can read JSON, and have created a bucket. S3 buckets are private by default. Access is granted by policies, and three mechanisms decide whether a request succeeds.
The three layers
- IAM identity policies attach to a user, group, or role. They say what that identity may do, in any bucket.
- Bucket policies attach to one bucket. They say who may do what to that bucket and its objects, including principals from other accounts.
- Block Public Access (BPA) is a set of four switches that override any policy or ACL that would make data public.
S3 evaluates all of them for each request. An explicit Deny always wins. Otherwise the request needs an Allow from somewhere. Within one account, an Allow in either the identity policy or the bucket policy is enough. For a principal in another account, both sides must allow the request.
Block Public Access
BPA has four settings. Turn on all four unless a bucket is meant to be public.
| Setting | Effect |
|---|---|
| `BlockPublicAcls` | Rejects new public ACLs |
| `IgnorePublicAcls` | Ignores existing public ACLs |
| `BlockPublicPolicy` | Rejects bucket policies that grant public access |
| `RestrictPublicBuckets` | Limits access to a bucket with a public policy to AWS services and authorized users in the account |
You can set BPA on a bucket or for the whole account. The account-level setting applies to every bucket in it.
aws s3api put-public-access-block \
--bucket my-example-bucket-123456789012 \
--public-access-block-configuration BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=trueA bucket policy
A bucket policy is a JSON document. Each statement names an effect, a principal, actions, resources, and optional conditions. This policy denies every request that does not use HTTPS.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyInsecureTransport",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::my-example-bucket-123456789012",
"arn:aws:s3:::my-example-bucket-123456789012/*"
],
"Condition": {
"Bool": { "aws:SecureTransport": "false" }
}
}
]
}Apply it with aws s3api put-bucket-policy --bucket my-example-bucket-123456789012 --policy file://policy.json.
The Resource list has two entries because bucket-level actions, such as s3:ListBucket, use the bucket ARN. Object-level actions, such as s3:GetObject, use the ARN ending in /*.
Common errors
- Wrong resource ARN. Granting
s3:ListBucketonbucket/*has no effect. Listing needs the bucket ARN without/*. - Access Denied with a correct Allow. Look for an explicit Deny, a BPA setting, or a missing permission on the other account's side.
- Policy rejected on save. With
BlockPublicPolicyon, a policy withPrincipal: "*"and an Allow is refused. A Deny statement like the one above is fine. - Locking yourself out. A broad Deny can block your own administrators. Add a condition that exempts a trusted role before you apply it.
- Relying on ACLs. Access control lists are a legacy mechanism. Prefer policies, and set bucket ownership to "bucket owner enforced" so ACLs are disabled.
Exercise
A role has an IAM policy allowing s3:GetObject on a bucket, but the bucket policy contains an explicit Deny for the same action when the request is not over HTTPS. A request arrives over plain HTTP. Is it allowed, and why?
Show answer
No. An explicit Deny always overrides an Allow. The bucket policy's Deny condition matches the HTTP request, so S3 rejects it even though the IAM policy allows the action.