Description
Use separate ServiceAccounts when workloads need different API permissions. Pods sharing an account in one namespace use the same identity and permissions even with distinct Pod-bound tokens. Replicas that need identical access can share an account.
Potential impact
A grant added for one workload applies to every Pod using the account. Consider all those Pods when attributing requests or responding to a token exposure.
Remediation
Set service_account_name for each workload. Check each account’s role bindings and effective permissions. Disable token automounting on Pods that need no API token.
Examples
Each excerpt shows one Pod. Create the account first and use a maintained image for deployment.
Before
resource "kubernetes_pod" "pod" {
metadata {
name = "with-pod-affinity"
}
spec {
container {
name = "with-pod-affinity"
image = "k8s.gcr.io/pause:2.0"
}
service_account_name = "terraform-example"
}
}
resource "kubernetes_service_account" "shared" {
metadata {
name = "terraform-example"
}
}
After
resource "kubernetes_pod" "pod" {
metadata {
name = "with-pod-affinity-2"
}
spec {
container {
name = "with-pod-affinity"
image = "k8s.gcr.io/pause:2.0"
}
service_account_name = "service-name"
}
}
resource "kubernetes_service_account" "dedicated" {
metadata {
name = "service-name"
}
}
Explanation:
- Before: Check whether another workload uses
terraform-example. The name alone does not establish sharing. - After: The new Pod uses
service-name. Grant that account only the permissions the Pod needs.