基础镜像和 SBOM 验证

如果我们只允许部署特定类型的基础镜像, 则可以在获取这些信息后,将其保存到镜像证明中。

在漏洞扫描和验证中,cosign-vuln 格式的证明已包含基础镜像信息。 但在这里,我们将采用不同的方法,使用 syft 为镜像生成 SBOM。 SBOM 信息也包含基础镜像信息。

在 ACP(Alauda Container Platform)中,可以在 Tekton Pipeline 中使用 trivy 或 syft task 为镜像生成 SBOM。 这里我们使用 syft task 生成 SBOM。

功能概述

此方法使用类似 syft 的工具为镜像生成 SBOM,然后使用 Kyverno 验证 SBOM:

  1. 使用 syft Tekton Task 为镜像生成 SBOM,并将其附加到镜像。
  2. 配置 Kyverno 规则以验证 SBOM。
  3. 使用该镜像创建 Pod,以验证 SBOM。

使用场景

以下场景需要参考本文档中的指导:

  • 在使用 Kyverno 的 Kubernetes 集群中实施基础镜像验证
  • 强制实施安全策略,仅允许部署特定的基础镜像
  • 在 CI/CD Pipeline 中设置自动化 SBOM 生成和验证
  • 确保生产环境中的基础镜像合规性
  • 通过验证容器镜像的基础镜像信息,实施供应链安全控制

前提条件

  • 已安装 Tekton Pipelines、Tekton Chains 和 Kyverno 的 Kubernetes 集群
  • 已启用镜像推送功能的 registry
  • 已安装并配置 kubectl CLI,以访问集群
  • 已安装 cosign CLI 工具
  • 已安装 jq CLI 工具

流程概述

步骤操作描述
1生成签名密钥使用 cosign 创建用于签名制品的密钥对
2设置身份验证配置 registry 凭证以推送镜像
3配置 Tekton Chains设置 Chains 使用 OCI 存储并配置签名
4创建示例 Pipeline创建包含 syft task 的 Pipeline 定义,以生成 SBOM
5运行示例 Pipeline使用正确的配置创建并运行 PipelineRun
6等待签名等待 Chains 为 PipelineRun 完成签名
7获取镜像信息从 PipelineRun 中提取镜像 URI 和摘要
8(可选)获取 SBOM 证明获取并验证 SBOM 证明
9使用 Kyverno 验证创建并应用 Kyverno policy,以验证基础镜像信息
10清理删除测试资源和 policy

分步说明

步骤 1-3:基本设置

这些步骤与快速开始:已签名的 Provenance指南相同。请按照该指南中的说明完成以下操作:

步骤 4:创建示例 Pipeline

这是一个 Pipeline 资源,用于构建镜像并生成 SBOM。

apiVersion: tekton.dev/v1
kind: Pipeline
metadata:
  name: chains-demo-5
spec:
  params:
    - default: |-
        echo "Generate a Containerfile for building an image."

        cat << 'EOF' > Containerfile
        FROM ubuntu:latest
        ENV TIME=1
        EOF

        echo -e "\nContainerfile contents:"
        echo "-------------------"
        cat Containerfile
        echo "-------------------"
        echo -e "\nContainerfile generated successfully!"
      description: A script to generate a Containerfile for building an image.
      name: generate-containerfile
      type: string
    - default: <registry>/test/chains/demo-5:latest
      description: The target image address built
      name: image
      type: string
  results:
    - description: first image artifact output
      name: first_image_ARTIFACT_OUTPUTS
      type: object
      value:
        digest: $(tasks.build-image.results.IMAGE_DIGEST)
        uri: $(tasks.build-image.results.IMAGE_URL)
  tasks:
    - name: generate-containerfile
      params:
        - name: script
          value: $(params.generate-containerfile)
      taskRef:
        params:
          - name: kind
            value: task
          - name: catalog
            value: catalog
          - name: name
            value: run-script
          - name: version
            value: "0.1"
        resolver: hub
      timeout: 30m0s
      workspaces:
        - name: source
          workspace: source
    - name: build-image
      params:
        - name: IMAGES
          value:
            - $(params.image)
        - name: TLS_VERIFY
          value: "false"
        - name: CONTAINERFILE
          value: ./Containerfile
      runAfter:
        - generate-containerfile
      taskRef:
        params:
          - name: kind
            value: task
          - name: catalog
            value: catalog
          - name: name
            value: buildah
          - name: version
            value: "0.9"
        resolver: hub
      timeout: 30m0s
      workspaces:
        - name: source
          workspace: source
        - name: registryconfig
          workspace: registryconfig
    - name: syft-sbom
      params:
        - name: COMMAND
          value: |-
            set -x

            mkdir -p .git

            echo "Generate sbom.json"
            syft scan $(tasks.build-image.results.IMAGE_URL)@$(tasks.build-image.results.IMAGE_DIGEST) -o cyclonedx-json=.git/sbom.json > /dev/null

            echo -e "\n\n"
            cat .git/sbom.json
            echo -e "\n\n"

            echo "Generate and Attestation sbom"
            syft attest $(tasks.build-image.results.IMAGE_URL)@$(tasks.build-image.results.IMAGE_DIGEST) -o cyclonedx-json
      runAfter:
        - build-image
      taskRef:
        params:
          - name: kind
            value: task
          - name: catalog
            value: catalog
          - name: name
            value: syft
          - name: version
            value: "0.1"
        resolver: hub
      timeout: 30m0s
      workspaces:
        - name: source
          workspace: source
        - name: registryconfig
          workspace: registryconfig
        - name: signkey
          workspace: signkey
  workspaces:
    - name: source
      description: The workspace for source code.
    - name: registryconfig
      description: The workspace for distribution registry configuration.
    - name: signkey
      description: The workspace for private keys and passwords used for image signatures.
