From 2ea7d09412e25a838e217e2c4d52ee4d0f51ec1f Mon Sep 17 00:00:00 2001 From: "stonezdj(Daojun Zhang)" Date: Wed, 17 Apr 2024 22:51:11 +0800 Subject: [PATCH] skip to log scan sbom accessory for sbom accessory (#20290) Avoid to log the generate SBOM failure message when the artifact is SBOM in webhook event Signed-off-by: stonezdj --- src/controller/event/handler/internal/util.go | 6 +++--- src/controller/event/handler/internal/util_test.go | 6 ++---- src/controller/scan/base_controller.go | 13 ++++++++----- src/controller/scan/options.go | 9 +++++++++ 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/controller/event/handler/internal/util.go b/src/controller/event/handler/internal/util.go index cc10e09ca..51085a6f1 100644 --- a/src/controller/event/handler/internal/util.go +++ b/src/controller/event/handler/internal/util.go @@ -22,6 +22,7 @@ import ( "github.com/goharbor/harbor/src/controller/scan" "github.com/goharbor/harbor/src/lib/log" "github.com/goharbor/harbor/src/lib/orm" + v1 "github.com/goharbor/harbor/src/pkg/scan/rest/v1" ) // autoScan scan artifact when the project of the artifact enable auto scan @@ -38,7 +39,7 @@ func autoScan(ctx context.Context, a *artifact.Artifact, tags ...string) error { return orm.WithTransaction(func(ctx context.Context) error { options := []scan.Option{} if len(tags) > 0 { - options = append(options, scan.WithTag(tags[0])) + options = append(options, scan.WithTag(tags[0]), scan.WithFromEvent(true)) } return scan.DefaultController.Scan(ctx, a, options...) @@ -56,8 +57,7 @@ func autoGenSBOM(ctx context.Context, a *artifact.Artifact) error { // transaction here to work with the image index return orm.WithTransaction(func(ctx context.Context) error { options := []scan.Option{} - // TODO: extract the sbom scan type to a constant - options = append(options, scan.WithScanType("sbom")) + options = append(options, scan.WithScanType(v1.ScanTypeSbom), scan.WithFromEvent(true)) log.Debugf("sbom scan controller artifact %+v, options %+v", a, options) return scan.DefaultController.Scan(ctx, a, options...) })(orm.SetTransactionOpNameToContext(ctx, "tx-auto-gen-sbom")) diff --git a/src/controller/event/handler/internal/util_test.go b/src/controller/event/handler/internal/util_test.go index 4a48378a4..158bcc2fe 100644 --- a/src/controller/event/handler/internal/util_test.go +++ b/src/controller/event/handler/internal/util_test.go @@ -101,9 +101,7 @@ func (suite *AutoScanTestSuite) TestAutoScanSBOM() { proModels.ProMetaAutoSBOMGen: "true", }, }, nil) - - mock.OnAnything(suite.scanController, "Scan").Return(nil) - + suite.scanController.On("Scan", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Once() ctx := orm.NewContext(nil, &ormtesting.FakeOrmer{}) art := &artifact.Artifact{} @@ -117,7 +115,7 @@ func (suite *AutoScanTestSuite) TestAutoScanSBOMFalse() { }, }, nil) - mock.OnAnything(suite.scanController, "Scan").Return(nil) + suite.scanController.On("Scan", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Once() ctx := orm.NewContext(nil, &ormtesting.FakeOrmer{}) art := &artifact.Artifact{} diff --git a/src/controller/scan/base_controller.go b/src/controller/scan/base_controller.go index 25f0fc791..52eb4eefe 100644 --- a/src/controller/scan/base_controller.go +++ b/src/controller/scan/base_controller.go @@ -247,17 +247,20 @@ func (bc *basicController) Scan(ctx context.Context, artifact *ar.Artifact, opti if err != nil { return err } - - if !scannable { - return errors.BadRequestError(nil).WithMessage("the configured scanner %s does not support scanning artifact with mime type %s", r.Name, artifact.ManifestMediaType) - } - // Parse options opts, err := parseOptions(options...) if err != nil { return errors.Wrap(err, "scan controller: scan") } + if !scannable { + if opts.FromEvent { + // skip to return err for event related scan + return nil + } + return errors.BadRequestError(nil).WithMessage("the configured scanner %s does not support scanning artifact with mime type %s", r.Name, artifact.ManifestMediaType) + } + var ( errs []error launchScanJobParams []*launchScanJobParam diff --git a/src/controller/scan/options.go b/src/controller/scan/options.go index 82e4e3d3e..c751ee100 100644 --- a/src/controller/scan/options.go +++ b/src/controller/scan/options.go @@ -21,6 +21,7 @@ type Options struct { ExecutionID int64 // The execution id to scan artifact Tag string // The tag of the artifact to scan ScanType string // The scan type could be sbom or vulnerability + FromEvent bool // indicate the current call from event or not } // GetScanType returns the scan type. for backward compatibility, the default type is vulnerability. @@ -63,3 +64,11 @@ func WithScanType(scanType string) Option { return nil } } + +// WithFromEvent set the caller's source +func WithFromEvent(fromEvent bool) Option { + return func(options *Options) error { + options.FromEvent = fromEvent + return nil + } +}