Skip to content

Commit 037aa91

Browse files
committed
Reduced usage of NetworkInfo
1 parent d3c4a64 commit 037aa91

File tree

8 files changed

+238
-161
lines changed

8 files changed

+238
-161
lines changed

integration-tests/pkg/mock_sensor/expect_conn.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ import (
99

1010
collectorAssert "github.com/stackrox/collector/integration-tests/pkg/assert"
1111
"github.com/stackrox/collector/integration-tests/pkg/types"
12+
13+
sensorAPI "github.com/stackrox/rox/generated/internalapi/sensor"
1214
)
1315

1416
// ExpectConnections waits up to the timeout for the gRPC server to receive
1517
// the list of expected Connections. It will first check to see if the connections
1618
// have been received already, and then monitor the live feed of connections
1719
// until timeout or until all the events have been received.
18-
func (s *MockSensor) ExpectConnections(t *testing.T, containerID string, timeout time.Duration, expected ...types.NetworkInfo) bool {
20+
func (s *MockSensor) ExpectConnections(t *testing.T, containerID string, timeout time.Duration, expected ...*sensorAPI.NetworkConnection) bool {
1921

20-
to_find := funk.Filter(expected, func(x types.NetworkInfo) bool {
22+
to_find := funk.Filter(expected, func(x *sensorAPI.NetworkConnection) bool {
2123
return !s.HasConnection(containerID, x)
2224
}).([]types.NetworkInfo)
2325

@@ -39,7 +41,7 @@ loop:
3941
continue loop
4042
}
4143

42-
to_find = funk.Filter(expected, func(x types.NetworkInfo) bool {
44+
to_find = funk.Filter(expected, func(x *sensorAPI.NetworkConnection) bool {
4345
return !s.HasConnection(containerID, x)
4446
}).([]types.NetworkInfo)
4547

@@ -57,7 +59,7 @@ loop:
5759
//
5860
// It does not consider the content of the events, just that a certain number
5961
// have been received
60-
func (s *MockSensor) ExpectConnectionsN(t *testing.T, containerID string, timeout time.Duration, n int) []types.NetworkInfo {
62+
func (s *MockSensor) ExpectConnectionsN(t *testing.T, containerID string, timeout time.Duration, n int) []*sensorAPI.NetworkConnection {
6163
if len(s.Connections(containerID)) == n {
6264
return s.Connections(containerID)
6365
}
@@ -82,11 +84,11 @@ loop:
8284
// ExpectSameElementsConnections compares a list of expected connections to the observed connections. This comparison is done at the beginning, when a new
8385
// connection arrives, and after a timeout period. The number of connections must match and the expected and observed connections must match, but the order
8486
// does not matter.
85-
func (s *MockSensor) ExpectSameElementsConnections(t *testing.T, containerID string, timeout time.Duration, expected ...types.NetworkInfo) bool {
87+
func (s *MockSensor) ExpectSameElementsConnections(t *testing.T, containerID string, timeout time.Duration, expected ...*sensorAPI.NetworkConnection) bool {
8688
types.SortConnections(expected)
8789

88-
equal := func(c1, c2 types.NetworkInfo) bool {
89-
return c1.Equal(c2)
90+
equal := func(c1, c2 *sensorAPI.NetworkConnection) bool {
91+
return types.Equal(c1, c2)
9092
}
9193

9294
connections := s.Connections(containerID)
@@ -123,7 +125,7 @@ func (s *MockSensor) ExpectSameElementsConnectionsScrapes(t *testing.T, containe
123125
types.SortConnections(c2)
124126

125127
for i := range c2 {
126-
if !c1[i].Equal(c2[i]) {
128+
if !types.Equal(c1[i], c2[i]) {
127129
return false
128130
}
129131
}

integration-tests/pkg/mock_sensor/server.go

Lines changed: 11 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@ func (m *MockSensor) GetConnectionsInBatches(containerID string) []types.Network
167167

168168
// Connections returns a list of all connections that have been received for
169169
// a given container ID
170-
func (m *MockSensor) Connections(containerID string) []types.NetworkInfo {
170+
func (m *MockSensor) Connections(containerID string) []*sensorAPI.NetworkConnection {
171171
m.networkMutex.Lock()
172172
defer m.networkMutex.Unlock()
173173

174-
allConns := make([]types.NetworkInfo, 0)
174+
allConns := make([]*sensorAPI.NetworkConnection, 0)
175175
if connections, ok := m.connections[containerID]; ok {
176176
conns := make([]types.NetworkInfoBatch, len(connections))
177177
copy(conns, connections)
@@ -183,16 +183,16 @@ func (m *MockSensor) Connections(containerID string) []types.NetworkInfo {
183183

184184
return allConns
185185
}
186-
return make([]types.NetworkInfo, 0)
186+
return make([]*sensorAPI.NetworkConnection, 0)
187187
}
188188

189189
// HasConnection returns whether a given connection has been seen for a given
190190
// container ID
191-
func (m *MockSensor) HasConnection(containerID string, conn types.NetworkInfo) bool {
191+
func (m *MockSensor) HasConnection(containerID string, conn *sensorAPI.NetworkConnection) bool {
192192
conns := m.Connections(containerID)
193193
if len(conns) > 0 {
194-
return slices.ContainsFunc(conns, func(c types.NetworkInfo) bool {
195-
return c.Equal(conn)
194+
return slices.ContainsFunc(conns, func(c *sensorAPI.NetworkConnection) bool {
195+
return types.Equal(c, conn)
196196
})
197197
}
198198

@@ -348,30 +348,15 @@ func (m *MockSensor) PushSignals(stream sensorAPI.SignalService_PushSignalsServe
348348
}
349349
}
350350

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)
351+
func (m *MockSensor) convertToContainerConnsMap(connections []*sensorAPI.NetworkConnection) map[string][]*sensorAPI.NetworkConnection {
352+
containerConnsMap := make(map[string][]*sensorAPI.NetworkConnection)
367353
for _, connection := range connections {
368-
conn := m.convertConnection(connection)
369354
containerID := connection.GetContainerId()
370355

371356
if c, ok := containerConnsMap[containerID]; ok {
372-
containerConnsMap[containerID] = append(c, conn)
357+
containerConnsMap[containerID] = append(c, connection)
373358
} else {
374-
containerConnsMap[containerID] = []types.NetworkInfo{conn}
359+
containerConnsMap[containerID] = []*sensorAPI.NetworkConnection{connection}
375360
}
376361
}
377362

@@ -462,7 +447,7 @@ func (m *MockSensor) pushLineage(containerID string, process *storage.ProcessSig
462447
}
463448
}
464449

465-
func (m *MockSensor) pushConnections(containerConnsMap map[string][]types.NetworkInfo) {
450+
func (m *MockSensor) pushConnections(containerConnsMap map[string][]*sensorAPI.NetworkConnection) {
466451
m.networkMutex.Lock()
467452
defer m.networkMutex.Unlock()
468453

integration-tests/pkg/types/network.go

Lines changed: 106 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package types
22

33
import (
4-
"fmt"
4+
"net"
55
"sort"
66

77
sensorAPI "github.com/stackrox/rox/generated/internalapi/sensor"
@@ -20,7 +20,7 @@ type NetworkInfo struct {
2020
CloseTimestamp string
2121
}
2222

23-
type NetworkInfoBatch []NetworkInfo
23+
type NetworkInfoBatch []*sensorAPI.NetworkConnection
2424

2525
// TranslateAddress is a helper function for converting binary representations
2626
// of network addresses (in the signals) to usable forms for testing
@@ -52,52 +52,127 @@ func TranslateAddress(addr *sensorAPI.NetworkAddress) string {
5252
return peerId.String()
5353
}
5454

55-
func (n *NetworkInfo) String() string {
56-
return fmt.Sprintf("%s|%s|%s|%s|%s",
57-
n.LocalAddress,
58-
n.RemoteAddress,
59-
n.Role,
60-
n.SocketFamily,
61-
n.CloseTimestamp)
55+
func IsActive(conn *sensorAPI.NetworkConnection) bool {
56+
// no close timestamp means the connection is open, and active
57+
return conn.GetCloseTimestamp() == nil
6258
}
6359

64-
func (n *NetworkInfo) IsActive() bool {
65-
// no close timestamp means the connection is open, and active
66-
return n.CloseTimestamp == NilTimestamp
60+
func Equal(conn1 *sensorAPI.NetworkConnection, conn2 *sensorAPI.NetworkConnection) bool {
61+
return EqualNetworkAddress(conn1.LocalAddress, conn2.LocalAddress) &&
62+
EqualNetworkAddress(conn1.RemoteAddress, conn2.RemoteAddress) &&
63+
conn1.Role == conn2.Role &&
64+
conn1.SocketFamily == conn2.SocketFamily &&
65+
IsActive(conn1) == IsActive(conn2)
6766
}
6867

69-
func (n *NetworkInfo) Equal(other NetworkInfo) bool {
70-
return n.LocalAddress == other.LocalAddress &&
71-
n.RemoteAddress == other.RemoteAddress &&
72-
n.Role == other.Role &&
73-
n.SocketFamily == other.SocketFamily &&
74-
n.IsActive() == other.IsActive()
68+
func CompareBytes(b1 []byte, b2 []byte) int {
69+
if len(b1) != len(b2) {
70+
if len(b1) < len(b2) {
71+
return -1
72+
} else {
73+
return 1
74+
}
75+
}
76+
77+
for i := range b1 {
78+
if b1[i] != b2[i] {
79+
if b1[i] < b2[i] {
80+
return -1
81+
} else {
82+
return 1
83+
}
84+
}
85+
}
86+
87+
return 0
7588
}
7689

77-
func (n *NetworkInfo) Less(other NetworkInfo) bool {
78-
if n.LocalAddress != other.LocalAddress {
79-
return n.LocalAddress < other.LocalAddress
90+
func EqualNetworkAddress(addr1 *sensorAPI.NetworkAddress, addr2 *sensorAPI.NetworkAddress) bool {
91+
comp := CompareBytes(addr1.GetAddressData(), addr2.GetAddressData())
92+
93+
if comp != 0 {
94+
return false
8095
}
8196

82-
if n.RemoteAddress != other.RemoteAddress {
83-
return n.RemoteAddress < other.RemoteAddress
97+
comp = CompareBytes(addr1.GetIpNetwork(), addr2.GetIpNetwork())
98+
99+
if comp != 0 {
100+
return false
84101
}
85102

86-
if n.Role != other.Role {
87-
return n.Role < other.Role
103+
return addr1.GetPort() == addr2.GetPort()
104+
}
105+
106+
func LessNetworkAddress(addr1 *sensorAPI.NetworkAddress, addr2 *sensorAPI.NetworkAddress) bool {
107+
comp := CompareBytes(addr1.GetAddressData(), addr2.GetAddressData())
108+
109+
if comp != 0 {
110+
return comp < 0
88111
}
89112

90-
if n.SocketFamily != other.SocketFamily {
91-
return n.SocketFamily < other.SocketFamily
113+
comp = CompareBytes(addr1.GetIpNetwork(), addr2.GetIpNetwork())
114+
115+
if comp != 0 {
116+
return comp < 0
92117
}
93118

94-
if n.IsActive() != other.IsActive() {
95-
return n.IsActive()
119+
return addr1.GetPort() < addr2.GetPort()
120+
}
121+
122+
func LessNetworkConnection(conn1 *sensorAPI.NetworkConnection, conn2 *sensorAPI.NetworkConnection) bool {
123+
if !EqualNetworkAddress(conn1.LocalAddress, conn2.LocalAddress) {
124+
return LessNetworkAddress(conn1.GetLocalAddress(), conn2.GetLocalAddress())
125+
}
126+
127+
if !EqualNetworkAddress(conn1.RemoteAddress, conn2.RemoteAddress) {
128+
return LessNetworkAddress(conn1.GetRemoteAddress(), conn2.GetRemoteAddress())
129+
}
130+
131+
if conn1.Role != conn2.Role {
132+
return conn1.Role < conn2.Role
133+
}
134+
135+
if conn1.SocketFamily != conn2.SocketFamily {
136+
return conn1.SocketFamily < conn2.SocketFamily
137+
}
138+
139+
if IsActive(conn1) != IsActive(conn2) {
140+
return IsActive(conn1)
96141
}
97142

98143
return false
99144
}
100145

101-
func SortConnections(connections []NetworkInfo) {
102-
sort.Slice(connections, func(i, j int) bool { return connections[i].Less(connections[j]) })
146+
func stringToIPBytes(ipStr string) []byte {
147+
ip := net.ParseIP(ipStr)
148+
149+
if ip == nil {
150+
return nil
151+
}
152+
153+
return ip.To4()
154+
155+
}
156+
157+
func stringToIPNetworkBytes(ipStr string) []byte {
158+
ip := net.ParseIP(ipStr)
159+
160+
if ip == nil {
161+
return nil
162+
}
163+
164+
return append(ip.To4(), 32)
165+
}
166+
167+
func CreateNetworkAddress(ipAddress string, ipNetwork string, port uint32) *sensorAPI.NetworkAddress {
168+
169+
return &sensorAPI.NetworkAddress{
170+
AddressData: stringToIPBytes(ipAddress),
171+
IpNetwork: stringToIPNetworkBytes(ipNetwork),
172+
Port: port,
173+
}
174+
}
175+
176+
func SortConnections(connections []*sensorAPI.NetworkConnection) {
177+
sort.Slice(connections, func(i, j int) bool { return LessNetworkConnection(connections[i], connections[j]) })
103178
}

integration-tests/suites/base.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,8 +434,10 @@ func (s *IntegrationTestSuiteBase) getIPAddress(containerName string) (string, e
434434
return s.Executor().GetContainerIP(containerName)
435435
}
436436

437-
func (s *IntegrationTestSuiteBase) getPort(containerName string) (string, error) {
438-
return s.Executor().GetContainerPort(containerName)
437+
func (s *IntegrationTestSuiteBase) getPort(containerName string) (uint32, error) {
438+
portStr, err := s.Executor().GetContainerPort(containerName)
439+
port, _ := strconv.ParseUint(portStr, 10, 32)
440+
return uint32(port), err
439441
}
440442

441443
func (s *IntegrationTestSuiteBase) StartContainerStats() {

integration-tests/suites/process_network.go

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
"github.com/stackrox/collector/integration-tests/pkg/common"
88
"github.com/stackrox/collector/integration-tests/pkg/config"
99
"github.com/stackrox/collector/integration-tests/pkg/types"
10+
11+
sensorAPI "github.com/stackrox/rox/generated/internalapi/sensor"
1012
)
1113

1214
type ProcessNetworkTestSuite struct {
@@ -15,7 +17,7 @@ type ProcessNetworkTestSuite struct {
1517
clientIP string
1618
serverContainer string
1719
serverIP string
18-
serverPort string
20+
serverPort uint32
1921
}
2022

2123
// Launches collector
@@ -147,22 +149,22 @@ func (s *ProcessNetworkTestSuite) TestProcessLineageInfo() {
147149

148150
func (s *ProcessNetworkTestSuite) TestNetworkFlows() {
149151
s.Sensor().ExpectConnections(s.T(), s.serverContainer, 10*time.Second,
150-
types.NetworkInfo{
151-
LocalAddress: fmt.Sprintf(":%s", s.serverPort),
152-
RemoteAddress: s.clientIP,
153-
Role: "ROLE_SERVER",
154-
SocketFamily: "SOCKET_FAMILY_UNKNOWN",
155-
CloseTimestamp: types.NilTimestamp,
152+
&sensorAPI.NetworkConnection{
153+
LocalAddress: types.CreateNetworkAddress("", "", s.serverPort),
154+
RemoteAddress: types.CreateNetworkAddress(s.clientIP, "", s.serverPort),
155+
Role: sensorAPI.ClientServerRole_ROLE_SERVER,
156+
SocketFamily: sensorAPI.SocketFamily_SOCKET_FAMILY_UNKNOWN,
157+
CloseTimestamp: nil,
156158
},
157159
)
158160

159161
s.Sensor().ExpectConnections(s.T(), s.clientContainer, 10*time.Second,
160-
types.NetworkInfo{
161-
LocalAddress: "",
162-
RemoteAddress: fmt.Sprintf("%s:%s", s.serverIP, s.serverPort),
163-
Role: "ROLE_CLIENT",
164-
SocketFamily: "SOCKET_FAMILY_UNKNOWN",
165-
CloseTimestamp: types.NilTimestamp,
162+
&sensorAPI.NetworkConnection{
163+
LocalAddress: types.CreateNetworkAddress("", "", 0),
164+
RemoteAddress: types.CreateNetworkAddress(s.clientIP, "", s.serverPort),
165+
Role: sensorAPI.ClientServerRole_ROLE_CLIENT,
166+
SocketFamily: sensorAPI.SocketFamily_SOCKET_FAMILY_UNKNOWN,
167+
CloseTimestamp: nil,
166168
},
167169
)
168170
}

0 commit comments

Comments
 (0)