对非图像工件进行签名

DevSecOps 工具链支持对非镜像类构建产物进行签名,但仅限于 tar 和 zip 等文件格式。 此过程涉及将脚本与缺省签名脚本结合使用。 如果需要对 tar 和 zip 文件以外的工件进行签名,那么必须提供脚本的定制实现。 使用 skip-sign-artifact-type 文本属性,可跳过指定工件类型(例如:log、report、documentation)的图像签名。

必需的运行时环境必须在映像中可用,以支持签名过程。

准备工作

在开始使用非映像工件签名之前,必须具有以下先决条件。

  1. 要对工件进行签名,必须具有 GPG 专用密钥。 要生成密钥,可以使用 文档。

  2. 必须使用以下阶段配置更新 .pipline-config.yaml 文件。

    sign-artifact:
       image: icr.io/continuous-delivery/pipeline/image-signing:1.0.0@sha256:e9d8e354668ba3d40be2aaee08298d2aa7f0e1c8a1829cca4094ec93830e3e6a
       image_pull_policy: IfNotPresent
       abort_on_failure: false
       dind: true
       script: |
          #!/usr/bin/env bash
          source ${COMMONS_PATH}/sign-artifact/sign-artifacts.sh
    
  3. 默认情况下,sign-artifacts脚本 可以对 list_artifacts 集合中列出的所有工件进行签名。 确保正确存储和集成工件。

    1. 确保在 Cocoa 管道运行时中存储已构建的工件。 这可以通过以下两种方式之一来实现。

      • 使用 save_file 工具来保存工件 tar 文件,这允许在后续阶段中进行检索。

        save_file "${APP_NAME}" "${APP_NAME}-${VERSION}.tgz"
        
      • 将工件存储在指定位置,其工件名称与应用程序名称相匹配。

        location="$(basename $(pwd))"
        
    2. 使用以下代码片段作为指南,将保存的工件集成到 Cocoa 运行时中。

      save_artifact "${APP_NAME}" \
         type=tar \
         "name=${APP_NAME}-${VERSION}.tgz" \
         "digest=${DIGEST}" \
         "location=${location}" \
         "provenance=${APP_NAME}-${VERSION}.tgz@${DIGEST}"
      

对非图像工件进行签名

缺省脚本使用 GPG 客户机进行签名。 在此阶段中,所有工件都将执行签名过程。 对工件进行签名后,脚本会将工件和签名同时存储在 Cocoa 管道运行时中。

签署后步骤

要检索已签名工件及其相应的签名,可以使用以下方法。

signed_artifact=$(mktemp)
if load_file "${artifact}_signed_artifact" >"${signed_artifact}" 2> /dev/null; then
    echo "Loaded the signed artifact ${artifact} from load_file at the ${signed_artifact}"
else
    artifact_location="$(load_artifact "$artifact" "location")"
    cp "${WORKSPACE}/${artifact_location}/${name}" "${signed_artifact}"
    echo "Loaded the artifact at the ${WORKSPACE}/${artifact_location}/${name}"   
fi
artifact_signature=$(mktemp)
if load_file "${artifact}_signature" >"${artifact_signature}" 2> /dev/null; then
    echo "Loaded the signature of ${artifact} from load_file at the ${artifact_signature}"
fi

如果您需要保存已签名的作品,您可以选择自己喜欢的存储方案,也可以发送邮件至 IBM Cloud Object Storage。 以下代码片段展示了如何将签名后的工件和签名上传到 Cloud Object Storage 并更新您的库存库。

 # Upload the artifact and signature to Cloud Object Storage.
        namespace="$(get_env pipeline_namespace)"
        local cos_params=(
            --pipeline-run-id="${PIPELINE_RUN_ID}"
            --namespace="${namespace}"
            --backend cos
        )
        # Upload the artifact
        artifact_cos_url=$(cocoa artifact upload "${signed_artifact}" "${cos_params[@]}")
        # Upload the artifact
        artifact_signature_cos_url=$(cocoa artifact upload "${artifact_signature}" "${cos_params[@]}")

        APP_ARTIFACTS='{ "app": "'${APP_NAME}'", "artifact_url": "'${artifact_cos_url}'", "artifact_signature": "'${artifact_signature_cos_url}'" }'
        cocoa inventory add \
            --artifact="${name}@${digest}" \
            --name="${APP_REPO_NAME}" \
            --app-artifacts="${APP_ARTIFACTS}" \
            --signature="${signature}" \
            --provenance="${name}@${digest}" \
            --sha256="${digest}" \
            --type=$type" \
            "${params[@]}"