|
| 1 | +# Alarms when the pinned Semgrep CI container image digest in Semgrep.yml goes STALE |
| 2 | +# (DEVA11Y-476 / chain DEVA11Y-485). |
| 3 | +# |
| 4 | +# Dependabot cannot rotate a `container:` image digest in a workflow file |
| 5 | +# (dependabot-core#5819), so instead of auto-adopting whatever `latest` resolves to |
| 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 |
| 21 | +on: |
| 22 | + schedule: |
| 23 | + - cron: "0 7 * * 1" # Mondays 07:00 UTC |
| 24 | + workflow_dispatch: |
| 25 | +permissions: |
| 26 | + contents: read |
| 27 | +jobs: |
| 28 | + freshness: |
| 29 | + name: Pinned Semgrep image age |
| 30 | + runs-on: ubuntu-latest |
| 31 | + timeout-minutes: 5 |
| 32 | + steps: |
| 33 | + - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 |
| 34 | + - name: Fail if the pinned Semgrep image digest is stale |
| 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). |
| 42 | + set -uo pipefail |
| 43 | + MAX_AGE_DAYS=45 |
| 44 | + repo="returntocorp/semgrep" |
| 45 | +
|
| 46 | + pinned=$(grep -oE 'returntocorp/semgrep@sha256:[0-9a-f]{64}' \ |
| 47 | + .github/workflows/Semgrep.yml | head -n1 | cut -d@ -f2 || true) |
| 48 | + if [ -z "${pinned:-}" ]; then |
| 49 | + echo "::error::Could not find a returntocorp/semgrep@sha256 pin in .github/workflows/Semgrep.yml" |
| 50 | + exit 1 |
| 51 | + fi |
| 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}" \ |
| 61 | + -H 'Accept: application/vnd.oci.image.index.v1+json' \ |
| 62 | + -H 'Accept: application/vnd.docker.distribution.manifest.list.v2+json' \ |
| 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)" |
| 68 | + exit 1 |
| 69 | + fi |
| 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) |
| 80 | + else |
| 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" |
| 113 | + exit 1 |
| 114 | + fi |
| 115 | + echo "::notice::Pinned Semgrep image is ${age_days} days old (<= ${MAX_AGE_DAYS}); no action needed." |
0 commit comments