Skip to content

Commit

Permalink
ROX-26711: Collector mock server should handle external ips (#1901)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoukoVirtanen authored Oct 30, 2024
1 parent 629d0e1 commit 99d0837
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions integration-tests/pkg/mock_sensor/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,29 @@ func (m *MockSensor) pushEndpoint(containerID string, endpoint *sensorAPI.Networ
// translateAddress is a helper function for converting binary representations
// of network addresses (in the signals) to usable forms for testing
func (m *MockSensor) translateAddress(addr *sensorAPI.NetworkAddress) string {
ipPortPair := utils.NetworkPeerID{
Address: utils.IPFromBytes(addr.GetAddressData()),
Port: uint16(addr.GetPort()),
peerId := utils.NetworkPeerID{Port: uint16(addr.GetPort())}
addressData := addr.GetAddressData()
if len(addressData) > 0 {
peerId.Address = utils.IPFromBytes(addressData)
return peerId.String()
}
return ipPortPair.String()

// If there is no address data, this is either the source address or
// IpNetwork should be set and represent a CIDR block or external IP address.
ipNetworkData := addr.GetIpNetwork()
if len(ipNetworkData) == 0 {
return peerId.String()
}

ipNetwork := utils.IPNetworkFromCIDRBytes(ipNetworkData)
prefixLen := ipNetwork.PrefixLen()
// If this is IPv4 and the prefix length is 32 or this is IPv6 and the prefix length
// is 128 this is a regular IP address and not a CIDR block
if (ipNetwork.Family() == utils.IPv4 && prefixLen == byte(32)) ||
(ipNetwork.Family() == utils.IPv6 && prefixLen == byte(128)) {
peerId.Address = ipNetwork.IP()
} else {
peerId.IPNetwork = ipNetwork
}
return peerId.String()
}

0 comments on commit 99d0837

Please sign in to comment.