Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Set default capability for old scanners #20306

Merged
merged 2 commits into from
Apr 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Comment on lines 169 to 173
Copy link
Contributor

@zyyw zyyw Apr 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switch ... case instead of if ... else if?

}
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)
}
Loading