Repository navigation
[CI] Gate SCL publication on dependency audits #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
lucaspimentel
wants to merge
8
commits into
main
Choose a base branch
from
lpimentel/audit-release-gates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c614fb5
Gate SCL publication on dependency scans
lucaspimentel e7e4f43
Simplify dependency audit regression tests
lucaspimentel 5b3bc5e
Streamline release-gate regression tests
lucaspimentel 4fc13e2
Don't persist credentials in release checkout
lucaspimentel e341804
Don't persist credentials in publish checkouts
lucaspimentel bdcc682
Skip gate tests on lockfile-only changes
lucaspimentel 0f46c50
Simplify cargo-audit release gate scripts
lucaspimentel 3338831
Document CI runs of the audit release gate tests
lucaspimentel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| name: Audit Release Gate Tests | ||
|
|
||
| on: | ||
| pull_request: | ||
| paths: | ||
| - '.github/workflows/audit-release-gate-tests.yml' | ||
| - '.github/workflows/publish.yml' | ||
| - '.github/workflows/release-datadog-serverless-compat.yml' | ||
| - '.github/workflows/build-datadog-serverless-compat.yml' | ||
| - 'scripts/cargo-audit-release-gate.sh' | ||
| - 'scripts/cargo-audit-report-guard.sh' | ||
| - 'scripts/tests/audit-release-gate/**' | ||
| workflow_dispatch: | ||
| inputs: | ||
| network_smoke: | ||
| description: 'Also run the opt-in real-scanner network smoke suite' | ||
| required: false | ||
| type: boolean | ||
| default: false | ||
|
|
||
| # Read-only: this check never publishes, signs, or uploads anything. | ||
| permissions: | ||
| contents: read | ||
|
|
||
| jobs: | ||
| offline: | ||
| name: Offline gate tests | ||
| runs-on: ubuntu-24.04 | ||
| steps: | ||
| - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | ||
| - uses: actions/setup-python@0b93645e9fea7318ecaed2b359559ac225c90a2b # v5.3.0 | ||
| with: | ||
| python-version: '3.12' | ||
| - name: Install test-only dependencies | ||
| run: python3 -m pip install --disable-pip-version-check 'PyYAML==6.0.2' | ||
| - name: Run deterministic offline gate tests | ||
| run: | | ||
| python3 -m unittest discover -v -s scripts/tests/audit-release-gate -p 'test_*.py' | ||
|
|
||
| network-smoke: | ||
| name: Opt-in real-scanner smoke | ||
| if: ${{ github.event_name == 'workflow_dispatch' && inputs.network_smoke }} | ||
| runs-on: ubuntu-24.04 | ||
| steps: | ||
| - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 | ||
| - uses: actions-rust-lang/setup-rust-toolchain@9399c7bb15d4c7d47b27263d024f0a4978346ba4 # v1.11.0 | ||
| with: | ||
| cache: false | ||
| - name: Run real scanner download, checksum, refresh, and scan | ||
| run: python3 scripts/tests/audit-release-gate/network_smoke.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| #!/usr/bin/env bash | ||
| # | ||
| # Strict cargo-audit release gate for datadog-serverless-compat publication. | ||
| # | ||
| # Usage: scripts/cargo-audit-release-gate.sh <expected-git-sha> | ||
| # | ||
| # Run this gate inside a protected publication job, immediately before its first | ||
| # publication side effect. It performs downloads and a dependency scan only: | ||
| # it never publishes, signs, uploads, or pushes anything. | ||
| # | ||
| # The gate fails closed unless every one of these holds: | ||
| # * the repository root is the expected commit (usually $GITHUB_SHA); | ||
| # * the working-tree Cargo.lock and .cargo/audit.toml byte-match that commit, | ||
| # so the audited lockfile and advisory policy cannot be swapped out; | ||
| # * a working Rust toolchain is present, because the pinned prebuilt | ||
| # cargo-audit still shells out to `cargo -V`; | ||
| # * the pinned official cargo-audit 0.22.2 (Linux x86_64 musl) archive | ||
| # downloads (bounded by curl's --connect-timeout 15 and --max-time 180) and | ||
| # matches its reviewed SHA-256; | ||
| # * the installed binary reports exactly the expected version; | ||
| # * a fresh RustSec advisory database and a fresh Cargo cache are created in a | ||
| # private temporary directory, so a warm cache can never turn a failed | ||
| # advisory refresh into an offline success; | ||
| # * the scanner runs under coreutils timeout (--kill-after=10s, 5m) and exits | ||
| # zero, the terminal log shows the full Fetching/Loaded/Updating/Scanning | ||
| # sequence with nonzero advisory and dependency counts for the expected | ||
| # lockfile, and the log carries none of the crates.io index diagnostics the | ||
| # pinned binary can otherwise hide behind a zero exit status. See | ||
| # scripts/cargo-audit-report-guard.sh. | ||
| # | ||
| # The archive URL, digest, scanner version, and advisory database URL are fixed | ||
| # reviewed constants. This script deliberately honors no environment variable | ||
| # that could point it at a different archive, digest, or advisory source, and it | ||
| # passes no --no-fetch, --stale, or extra ignore flags. All exceptions come from | ||
| # the repository's reviewed .cargo/audit.toml. | ||
| set -euo pipefail | ||
|
|
||
| readonly SCANNER_VERSION_LINE="cargo-audit-audit 0.22.2" | ||
| readonly ARCHIVE_URL="https://github.com/rustsec/rustsec/releases/download/cargo-audit/v0.22.2/cargo-audit-x86_64-unknown-linux-musl-v0.22.2.tgz" | ||
| readonly ARCHIVE_SHA256="7fb9497f8594b389e5fce5ef9b92db08432996895b2e0c5a0167a69ed445c428" | ||
| readonly ADVISORY_DB_URL="https://github.com/RustSec/advisory-db.git" | ||
|
|
||
| script_dir="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)" | ||
| readonly script_dir | ||
| readonly GUARD_SCRIPT="${script_dir}/cargo-audit-report-guard.sh" | ||
|
|
||
| die() { | ||
| printf 'cargo-audit release gate: ERROR: %s\n' "$*" >&2 | ||
| exit 2 | ||
| } | ||
|
|
||
| fail() { | ||
| printf 'cargo-audit release gate: FAILED: %s\n' "$*" >&2 | ||
| exit 1 | ||
| } | ||
|
|
||
| if [ "$#" -ne 1 ]; then | ||
| die "usage: $0 <expected-git-sha>" | ||
| fi | ||
|
|
||
| expected_sha="$1" | ||
| if ! [[ "$expected_sha" =~ ^([0-9a-f]{40}|[0-9a-f]{64})$ ]]; then | ||
| die "expected commit must be a 40- or 64-character lowercase hex id" | ||
| fi | ||
|
|
||
| [ -x "$GUARD_SCRIPT" ] || fail "report guard is missing or not executable: $GUARD_SCRIPT" | ||
|
|
||
| for tool in git cargo curl tar sha256sum cut timeout; do | ||
| command -v "$tool" >/dev/null 2>&1 || die "required tool not found on PATH: $tool" | ||
| done | ||
|
|
||
| cargo_version="$(cargo -V 2>&1)" || die "cargo -V failed; a working Rust toolchain must be installed before this gate" | ||
|
|
||
| repo_root="$(git rev-parse --show-toplevel 2>/dev/null)" || die "not inside a git work tree" | ||
| cd "$repo_root" | ||
|
|
||
| head_sha="$(git rev-parse HEAD)" || die "cannot resolve HEAD" | ||
|
|
||
| lockfile="$repo_root/Cargo.lock" | ||
| policy="$repo_root/.cargo/audit.toml" | ||
| [ -f "$lockfile" ] || fail "missing Cargo.lock at repository root" | ||
| [ -f "$policy" ] || fail "missing .cargo/audit.toml at repository root" | ||
|
|
||
| if [ "$head_sha" != "$expected_sha" ]; then | ||
| fail "HEAD ($head_sha) does not match expected release commit ($expected_sha)" | ||
| fi | ||
|
|
||
| sha256_of() { | ||
| sha256sum "$@" | cut -d' ' -f1 | ||
| } | ||
|
|
||
| # Bind an audited input to the expected commit, not to the working tree alone, | ||
| # and print its verified SHA-256. <path> is relative to the repository root. | ||
| verified_sha256() { | ||
| local path="$1" committed actual | ||
| committed="$(git show "${expected_sha}:${path}" | sha256_of)" || | ||
| fail "cannot read $path from expected commit $expected_sha" | ||
| actual="$(sha256_of "$path")" | ||
| [ "$committed" = "$actual" ] || fail "working-tree $path does not match the expected commit" | ||
| printf '%s\n' "$actual" | ||
| } | ||
|
|
||
| # Command substitution runs in a subshell, so propagate a failure explicitly. | ||
| lock_hash="$(verified_sha256 Cargo.lock)" || exit "$?" | ||
| policy_hash="$(verified_sha256 .cargo/audit.toml)" || exit "$?" | ||
|
|
||
| work="$(mktemp -d "${TMPDIR:-/tmp}/cargo-audit-release-gate.XXXXXXXX")" || die "cannot create a temporary directory" | ||
| readonly work | ||
| # Single quotes defer expansion to EXIT so the cleanup always targets this run's | ||
| # directory without interpolating it into a fragile command string. | ||
| trap 'rm -rf -- "$work"' EXIT | ||
|
|
||
| archive="$work/cargo-audit.tgz" | ||
| install_dir="$work/install" | ||
| mkdir -p "$install_dir" | ||
|
|
||
| if ! curl --fail --silent --show-error --location --proto '=https' \ | ||
| --connect-timeout 15 --max-time 180 --output "$archive" "$ARCHIVE_URL"; then | ||
| fail "failed to download the pinned cargo-audit archive" | ||
| fi | ||
|
|
||
| if [ "$(sha256_of "$archive")" != "$ARCHIVE_SHA256" ]; then | ||
| fail "pinned cargo-audit archive checksum mismatch" | ||
| fi | ||
|
|
||
| if ! tar -xzf "$archive" -C "$install_dir" --strip-components=1; then | ||
| fail "failed to extract the pinned cargo-audit archive" | ||
| fi | ||
|
|
||
| scanner="$install_dir/cargo-audit" | ||
| [ -x "$scanner" ] || fail "extracted archive does not contain an executable cargo-audit" | ||
|
|
||
| scanner_version="$("$scanner" audit --version 2>&1)" || fail "cannot execute the installed cargo-audit" | ||
| if [ "$scanner_version" != "$SCANNER_VERSION_LINE" ]; then | ||
| fail "unexpected cargo-audit version: '$scanner_version' (expected '$SCANNER_VERSION_LINE')" | ||
| fi | ||
|
|
||
| # Fresh advisory database and fresh Cargo cache on every execution. Nothing from | ||
| # a previous run, job, or retry is reused, so a stale cache cannot authorize a | ||
| # release when the required refresh fails. | ||
| cargo_home="$work/cargo-home" | ||
| mkdir -p "$cargo_home" | ||
| db="$work/rustsec-db" | ||
| scan_log="$work/cargo-audit.log" | ||
|
|
||
| # The scan is bounded by coreutils timeout: 5 minutes of runtime, then a 10 | ||
| # second grace before SIGKILL. A killed scan exits 124 (SIGTERM) or 137 | ||
| # (SIGKILL); both are nonzero and are rejected by the report guard below. | ||
| scan_rc=0 | ||
| CARGO_HOME="$cargo_home" timeout --kill-after=10s 5m "$scanner" audit \ | ||
| --file "$lockfile" \ | ||
| --db "$db" \ | ||
| --url "$ADVISORY_DB_URL" \ | ||
| --format terminal \ | ||
| --color never >"$scan_log" 2>&1 || scan_rc=$? | ||
|
|
||
| db_origin="unavailable" | ||
| db_revision="unavailable" | ||
| if [ -d "$db/.git" ]; then | ||
| db_origin="$(git -C "$db" remote get-url origin 2>/dev/null || printf 'unavailable')" | ||
| db_revision="$(git -C "$db" rev-parse HEAD 2>/dev/null || printf 'unavailable')" | ||
| fi | ||
|
|
||
| printf 'cargo-audit release gate\n' | ||
| printf 'expected-sha: %s\n' "$expected_sha" | ||
| printf 'head-sha: %s\n' "$head_sha" | ||
| printf 'cargo-lock-sha256: %s\n' "$lock_hash" | ||
| printf 'audit-toml-sha256: %s\n' "$policy_hash" | ||
| printf 'cargo-version: %s\n' "$cargo_version" | ||
| printf 'scanner-version: %s\n' "$scanner_version" | ||
| printf 'advisory-db-url: %s\n' "$ADVISORY_DB_URL" | ||
| printf 'advisory-db-origin: %s\n' "$db_origin" | ||
| printf 'advisory-db-revision: %s\n' "$db_revision" | ||
| printf 'scanner-exit: %s\n' "$scan_rc" | ||
|
|
||
| printf '%s\n' '----- begin cargo-audit output -----' | ||
| if ! cat "$scan_log"; then | ||
| fail "cannot read the cargo-audit scan log: $scan_log" | ||
| fi | ||
| printf '%s\n' '----- end cargo-audit output -----' | ||
|
|
||
| guard_rc=0 | ||
| "$GUARD_SCRIPT" "$scan_log" "$scan_rc" "$lockfile" || guard_rc=$? | ||
| case "$guard_rc" in | ||
| 0) ;; | ||
| 1) | ||
| printf 'cargo-audit release gate: result: FAIL (scanner-exit=%s)\n' "$scan_rc" >&2 | ||
| # Preserve the scanner's own status for rejected findings and errors. | ||
| if [ "$scan_rc" -ne 0 ]; then | ||
| exit "$scan_rc" | ||
| fi | ||
| exit 1 | ||
| ;; | ||
| *) fail "report guard returned an unexpected status ($guard_rc); treating as failure" ;; | ||
| esac | ||
|
|
||
| if [ "$db_origin" != "$ADVISORY_DB_URL" ]; then | ||
| fail "unexpected advisory database origin: '$db_origin'" | ||
| fi | ||
|
|
||
| printf 'cargo-audit release gate: result: PASS\n' | ||
| exit 0 |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.