← Policy catalog
</> ValidatingAdmissionPolicy · Apache-2.0

Grants Creation of ServiceAccount Tokens

role-serviceaccount-tokens

severityCritical resourceRoles productKubernetes bundles1

Creating the token subresource mints a working bearer token for any service account in the namespace, 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,
  !(r.apiGroups.exists(ag, ag == "") &&
    r.resources.exists(res, res == "serviceaccounts/token") &&
    r.verbs.exists(v, v in ["create"]))
)

Roles 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

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

Manifest

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: role-serviceaccount-tokens
  annotations:
    kubeapt.io/uuid: "576fac7d-7f86-4406-8698-d62042ad5a36"
    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 namespace, letting the holder act as the most privileged one and reuse that token from outside the cluster."
    security.kubeapt.io/resource: "Roles"
    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:
      - roles
  validations:
  - expression: |
      !has(object.rules) || object.rules.all(r,
        !(r.apiGroups.exists(ag, ag == "") &&
          r.resources.exists(res, res == "serviceaccounts/token") &&
          r.verbs.exists(v, v in ["create"]))
      )
    message: |
      Roles must not allow creating serviceaccount/token subresource.

Save it as role-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 ./role-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 role-serviceaccount-tokens -r ./manifests

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

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