# age-rule.yaml - the repository's verify-images policy with one more validation: the
# signed scan's own metadata.scanFinishedOn must be younger than MAXAGE. It binds to its
# own namespace label so it can be tried next to the original without changing it.
# Render with: sed "s/MAXAGE/24h/g" age-rule.yaml | kubectl apply -f -
apiVersion: v1
kind: Namespace
metadata:
  name: age-lab
  labels:
    lab.example/scan-age: checked
    pod-security.kubernetes.io/enforce: restricted
---
apiVersion: policies.kyverno.io/v1
kind: ImageValidatingPolicy
metadata:
  name: verify-images-age
spec:
  validationActions: [Deny]
  failurePolicy: Fail
  webhookConfiguration:
    # The API server's maximum. One verified admission reads the image's
    # bundles from the registry for the signature and for each attestation,
    # and took 4 to 26 seconds in the e2e runs; past this the request fails
    # closed and the pod's controller creates it again.
    timeoutSeconds: 30
  evaluation:
    background:
      enabled: false
  matchConstraints:
    namespaceSelector:
      matchLabels:
        lab.example/scan-age: "checked"
    resourceRules:
      - apiGroups: [""]
        apiVersions: [v1]
        operations: [CREATE, UPDATE]
        resources: [pods, pods/ephemeralcontainers]
  matchImageReferences:
    - glob: "*"
  images:
    - name: volumes
      expression: >-
        object.spec.?volumes.orValue([]).filter(v, has(v.image)).map(v, v.image.reference)
  validationConfigurations:
    verifyDigest: true
    mutateDigest: false
    required: true
  attestors:
    - name: publisher
      cosign:
        keyless:
          identities:
            - subject: https://github.com/heyvaldemar/kubernetes-supply-chain-admission/.github/workflows/publish.yml@refs/heads/main
              issuer: https://token.actions.githubusercontent.com
        # No ctlog block. The transparency log is still required: with none,
        # Kyverno takes Rekor's public key from Sigstore's TUF root and checks
        # the inclusion proof each bundle carries, offline. A ctlog.url makes
        # it build an online Rekor client too, which cosign does not need to
        # verify a bundle (docs/threat-model.md).
  attestations:
    - name: provenance
      intoto:
        type: https://slsa.dev/provenance/v1
    - name: sbom
      intoto:
        type: https://cyclonedx.org/bom
    - name: scan
      intoto:
        type: https://cosign.sigstore.dev/attestation/vuln/v1
  validations:
    - expression: >-
        (images.containers + images.initContainers + images.ephemeralContainers + images.volumes).map(image, verifyImageSignatures(image, [attestors.publisher])).all(n, n > 0)
      message: every image is signed by this repository's publish workflow on main
    - expression: >-
        (images.containers + images.initContainers + images.ephemeralContainers + images.volumes).map(image, verifyAttestationSignatures(image, attestations.provenance, [attestors.publisher])).all(n, n > 0) &&
        (images.containers + images.initContainers + images.ephemeralContainers + images.volumes).map(image, extractPayload(image, attestations.provenance).predicate.buildDefinition.resolvedDependencies
          .exists(d, d.uri.startsWith('git+https://github.com/heyvaldemar/kubernetes-supply-chain-admission@'))).all(ok, ok)
      message: every image carries signed provenance that says it was built from this repository
    - expression: >-
        (images.containers + images.initContainers + images.ephemeralContainers + images.volumes).map(image, verifyAttestationSignatures(image, attestations.sbom, [attestors.publisher])).all(n, n > 0) &&
        (images.containers + images.initContainers + images.ephemeralContainers + images.volumes).map(image, extractPayload(image, attestations.sbom).predicate.bomFormat == 'CycloneDX').all(ok, ok)
      message: every image carries a signed CycloneDX SBOM
    - expression: >-
        (images.containers + images.initContainers + images.ephemeralContainers + images.volumes).map(image, verifyAttestationSignatures(image, attestations.scan, [attestors.publisher])).all(n, n > 0) &&
        (images.containers + images.initContainers + images.ephemeralContainers + images.volumes).map(image, extractPayload(image, attestations.scan).predicate.scanner.result).all(r,
          has(r.Results) && r.Results.exists(t, t.Class == 'os-pkgs') &&
          r.Results.all(t, !has(t.Vulnerabilities) || t.Vulnerabilities.all(v, v.Severity != 'CRITICAL')))
      message: every image carries a signed scan that read its operating system's packages and found nothing critical
    - expression: >-
        (images.containers + images.initContainers + images.ephemeralContainers + images.volumes).map(image, extractPayload(image, attestations.scan).predicate.metadata.scanFinishedOn).all(t,
          time.now() - timestamp(t) < duration('MAXAGE'))
      message: every image's scan is younger than MAXAGE
