簽署非映像檔構件

DevSecOps 工具鏈提供簽署非映像檔成果的功能,但僅限於 tar 和 zip 等檔案格式。 此處理程序涉及將 Script 與預設簽署 Script 一起使用。 如果您需要簽署 tar 和 zip 檔案以外的構件,您必須提供 Script 的自訂實作。 使用 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. 使用下列程式碼 Snippet 作為指引,將已儲存的構件整合至 Cocoa 執行時期。

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

簽署非映像檔構件

預設 Script 會採用 GPG 用戶端來進行簽署。 在此階段期間,所有構件都會經歷簽署程序。 簽署構件之後,Script 會將構件和簽章儲存在 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[@]}"