← Policy catalog
</> ValidatingAdmissionPolicy · Apache-2.0

Runs an Image Tagged latest

initcontainer-image-tag

severityModerate resourceInitContainers productKubernetes bundles1

Untagged and latest references float to whatever was pushed to the registry most recently, so a restart or reschedule can pull unreviewed or poisoned code into the init container without any spec change.

Rejects unless

variables.isWindows || !has(object.spec.initContainers) || object.spec.initContainers.all(ic,
  ic.image.matches(".*@sha256:[0-9A-Fa-f]{64}") ||
  (
    ic.image.lastIndexOf(":") > ic.image.lastIndexOf("/") &&
    !ic.image.lowerAscii().endsWith(":latest")
  )
)

spec.initContainers[*].image must use an immutable digest or a non-latest tag.

Variables

isWindows

has(object.spec.os) && has(object.spec.os.name) && object.spec.os.name.lowerAscii() == "windows"

Remediation

Pin the image to a specific released version tag or to an immutable @sha256 digest instead of latest or a bare repository name. Field: spec.initContainers[*].image.

Applies to

  • pods · v1 · CREATE, UPDATE

Manifest

apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
  name: initcontainer-image-tag
  annotations:
    kubeapt.io/uuid: "a52a6b23-ee6b-41cb-ab79-140ce371ed6d"
    security.kubeapt.io/displayName: "Runs an Image Tagged latest"
    security.kubeapt.io/description: "Untagged and latest references float to whatever was pushed to the registry most recently, so a restart or reschedule can pull unreviewed or poisoned code into the init container without any spec change."
    security.kubeapt.io/resource: "InitContainers"
    security.kubeapt.io/severity: "Moderate"
    security.kubeapt.io/remediation: "Pin the image to a specific released version tag or to an immutable @sha256 digest instead of latest or a bare repository name. Field: spec.initContainers[*].image."
    security.kubeapt.io/product: "Kubernetes"
spec:
  failurePolicy: Fail
  matchConstraints:
    resourceRules:
    - apiGroups:
      - ''
      apiVersions:
      - v1
      operations:
      - CREATE
      - UPDATE
      resources:
      - pods
  variables:
  - name: isWindows
    expression: |
      has(object.spec.os) && has(object.spec.os.name) && object.spec.os.name.lowerAscii() == "windows"
  validations:
  - expression: |
      variables.isWindows || !has(object.spec.initContainers) || object.spec.initContainers.all(ic,
        ic.image.matches(".*@sha256:[0-9A-Fa-f]{64}") ||
        (
          ic.image.lastIndexOf(":") > ic.image.lastIndexOf("/") &&
          !ic.image.lowerAscii().endsWith(":latest")
        )
      )
    message: |
      spec.initContainers[*].image must use an immutable digest or a non-latest tag.

Save it as initcontainer-image-tag.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 ./initcontainer-image-tag.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 initcontainer-image-tag -r ./manifests

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

$ kubeapt validate --policy-name initcontainer-image-tag -A