diff --git a/.github/workflows/cve-edge-post.yml b/.github/workflows/cve-edge-post.yml index 0ce5fc8..d19b313 100644 --- a/.github/workflows/cve-edge-post.yml +++ b/.github/workflows/cve-edge-post.yml @@ -42,6 +42,7 @@ jobs: OUT_DIR: ${{ github.workspace }}/sbom-out TRIVY_CACHE_DIR: ${{ github.workspace }}/sbom-out/cache PARALLEL: '3' + SEVERITY: 'LOW,MEDIUM,HIGH,CRITICAL' steps: - name: Preflight — 도구 확인 run: | @@ -101,37 +102,36 @@ jobs: fi wc -l "$OUT_DIR/images_scan.txt" - # SBOM 산출물은 필요 없으므로 trivy image 로 이미지를 직접 스캔한다(SBOM 생성 단계 생략). - - name: 취약점 스캔 (trivy image 직접 실행) + # sbom.yml 과 동일하게 SBOM(CycloneDX) 생성 후 그 SBOM 을 기준으로 스캔한다. + # (이미지 재pull 없이 오프라인으로 취약점만 재조회 — generate-sbom.sh / scan-sbom.sh 재사용) + - name: SBOM 생성 (CycloneDX) run: | - set -uo pipefail - REPORTS_DIR="$OUT_DIR/trivy-reports" - mkdir -p "$REPORTS_DIR" - [ -s "$OUT_DIR/images_scan.txt" ] || { echo "대상 이미지 없음 — 스캔 생략"; exit 0; } + if [ -s "$OUT_DIR/images_scan.txt" ]; then + bash doc/scripts/generate-sbom.sh "$OUT_DIR/images_scan.txt" "$OUT_DIR" + else + echo "대상 이미지 없음 — SBOM 생성 생략"; mkdir -p "$OUT_DIR/sbom" + fi - echo ">> trivy DB 워밍 중..." - trivy image --download-db-only + - name: 취약점 스캔 (SBOM 입력, 오프라인) + run: | + if ls "$OUT_DIR/sbom/"*.cdx.json >/dev/null 2>&1; then + bash doc/scripts/scan-sbom.sh "$OUT_DIR" + else + echo "SBOM 없음 — 스캔 생략" + fi - xargs -P "${PARALLEL:-3}" -I{} bash -c ' - img="$1"; reports="$2" - safe=$(printf "%s" "$img" | tr "/:@" "___") - echo ">> 스캔: $img" - trivy image --quiet --cache-backend memory --skip-db-update \ - --severity LOW,MEDIUM,HIGH,CRITICAL \ - --format json --timeout 15m --output "$reports/$safe.json" "$img" \ - || echo "::warning::스캔 실패: $img" - ' _ {} "$REPORTS_DIR" < "$OUT_DIR/images_scan.txt" - - # trivy-reports/*.json (이미지별 원본 Trivy 결과)을 python3 로 집계한 뒤, + # trivy-reports/*.json (SBOM 기준 스캔 결과)을 python3 로 집계한 뒤, # images_final.tsv(chart⇥version⇥image) 기준으로 (catalog, version) 별 항목을 만든다. # 하나의 이미지가 여러 chart/version 에서 쓰이면 그 수만큼 결과가 중복 생성된다. + # SBOM 기준 스캔이라 ArtifactName 은 SBOM 파일명이 되므로, sbom-index.tsv + # (chart⇥version⇥image⇥status⇥seconds⇥sbom_file)로 리포트 파일 → 원본 이미지명을 복원한다. - name: CVE JSON 생성 run: | - python3 - "$OUT_DIR/trivy-reports" "$OUT_DIR/images_final.tsv" "$OUT_DIR/cve-summary.json" <<'PY' + python3 - "$OUT_DIR/trivy-reports" "$OUT_DIR/images_final.tsv" "$OUT_DIR/sbom-index.tsv" "$OUT_DIR/cve-summary.json" <<'PY' import sys, os, json, glob, collections from datetime import datetime, timezone - reports_dir, images_final_path, out_path = sys.argv[1], sys.argv[2], sys.argv[3] + reports_dir, images_final_path, sbom_index_path, out_path = sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4] scanned_at = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ") image_catalogs = collections.defaultdict(list) @@ -144,11 +144,24 @@ jobs: if entry not in image_catalogs[parts[2]]: image_catalogs[parts[2]].append(entry) + # 리포트 파일명(sbom_file 의 basename, 확장자 제외) -> 원본 이미지명 + report_stem_to_image = {} + if os.path.exists(sbom_index_path): + with open(sbom_index_path) as f: + for line in f: + parts = line.rstrip("\n").split("\t") + if len(parts) >= 6 and parts[2] and parts[5]: + stem = os.path.basename(parts[5]) + if stem.endswith(".cdx.json"): + stem = stem[: -len(".cdx.json")] + report_stem_to_image[stem] = parts[2] + results = [] for path in sorted(glob.glob(os.path.join(reports_dir, "*.json"))): with open(path) as f: data = json.load(f) - image = data.get("ArtifactName", os.path.basename(path)) + stem = os.path.basename(path)[: -len(".json")] + image = report_stem_to_image.get(stem, data.get("ArtifactName", os.path.basename(path))) counts = {"LOW": 0, "HIGH": 0, "MEDIUM": 0, "CRITICAL": 0} critical_desc = {} for result in data.get("Results") or []: