From d154c27362323bcfc009901a939ce976592c9b03 Mon Sep 17 00:00:00 2001 From: "stonezdj(Daojun Zhang)" Date: Mon, 29 Apr 2024 21:51:09 +0800 Subject: [PATCH] Add scan type in webhook event (#20363) fixes #20331 Signed-off-by: stonezdj --- .../event/handler/webhook/scan/scan.go | 26 +++++++++++++++---- src/controller/event/metadata/scan.go | 2 ++ src/controller/event/model/event.go | 6 +++++ src/controller/event/topic.go | 1 + src/controller/scan/callback.go | 7 +++++ src/pkg/notifier/model/event.go | 2 ++ 6 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/controller/event/handler/webhook/scan/scan.go b/src/controller/event/handler/webhook/scan/scan.go index fda487a07..04b3fffb5 100644 --- a/src/controller/event/handler/webhook/scan/scan.go +++ b/src/controller/event/handler/webhook/scan/scan.go @@ -21,6 +21,7 @@ import ( "github.com/goharbor/harbor/src/controller/artifact" "github.com/goharbor/harbor/src/controller/event" "github.com/goharbor/harbor/src/controller/event/handler/util" + eventModel "github.com/goharbor/harbor/src/controller/event/model" "github.com/goharbor/harbor/src/controller/project" "github.com/goharbor/harbor/src/controller/scan" "github.com/goharbor/harbor/src/lib/errors" @@ -104,6 +105,9 @@ func constructScanImagePayload(ctx context.Context, event *event.ScanImageEvent, RepoFullName: event.Artifact.Repository, RepoType: repoType, }, + Scan: &eventModel.Scan{ + ScanType: event.ScanType, + }, }, Operator: event.Operator, } @@ -138,17 +142,29 @@ func constructScanImagePayload(ctx context.Context, event *event.ScanImageEvent, time.Sleep(500 * time.Millisecond) } - // Add scan overview - summaries, err := scan.DefaultController.GetSummary(ctx, art, []string{v1.MimeTypeNativeReport, v1.MimeTypeGenericVulnerabilityReport}) - if err != nil { - return nil, errors.Wrap(err, "construct scan payload") + scanSummaries := map[string]interface{}{} + if event.ScanType == v1.ScanTypeVulnerability { + scanSummaries, err = scan.DefaultController.GetSummary(ctx, art, []string{v1.MimeTypeNativeReport, v1.MimeTypeGenericVulnerabilityReport}) + if err != nil { + return nil, errors.Wrap(err, "construct scan payload") + } } + sbomOverview := map[string]interface{}{} + if event.ScanType == v1.ScanTypeSbom { + sbomOverview, err = scan.DefaultController.GetSummary(ctx, art, []string{v1.MimeTypeSBOMReport}) + if err != nil { + return nil, errors.Wrap(err, "construct scan payload") + } + } + + // Add scan overview and sbom overview resource := &model.Resource{ Tag: event.Artifact.Tag, Digest: event.Artifact.Digest, ResourceURL: resURL, - ScanOverview: summaries, + ScanOverview: scanSummaries, + SBOMOverview: sbomOverview, } payload.EventData.Resources = append(payload.EventData.Resources, resource) diff --git a/src/controller/event/metadata/scan.go b/src/controller/event/metadata/scan.go index a05cc3d8f..588a9e3e1 100644 --- a/src/controller/event/metadata/scan.go +++ b/src/controller/event/metadata/scan.go @@ -27,6 +27,7 @@ import ( // ScanImageMetaData defines meta data of image scanning event type ScanImageMetaData struct { Artifact *v1.Artifact + ScanType string Status string Operator string } @@ -55,6 +56,7 @@ func (si *ScanImageMetaData) Resolve(evt *event.Event) error { Artifact: si.Artifact, OccurAt: time.Now(), Operator: si.Operator, + ScanType: si.ScanType, } evt.Topic = topic diff --git a/src/controller/event/model/event.go b/src/controller/event/model/event.go index 6782b152d..2e7021bc3 100644 --- a/src/controller/event/model/event.go +++ b/src/controller/event/model/event.go @@ -74,3 +74,9 @@ type RetentionRule struct { // Selector attached to the rule for filtering scope (e.g: repositories or namespaces) ScopeSelectors map[string][]*rule.Selector `json:"scope_selectors,omitempty"` } + +// Scan describes scan infos +type Scan struct { + // ScanType the scan type + ScanType string `json:"scan_type,omitempty"` +} diff --git a/src/controller/event/topic.go b/src/controller/event/topic.go index 5898bf4af..08e133e1a 100644 --- a/src/controller/event/topic.go +++ b/src/controller/event/topic.go @@ -289,6 +289,7 @@ func (d *DeleteTagEvent) String() string { // ScanImageEvent is scanning image related event data to publish type ScanImageEvent struct { EventType string + ScanType string Artifact *v1.Artifact OccurAt time.Time Operator string diff --git a/src/controller/scan/callback.go b/src/controller/scan/callback.go index 978219f26..5229ca0b1 100644 --- a/src/controller/scan/callback.go +++ b/src/controller/scan/callback.go @@ -120,6 +120,13 @@ func scanTaskStatusChange(ctx context.Context, taskID int64, status string) (err if operator, ok := exec.ExtraAttrs["operator"].(string); ok { e.Operator = operator } + + // extract ScanType if exist in ExtraAttrs + if c, ok := exec.ExtraAttrs["enabled_capabilities"].(map[string]interface{}); ok { + if Type, ok := c["type"].(string); ok { + e.ScanType = Type + } + } // fire event notification.AddEvent(ctx, e) } diff --git a/src/pkg/notifier/model/event.go b/src/pkg/notifier/model/event.go index bcdc2a6c1..4bf852df0 100644 --- a/src/pkg/notifier/model/event.go +++ b/src/pkg/notifier/model/event.go @@ -42,6 +42,7 @@ type EventData struct { Repository *Repository `json:"repository,omitempty"` Replication *model.Replication `json:"replication,omitempty"` Retention *model.Retention `json:"retention,omitempty"` + Scan *model.Scan `json:"scan,omitempty"` Custom map[string]string `json:"custom_attributes,omitempty"` } @@ -51,6 +52,7 @@ type Resource struct { Tag string `json:"tag,omitempty"` ResourceURL string `json:"resource_url,omitempty"` ScanOverview map[string]interface{} `json:"scan_overview,omitempty"` + SBOMOverview map[string]interface{} `json:"sbom_overview,omitempty"` } // Repository info of notification event