mirror of
https://github.com/goharbor/harbor.git
synced 2025-02-01 12:31:23 +01:00
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 <stone.zhang@broadcom.com>
This commit is contained in:
parent
fb2e0042d0
commit
2ea7d09412
@ -22,6 +22,7 @@ import (
|
|||||||
"github.com/goharbor/harbor/src/controller/scan"
|
"github.com/goharbor/harbor/src/controller/scan"
|
||||||
"github.com/goharbor/harbor/src/lib/log"
|
"github.com/goharbor/harbor/src/lib/log"
|
||||||
"github.com/goharbor/harbor/src/lib/orm"
|
"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
|
// 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 {
|
return orm.WithTransaction(func(ctx context.Context) error {
|
||||||
options := []scan.Option{}
|
options := []scan.Option{}
|
||||||
if len(tags) > 0 {
|
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...)
|
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
|
// transaction here to work with the image index
|
||||||
return orm.WithTransaction(func(ctx context.Context) error {
|
return orm.WithTransaction(func(ctx context.Context) error {
|
||||||
options := []scan.Option{}
|
options := []scan.Option{}
|
||||||
// TODO: extract the sbom scan type to a constant
|
options = append(options, scan.WithScanType(v1.ScanTypeSbom), scan.WithFromEvent(true))
|
||||||
options = append(options, scan.WithScanType("sbom"))
|
|
||||||
log.Debugf("sbom scan controller artifact %+v, options %+v", a, options)
|
log.Debugf("sbom scan controller artifact %+v, options %+v", a, options)
|
||||||
return scan.DefaultController.Scan(ctx, a, options...)
|
return scan.DefaultController.Scan(ctx, a, options...)
|
||||||
})(orm.SetTransactionOpNameToContext(ctx, "tx-auto-gen-sbom"))
|
})(orm.SetTransactionOpNameToContext(ctx, "tx-auto-gen-sbom"))
|
||||||
|
@ -101,9 +101,7 @@ func (suite *AutoScanTestSuite) TestAutoScanSBOM() {
|
|||||||
proModels.ProMetaAutoSBOMGen: "true",
|
proModels.ProMetaAutoSBOMGen: "true",
|
||||||
},
|
},
|
||||||
}, nil)
|
}, nil)
|
||||||
|
suite.scanController.On("Scan", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Once()
|
||||||
mock.OnAnything(suite.scanController, "Scan").Return(nil)
|
|
||||||
|
|
||||||
ctx := orm.NewContext(nil, &ormtesting.FakeOrmer{})
|
ctx := orm.NewContext(nil, &ormtesting.FakeOrmer{})
|
||||||
art := &artifact.Artifact{}
|
art := &artifact.Artifact{}
|
||||||
|
|
||||||
@ -117,7 +115,7 @@ func (suite *AutoScanTestSuite) TestAutoScanSBOMFalse() {
|
|||||||
},
|
},
|
||||||
}, nil)
|
}, 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{})
|
ctx := orm.NewContext(nil, &ormtesting.FakeOrmer{})
|
||||||
art := &artifact.Artifact{}
|
art := &artifact.Artifact{}
|
||||||
|
@ -247,17 +247,20 @@ func (bc *basicController) Scan(ctx context.Context, artifact *ar.Artifact, opti
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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
|
// Parse options
|
||||||
opts, err := parseOptions(options...)
|
opts, err := parseOptions(options...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "scan controller: scan")
|
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 (
|
var (
|
||||||
errs []error
|
errs []error
|
||||||
launchScanJobParams []*launchScanJobParam
|
launchScanJobParams []*launchScanJobParam
|
||||||
|
@ -21,6 +21,7 @@ type Options struct {
|
|||||||
ExecutionID int64 // The execution id to scan artifact
|
ExecutionID int64 // The execution id to scan artifact
|
||||||
Tag string // The tag of the artifact to scan
|
Tag string // The tag of the artifact to scan
|
||||||
ScanType string // The scan type could be sbom or vulnerability
|
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.
|
// GetScanType returns the scan type. for backward compatibility, the default type is vulnerability.
|
||||||
@ -63,3 +64,11 @@ func WithScanType(scanType string) Option {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// WithFromEvent set the caller's source
|
||||||
|
func WithFromEvent(fromEvent bool) Option {
|
||||||
|
return func(options *Options) error {
|
||||||
|
options.FromEvent = fromEvent
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user