lighthouse_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. package nebula
  2. import (
  3. "fmt"
  4. "net"
  5. "testing"
  6. "github.com/golang/protobuf/proto"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. //TODO: Add a test to ensure udpAddr is copied and not reused
  10. func TestOldIPv4Only(t *testing.T) {
  11. // This test ensures our new ipv6 enabled LH protobuf IpAndPorts works with the old style to enable backwards compatibility
  12. b := []byte{8, 129, 130, 132, 80, 16, 10}
  13. var m Ip4AndPort
  14. err := proto.Unmarshal(b, &m)
  15. assert.NoError(t, err)
  16. assert.Equal(t, "10.1.1.1", int2ip(m.GetIp()).String())
  17. }
  18. func TestNewLhQuery(t *testing.T) {
  19. myIp := net.ParseIP("192.1.1.1")
  20. myIpint := ip2int(myIp)
  21. // Generating a new lh query should work
  22. a := NewLhQueryByInt(myIpint)
  23. // The result should be a nebulameta protobuf
  24. assert.IsType(t, &NebulaMeta{}, a)
  25. // It should also Marshal fine
  26. b, err := proto.Marshal(a)
  27. assert.Nil(t, err)
  28. // and then Unmarshal fine
  29. n := &NebulaMeta{}
  30. err = proto.Unmarshal(b, n)
  31. assert.Nil(t, err)
  32. }
  33. func Test_lhStaticMapping(t *testing.T) {
  34. l := NewTestLogger()
  35. lh1 := "10.128.0.2"
  36. lh1IP := net.ParseIP(lh1)
  37. udpServer, _ := NewListener(l, "0.0.0.0", 0, true)
  38. meh := NewLightHouse(l, true, 1, []uint32{ip2int(lh1IP)}, 10, 10003, udpServer, false, 1, false)
  39. meh.AddRemote(ip2int(lh1IP), NewUDPAddr(lh1IP, uint16(4242)), true)
  40. err := meh.ValidateLHStaticEntries()
  41. assert.Nil(t, err)
  42. lh2 := "10.128.0.3"
  43. lh2IP := net.ParseIP(lh2)
  44. meh = NewLightHouse(l, true, 1, []uint32{ip2int(lh1IP), ip2int(lh2IP)}, 10, 10003, udpServer, false, 1, false)
  45. meh.AddRemote(ip2int(lh1IP), NewUDPAddr(lh1IP, uint16(4242)), true)
  46. err = meh.ValidateLHStaticEntries()
  47. assert.EqualError(t, err, "Lighthouse 10.128.0.3 does not have a static_host_map entry")
  48. }
  49. func BenchmarkLighthouseHandleRequest(b *testing.B) {
  50. l := NewTestLogger()
  51. lh1 := "10.128.0.2"
  52. lh1IP := net.ParseIP(lh1)
  53. udpServer, _ := NewListener(l, "0.0.0.0", 0, true)
  54. lh := NewLightHouse(l, true, 1, []uint32{ip2int(lh1IP)}, 10, 10003, udpServer, false, 1, false)
  55. hAddr := NewUDPAddrFromString("4.5.6.7:12345")
  56. hAddr2 := NewUDPAddrFromString("4.5.6.7:12346")
  57. lh.addrMap[3] = &ip4And6{v4: []*Ip4AndPort{
  58. NewIp4AndPort(hAddr.IP, uint32(hAddr.Port)),
  59. NewIp4AndPort(hAddr2.IP, uint32(hAddr2.Port))},
  60. }
  61. rAddr := NewUDPAddrFromString("1.2.2.3:12345")
  62. rAddr2 := NewUDPAddrFromString("1.2.2.3:12346")
  63. lh.addrMap[2] = &ip4And6{v4: []*Ip4AndPort{
  64. NewIp4AndPort(rAddr.IP, uint32(rAddr.Port)),
  65. NewIp4AndPort(rAddr2.IP, uint32(rAddr2.Port))},
  66. }
  67. mw := &mockEncWriter{}
  68. b.Run("notfound", func(b *testing.B) {
  69. lhh := lh.NewRequestHandler()
  70. req := &NebulaMeta{
  71. Type: NebulaMeta_HostQuery,
  72. Details: &NebulaMetaDetails{
  73. VpnIp: 4,
  74. Ip4AndPorts: nil,
  75. },
  76. }
  77. p, err := proto.Marshal(req)
  78. assert.NoError(b, err)
  79. for n := 0; n < b.N; n++ {
  80. lhh.HandleRequest(rAddr, 2, p, mw)
  81. }
  82. })
  83. b.Run("found", func(b *testing.B) {
  84. lhh := lh.NewRequestHandler()
  85. req := &NebulaMeta{
  86. Type: NebulaMeta_HostQuery,
  87. Details: &NebulaMetaDetails{
  88. VpnIp: 3,
  89. Ip4AndPorts: nil,
  90. },
  91. }
  92. p, err := proto.Marshal(req)
  93. assert.NoError(b, err)
  94. for n := 0; n < b.N; n++ {
  95. lhh.HandleRequest(rAddr, 2, p, mw)
  96. }
  97. })
  98. }
  99. func TestLighthouse_Memory(t *testing.T) {
  100. l := NewTestLogger()
  101. myUdpAddr0 := &udpAddr{IP: net.ParseIP("10.0.0.2"), Port: 4242}
  102. myUdpAddr1 := &udpAddr{IP: net.ParseIP("192.168.0.2"), Port: 4242}
  103. myUdpAddr2 := &udpAddr{IP: net.ParseIP("172.16.0.2"), Port: 4242}
  104. myUdpAddr3 := &udpAddr{IP: net.ParseIP("100.152.0.2"), Port: 4242}
  105. myUdpAddr4 := &udpAddr{IP: net.ParseIP("24.15.0.2"), Port: 4242}
  106. myUdpAddr5 := &udpAddr{IP: net.ParseIP("192.168.0.2"), Port: 4243}
  107. myUdpAddr6 := &udpAddr{IP: net.ParseIP("192.168.0.2"), Port: 4244}
  108. myUdpAddr7 := &udpAddr{IP: net.ParseIP("192.168.0.2"), Port: 4245}
  109. myUdpAddr8 := &udpAddr{IP: net.ParseIP("192.168.0.2"), Port: 4246}
  110. myUdpAddr9 := &udpAddr{IP: net.ParseIP("192.168.0.2"), Port: 4247}
  111. myUdpAddr10 := &udpAddr{IP: net.ParseIP("192.168.0.2"), Port: 4248}
  112. myUdpAddr11 := &udpAddr{IP: net.ParseIP("192.168.0.2"), Port: 4249}
  113. myVpnIp := ip2int(net.ParseIP("10.128.0.2"))
  114. theirUdpAddr0 := &udpAddr{IP: net.ParseIP("10.0.0.3"), Port: 4242}
  115. theirUdpAddr1 := &udpAddr{IP: net.ParseIP("192.168.0.3"), Port: 4242}
  116. theirUdpAddr2 := &udpAddr{IP: net.ParseIP("172.16.0.3"), Port: 4242}
  117. theirUdpAddr3 := &udpAddr{IP: net.ParseIP("100.152.0.3"), Port: 4242}
  118. theirUdpAddr4 := &udpAddr{IP: net.ParseIP("24.15.0.3"), Port: 4242}
  119. theirVpnIp := ip2int(net.ParseIP("10.128.0.3"))
  120. lhIP := net.ParseIP("10.128.0.1")
  121. udpServer, _ := NewListener(l, "0.0.0.0", 0, true)
  122. lh := NewLightHouse(l, true, 1, []uint32{ip2int(lhIP)}, 10, 10003, udpServer, false, 1, false)
  123. lhh := lh.NewRequestHandler()
  124. // Test that my first update responds with just that
  125. newLHHostUpdate(myUdpAddr0, myVpnIp, []*udpAddr{myUdpAddr1, myUdpAddr2}, lhh)
  126. r := newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  127. assertIp4InArray(t, r.msg.Details.Ip4AndPorts, myUdpAddr1, myUdpAddr2)
  128. // Ensure we don't accumulate addresses
  129. newLHHostUpdate(myUdpAddr0, myVpnIp, []*udpAddr{myUdpAddr3}, lhh)
  130. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  131. assertIp4InArray(t, r.msg.Details.Ip4AndPorts, myUdpAddr3)
  132. // Grow it back to 2
  133. newLHHostUpdate(myUdpAddr0, myVpnIp, []*udpAddr{myUdpAddr1, myUdpAddr4}, lhh)
  134. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  135. assertIp4InArray(t, r.msg.Details.Ip4AndPorts, myUdpAddr1, myUdpAddr4)
  136. // Update a different host
  137. newLHHostUpdate(theirUdpAddr0, theirVpnIp, []*udpAddr{theirUdpAddr1, theirUdpAddr2, theirUdpAddr3, theirUdpAddr4}, lhh)
  138. r = newLHHostRequest(theirUdpAddr0, theirVpnIp, myVpnIp, lhh)
  139. assertIp4InArray(t, r.msg.Details.Ip4AndPorts, theirUdpAddr1, theirUdpAddr2, theirUdpAddr3, theirUdpAddr4)
  140. // Make sure we didn't get changed
  141. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  142. assertIp4InArray(t, r.msg.Details.Ip4AndPorts, myUdpAddr1, myUdpAddr4)
  143. // Finally ensure proper ordering and limiting
  144. // Send 12 addrs, get 10 back, one removed on a dupe check the other by limiting
  145. newLHHostUpdate(
  146. myUdpAddr0,
  147. myVpnIp,
  148. []*udpAddr{
  149. myUdpAddr1,
  150. myUdpAddr2,
  151. myUdpAddr3,
  152. myUdpAddr4,
  153. myUdpAddr5,
  154. myUdpAddr5, //Duplicated on purpose
  155. myUdpAddr6,
  156. myUdpAddr7,
  157. myUdpAddr8,
  158. myUdpAddr9,
  159. myUdpAddr10,
  160. myUdpAddr11, // This should get cut
  161. }, lhh)
  162. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  163. assertIp4InArray(
  164. t,
  165. r.msg.Details.Ip4AndPorts,
  166. myUdpAddr1, myUdpAddr2, myUdpAddr3, myUdpAddr4, myUdpAddr5, myUdpAddr6, myUdpAddr7, myUdpAddr8, myUdpAddr9, myUdpAddr10,
  167. )
  168. }
  169. func newLHHostRequest(fromAddr *udpAddr, myVpnIp, queryVpnIp uint32, lhh *LightHouseHandler) testLhReply {
  170. req := &NebulaMeta{
  171. Type: NebulaMeta_HostQuery,
  172. Details: &NebulaMetaDetails{
  173. VpnIp: queryVpnIp,
  174. },
  175. }
  176. b, err := req.Marshal()
  177. if err != nil {
  178. panic(err)
  179. }
  180. w := &testEncWriter{}
  181. lhh.HandleRequest(fromAddr, myVpnIp, b, w)
  182. return w.lastReply
  183. }
  184. func newLHHostUpdate(fromAddr *udpAddr, vpnIp uint32, addrs []*udpAddr, lhh *LightHouseHandler) {
  185. req := &NebulaMeta{
  186. Type: NebulaMeta_HostUpdateNotification,
  187. Details: &NebulaMetaDetails{
  188. VpnIp: vpnIp,
  189. Ip4AndPorts: make([]*Ip4AndPort, len(addrs)),
  190. },
  191. }
  192. for k, v := range addrs {
  193. req.Details.Ip4AndPorts[k] = &Ip4AndPort{Ip: ip2int(v.IP), Port: uint32(v.Port)}
  194. }
  195. b, err := req.Marshal()
  196. if err != nil {
  197. panic(err)
  198. }
  199. w := &testEncWriter{}
  200. lhh.HandleRequest(fromAddr, vpnIp, b, w)
  201. }
  202. func Test_lhRemoteAllowList(t *testing.T) {
  203. l := NewTestLogger()
  204. c := NewConfig(l)
  205. c.Settings["remoteallowlist"] = map[interface{}]interface{}{
  206. "10.20.0.0/12": false,
  207. }
  208. allowList, err := c.GetAllowList("remoteallowlist", false)
  209. assert.Nil(t, err)
  210. lh1 := "10.128.0.2"
  211. lh1IP := net.ParseIP(lh1)
  212. udpServer, _ := NewListener(l, "0.0.0.0", 0, true)
  213. lh := NewLightHouse(l, true, 1, []uint32{ip2int(lh1IP)}, 10, 10003, udpServer, false, 1, false)
  214. lh.SetRemoteAllowList(allowList)
  215. // A disallowed ip should not enter the cache but we should end up with an empty entry in the addrMap
  216. remote1IP := net.ParseIP("10.20.0.3")
  217. lh.AddRemote(ip2int(remote1IP), NewUDPAddr(remote1IP, uint16(4242)), true)
  218. assert.NotNil(t, lh.addrMap[ip2int(remote1IP)])
  219. assert.Empty(t, lh.addrMap[ip2int(remote1IP)].v4)
  220. assert.Empty(t, lh.addrMap[ip2int(remote1IP)].v6)
  221. // Make sure a good ip enters the cache and addrMap
  222. remote2IP := net.ParseIP("10.128.0.3")
  223. remote2UDPAddr := NewUDPAddr(remote2IP, uint16(4242))
  224. lh.AddRemote(ip2int(remote2IP), remote2UDPAddr, true)
  225. assertIp4InArray(t, lh.addrMap[ip2int(remote2IP)].learnedV4, remote2UDPAddr)
  226. // Another good ip gets into the cache, ordering is inverted
  227. remote3IP := net.ParseIP("10.128.0.4")
  228. remote3UDPAddr := NewUDPAddr(remote3IP, uint16(4243))
  229. lh.AddRemote(ip2int(remote2IP), remote3UDPAddr, true)
  230. assertIp4InArray(t, lh.addrMap[ip2int(remote2IP)].learnedV4, remote3UDPAddr, remote2UDPAddr)
  231. // If we exceed the length limit we should only have the most recent addresses
  232. addedAddrs := []*udpAddr{}
  233. for i := 0; i < 11; i++ {
  234. remoteUDPAddr := NewUDPAddr(net.IP{10, 128, 0, 4}, uint16(4243+i))
  235. lh.AddRemote(ip2int(remote2IP), remoteUDPAddr, true)
  236. // The first entry here is a duplicate, don't add it to the assert list
  237. if i != 0 {
  238. addedAddrs = append(addedAddrs, remoteUDPAddr)
  239. }
  240. }
  241. // We should only have the last 10 of what we tried to add
  242. assert.True(t, len(addedAddrs) >= 10, "We should have tried to add at least 10 addresses")
  243. ln := len(addedAddrs)
  244. assertIp4InArray(
  245. t,
  246. lh.addrMap[ip2int(remote2IP)].learnedV4,
  247. addedAddrs[ln-1],
  248. addedAddrs[ln-2],
  249. addedAddrs[ln-3],
  250. addedAddrs[ln-4],
  251. addedAddrs[ln-5],
  252. addedAddrs[ln-6],
  253. addedAddrs[ln-7],
  254. addedAddrs[ln-8],
  255. addedAddrs[ln-9],
  256. addedAddrs[ln-10],
  257. )
  258. }
  259. type testLhReply struct {
  260. nebType NebulaMessageType
  261. nebSubType NebulaMessageSubType
  262. vpnIp uint32
  263. msg *NebulaMeta
  264. }
  265. type testEncWriter struct {
  266. lastReply testLhReply
  267. }
  268. func (tw *testEncWriter) SendMessageToVpnIp(t NebulaMessageType, st NebulaMessageSubType, vpnIp uint32, p, _, _ []byte) {
  269. tw.lastReply = testLhReply{
  270. nebType: t,
  271. nebSubType: st,
  272. vpnIp: vpnIp,
  273. msg: &NebulaMeta{},
  274. }
  275. err := proto.Unmarshal(p, tw.lastReply.msg)
  276. if err != nil {
  277. panic(err)
  278. }
  279. }
  280. // assertIp4InArray asserts every address in want is at the same position in have and that the lengths match
  281. func assertIp4InArray(t *testing.T, have []*Ip4AndPort, want ...*udpAddr) {
  282. assert.Len(t, have, len(want))
  283. for k, w := range want {
  284. if !(have[k].Ip == ip2int(w.IP) && have[k].Port == uint32(w.Port)) {
  285. assert.Fail(t, fmt.Sprintf("Response did not contain: %v:%v at %v; %v", w.IP, w.Port, k, translateV4toUdpAddr(have)))
  286. }
  287. }
  288. }
  289. func translateV4toUdpAddr(ips []*Ip4AndPort) []*udpAddr {
  290. addrs := make([]*udpAddr, len(ips))
  291. for k, v := range ips {
  292. addrs[k] = NewUDPAddrFromLH4(v)
  293. }
  294. return addrs
  295. }