|
1 | | -# Alarms when the pinned Semgrep CI container image digest in Semgrep.yml drifts |
2 | | -# from returntocorp/semgrep:latest (DEVA11Y-476 / chain DEVA11Y-485). |
| 1 | +# Alarms when the pinned Semgrep CI container image digest in Semgrep.yml goes STALE |
| 2 | +# (DEVA11Y-476 / chain DEVA11Y-485). |
3 | 3 | # |
4 | 4 | # Dependabot cannot rotate a `container:` image digest in a workflow file |
5 | 5 | # (dependabot-core#5819), so instead of auto-adopting whatever `latest` resolves to |
6 | | -# — undesirable for the C-001 threat model, where a poisoned upstream tag is the |
7 | | -# risk — this job keeps a human in the loop: it fails on drift so someone bumps the |
8 | | -# pin deliberately after reviewing the new digest. Read-only; no write permissions. |
9 | | -name: Semgrep image pin drift |
| 6 | +# — undesirable for the C-001 threat model, where a freshly-poisoned upstream tag is |
| 7 | +# the risk — this job keeps a human in the loop. It does NOT fail merely because |
| 8 | +# `latest` has moved (returntocorp/semgrep is rebuilt ~weekly, so a "differs from |
| 9 | +# latest" alarm would be red most weeks and get muted). Instead it fails only when |
| 10 | +# the *pinned* image is older than MAX_AGE_DAYS, which is actionable ("your pin is N |
| 11 | +# days stale") rather than noise ("upstream pushed yesterday"). Read-only; no write |
| 12 | +# permissions. |
| 13 | +# |
| 14 | +# OPS CAVEAT: GitHub disables scheduled workflows after 60 days of repository |
| 15 | +# inactivity, and a failed scheduled run only notifies whoever last edited the cron |
| 16 | +# (no team routing here). So this alarm is a backstop, NOT a substitute for the |
| 17 | +# committed digest pin in Semgrep.yml — do not rely on it as the sole control. If |
| 18 | +# stronger routing is wanted later, add an issue-creating or Slack step (would need |
| 19 | +# additional permissions, intentionally omitted here to keep this job read-only). |
| 20 | +name: Semgrep image pin freshness |
10 | 21 | on: |
11 | 22 | schedule: |
12 | 23 | - cron: "0 7 * * 1" # Mondays 07:00 UTC |
13 | 24 | workflow_dispatch: |
14 | 25 | permissions: |
15 | 26 | contents: read |
16 | 27 | jobs: |
17 | | - drift: |
18 | | - name: Pinned digest vs returntocorp/semgrep:latest |
| 28 | + freshness: |
| 29 | + name: Pinned Semgrep image age |
19 | 30 | runs-on: ubuntu-latest |
20 | 31 | timeout-minutes: 5 |
21 | 32 | steps: |
22 | 33 | - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
23 | | - - name: Compare pinned digest against returntocorp/semgrep:latest |
| 34 | + - name: Fail if the pinned Semgrep image digest is stale |
24 | 35 | run: | |
| 36 | + # NOTE: GitHub runs this as `bash -e {0}`; `set -uo pipefail` does not clear |
| 37 | + # that injected -e. Every command substitution whose failure must be handled |
| 38 | + # by a guard below (rather than aborting the step with no annotation) is |
| 39 | + # suffixed with `|| true` — otherwise a non-matching grep, a SIGPIPE from |
| 40 | + # `| head`, or a registry error would exit the step before the guard runs, |
| 41 | + # leaving a bare red square with no ::error:: (the trap fixed in PR #37). |
25 | 42 | set -uo pipefail |
| 43 | + MAX_AGE_DAYS=45 |
| 44 | + repo="returntocorp/semgrep" |
| 45 | +
|
26 | 46 | pinned=$(grep -oE 'returntocorp/semgrep@sha256:[0-9a-f]{64}' \ |
27 | | - .github/workflows/Semgrep.yml | head -n1 | cut -d@ -f2) |
28 | | - if [ -z "$pinned" ]; then |
| 47 | + .github/workflows/Semgrep.yml | head -n1 | cut -d@ -f2 || true) |
| 48 | + if [ -z "${pinned:-}" ]; then |
29 | 49 | echo "::error::Could not find a returntocorp/semgrep@sha256 pin in .github/workflows/Semgrep.yml" |
30 | 50 | exit 1 |
31 | 51 | fi |
32 | | - token=$(curl -fsS "https://auth.docker.io/token?service=registry.docker.io&scope=repository:returntocorp/semgrep:pull" | jq -r .token) |
33 | | - latest=$(curl -fsSI -H "Authorization: Bearer $token" \ |
| 52 | +
|
| 53 | + token=$(curl -fsS "https://auth.docker.io/token?service=registry.docker.io&scope=repository:${repo}:pull" \ |
| 54 | + | jq -r .token || true) |
| 55 | + if [ -z "${token:-}" ] || [ "${token}" = "null" ]; then |
| 56 | + echo "::error::Could not obtain a Docker registry auth token for ${repo} (registry auth failure)" |
| 57 | + exit 1 |
| 58 | + fi |
| 59 | +
|
| 60 | + man=$(curl -fsS -H "Authorization: Bearer ${token}" \ |
34 | 61 | -H 'Accept: application/vnd.oci.image.index.v1+json' \ |
35 | 62 | -H 'Accept: application/vnd.docker.distribution.manifest.list.v2+json' \ |
36 | | - "https://registry-1.docker.io/v2/returntocorp/semgrep/manifests/latest" \ |
37 | | - | tr -d '\r' | awk -F': ' 'tolower($1)=="docker-content-digest"{print $2}') |
38 | | - if [ -z "$latest" ]; then |
39 | | - echo "::error::Could not resolve returntocorp/semgrep:latest digest from the registry" |
| 63 | + -H 'Accept: application/vnd.oci.image.manifest.v1+json' \ |
| 64 | + -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' \ |
| 65 | + "https://registry-1.docker.io/v2/${repo}/manifests/${pinned}" || true) |
| 66 | + if [ -z "${man:-}" ]; then |
| 67 | + echo "::error::Could not fetch the manifest for pinned digest ${pinned} (registry unreachable or digest gone)" |
40 | 68 | exit 1 |
41 | 69 | fi |
42 | | - echo "pinned: $pinned" |
43 | | - echo "latest: $latest" |
44 | | - if [ "$pinned" = "$latest" ]; then |
45 | | - echo "Semgrep image pin is current." |
| 70 | +
|
| 71 | + # Multi-arch index: descend into the linux/amd64 child image manifest. |
| 72 | + child=$(printf '%s' "${man}" \ |
| 73 | + | jq -r '(.manifests // [])[] | select(.platform.os=="linux" and .platform.architecture=="amd64") | .digest' \ |
| 74 | + | head -n1 || true) |
| 75 | + if [ -n "${child:-}" ] && [ "${child}" != "null" ]; then |
| 76 | + imgman=$(curl -fsS -H "Authorization: Bearer ${token}" \ |
| 77 | + -H 'Accept: application/vnd.oci.image.manifest.v1+json' \ |
| 78 | + -H 'Accept: application/vnd.docker.distribution.manifest.v2+json' \ |
| 79 | + "https://registry-1.docker.io/v2/${repo}/manifests/${child}" || true) |
46 | 80 | else |
47 | | - echo "::error::Semgrep image pin drifted ($pinned != $latest) — review and bump the digest in .github/workflows/Semgrep.yml" |
| 81 | + imgman="${man}" |
| 82 | + fi |
| 83 | + if [ -z "${imgman:-}" ]; then |
| 84 | + echo "::error::Could not fetch the image manifest for pinned digest ${pinned}" |
| 85 | + exit 1 |
| 86 | + fi |
| 87 | +
|
| 88 | + cfg=$(printf '%s' "${imgman}" | jq -r '.config.digest // empty' || true) |
| 89 | + if [ -z "${cfg:-}" ]; then |
| 90 | + echo "::error::Could not locate the image config descriptor for pinned digest ${pinned}" |
| 91 | + exit 1 |
| 92 | + fi |
| 93 | +
|
| 94 | + created=$(curl -fsS -L -H "Authorization: Bearer ${token}" \ |
| 95 | + "https://registry-1.docker.io/v2/${repo}/blobs/${cfg}" | jq -r '.created // empty' || true) |
| 96 | + if [ -z "${created:-}" ]; then |
| 97 | + echo "::error::Could not read the created timestamp from the image config for ${pinned}" |
| 98 | + exit 1 |
| 99 | + fi |
| 100 | +
|
| 101 | + created_epoch=$(date -u -d "${created}" +%s 2>/dev/null || true) |
| 102 | + if [ -z "${created_epoch:-}" ]; then |
| 103 | + echo "::error::Could not parse the image created timestamp: ${created}" |
| 104 | + exit 1 |
| 105 | + fi |
| 106 | + now_epoch=$(date -u +%s) |
| 107 | + age_days=$(( (now_epoch - created_epoch) / 86400 )) |
| 108 | +
|
| 109 | + echo "pinned digest: ${pinned}" |
| 110 | + echo "image created: ${created} (${age_days} days ago)" |
| 111 | + if [ "${age_days}" -gt "${MAX_AGE_DAYS}" ]; then |
| 112 | + echo "::error::Pinned Semgrep image is ${age_days} days old (> ${MAX_AGE_DAYS}) — re-resolve returntocorp/semgrep:latest and bump the digest in .github/workflows/Semgrep.yml" |
48 | 113 | exit 1 |
49 | 114 | fi |
| 115 | + echo "::notice::Pinned Semgrep image is ${age_days} days old (<= ${MAX_AGE_DAYS}); no action needed." |
0 commit comments