TIP

本教程通过在 pipeline 中以内联方式生成 Containerfile 和 git-clone task 输出,演示了一个简化的工作流。 在生产环境中,您通常会:

  1. 使用 git-clone task 从存储库获取源代码
  2. 使用源代码中现有的 Containerfile 构建镜像
  3. 此方法可确保正确的版本控制,并保持代码与 pipeline 配置之间的分离
YAML 字段说明
  • 与步骤 4:创建示例 Pipeline中的内容相同,但增加了以下内容:
    • workspaces:
      • signkey:用于镜像签名的私钥和密码的 workspace。
    • tasks:
      • syft-sbom:用于为镜像生成 SBOM 并上传证明的 task。 :::

将 generate-containerfile 默认值中的公共 ubuntu:latest 镜像替换为集群可以拉取的镜像,例如在 air-gapped 环境中使用内部 registry 中的镜像。

保存到名为 chains-demo-5.yaml 的 yaml 文件中,并使用以下命令应用:

$ export NAMESPACE=<default>

# create the pipeline in the namespace
$ kubectl create -n $NAMESPACE -f chains-demo-5.yaml

pipeline.tekton.dev/chains-demo-5 created

步骤 5:运行示例 Pipeline

这是一个 PipelineRun 资源,用于运行 pipeline。

apiVersion: tekton.dev/v1
kind: PipelineRun
metadata:
  generateName: chains-demo-5-
spec:
  pipelineRef:
    name: chains-demo-5
  taskRunTemplate:
    serviceAccountName: <default>
  workspaces:
    - name: registryconfig
      secret:
        secretName: <registry-credentials>
    - name: source
      volumeClaimTemplate:
        spec:
          accessModes:
            - ReadWriteOnce
          resources:
            requests:
              storage: 1Gi
          storageClassName: <nfs>

:::details {title="YAML 字段说明"}

  • 与步骤 5:运行示例 Pipeline中的内容相同。以下仅介绍差异。
  • workspaces
    • signkey:签名密钥的 secret 名称。
      • secret.secretName:在上一步获取签名密钥中准备的签名 secret。但您需要在与 PipelineRun 相同的命名空间中创建一个新的 secret。 :::

保存到名为 chains-demo-5.pipelinerun.yaml 的 yaml 文件中,并使用以下命令应用:

$ export NAMESPACE=<default>

# create the pipeline run in the namespace
$ kubectl create -n $NAMESPACE -f chains-demo-5.pipelinerun.yaml

等待 PipelineRun 完成。

$ kubectl get pipelinerun -n $NAMESPACE -w

chains-demo-5-<xxxxx>     True        Succeeded   2m  2m

步骤 6:等待 PipelineRun 完成签名

等待 PipelineRun 具有 chains.tekton.dev/signed: "true" annotation。

$ export NAMESPACE=<default>
$ export PIPELINERUN_NAME=<chains-demo-5-xxxxx>

$ kubectl get pipelinerun -n $NAMESPACE $PIPELINERUN_NAME -o yaml | grep "chains.tekton.dev/signed"

    chains.tekton.dev/signed: "true"

chains.tekton.dev/signed: "true" annotation 表示 Chains 已处理 PipelineRun。但这并不能证明镜像已签名;如需验证签名,请参阅快速开始中的步骤 8:验证镜像和证明。

步骤 7:从 PipelineRun 获取镜像

# Get the image URI
$ export IMAGE_URI=$(kubectl get pipelinerun -n $NAMESPACE $PIPELINERUN_NAME -o jsonpath='{.status.results[?(@.name=="first_image_ARTIFACT_OUTPUTS")].value.uri}')

# Get the image digest
$ export IMAGE_DIGEST=$(kubectl get pipelinerun -n $NAMESPACE $PIPELINERUN_NAME -o jsonpath='{.status.results[?(@.name=="first_image_ARTIFACT_OUTPUTS")].value.digest}')

