Skip to content

Commit

Permalink
Set default capability for old scanners (goharbor#20306)
Browse files Browse the repository at this point in the history
Signed-off-by: stonezdj <[email protected]>
Co-authored-by: Wang Yan <[email protected]>
  • Loading branch information
stonezdj and wy65701436 authored Apr 20, 2024
1 parent 0d9dc4b commit d759429
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/pkg/scan/rest/v1/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,22 @@ func (md *ScannerAdapterMetadata) GetCapability(mimeType string) *ScannerCapabil
// ConvertCapability converts the capability to map, used in get scanner API
func (md *ScannerAdapterMetadata) ConvertCapability() map[string]interface{} {
capabilities := make(map[string]interface{})
oldScanner := true
for _, c := range md.Capabilities {
if len(c.Type) > 0 {
oldScanner = false
}
if c.Type == ScanTypeVulnerability {
capabilities[supportVulnerability] = true
}
if c.Type == ScanTypeSbom {
} else if c.Type == ScanTypeSbom {
capabilities[supportSBOM] = true
}
}
if oldScanner && len(capabilities) == 0 {
// to compatible with old version scanner, suppose they should always support scan vulnerability when capability is empty
capabilities[supportVulnerability] = true
capabilities[supportSBOM] = false
}
return capabilities
}

Expand Down
26 changes: 26 additions & 0 deletions src/pkg/scan/rest/v1/models_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,29 @@ func TestIsSupportedMimeType(t *testing.T) {
// Test with an unsupported mime type
assert.False(t, isSupportedMimeType("unsupported/mime-type"), "isSupportedMimeType should return false for unsupported mime types")
}

func TestConvertCapability(t *testing.T) {
md := &ScannerAdapterMetadata{
Capabilities: []*ScannerCapability{
{Type: ScanTypeSbom},
{Type: ScanTypeVulnerability},
},
}
result := md.ConvertCapability()
assert.Equal(t, result[supportSBOM], true)
assert.Equal(t, result[supportVulnerability], true)
}

func TestConvertCapabilityOldScaner(t *testing.T) {
md := &ScannerAdapterMetadata{
Capabilities: []*ScannerCapability{
{
ConsumesMimeTypes: []string{"application/vnd.oci.image.manifest.v1+json", "application/vnd.docker.distribution.manifest.v2+json"},
ProducesMimeTypes: []string{MimeTypeNativeReport},
},
},
}
result := md.ConvertCapability()
assert.Equal(t, result[supportSBOM], false)
assert.Equal(t, result[supportVulnerability], true)
}

0 comments on commit d759429

Please sign in to comment.