Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
11 changes: 8 additions & 3 deletions central/cve/fetcher/manager_impl_postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/stackrox/rox/central/cve/converter/v2"
"github.com/stackrox/rox/central/cve/matcher"
mockImageDataStore "github.com/stackrox/rox/central/image/datastore/mocks"
mockImageV2DataStore "github.com/stackrox/rox/central/imagev2/datastore/mocks"
mockNSDataStore "github.com/stackrox/rox/central/namespace/datastore/mocks"
"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/cve"
Expand Down Expand Up @@ -133,9 +134,10 @@ func TestReconcileIstioCVEsInPostgres(t *testing.T) {
mockClusters := mockClusterDataStore.NewMockDataStore(ctrl)
mockNamespaces := mockNSDataStore.NewMockDataStore(ctrl)
mockImages := mockImageDataStore.NewMockDataStore(ctrl)
mockImagesV2 := mockImageV2DataStore.NewMockDataStore(ctrl)
mockCVEs := mockCVEDataStore.NewMockDataStore(ctrl)

cveMatcher, err := matcher.NewCVEMatcher(mockClusters, mockNamespaces, mockImages)
cveMatcher, err := matcher.NewCVEMatcher(mockClusters, mockNamespaces, mockImages, mockImagesV2)
require.NoError(t, err)

cveManager := &orchestratorIstioCVEManagerImpl{
Expand Down Expand Up @@ -404,9 +406,10 @@ func TestReconcileCVEsInPostgres(t *testing.T) {
mockClusters := mockClusterDataStore.NewMockDataStore(ctrl)
mockNamespaces := mockNSDataStore.NewMockDataStore(ctrl)
mockImages := mockImageDataStore.NewMockDataStore(ctrl)
mockImagesV2 := mockImageV2DataStore.NewMockDataStore(ctrl)
mockCVEs := mockCVEDataStore.NewMockDataStore(ctrl)

cveMatcher, err := matcher.NewCVEMatcher(mockClusters, mockNamespaces, mockImages)
cveMatcher, err := matcher.NewCVEMatcher(mockClusters, mockNamespaces, mockImages, mockImagesV2)
require.NoError(t, err)

cveManager := &orchestratorIstioCVEManagerImpl{
Expand Down Expand Up @@ -436,6 +439,7 @@ type TestClusterCVEOpsInPostgresTestSuite struct {
clusterCVEDatastore clusterCVEDataStore.DataStore
mockNamespaces *mockNSDataStore.MockDataStore
mockImages *mockImageDataStore.MockDataStore
mockImagesV2 *mockImageV2DataStore.MockDataStore
cveManager *orchestratorCVEManager
}

Expand All @@ -447,6 +451,7 @@ func (s *TestClusterCVEOpsInPostgresTestSuite) SetupSuite() {
// Create cluster datastore
s.mockNamespaces = mockNSDataStore.NewMockDataStore(s.mockCtrl)
s.mockImages = mockImageDataStore.NewMockDataStore(s.mockCtrl)
s.mockImagesV2 = mockImageV2DataStore.NewMockDataStore(s.mockCtrl)

// Create cluster cve datastore
clusterCVEDatastore, err := clusterCVEDataStore.GetTestPostgresDataStore(s.T(), s.testPostgres.DB)
Expand All @@ -458,7 +463,7 @@ func (s *TestClusterCVEOpsInPostgresTestSuite) SetupSuite() {
s.clusterDataStore = clusterDataStore

// Create cve manager
cveMatcher, err := matcher.NewCVEMatcher(clusterDataStore, s.mockNamespaces, s.mockImages)
cveMatcher, err := matcher.NewCVEMatcher(clusterDataStore, s.mockNamespaces, s.mockImages, s.mockImagesV2)
s.NoError(err)

s.cveManager = &orchestratorCVEManager{
Expand Down
28 changes: 21 additions & 7 deletions central/cve/matcher/matcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@ import (
clusterDataStore "github.com/stackrox/rox/central/cluster/datastore"
"github.com/stackrox/rox/central/cve/converter/utils"
imageDataStore "github.com/stackrox/rox/central/image/datastore"
imageV2DataStore "github.com/stackrox/rox/central/imagev2/datastore"
nsDataStore "github.com/stackrox/rox/central/namespace/datastore"
"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/errorhelpers"
"github.com/stackrox/rox/pkg/features"
"github.com/stackrox/rox/pkg/logging"
"github.com/stackrox/rox/pkg/search"
"github.com/stackrox/rox/pkg/set"
Expand All @@ -33,14 +35,16 @@ type CVEMatcher struct {
clusters clusterDataStore.DataStore
namespaces nsDataStore.DataStore
images imageDataStore.DataStore
imagesV2 imageV2DataStore.DataStore
}

// NewCVEMatcher returns new instance of CVEMatcher
func NewCVEMatcher(clusters clusterDataStore.DataStore, namespaces nsDataStore.DataStore, images imageDataStore.DataStore) (*CVEMatcher, error) {
func NewCVEMatcher(clusters clusterDataStore.DataStore, namespaces nsDataStore.DataStore, images imageDataStore.DataStore, imagesV2 imageV2DataStore.DataStore) (*CVEMatcher, error) {
return &CVEMatcher{
clusters: clusters,
namespaces: namespaces,
images: images,
imagesV2: imagesV2,
}, nil
}

Expand Down Expand Up @@ -183,12 +187,22 @@ func (m *CVEMatcher) getAllIstioComponentsVersionsInCluster(ctx context.Context,
AddExactMatches(search.ImageRegistry, "docker.io").
AddStrings(search.ImageRemote, "istio").
ProtoQuery()
images, err := m.images.SearchRawImages(ctx, q)
if err != nil {
return set, err
}
for _, image := range images {
set.Add(image.GetName().GetTag())
if features.FlattenImageData.Enabled() {
images, err := m.imagesV2.SearchRawImages(ctx, q)
if err != nil {
return set, err
}
for _, image := range images {
set.Add(image.GetName().GetTag())
}
} else {
images, err := m.images.SearchRawImages(ctx, q)
if err != nil {
return set, err
}
for _, image := range images {
set.Add(image.GetName().GetTag())
}
}
return set, nil
}
Expand Down
18 changes: 16 additions & 2 deletions central/cve/matcher/matcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"github.com/facebookincubator/nvdtools/cvefeed/nvd/schema"
mockClusterDataStore "github.com/stackrox/rox/central/cluster/datastore/mocks"
mockImagesDataStore "github.com/stackrox/rox/central/image/datastore/mocks"
mockImageV2DataStore "github.com/stackrox/rox/central/imagev2/datastore/mocks"
mockNamespaceDataStore "github.com/stackrox/rox/central/namespace/datastore/mocks"
"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/features"
"github.com/stackrox/rox/pkg/sac"
"github.com/stackrox/rox/pkg/sac/resources"
"github.com/stackrox/rox/pkg/search"
Expand All @@ -30,6 +32,7 @@ type cveMatcherTestSuite struct {
clusters *mockClusterDataStore.MockDataStore
namespaces *mockNamespaceDataStore.MockDataStore
images *mockImagesDataStore.MockDataStore
imagesV2 *mockImageV2DataStore.MockDataStore

mockCtrl *gomock.Controller
}
Expand All @@ -48,9 +51,10 @@ func (s *cveMatcherTestSuite) SetupTest() {
s.clusters = mockClusterDataStore.NewMockDataStore(s.mockCtrl)
s.namespaces = mockNamespaceDataStore.NewMockDataStore(s.mockCtrl)
s.images = mockImagesDataStore.NewMockDataStore(s.mockCtrl)
s.imagesV2 = mockImageV2DataStore.NewMockDataStore(s.mockCtrl)

var err error
s.cveMatcher, err = NewCVEMatcher(s.clusters, s.namespaces, s.images)
s.cveMatcher, err = NewCVEMatcher(s.clusters, s.namespaces, s.images, s.imagesV2)
s.Require().NoError(err)
}

Expand Down Expand Up @@ -746,7 +750,17 @@ func (s *cveMatcherTestSuite) TestIstioCVEImpactsCluster() {

s.clusters.EXPECT().GetClusters(gomock.Any()).Return(clusters, nil).AnyTimes()
s.namespaces.EXPECT().Search(gomock.Any(), gomock.Any()).Return(namespaces, nil).AnyTimes()
s.images.EXPECT().SearchRawImages(gomock.Any(), gomock.Any()).Return(images, nil).AnyTimes()

// Matcher uses images or imagesV2 depending on FlattenImageData.
imagesV2 := make([]*storage.ImageV2, len(images))
for i, img := range images {
imagesV2[i] = &storage.ImageV2{Id: img.GetId(), Name: img.GetName()}
}
if features.FlattenImageData.Enabled() {
s.imagesV2.EXPECT().SearchRawImages(gomock.Any(), gomock.Any()).Return(imagesV2, nil).AnyTimes()
} else {
s.images.EXPECT().SearchRawImages(gomock.Any(), gomock.Any()).Return(images, nil).AnyTimes()
}

ok, err := s.cveMatcher.isIstioControlPlaneRunning(context.Background())
s.Nil(err)
Expand Down
3 changes: 2 additions & 1 deletion central/cve/matcher/singleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package matcher
import (
clusterDataStore "github.com/stackrox/rox/central/cluster/datastore"
imageDataStore "github.com/stackrox/rox/central/image/datastore"
imageV2DataStore "github.com/stackrox/rox/central/imagev2/datastore"
nsDataStore "github.com/stackrox/rox/central/namespace/datastore"
"github.com/stackrox/rox/pkg/sync"
"github.com/stackrox/rox/pkg/utils"
Expand All @@ -15,7 +16,7 @@ var (

func initialize() {
var err error
cveMatcher, err = NewCVEMatcher(clusterDataStore.Singleton(), nsDataStore.Singleton(), imageDataStore.Singleton())
cveMatcher, err = NewCVEMatcher(clusterDataStore.Singleton(), nsDataStore.Singleton(), imageDataStore.Singleton(), imageV2DataStore.Singleton())
utils.CrashOnError(err)
}

Expand Down
4 changes: 3 additions & 1 deletion central/graphql/resolvers/vulnerabilities_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/stackrox/rox/central/cve/converter/utils"
"github.com/stackrox/rox/central/cve/matcher"
imageMocks "github.com/stackrox/rox/central/image/datastore/mocks"
imageV2Mocks "github.com/stackrox/rox/central/imagev2/datastore/mocks"
nsMocks "github.com/stackrox/rox/central/namespace/datastore/mocks"
"github.com/stackrox/rox/generated/storage"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -71,7 +72,8 @@ func TestK8sCVEEnvImpact(t *testing.T) {
clusterDataStore := clusterMocks.NewMockDataStore(ctrl)
nsDataStore := nsMocks.NewMockDataStore(ctrl)
imageDataStore := imageMocks.NewMockDataStore(ctrl)
cveMatcher, err := matcher.NewCVEMatcher(clusterDataStore, nsDataStore, imageDataStore)
imageV2DataStore := imageV2Mocks.NewMockDataStore(ctrl)
cveMatcher, err := matcher.NewCVEMatcher(clusterDataStore, nsDataStore, imageDataStore, imageV2DataStore)
require.NoError(t, err)

clusterDataStore.EXPECT().GetClusters(gomock.Any()).Return(clusters, nil).AnyTimes()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/stackrox/rox/central/vulnmgmt/vulnerabilityrequest/utils"
v1 "github.com/stackrox/rox/generated/api/v1"
"github.com/stackrox/rox/generated/storage"
"github.com/stackrox/rox/pkg/features"
"github.com/stackrox/rox/pkg/sac"
"github.com/stackrox/rox/pkg/sac/resources"
)
Expand Down Expand Up @@ -45,6 +46,9 @@ func (m *queryManagerImpl) ImageCount(ctx context.Context, requestID string, que
if err != nil {
return 0, err
}
if features.FlattenImageData.Enabled() {
return m.imageV2s.Count(ctx, query)
}
return m.images.Count(ctx, query)
}

Expand Down
1 change: 1 addition & 0 deletions pkg/images/types/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func ConvertImageToListImage(i *storage.Image) *storage.ListImage {
Name: i.GetName().GetFullName(),
Created: i.GetMetadata().GetV1().GetCreated(),
LastUpdated: i.GetLastUpdated(),
Priority: i.GetPriority(),
}
if i.GetSetComponents() != nil {
listImage.SetComponents = &storage.ListImage_Components{
Expand Down
2 changes: 2 additions & 0 deletions pkg/images/utils/convert_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func ConvertToV1(image *storage.ImageV2, names ...*storage.ImageName) *storage.I
},
Signature: image.GetSignature(),
SignatureVerificationData: image.GetSignatureVerificationData(),
BaseImageInfo: image.GetBaseImageInfo(),
}
}

Expand Down Expand Up @@ -87,6 +88,7 @@ func ConvertToV2(image *storage.Image) *storage.ImageV2 {
TopCvss: image.GetTopCvss(),
SignatureVerificationData: image.GetSignatureVerificationData(),
Signature: image.GetSignature(),
BaseImageInfo: image.GetBaseImageInfo(),
}
FillScanStatsV2(ret)
return ret
Expand Down
Loading