2020-09-18 08:20:09 -06:00
|
|
|
package nebula
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/slackhq/nebula/cert"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/iputil"
|
2021-11-10 20:47:38 -07:00
|
|
|
"github.com/slackhq/nebula/test"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/udp"
|
2020-09-18 08:20:09 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
func TestControl_GetHostInfoByVpnIp(t *testing.T) {
|
2021-11-10 20:47:38 -07:00
|
|
|
l := test.NewLogger()
|
2020-09-18 08:20:09 -06:00
|
|
|
// Special care must be taken to re-use all objects provided to the hostmap and certificate in the expectedInfo object
|
|
|
|
// To properly ensure we are not exposing core memory to the caller
|
2024-04-03 21:14:51 -06:00
|
|
|
hm := newHostMap(l, &net.IPNet{})
|
|
|
|
hm.preferredRanges.Store(&[]*net.IPNet{})
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
remote1 := udp.NewAddr(net.ParseIP("0.0.0.100"), 4444)
|
|
|
|
remote2 := udp.NewAddr(net.ParseIP("1:2:3:4:5:6:7:8"), 4444)
|
2020-09-18 08:20:09 -06:00
|
|
|
ipNet := net.IPNet{
|
|
|
|
IP: net.IPv4(1, 2, 3, 4),
|
|
|
|
Mask: net.IPMask{255, 255, 255, 0},
|
|
|
|
}
|
|
|
|
|
|
|
|
ipNet2 := net.IPNet{
|
2021-03-18 19:37:24 -06:00
|
|
|
IP: net.ParseIP("1:2:3:4:5:6:7:8"),
|
2020-09-18 08:20:09 -06:00
|
|
|
Mask: net.IPMask{255, 255, 255, 0},
|
|
|
|
}
|
|
|
|
|
|
|
|
crt := &cert.NebulaCertificate{
|
|
|
|
Details: cert.NebulaCertificateDetails{
|
|
|
|
Name: "test",
|
|
|
|
Ips: []*net.IPNet{&ipNet},
|
|
|
|
Subnets: []*net.IPNet{},
|
|
|
|
Groups: []string{"default-group"},
|
|
|
|
NotBefore: time.Unix(1, 0),
|
|
|
|
NotAfter: time.Unix(2, 0),
|
|
|
|
PublicKey: []byte{5, 6, 7, 8},
|
|
|
|
IsCA: false,
|
|
|
|
Issuer: "the-issuer",
|
|
|
|
InvertedGroups: map[string]struct{}{"default-group": {}},
|
|
|
|
},
|
|
|
|
Signature: []byte{1, 2, 1, 2, 1, 3},
|
|
|
|
}
|
|
|
|
|
2023-05-09 09:22:08 -06:00
|
|
|
remotes := NewRemoteList(nil)
|
2021-04-14 12:50:09 -06:00
|
|
|
remotes.unlockedPrependV4(0, NewIp4AndPort(remote1.IP, uint32(remote1.Port)))
|
|
|
|
remotes.unlockedPrependV6(0, NewIp6AndPort(remote2.IP, uint32(remote2.Port)))
|
2023-07-24 11:37:52 -06:00
|
|
|
hm.unlockedAddHostInfo(&HostInfo{
|
2020-09-18 08:20:09 -06:00
|
|
|
remote: remote1,
|
2021-04-14 12:50:09 -06:00
|
|
|
remotes: remotes,
|
2020-09-18 08:20:09 -06:00
|
|
|
ConnectionState: &ConnectionState{
|
2021-03-05 19:18:33 -07:00
|
|
|
peerCert: crt,
|
2020-09-18 08:20:09 -06:00
|
|
|
},
|
|
|
|
remoteIndexId: 200,
|
|
|
|
localIndexId: 201,
|
2021-11-03 19:54:04 -06:00
|
|
|
vpnIp: iputil.Ip2VpnIp(ipNet.IP),
|
2022-06-21 12:35:23 -06:00
|
|
|
relayState: RelayState{
|
|
|
|
relays: map[iputil.VpnIp]struct{}{},
|
|
|
|
relayForByIp: map[iputil.VpnIp]*Relay{},
|
|
|
|
relayForByIdx: map[uint32]*Relay{},
|
|
|
|
},
|
2023-07-24 11:37:52 -06:00
|
|
|
}, &Interface{})
|
2020-09-18 08:20:09 -06:00
|
|
|
|
2023-07-24 11:37:52 -06:00
|
|
|
hm.unlockedAddHostInfo(&HostInfo{
|
2020-09-18 08:20:09 -06:00
|
|
|
remote: remote1,
|
2021-04-14 12:50:09 -06:00
|
|
|
remotes: remotes,
|
2020-09-18 08:20:09 -06:00
|
|
|
ConnectionState: &ConnectionState{
|
2021-03-05 19:18:33 -07:00
|
|
|
peerCert: nil,
|
2020-09-18 08:20:09 -06:00
|
|
|
},
|
|
|
|
remoteIndexId: 200,
|
|
|
|
localIndexId: 201,
|
2021-11-03 19:54:04 -06:00
|
|
|
vpnIp: iputil.Ip2VpnIp(ipNet2.IP),
|
2022-06-21 12:35:23 -06:00
|
|
|
relayState: RelayState{
|
|
|
|
relays: map[iputil.VpnIp]struct{}{},
|
|
|
|
relayForByIp: map[iputil.VpnIp]*Relay{},
|
|
|
|
relayForByIdx: map[uint32]*Relay{},
|
|
|
|
},
|
2023-07-24 11:37:52 -06:00
|
|
|
}, &Interface{})
|
2020-09-18 08:20:09 -06:00
|
|
|
|
|
|
|
c := Control{
|
|
|
|
f: &Interface{
|
|
|
|
hostMap: hm,
|
|
|
|
},
|
|
|
|
l: logrus.New(),
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
thi := c.GetHostInfoByVpnIp(iputil.Ip2VpnIp(ipNet.IP), false)
|
2020-09-18 08:20:09 -06:00
|
|
|
|
|
|
|
expectedInfo := ControlHostInfo{
|
2022-06-21 12:35:23 -06:00
|
|
|
VpnIp: net.IPv4(1, 2, 3, 4).To4(),
|
|
|
|
LocalIndex: 201,
|
|
|
|
RemoteIndex: 200,
|
|
|
|
RemoteAddrs: []*udp.Addr{remote2, remote1},
|
|
|
|
Cert: crt.Copy(),
|
|
|
|
MessageCounter: 0,
|
|
|
|
CurrentRemote: udp.NewAddr(net.ParseIP("0.0.0.100"), 4444),
|
|
|
|
CurrentRelaysToMe: []iputil.VpnIp{},
|
|
|
|
CurrentRelaysThroughMe: []iputil.VpnIp{},
|
2020-09-18 08:20:09 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we don't have any unexpected fields
|
2023-11-02 15:53:59 -06:00
|
|
|
assertFields(t, []string{"VpnIp", "LocalIndex", "RemoteIndex", "RemoteAddrs", "Cert", "MessageCounter", "CurrentRemote", "CurrentRelaysToMe", "CurrentRelaysThroughMe"}, thi)
|
2021-11-10 20:47:38 -07:00
|
|
|
test.AssertDeepCopyEqual(t, &expectedInfo, thi)
|
2020-09-18 08:20:09 -06:00
|
|
|
|
|
|
|
// Make sure we don't panic if the host info doesn't have a cert yet
|
|
|
|
assert.NotPanics(t, func() {
|
2021-11-03 19:54:04 -06:00
|
|
|
thi = c.GetHostInfoByVpnIp(iputil.Ip2VpnIp(ipNet2.IP), false)
|
2020-09-18 08:20:09 -06:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertFields(t *testing.T, expected []string, actualStruct interface{}) {
|
|
|
|
val := reflect.ValueOf(actualStruct).Elem()
|
|
|
|
fields := make([]string, val.NumField())
|
|
|
|
for i := 0; i < val.NumField(); i++ {
|
|
|
|
fields[i] = val.Type().Field(i).Name
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.Equal(t, expected, fields)
|
|
|
|
}
|