mirror of
https://github.com/goharbor/harbor.git
synced 2025-01-11 18:38:14 +01:00
refactor(scan): remove duplicate CVESet types
Closes #9471 Signed-off-by: He Weiwei <hweiwei@vmware.com>
This commit is contained in:
parent
3364f76d99
commit
ef37bd1afb
@ -14,7 +14,9 @@
|
|||||||
|
|
||||||
package models
|
package models
|
||||||
|
|
||||||
import "time"
|
import (
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
// CVEAllowlist defines the data model for a CVE allowlist
|
// CVEAllowlist defines the data model for a CVE allowlist
|
||||||
type CVEAllowlist struct {
|
type CVEAllowlist struct {
|
||||||
@ -38,8 +40,8 @@ func (c *CVEAllowlist) TableName() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CVESet returns the set of CVE id of the items in the allowlist to help filter the vulnerability list
|
// CVESet returns the set of CVE id of the items in the allowlist to help filter the vulnerability list
|
||||||
func (c *CVEAllowlist) CVESet() map[string]struct{} {
|
func (c *CVEAllowlist) CVESet() CVESet {
|
||||||
r := map[string]struct{}{}
|
r := CVESet{}
|
||||||
for _, it := range c.Items {
|
for _, it := range c.Items {
|
||||||
r[it.CVEID] = struct{}{}
|
r[it.CVEID] = struct{}{}
|
||||||
}
|
}
|
||||||
@ -53,3 +55,13 @@ func (c *CVEAllowlist) IsExpired() bool {
|
|||||||
}
|
}
|
||||||
return time.Now().Unix() >= *c.ExpiresAt
|
return time.Now().Unix() >= *c.ExpiresAt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// CVESet defines the CVE allowlist with a hash set way for easy query.
|
||||||
|
type CVESet map[string]struct{}
|
||||||
|
|
||||||
|
// Contains checks whether the specified CVE is in the set or not.
|
||||||
|
func (cs CVESet) Contains(cve string) bool {
|
||||||
|
_, ok := cs[cve]
|
||||||
|
|
||||||
|
return ok
|
||||||
|
}
|
||||||
|
@ -15,10 +15,10 @@
|
|||||||
package models
|
package models
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"reflect"
|
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCVEAllowlist_All(t *testing.T) {
|
func TestCVEAllowlist_All(t *testing.T) {
|
||||||
@ -26,7 +26,7 @@ func TestCVEAllowlist_All(t *testing.T) {
|
|||||||
now := time.Now().Unix()
|
now := time.Now().Unix()
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
input CVEAllowlist
|
input CVEAllowlist
|
||||||
cveset map[string]struct{}
|
cveset CVESet
|
||||||
expired bool
|
expired bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
@ -35,7 +35,7 @@ func TestCVEAllowlist_All(t *testing.T) {
|
|||||||
ProjectID: 0,
|
ProjectID: 0,
|
||||||
Items: []CVEAllowlistItem{},
|
Items: []CVEAllowlistItem{},
|
||||||
},
|
},
|
||||||
cveset: map[string]struct{}{},
|
cveset: CVESet{},
|
||||||
expired: false,
|
expired: false,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -45,7 +45,7 @@ func TestCVEAllowlist_All(t *testing.T) {
|
|||||||
Items: []CVEAllowlistItem{},
|
Items: []CVEAllowlistItem{},
|
||||||
ExpiresAt: &now,
|
ExpiresAt: &now,
|
||||||
},
|
},
|
||||||
cveset: map[string]struct{}{},
|
cveset: CVESet{},
|
||||||
expired: true,
|
expired: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
@ -58,7 +58,7 @@ func TestCVEAllowlist_All(t *testing.T) {
|
|||||||
},
|
},
|
||||||
ExpiresAt: &future,
|
ExpiresAt: &future,
|
||||||
},
|
},
|
||||||
cveset: map[string]struct{}{
|
cveset: CVESet{
|
||||||
"CVE-1999-0067": {},
|
"CVE-1999-0067": {},
|
||||||
"CVE-2016-7654321": {},
|
"CVE-2016-7654321": {},
|
||||||
},
|
},
|
||||||
@ -67,6 +67,6 @@ func TestCVEAllowlist_All(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
assert.Equal(t, c.expired, c.input.IsExpired())
|
assert.Equal(t, c.expired, c.input.IsExpired())
|
||||||
assert.True(t, reflect.DeepEqual(c.cveset, c.input.CVESet()))
|
assert.Equal(t, c.cveset, c.input.CVESet())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -476,7 +476,7 @@ func (de *defaultEnforcer) startTask(ctx context.Context, executionID int64, can
|
|||||||
|
|
||||||
// getVulnerabilitySev gets the severity code value for the given artifact with allowlist option set
|
// getVulnerabilitySev gets the severity code value for the given artifact with allowlist option set
|
||||||
func (de *defaultEnforcer) getVulnerabilitySev(ctx context.Context, p *models.Project, art *artifact.Artifact) (uint, error) {
|
func (de *defaultEnforcer) getVulnerabilitySev(ctx context.Context, p *models.Project, art *artifact.Artifact) (uint, error) {
|
||||||
al := report.CVESet(p.CVEAllowlist.CVESet())
|
al := p.CVEAllowlist.CVESet()
|
||||||
r, err := de.scanCtl.GetSummary(ctx, art, []string{v1.MimeTypeNativeReport}, report.WithCVEAllowlist(&al))
|
r, err := de.scanCtl.GetSummary(ctx, art, []string{v1.MimeTypeNativeReport}, report.WithCVEAllowlist(&al))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if errors.IsNotFoundErr(err) {
|
if errors.IsNotFoundErr(err) {
|
||||||
|
@ -17,6 +17,7 @@ package report
|
|||||||
import (
|
import (
|
||||||
"reflect"
|
"reflect"
|
||||||
|
|
||||||
|
"github.com/goharbor/harbor/src/common/models"
|
||||||
"github.com/goharbor/harbor/src/jobservice/job"
|
"github.com/goharbor/harbor/src/jobservice/job"
|
||||||
"github.com/goharbor/harbor/src/lib/errors"
|
"github.com/goharbor/harbor/src/lib/errors"
|
||||||
"github.com/goharbor/harbor/src/pkg/scan/dao/scan"
|
"github.com/goharbor/harbor/src/pkg/scan/dao/scan"
|
||||||
@ -24,29 +25,19 @@ import (
|
|||||||
"github.com/goharbor/harbor/src/pkg/scan/vuln"
|
"github.com/goharbor/harbor/src/pkg/scan/vuln"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CVESet defines the CVE allowlist with a hash set way for easy query.
|
|
||||||
type CVESet map[string]struct{}
|
|
||||||
|
|
||||||
// Contains checks whether the specified CVE is in the set or not.
|
|
||||||
func (cs CVESet) Contains(cve string) bool {
|
|
||||||
_, ok := cs[cve]
|
|
||||||
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
|
|
||||||
// Options provides options for getting the report w/ summary.
|
// Options provides options for getting the report w/ summary.
|
||||||
type Options struct {
|
type Options struct {
|
||||||
// If it is set, the returned report will contains artifact digest for the vulnerabilities
|
// If it is set, the returned report will contains artifact digest for the vulnerabilities
|
||||||
ArtifactDigest string
|
ArtifactDigest string
|
||||||
// If it is set, the returned summary will not count the CVEs in the list in.
|
// If it is set, the returned summary will not count the CVEs in the list in.
|
||||||
CVEAllowlist CVESet
|
CVEAllowlist models.CVESet
|
||||||
}
|
}
|
||||||
|
|
||||||
// Option for getting the report w/ summary with func template way.
|
// Option for getting the report w/ summary with func template way.
|
||||||
type Option func(options *Options)
|
type Option func(options *Options)
|
||||||
|
|
||||||
// WithCVEAllowlist is an option of setting CVE allowlist.
|
// WithCVEAllowlist is an option of setting CVE allowlist.
|
||||||
func WithCVEAllowlist(set *CVESet) Option {
|
func WithCVEAllowlist(set *models.CVESet) Option {
|
||||||
return func(options *Options) {
|
return func(options *Options) {
|
||||||
options.CVEAllowlist = *set
|
options.CVEAllowlist = *set
|
||||||
}
|
}
|
||||||
|
@ -19,6 +19,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/goharbor/harbor/src/common/models"
|
||||||
"github.com/goharbor/harbor/src/pkg/scan/dao/scan"
|
"github.com/goharbor/harbor/src/pkg/scan/dao/scan"
|
||||||
v1 "github.com/goharbor/harbor/src/pkg/scan/rest/v1"
|
v1 "github.com/goharbor/harbor/src/pkg/scan/rest/v1"
|
||||||
"github.com/goharbor/harbor/src/pkg/scan/vuln"
|
"github.com/goharbor/harbor/src/pkg/scan/vuln"
|
||||||
@ -108,7 +109,7 @@ func (suite *SummaryTestSuite) TestSummaryGenerateSummaryNoOptions() {
|
|||||||
|
|
||||||
// TestSummaryGenerateSummaryWithOptions ...
|
// TestSummaryGenerateSummaryWithOptions ...
|
||||||
func (suite *SummaryTestSuite) TestSummaryGenerateSummaryWithOptions() {
|
func (suite *SummaryTestSuite) TestSummaryGenerateSummaryWithOptions() {
|
||||||
cveSet := make(CVESet)
|
cveSet := make(models.CVESet)
|
||||||
cveSet["2019-0980-0909"] = struct{}{}
|
cveSet["2019-0980-0909"] = struct{}{}
|
||||||
|
|
||||||
summaries, err := GenerateSummary(suite.r, WithCVEAllowlist(&cveSet))
|
summaries, err := GenerateSummary(suite.r, WithCVEAllowlist(&cveSet))
|
||||||
|
@ -91,7 +91,7 @@ func Middleware() func(http.Handler) http.Handler {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
allowlist := report.CVESet(proj.CVEAllowlist.CVESet())
|
allowlist := proj.CVEAllowlist.CVESet()
|
||||||
summaries, err := scanController.GetSummary(ctx, art, []string{v1.MimeTypeNativeReport}, report.WithCVEAllowlist(&allowlist))
|
summaries, err := scanController.GetSummary(ctx, art, []string{v1.MimeTypeNativeReport}, report.WithCVEAllowlist(&allowlist))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Errorf("get vulnerability summary of the artifact %s@%s failed, error: %v", art.RepositoryName, art.Digest, err)
|
logger.Errorf("get vulnerability summary of the artifact %s@%s failed, error: %v", art.RepositoryName, art.Digest, err)
|
||||||
|
Loading…
Reference in New Issue
Block a user