Skip to content

ROX-13627: Extend GetNodeVulnerabilities API by supporting Node Inventory #1004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 41 commits into from
Jan 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
79eec92
Extend GetNodeVulnerabilitiesRequest/Response
vikin91 Nov 4, 2022
75760af
Fix Makefile for devs who have spaces in the PATH and do not use wget
vikin91 Nov 4, 2022
858a2c4
Extend GetNodeVulnerabilities-API by adding node inventory support
vikin91 Nov 4, 2022
6d68e7c
Add e2e test for GetRHCOSNodeVulnerabilities
vikin91 Nov 7, 2022
a100e49
Add e2e testcase for a vulnerable and non-fixable pkg
vikin91 Nov 22, 2022
371c99e
Remove WIP integration test from this branch
vikin91 Nov 22, 2022
892a246
Cleanup debug printlns
vikin91 Nov 22, 2022
fca9919
Add a feature flad for the RHCOS node scanning
vikin91 Nov 22, 2022
75fadf0
Minor: Fix indentation in proto file
vikin91 Nov 22, 2022
bf11cb0
Fix style
vikin91 Nov 22, 2022
cd12427
Readd accidentally removed line
vikin91 Nov 22, 2022
2e12f90
Style: var found bool
vikin91 Nov 22, 2022
a1d9cf9
Use less confusing strings in test case
vikin91 Nov 22, 2022
d6ec31a
Add more debug to a test failure msg
vikin91 Nov 23, 2022
e818a91
Fix mistake in test
vikin91 Nov 23, 2022
470c4b3
Skip e2e test if a feature flag is disabled
vikin91 Nov 23, 2022
e0e2842
Enable the ff and see what CI says
vikin91 Nov 23, 2022
2faba04
Disable the feature flag again
vikin91 Nov 24, 2022
6ffc2e2
Enable the ROX_RHCOS_NODE_SCANNING feature flag in the chart, so that…
vikin91 Nov 24, 2022
52b12a7
Remove feature flag RHCOSNodeScanning
vikin91 Dec 6, 2022
cf0dc19
Improve code in the e2e test
vikin91 Dec 6, 2022
f6fe85f
Use realistic namespace value
vikin91 Dec 6, 2022
089f1df
Rename fields in proto. Add Notes to GetNodeVulnerabilitiesRequest
vikin91 Dec 7, 2022
45be13f
Handle uncertifiedRHEL. Move hasUncertifiedRHEL to common pkg
vikin91 Dec 7, 2022
f6c9742
Add test case for uncertifiedRHEL scan
vikin91 Dec 7, 2022
7c8416a
Apply renaming in e2e test
vikin91 Dec 7, 2022
f75f7d3
Fix expected result for uncertifiedRHEL scan
vikin91 Dec 7, 2022
e0a6003
Add testcase for feat with 0 vulnerabilities. Fix issues found on the…
vikin91 Dec 7, 2022
0070416
Add --fail to curl
vikin91 Dec 7, 2022
de9c59c
Add assertion on number of features in the API response
vikin91 Dec 7, 2022
5fe7d4f
Remove curl support
vikin91 Dec 7, 2022
c1eee55
Correct leftovers
vikin91 Dec 7, 2022
687f46c
Extract ConvertFeatures to a separate pkg
vikin91 Dec 7, 2022
875c69d
Add comment to meet style reqs
vikin91 Dec 7, 2022
e2e5d30
Simplify the test assertions
vikin91 Dec 8, 2022
fe94c74
Add comments about spot-checking of vulns
vikin91 Dec 9, 2022
6692db4
Address review comments
vikin91 Jan 2, 2023
fe869a5
Remove nil check in test code
vikin91 Jan 4, 2023
1e3bd9d
Remove accidentally added blank lines
vikin91 Jan 4, 2023
5216a20
Add comment to the grep test case stating that this may fail in the f…
vikin91 Jan 4, 2023
d26da56
Use map for storing test cases
vikin91 Jan 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Store tooling in a location that does not affect the system.
GOBIN := $(CURDIR)/.gobin
export GOBIN
PATH := $(GOBIN):$(PATH)
PATH := $(GOBIN):"$(PATH)"
export PATH

# Set to empty string to echo some command lines which are hidden by default.
Expand Down
11 changes: 11 additions & 0 deletions api/v1/common/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,14 @@ func toFeatureNameVersions(keys FeatureKeySet) []*scannerV1.FeatureNameVersion {
}
return features
}

// HasUncertifiedRHEL returns true if Notes contain CERTIFIED_RHEL_SCAN_UNAVAILABLE; false otherwise
func HasUncertifiedRHEL(notes []scannerV1.Note) bool {
for _, note := range notes {
if note == scannerV1.Note_CERTIFIED_RHEL_SCAN_UNAVAILABLE {
return true
}
}

return false
}
53 changes: 53 additions & 0 deletions api/v1/features/convert.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package features

import (
log "github.com/sirupsen/logrus"
apiV1 "github.com/stackrox/scanner/api/v1"
"github.com/stackrox/scanner/api/v1/convert"
v1 "github.com/stackrox/scanner/generated/scanner/api/v1"
)

