Sub topic 11
Limiting who can read Secrets
This chapter assumes you know basic kubectl usage, namespaces, and how to read YAML manifests. You should also have created a Secret and mounted it in a Pod, as covered in the earlier chapters.
Why RBAC matters for Secrets
A Secret's base64 encoding hides nothing. Anyone who can read the object can decode it. The main protection is therefore authorization: deciding which users and ServiceAccounts may call the API on Secrets.
Kubernetes uses role-based access control (RBAC) for this. A Role lists allowed verbs on resources in one namespace. A RoleBinding grants that Role to a user, group, or ServiceAccount.
Verbs that expose Secret data
Not every verb is equally risky. Three of them return the data:
getreturns one Secret by name.listreturns every Secret in the namespace, including the values. Grantinglistis as sensitive as grantinggeton everything.watchstreams Secrets as they change, and the stream includes the values.
A fourth path is easy to miss. Anyone who can create a Pod in a namespace can mount any Secret in that namespace and print it. Restricting get on Secrets does not help if people can still create Pods freely.
A narrow Role
The Role below allows reading only one named Secret. The resourceNames field limits the rule to that object.
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: read-db-credentials
namespace: default
rules:
- apiGroups: [""]
resources: ["secrets"]
resourceNames: ["db-credentials"]
verbs: ["get"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: read-db-credentials
namespace: default
subjects:
- kind: ServiceAccount
name: db-client
namespace: default
roleRef:
kind: Role
name: read-db-credentials
apiGroup: rbac.authorization.k8s.ioresourceNames works with get. It does not restrict list or watch usefully, because those requests are not made for a single name. Do not rely on it to narrow list.
Checking what an identity can do
Use kubectl auth can-i to test permissions. The --as flag impersonates another identity.
kubectl auth can-i get secret/db-credentials \
--as=system:serviceaccount:default:db-client -n default
kubectl auth can-i list secrets \
--as=system:serviceaccount:default:db-client -n defaultWith the Role above, the first command prints yes and the second prints no.
Common mistakes
- Using a ClusterRole with a ClusterRoleBinding for Secrets. This grants access in every namespace, including
kube-system. - Granting wildcards such as
resources: ["*"]orverbs: ["*"], which include Secrets. - Letting application Pods use the default ServiceAccount with broad rights. Create a dedicated ServiceAccount for each workload.
- Forgetting that the Pod's own mounted Secret does not need RBAC. The kubelet reads it on the Pod's behalf, so the application does not need
getpermission to use a mounted Secret.
Exercise
Exercise
A ServiceAccount has a Role allowing only list on secrets in the prod namespace. A teammate says this is safe because it cannot get anything. Is the teammate right?
Show answer
No. A list response includes the full contents of every Secret in the namespace, so the ServiceAccount can read all the values. Treat list and watch as equal in sensitivity to get.
Exercise
Which command checks whether the ServiceAccount reporter in namespace prod can read the Secret api-key?
Show answer
kubectl auth can-i get secret/api-key --as=system:serviceaccount:prod:reporter -n prod