diff --git a/.github/workflows/ibm-mend.yaml b/.github/workflows/ibm-mend.yaml new file mode 100644 index 0000000000..8083855d14 --- /dev/null +++ b/.github/workflows/ibm-mend.yaml @@ -0,0 +1,273 @@ +# IBM Mend scans, using the Mend CLI path that the DataStax Mend onboarding runbook requires for repos +# outside IBM's GitHub Enterprise space. It runs two jobs. +# * SCA ("mend dep") checks open-source dependencies and fails on Critical or High findings +# * SAST ("mend sast") checks our own source code and fails on any finding +# Results land under Products////Astra DB - data-api in the IBM Mend platform. +# +# This repo is public, so anyone signed in to GitHub can read its run logs and artifacts. Scan output +# therefore stays in runner temp files, the logs show only finding counts, and no report is uploaded. +# The findings themselves live on the Mend platform. +# +# The jobs follow riptano/shared-github-actions mend-sca.yml and mend-sast.yml, which riptano/cloud-billing +# calls. We copy their steps instead of calling them, because GitHub does not let a public repository use +# reusable workflows that live in an internal one. +# +# Needs the same settings as cloud-billing, set on the stargate org or on this repo. +# * variables ASTRA_DB_MEND_EMAIL and ASTRA_DB_MEND_PRODUCTNAME (PSIRT_PRODxxxxxx) +# * optional variables ASTRA_DB_MEND_PROJECTNAME (defaults to "Astra DB") and ASTRA_DB_MEND_URL +# * secret ASTRA_DB_MEND_USER_KEY +name: IBM Mend Scan + +# runs on +# * pushes to "main" and release tags, where SCA also updates the project on the Mend platform +# * pull requests against "main", where SCA only reports +# * manual trigger, where SCA only reports +# SAST updates the Mend platform on every run. +on: + push: + branches: [ "main" ] + tags: [ "v*" ] + + pull_request: + types: [ opened, synchronize, reopened ] + branches: [ "main" ] + + workflow_dispatch: + +# cancel same workflows in progress for pull request branches +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: ${{ github.ref != 'refs/heads/main' }} + +permissions: + contents: read + +# global env vars, available in all jobs and steps +env: + MEND_URL: ${{ vars.ASTRA_DB_MEND_URL || 'https://ibmets.whitesourcesoftware.com' }} + MEND_EMAIL: ${{ vars.ASTRA_DB_MEND_EMAIL }} + MEND_ORGNAME: Products + MEND_PRODUCTNAME: ${{ vars.ASTRA_DB_MEND_PRODUCTNAME }} + MEND_PROJECTNAME: ${{ vars.ASTRA_DB_MEND_PROJECTNAME || 'Astra DB' }} + MEND_REPO: ${{ github.event.repository.name }} + +jobs: + + mend-sca: + name: IBM Mend SCA + # fork and Dependabot pull requests get no Actions secrets, so the scan could not authenticate, + # and their changes are scanned on the push to "main" after merge + if: >- + github.event_name != 'pull_request' || + (github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]') + runs-on: ubuntu-latest + + # max run time 20 minutes + timeout-minutes: 20 + + env: + MAVEN_OPTS: '-Xmx4g' + DS_ARTIFACTORY_USERNAME: ${{ secrets.DS_ARTIFACTORY_USERNAME }} + DS_ARTIFACTORY_PASSWORD: ${{ secrets.DS_ARTIFACTORY_PASSWORD }} + + steps: + - name: Check Mend settings + env: + MEND_USER_KEY: ${{ secrets.ASTRA_DB_MEND_USER_KEY }} + run: | + missing="" + [ -n "$MEND_USER_KEY" ] || missing="$missing secret:ASTRA_DB_MEND_USER_KEY" + [ -n "$MEND_EMAIL" ] || missing="$missing var:ASTRA_DB_MEND_EMAIL" + [ -n "$MEND_PRODUCTNAME" ] || missing="$missing var:ASTRA_DB_MEND_PRODUCTNAME" + if [ -n "$missing" ]; then + echo "::error::Mend settings are not configured:$missing" + exit 1 + fi + + - uses: actions/checkout@v7 + + # the Mend CLI resolves dependencies through Maven, so it needs the JDK and the private repos + - name: Set up JDK 21 + uses: actions/setup-java@v5 + with: + distribution: 'temurin' + java-version: '21' + cache: maven + + - name: Setup Maven + run: | + mkdir -p ~/.m2 + cat < ~/.m2/settings.xml + + + + stargate-central + ${DS_ARTIFACTORY_USERNAME} + ${DS_ARTIFACTORY_PASSWORD} + + + stargate-snapshots + ${DS_ARTIFACTORY_USERNAME} + ${DS_ARTIFACTORY_PASSWORD} + + + artifactory + ${DS_ARTIFACTORY_USERNAME} + ${DS_ARTIFACTORY_PASSWORD} + + + artifactory-snapshots + ${DS_ARTIFACTORY_USERNAME} + ${DS_ARTIFACTORY_PASSWORD} + + + artifactory-releases + ${DS_ARTIFACTORY_USERNAME} + ${DS_ARTIFACTORY_PASSWORD} + + + + EOF + + # the CLI lives outside the workspace so it is never part of the scanned tree + - name: Download Mend CLI + run: | + curl --fail --silent --show-error --retry 3 --location \ + https://downloads.mend.io/cli/linux_amd64/mend -o "$RUNNER_TEMP/mend" + chmod +x "$RUNNER_TEMP/mend" + + # pushes to "main" and release tags update the Mend platform, and every other run only reports. + # the CLI output names every finding, so it stays in a temp file. If the CLI fails, only lines that + # look like connection or credential errors reach the log, and never a findings table row. + - name: Mend dependency scan + env: + MEND_USER_KEY: ${{ secrets.ASTRA_DB_MEND_USER_KEY }} + run: | + update_args=() + if [[ "$GITHUB_EVENT_NAME" == "push" && ( "$GITHUB_REF" == refs/heads/main || "$GITHUB_REF" == refs/tags/* ) ]]; then + update_args=(--update) + fi + output="$RUNNER_TEMP/mend-sca-output.txt" + status=0 + "$RUNNER_TEMP/mend" dep --dir . \ + --scope "$MEND_ORGNAME//$MEND_PRODUCTNAME//$MEND_PROJECTNAME - $MEND_REPO" \ + --non-interactive \ + "${update_args[@]}" > "$output" 2>&1 || status=$? + if [ "$status" -ne 0 ]; then + echo "::error::Mend CLI failed with exit code $status." + grep -i -E 'unauthori[sz]ed|forbidden|denied|invalid|authenticat|connection|timed out' "$output" \ + | grep -v '^|' | head -n 20 || true + exit "$status" + fi + + # the Mend CLI exits 0 on findings, so parse its summary line and fail the job on Critical/High. + # CVE IDs listed in .github/mend-allowlist.txt (one per line, # for comments) are reviewed + # exceptions and do not count. Only the counts reach the log. + - name: Fail on Critical/High findings + if: always() + run: | + output="$RUNNER_TEMP/mend-sca-output.txt" + [ -f "$output" ] || { echo "::warning::Mend scan output not found."; exit 0; } + summary=$(grep 'Detected .* vulnerabilities' "$output" || true) + critical=$(echo "$summary" | sed 's/.*(\([0-9]*\) Critical.*/\1/' | grep -E '^[0-9]+$' || echo 0) + high=$(echo "$summary" | sed 's/.*(\([0-9]* Critical, \)\([0-9]*\) High.*/\2/' | grep -E '^[0-9]+$' || echo 0) + + accepted=0 + if [ -f .github/mend-allowlist.txt ]; then + while read -r cve _rest; do + case "$cve" in ''|\#*) continue ;; esac + n=$(grep -cE "^\| (CRITICAL|HIGH) .*\| ${cve} " "$output" || true) + if [ "${n:-0}" -gt 0 ]; then + echo "::notice::Accepted finding ${cve} (listed in .github/mend-allowlist.txt)" + accepted=$((accepted + n)) + fi + done < .github/mend-allowlist.txt + fi + + blocking=$(( ${critical:-0} + ${high:-0} - accepted )) + echo "Mend SCA: ${critical:-0} Critical, ${high:-0} High (${accepted} accepted)" | tee -a "$GITHUB_STEP_SUMMARY" + if [ "$blocking" -gt 0 ]; then + echo "::error::Mend SCA: ${blocking} blocking Critical/High findings. They stay out of this public log, so see the Mend platform or run mend dep locally." + exit 1 + fi + + mend-sast: + name: IBM Mend SAST + # fork and Dependabot pull requests get no Actions secrets, so the scan could not authenticate, + # and their changes are scanned on the push to "main" after merge + if: >- + github.event_name != 'pull_request' || + (github.event.pull_request.head.repo.full_name == github.repository && github.actor != 'dependabot[bot]') + runs-on: ubuntu-latest + + # max run time 30 minutes + timeout-minutes: 30 + + env: + # base name only, since the Mend CLI appends .json + MEND_REPORT: "mend-sast-results" + + steps: + - name: Check Mend settings + env: + MEND_USER_KEY: ${{ secrets.ASTRA_DB_MEND_USER_KEY }} + run: | + missing="" + [ -n "$MEND_USER_KEY" ] || missing="$missing secret:ASTRA_DB_MEND_USER_KEY" + [ -n "$MEND_EMAIL" ] || missing="$missing var:ASTRA_DB_MEND_EMAIL" + [ -n "$MEND_PRODUCTNAME" ] || missing="$missing var:ASTRA_DB_MEND_PRODUCTNAME" + if [ -n "$missing" ]; then + echo "::error::Mend settings are not configured:$missing" + exit 1 + fi + + - uses: actions/checkout@v7 + + # the CLI lives outside the workspace so it is never part of the scanned tree + - name: Download Mend CLI + run: | + curl --fail --silent --show-error --retry 3 --location \ + https://downloads.mend.io/cli/linux_amd64/mend -o "$RUNNER_TEMP/mend" + chmod +x "$RUNNER_TEMP/mend" + + # every SAST run uploads to the same Mend project, so the latest scan (PR or main) is what it shows. + # the CLI output stays in a temp file, the same way as for SCA. + - name: Mend SAST scan + env: + MEND_USER_KEY: ${{ secrets.ASTRA_DB_MEND_USER_KEY }} + run: | + output="$RUNNER_TEMP/mend-sast-output.txt" + status=0 + "$RUNNER_TEMP/mend" sast --dir . \ + --scope "$MEND_ORGNAME//$MEND_PRODUCTNAME//$MEND_PROJECTNAME - $MEND_REPO" \ + --report --formats json --filename "$MEND_REPORT" > "$output" 2>&1 || status=$? + if [ "$status" -ne 0 ]; then + echo "::error::Mend CLI failed with exit code $status." + grep -i -E 'unauthori[sz]ed|forbidden|denied|invalid|authenticat|connection|timed out' "$output" \ + | grep -v '^|' | head -n 20 || true + exit "$status" + fi + + # unlike SCA, SAST fails on a finding of any severity, and a missing or malformed report also fails. + # the report stays on the runner, and only its total reaches the log. + - name: Fail on any SAST finding + if: always() + run: | + total=$(jq -er ' + if type != "array" or length == 0 then + error("Expected a non-empty array of Mend scan reports") + else + map(.stats.totalVulnerabilities | + if type != "number" then + error("Missing or invalid stats.totalVulnerabilities") + elif . < 0 or . != floor then + error("stats.totalVulnerabilities must be a non-negative integer") + else . end + ) | add + end + ' "$MEND_REPORT.json") + echo "Mend SAST total vulnerabilities: $total" | tee -a "$GITHUB_STEP_SUMMARY" + if [ "$total" -gt 0 ]; then + echo "::error::Mend SAST found $total vulnerabilities. They stay out of this public log, so see the Mend platform." + exit 1 + fi