// ConvertFeatures converts api Features into v1 (proto) Feature pointers.
func ConvertFeatures(apiFeatures []apiV1.Feature) []*v1.Feature {
features := make([]*v1.Feature, 0, len(apiFeatures))
for _, a := range apiFeatures {
vulns := convertVulnerabilities(a.Vulnerabilities)

features = append(features, &v1.Feature{
Name: a.Name,
Version: a.Version,
FeatureType: a.VersionFormat,
AddedByLayer: a.AddedBy,
Location: a.Location,
Vulnerabilities: vulns,
FixedBy: a.FixedBy,
ProvidedExecutables: a.Executables,
})
}
return features
}

func convertVulnerabilities(apiVulns []apiV1.Vulnerability) []*v1.Vulnerability {
vulns := make([]*v1.Vulnerability, 0, len(apiVulns))
for _, v := range apiVulns {
metadata, err := convert.MetadataMap(v.Metadata)
if err != nil {
log.Errorf("error converting metadata map: %v", err)
continue
}
if metadata == nil {
log.Warnf("metadata is nil for %s; skipping...", v.Name)
continue
}

vulns = append(vulns, &v1.Vulnerability{
Name: v.Name,
Description: v.Description,
Link: v.Link,
MetadataV2: metadata,
FixedBy: v.FixedBy,
Severity: v.Severity,
})
}
return vulns
}
46 changes: 0 additions & 46 deletions api/v1/imagescan/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"strings"

"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/stackrox/rox/pkg/utils"
apiV1 "github.com/stackrox/scanner/api/v1"
"github.com/stackrox/scanner/api/v1/common"
Expand Down Expand Up @@ -39,51 +38,6 @@ var (
}()
)

func convertVulnerabilities(apiVulns []apiV1.Vulnerability) []*v1.Vulnerability {
vulns := make([]*v1.Vulnerability, 0, len(apiVulns))
for _, v := range apiVulns {
metadata, err := convert.MetadataMap(v.Metadata)
if err != nil {
log.Errorf("error converting metadata map: %v", err)
continue
}
if metadata == nil {
log.Warnf("metadata is nil for %s; skipping...", v.Name)
continue
}

vulns = append(vulns, &v1.Vulnerability{
Name: v.Name,
Description: v.Description,
Link: v.Link,
MetadataV2: metadata,
FixedBy: v.FixedBy,
Severity: v.Severity,
})
}
return vulns
}

// ConvertFeatures converts api Features into v1 (proto) Feature pointers.
func ConvertFeatures(apiFeatures []apiV1.Feature) []*v1.Feature {
features := make([]*v1.Feature, 0, len(apiFeatures))
for _, a := range apiFeatures {
vulns := convertVulnerabilities(a.Vulnerabilities)

features = append(features, &v1.Feature{
Name: a.Name,
Version: a.Version,
FeatureType: a.VersionFormat,
AddedByLayer: a.AddedBy,
Location: a.Location,
Vulnerabilities: vulns,
FixedBy: a.FixedBy,
ProvidedExecutables: a.Executables,
})
}
return features
}

