Skip to content

Commit e693353

Browse files
committed
Extract ConvertFeatures to a separate pkg
1 parent 1b792ee commit e693353

File tree

5 files changed

+61
-53
lines changed

5 files changed

+61
-53
lines changed

api/v1/features/convert.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package features
2+
3+
import (
4+
log "github.com/sirupsen/logrus"
5+
apiV1 "github.com/stackrox/scanner/api/v1"
6+
"github.com/stackrox/scanner/api/v1/convert"
7+
v1 "github.com/stackrox/scanner/generated/scanner/api/v1"
8+
)
9+
10+
// ConvertFeatures converts api Features into v1 (proto) Feature pointers.
11+
func ConvertFeatures(apiFeatures []apiV1.Feature) []*v1.Feature {
12+
features := make([]*v1.Feature, 0, len(apiFeatures))
13+
for _, a := range apiFeatures {
14+
vulns := convertVulnerabilities(a.Vulnerabilities)
15+
16+
features = append(features, &v1.Feature{
17+
Name: a.Name,
18+
Version: a.Version,
19+
FeatureType: a.VersionFormat,
20+
AddedByLayer: a.AddedBy,
21+
Location: a.Location,
22+
Vulnerabilities: vulns,
23+
FixedBy: a.FixedBy,
24+
ProvidedExecutables: a.Executables,
25+
})
26+
}
27+
return features
28+
}
29+
30+
func convertVulnerabilities(apiVulns []apiV1.Vulnerability) []*v1.Vulnerability {
31+
vulns := make([]*v1.Vulnerability, 0, len(apiVulns))
32+
for _, v := range apiVulns {
33+
metadata, err := convert.MetadataMap(v.Metadata)
34+
if err != nil {
35+
log.Errorf("error converting metadata map: %v", err)
36+
continue
37+
}
38+
if metadata == nil {
39+
log.Warnf("metadata is nil for %s; skipping...", v.Name)
40+
continue
41+
}
42+
43+
vulns = append(vulns, &v1.Vulnerability{
44+
Name: v.Name,
45+
Description: v.Description,
46+
Link: v.Link,
47+
MetadataV2: metadata,
48+
FixedBy: v.FixedBy,
49+
Severity: v.Severity,
50+
})
51+
}
52+
return vulns
53+
}

api/v1/imagescan/convert.go

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"strings"
55

66
"github.com/pkg/errors"
7-
log "github.com/sirupsen/logrus"
87
"github.com/stackrox/rox/pkg/utils"
98
apiV1 "github.com/stackrox/scanner/api/v1"
109
"github.com/stackrox/scanner/api/v1/common"
@@ -39,51 +38,6 @@ var (
3938
}()
4039
)
4140

