Return a job's existing scan when it reuses a digest again

What does this MR do?

The SBOM scan reuse endpoint inserts a new sbom_vulnerability_scans row on every call. A job that already reused a digest now gets its existing finished scan back instead. This is behind the sbom_scan_reuse_job_scan ops flag.

The existing row still returns 201, because the analyzer only treats 201 as a cache hit.

Only finished scans are returned, so a scan the job uploaded and is still running is never handed back as a cache hit.

Split from !266517 (merged), which adds the rate limit.

Database

New query in find_build_scan. It only reads one job's rows through index_sbom_vulnerability_scans_on_build_id and returns at most one row:

SELECT "sbom_vulnerability_scans".* FROM "sbom_vulnerability_scans"
WHERE "sbom_vulnerability_scans"."build_id" = :build_id
  AND "sbom_vulnerability_scans"."project_id" = :project_id
  AND "sbom_vulnerability_scans"."sbom_digest" = :sbom_digest
  AND "sbom_vulnerability_scans"."sbom_digest" IS NOT NULL
  AND "sbom_vulnerability_scans"."status" = 2
ORDER BY created_at desc LIMIT 1

Runbook

sbom_scan_reuse_job_scan is on by default. Turn it off if dependency scanning jobs get the wrong scan back from the reuse call:

/chatops gitlab run feature set sbom_scan_reuse_job_scan false

This turns it off for the whole instance. The endpoint goes back to a new row on every call.

Relates to

gitlab-org/gitlab#636067

Edited by Ahmad Zaydan

Merge request reports

Loading
Loading