-
Notifications
You must be signed in to change notification settings - Fork 174
[WIP] DON'T REVIEW Jv run perf scale using comment get diagnostic bundle #19907
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
base: master
Are you sure you want to change the base?
Changes from all commits
a29c4e5
d6431ac
f611b6b
61e0dd6
010938f
e2a34b2
8497126
e1002a8
e5a6104
9c0df2f
b48eaa8
c0ac790
34d493e
850afc7
ff445cc
a071937
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ package datastore | |
|
|
||
| import ( | ||
| "errors" | ||
| "regexp" | ||
| "testing" | ||
| "time" | ||
|
|
||
|
|
@@ -26,6 +27,7 @@ import ( | |
| serviceAccountDataStoreMocks "github.com/stackrox/rox/central/serviceaccount/datastore/mocks" | ||
| "github.com/stackrox/rox/generated/internalapi/central" | ||
| "github.com/stackrox/rox/generated/storage" | ||
| clusterPkg "github.com/stackrox/rox/pkg/cluster" | ||
| "github.com/stackrox/rox/pkg/concurrency" | ||
| "github.com/stackrox/rox/pkg/features" | ||
| "github.com/stackrox/rox/pkg/fixtures/fixtureconsts" | ||
|
|
@@ -129,6 +131,7 @@ func (s *clusterDataStoreTestSuite) SetupTest() { | |
| networkBaselineMgr: s.networkBaselineMgr, | ||
| compliancePruner: s.compliancePruner, | ||
| idToNameCache: simplecache.New(), | ||
| idToRegexCache: simplecache.New(), | ||
| nameToIDCache: simplecache.New(), | ||
| } | ||
| } | ||
|
|
@@ -826,3 +829,72 @@ func (s *clusterDataStoreTestSuite) TestGetClusterLabels() { | |
| }) | ||
| } | ||
| } | ||
|
|
||
| func (s *clusterDataStoreTestSuite) TestProcessMatching() { | ||
| clusterID := fixtureconsts.Cluster1 | ||
| testCluster := &storage.Cluster{ | ||
| Id: clusterID, | ||
| Name: "test", | ||
| ManagedBy: storage.ManagerType_MANAGER_TYPE_HELM_CHART, | ||
| HelmConfig: &storage.CompleteClusterConfig{ | ||
| DynamicConfig: &storage.DynamicClusterConfig{ | ||
| RuntimeDataControl: &storage.DynamicClusterConfig_RuntimeDataControl{ | ||
| NamespaceFilter: "test-.*", | ||
|
Comment on lines
+833
to
+842
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Consider broadening TestProcessMatching to cover clusters without runtimeDataControl and non-Helm-managed clusters Currently this only exercises regex matching for a Helm-managed cluster with a non-nil
This will validate matching behavior across the main cluster management/configuration variants. Suggested implementation: func (s *clusterDataStoreTestSuite) TestProcessMatching() {
ctx := context.Background()
helmClusterID := fixtureconsts.Cluster1
manualClusterID := fixtureconsts.Cluster2
// Helm-managed cluster with non-nil RuntimeDataControl; Regex should match "test-.*" namespaces.
helmCluster := &storage.Cluster{
Id: helmClusterID,
Name: "helm-cluster",
ManagedBy: storage.ManagerType_MANAGER_TYPE_HELM_CHART,
HelmConfig: &storage.CompleteClusterConfig{
DynamicConfig: &storage.DynamicClusterConfig{
RuntimeDataControl: &storage.DynamicClusterConfig_RuntimeDataControl{
NamespaceFilter: "test-.*",
Persistence: true,
},
},
},
}
// Helm-managed cluster with nil DynamicConfig/RuntimeDataControl; should take the default path
// and return "not matched" without error.
nilRuntimeControlCluster := &storage.Cluster{
Id: uuid.NewV4().String(),
Name: "no-runtime-control",
ManagedBy: storage.ManagerType_MANAGER_TYPE_HELM_CHART,
HelmConfig: &storage.CompleteClusterConfig{
// DynamicConfig intentionally nil.
},
}
// MANUAL-managed cluster to exercise non-Helm cache path and namespace filter source.
// For these tests we rely on the default behavior (no RuntimeDataControl) and verify that
// we still get a deterministic non-error result.
manualCluster := &storage.Cluster{
Id: manualClusterID,
Name: "manual-cluster",
ManagedBy: storage.ManagerType_MANAGER_TYPE_MANUAL,
// No HelmConfig – this should force the datastore down the MANUAL-management path.
}
// Upsert clusters so MatchProcessIndicator can look them up.
require.NoError(s.T(), s.datastore.UpsertCluster(ctx, helmCluster))
require.NoError(s.T(), s.datastore.UpsertCluster(ctx, nilRuntimeControlCluster))
require.NoError(s.T(), s.datastore.UpsertCluster(ctx, manualCluster))
// Process indicators to exercise regex matching and default behavior.
matchingIndicator := &storage.ProcessIndicator{
ClusterId: helmClusterID,
Namespace: "test-namespace",
PodId: "pod-1",
Signal: &storage.ProcessSignal{
ContainerId: "container-1",
Name: "nginx",
},
}
nonMatchingIndicator := &storage.ProcessIndicator{
ClusterId: helmClusterID,
Namespace: "prod-namespace",
PodId: "pod-2",
Signal: &storage.ProcessSignal{
ContainerId: "container-2",
Name: "nginx",
},
}
nilRuntimeControlIndicator := &storage.ProcessIndicator{
ClusterId: nilRuntimeControlCluster.Id,
Namespace: "any-namespace",
PodId: "pod-3",
Signal: &storage.ProcessSignal{
ContainerId: "container-3",
Name: "redis",
},
}
manualClusterIndicator := &storage.ProcessIndicator{
ClusterId: manualClusterID,
Namespace: "manual-namespace",
PodId: "pod-4",
Signal: &storage.ProcessSignal{
ContainerId: "container-4",
Name: "busybox",
},
}
s.Run("helm cluster with matching namespace filter", func() {
matched, err := s.datastore.MatchProcessIndicator(ctx, matchingIndicator)
require.NoError(s.T(), err)
assert.True(s.T(), matched, "expected helm cluster regex to match namespace")
})
s.Run("helm cluster with non-matching namespace filter", func() {
matched, err := s.datastore.MatchProcessIndicator(ctx, nonMatchingIndicator)
require.NoError(s.T(), err)
assert.False(s.T(), matched, "expected helm cluster regex NOT to match namespace")
})
s.Run("helm cluster with nil RuntimeDataControl", func() {
matched, err := s.datastore.MatchProcessIndicator(ctx, nilRuntimeControlIndicator)
require.NoError(s.T(), err, "expected default behavior without error when RuntimeDataControl is nil")
assert.False(s.T(), matched, "expected default behavior to return not-matched when RuntimeDataControl is nil")
})
s.Run("manual-managed cluster", func() {
// First call exercises initial cache population / lookup path.
matched1, err := s.datastore.MatchProcessIndicator(ctx, manualClusterIndicator)
require.NoError(s.T(), err)
// We don't assert a specific match policy here, only that the behavior is consistent and non-erroring.
// For the current implementation we expect "false" when no RuntimeDataControl is present.
assert.False(s.T(), matched1, "expected manual-managed cluster to return not-matched by default")
// Second call should hit any internal cache path, and must behave identically.
matched2, err := s.datastore.MatchProcessIndicator(ctx, manualClusterIndicator)
require.NoError(s.T(), err)
assert.Equal(s.T(), matched1, matched2, "manual-managed cluster matching should be stable across cache hits")
})The edit assumes the following, which you may need to adjust to your existing code:
|
||
| Persistence: true, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| ctx := sac.WithAllAccess(s.T().Context()) | ||
|
|
||
| // populate regexp cache | ||
| s.datastore.idToRegexCache.Add(clusterID, | ||
| regexp.MustCompile(clusterPkg.GetNamespaceFilter(testCluster))) | ||
|
|
||
| indicator := &storage.ProcessIndicator{ | ||
| Id: uuid.NewV4().String(), | ||
| DeploymentId: uuid.NewV4().String(), | ||
| ContainerName: uuid.NewV4().String(), | ||
| PodId: uuid.NewV4().String(), | ||
| ClusterId: clusterID, | ||
| Namespace: "test-namespace", | ||
| } | ||
|
|
||
| // The process with matching namespace should be found | ||
| match, err := s.datastore.MatchProcessIndicator(ctx, indicator) | ||
| s.NoError(err) | ||
| assert.True(s.T(), match) | ||
|
|
||
| // We change the namespace to not match anymore, and the process passes | ||
| indicator.Namespace = "other-namespace" | ||
| match, err = s.datastore.MatchProcessIndicator(ctx, indicator) | ||
| s.NoError(err) | ||
| assert.False(s.T(), match) | ||
|
|
||
| // We change the namespace to match the openshift pattern, | ||
| // and ask to exclude it | ||
| indicator.Namespace = "openshift-test" | ||
| testCluster.HelmConfig.DynamicConfig.RuntimeDataControl.ExcludeOpenshift = true | ||
|
|
||
| // We need to update the cache to take persistence into account | ||
| s.datastore.idToRegexCache.Add(clusterID, | ||
| regexp.MustCompile(clusterPkg.GetNamespaceFilter(testCluster))) | ||
|
|
||
| match, err = s.datastore.MatchProcessIndicator(ctx, indicator) | ||
| s.NoError(err) | ||
| assert.True(s.T(), match) | ||
|
|
||
| // No matter how the namespace looks like, if no persistence is requested, | ||
| // the process will match | ||
| indicator.Namespace = "something-completely-different" | ||
| testCluster.HelmConfig.DynamicConfig.RuntimeDataControl.Persistence = false | ||
|
|
||
| // We need to update the cache to take persistence into account | ||
| s.datastore.idToRegexCache.Add(clusterID, | ||
| regexp.MustCompile(clusterPkg.GetNamespaceFilter(testCluster))) | ||
|
|
||
| match, err = s.datastore.MatchProcessIndicator(ctx, indicator) | ||
| s.NoError(err) | ||
| assert.True(s.T(), match) | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -128,7 +128,7 @@ func (m *managerImpl) buildIndicatorFilter() { | |
| } | ||
|
|
||
| log.Infof("Cleaning up %d processes as a part of building process filter", len(processesToRemove)) | ||
| if err := m.processesDataStore.RemoveProcessIndicators(ctx, processesToRemove); err != nil { | ||
| if err := m.processesDataStore.RemoveProcessIndicators(ctx, processesToRemove, processIndicatorDatastore.RemovalReasonProcessFilter); err != nil { | ||
| utils.Should(errors.Wrap(err, "error removing process indicators")) | ||
| } | ||
| log.Infof("Successfully cleaned up those %d processes", len(processesToRemove)) | ||
|
|
@@ -209,7 +209,8 @@ func (m *managerImpl) autoLockProcessBaselines(baselines []*storage.ProcessBasel | |
| } | ||
| } | ||
|
|
||
| // Perhaps the cluster config should be kept in memory and calling the database should not be needed | ||
| // Lifecycle manager uses cachedStorage for cluster datastore, | ||
| // thus repeated calls are memoized | ||
| func (m *managerImpl) isAutoLockEnabledForCluster(clusterId string) bool { | ||
| if !features.AutoLockProcessBaselines.Enabled() { | ||
| return false | ||
|
|
@@ -252,7 +253,15 @@ func (m *managerImpl) flushIndicatorQueue() { | |
| if m.deletedDeploymentsCache.Contains(indicator.GetDeploymentId()) { | ||
| continue | ||
| } | ||
| indicatorSlice = append(indicatorSlice, indicator) | ||
|
|
||
| match, err := m.clusterDataStore.MatchProcessIndicator(lifecycleMgrCtx, indicator) | ||
| if err != nil { | ||
| log.Errorf("Cannot match indicator %+v: %v", indicator, err) | ||
| } else if !match { | ||
| indicatorSlice = append(indicatorSlice, indicator) | ||
| } else { | ||
|
Comment on lines
+257
to
+262
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🚨 suggestion (security): Reduce verbose logging of full process indicators in the hot path This branch logs the full Suggested implementation: } else {
log.Debugf("Process indicator filtered out (cluster=%s, namespace=%s, deployment=%s, execPath=%s)",
indicator.GetClusterId(),
indicator.GetNamespace(),
indicator.GetDeploymentId(),
indicator.GetSignal().GetExecFilePath())
}You may need to adjust the field accessors used in the debug log depending on the actual
|
||
| log.Infof("Process Indicator doesn't match %+v", indicator) | ||
| } | ||
| } | ||
|
|
||
| // Index the process indicators in batch | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,16 +7,20 @@ import ( | |
|
|
||
| "github.com/pkg/errors" | ||
| clusterDataStoreMocks "github.com/stackrox/rox/central/cluster/datastore/mocks" | ||
| "github.com/stackrox/rox/central/deployment/cache" | ||
| queueMocks "github.com/stackrox/rox/central/deployment/queue/mocks" | ||
| alertManagerMocks "github.com/stackrox/rox/central/detection/alertmanager/mocks" | ||
| processBaselineDataStoreMocks "github.com/stackrox/rox/central/processbaseline/datastore/mocks" | ||
| processIndicatorsDataStoreMocks "github.com/stackrox/rox/central/processindicator/datastore/mocks" | ||
| piFilter "github.com/stackrox/rox/central/processindicator/filter" | ||
| reprocessorMocks "github.com/stackrox/rox/central/reprocessor/mocks" | ||
| connectionMocks "github.com/stackrox/rox/central/sensor/service/connection/mocks" | ||
| "github.com/stackrox/rox/generated/storage" | ||
| "github.com/stackrox/rox/pkg/env" | ||
| "github.com/stackrox/rox/pkg/features" | ||
| "github.com/stackrox/rox/pkg/fixtures" | ||
| "github.com/stackrox/rox/pkg/fixtures/fixtureconsts" | ||
| "github.com/stackrox/rox/pkg/process/filter" | ||
| "github.com/stackrox/rox/pkg/protoassert" | ||
| "github.com/stackrox/rox/pkg/protocompat" | ||
| "github.com/stackrox/rox/pkg/set" | ||
|
|
@@ -82,6 +86,8 @@ type ManagerTestSuite struct { | |
| mockCtrl *gomock.Controller | ||
| connectionManager *connectionMocks.MockManager | ||
| cluster *clusterDataStoreMocks.MockDataStore | ||
| indicators *processIndicatorsDataStoreMocks.MockDataStore | ||
| filter filter.Filter | ||
| } | ||
|
|
||
| func (suite *ManagerTestSuite) SetupTest() { | ||
|
|
@@ -93,6 +99,8 @@ func (suite *ManagerTestSuite) SetupTest() { | |
| suite.deploymentObservationQueue = queueMocks.NewMockDeploymentObservationQueue(suite.mockCtrl) | ||
| suite.connectionManager = connectionMocks.NewMockManager(suite.mockCtrl) | ||
| suite.cluster = clusterDataStoreMocks.NewMockDataStore(suite.mockCtrl) | ||
| suite.filter = piFilter.Singleton() | ||
| suite.indicators = processIndicatorsDataStoreMocks.NewMockDataStore(suite.mockCtrl) | ||
|
|
||
| suite.manager = &managerImpl{ | ||
| baselines: suite.baselines, | ||
|
|
@@ -101,6 +109,10 @@ func (suite *ManagerTestSuite) SetupTest() { | |
| deploymentObservationQueue: suite.deploymentObservationQueue, | ||
| connectionManager: suite.connectionManager, | ||
| clusterDataStore: suite.cluster, | ||
| processFilter: suite.filter, | ||
| processesDataStore: suite.indicators, | ||
| queuedIndicators: make(map[string]*storage.ProcessIndicator), | ||
| deletedDeploymentsCache: cache.DeletedDeploymentsSingleton(), | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -331,3 +343,45 @@ func (suite *ManagerTestSuite) TestAutoLockProcessBaselinesNoCluster() { | |
| enabled := suite.manager.isAutoLockEnabledForCluster(clusterId) | ||
| suite.False(enabled) | ||
| } | ||
|
|
||
| func (suite *ManagerTestSuite) TestFlushIndicators() { | ||
| _, indicator1 := makeIndicator() | ||
| _, indicator2 := makeIndicator() | ||
|
|
||
| // Make first indicator to match and be filtered out | ||
| suite.cluster.EXPECT().MatchProcessIndicator(gomock.Any(), indicator1). | ||
| Return(true, nil) | ||
|
|
||
| // The second indicator should pass through | ||
| suite.cluster.EXPECT().MatchProcessIndicator(gomock.Any(), indicator2). | ||
|
Comment on lines
+347
to
+356
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion (testing): Add tests for error and edge paths in flushIndicatorQueue (MatchProcessIndicator errors and empty queue) Right now this only validates the happy path. Please also add tests for:
These will better cover the new filtering behavior in the lifecycle manager. Suggested implementation: func (suite *ManagerTestSuite) TestFlushIndicators() {
deploymentID1, indicator1 := makeIndicator()
deploymentID2, indicator2 := makeIndicator()
// Queue both indicators.
suite.manager.queuedIndicators[deploymentID1] = indicator1
suite.manager.queuedIndicators[deploymentID2] = indicator2
// Make first indicator to match and be filtered out.
suite.cluster.EXPECT().MatchProcessIndicator(gomock.Any(), indicator1).
Return(true, nil)
// The second indicator should pass through.
suite.cluster.EXPECT().MatchProcessIndicator(gomock.Any(), indicator2).
Return(false, nil)
// Only the second indicator should be persisted.
suite.indicators.EXPECT().
AddProcessIndicators(gomock.Any(), []*storage.ProcessIndicator{indicator2}).
Return(nil)
suite.manager.flushIndicatorQueue(context.Background())
}
func (suite *ManagerTestSuite) TestFlushIndicators_MatchError() {
deploymentID, indicator := makeIndicator()
// Queue the indicator.
suite.manager.queuedIndicators[deploymentID] = indicator
// Simulate a matching error for this indicator.
suite.cluster.EXPECT().
MatchProcessIndicator(gomock.Any(), indicator).
Return(false, errors.New("match error"))
// On error, the indicator should still be persisted so it isn't lost.
suite.indicators.EXPECT().
AddProcessIndicators(gomock.Any(), []*storage.ProcessIndicator{indicator}).
Return(nil)
suite.manager.flushIndicatorQueue(context.Background())
}
func (suite *ManagerTestSuite) TestFlushIndicators_EmptyQueue() {
// Start with an empty queue to exercise the edge case.
suite.manager.queuedIndicators = make(map[string]*storage.ProcessIndicator)
// No indicators should be written when the queue is empty.
suite.indicators.EXPECT().
AddProcessIndicators(gomock.Any(), gomock.Any()).
Times(0)
// Should not panic and should not attempt to persist indicators.
suite.manager.flushIndicatorQueue(context.Background())
}
|
||
| Return(false, nil) | ||
|
|
||
| // The second indicator will cause reading of the corresponding baseline... | ||
| suite.baselines.EXPECT().GetProcessBaseline(gomock.Any(), gomock.Any()). | ||
| Return(nil, false, nil) | ||
|
|
||
| // ... as well as new deployment in the observation queue ... | ||
| suite.deploymentObservationQueue.EXPECT(). | ||
| InObservation(gomock.Any()) | ||
|
|
||
| // ... and update of the corresponding baseline | ||
| suite.baselines.EXPECT().UpsertProcessBaseline( | ||
| gomock.Any(), gomock.Any(), gomock.Any(), true, true). | ||
| Return(nil, nil) | ||
|
|
||
| // Only the second indicator is getting stored | ||
| suite.indicators.EXPECT(). | ||
| AddProcessIndicators(gomock.Any(), []*storage.ProcessIndicator{indicator2}) | ||
|
|
||
| // Unfortunately it's not easy to test new indicators in the lifecycle | ||
| // manager at the higher level, since flushIndicatorQueue is executed in a | ||
| // separate go routine, which makes mock expectation checking complicated | ||
| // (we have to somehow wait for this routine to end). Thus we test at | ||
| // flushIndicatorQueue level, and manually prepare the indicator queue. | ||
| // | ||
| // TODO: Is it possible to incorporate testing/synctest here on top of testify? | ||
| suite.manager.queuedIndicators[indicator1.GetId()] = indicator1 | ||
| suite.manager.queuedIndicators[indicator2.GetId()] = indicator2 | ||
|
|
||
| suite.manager.flushIndicatorQueue() | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Consider what regex is stored when no runtime data control is configured to avoid over-filtering
GetNamespaceFilter’s return value is compiled and cached here. If it returns an empty string,regexp.MustCompile("")matches all namespaces, and withMatchProcessIndicator’s “exclude on match” behavior this would suppress all indicators when runtime data control is unset/defaulted.This logic therefore depends on
GetNamespaceFilterreturning a “match nothing” pattern (or similar) when no filtering is desired; otherwise the cache causes unintended global suppression tied to the default behavior ofGetNamespaceFilter.