-
Notifications
You must be signed in to change notification settings - Fork 174
ROX-12835: Add support for NodeScanV2 to Sensor #3533
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
Changes from all commits
d6c6ccf
1aced57
7f22b5f
9bb14b4
27eeaa3
e17abf2
20aa305
f461862
94eecba
50f804b
734abac
3981b0e
91d005e
61549c7
dd052b3
cdb2492
7c3634a
7bbcff1
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 |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package nodescansv2 | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/pkg/errors" | ||
| countMetrics "github.com/stackrox/rox/central/metrics" | ||
| "github.com/stackrox/rox/central/sensor/service/common" | ||
| "github.com/stackrox/rox/central/sensor/service/pipeline" | ||
| "github.com/stackrox/rox/central/sensor/service/pipeline/reconciliation" | ||
| "github.com/stackrox/rox/generated/internalapi/central" | ||
| "github.com/stackrox/rox/pkg/logging" | ||
| "github.com/stackrox/rox/pkg/metrics" | ||
| ) | ||
|
|
||
| var ( | ||
| log = logging.LoggerForModule() | ||
| ) | ||
|
|
||
| // GetPipeline returns an instantiation of this particular pipeline | ||
| func GetPipeline() pipeline.Fragment { | ||
| return NewPipeline() | ||
| } | ||
|
|
||
| // NewPipeline returns a new instance of Pipeline. | ||
| func NewPipeline() pipeline.Fragment { | ||
| return &pipelineImpl{} | ||
| } | ||
|
|
||
| type pipelineImpl struct { | ||
| } | ||
|
|
||
| func (p *pipelineImpl) Reconcile(ctx context.Context, clusterID string, storeMap *reconciliation.StoreMap) error { | ||
| return nil | ||
| } | ||
|
|
||
| func (p *pipelineImpl) Match(msg *central.MsgFromSensor) bool { | ||
| return msg.GetEvent().GetNodeScanV2() != nil | ||
| } | ||
|
|
||
| // Run runs the pipeline template on the input and returns the output. | ||
| func (p *pipelineImpl) Run(ctx context.Context, clusterID string, msg *central.MsgFromSensor, _ common.MessageInjector) error { | ||
| defer countMetrics.IncrementResourceProcessedCounter(pipeline.ActionToOperation(msg.GetEvent().GetAction()), metrics.NodeScanV2) | ||
|
|
||
| event := msg.GetEvent() | ||
| nodeScan := event.GetNodeScanV2() | ||
| if nodeScan == nil { | ||
| return errors.Errorf("unexpected resource type %T for node scan v2", event.GetResource()) | ||
| } | ||
|
|
||
| // TODO(ROX-12240, ROX-13053): Do something meaningful with the nodeScan | ||
| log.Infof("Central received NodeScanV2: %+v", nodeScan) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func (p *pipelineImpl) OnFinish(_ string) {} |
Large diffs are not rendered by default.
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 |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package compliance | ||
|
|
||
| import ( | ||
| "github.com/stackrox/rox/generated/internalapi/central" | ||
| "github.com/stackrox/rox/generated/storage" | ||
| "github.com/stackrox/rox/pkg/concurrency" | ||
| "github.com/stackrox/rox/sensor/common" | ||
| ) | ||
|
|
||
| // NodeScanHandler is responsible for handling the arriving NodeScanV2 messages, processing then, and sending them to central | ||
| type NodeScanHandler interface { | ||
| common.SensorComponent | ||
| } | ||
|
|
||
| // NewNodeScanHandler returns a new instance of a NodeScanHandler | ||
| func NewNodeScanHandler(ch <-chan *storage.NodeScanV2) NodeScanHandler { | ||
| return &nodeScanHandlerImpl{ | ||
| nodeScans: ch, | ||
| toCentral: make(chan *central.MsgFromSensor), | ||
|
|
||
| stopC: concurrency.NewErrorSignal(), | ||
| stoppedC: concurrency.NewErrorSignal(), | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package compliance | ||
|
|
||
| import ( | ||
| "github.com/pkg/errors" | ||
| "github.com/stackrox/rox/generated/internalapi/central" | ||
| "github.com/stackrox/rox/generated/storage" | ||
| "github.com/stackrox/rox/pkg/centralsensor" | ||
| "github.com/stackrox/rox/pkg/concurrency" | ||
| ) | ||
|
|
||
| type nodeScanHandlerImpl struct { | ||
| nodeScans <-chan *storage.NodeScanV2 | ||
| toCentral chan *central.MsgFromSensor | ||
|
|
||
| stopC concurrency.ErrorSignal | ||
| stoppedC concurrency.ErrorSignal | ||
|
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. what is this used for?
Contributor
Author
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. This seems to be a leftover. |
||
| } | ||
|
|
||
| func (c *nodeScanHandlerImpl) Capabilities() []centralsensor.SensorCapability { | ||
| return []centralsensor.SensorCapability{centralsensor.NodeScanningCap} | ||
| } | ||
|
|
||
| func (c *nodeScanHandlerImpl) ResponsesC() <-chan *central.MsgFromSensor { | ||
| return c.toCentral | ||
| } | ||
|
|
||
| func (c *nodeScanHandlerImpl) Start() error { | ||
|
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. Do we want to allow multiple For example, this seems possible:
Contributor
Author
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. I investigated and this will cause a panic due to closing a channel twice (only when the feature-flag is enabled). I will provide a fix and a test for this. |
||
| // c.run closes chan toCentral on exit, so we do not want to close twice in case of a restart | ||
| if !c.stopC.IsDone() { | ||
| go c.run() | ||
| return nil | ||
| } | ||
| return errors.New("stopped handlers cannot be restarted") | ||
| } | ||
|
|
||
| func (c *nodeScanHandlerImpl) Stop(err error) { | ||
| c.stopC.SignalWithError(err) | ||
| } | ||
|
|
||
| func (c *nodeScanHandlerImpl) ProcessMessage(_ *central.MsgToSensor) error { | ||
| // This component doesn't actually process or handle any messages sent from Central to Sensor (yet). | ||
|
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. Does this imply there is a plan to add this (I'm imagining Central requesting a scan periodically as part of reprocessing or user forcing it)?
Contributor
Author
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. This is added to implement the handler interface. It does not meant that we will support anything arriving from central, but we do have such possibility of we want to :) |
||
| // It uses the sensor component so that the lifecycle (start, stop) can be handled when Sensor starts up. | ||
| return nil | ||
| } | ||
|
|
||
| func (c *nodeScanHandlerImpl) run() { | ||
| defer c.stoppedC.SignalWithError(c.stopC.Err()) | ||
|
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. It seems to me, that In such situation, I think it makes sense to
Contributor
Author
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. Got it covered! I am only not fully sure which goroutine is meant with "eliminate one forwarding goroutine" 🤔 |
||
| defer close(c.toCentral) | ||
|
|
||
| for !c.stopC.IsDone() { | ||
| select { | ||
| case <-c.stopC.Done(): | ||
| return | ||
| case scan, ok := <-c.nodeScans: | ||
| if !ok { | ||
| c.stopC.SignalWithError(errors.New("channel receiving node scans v2 is closed")) | ||
| return | ||
| } | ||
| // TODO(ROX-12943): Do something with the scan, e.g., attach NodeID | ||
| c.sendScan(scan) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (c *nodeScanHandlerImpl) sendScan(scan *storage.NodeScanV2) { | ||
| if scan != nil { | ||
|
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. up to you, but maybe we can do something like the following to remove all the indentation: |
||
| select { | ||
| case <-c.stopC.Done(): | ||
| return | ||
| case c.toCentral <- ¢ral.MsgFromSensor{ | ||
| Msg: ¢ral.MsgFromSensor_Event{ | ||
| Event: ¢ral.SensorEvent{ | ||
| Resource: ¢ral.SensorEvent_NodeScanV2{ | ||
| NodeScanV2: scan, | ||
| }, | ||
| }, | ||
| }, | ||
| }: | ||
| return | ||
|
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. do we need the
Contributor
Author
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. No, no need for them. I will remove them. |
||
| } | ||
| } | ||
| } | ||
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.
I think this is a go1.19 thing. Since we are updating the file anyway, might as well keep it, but I have also had the displeasure of dealing with this when trying to style this repo
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.
Yeah, I saw that and reverted to go 1.18