Sub topic 10
Session policies for temporary credentials
This chapter assumes you know how IAM roles, AssumeRole, and temporary credentials work, and that you have read the earlier chapters on identity policies, resource policies, and permission boundaries. Session policies use the same ceiling idea as permission boundaries, but they apply to a single session instead of to an identity.
What a session policy is
A session policy is an inline JSON policy that you pass as a parameter when you create temporary credentials programmatically. It limits what that one session can do. When the session expires, the policy goes with it.
Session policies matter in these cases:
- A service or application assumes a role and hands the resulting credentials to a less trusted component, such as a browser client or a customer-specific worker.
- One role is shared by many callers, and each caller should get only a slice of it.
- You federate users and want each federated user to get different permissions without creating a role for each one.
A session policy never grants permissions by itself. It only narrows what the session could otherwise do. This is the same rule you saw for permission boundaries.
Where you can pass one
You supply a session policy when you call one of these AWS STS operations:
| Operation | Who the session represents | Policy parameters |
|---|---|---|
| `AssumeRole` | An IAM role | `Policy` (inline JSON), `PolicyArns` (managed policies) |
| `AssumeRoleWithSAML` | A role, for a SAML federated user | `Policy`, `PolicyArns` |
| `AssumeRoleWithWebIdentity` | A role, for an OIDC federated user | `Policy`, `PolicyArns` |
| `GetFederationToken` | A federated user created from an IAM user | `Policy`, `PolicyArns` |
You can pass an inline policy, up to 10 managed policy ARNs, or both. The inline policy and the managed policies together have a combined size limit. The plaintext of the inline policy and the managed policy ARNs is compressed into a packed form, and STS returns a PackedPolicySize value that tells you how much of the limit you used. If you exceed it, the call fails. Check the current limit in the AWS documentation, because it can change.
The effective permissions rule
For a role session, the effective permissions are the intersection of:
- The role's identity-based policies, and
- The session policy.
If the role also has a permission boundary, the boundary joins the intersection. All of them must allow the action. An explicit deny in any of them still wins.
Consider a role ReportWriter with this identity policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ReadWriteReports",
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": "arn:aws:s3:::example-reports-bucket/*"
}
]
}A caller assumes the role and passes this session policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example-reports-bucket/*"
}
]
}The session can call s3:GetObject and nothing else. s3:PutObject is allowed by the role but not by the session policy, so it is implicitly denied. Now suppose the session policy also listed s3:DeleteObject. The session still could not delete objects, because the role never allowed it. The session policy cannot add anything.
How a session policy fits the evaluation logic
Session policies are an extra layer in the core evaluation. They do not change the core rule. They add one more place where a missing allow turns into an implicit deny.
The order of checks for a request made with a role session looks like this:
- Collect all policies that apply to the request.
- If any applicable policy has a matching explicit deny, the result is deny.
- Check Organizations guardrails such as SCPs. If they are present and do not allow the action, the result is deny.
- Check resource-based policies. In some same-account cases, a resource policy that names the role's ARN directly can allow the request by itself, even when identity policies and the session policy do not allow it. This is the exception covered next.
- Check the permission boundary, if the role has one. If it does not allow the action, the result is deny.
- Check the session policy, if one was passed. If it does not allow the action, the result is deny.
- Check identity-based policies. If at least one allows the action, the result is allow. Otherwise it is an implicit deny.
The diagram shows the common path. It leaves out resource-based policies so the position of the session policy is easy to see.
The resource-based policy exception
Resource-based policies behave differently depending on what they name as the principal. This matters a great deal for session policies.
- If a resource policy names the role ARN as the principal, that grant is not limited by the session policy in the same account. The role's session can use that allow even if the session policy does not mention the action. In this case, the permission boundary is also bypassed.
- If a resource policy names the assumed-role session ARN (for example
arn:aws:sts::111122223333:assumed-role/ReportWriter/alice), the session policy does limit it. The request must be allowed by the session policy as well.
A resource policy that names the role ARN looks like this:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowReportReaderRoleFromOtherAccount",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::222233334444:role/ReportReader" },
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example-reports-bucket/*"
}
]
}In a cross-account case like this one, the rules are stricter. Both accounts must allow the request. The role's identity policy in the other account must allow the action, and any session policy must allow it too. The same-account shortcut does not apply across accounts.
If you use session policies as a security control, do not assume they restrict access to resources whose policies name the role directly. Review those resource policies, or add an explicit deny.
Session policies and role chaining
When a role session assumes a second role, you have role chaining. A session policy passed to the first role does not carry over automatically. Each AssumeRole call creates a new session whose limits come from the second role's policies and any session policy you pass on that call.
Treat each hop as a separate evaluation. If you want to keep the restrictions, pass the policy again at each step. Also check that the first role is allowed to call sts:AssumeRole on the second one, since the session policy from the first hop applies to that call.
Passing a session policy with the CLI and SDK
This example assumes a role and passes an inline session policy. The --policy value is a JSON string.
aws sts assume-role \
--role-arn arn:aws:iam::111122223333:role/ReportWriter \
--role-session-name alice-read-only \
--duration-seconds 900 \
--policy '{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"s3:GetObject","Resource":"arn:aws:s3:::example-reports-bucket/*"}]}'The same call in Python with boto3:
import json
import boto3
sts = boto3.client("sts")
session_policy = {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example-reports-bucket/*",
}
],
}
response = sts.assume_role(
RoleArn="arn:aws:iam::111122223333:role/ReportWriter",
RoleSessionName="alice-read-only",
DurationSeconds=900,
Policy=json.dumps(session_policy),
)
creds = response["Credentials"]
s3 = boto3.client(
"s3",
aws_access_key_id=creds["AccessKeyId"],
aws_secret_access_key=creds["SecretAccessKey"],
aws_session_token=creds["SessionToken"],
)
print("Packed policy size:", response.get("PackedPolicySize"))A call such as s3.put_object(...) with these credentials fails with AccessDenied, even though the role itself can write objects.
Common mistakes
- Treating the session policy as a grant. If the role cannot do it, the session cannot either. A session policy with
"Action": "*"on"Resource": "*"adds nothing. It just leaves the role's permissions unchanged. - Forgetting that the role must allow
sts:AssumeRole. The trust policy of the target role controls who may assume it. The session policy does not replace the trust policy. - Expecting the policy to stay on credentials you did not create with it. The policy is fixed at creation time. You cannot attach or change it later. To change it, request new credentials.
- Assuming a session policy blocks resource policies that name the role. As described above, it does not in the same account.
- Hitting the size limit. Long inline policies and many managed policy ARNs can make the call fail. Use broad wildcards carefully, or use managed policy ARNs when the size is a problem.
- Leaving it out of troubleshooting. When a session gets
AccessDeniedbut the role's identity policy looks correct, check whether the credentials were created with a session policy. The policy is not visible in the role's attached policies. CloudTrail records theAssumeRoleevent, and the request parameters in that event can show the policy that was passed.
Quick reference
| Question | Answer |
|---|---|
| Does a session policy grant permissions? | No. It only limits. |
| What does it attach to? | One session of temporary credentials. |
| Where do you set it? | A parameter on the STS call that creates the session. |
| How does it combine with the role's policies? | Intersection. |
| Does it bypass an explicit deny? | No. An explicit deny always wins. |
| Does it limit a resource policy that names the role ARN in the same account? | No. |
| Does it limit a resource policy that names the session ARN? | Yes. |
| Does it persist after the session ends? | No. |
Exercise
The role DataLoader has an identity policy that allows s3:GetObject, s3:PutObject, and s3:DeleteObject on arn:aws:s3:::example-data/*. A caller assumes the role and passes a session policy that allows only s3:GetObject and s3:ListBucket on that bucket and its objects. Which of these actions can the session perform on objects in the bucket: GetObject, PutObject, DeleteObject, ListBucket? Assume the bucket has no resource-based policy and the role has no permission boundary.
Show answer
Only s3:GetObject. The effective permissions are the intersection of the role's identity policy and the session policy. s3:GetObject is in both. s3:PutObject and s3:DeleteObject are allowed by the role but not by the session policy, so they are implicitly denied. s3:ListBucket is allowed by the session policy but not by the role's identity policy, so it is also implicitly denied. The session policy cannot add permissions.
Exercise
A bucket policy in the same account allows Principal: arn:aws:iam::111122223333:role/DataLoader to perform s3:PutObject. A session of DataLoader was created with a session policy that allows only s3:GetObject. Can this session put objects? How would your answer change if the bucket policy named arn:aws:sts::111122223333:assumed-role/DataLoader/alice instead?
Show answer
Yes, the session can put objects. In the same account, a resource-based policy that names the role ARN grants access on its own, and the session policy does not limit it. If the bucket policy named the assumed-role session ARN, the session policy would apply. Since it allows only s3:GetObject, the s3:PutObject request would be denied.