func convertLanguageLevelComponents(layersToComponents []*component.LayerToComponents) map[string]*v1.LanguageLevelComponents {
converted := make(map[string]*v1.LanguageLevelComponents, len(layersToComponents))
for _, layerToComponents := range layersToComponents {
Expand Down
17 changes: 4 additions & 13 deletions api/v1/imagescan/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
apiGRPC "github.com/stackrox/scanner/api/grpc"
apiV1 "github.com/stackrox/scanner/api/v1"
"github.com/stackrox/scanner/api/v1/common"
"github.com/stackrox/scanner/api/v1/features"
"github.com/stackrox/scanner/cpe/nvdtoolscache"
"github.com/stackrox/scanner/database"
v1 "github.com/stackrox/scanner/generated/scanner/api/v1"
Expand Down Expand Up @@ -91,7 +92,7 @@ func (s *serviceImpl) GetImageScan(_ context.Context, req *v1.GetImageScanReques
Status: v1.ScanStatus_SUCCEEDED,
Image: &v1.Image{
Namespace: layer.NamespaceName,
Features: ConvertFeatures(layer.Features),
Features: features.ConvertFeatures(layer.Features),
},
Notes: convertNotes(notes),
}, nil
Expand Down Expand Up @@ -206,7 +207,7 @@ func (s *serviceImpl) getImageComponents(ctx context.Context, req *v1.GetImageCo
}

func (s *serviceImpl) GetImageVulnerabilities(_ context.Context, req *v1.GetImageVulnerabilitiesRequest) (*v1.GetImageVulnerabilitiesResponse, error) {
layer, err := apiV1.GetVulnerabilitiesForComponents(s.db, req.GetComponents(), hasUncertifiedRHEL(req.GetNotes()))
layer, err := apiV1.GetVulnerabilitiesForComponents(s.db, req.GetComponents(), common.HasUncertifiedRHEL(req.GetNotes()))
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
Expand All @@ -216,21 +217,11 @@ func (s *serviceImpl) GetImageVulnerabilities(_ context.Context, req *v1.GetImag
Status: v1.ScanStatus_SUCCEEDED,
Image: &v1.Image{
Namespace: layer.NamespaceName,
Features: ConvertFeatures(layer.Features),
Features: features.ConvertFeatures(layer.Features),
},
}, nil
}

func hasUncertifiedRHEL(notes []v1.Note) bool {
for _, note := range notes {
if note == v1.Note_CERTIFIED_RHEL_SCAN_UNAVAILABLE {
return true
}
}

return false
}

func (s *serviceImpl) GetLanguageLevelComponents(_ context.Context, req *v1.GetLanguageLevelComponentsRequest) (*v1.GetLanguageLevelComponentsResponse, error) {
layerName, lineage, err := s.getLayerNameFromImageReq(req)
if err != nil {
Expand Down
15 changes: 13 additions & 2 deletions api/v1/nodescan/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
log "github.com/sirupsen/logrus"
"github.com/stackrox/rox/pkg/stringutils"
apiGRPC "github.com/stackrox/scanner/api/grpc"
apiV1 "github.com/stackrox/scanner/api/v1"
"github.com/stackrox/scanner/api/v1/common"
"github.com/stackrox/scanner/api/v1/convert"
"github.com/stackrox/scanner/api/v1/features"
"github.com/stackrox/scanner/cpe/nvdtoolscache"
"github.com/stackrox/scanner/database"
"github.com/stackrox/scanner/ext/kernelparser"
Expand Down Expand Up @@ -247,10 +250,10 @@ func (s *serviceImpl) GetNodeVulnerabilities(_ context.Context, req *v1.GetNodeV
case nil: // Normal
case kernelparser.ErrNodeUnsupported:
// The node is unsupported, exit early.
resp.Notes = append(resp.Notes, v1.NodeNote_NODE_UNSUPPORTED)
resp.NodeNotes = append(resp.GetNodeNotes(), v1.NodeNote_NODE_UNSUPPORTED)
return resp, nil
case kernelparser.ErrKernelUnsupported:
resp.Notes = append(resp.Notes, v1.NodeNote_NODE_KERNEL_UNSUPPORTED)
resp.NodeNotes = append(resp.GetNodeNotes(), v1.NodeNote_NODE_KERNEL_UNSUPPORTED)
default:
return nil, status.Error(codes.Internal, err.Error())
}
Expand All @@ -270,6 +273,14 @@ func (s *serviceImpl) GetNodeVulnerabilities(_ context.Context, req *v1.GetNodeV
return nil, status.Error(codes.Internal, err.Error())
}

// Handle the new format of the request and scan node inventory additionally
if req.GetComponents() != nil {
layer, err := apiV1.GetVulnerabilitiesForComponents(s.db, req.GetComponents(), common.HasUncertifiedRHEL(req.GetNotes()))
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
resp.Features = features.ConvertFeatures(layer.Features)
}
return resp, nil
}

Expand Down
6 changes: 3 additions & 3 deletions e2etests/grpc_full_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"sort"
"testing"

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

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

for _, feature := range imagescan.ConvertFeatures(test.unexpectedFeatures) {
for _, feature := range features.ConvertFeatures(test.unexpectedFeatures) {
assert.Nil(t, getMatchingGRPCFeature(t, imgScan.Features, feature, true))
}
}
Expand Down
13 changes: 2 additions & 11 deletions e2etests/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

apiV1 "github.com/stackrox/scanner/api/v1"
"github.com/stackrox/scanner/api/v1/common"
v1 "github.com/stackrox/scanner/generated/scanner/api/v1"
namespaces "github.com/stackrox/scanner/pkg/wellknownnamespaces"
"github.com/stretchr/testify/assert"
Expand All @@ -34,23 +35,13 @@ func TestGRPCGetImageComponents(t *testing.T) {
require.NotNil(t, imgComponentsResp.GetStatus())

assert.Equal(t, imgComponentsResp.GetStatus(), v1.ScanStatus_SUCCEEDED, "Image %s", testCase.image)
assert.Equal(t, testCase.uncertifiedRHEL, hasUncertifiedRHEL(imgComponentsResp.GetNotes()), "Image %s", testCase.image)
assert.Equal(t, testCase.uncertifiedRHEL, common.HasUncertifiedRHEL(imgComponentsResp.GetNotes()), "Image %s", testCase.image)
assert.Equal(t, testCase.namespace, imgComponentsResp.GetComponents().GetNamespace())
verifyComponents(t, imgComponentsResp.GetComponents(), testCase)
})
}
}

func hasUncertifiedRHEL(notes []v1.Note) bool {
for _, note := range notes {
if note == v1.Note_CERTIFIED_RHEL_SCAN_UNAVAILABLE {
return true
}
}

return false
}

func verifyComponents(t *testing.T, components *v1.Components, test testCase) {
assert.True(t, len(components.RhelComponents) == 0 || len(components.OsComponents) == 0)

Expand Down
Loading