# Combine the image URI and digest to form the full image reference
$ export IMAGE=$IMAGE_URI@$IMAGE_DIGEST

# Print the image reference
$ echo $IMAGE

<registry>/test/chains/demo-5:latest@sha256:a6c727554be7f9496e413a789663060cd2e62b3be083954188470a94b66239c7

此镜像将用于验证 SBOM。

步骤 8:(可选)获取 SBOM 证明

如果您对 SBOM 证明的内容感兴趣,可以继续阅读以下内容。

有关 cyclonedx SBOM 证明的更多详细信息,请参阅cyclonedx SBOM 证明

根据获取签名公钥部分获取签名公钥。

# Disable tlog upload and enable private infrastructure
$ export COSIGN_TLOG_UPLOAD=false
$ export COSIGN_PRIVATE_INFRASTRUCTURE=true

$ export IMAGE=<<registry>/test/chains/demo-5:latest@sha256:a6c727554be7f9496e413a789663060cd2e62b3be083954188470a94b66239c7>

$ cosign verify-attestation --key cosign.pub --type cyclonedx $IMAGE | jq -r '.payload | @base64d' | jq -s

输出类似于以下内容,其中包含镜像的组件信息。

:::details {title="cyclonedx SBOM 证明"}

{
  "_type": "https://in-toto.io/Statement/v0.1",
  "predicateType": "https://cyclonedx.org/bom",
  "predicate": {
    "$schema": "http://cyclonedx.org/schema/bom-1.6.schema.json",
    "bomFormat": "CycloneDX",
    "components": [
      {
        "bom-ref": "os:ubuntu@24.04",
        "licenses": [
          {
            "license": {
              "name": "GPL"
            }
          }
        ],
        "description": "Ubuntu 24.04.2 LTS",
        "name": "ubuntu",
        "type": "operating-system",
        "version": "24.04"
      }
    ],
    "metadata": {
      "timestamp": "2025-06-07T09:56:05Z",
      "tools": {
        "components": [
          {
            "author": "anchore",
            "name": "syft",
            "type": "application",
            "version": "1.23.1"
          }
        ]
      }
    }
  }
}

:::details {title="字段说明"}

  • predicateType:predicate 的类型。
  • predicate:
    • components:镜像的组件。
      • bom-ref:组件的 BOM 引用。
      • licenses:组件的许可证。
        • license:组件的许可证。
          • name:许可证的名称。
          • id:许可证的 ID。
      • name:组件的名称。
      • type:组件的类型。
      • version:组件的版本。
    • metadata:镜像的元数据。
      • timestamp:镜像的时间戳。
      • tools:
        • components:工具的组件。
          • author:工具的作者。
          • name:工具的名称。
          • type:工具的类型。
          • version:工具的版本。 :::

步骤 9:验证基础镜像信息

步骤 9.1:创建用于验证基础镜像信息的 Kyverno policy

TIP

此步骤需要集群管理员权限。

有关 Kyverno ClusterPolicy 的更多详细信息,请参阅Kyverno ClusterPolicy

policy 如下:

apiVersion: kyverno.io/v1
kind: ClusterPolicy
metadata:
  name: verify-base-image
spec:
  webhookConfiguration:
    failurePolicy: Fail
    timeoutSeconds: 30
  background: false
  rules:
    - name: check-image
      match:
        any:
          - resources:
              kinds:
                - Pod
              namespaces:
                - policy
      verifyImages:
        - imageReferences:
            - "*"
            # - "<registry>/test/*"
          skipImageReferences:
            - "ghcr.io/trusted/*"
          failureAction: Enforce
          verifyDigest: false
          required: false
          useCache: false
          imageRegistryCredentials:
            allowInsecureRegistry: true
            secrets:
              # The credential needs to exist in the namespace where kyverno is deployed
              - registry-credentials

          attestations:
            - type: https://cyclonedx.org/bom
              attestors:
                - entries:
                    - attestor:
                      keys:
                        publicKeys: |- # <- The public key of the signer
                          -----BEGIN PUBLIC KEY-----
                          MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEFZNGfYwn7+b4uSdEYLKjxWi3xtP3
                          UkR8hQvGrG25r0Ikoq0hI3/tr0m7ecvfM75TKh5jGAlLKSZUJpmCGaTToQ==
                          -----END PUBLIC KEY-----

                        ctlog:
                          ignoreSCT: true

                        rekor:
                          ignoreTlog: true

              conditions:
                - any:
                    - key: "{{ components[?type=='operating-system'] | [?name=='ubuntu' && (version=='22.04' || version=='24.04')] | length(@) }}"
                      operator: GreaterThan
                      value: 0
                      message: "The operating system must be Ubuntu 22.04 or 24.04, not {{ components[?type=='operating-system'].name[] }} {{ components[?type=='operating-system'].version[] }}"

                    - key: "{{ components[?type=='operating-system'] | [?name=='alpine' && (version=='3.18' || version=='3.20')] | length(@) }}"
                      operator: GreaterThan
                      value: 0
                      message: "The operating system must be Alpine 3.18 or 3.20, not {{ components[?type=='operating-system'].name[] }} {{ components[?type=='operating-system'].version[] }}"
                       PkgIDs: {{ scanner.result.Results[].Vulnerabilities[?CVSS.redhat.V3Score > `1.0`].PkgID[] }}.

