Skip to content

Commit

Permalink
Wrong artifact scanned count
Browse files Browse the repository at this point in the history
 fixes goharbor#19197

Signed-off-by: stonezdj <[email protected]>
  • Loading branch information
stonezdj authored and stonezdj committed Aug 17, 2023
1 parent d98699b commit 3aceb34
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/pkg/securityhub/dao/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,20 @@ where a.digest = s.digest
order by s.critical_cnt desc, s.high_cnt desc, s.medium_cnt desc, s.low_cnt desc
limit 5`

// sql to query the total artifact count, exclude the artifact accessory, and child artifact in image index
// sql to query the total artifact count,
// 1. exclude the artifact accessory,
// 2. exclude child artifact without tag
// 3. include top level artifact in image index
// The totalArtifactCountSQL and scannedArtifactCountSQL should use the same criteria to filter the artifact
totalArtifactCountSQL = `SELECT COUNT(1)
FROM artifact A
FROM artifact a
WHERE NOT EXISTS (select 1 from artifact_accessory acc WHERE acc.artifact_id = a.id)
AND (EXISTS (SELECT 1 FROM tag WHERE tag.artifact_id = a.id)
OR NOT EXISTS (SELECT 1 FROM artifact_reference ref WHERE ref.child_id = a.id))`

// sql to query the scanned artifact count, exclude the artifact accessory, and child artifact in image index,
// and include the image index artifact which at least one child artifact is scanned
// sql to query the scanned artifact count,
// exclude the artifact accessory, and child artifact in image index (without tag),
// include the image index artifact which at least one child artifact is scanned
scannedArtifactCountSQL = `SELECT COUNT(1)
FROM artifact a
WHERE EXISTS (SELECT 1
Expand All @@ -65,13 +70,10 @@ WHERE EXISTS (SELECT 1
AND s.registration_uuid = ?)
-- exclude artifact accessory
AND NOT EXISTS (SELECT 1 FROM artifact_accessory acc WHERE acc.artifact_id = a.id)
-- exclude artifact without tag and part of the image index
AND EXISTS (SELECT 1
FROM tag
WHERE tag.artifact_id = id
OR (NOT EXISTS (SELECT 1 FROM artifact_reference ref WHERE ref.child_id = a.id)))
-- include image index which is scanned
OR EXISTS (SELECT 1
-- not a child without tag
AND NOT EXISTS (SELECT 1 FROM artifact_reference WHERE child_id = a.id AND NOT EXISTS (SELECT 1 FROM tag WHERE artifact_id = a.id))
-- include image index which is scanned
OR EXISTS (SELECT 1
FROM scan_report s,
artifact_reference ref
WHERE s.digest = ref.child_digest
Expand Down

0 comments on commit 3aceb34

Please sign in to comment.