Sub topic 8
Giving the role only the permissions it needs
This chapter assumes you have created the OIDC provider and a role with a trust policy, as covered earlier. It also assumes you know the basics of IAM policies in JSON.
Two policies, two questions
A deployment role carries two separate policies:
- The trust policy answers "who may assume this role?"
- The permissions policy answers "what can the role do once assumed?"
A tight trust policy does not limit what a job can do after it gets in. If the role has AdministratorAccess, any workflow that passes the trust check controls your whole account. The permissions policy is your second line of defense.
Start from what the workflow does
The deploy workflow in this tutorial runs two commands: aws sts get-caller-identity and aws s3 sync ./site s3://my-example-bucket --delete. The first needs no permissions. The second needs to list the bucket, upload objects, and delete objects.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListSiteBucket",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::my-example-bucket"
},
{
"Sid": "ManageSiteObjects",
"Effect": "Allow",
"Action": [
"s3:PutObject",
"s3:DeleteObject"
],
"Resource": "arn:aws:s3:::my-example-bucket/*"
}
]
}Note the two resource ARNs. s3:ListBucket applies to the bucket itself. s3:PutObject and s3:DeleteObject apply to objects inside it, which is why the second ARN ends in /*. Putting an object action on the bucket ARN is a common mistake, and it produces AccessDenied during the sync.
Attach the policy
Save the JSON as deploy-policy.json, then add it to the role as an inline policy:
aws iam put-role-policy \
--role-name github-deploy \
--policy-name deploy-site \
--policy-document file://deploy-policy.jsonAn inline policy lives and is deleted with the role. Use a managed policy instead if several roles need the same permissions.
Narrowing further
- Name resources explicitly. Avoid
"Resource": "*"unless the action does not support resource-level permissions. - Avoid wildcard actions.
s3:*grants far more than a sync needs. - Split roles by job. A role that deploys to production should not be the role that runs pull request previews.
- Let access analysis help. After the role has been used, IAM Access Analyzer can generate a policy from CloudTrail activity. Treat the result as a draft to review.
How the pieces connect
Exercise
The sync step fails with AccessDenied on s3:DeleteObject, although the policy lists that action with "Resource": "arn:aws:s3:::my-example-bucket". What is wrong, and how do you fix it?
Show answer
Object-level actions such as s3:DeleteObject apply to objects, so the resource must be "arn:aws:s3:::my-example-bucket/". The bucket ARN only matches bucket-level actions such as s3:ListBucket. Move the object actions to a statement whose resource ends in /.