ServiceAccount access to Secrets

Secret read permissions allow a ServiceAccount and its workloads to access sensitive credentials.

Description

A Pod can read Secret values within the granted scope if its ServiceAccount is bound to a Role or ClusterRole that allows get, list or watch on secrets.

Potential impact

An exposed password or token can grant access to other systems. The update permission also allows an account to change stored credentials.

Remediation

Grant a dedicated account only the operations an app needs to read Secrets through the API. Pods receiving Secrets as volumes or environment variables need no Secret-read grant for that delivery. Check other roles and bindings for wildcard grants when removing unnecessary Secret access.

Examples

Create testsa in assembly-prod first. The examples use different names for both the Role and the RoleBinding.

Before

yaml
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: assembly-prod
  name: testRoleVulnerable
rules:
  - apiGroups: [""]
    resources: ["secrets"]
    verbs: ["get", "watch", "list"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: assembly-prod
  name: testRoleBinding
subjects:
  - kind: ServiceAccount
    name: testsa
    namespace: assembly-prod
roleRef:
  kind: Role
  name: testRoleVulnerable
  apiGroup: rbac.authorization.k8s.io

After

yaml
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: assembly-prod
  name: testRoleWithBindingSafe
rules:
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["get", "list"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: assembly-prod
  name: bindingtestRoleWithBindingSafe
subjects:
  - kind: ServiceAccount
    name: testsa
    namespace: assembly-prod
roleRef:
  kind: Role
  name: testRoleWithBindingSafe
  apiGroup: rbac.authorization.k8s.io

Explanation:

  • Before: testRoleBinding lets testsa get, list and watch Secrets in this namespace.
  • After: The new binding allows only getting and listing Pods. Remove the old testRoleBinding too, then verify testsa can no longer read Secret values.

References