← Policy catalog
</> ValidatingAdmissionPolicy · Apache-2.0

Grants Creation of ServiceAccount Tokens

clusterrole-serviceaccount-tokens

severityCritical resourceClusterRoles productKubernetes bundles1

Creating the token subresource mints a working bearer token for any service account in the cluster, letting the holder act as the most privileged one and reuse that token from outside the cluster.

Rejects unless

!has(object.rules) || object.rules.all(r,
  !(has(r.apiGroups) && has(r.resources) && has(r.verbs) &&
    r.apiGroups.exists(ag, ag == "") &&
    r.resources.exists(res, res == "serviceaccounts/token") &&
    r.verbs.exists(v, v in ["create"]))
)

ClusterRoles must not allow creating serviceaccount/token subresource.

Remediation

Remove the create verb from every rule covering the serviceaccounts/token subresource, and let the kubelet project tokens into pods instead. Fields: rules[*].apiGroups, rules[*].resources, rules[*].verbs.

Applies to

  • clusterroles · rbac.authorization.k8s.io/v1 · CREATE, UPDATE

Manifest

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: clusterrole-serviceaccount-tokens
  annotations:
    kubeapt.io/uuid: "93c46b8d-106a-47f8-861b-a968aec95c05"
    security.kubeapt.io/displayName: "Grants Creation of ServiceAccount Tokens"
    security.kubeapt.io/description: "Creating the token subresource mints a working bearer token for any service account in the cluster, letting the holder act as the most privileged one and reuse that token from outside the cluster."
    security.kubeapt.io/resource: "ClusterRoles"
    security.kubeapt.io/severity: "Critical"
    security.kubeapt.io/remediation: "Remove the create verb from every rule covering the serviceaccounts/token subresource, and let the kubelet project tokens into pods instead. Fields: rules[*].apiGroups, rules[*].resources, rules[*].verbs."
    security.kubeapt.io/product: "Kubernetes"
spec:
  failurePolicy: Fail
  matchConstraints:
    resourceRules:
    - apiGroups:
      - rbac.authorization.k8s.io
      apiVersions:
      - v1
      operations:
      - CREATE
      - UPDATE
      resources:
      - clusterroles
  validations:
  - expression: |
      !has(object.rules) || object.rules.all(r,
        !(has(r.apiGroups) && has(r.resources) && has(r.verbs) &&
          r.apiGroups.exists(ag, ag == "") &&
          r.resources.exists(res, res == "serviceaccounts/token") &&
          r.verbs.exists(v, v in ["create"]))
      )
    message: |
      ClusterRoles must not allow creating serviceaccount/token subresource.

Save it as clusterrole-serviceaccount-tokens.yaml — the commands below assume that name.

Validate with kubeapt

Check a workload against this one policy, before anything is installed on a cluster.

$ kubeapt validate -p ./clusterrole-serviceaccount-tokens.yaml -r ./workload.yaml

Or without the file, from the published policy set — -r takes a directory too:

$ kubeapt policies download && kubeapt validate --policy-name clusterrole-serviceaccount-tokens -r ./manifests

Or against every namespace in the cluster your kubeconfig points at:

$ kubeapt validate --policy-name clusterrole-serviceaccount-tokens -A