Sub topic 3
Defining what the role can do
This chapter covers the permissions policy of a role: the part that decides what a caller can do after assuming it. It assumes you know IAM users, roles, and JSON policy documents, and that you have used the AWS CLI. Trust policies and the caller's sts:AssumeRole permission come in the next two chapters. Here we only decide what the role is allowed to do.
Two questions, two policies
A role answers two separate questions, and each has its own policy.
| Question | Policy | Lives in |
|---|---|---|
| Who may become this role? | Trust policy | The role's account |
| What may the role do once assumed? | Permissions policy | The role's account |
The permissions policy is an ordinary IAM identity policy attached to the role. It uses the same Effect, Action, Resource, and Condition elements you already know from users and groups. Nothing in it mentions the other account. It describes what the role can do, not who can use it.
In the running example, account 222222222222 owns a role named ReadOnlyAuditor. People in account 111111111111 will assume it. The permissions policy decides what those people can see in account 222222222222.
What the session actually gets
When a caller assumes a role, STS returns temporary credentials for a new session. The session acts as the role, not as the original user. The caller's own identity permissions do not carry over. Three consequences follow.
- If your user in account
111111111111can delete S3 buckets, that ability disappears while you use the role's credentials. The session can only do what the role allows. - If your user cannot read a bucket in account
222222222222but the role can, the session can read it. - Actions in account
111111111111are not available through the session unless the role's policy and the resources in that account allow them.
You will often see the effective permissions of a session described as an intersection. The session can perform an action only if every applicable layer allows it:
- The role's permissions policies.
- The role's permissions boundary, if one is set.
- Service control policies (SCPs) that apply to the role's account.
- A session policy, if the caller passed one when assuming the role.
An explicit Deny in any of these wins. The last two layers are covered in later chapters. For now, remember that the permissions policy is where access is granted, and the other layers can only narrow it.
Choosing a policy type
You can give a role permissions in three ways.
- AWS managed policies are maintained by AWS.
arn:aws:iam::aws:policy/ReadOnlyAccessis a common choice for auditing. They are quick to attach, but they are broad, and AWS can change them when new services appear. - Customer managed policies are policies you create in the role's account. You can attach one to many roles and keep a version history.
- Inline policies are embedded in a single role and deleted with it. They suit permissions that belong to exactly one role.
For cross-account roles, a customer managed policy is usually the best fit when more than one role shares the permissions. An inline policy is simpler when the role is a one-off. Avoid broad managed policies such as AdministratorAccess unless the role is truly meant to be an administrator role.
Creating the role and attaching permissions
The trust policy is required to create a role. Save the trust policy from the next chapter as trust.json, then create the role and attach the managed policy:
aws iam create-role \
--role-name ReadOnlyAuditor \
--assume-role-policy-document file://trust.json
aws iam attach-role-policy \
--role-name ReadOnlyAuditor \
--policy-arn arn:aws:iam::aws:policy/ReadOnlyAccessRun these with credentials from account 222222222222, since that account owns the role. Creating a role does not make it usable by anyone yet. A caller still needs the trust relationship and its own permission to call sts:AssumeRole.
Writing a least-privilege policy
ReadOnlyAccess lets the auditor read almost everything in the account. If auditors only need to read one bucket of logs, a custom policy is safer. This one grants read access to the bucket example-audit-logs:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListAuditBucket",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::example-audit-logs"
},
{
"Sid": "ReadAuditObjects",
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example-audit-logs/*"
}
]
}The policy has two statements because S3 actions apply to different resource types. s3:ListBucket acts on the bucket, so its ARN has no path. s3:GetObject acts on objects, so its ARN ends in /*. Putting both actions in one statement with one resource is a common mistake. It produces AccessDenied on either listing or reading.
Save the document as audit-s3-read.json and attach it as an inline policy:
aws iam put-role-policy \
--role-name ReadOnlyAuditor \
--policy-name AuditS3Read \
--policy-document file://audit-s3-read.jsonTo check what is attached to the role, list both kinds of policy:
aws iam list-attached-role-policies --role-name ReadOnlyAuditor
aws iam list-role-policies --role-name ReadOnlyAuditorlist-attached-role-policies shows managed policies. list-role-policies shows the names of inline policies. Check both when you audit a role, because it is easy to miss one.
Scoping with conditions
Conditions let you narrow a role further without writing more statements. A few that suit cross-account roles:
aws:RequestedRegionrestricts actions to specific Regions.aws:PrincipalTagandaws:ResourceTagsupport attribute-based access control. For example, the session may touch only resources tagged with the same team as the caller.aws:MultiFactorAuthPresentcan require MFA for sensitive actions. It is evaluated against the session, so it only works when the original caller authenticated with MFA and the role was assumed with that MFA context.
Here is a statement that limits read access to two Regions:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ec2:Describe*",
"rds:Describe*"
],
"Resource": "*",
"Condition": {
"StringEquals": {
"aws:RequestedRegion": ["us-east-1", "eu-west-1"]
}
}
}
]
}Many Describe* actions do not support resource-level restrictions, so "Resource": "*" is correct here. The condition is what narrows the access. Check the Service Authorization Reference for each service before you rely on a resource ARN or condition key. If an action does not support one, the statement silently stops matching.
Where the permissions apply
The role's permissions policy covers resources the role is allowed to reach. Resources in the role's own account are controlled by the role's identity policies, and by resource-based policies that name the role.
- A resource-based policy in the same account that names the role's ARN as a principal can grant access even if the role's identity policy does not mention the resource. In the same account, either side allowing is enough.
- A resource in a third account needs both sides: the role's permissions policy must allow the action, and the resource's policy in that account must allow the role. Cross-account access always needs permission on both sides.
Keep this in mind when an assumed role gets AccessDenied on a resource you expected it to reach. The permissions policy is only half the picture when the resource lives elsewhere.
Common mistakes
- Attaching the policy to the wrong account. The permissions policy belongs to the role in account
222222222222. Attaching it to the user in111111111111does nothing for the assumed session. - Expecting the caller's permissions to add up. The session has the role's permissions, not the union of the role's and the caller's.
- Mismatched resource ARNs. Bucket-level and object-level actions need different ARNs, as shown above.
- Granting
iam:*orsts:AssumeRoleon*to a role. A broad role that can create or assume other roles can be used to widen access beyond what you intended. - Forgetting that policy changes take effect for new sessions only partly. Policy changes on a role apply to existing sessions as well, within a short propagation delay. To cut off an active session, revoke it from the role's Revoke sessions option, which adds a deny for tokens issued before a given time.
Exercise
A user in account 111111111111 can delete objects in an S3 bucket named example-audit-logs, which is owned by account 111111111111. They assume ReadOnlyAuditor in account 222222222222, which only allows s3:GetObject and s3:ListBucket on a bucket in account 222222222222. Using the assumed-role credentials, can they delete objects in the bucket in their own account? Why, or why not?
Show answer
No. The session acts as the role, and the user's own permissions do not carry over. The role's permissions policy allows only s3:GetObject and s3:ListBucket on a bucket in account 222222222222, and nothing grants delete access on the bucket in account 111111111111. To delete objects, the user must use their original credentials again, or the bucket's own policy in 111111111111 would have to allow the role.
Exercise
A role's inline policy has one statement that allows s3:ListBucket and s3:GetObject on "arn:aws:s3:::example-audit-logs/*". Listing the bucket fails but reading a known object works. What is wrong, and how do you fix it?
Show answer
s3:ListBucket applies to the bucket ARN, "arn:aws:s3:::example-audit-logs", not to the object path. Add a second statement (or add the bucket ARN to the Resource list) that allows s3:ListBucket on the bucket ARN, and keep s3:GetObject on the "/*" ARN.