Skip to content

Commit 27f2d4e

Browse files
committed
Updated e2e test
1 parent 807c953 commit 27f2d4e

File tree

5 files changed

+72
-49
lines changed

5 files changed

+72
-49
lines changed

api/v1/convert/istio.go

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,44 @@
11
package convert
22

33
import (
4+
"github.com/hashicorp/go-version"
45
log "github.com/sirupsen/logrus"
56
"github.com/stackrox/istio-cves/types"
67
"github.com/stackrox/rox/pkg/stringutils"
78
v1 "github.com/stackrox/scanner/generated/scanner/api/v1"
8-
"github.com/stackrox/scanner/pkg/istioUtil"
9+
"github.com/stackrox/scanner/pkg/istioutil"
910
pkgtypes "github.com/stackrox/scanner/pkg/types"
1011
)
1112

1213
// IstioVulnerabilities converts istio cve schema to vulnerability.
13-
func IstioVulnerabilities(version string, istioVulns []types.Vuln) []*v1.Vulnerability {
14+
func IstioVulnerabilities(vStr string, istioVulns []types.Vuln) []*v1.Vulnerability {
1415
res := make([]*v1.Vulnerability, 0, len(istioVulns))
15-
for _, v := range istioVulns {
16-
m, err := pkgtypes.ConvertMetadataFromIstio(v)
16+
v, err := version.NewVersion(vStr)
17+
if err != nil {
18+
log.Infof("Failed to get version: %s", vStr)
19+
return nil
20+
}
21+
for _, istioVuln := range istioVulns {
22+
m, err := pkgtypes.ConvertMetadataFromIstio(istioVuln)
1723
if err != nil {
18-
log.Errorf("unable to convert metadata for %s: %v", v.Name, err)
24+
log.Errorf("unable to convert metadata for %s: %istioVuln", istioVuln.Name, err)
1925
continue
2026
}
2127
if m.IsNilOrEmpty() {
22-
log.Warnf("nil or empty metadata for %s", v.Name)
28+
log.Warnf("nil or empty metadata for %s", istioVuln.Name)
2329
continue
2430
}
2531

26-
link := stringutils.OrDefault(v.Link, "https://istio.io/latest/news/security/")
27-
_, fixedBy, err := istioUtil.IstioIsAffected(version, v)
32+
link := stringutils.OrDefault(istioVuln.Link, "https://istio.io/latest/news/security/")
33+
_, fixedBy, err := istioutil.IsAffected(v, istioVuln)
2834
if err != nil {
29-
log.Errorf("unable to get fixedBy for %s: %v", v.Name, err)
35+
log.Errorf("unable to get fixedBy for %s: %istioVuln", istioVuln.Name, err)
3036
continue
3137
}
3238

3339
res = append(res, &v1.Vulnerability{
34-
Name: v.Name,
35-
Description: v.Description,
40+
Name: istioVuln.Name,
41+
Description: istioVuln.Description,
3642
Link: link,
3743
MetadataV2: Metadata(m),
3844
FixedBy: fixedBy,

api/v1/orchestratorscan/service.go

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ func NewService(db database.Datastore, k8sCache k8scache.Cache, istioCache istio
3838
}
3939

4040
type serviceImpl struct {
41+
v1.UnimplementedOrchestratorScanServiceServer
42+
4143
version string
4244
db database.Datastore
4345
k8sCache k8scache.Cache
@@ -110,31 +112,24 @@ func (s *serviceImpl) GetKubeVulnerabilities(_ context.Context, req *v1.GetKubeV
110112

111113
// GetIstioVulnerabilities returns Istio vulnerabilities for requested Kubernetes version.
112114
func (s *serviceImpl) GetIstioVulnerabilities(_ context.Context, req *v1.GetIstioVulnerabilitiesRequest) (*v1.GetIstioVulnerabilitiesResponse, error) {
113-
var err error
114115
resp := &v1.GetIstioVulnerabilitiesResponse{
115116
ScannerVersion: s.version,
116117
}
117118

118-
getIstioVuln := func(version string) ([]*v1.Vulnerability, error) {
119-
if version == "" {
120-
return nil, errors.New("Can't get vulnerabilities for empty version.")
121-
}
122-
version, err := convert.TruncateVersion(version)
123-
if err != nil {
124-
log.Warnf("Unable to convert Istio version of %s - %v. Skipping...", version, err)
125-
return nil, nil
126-
}
127-
128-
vulns := s.istioCache.GetVulnsByVersion(version)
129-
converted := convert.IstioVulnerabilities(version, vulns)
130-
return filterInvalidVulns(converted), nil
119+
if req.GetIstioVersion() == "" {
120+
return nil, errors.New("Can't get vulnerabilities for empty version.")
131121
}
132-
133-
resp.Vulnerabilities, err = getIstioVuln(req.GetIstioVersion())
122+
version, err := convert.TruncateVersion(req.GetIstioVersion())
134123
if err != nil {
135-
return nil, status.Error(codes.Internal, err.Error())
124+
log.Warnf("Unable to convert Istio version of %s - %v. Skipping...", version, err)
125+
return nil, nil
136126
}
137127

128+
vulns := s.istioCache.GetVulnsByVersion(version)
129+
converted := convert.IstioVulnerabilities(version, vulns)
130+
131+
resp.Vulnerabilities = filterInvalidVulns(converted)
132+
138133
return resp, nil
139134
}
140135

e2etests/orchestrator_scan_test.go

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,45 @@ func TestGRPCGetIstioVulnerabilities(t *testing.T) {
2424
conn := connectToScanner(t)
2525
client := v1.NewOrchestratorScanServiceClient(conn)
2626

27-
testCases := []struct {
28-
version string
29-
fixedBy string
30-
}{
27+
type istionVulnStruct struct {
28+
version string
29+
name string
30+
fixedBy string
31+
severity string
32+
score float32
33+
}
34+
35+
testCases := []istionVulnStruct{
3136
{
32-
version: "1.13.6",
33-
fixedBy: "1.13.7",
37+
version: "1.13.6",
38+
name: "ISTIO-SECURITY-2022-006",
39+
fixedBy: "1.13.7",
40+
severity: "Moderate",
41+
score: 5.9,
3442
},
3543
}
3644

45+
testSet := make(map[string]istionVulnStruct)
46+
3747
for _, c := range testCases {
3848
t.Run(fmt.Sprintf("case-%s", c.version), func(t *testing.T) {
3949
req := &v1.GetIstioVulnerabilitiesRequest{IstioVersion: c.version}
4050
resp, err := client.GetIstioVulnerabilities(context.Background(), req)
4151
assert.NoError(t, err)
42-
testSet := make(map[string]bool)
52+
4353
for _, vuln := range resp.GetVulnerabilities() {
4454
assert.NotNil(t, vuln.GetMetadataV2().GetCvssV3())
55+
assert.NotEmpty(t, vuln.Name)
4556
assert.NotEmpty(t, vuln.FixedBy)
46-
testSet[vuln.FixedBy] = true
57+
assert.NotEmpty(t, vuln.Severity)
58+
sample := istionVulnStruct{version: c.version, name: vuln.Name, fixedBy: vuln.FixedBy, severity: vuln.Severity, score: vuln.GetMetadataV2().GetCvssV3().Score}
59+
testSet[vuln.Name] = sample
4760
}
48-
assert.True(t, testSet[c.fixedBy])
61+
62+
assert.NotEmpty(t, testSet[c.name])
63+
assert.Equal(t, c.fixedBy, testSet[c.name].fixedBy)
64+
assert.Equal(t, c.severity, testSet[c.name].severity)
65+
assert.Equal(t, c.score, testSet[c.name].score)
4966
})
5067
}
5168
}

istio/cache/db.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import (
44
"sync"
55
"time"
66

7+
"github.com/hashicorp/go-version"
8+
log "github.com/sirupsen/logrus"
79
"github.com/stackrox/istio-cves/types"
8-
"github.com/stackrox/scanner/pkg/istioUtil"
10+
"github.com/stackrox/scanner/pkg/istioutil"
911
"github.com/stackrox/scanner/pkg/vulndump"
1012
)
1113

@@ -23,15 +25,23 @@ type cacheImpl struct {
2325
lastUpdatedTime time.Time
2426
}
2527

26-
func (c *cacheImpl) GetVulnsByVersion(version string) []types.Vuln {
28+
func (c *cacheImpl) GetVulnsByVersion(vStr string) []types.Vuln {
2729
c.cacheRWLock.RLock()
2830
defer c.cacheRWLock.RUnlock()
2931

3032
var vulns []types.Vuln
33+
v, err := version.NewVersion(vStr)
34+
if err != nil {
35+
log.Infof("Failed to get version: %s", vStr)
36+
return nil
37+
}
3138
for _, vuln := range c.cache {
32-
isAffected, _, _ := istioUtil.IstioIsAffected(version, vuln)
39+
isAffected, _, error := istioutil.IsAffected(v, vuln)
40+
if error != nil {
41+
continue
42+
}
3343
if isAffected {
34-
// Only return vulnerabilities relevant to the given version.
44+
// Only return vulnerabilities relevant to the given vStr.
3545
vulns = append(vulns, vuln)
3646
}
3747
}

pkg/istioUtil/util.go renamed to pkg/istioutil/util.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,12 @@
1-
package istioUtil
1+
package istioutil
22

33
import (
44
"github.com/hashicorp/go-version"
55
"github.com/stackrox/istio-cves/types"
66
)
77

8-
// IstioIsAffected gets the fixed-by version for vStr in Istion vuln.
9-
func IstioIsAffected(vStr string, vuln types.Vuln) (bool, string, error) {
10-
v, err := version.NewVersion(vStr)
11-
if err != nil {
12-
return false, "", err
13-
}
14-
8+
// IsAffected gets the fixed-by version for vStr in Istion vuln.
9+
func IsAffected(v *version.Version, vuln types.Vuln) (bool, string, error) {
1510
for _, affected := range vuln.Affected {
1611
constraint, err := version.NewConstraint(affected.Range)
1712
if err != nil {

0 commit comments

Comments
 (0)