CI/CD 管線

Azure Pipelines

為 Azure DevOps 管線加入 VulnCheck 漏洞把關關卡,並將發現項目發布到 pull request 上。

VulnCheck 透過 VulnCheck CLI 在 Azure Pipelines 中執行,不需要從 marketplace 安裝任何擴充功能。下方的管線會安裝固定版本的 CLI、掃描儲存庫、在發現項目違反政策時讓執行失敗,並將結果發布為管線產出物。

儲存您的權杖

從 API 權杖建立 VulnCheck API 權杖,然後將其加入為密鑰變數:可以加在管線本身(Edit > Variables > New variable,並勾選 Keep this value secret),或加入 Pipelines > Library 下的變數群組,以便在多條管線間共用。

與一般變數不同,Azure Pipelines 不會自動將密鑰變數解密到環境中。每個需要權杖的工作都必須像下方範例一樣,以 env: 區塊明確對應。若遺漏此步驟,即使已設定變數,CLI 仍會以驗證錯誤並以結束代碼 3 結束。

掃描每次執行

trigger:
  - main

pr:
  - main

pool:
  vmImage: ubuntu-latest

variables:
  VC_CLI_VERSION: '1.1.0'
  VC_CVSS_THRESHOLD: '7.0'

steps:
  - bash: |
      set -euo pipefail
      ARCH="$(uname -m)"
      case "$ARCH" in x86_64) ARCH=amd64 ;; aarch64) ARCH=arm64 ;; esac
      curl -sSL "https://github.com/vulncheck-oss/cli/releases/download/v${VC_CLI_VERSION}/vulncheck_${VC_CLI_VERSION}_linux_${ARCH}.tar.gz" | tar -xz -C /tmp
      sudo install -m 0755 "/tmp/vulncheck_${VC_CLI_VERSION}_linux_${ARCH}/bin/vulncheck" /usr/local/bin/vulncheck
      vulncheck version
    displayName: Install the VulnCheck CLI

  - bash: |
      set -euo pipefail
      vulncheck scan . --json > "$(Build.ArtifactStagingDirectory)/scan.json"
      jq -r '"\((.vulnerabilities // []) | length) vulnerabilities found"' "$(Build.ArtifactStagingDirectory)/scan.json"
    displayName: Scan with VulnCheck
    env:
      VC_TOKEN: $(VC_TOKEN)

  - bash: |
      SCAN="$(Build.ArtifactStagingDirectory)/scan.json"
      jq -e --argjson max "$VC_CVSS_THRESHOLD" '
        [ (.vulnerabilities // [])[]
          | select((.cvss_base_score // 0 | tonumber? // 0) >= $max or .in_kev) ] | length == 0
      ' "$SCAN" > /dev/null || {
        echo "##vso[task.logissue type=error]Findings at or above CVSS ${VC_CVSS_THRESHOLD}, or in VulnCheck KEV"
        jq -r --argjson max "$VC_CVSS_THRESHOLD" '
          (.vulnerabilities // [])[]
          | select((.cvss_base_score // 0 | tonumber? // 0) >= $max or .in_kev)
          | "  \(.cve)  \(.name)@\(.version)  CVSS \(.cvss_base_score)  KEV \(.in_kev)  fixed in \(.fixed_versions // "n/a")"
        ' "$SCAN"
        exit 1
      }
    displayName: Gate on findings

  - task: PublishPipelineArtifact@1
    condition: always()
    inputs:
      targetPath: $(Build.ArtifactStagingDirectory)/scan.json
      artifact: vulncheck-scan
    displayName: Publish scan results

值得了解的細節:

  • 把關刻意獨立成一個步驟。 即使發現重大漏洞,vulncheck scan 仍會以 0 結束,因此掃描步驟永遠會成功,而政策判定則會清楚呈現在執行摘要中。請參閱在 CI/CD 中使用 VulnCheck。
  • condition: always() 即使把關讓執行失敗,仍會發布產出物,而這正是您最需要證據的執行。
  • Microsoft 託管的 Ubuntu 映像檔已預先安裝 curl 與 jq。在自架 agent 上,請在主機上安裝一次。
  • 固定 VC_CLI_VERSION。 如此每次執行都會使用已知版本的 CLI 掃描,也能避免 install.sh 為解析最新版本而發出的未經驗證 GitHub API 呼叫。

在 pull request 上留言

Azure Repos 的 pull request 可透過 REST API 接受留言串,並使用建置本身的 OAuth 權杖:

  - bash: |
      if [ "$BUILD_REASON" != "PullRequest" ]; then exit 0; fi
      SCAN="$(Build.ArtifactStagingDirectory)/scan.json"
      BODY=$(jq -r '
        "## VulnCheck scan\n\n" +
        (if ((.vulnerabilities // []) | length) == 0
         then "No vulnerabilities found."
         else "| CVE | Package | CVSS | KEV | Fixed in |\n|---|---|---|---|---|\n" +
              ([ (.vulnerabilities // [])[]
                 | "| \(.cve) | \(.name)@\(.version) | \(.cvss_base_score) | \(if .in_kev then "Yes" else "No" end) | \(.fixed_versions // "n/a") |" ]
               | join("\n"))
         end)' "$SCAN")
      jq -n --arg body "$BODY" '{comments: [{parentCommentId: 0, content: $body, commentType: 1}], status: 1}' > thread.json
      curl -sS --fail-with-body \
        --header "Authorization: Bearer ${SYSTEM_ACCESSTOKEN}" \
        --header "Content-Type: application/json" \
        --data @thread.json \
        "${SYSTEM_COLLECTIONURI}${SYSTEM_TEAMPROJECT}/_apis/git/repositories/${BUILD_REPOSITORY_ID}/pullRequests/${SYSTEM_PULLREQUEST_PULLREQUESTID}/threads?api-version=7.1"
    displayName: Comment on the pull request
    condition: always()
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)
除非您明確對應,否則 System.AccessToken 不會公開給指令碼,上方的 env: 區塊即是在做這件事。其背後的身分(Project Collection Build Service 或專案的建置服務帳戶)也需要在儲存庫上具備 Contribute to pull requests 權限,否則 API 會回傳 403。

改為警告而非失敗

若要呈現發現項目但不阻擋執行,請記錄警告並讓步驟成功:

  - bash: |
      SCAN="$(Build.ArtifactStagingDirectory)/scan.json"
      jq -e --argjson max "$VC_CVSS_THRESHOLD" '
        [ (.vulnerabilities // [])[]
          | select((.cvss_base_score // 0 | tonumber? // 0) >= $max or .in_kev) ] | length == 0
      ' "$SCAN" > /dev/null || echo "##vso[task.logissue type=warning]VulnCheck policy breach — see the vulncheck-scan artifact"
    displayName: Report findings

常見的安排是在 pull request 執行上硬性把關,而在分支建置上只發出警告,如此新發布的 CVE 就不會阻擋無關的工作——可使用 condition: eq(variables['Build.Reason'], 'PullRequest') 區分兩者。

Windows 與自架 agent

在 Windows agent 上,請下載 windows_amd64.zip 版本,並將解壓縮後的 bin 目錄加入 PATH,接著在 PowerShell 中使用相同的 vulncheck scan 與 jq 步驟。自架 agent 需要能以 HTTPS 對外連線至 api.vulncheck.com,以及連線至 github.com 以下載 CLI;若對外連線受限,請在內部鏡像 release 壓縮檔。若是完全隔離網路的 agent,請參閱離線模式。

疑難排解

症狀原因
已設定密鑰變數卻仍以結束代碼 3 結束工作缺少 env: 對應——Azure Pipelines 不會自行將密鑰解密到環境中。
只有 pull request 執行以結束代碼 3 結束密鑰變數不會傳遞給來自 fork 的 pull request 建置。請使用 condition: eq(variables['System.PullRequest.IsFork'], 'False') 將掃描限制在非 fork 的執行,並由分支建置涵蓋合併後的結果。
Pull request threads API 回傳 403建置服務身分在儲存庫上缺少 Contribute to pull requests 權限。
結束代碼 5遭速率限制,通常是平行作業共用同一個權杖。
jq 出現 Cannot iterate over null沒有發現的掃描中不存在 vulnerabilities 鍵。請如上所示使用 (.vulnerabilities // [])[]。
jq 出現 null (null) cannot be parsed as a number有發現項目沒有 cvss_base_score。請如上所示使用 .cvss_base_score // 0 | tonumber? // 0。