snowplow-kyverno-policies

by Snowplow

Helm chartv0.4.0App 0.4.0Verified publisher

Snowplow-authored Kyverno policies for Kubernetes clusters running Kyverno

About this chart

snowplow-kyverno-policies

Snowplow-authored Kyverno policies for Kubernetes clusters, packaged as a single Helm release per cluster. The chart targets Kyverno's CEL-based policy API (policies.kyverno.io/v1: ValidatingPolicy, MutatingPolicy, GeneratingPolicy, ...), not the legacy kyverno.io/v1 ClusterPolicy.

Prerequisites

This chart only ships policies and the RBAC they need. It does not install Kyverno or any CRDs. The cluster must already have:

  • Kyverno installed with its CRDs (crds.install=true), and the background controller enabled (it reconciles GeneratingPolicy).
  • For ackAcmDnsValidation: the ACK ACM and Route53 controllers installed.

With default values the chart renders nothing, so installing it on a cluster without these prerequisites is a safe no-op.

How it is organised

Kyverno policies sit on a spectrum from "static logic" to "pure data", and the chart treats those cases differently rather than forcing one model:

MechanismUse forWhere it lives
Curated catalog (policies.*)Opinionated, logic-heavy policies (lots of CEL). Reviewed and versioned as first-class templates, toggled on/off.templates/<cloud>/<name>.yaml
Data-driven families (customPolicies[])N parameterised policies of a kind, where the difference between instances is pure data. No template change to add another.templates/generic/custom-policies.yaml

Cloud-specific curated policies are gated on both their enabled flag and global.cloud, so enabling an AWS policy on a non-AWS cluster is a no-op.

Values

KeyDefaultDescription
global.cloud""aws / gcp / azure. Gates cloud-specific curated policies.
labels{}Extra labels applied to every rendered object.
globalExemptionLabel""Opt-in. When set (for example snowplow.io/kyverno-exempt), Pods labelled <key>: "true" are exempt from every Pod policy, on top of each policy's own exemptionLabel, including policies in Deny mode.
policies.ackAcmDnsValidation.enabledfalseAWS-only. Bridge ACK ACM Certificate validation to ACK Route53 RecordSet.
policies.ackAcmDnsValidation.hostedZoneAnnotationsnowplow.io/hosted-zone-idCertificate annotation carrying the target hosted zone ID.
policies.ackAcmDnsValidation.ttl60TTL (seconds) for the generated validation RecordSet.
policies.ackAcmDnsValidation.evaluation.admissiontrueGenerate the RecordSet on Certificate admission (CREATE/UPDATE).
policies.ackAcmDnsValidation.evaluation.generateExistingtrueGenerate for Certificates that already exist when the policy is installed.
policies.ackAcmDnsValidation.evaluation.synchronizetrueRe-reconcile the generated RecordSet if it is modified or deleted.
policies.disableServiceAccountTokenAutomount.mutate.enabledfalsePatch Pods on CREATE to spec.automountServiceAccountToken: false.
policies.disableServiceAccountTokenAutomount.mutate.failurePolicyIgnoreFail blocks every Pod CREATE in the matched namespaces while the Kyverno webhook is unavailable.
policies.disableServiceAccountTokenAutomount.mutate.removeTokenVolumefalseAlso remove the kube-api-access-* token volume and its mounts. Without it the token stays mounted. See Removing the token.
policies.disableServiceAccountTokenAutomount.validate.enabledfalseReport or block Pods that do not set it to false.
policies.disableServiceAccountTokenAutomount.validate.validationActions[Audit]Audit (PolicyReport only), Warn (admission warning) or Deny (block).
policies.disableServiceAccountTokenAutomount.validate.failurePolicyIgnoreFail makes admission depend on the Kyverno webhook being up.
policies.disableServiceAccountTokenAutomount.validate.backgroundtrueBackground-scan already-running Pods into PolicyReports.
policies.disableServiceAccountTokenAutomount.validate.checkMountedTokenfalseFail Pods where a container mounts a kube-api-access-* volume, instead of checking the field. Under Deny requires mutate.removeTokenVolume.
policies.disableServiceAccountTokenAutomount.excludedNamespaces[kube-system, kyverno]Namespaces exempt from both modes. Emptying it applies the policy cluster-wide.
policies.disableServiceAccountTokenAutomount.exemptionLabelsnowplow.io/automount-service-account-tokenPods labelled <key>: "true" are exempt from both modes. "" removes the escape hatch.
policies.requireResourceLimits.enabledfalseReport or block Pods whose containers do not set resources.limits for every requiredLimits entry.
policies.requireResourceLimits.requiredLimits[cpu]Resource names each container must limit. Add memory to extend.
policies.podSecurityRestricted.enabledfalseThe Pod Security Standards restricted profile, as 15 pss-* ValidatingPolicies.
policies.requireImageDigest.enabledfalseReport or block Pods with an image not pinned by digest.
policies.restrictImageRegistries.enabledfalseReport or block Pods with an image from outside allowedRegistries.
policies.restrictImageRegistries.allowedRegistries[]Registry hosts, optionally with a repository path. Required when enabled.
policies.requireNamespaceNetworkPolicy.enabledfalseReport or block Pods created in a namespace with no NetworkPolicy. Also renders a ClusterRole.
policies.restrictSecretMounts.enabledfalseReport or block Pods that reference a Secret not in allowedSecrets.
policies.restrictSecretMounts.allowedSecrets[]Exact Secret names Pods may reference. Empty allows none.
policies.<policy>.validationActions[Audit]For each policy above. Audit, Warn or Deny.
policies.<policy>.failurePolicyIgnoreFor each policy above. Fail makes admission depend on the Kyverno webhook being up.
policies.<policy>.backgroundtrueFor each policy above. Background-scan already-running Pods into PolicyReports.
policies.<policy>.excludedNamespaces[kube-system, kyverno]For each policy above. Emptying it applies the policy cluster-wide.
policies.<policy>.exemptionLabelsee Exemption labelsFor each policy above. Pods labelled <key>: "true" are exempt. "" removes the escape hatch.
customPolicies[]Data-driven policy families. See below.

