|
| 1 | +# Scheduled CVE audit. Produces JSON + human-readable reports as artifacts. |
| 2 | +# |
| 3 | +# Trigger: GitLab CI Schedule with variable SECURITY_AUDIT=1. |
| 4 | +# Also runs on manual (web) trigger with the same variable. |
| 5 | +# |
| 6 | +# Configure a weekly schedule at: Settings > CI/CD > Schedules. |
| 7 | +# Recommended cadence: weekly (e.g. Monday 03:00 UTC). |
| 8 | + |
| 9 | +.security_rules: &security_rules |
| 10 | + rules: |
| 11 | + - if: $CI_PIPELINE_SOURCE == "schedule" && $SECURITY_AUDIT == "1" |
| 12 | + - if: $CI_PIPELINE_SOURCE == "web" && $SECURITY_AUDIT == "1" |
| 13 | + interruptible: false |
| 14 | + |
| 15 | +# Must list every stage used across all included files — a later `stages:` |
| 16 | +# declaration replaces earlier ones (GitLab merges by overwrite, not append). |
| 17 | +# Keep in sync with engine/.gitlab-ci.yml if stages change there. |
| 18 | +stages: |
| 19 | + - test |
| 20 | + - build-binary |
| 21 | + - build |
| 22 | + - integration-test |
| 23 | + - security |
| 24 | + |
| 25 | +cve-govulncheck: |
| 26 | + <<: *security_rules |
| 27 | + stage: security |
| 28 | + image: |
| 29 | + name: golang:1.26 |
| 30 | + pull_policy: if-not-present |
| 31 | + before_script: |
| 32 | + - go install golang.org/x/vuln/cmd/govulncheck@latest |
| 33 | + script: |
| 34 | + - mkdir -p cve-reports |
| 35 | + - cd engine |
| 36 | + - govulncheck -format json ./... > ../cve-reports/govulncheck.json || true |
| 37 | + - govulncheck ./... > ../cve-reports/govulncheck.txt || true |
| 38 | + - cd .. |
| 39 | + - echo "=== govulncheck summary ===" |
| 40 | + - tail -40 cve-reports/govulncheck.txt || true |
| 41 | + artifacts: |
| 42 | + when: always |
| 43 | + name: "cve-go-$CI_COMMIT_SHORT_SHA" |
| 44 | + paths: |
| 45 | + - cve-reports/govulncheck.json |
| 46 | + - cve-reports/govulncheck.txt |
| 47 | + expire_in: 90 days |
| 48 | + allow_failure: true |
| 49 | + |
| 50 | +cve-pnpm-audit: |
| 51 | + <<: *security_rules |
| 52 | + stage: security |
| 53 | + image: |
| 54 | + name: node:20-alpine |
| 55 | + pull_policy: if-not-present |
| 56 | + before_script: |
| 57 | + - apk add --no-cache git jq |
| 58 | + - npm install -g pnpm@9 |
| 59 | + script: |
| 60 | + - mkdir -p cve-reports |
| 61 | + - cd ui |
| 62 | + - pnpm install --frozen-lockfile --ignore-scripts || pnpm install --ignore-scripts |
| 63 | + - pnpm audit --json > ../cve-reports/pnpm-audit.json || true |
| 64 | + - pnpm audit > ../cve-reports/pnpm-audit.txt || true |
| 65 | + - cd .. |
| 66 | + - echo "=== pnpm audit summary ===" |
| 67 | + - jq '.metadata.vulnerabilities // .vulnerabilities // {}' cve-reports/pnpm-audit.json || true |
| 68 | + artifacts: |
| 69 | + when: always |
| 70 | + name: "cve-ui-$CI_COMMIT_SHORT_SHA" |
| 71 | + paths: |
| 72 | + - cve-reports/pnpm-audit.json |
| 73 | + - cve-reports/pnpm-audit.txt |
| 74 | + expire_in: 90 days |
| 75 | + allow_failure: true |
| 76 | + |
| 77 | +cve-trivy-image: |
| 78 | + <<: *security_rules |
| 79 | + stage: security |
| 80 | + image: |
| 81 | + name: aquasec/trivy:latest |
| 82 | + entrypoint: [""] |
| 83 | + pull_policy: if-not-present |
| 84 | + variables: |
| 85 | + # Latest build from the master branch. Override via schedule variable for ad-hoc scans. |
| 86 | + CVE_SCAN_IMAGE: "registry.gitlab.com/postgres-ai/database-lab/dblab-server:master" |
| 87 | + TRIVY_CACHE_DIR: ".trivycache" |
| 88 | + TRIVY_NO_PROGRESS: "true" |
| 89 | + # Authenticate to GitLab Container Registry via CI job token. |
| 90 | + TRIVY_USERNAME: "$CI_REGISTRY_USER" |
| 91 | + TRIVY_PASSWORD: "$CI_REGISTRY_PASSWORD" |
| 92 | + cache: |
| 93 | + key: trivy-db |
| 94 | + paths: |
| 95 | + - .trivycache/ |
| 96 | + script: |
| 97 | + - mkdir -p cve-reports |
| 98 | + - trivy --version |
| 99 | + - trivy image --download-db-only |
| 100 | + - trivy image --severity CRITICAL,HIGH,MEDIUM --format json -o cve-reports/trivy.json "$CVE_SCAN_IMAGE" || true |
| 101 | + - trivy image --severity CRITICAL,HIGH -o cve-reports/trivy.txt "$CVE_SCAN_IMAGE" || true |
| 102 | + - echo "=== trivy severity counts ===" |
| 103 | + - | |
| 104 | + trivy image --severity CRITICAL,HIGH,MEDIUM,LOW --format json "$CVE_SCAN_IMAGE" 2>/dev/null \ |
| 105 | + | grep -oE '"Severity":"[A-Z]+"' | sort | uniq -c || true |
| 106 | + artifacts: |
| 107 | + when: always |
| 108 | + name: "cve-image-$CI_COMMIT_SHORT_SHA" |
| 109 | + paths: |
| 110 | + - cve-reports/trivy.json |
| 111 | + - cve-reports/trivy.txt |
| 112 | + expire_in: 90 days |
| 113 | + allow_failure: true |
| 114 | + |
| 115 | +cve-summary: |
| 116 | + <<: *security_rules |
| 117 | + stage: security |
| 118 | + needs: |
| 119 | + - job: cve-govulncheck |
| 120 | + artifacts: true |
| 121 | + optional: true |
| 122 | + - job: cve-pnpm-audit |
| 123 | + artifacts: true |
| 124 | + optional: true |
| 125 | + - job: cve-trivy-image |
| 126 | + artifacts: true |
| 127 | + optional: true |
| 128 | + image: |
| 129 | + name: alpine:3.20 |
| 130 | + pull_policy: if-not-present |
| 131 | + before_script: |
| 132 | + - apk add --no-cache jq bash |
| 133 | + script: |
| 134 | + - | |
| 135 | + bash -c ' |
| 136 | + set -u |
| 137 | + mkdir -p cve-reports |
| 138 | + out=cve-reports/SUMMARY.md |
| 139 | + { |
| 140 | + echo "# CVE audit report" |
| 141 | + echo |
| 142 | + echo "- Date: $(date -u +%FT%TZ)" |
| 143 | + echo "- Pipeline: $CI_PIPELINE_URL" |
| 144 | + echo "- Commit: $CI_COMMIT_SHORT_SHA" |
| 145 | + echo |
| 146 | +
|
| 147 | + echo "## Severity counts" |
| 148 | + echo |
| 149 | + echo "### Docker image (trivy)" |
| 150 | + if [ -f cve-reports/trivy.json ]; then |
| 151 | + jq -r "[.Results[]? | .Vulnerabilities[]? | .Severity] | group_by(.) | map(\"- \(.[0]): \(length)\") | .[]" cve-reports/trivy.json || echo "- (no data)" |
| 152 | + else |
| 153 | + echo "- (trivy did not run)" |
| 154 | + fi |
| 155 | + echo |
| 156 | +
|
| 157 | + echo "### Go (govulncheck)" |
| 158 | + if [ -f cve-reports/govulncheck.json ]; then |
| 159 | + called=$(jq -rs "[.[] | .finding? | select(. != null) | select((.trace // []) | any(.function != null)) | .osv] | unique | length" cve-reports/govulncheck.json 2>/dev/null || echo "?") |
| 160 | + imported=$(jq -rs "[.[] | .finding? | select(. != null) | .osv] | unique | length" cve-reports/govulncheck.json 2>/dev/null || echo "?") |
| 161 | + echo "- called (code path reachable): $called unique advisories" |
| 162 | + echo "- imported (including unreachable): $imported unique advisories" |
| 163 | + else |
| 164 | + echo "- (govulncheck did not run)" |
| 165 | + fi |
| 166 | + echo |
| 167 | +
|
| 168 | + echo "### UI (pnpm audit)" |
| 169 | + if [ -f cve-reports/pnpm-audit.json ]; then |
| 170 | + jq -r "(.metadata.vulnerabilities // .vulnerabilities // {}) | to_entries[] | \"- \(.key): \(.value)\"" cve-reports/pnpm-audit.json 2>/dev/null || echo "- (parse failed; see pnpm-audit.txt)" |
| 171 | + else |
| 172 | + echo "- (pnpm audit did not run)" |
| 173 | + fi |
| 174 | + echo |
| 175 | +
|
| 176 | + echo "## Top fixable CRITICAL/HIGH in image" |
| 177 | + echo |
| 178 | + if [ -f cve-reports/trivy.json ]; then |
| 179 | + jq -r "[.Results[]? | .Vulnerabilities[]? | select((.Severity==\"CRITICAL\" or .Severity==\"HIGH\") and (.Status==\"fixed\" or .FixedVersion != null and .FixedVersion != \"\"))] | unique_by(.VulnerabilityID + .PkgName) | map(\"- \(.Severity) \(.VulnerabilityID) — \(.PkgName) \(.InstalledVersion) → \(.FixedVersion)\") | .[]" cve-reports/trivy.json | head -40 |
| 180 | + fi |
| 181 | + echo |
| 182 | +
|
| 183 | + echo "## Unfixed CRITICAL/HIGH in image" |
| 184 | + echo |
| 185 | + if [ -f cve-reports/trivy.json ]; then |
| 186 | + jq -r "[.Results[]? | .Vulnerabilities[]? | select((.Severity==\"CRITICAL\" or .Severity==\"HIGH\") and ((.FixedVersion // \"\") == \"\"))] | unique_by(.VulnerabilityID + .PkgName) | map(\"- \(.Severity) \(.VulnerabilityID) — \(.PkgName) \(.InstalledVersion) (no upstream fix)\") | .[]" cve-reports/trivy.json | head -40 |
| 187 | + fi |
| 188 | + echo |
| 189 | +
|
| 190 | + echo "## Artifacts" |
| 191 | + echo |
| 192 | + echo "- cve-reports/govulncheck.{json,txt}" |
| 193 | + echo "- cve-reports/pnpm-audit.{json,txt}" |
| 194 | + echo "- cve-reports/trivy.{json,txt}" |
| 195 | + } > "$out" |
| 196 | + cat "$out" |
| 197 | + ' |
| 198 | + artifacts: |
| 199 | + when: always |
| 200 | + name: "cve-summary-$CI_COMMIT_SHORT_SHA" |
| 201 | + paths: |
| 202 | + - cve-reports/ |
| 203 | + expire_in: 365 days |
0 commit comments