@@ -11,7 +11,6 @@ import (
11
11
"time"
12
12
13
13
sensorAPI "github.com/stackrox/rox/generated/internalapi/sensor"
14
- utils "github.com/stackrox/rox/pkg/net"
15
14
16
15
"github.com/stackrox/rox/generated/storage"
17
16
"google.golang.org/grpc"
@@ -47,7 +46,7 @@ type MockSensor struct {
47
46
processLineages map [string ]LineageMap
48
47
processMutex sync.Mutex
49
48
50
- connections map [string ][]types.NetworkInfo
49
+ connections map [string ][]types.NetworkInfoBatch
51
50
endpoints map [string ]EndpointMap
52
51
networkMutex sync.Mutex
53
52
@@ -65,7 +64,7 @@ func NewMockSensor(test string) *MockSensor {
65
64
testName : test ,
66
65
processes : make (map [string ]ProcessMap ),
67
66
processLineages : make (map [string ]LineageMap ),
68
- connections : make (map [string ][]types.NetworkInfo ),
67
+ connections : make (map [string ][]types.NetworkInfoBatch ),
69
68
endpoints : make (map [string ]EndpointMap ),
70
69
}
71
70
}
@@ -150,26 +149,48 @@ func (m *MockSensor) LiveConnections() <-chan *sensorAPI.NetworkConnection {
150
149
151
150
// Connections returns a list of all connections that have been received for
152
151
// a given container ID
153
- func (m * MockSensor ) Connections (containerID string ) []types.NetworkInfo {
152
+ func (m * MockSensor ) GetConnectionsInBatches (containerID string ) []types.NetworkInfoBatch {
154
153
m .networkMutex .Lock ()
155
154
defer m .networkMutex .Unlock ()
156
155
157
156
if connections , ok := m .connections [containerID ]; ok {
158
- conns := make ([]types.NetworkInfo , len (connections ))
157
+ conns := make ([]types.NetworkInfoBatch , len (connections ))
159
158
copy (conns , connections )
160
- types .SortConnections (conns )
159
+ for _ , conn := range conns {
160
+ types .SortConnections (conn )
161
+ }
162
+
161
163
return conns
162
164
}
165
+ return make ([]types.NetworkInfoBatch , 0 )
166
+ }
167
+
168
+ // Connections returns a list of all connections that have been received for
169
+ // a given container ID
170
+ func (m * MockSensor ) Connections (containerID string ) []types.NetworkInfo {
171
+ m .networkMutex .Lock ()
172
+ defer m .networkMutex .Unlock ()
173
+
174
+ allConns := make ([]types.NetworkInfo , 0 )
175
+ if connections , ok := m .connections [containerID ]; ok {
176
+ conns := make ([]types.NetworkInfoBatch , len (connections ))
177
+ copy (conns , connections )
178
+ for _ , conn := range conns {
179
+ allConns = append (allConns , conn ... )
180
+ }
181
+
182
+ types .SortConnections (allConns )
183
+
184
+ return allConns
185
+ }
163
186
return make ([]types.NetworkInfo , 0 )
164
187
}
165
188
166
189
// HasConnection returns whether a given connection has been seen for a given
167
190
// container ID
168
191
func (m * MockSensor ) HasConnection (containerID string , conn types.NetworkInfo ) bool {
169
- m .networkMutex .Lock ()
170
- defer m .networkMutex .Unlock ()
171
-
172
- if conns , ok := m .connections [containerID ]; ok {
192
+ conns := m .Connections (containerID )
193
+ if len (conns ) > 0 {
173
194
return slices .ContainsFunc (conns , func (c types.NetworkInfo ) bool {
174
195
return c .Equal (conn )
175
196
})
@@ -271,7 +292,7 @@ func (m *MockSensor) Stop() {
271
292
272
293
m .processes = make (map [string ]ProcessMap )
273
294
m .processLineages = make (map [string ]LineageMap )
274
- m .connections = make (map [string ][]types.NetworkInfo )
295
+ m .connections = make (map [string ][]types.NetworkInfoBatch )
275
296
m .endpoints = make (map [string ]EndpointMap )
276
297
277
298
m .processChannel .Stop ()
@@ -327,6 +348,36 @@ func (m *MockSensor) PushSignals(stream sensorAPI.SignalService_PushSignalsServe
327
348
}
328
349
}
329
350
351
+ func (m * MockSensor ) convertConnection (connection * sensorAPI.NetworkConnection ) types.NetworkInfo {
352
+ conn := types.NetworkInfo {
353
+ LocalAddress : types .TranslateAddress (connection .LocalAddress ),
354
+ RemoteAddress : types .TranslateAddress (connection .RemoteAddress ),
355
+ Role : connection .GetRole ().String (),
356
+ SocketFamily : connection .GetSocketFamily ().String (),
357
+ CloseTimestamp : connection .GetCloseTimestamp ().String (),
358
+ }
359
+
360
+ m .logger .Printf ("NetworkInfo: %s, %s\n " , connection .GetContainerId (), conn )
361
+
362
+ return conn
363
+ }
364
+
365
+ func (m * MockSensor ) convertToContainerConnsMap (connections []* sensorAPI.NetworkConnection ) map [string ][]types.NetworkInfo {
366
+ containerConnsMap := make (map [string ][]types.NetworkInfo )
367
+ for _ , connection := range connections {
368
+ conn := m .convertConnection (connection )
369
+ containerID := connection .GetContainerId ()
370
+
371
+ if c , ok := containerConnsMap [containerID ]; ok {
372
+ containerConnsMap [containerID ] = append (c , conn )
373
+ } else {
374
+ containerConnsMap [containerID ] = []types.NetworkInfo {conn }
375
+ }
376
+ }
377
+
378
+ return containerConnsMap
379
+ }
380
+
330
381
// PushNetworkConnectionInfo conforms to the Sensor API. It is here that networking
331
382
// events (connections and endpoints) are handled and stored/sent to the relevant channel
332
383
func (m * MockSensor ) PushNetworkConnectionInfo (stream sensorAPI.NetworkConnectionInfoService_PushNetworkConnectionInfoServer ) error {
@@ -345,8 +396,9 @@ func (m *MockSensor) PushNetworkConnectionInfo(stream sensorAPI.NetworkConnectio
345
396
m .endpointChannel .Write (endpoint )
346
397
}
347
398
399
+ containerConnsMap := m .convertToContainerConnsMap (connections )
400
+ m .pushConnections (containerConnsMap )
348
401
for _ , connection := range connections {
349
- m .pushConnection (connection .GetContainerId (), connection )
350
402
m .connectionChannel .Write (connection )
351
403
}
352
404
}
@@ -410,32 +462,16 @@ func (m *MockSensor) pushLineage(containerID string, process *storage.ProcessSig
410
462
}
411
463
}
412
464
413
- // pushConnection converts a connection event into the test's own structure
414
- // and stores it
415
- func (m * MockSensor ) pushConnection (containerID string , connection * sensorAPI.NetworkConnection ) {
465
+ func (m * MockSensor ) pushConnections (containerConnsMap map [string ][]types.NetworkInfo ) {
416
466
m .networkMutex .Lock ()
417
467
defer m .networkMutex .Unlock ()
418
468
419
- m .logger .Printf ("NetworkInfo: %s %s|%s|%s|%s|%s\n " ,
420
- connection .GetContainerId (),
421
- m .translateAddress (connection .GetLocalAddress ()),
422
- m .translateAddress (connection .GetRemoteAddress ()),
423
- connection .GetRole ().String (),
424
- connection .GetSocketFamily ().String (),
425
- connection .GetCloseTimestamp ().String ())
426
-
427
- conn := types.NetworkInfo {
428
- LocalAddress : m .translateAddress (connection .LocalAddress ),
429
- RemoteAddress : m .translateAddress (connection .RemoteAddress ),
430
- Role : connection .GetRole ().String (),
431
- SocketFamily : connection .GetSocketFamily ().String (),
432
- CloseTimestamp : connection .GetCloseTimestamp ().String (),
433
- }
434
-
435
- if c , ok := m .connections [containerID ]; ok {
436
- m .connections [containerID ] = append (c , conn )
437
- } else {
438
- m .connections [containerID ] = []types.NetworkInfo {conn }
469
+ for containerID , connections := range containerConnsMap {
470
+ if c , ok := m .connections [containerID ]; ok {
471
+ m .connections [containerID ] = append (c , connections )
472
+ } else {
473
+ m .connections [containerID ] = []types.NetworkInfoBatch {connections }
474
+ }
439
475
}
440
476
}
441
477
@@ -485,36 +521,6 @@ func (m *MockSensor) pushEndpoint(containerID string, endpoint *sensorAPI.Networ
485
521
}
486
522
}
487
523
488
- // translateAddress is a helper function for converting binary representations
489
- // of network addresses (in the signals) to usable forms for testing
490
- func (m * MockSensor ) translateAddress (addr * sensorAPI.NetworkAddress ) string {
491
- peerId := utils.NetworkPeerID {Port : uint16 (addr .GetPort ())}
492
- addressData := addr .GetAddressData ()
493
- if len (addressData ) > 0 {
494
- peerId .Address = utils .IPFromBytes (addressData )
495
- return peerId .String ()
496
- }
497
-
498
- // If there is no address data, this is either the source address or
499
- // IpNetwork should be set and represent a CIDR block or external IP address.
500
- ipNetworkData := addr .GetIpNetwork ()
501
- if len (ipNetworkData ) == 0 {
502
- return peerId .String ()
503
- }
504
-
505
- ipNetwork := utils .IPNetworkFromCIDRBytes (ipNetworkData )
506
- prefixLen := ipNetwork .PrefixLen ()
507
- // If this is IPv4 and the prefix length is 32 or this is IPv6 and the prefix length
508
- // is 128 this is a regular IP address and not a CIDR block
509
- if (ipNetwork .Family () == utils .IPv4 && prefixLen == byte (32 )) ||
510
- (ipNetwork .Family () == utils .IPv6 && prefixLen == byte (128 )) {
511
- peerId .Address = ipNetwork .IP ()
512
- } else {
513
- peerId .IPNetwork = ipNetwork
514
- }
515
- return peerId .String ()
516
- }
517
-
518
524
func (m * MockSensor ) SetTestName (testName string ) {
519
525
m .testName = testName
520
526
}
0 commit comments