:::details {title="YAML 字段说明"}

  • 此 policy 与镜像签名验证中的 policy 基本一致
  • spec.rules[0].verifyImages[].attestations[0].conditions
    • type:cyclonedx SBOM 证明类型为 https://cyclonedx.org/bom
    • attestors:与上述内容相同。
    • conditions:要验证的条件。
      • any:必须满足任意一个条件。
        • key: "{{ components[?type=='operating-system'] | [?name=='ubuntu' && (version=='22.04' || version=='24.04')] | length(@) }}":操作系统必须为 Ubuntu 22.04 或 24.04。
        • key: "{{ components[?type=='operating-system'] | [?name=='alpine' && (version=='3.18' || version=='3.20')] | length(@) }}":操作系统必须为 Alpine 3.18 或 3.20。 :::

将 policy 保存到名为 kyverno.verify-base-image.yaml 的 yaml 文件中,并使用以下命令应用:

$ kubectl create -f kyverno.verify-base-image.yaml

clusterpolicy.kyverno.io/verify-base-image created

步骤 9.2:验证 policy

该 policy 应用于命名空间 policy 中的 Pod,该命名空间列在 ClusterPolicy 的 match 下。请在该命名空间中创建 Pod;如果命名空间不存在,请先创建它。如需使用其他命名空间,请将其列在 policy 的 namespaces 下:该 policy 不会检查未列出的命名空间中的 Pod。

使用构建的镜像创建 Pod。

$ export NAMESPACE=policy
$ export IMAGE=<<registry>/test/chains/demo-5:latest@sha256:a6c727554be7f9496e413a789663060cd2e62b3be083954188470a94b66239c7>

$ kubectl run -n $NAMESPACE base-image --image=${IMAGE} -- sleep 3600

如果您的基础镜像为 Ubuntu 22.04 或 24.04,Pod 将创建成功。

修改 ClusterPolicy 中的条件,使其仅允许 Alpine 3.18 或 3.20。

conditions:
  - any:
      - key: "{{ components[?type=='operating-system'] | [?name=='alpine' && (version=='3.18' || version=='3.20')] | length(@) }}"
        operator: GreaterThan
        value: 0
        message: "The operating system must be Alpine 3.18 or 3.20, not {{ components[?type=='operating-system'].name[] }} {{ components[?type=='operating-system'].version[] }}"

然后创建 Pod 以验证 policy。

$ kubectl run -n $NAMESPACE deny-base-image --image=${IMAGE} -- sleep 3600

您将收到类似以下的输出:

Error from server: admission webhook "mutate.kyverno.svc-fail" denied the request:

resource Pod/policy/deny-base-image was blocked due to the following policies

verify-base-image:
  check-image: 'image attestations verification failed, verifiedCount: 0, requiredCount:
    1, error: .attestations[0].attestors[0].entries[0].keys: attestation checks failed
    for <registry>/test/chains/demo-5:latest and predicate https://cyclonedx.org/bom:
    The operating system must be Alpine 3.18 or 3.20, not ["ubuntu"] ["24.04"]'

步骤 10:清理资源

删除在前面步骤中创建的 Pod。

$ export NAMESPACE=policy
$ kubectl delete pod -n $NAMESPACE base-image

删除 policy。

$ kubectl delete clusterpolicy verify-base-image

预期结果

完成本指南后:

  • 您将拥有一个可正常运行的设置,其中使用 Tekton Chains 生成 SBOM,并使用 Kyverno 验证基础镜像
  • 您的容器镜像将在其证明中自动包含 SBOM 信息
  • 只有使用可接受基础镜像的镜像才能部署到指定命名空间中
  • 使用不合规基础镜像的镜像将被 Kyverno policy 自动阻止
  • 您已通过验证容器镜像的基础镜像信息,实现基本的供应链安全控制

本指南为在 CI/CD 流水线中实施供应链安全奠定了基础。在生产环境中,您应:

  1. 配置适当的命名空间隔离和访问控制
  2. 为签名密钥实施安全的密钥管理
  3. 设置针对 policy 违规的监控和告警
  4. 定期轮换签名密钥并更新安全 policy

参考资料