Skip to content

Commit b465bf6

Browse files
committed
Using EqualVT from generated code rather than own function
1 parent 23de1bb commit b465bf6

File tree

7 files changed

+92
-78
lines changed

7 files changed

+92
-78
lines changed

integration-tests/pkg/mock_sensor/expect_conn.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (s *MockSensor) ExpectSameElementsConnections(t *testing.T, containerID str
8888
types.SortConnections(expected)
8989

9090
equal := func(c1, c2 *sensorAPI.NetworkConnection) bool {
91-
return types.EqualNetworkConnection(c1, c2)
91+
return types.EqualNetworkConnection(*c1, *c2)
9292
}
9393

9494
connections := s.Connections(containerID)
@@ -125,7 +125,7 @@ func (s *MockSensor) ExpectSameElementsConnectionsScrapes(t *testing.T, containe
125125
types.SortConnections(c2)
126126

127127
for i := range c2 {
128-
if !types.EqualNetworkConnection(c1[i], c2[i]) {
128+
if !types.EqualNetworkConnection(*c1[i], *c2[i]) {
129129
return false
130130
}
131131
}

integration-tests/pkg/mock_sensor/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func (m *MockSensor) HasConnection(containerID string, conn *sensorAPI.NetworkCo
192192
conns := m.Connections(containerID)
193193
if len(conns) > 0 {
194194
return slices.ContainsFunc(conns, func(c *sensorAPI.NetworkConnection) bool {
195-
return types.EqualNetworkConnection(c, conn)
195+
return types.EqualNetworkConnection(*c, *conn)
196196
})
197197
}
198198

integration-tests/pkg/types/network.go

Lines changed: 45 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,59 +3,66 @@ package types
33
import (
44
"net"
55
"sort"
6+
"time"
7+
8+
timestamppb "google.golang.org/protobuf/types/known/timestamppb"
69

710
sensorAPI "github.com/stackrox/rox/generated/internalapi/sensor"
8-
utils "github.com/stackrox/rox/pkg/net"
911
)
1012

1113
const (
1214
NilTimestamp = "<nil>"
1315
)
1416

17+
var (
18+
nilTimestamp = timestamppb.New(time.Date(1970, 1, 1, 0, 0, 0, 0, time.UTC))
19+
notNilTimestamp = timestamppb.New(time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC))
20+
)
21+
1522
type NetworkConnectionBatch []*sensorAPI.NetworkConnection
1623

17-
// TranslateAddress is a helper function for converting binary representations
18-
// of network addresses (in the signals) to usable forms for testing
19-
func TranslateAddress(addr *sensorAPI.NetworkAddress) string {
20-
peerId := utils.NetworkPeerID{Port: uint16(addr.GetPort())}
21-
addressData := addr.GetAddressData()
22-
if len(addressData) > 0 {
23-
peerId.Address = utils.IPFromBytes(addressData)
24-
return peerId.String()
24+
func IsActive(conn *sensorAPI.NetworkConnection) bool {
25+
// no close timestamp means the connection is open, and active
26+
return conn.GetCloseTimestamp() == nil
27+
}
28+
29+
// The EqualVT method for NetworkAddress returns false if both of them are nil. That is not what
30+
// we want, so replace nil addr with a default NetworkAddress.
31+
func adjustNetworkAddressForComparison(addr *sensorAPI.NetworkAddress) *sensorAPI.NetworkAddress {
32+
if addr == nil {
33+
return CreateNetworkAddress("", "", 0)
2534
}
2635

27-
// If there is no address data, this is either the source address or
28-
// IpNetwork should be set and represent a CIDR block or external IP address.
29-
ipNetworkData := addr.GetIpNetwork()
30-
if len(ipNetworkData) == 0 {
31-
return peerId.String()
36+
return addr
37+
}
38+
39+
// The EqualVT method for NetworkConnection returns false if both CloseTimestamps
40+
// are nil. Same goes for LocalAddress and Remote Address. That is not the desired
41+
// result. Also EqualVT returns false if the CloseTimestamp are different non-nil
42+
// timestamps. We want the equal function to return true if neither of them are nil
43+
// or both of them are nil. This function adjusts the fields so that the comparison
44+
// works the way we want it to.
45+
func adjustNetworkConnectionForComparison(conn *sensorAPI.NetworkConnection) {
46+
conn.LocalAddress = adjustNetworkAddressForComparison(conn.LocalAddress)
47+
conn.RemoteAddress = adjustNetworkAddressForComparison(conn.RemoteAddress)
48+
49+
if conn.CloseTimestamp == nil {
50+
conn.CloseTimestamp = nilTimestamp
3251
}
3352

34-
ipNetwork := utils.IPNetworkFromCIDRBytes(ipNetworkData)
35-
prefixLen := ipNetwork.PrefixLen()
36-
// If this is IPv4 and the prefix length is 32 or this is IPv6 and the prefix length
37-
// is 128 this is a regular IP address and not a CIDR block
38-
if (ipNetwork.Family() == utils.IPv4 && prefixLen == byte(32)) ||
39-
(ipNetwork.Family() == utils.IPv6 && prefixLen == byte(128)) {
40-
peerId.Address = ipNetwork.IP()
41-
} else {
42-
peerId.IPNetwork = ipNetwork
53+
if conn.CloseTimestamp != nil {
54+
conn.CloseTimestamp = notNilTimestamp
4355
}
44-
return peerId.String()
4556
}
4657

47-
func IsActive(conn *sensorAPI.NetworkConnection) bool {
48-
// no close timestamp means the connection is open, and active
49-
return conn.GetCloseTimestamp() == nil
50-
}
58+
// EqualVT is not called directly because it returns false in cases that we don't want it to, for example
59+
// when both CloseTimestamp are nil, or when they have different non-nil values.
60+
func EqualNetworkConnection(conn1 sensorAPI.NetworkConnection, conn2 sensorAPI.NetworkConnection) bool {
61+
adjustNetworkConnectionForComparison(&conn1)
62+
adjustNetworkConnectionForComparison(&conn2)
63+
64+
return conn1.EqualVT(&conn2)
5165

52-
func EqualNetworkConnection(conn1 *sensorAPI.NetworkConnection, conn2 *sensorAPI.NetworkConnection) bool {
53-
return EqualNetworkAddress(conn1.LocalAddress, conn2.LocalAddress) &&
54-
EqualNetworkAddress(conn1.RemoteAddress, conn2.RemoteAddress) &&
55-
conn1.Protocol == conn2.Protocol &&
56-
conn1.Role == conn2.Role &&
57-
conn1.SocketFamily == conn2.SocketFamily &&
58-
IsActive(conn1) == IsActive(conn2)
5966
}
6067

6168
func CompareBytes(b1 []byte, b2 []byte) int {
@@ -81,19 +88,10 @@ func CompareBytes(b1 []byte, b2 []byte) int {
8188
}
8289

8390
func EqualNetworkAddress(addr1 *sensorAPI.NetworkAddress, addr2 *sensorAPI.NetworkAddress) bool {
84-
comp := CompareBytes(addr1.GetAddressData(), addr2.GetAddressData())
85-
86-
if comp != 0 {
87-
return false
88-
}
89-
90-
comp = CompareBytes(addr1.GetIpNetwork(), addr2.GetIpNetwork())
91-
92-
if comp != 0 {
93-
return false
94-
}
91+
ad1 := adjustNetworkAddressForComparison(addr1)
92+
ad2 := adjustNetworkAddressForComparison(addr2)
9593

96-
return addr1.GetPort() == addr2.GetPort()
94+
return ad1.EqualVT(ad2)
9795
}
9896

9997
func LessNetworkAddress(addr1 *sensorAPI.NetworkAddress, addr2 *sensorAPI.NetworkAddress) bool {

integration-tests/suites/process_network.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ func (s *ProcessNetworkTestSuite) TestNetworkFlows() {
156156
Protocol: storage.L4Protocol_L4_PROTOCOL_TCP,
157157
Role: sensorAPI.ClientServerRole_ROLE_SERVER,
158158
SocketFamily: sensorAPI.SocketFamily_SOCKET_FAMILY_UNKNOWN,
159+
ContainerId: s.serverContainer,
159160
CloseTimestamp: nil,
160161
},
161162
)
@@ -167,6 +168,7 @@ func (s *ProcessNetworkTestSuite) TestNetworkFlows() {
167168
Protocol: storage.L4Protocol_L4_PROTOCOL_TCP,
168169
Role: sensorAPI.ClientServerRole_ROLE_CLIENT,
169170
SocketFamily: sensorAPI.SocketFamily_SOCKET_FAMILY_UNKNOWN,
171+
ContainerId: s.clientContainer,
170172
CloseTimestamp: nil,
171173
},
172174
)

integration-tests/suites/repeated_network_flow.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ func (s *RepeatedNetworkFlowTestSuite) TearDownSuite() {
109109
}
110110

111111
func (s *RepeatedNetworkFlowTestSuite) TestRepeatedNetworkFlow() {
112-
networkInfos := s.Sensor().ExpectConnectionsN(s.T(), s.ServerContainer, 10*time.Second, s.ExpectedActive+s.ExpectedInactive)
112+
networkConnections := s.Sensor().ExpectConnectionsN(s.T(), s.ServerContainer, 10*time.Second, s.ExpectedActive+s.ExpectedInactive)
113113

114114
observedActive := 0
115115
observedInactive := 0
116116

117-
for _, info := range networkInfos {
117+
for _, info := range networkConnections {
118118
if types.IsActive(info) {
119119
observedActive++
120120
} else {
@@ -127,8 +127,8 @@ func (s *RepeatedNetworkFlowTestSuite) TestRepeatedNetworkFlow() {
127127

128128
// Server side checks
129129

130-
actualServerEndpoint := networkInfos[0].LocalAddress
131-
actualClientEndpoint := networkInfos[0].RemoteAddress
130+
actualServerEndpoint := networkConnections[0].LocalAddress
131+
actualClientEndpoint := networkConnections[0].RemoteAddress
132132

133133
// From server perspective, network connection info only has local port and remote IP
134134
expectedServerEndpoint := types.CreateNetworkAddress("", "", s.ServerPort)
@@ -143,8 +143,8 @@ func (s *RepeatedNetworkFlowTestSuite) TestRepeatedNetworkFlow() {
143143
// See the comment above for the server container endpoint test for more info.
144144
assert.Equal(s.T(), 0, len(s.Sensor().Endpoints(s.ClientContainer)))
145145

146-
networkInfos = s.Sensor().Connections(s.ClientContainer)
146+
networkConnections = s.Sensor().Connections(s.ClientContainer)
147147

148-
actualClientEndpoint = networkInfos[0].LocalAddress
149-
actualServerEndpoint = networkInfos[0].RemoteAddress
148+
actualClientEndpoint = networkConnections[0].LocalAddress
149+
actualServerEndpoint = networkConnections[0].RemoteAddress
150150
}

integration-tests/suites/runtime_config_file.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ func (s *RuntimeConfigFileTestSuite) SetupTest() {
9898
s.Require().NoError(err)
9999
s.ClientContainer = common.ContainerShortID(containerID)
100100

101+
activeNormalizedConnection.ContainerId = s.ClientContainer
102+
inactiveNormalizedConnection.ContainerId = s.ClientContainer
103+
activeUnnormalizedConnection.ContainerId = s.ClientContainer
104+
inactiveUnnormalizedConnection.ContainerId = s.ClientContainer
105+
101106
collectorOptions := collector.StartupOptions{
102107
Env: map[string]string{
103108
"ROX_AFTERGLOW_PERIOD": "6",

integration-tests/suites/udp_networkflow.go

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ func (s *UdpNetworkFlow) runTest(image, recv, send string, port uint32) {
124124
Protocol: storage.L4Protocol_L4_PROTOCOL_UDP,
125125
Role: sensorAPI.ClientServerRole_ROLE_CLIENT,
126126
SocketFamily: sensorAPI.SocketFamily_SOCKET_FAMILY_UNKNOWN,
127+
ContainerId: client.id,
127128
CloseTimestamp: nil,
128129
}
129130

@@ -134,6 +135,7 @@ func (s *UdpNetworkFlow) runTest(image, recv, send string, port uint32) {
134135
Protocol: storage.L4Protocol_L4_PROTOCOL_UDP,
135136
Role: sensorAPI.ClientServerRole_ROLE_SERVER,
136137
SocketFamily: sensorAPI.SocketFamily_SOCKET_FAMILY_UNKNOWN,
138+
ContainerId: server.id,
137139
CloseTimestamp: nil,
138140
}
139141

@@ -155,16 +157,6 @@ func (s *UdpNetworkFlow) TestMultipleDestinations() {
155157
Command: newServerCmd("recvfrom", port),
156158
}, port)
157159
log.Info("Server: %s\n", servers[i].String())
158-
159-
// Load the client connection collector has to send for this server.
160-
clientConnections[i] = &sensorAPI.NetworkConnection{
161-
LocalAddress: types.CreateNetworkAddress("", "", 0),
162-
RemoteAddress: types.CreateNetworkAddress(servers[i].ip, "", servers[i].port),
163-
Protocol: storage.L4Protocol_L4_PROTOCOL_UDP,
164-
Role: sensorAPI.ClientServerRole_ROLE_CLIENT,
165-
SocketFamily: sensorAPI.SocketFamily_SOCKET_FAMILY_UNKNOWN,
166-
CloseTimestamp: nil,
167-
}
168160
}
169161

170162
// We give a big period here to ensure the syscall happens just once
@@ -178,13 +170,28 @@ func (s *UdpNetworkFlow) TestMultipleDestinations() {
178170
})
179171
log.Info("Client: %s\n", client.String())
180172

173+
for i := 0; i < CONTAINER_COUNT; i++ {
174+
// Load the client connection collector has to send for this server.
175+
clientConnections[i] = &sensorAPI.NetworkConnection{
176+
//LocalAddress: nil,
177+
LocalAddress: types.CreateNetworkAddress("", "", 0),
178+
RemoteAddress: types.CreateNetworkAddress(servers[i].ip, "", servers[i].port),
179+
Protocol: storage.L4Protocol_L4_PROTOCOL_UDP,
180+
Role: sensorAPI.ClientServerRole_ROLE_CLIENT,
181+
SocketFamily: sensorAPI.SocketFamily_SOCKET_FAMILY_UNKNOWN,
182+
ContainerId: client.id,
183+
CloseTimestamp: nil,
184+
}
185+
}
186+
181187
for _, server := range servers {
182188
serverConnection := &sensorAPI.NetworkConnection{
183189
LocalAddress: types.CreateNetworkAddress("", "", server.port),
184190
RemoteAddress: types.CreateNetworkAddress(client.ip, "", 0),
185191
Protocol: storage.L4Protocol_L4_PROTOCOL_UDP,
186192
Role: sensorAPI.ClientServerRole_ROLE_SERVER,
187193
SocketFamily: sensorAPI.SocketFamily_SOCKET_FAMILY_UNKNOWN,
194+
ContainerId: server.id,
188195
CloseTimestamp: nil,
189196
}
190197
s.Sensor().ExpectConnections(s.T(), server.id, 5*time.Second, serverConnection)
@@ -205,6 +212,7 @@ func (s *UdpNetworkFlow) TestMultipleSources() {
205212

206213
clients := make([]containerData, CONTAINER_COUNT)
207214
serverConnections := make([]*sensorAPI.NetworkConnection, CONTAINER_COUNT)
215+
clientConnections := make([]*sensorAPI.NetworkConnection, CONTAINER_COUNT)
208216
for i := 0; i < CONTAINER_COUNT; i++ {
209217
name := fmt.Sprintf("%s-%d", UDP_CLIENT, i)
210218
clients[i] = s.runClient(config.ContainerStartConfig{
@@ -221,22 +229,23 @@ func (s *UdpNetworkFlow) TestMultipleSources() {
221229
RemoteAddress: types.CreateNetworkAddress(clients[i].ip, "", 0),
222230
Protocol: storage.L4Protocol_L4_PROTOCOL_UDP,
223231
Role: sensorAPI.ClientServerRole_ROLE_SERVER,
232+
ContainerId: server.id,
224233
SocketFamily: sensorAPI.SocketFamily_SOCKET_FAMILY_UNKNOWN,
225234
CloseTimestamp: nil,
226235
}
236+
clientConnections[i] = &sensorAPI.NetworkConnection{
237+
LocalAddress: types.CreateNetworkAddress("", "", 0),
238+
RemoteAddress: types.CreateNetworkAddress(server.ip, "", server.port),
239+
Protocol: storage.L4Protocol_L4_PROTOCOL_UDP,
240+
Role: sensorAPI.ClientServerRole_ROLE_CLIENT,
241+
SocketFamily: sensorAPI.SocketFamily_SOCKET_FAMILY_UNKNOWN,
242+
ContainerId: clients[i].id,
243+
CloseTimestamp: nil,
244+
}
227245
}
228246

229-
clientConnection := &sensorAPI.NetworkConnection{
230-
LocalAddress: types.CreateNetworkAddress("", "", 0),
231-
RemoteAddress: types.CreateNetworkAddress(server.ip, "", server.port),
232-
Protocol: storage.L4Protocol_L4_PROTOCOL_UDP,
233-
Role: sensorAPI.ClientServerRole_ROLE_CLIENT,
234-
SocketFamily: sensorAPI.SocketFamily_SOCKET_FAMILY_UNKNOWN,
235-
CloseTimestamp: nil,
236-
}
237-
238-
for _, client := range clients {
239-
s.Sensor().ExpectConnections(s.T(), client.id, 5*time.Second, clientConnection)
247+
for i, client := range clients {
248+
s.Sensor().ExpectConnections(s.T(), client.id, 5*time.Second, clientConnections[i])
240249
}
241250
s.Sensor().ExpectConnections(s.T(), server.id, 5*time.Second, serverConnections...)
242251
}

0 commit comments

Comments
 (0)