Sub topic 5
Hardening ServiceAccount usage
This chapter assumes you know Pods, namespaces, and kubectl. It also assumes you have seen ServiceAccounts, Roles, and RoleBindings, and that you know the difference between authentication and authorization. Hardening limits what a stolen Pod token can do and which Pods receive a token in the first place.
Why hardening matters
Every Pod runs as some ServiceAccount. If an attacker gets code execution in a container, the first thing they look for is a token at /var/run/secrets/kubernetes.io/serviceaccount/token. What they can do with it depends on three choices you control:
- Whether the token is mounted at all.
- Which ServiceAccount the Pod uses.
- What that ServiceAccount is allowed to do.
Disable automounting where it is not needed
By default, Kubernetes mounts a token into every Pod. Most web apps never call the Kubernetes API, so the token is exposure with no benefit. Set automountServiceAccountToken: false on the ServiceAccount.
apiVersion: v1
kind: ServiceAccount
metadata:
name: app-reader
namespace: demo
automountServiceAccountToken: falseA Pod can override this setting. The Pod-level field wins over the ServiceAccount-level field. A Pod that really needs the API can opt in, or it can mount a projected token explicitly with the audience and lifetime it needs.
apiVersion: v1
kind: Pod
metadata:
name: token-demo
namespace: demo
spec:
serviceAccountName: app-reader
containers:
- name: app
image: registry.example.com/app:1.0
volumeMounts:
- name: cloud-token
mountPath: /var/run/secrets/tokens
readOnly: true
volumes:
- name: cloud-token
projected:
sources:
- serviceAccountToken:
path: cloud-token
audience: sts.amazonaws.com
expirationSeconds: 3600This Pod gets one token, for one audience, that expires in an hour. The kubelet refreshes it before it expires.
Avoid the default ServiceAccount
Each namespace has a default ServiceAccount. Pods that do not name a ServiceAccount use it. If someone binds a Role to default, every such Pod in the namespace inherits those permissions, including Pods added later.
Follow these rules:
- Create one ServiceAccount per workload.
- Set
serviceAccountNameexplicitly in every Pod template. - Never bind Roles to
default. - Set
automountServiceAccountToken: falseondefaultas well.
Grant least privilege
Prefer a namespaced Role and RoleBinding over a ClusterRole and ClusterRoleBinding. List exact resources and verbs. Avoid wildcards.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: configmap-reader
namespace: demo
rules:
- apiGroups: [""]
resources: ["configmaps"]
verbs: ["get", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: app-reader-configmaps
namespace: demo
subjects:
- kind: ServiceAccount
name: app-reader
namespace: demo
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
name: configmap-readerThen verify the result, including a negative check.
kubectl auth can-i list configmaps -n demo --as=system:serviceaccount:demo:app-reader
kubectl auth can-i delete pods -n demo --as=system:serviceaccount:demo:app-readerThe first command should print yes. The second should print no. Run negative checks for dangerous actions such as reading Secrets or creating Pods.
Putting the controls together
Quick audit
List which ServiceAccounts have bindings in a namespace and review them:
kubectl get rolebindings,clusterrolebindings -A -o wide | grep -E "ServiceAccount|demo"Look for bindings to default, bindings to cluster-admin, and ClusterRoleBindings that cover a ServiceAccount used by an ordinary app.
Exercise
A Deployment in namespace demo does not set serviceAccountName, and someone earlier bound a Role that can read Secrets to the default ServiceAccount. What two problems does this create, and what two changes fix them?
Show answer
The Pods use default, so they inherit the Secret-reading permission, and they also receive an automounted token that an attacker could use. Fix it by removing the binding from default (and setting automountServiceAccountToken: false on it), then creating a dedicated ServiceAccount with only the permissions the app needs and referencing it in the Pod template with serviceAccountName.