diff --git a/.github/actions/govulncheck/scan.sh b/.github/actions/govulncheck/scan.sh index 295f03d9..448b8aa0 100644 --- a/.github/actions/govulncheck/scan.sh +++ b/.github/actions/govulncheck/scan.sh @@ -6,10 +6,44 @@ set -euo pipefail scanner="$1" module="$2" allow_file="${3-}" output_file="${4-}" script_dir="$(cd "$(dirname "$0")" && pwd)" work="$(mktemp -d)" -trap 'rm -rf "$work"' EXIT +monitor_pid='' +stop_monitor() { + if [[ -n "$monitor_pid" ]]; then + kill "$monitor_pid" 2>/dev/null || true + wait "$monitor_pid" 2>/dev/null || true + monitor_pid='' + fi +} +cleanup() { + stop_monitor + rm -rf "$work" +} +trap cleanup EXIT + +scan_command=("$scanner") +if [[ "$(uname -s)" == Linux ]]; then + # Stream aggregate host pressure while the scanner is alive: a lost runner + # cannot upload an after-the-fact report. Never print processes or environment. + echo 'govulncheck-resource sample interval_seconds=30 max_samples=49' >&2 + if command -v vmstat >/dev/null 2>&1; then + vmstat --wide --one-header 30 49 >&2 & + monitor_pid=$! + else + echo '::warning::vmstat is unavailable; live resource samples cannot be collected.' >&2 + fi + if [[ -x /usr/bin/time ]]; then + scan_command=(/usr/bin/time --quiet -f 'govulncheck-resource usage elapsed_seconds=%e max_rss_kib=%M user_seconds=%U system_seconds=%S' "$scanner") + else + echo '::warning::GNU time is unavailable; scanner peak memory cannot be collected.' >&2 + fi +fi rc=0 -(cd "$module" && "$scanner" -format=json -scan=symbol ./...) >"$work/scan.json" || rc=$? +(cd "$module" && "${scan_command[@]}" -format=json -scan=symbol ./...) >"$work/scan.json" || rc=$? +stop_monitor +if [[ "$(uname -s)" == Linux ]]; then + printf 'govulncheck-resource scanner_exit=%s\n' "$rc" >&2 +fi if [[ -n "$output_file" ]]; then cp "$work/scan.json" "$output_file" fi diff --git a/.github/tests/test-govulncheck-download.sh b/.github/tests/test-govulncheck-download.sh index a38575fe..c7e741a8 100644 --- a/.github/tests/test-govulncheck-download.sh +++ b/.github/tests/test-govulncheck-download.sh @@ -60,7 +60,7 @@ blocks 'a retried scan' '(.runs.steps[] | select(.id == "scan") | .run) = "bash # The load must cover exactly what the scan reads: the same ./... pattern, without tests. script="$root/.github/actions/govulncheck/download.sh" retry="$root/.scripts/retry.sh" -grep -qF '"$scanner" -format=json -scan=symbol ./...)' "$root/.github/actions/govulncheck/scan.sh" || +grep -qF '"${scan_command[@]}" -format=json -scan=symbol ./...)' "$root/.github/actions/govulncheck/scan.sh" || fail 'the scan must load ./... without tests, as the download does' grep -qF 'bash "$retry" go list -deps ./... >/dev/null' "$script" || fail 'the download must load the packages the scan reads, through the retry helper' diff --git a/.github/tests/test-govulncheck-scan.sh b/.github/tests/test-govulncheck-scan.sh index 2ae6a98c..10f8c267 100644 --- a/.github/tests/test-govulncheck-scan.sh +++ b/.github/tests/test-govulncheck-scan.sh @@ -13,6 +13,8 @@ set -euo pipefail [[ "$*" == '-format=json -scan=symbol ./...' ]] || exit 90 [[ "$PWD" == "$EXPECTED_DIR" ]] || exit 91 cat "$SCAN_FIXTURE" +sleep "${SCAN_DELAY:-0}" +if [[ "$SCAN_STATUS" == killed ]]; then kill -KILL "$$"; fi exit "$SCAN_STATUS" SH chmod +x "$work/scanner" @@ -39,6 +41,27 @@ check() { 1) grep -qxF 'verdict=blocking' "$GITHUB_OUTPUT" ;; *) [[ ! -s "$GITHUB_OUTPUT" ]] ;; esac + if [[ "$(uname -s)" == Linux ]]; then + local scanner_status="$SCAN_STATUS" + [[ "$scanner_status" != killed ]] || scanner_status=137 + [[ "$out" == *'govulncheck-resource sample interval_seconds=30 max_samples=49'* ]] || { + echo "FAIL: $label: Linux resource sampling was not announced" >&2 + exit 1 + } + if [[ -x /usr/bin/time ]]; then + [[ "$out" == *'govulncheck-resource usage elapsed_seconds='* && + "$out" == *"govulncheck-resource scanner_exit=$scanner_status"* ]] || { + echo "FAIL: $label: resource timing lost the scanner's exit status" >&2 + exit 1 + } + fi + if [[ "${SCAN_DELAY:-0}" == 1 ]]; then + [[ "$out" == *'procs'*'memory'*'swap'*'io'*'system'*'cpu'* ]] || { + echo "FAIL: $label: native vmstat did not produce a resource sample" >&2 + exit 1 + } + fi + fi echo "PASS: $label" } @@ -52,8 +75,17 @@ printf '%s\n' "$config" "$uncalled" >"$SCAN_FIXTURE" check 'imported but uncalled advisories do not block' 0 'No blocking vulnerabilities' printf '%s\n' "$config" >"$SCAN_FIXTURE" check 'clean scan passes' 0 'No blocking vulnerabilities' +if [[ "$(uname -s)" == Linux ]]; then + export SCAN_DELAY=1 + check 'native resource sampler completes with the scanner' 0 'No blocking vulnerabilities' + unset SCAN_DELAY +fi export SCAN_STATUS=2 check 'scanner failure is never interpreted as clean' 2 'operational error' "$work/allow.txt" +export SCAN_STATUS=137 +check 'scanner termination preserves its status and has no verdict' 137 'operational error' "$work/allow.txt" +export SCAN_STATUS=killed +check 'a killed scanner cannot publish a clean verdict' 137 'operational error' "$work/allow.txt" export SCAN_STATUS=0 : >"$SCAN_FIXTURE" check 'empty output fails closed' 2 'invalid scan output'