From f8b8f702157bb7d474146eb57b31b4091aca9ca3 Mon Sep 17 00:00:00 2001 From: Tariq Ibrahim Date: Tue, 5 Oct 2021 18:33:14 -0700 Subject: [PATCH] fix panics during Scans when there are no layers Signed-off-by: Tariq Ibrahim --- pkg/scanner/adapter.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/scanner/adapter.go b/pkg/scanner/adapter.go index bf9174f..6919efa 100644 --- a/pkg/scanner/adapter.go +++ b/pkg/scanner/adapter.go @@ -2,10 +2,14 @@ package scanner import ( "fmt" + "time" + + log "github.com/sirupsen/logrus" + "github.com/goharbor/harbor-scanner-clair/pkg/clair" + "github.com/goharbor/harbor-scanner-clair/pkg/etc" "github.com/goharbor/harbor-scanner-clair/pkg/harbor" "github.com/goharbor/harbor-scanner-clair/pkg/registry" - log "github.com/sirupsen/logrus" ) // Adapter wraps the Scan method. @@ -35,6 +39,16 @@ func (s *adapter) Scan(req harbor.ScanRequest) (harbor.ScanReport, error) { return harbor.ScanReport{}, fmt.Errorf("preparing layers: %v", err) } + if len(layers) == 0 { + return harbor.ScanReport{ + GeneratedAt: time.Now(), + Scanner: etc.GetScannerMetadata(), + Artifact: req.Artifact, + Severity: harbor.SevUnknown, + Vulnerabilities: []harbor.VulnerabilityItem{}, + }, nil + } + for _, l := range layers { layerLog := log.WithFields(log.Fields{ "layer_name": l.Name,