Sub topic 10
Session Policies for Temporary Credentials
This chapter assumes you know how identity policies and permission boundaries work, and that an explicit deny always beats an allow. Here you will see how a session policy narrows the permissions of one temporary session.
What a session policy is
A session policy is a policy you pass inline when you create a temporary session. You can pass one when you call AssumeRole, AssumeRoleWithSAML, AssumeRoleWithWebIdentity, or GetFederationToken. You can also pass one when you federate a user into the console.
A session policy never grants permissions. It sets a ceiling for that one session. Like a permission boundary, it removes anything outside it. Unlike a boundary, it is not attached to the role. It lives only as long as the session.
Effective permissions in a role session
For a role session, the session policy and the role's identity policy both have to allow an action. The effective permissions are the overlap.
The role's permission boundary, if one exists, also has to allow the action. Three sets must overlap: the identity policy, the boundary, and the session policy.
An explicit deny in any of them still wins.
Worked example
Suppose the role ReportWriter has an identity policy that allows s3:* on example-reports-bucket. A caller assumes the role and passes this session policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ReadOnlyForThisSession",
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example-reports-bucket/*"
}
]
}The session can read objects. It cannot write them, even though the role's identity policy allows s3:PutObject. The session policy does not include that action, so the overlap does not either.
You pass the policy with the --policy option:
aws sts assume-role \
--role-arn arn:aws:iam::111122223333:role/ReportWriter \
--role-session-name read-only-session \
--policy file://session-policy.jsonThe command returns temporary credentials. Requests signed with them are limited by the session policy. The role itself is unchanged, so other sessions of the same role keep full access.
Resource-based policies are the exception
For a role session, a resource-based policy that names the role's ARN as principal can grant access without the session policy allowing it. For example, an S3 bucket policy that allows arn:aws:iam::111122223333:role/ReportWriter still applies to the session.
A resource-based policy that names the session ARN, such as .../role/ReportWriter/read-only-session, is different. In that case the session policy does not limit the grant. Be careful when you rely on a session policy to restrict access to resources that have their own policies.
Federated users
With GetFederationToken, the IAM user's identity policy and the session policy are intersected in the same way. The resulting session never has more permission than the user who requested it.
Common uses and errors
Session policies suit cases where one role serves many callers who need different slices of access. A typical case is a broker service that assumes a role and gives each tenant a session limited to that tenant's prefix.
Common mistakes:
- Expecting the session policy to add permissions. It cannot.
- Passing a policy that is too large. The combined size of the session policies and tags is limited, and AWS returns a packed-size error if you exceed it.
- Forgetting that the session policy is not stored. To see what limited a session, look at the code or the
AssumeRolecall in CloudTrail, not at the role.
Exercise
Exercise
A role's identity policy allows s3:GetObject, s3:PutObject, and s3:DeleteObject on one bucket. Its permission boundary allows s3:GetObject and s3:PutObject. A caller assumes the role with a session policy that allows s3:PutObject and s3:DeleteObject. Which actions can the session perform on that bucket?
Show answer
Only s3:PutObject. It is the one action allowed by all three: the identity policy, the boundary, and the session policy. s3:GetObject is missing from the session policy. s3:DeleteObject is missing from the boundary.