← Policy catalog
</> ValidatingAdmissionPolicy · Apache-2.0

References a Long-Lived Token Secret

serviceaccount-token-secrets

severityModerate resourceServiceAccounts productKubernetes bundles1

A long-lived token Secret bound to a ServiceAccount never expires and is tied to no pod lifetime, so a single leaked value grants that identity permanent API access until someone notices and rotates it.

Rejects unless

!has(object.secrets) || size(object.secrets) == 0

Avoid explicit long-lived token Secrets on ServiceAccounts.

Remediation

Empty the ServiceAccount secrets list and delete the associated token Secrets, letting workloads use short-lived projected tokens instead. Field: secrets.

Applies to

  • serviceaccounts · v1 · CREATE, UPDATE

Manifest

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: serviceaccount-token-secrets
  annotations:
    kubeapt.io/uuid: "4274c4b5-6f75-45aa-837b-b0641f9a7660"
    security.kubeapt.io/displayName: "References a Long-Lived Token Secret"
    security.kubeapt.io/description: "A long-lived token Secret bound to a ServiceAccount never expires and is tied to no pod lifetime, so a single leaked value grants that identity permanent API access until someone notices and rotates it."
    security.kubeapt.io/resource: "ServiceAccounts"
    security.kubeapt.io/severity: "Moderate"
    security.kubeapt.io/remediation: "Empty the ServiceAccount secrets list and delete the associated token Secrets, letting workloads use short-lived projected tokens instead. Field: secrets."
    security.kubeapt.io/product: "Kubernetes"
spec:
  failurePolicy: Fail
  matchConstraints:
    resourceRules:
    - apiGroups:
      - ''
      apiVersions:
      - v1
      operations:
      - CREATE
      - UPDATE
      resources:
      - serviceaccounts
  validations:
  - expression: |
      !has(object.secrets) || size(object.secrets) == 0
    message: |
      Avoid explicit long-lived token Secrets on ServiceAccounts.

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

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

$ kubeapt validate --policy-name serviceaccount-token-secrets -A