Sub topic 2
Identity-Based Policies
This chapter assumes you know AWS accounts, IAM users and roles, JSON, API actions, and Amazon Resource Names (ARNs).
Identity-Based Policies
An identity-based policy is attached to an IAM user, group, or role. It grants that identity permission to perform actions on resources. It does not contain a Principal element because the attached identity is the principal.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ReadWriteReports",
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": "arn:aws:s3:::example-reports-bucket/*"
}
]
}This policy allows the attached identity to read and write objects in the bucket. An identity policy alone does not override an explicit Deny from another applicable policy.
Resource-Based Policies
A resource-based policy is attached to a resource, such as an Amazon S3 bucket, an SQS queue, or an SNS topic. It names the principal that may access the resource.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowReportReader",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::222233334444:role/ReportReader"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example-reports-bucket/*"
}
]
}The bucket owner can use this policy to permit a role from another account. The role still needs an identity policy that allows s3:GetObject for a cross-account request.
Narrowing Statements with Conditions
A Condition adds tests that must be true for a statement to apply. Conditions can restrict access by transport security, source IP, tags, or other request context.
{
"Sid": "DenyInsecureTransport",
"Effect": "Deny",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::example-reports-bucket",
"arn:aws:s3:::example-reports-bucket/*"
],
"Condition": {
"Bool": {
"aws:SecureTransport": "false"
}
}
}This explicit deny blocks requests that do not use TLS. An allow in another policy cannot override it.
Same-Account and Cross-Account Requests
For a same-account request, an applicable identity policy or resource policy can provide the allow, subject to boundaries, session policies, service control policies, and explicit denies.
A cross-account request normally needs both sides:
- The caller’s identity policy must allow the action.
- The resource policy must trust the caller.
- Permissions boundaries, session policies, SCPs, and explicit denies must not block it.
Exercise
A role has an identity allow for s3:GetObject, but a bucket policy explicitly denies the request. What is the result?
Show answer
AccessDenied, because an explicit deny overrides every allow.