Skip to content

Commit

Permalink
Merge branch 'main' into sbom-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
Shengwen YU authored Apr 22, 2024
2 parents b48dce4 + d759429 commit e616d62
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 e616d62

Please sign in to comment.