42-
func convertVulnerabilities(apiVulns []apiV1.Vulnerability) []*v1.Vulnerability {
43-
vulns := make([]*v1.Vulnerability, 0, len(apiVulns))
44-
for _, v := range apiVulns {
45-
metadata, err := convert.MetadataMap(v.Metadata)
46-
if err != nil {
47-
log.Errorf("error converting metadata map: %v", err)
48-
continue
49-
}
50-
if metadata == nil {
51-
log.Warnf("metadata is nil for %s; skipping...", v.Name)
52-
continue
53-
}
54-
55-
vulns = append(vulns, &v1.Vulnerability{
56-
Name: v.Name,
57-
Description: v.Description,
58-
Link: v.Link,
59-
MetadataV2: metadata,
60-
FixedBy: v.FixedBy,
61-
Severity: v.Severity,
62-
})
63-
}
64-
return vulns
65-
}
66-
67-
// ConvertFeatures converts api Features into v1 (proto) Feature pointers.
68-
func ConvertFeatures(apiFeatures []apiV1.Feature) []*v1.Feature {
69-
features := make([]*v1.Feature, 0, len(apiFeatures))
70-
for _, a := range apiFeatures {
71-
vulns := convertVulnerabilities(a.Vulnerabilities)
72-
73-
features = append(features, &v1.Feature{
74-
Name: a.Name,
75-
Version: a.Version,
76-
FeatureType: a.VersionFormat,
77-
AddedByLayer: a.AddedBy,
78-
Location: a.Location,
79-
Vulnerabilities: vulns,
80-
FixedBy: a.FixedBy,
81-
ProvidedExecutables: a.Executables,
82-
})
83-
}
84-
return features
85-
}
86-
8741
func convertLanguageLevelComponents(layersToComponents []*component.LayerToComponents) map[string]*v1.LanguageLevelComponents {
8842
converted := make(map[string]*v1.LanguageLevelComponents, len(layersToComponents))
8943
for _, layerToComponents := range layersToComponents {

api/v1/imagescan/service.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
apiGRPC "github.com/stackrox/scanner/api/grpc"
1010
apiV1 "github.com/stackrox/scanner/api/v1"
1111
"github.com/stackrox/scanner/api/v1/common"
12+
"github.com/stackrox/scanner/api/v1/features"
1213
"github.com/stackrox/scanner/cpe/nvdtoolscache"
1314
"github.com/stackrox/scanner/database"
1415
v1 "github.com/stackrox/scanner/generated/scanner/api/v1"
@@ -91,7 +92,7 @@ func (s *serviceImpl) GetImageScan(_ context.Context, req *v1.GetImageScanReques
9192
Status: v1.ScanStatus_SUCCEEDED,
9293
Image: &v1.Image{
9394
Namespace: layer.NamespaceName,
94-
Features: ConvertFeatures(layer.Features),
95+
Features: features.ConvertFeatures(layer.Features),
9596
},
9697
Notes: convertNotes(notes),
9798
}, nil
@@ -216,7 +217,7 @@ func (s *serviceImpl) GetImageVulnerabilities(_ context.Context, req *v1.GetImag
216217
Status: v1.ScanStatus_SUCCEEDED,
217218
Image: &v1.Image{
218219
Namespace: layer.NamespaceName,
219-
Features: ConvertFeatures(layer.Features),
220+
Features: features.ConvertFeatures(layer.Features),
220221
},
221222
}, nil
222223
}

api/v1/nodescan/service.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
apiV1 "github.com/stackrox/scanner/api/v1"
1414
"github.com/stackrox/scanner/api/v1/common"
1515
"github.com/stackrox/scanner/api/v1/convert"
16-
"github.com/stackrox/scanner/api/v1/imagescan"
16+
"github.com/stackrox/scanner/api/v1/features"
1717
"github.com/stackrox/scanner/cpe/nvdtoolscache"
1818
"github.com/stackrox/scanner/database"
1919
"github.com/stackrox/scanner/ext/kernelparser"
@@ -279,7 +279,7 @@ func (s *serviceImpl) GetNodeVulnerabilities(_ context.Context, req *v1.GetNodeV
279279
if err != nil {
280280
return nil, status.Error(codes.Internal, err.Error())
281281
}
282-
resp.Features = imagescan.ConvertFeatures(layer.Features)
282+
resp.Features = features.ConvertFeatures(layer.Features)
283283
}
284284
return resp, nil
285285
}

e2etests/grpc_full_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"sort"
1212
"testing"
1313

14-
"github.com/stackrox/scanner/api/v1/imagescan"
14+
"github.com/stackrox/scanner/api/v1/features"
1515
v1 "github.com/stackrox/scanner/generated/scanner/api/v1"
1616
"github.com/stretchr/testify/assert"
1717
"github.com/stretchr/testify/require"
@@ -43,7 +43,7 @@ func verifyImage(t *testing.T, imgScan *v1.Image, test testCase) {
4343
feature.Vulnerabilities = filteredVulns
4444
}
4545

46-
for _, feature := range imagescan.ConvertFeatures(test.expectedFeatures) {
46+
for _, feature := range features.ConvertFeatures(test.expectedFeatures) {
4747
t.Run(fmt.Sprintf("%s/%s/%s", test.image, feature.Name, feature.Version), func(t *testing.T) {
4848
matching := getMatchingGRPCFeature(t, imgScan.Features, feature, false)
4949
if matching.Vulnerabilities != nil {
@@ -96,7 +96,7 @@ func verifyImage(t *testing.T, imgScan *v1.Image, test testCase) {
9696
})
9797
}
9898

99-
for _, feature := range imagescan.ConvertFeatures(test.unexpectedFeatures) {
99+
for _, feature := range features.ConvertFeatures(test.unexpectedFeatures) {
100100
assert.Nil(t, getMatchingGRPCFeature(t, imgScan.Features, feature, true))
101101
}
102102
}

0 commit comments

Comments
 (0)