ackAcmDnsValidation

On Certificate CREATE/UPDATE, once the ACK ACM controller has populated status.domainValidations[], this GeneratingPolicy creates the DNS-01 validation CNAME as a Route53 RecordSet in the hosted zone named by the Certificate's hostedZoneAnnotation. The generated RecordSet carries an ownerReference back to the Certificate so it cascade-deletes.

The Certificate must be labelled snowplow.io/ack-managed: "true" and annotated with the hosted zone ID for the policy to act on it.

disableServiceAccountTokenAutomount

Cloud-agnostic. Stops Pods mounting a ServiceAccount token they do not need. The two modes are independent and can be enabled together:

  • mutate renders a MutatingPolicy that patches Pods on CREATE to spec.automountServiceAccountToken: false, and with removeTokenVolume also removes the token itself (see Removing the token). Controller-created Pods are patched at admission regardless of what the Deployment/StatefulSet template says, so no workload chart needs editing.
  • validate renders a ValidatingPolicy that flags (or with validationActions: [Deny], blocks) Pods that do not have it set to false, or with checkMountedToken, Pods that still mount the token, for ongoing compliance.

With both enabled the mutation runs first in the admission chain, so the validation sees the patched Pod and passes.

What this does and does not break

Cloud identity is unaffected. IRSA and Azure Workload Identity inject their own projected token volumes (aws-iam-token, azure-identity-token) through separate webhooks; automountServiceAccountToken only governs the default kube-api-access-* volume, so there is no conflict.

In-cluster Kubernetes API clients are affected. Anything that builds an in-cluster client — Kyverno itself, cluster-autoscaler, Karpenter, the AWS Load Balancer Controller, cert-manager, external-dns, ACK controllers, CoreDNS — loses its credentials and fails to authenticate. This is why the policies are not cluster-wide by default: exempt those workloads by namespace (excludedNamespaces) or per Pod (exemptionLabel) before enabling mutate, and roll out through validate with validationActions: [Audit] first to find them.

Opting a workload out

There are four ways to keep the token for a workload that needs the Kubernetes API. Use the narrowest one that fits. Labels can be set by anyone who can create the Pod; a PolicyException can be restricted to cluster admins.

OptionScopeUse it when
excludedNamespacesEvery Pod in the namespaceThe whole namespace is cluster infrastructure (controllers, operators). Set in this chart's values.
exemptionLabel on the PodOne workload, this policy onlyYou control the workload's Pod template, for example through its chart's podLabels value.
globalExemptionLabel on the PodOne workload, every Pod policy in this chartOff by default. Only if the cluster sets it and the workload must also skip the other policies. Prefer the per-policy label.
PolicyExceptionWorkloads selected by a CEL conditionYou cannot label the Pods, for example a third-party chart without a podLabels value, Helm hook Jobs, or Jobs created by a CronJob.

