Sub topic 9
Scoping Cloud Trust to One Namespace and ServiceAccount
This chapter assumes you know how a cluster issues projected ServiceAccount tokens and how the cluster's OIDC issuer lets a cloud provider verify them. Here you will learn how to limit which Pods can use a cloud role.
Why scoping matters
A cloud provider that trusts your cluster's OIDC issuer will, by default, accept any validly signed token from that issuer. If the trust policy only says "trust this issuer", every ServiceAccount in every namespace can assume the role. A compromised Pod in a test namespace could then reach production data.
Scoping closes this gap. You add conditions to the trust policy that match claims inside the token.
The two claims that matter
A projected token is a JWT (JSON Web Token). Two of its claims are the basis for scoping:
| Claim | Meaning | Example value |
|---|---|---|
| `sub` (subject) | Who the token represents | `system:serviceaccount:demo:app-reader` |
| `aud` (audience) | Who the token is meant for | `sts.amazonaws.com` |
The subject always has the form system:serviceaccount:<namespace>:<name>. Matching it exactly limits the role to one ServiceAccount in one namespace. The audience stops a token minted for one purpose from being replayed somewhere else.
A scoped trust policy
This AWS IAM trust policy accepts only the app-reader ServiceAccount in the demo namespace, and only with the sts.amazonaws.com audience.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::111122223333:oidc-provider/oidc.eks.us-east-1.amazonaws.com/id/EXAMPLED539D4633E53DE1B71EXAMPLE"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"oidc.eks.us-east-1.amazonaws.com/id/EXAMPLED539D4633E53DE1B71EXAMPLE:sub": "system:serviceaccount:demo:app-reader",
"oidc.eks.us-east-1.amazonaws.com/id/EXAMPLED539D4633E53DE1B71EXAMPLE:aud": "sts.amazonaws.com"
}
}
}
]
}The Principal identifies the OIDC provider. The Condition block does the scoping. Both conditions must be true for the request to succeed.
Choosing the matching operator
The operator you pick decides how wide the trust is.
StringEqualsonsuballows exactly one ServiceAccount. Prefer this.StringLikewith a wildcard, such assystem:serviceaccount:demo:*, allows every ServiceAccount in thedemonamespace. Use it only when all of them need the same access.- Omitting the
subcondition allows every ServiceAccount in the cluster. Avoid this.
Checking the subject before you write the policy
Confirm the exact subject string before you paste it into a policy. You can test what a ServiceAccount is allowed to do inside the cluster, and you can request a token with the audience you plan to use.
kubectl auth can-i list configmaps -n demo --as=system:serviceaccount:demo:app-reader
kubectl create token app-reader -n demo --audience=sts.amazonaws.comThe --as=system:serviceaccount:demo:app-reader value is the same subject string your trust policy must match. Decode the middle section of the token, which is base64url encoded, to see the sub and aud claims directly.
Common scoping mistakes
- Typos in namespace or name. The request is denied with an access error, and the cloud logs show the subject it received. Compare it with your policy.
- Reusing one role for many workloads. Create one role per workload so a compromise exposes only that role's permissions.
- Trusting the wrong issuer. If you rebuild a cluster, its issuer URL changes. Update the provider and the trust policy.
- Relying on RBAC for cloud limits. Kubernetes RBAC does not restrict cloud permissions. The cloud role's permission policy does that, so keep it least privilege as well.
Exercise
A trust policy for a role has aud set to sts.amazonaws.com and sub set with StringLike to system:serviceaccount:*:app-reader. What is the risk, and how do you fix it?
Show answer
The wildcard in the namespace position lets any ServiceAccount named app-reader in any namespace assume the role. Someone with rights to create a ServiceAccount in another namespace could use that name to gain access. Replace the condition with StringEquals and the exact value system:serviceaccount:demo:app-reader.