container-image-tag
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 container without any spec change.
variables.isWindows || object.spec.containers.all(c,
c.image.matches(".*@sha256:[0-9A-Fa-f]{64}") ||
(
c.image.lastIndexOf(":") > c.image.lastIndexOf("/") &&
!c.image.lowerAscii().endsWith(":latest")
)
)
spec.containers[*].image must use an immutable digest or a non-latest tag.
isWindows
has(object.spec.os) && has(object.spec.os.name) && object.spec.os.name.lowerAscii() == "windows"
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.containers[*].image.
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingAdmissionPolicy
metadata:
name: container-image-tag
annotations:
kubeapt.io/uuid: "aad5f040-d206-4a33-ae58-95aa2f5d8578"
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 container without any spec change."
security.kubeapt.io/resource: "Containers"
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.containers[*].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 || object.spec.containers.all(c,
c.image.matches(".*@sha256:[0-9A-Fa-f]{64}") ||
(
c.image.lastIndexOf(":") > c.image.lastIndexOf("/") &&
!c.image.lowerAscii().endsWith(":latest")
)
)
message: |
spec.containers[*].image must use an immutable digest or a non-latest tag.Save it as container-image-tag.yaml — the commands below assume that name.
Check a workload against this one policy, before anything is installed on a cluster.
$ kubeapt validate -p ./container-image-tag.yaml -r ./workload.yamlOr without the file, from the published policy set — -r takes a directory too:
$ kubeapt policies download && kubeapt validate --policy-name container-image-tag -r ./manifestsOr against every namespace in the cluster your kubeconfig points at:
$ kubeapt validate --policy-name container-image-tag -A