Label. Put the label on the Pod template, not only on the Deployment, CronJob or other controller. Kyverno checks it both on the Pod and, through autogen, on the controller's spec.template:

# e.g. a chart's podLabels value, or spec.template.metadata.labels
snowplow.io/automount-service-account-token: "true"

The label is the default exemptionLabel. It exempts the Pod from both mutate and validate, and it has no effect on clusters without Kyverno, so it is safe to set unconditionally. If a cluster sets globalExemptionLabel, that label works the same way but exempts the Pod from every Pod policy.

PolicyException. Kyverno must have PolicyExceptions enabled (features.policyExceptions.enabled in the Kyverno chart). Restrict them to a namespace that workload owners cannot write to (features.policyExceptions.namespace), otherwise anyone who can create Pods in a namespace can also exempt them. Reference both policies, so an exempted workload is not rejected once validate moves to Deny, and keep the condition as narrow as possible:

apiVersion: policies.kyverno.io/v1beta1
kind: PolicyException
metadata:
  name: my-namespace-my-release
  namespace: kyverno   # the namespace set in features.policyExceptions.namespace
spec:
  policyRefs:
    - name: disable-automount-service-account-token
      kind: MutatingPolicy
    - name: require-service-account-token-automount-disabled
      kind: ValidatingPolicy
  matchConditions:
    - name: release-workloads
      # Guard every field access with has(). An evaluation error counts as a
      # policy error, so an unguarded expression can block Pod creation.
      expression: >-
        request.namespace == 'my-namespace' &&
        has(object.metadata.labels) &&
        object.metadata.labels['app.kubernetes.io/instance'] == 'my-release'

As with the policies themselves, an exception only affects Pods created after it exists. Recreate the workload's Pods to pick it up.

Removing the token

The ServiceAccount admission plugin runs before any webhook. By the time Kyverno sees a Pod, the plugin has already added the kube-api-access-<random> projected volume and mounted it into every container. Setting automountServiceAccountToken: false at that point changes the field but not the mount, so with the defaults the token is still there.

  • mutate.removeTokenVolume: true also removes that volume, and every volumeMount of it from containers and initContainers. Ephemeral containers cannot be set on Pod CREATE, and one added later through kubectl debug has no token volume left to mount. Only projected volumes named kube-api-access-* are removed. IRSA (aws-iam-token), EKS Pod Identity (eks-pod-identity-token) and Azure Workload Identity (azure-identity-token) tokens are left alone, so cloud credentials keep working.
  • validate.checkMountedToken: true makes the validation check what is actually mounted: it fails a Pod where any container mounts a kube-api-access-* volume, and passes one that mounts none, whatever the field says. That also fixes the ServiceAccount-level case in the caveats below.

Both default to false, so upgrading to 0.4.0 does not change behaviour. Mutating webhooks run before validating ones, so with both on a Pod is validated after its token is removed and passes. Under Deny, checkMountedToken without removeTokenVolume would reject every Pod in scope, so the chart refuses to render that combination.

With both on and Deny, mind the failurePolicy interaction. With the default Ignore on both webhooks, a Kyverno outage skips both and Pods are admitted with their token, so nothing is blocked. If only the mutation call fails (it errors or times out) while the validation call succeeds, the token is still mounted and that Pod is denied; its controller retries. In other words Deny fails closed on a failed mutation but still fails open on an ou

Deploy snowplow-kyverno-policies with OpenTenant

Each tenant gets its own release of this chart, with its own namespace, secrets and domain. The builder below already has snowplow-kyverno-policies on a Hetzner server. Change the host, add apps from the marketplace, then copy the generated files. Providers and prices are samples, and nothing here is sent anywhere.

  1. Pick a serverConnect Hetzner, OVHcloud or AWS. The builder starts on Hetzner, the cheapest sample rate.
  2. Place the chartsnowplow-kyverno-policies is pinned at v0.4.0 in opentenant.yaml. Drag more apps from the marketplace onto a rack.
  3. ApplySave opentenant.yaml and main.tf from the console, then run the commands below.
# opentenant.yaml, main.tf and dns.tf come from the builder below
opentenant validate opentenant.yaml
terraform init && terraform apply