lighthouse_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. package nebula
  2. import (
  3. "context"
  4. "fmt"
  5. "net"
  6. "testing"
  7. "github.com/slackhq/nebula/config"
  8. "github.com/slackhq/nebula/header"
  9. "github.com/slackhq/nebula/iputil"
  10. "github.com/slackhq/nebula/test"
  11. "github.com/slackhq/nebula/udp"
  12. "github.com/stretchr/testify/assert"
  13. )
  14. //TODO: Add a test to ensure udpAddr is copied and not reused
  15. func TestOldIPv4Only(t *testing.T) {
  16. // This test ensures our new ipv6 enabled LH protobuf IpAndPorts works with the old style to enable backwards compatibility
  17. b := []byte{8, 129, 130, 132, 80, 16, 10}
  18. var m Ip4AndPort
  19. err := m.Unmarshal(b)
  20. assert.NoError(t, err)
  21. assert.Equal(t, "10.1.1.1", iputil.VpnIp(m.GetIp()).String())
  22. }
  23. func TestNewLhQuery(t *testing.T) {
  24. myIp := net.ParseIP("192.1.1.1")
  25. myIpint := iputil.Ip2VpnIp(myIp)
  26. // Generating a new lh query should work
  27. a := NewLhQueryByInt(myIpint)
  28. // The result should be a nebulameta protobuf
  29. assert.IsType(t, &NebulaMeta{}, a)
  30. // It should also Marshal fine
  31. b, err := a.Marshal()
  32. assert.Nil(t, err)
  33. // and then Unmarshal fine
  34. n := &NebulaMeta{}
  35. err = n.Unmarshal(b)
  36. assert.Nil(t, err)
  37. }
  38. func Test_lhStaticMapping(t *testing.T) {
  39. l := test.NewLogger()
  40. _, myVpnNet, _ := net.ParseCIDR("10.128.0.1/16")
  41. lh1 := "10.128.0.2"
  42. c := config.NewC(l)
  43. c.Settings["lighthouse"] = map[interface{}]interface{}{"hosts": []interface{}{lh1}}
  44. c.Settings["static_host_map"] = map[interface{}]interface{}{lh1: []interface{}{"1.1.1.1:4242"}}
  45. _, err := NewLightHouseFromConfig(context.Background(), l, c, myVpnNet, nil, nil)
  46. assert.Nil(t, err)
  47. lh2 := "10.128.0.3"
  48. c = config.NewC(l)
  49. c.Settings["lighthouse"] = map[interface{}]interface{}{"hosts": []interface{}{lh1, lh2}}
  50. c.Settings["static_host_map"] = map[interface{}]interface{}{lh1: []interface{}{"100.1.1.1:4242"}}
  51. _, err = NewLightHouseFromConfig(context.Background(), l, c, myVpnNet, nil, nil)
  52. assert.EqualError(t, err, "lighthouse 10.128.0.3 does not have a static_host_map entry")
  53. }
  54. func BenchmarkLighthouseHandleRequest(b *testing.B) {
  55. l := test.NewLogger()
  56. _, myVpnNet, _ := net.ParseCIDR("10.128.0.1/0")
  57. c := config.NewC(l)
  58. lh, err := NewLightHouseFromConfig(context.Background(), l, c, myVpnNet, nil, nil)
  59. if !assert.NoError(b, err) {
  60. b.Fatal()
  61. }
  62. hAddr := udp.NewAddrFromString("4.5.6.7:12345")
  63. hAddr2 := udp.NewAddrFromString("4.5.6.7:12346")
  64. lh.addrMap[3] = NewRemoteList(nil)
  65. lh.addrMap[3].unlockedSetV4(
  66. 3,
  67. 3,
  68. []*Ip4AndPort{
  69. NewIp4AndPort(hAddr.IP, uint32(hAddr.Port)),
  70. NewIp4AndPort(hAddr2.IP, uint32(hAddr2.Port)),
  71. },
  72. func(iputil.VpnIp, *Ip4AndPort) bool { return true },
  73. )
  74. rAddr := udp.NewAddrFromString("1.2.2.3:12345")
  75. rAddr2 := udp.NewAddrFromString("1.2.2.3:12346")
  76. lh.addrMap[2] = NewRemoteList(nil)
  77. lh.addrMap[2].unlockedSetV4(
  78. 3,
  79. 3,
  80. []*Ip4AndPort{
  81. NewIp4AndPort(rAddr.IP, uint32(rAddr.Port)),
  82. NewIp4AndPort(rAddr2.IP, uint32(rAddr2.Port)),
  83. },
  84. func(iputil.VpnIp, *Ip4AndPort) bool { return true },
  85. )
  86. mw := &mockEncWriter{}
  87. b.Run("notfound", func(b *testing.B) {
  88. lhh := lh.NewRequestHandler()
  89. req := &NebulaMeta{
  90. Type: NebulaMeta_HostQuery,
  91. Details: &NebulaMetaDetails{
  92. VpnIp: 4,
  93. Ip4AndPorts: nil,
  94. },
  95. }
  96. p, err := req.Marshal()
  97. assert.NoError(b, err)
  98. for n := 0; n < b.N; n++ {
  99. lhh.HandleRequest(rAddr, 2, p, mw)
  100. }
  101. })
  102. b.Run("found", func(b *testing.B) {
  103. lhh := lh.NewRequestHandler()
  104. req := &NebulaMeta{
  105. Type: NebulaMeta_HostQuery,
  106. Details: &NebulaMetaDetails{
  107. VpnIp: 3,
  108. Ip4AndPorts: nil,
  109. },
  110. }
  111. p, err := req.Marshal()
  112. assert.NoError(b, err)
  113. for n := 0; n < b.N; n++ {
  114. lhh.HandleRequest(rAddr, 2, p, mw)
  115. }
  116. })
  117. }
  118. func TestLighthouse_Memory(t *testing.T) {
  119. l := test.NewLogger()
  120. myUdpAddr0 := &udp.Addr{IP: net.ParseIP("10.0.0.2"), Port: 4242}
  121. myUdpAddr1 := &udp.Addr{IP: net.ParseIP("192.168.0.2"), Port: 4242}
  122. myUdpAddr2 := &udp.Addr{IP: net.ParseIP("172.16.0.2"), Port: 4242}
  123. myUdpAddr3 := &udp.Addr{IP: net.ParseIP("100.152.0.2"), Port: 4242}
  124. myUdpAddr4 := &udp.Addr{IP: net.ParseIP("24.15.0.2"), Port: 4242}
  125. myUdpAddr5 := &udp.Addr{IP: net.ParseIP("192.168.0.2"), Port: 4243}
  126. myUdpAddr6 := &udp.Addr{IP: net.ParseIP("192.168.0.2"), Port: 4244}
  127. myUdpAddr7 := &udp.Addr{IP: net.ParseIP("192.168.0.2"), Port: 4245}
  128. myUdpAddr8 := &udp.Addr{IP: net.ParseIP("192.168.0.2"), Port: 4246}
  129. myUdpAddr9 := &udp.Addr{IP: net.ParseIP("192.168.0.2"), Port: 4247}
  130. myUdpAddr10 := &udp.Addr{IP: net.ParseIP("192.168.0.2"), Port: 4248}
  131. myUdpAddr11 := &udp.Addr{IP: net.ParseIP("192.168.0.2"), Port: 4249}
  132. myVpnIp := iputil.Ip2VpnIp(net.ParseIP("10.128.0.2"))
  133. theirUdpAddr0 := &udp.Addr{IP: net.ParseIP("10.0.0.3"), Port: 4242}
  134. theirUdpAddr1 := &udp.Addr{IP: net.ParseIP("192.168.0.3"), Port: 4242}
  135. theirUdpAddr2 := &udp.Addr{IP: net.ParseIP("172.16.0.3"), Port: 4242}
  136. theirUdpAddr3 := &udp.Addr{IP: net.ParseIP("100.152.0.3"), Port: 4242}
  137. theirUdpAddr4 := &udp.Addr{IP: net.ParseIP("24.15.0.3"), Port: 4242}
  138. theirVpnIp := iputil.Ip2VpnIp(net.ParseIP("10.128.0.3"))
  139. c := config.NewC(l)
  140. c.Settings["lighthouse"] = map[interface{}]interface{}{"am_lighthouse": true}
  141. c.Settings["listen"] = map[interface{}]interface{}{"port": 4242}
  142. lh, err := NewLightHouseFromConfig(context.Background(), l, c, &net.IPNet{IP: net.IP{10, 128, 0, 1}, Mask: net.IPMask{255, 255, 255, 0}}, nil, nil)
  143. assert.NoError(t, err)
  144. lhh := lh.NewRequestHandler()
  145. // Test that my first update responds with just that
  146. newLHHostUpdate(myUdpAddr0, myVpnIp, []*udp.Addr{myUdpAddr1, myUdpAddr2}, lhh)
  147. r := newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  148. assertIp4InArray(t, r.msg.Details.Ip4AndPorts, myUdpAddr1, myUdpAddr2)
  149. // Ensure we don't accumulate addresses
  150. newLHHostUpdate(myUdpAddr0, myVpnIp, []*udp.Addr{myUdpAddr3}, lhh)
  151. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  152. assertIp4InArray(t, r.msg.Details.Ip4AndPorts, myUdpAddr3)
  153. // Grow it back to 2
  154. newLHHostUpdate(myUdpAddr0, myVpnIp, []*udp.Addr{myUdpAddr1, myUdpAddr4}, lhh)
  155. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  156. assertIp4InArray(t, r.msg.Details.Ip4AndPorts, myUdpAddr1, myUdpAddr4)
  157. // Update a different host and ask about it
  158. newLHHostUpdate(theirUdpAddr0, theirVpnIp, []*udp.Addr{theirUdpAddr1, theirUdpAddr2, theirUdpAddr3, theirUdpAddr4}, lhh)
  159. r = newLHHostRequest(theirUdpAddr0, theirVpnIp, theirVpnIp, lhh)
  160. assertIp4InArray(t, r.msg.Details.Ip4AndPorts, theirUdpAddr1, theirUdpAddr2, theirUdpAddr3, theirUdpAddr4)
  161. // Have both hosts ask about the other
  162. r = newLHHostRequest(theirUdpAddr0, theirVpnIp, myVpnIp, lhh)
  163. assertIp4InArray(t, r.msg.Details.Ip4AndPorts, myUdpAddr1, myUdpAddr4)
  164. r = newLHHostRequest(myUdpAddr0, myVpnIp, theirVpnIp, lhh)
  165. assertIp4InArray(t, r.msg.Details.Ip4AndPorts, theirUdpAddr1, theirUdpAddr2, theirUdpAddr3, theirUdpAddr4)
  166. // Make sure we didn't get changed
  167. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  168. assertIp4InArray(t, r.msg.Details.Ip4AndPorts, myUdpAddr1, myUdpAddr4)
  169. // Ensure proper ordering and limiting
  170. // Send 12 addrs, get 10 back, the last 2 removed, allowing the duplicate to remain (clients dedupe)
  171. newLHHostUpdate(
  172. myUdpAddr0,
  173. myVpnIp,
  174. []*udp.Addr{
  175. myUdpAddr1,
  176. myUdpAddr2,
  177. myUdpAddr3,
  178. myUdpAddr4,
  179. myUdpAddr5,
  180. myUdpAddr5, //Duplicated on purpose
  181. myUdpAddr6,
  182. myUdpAddr7,
  183. myUdpAddr8,
  184. myUdpAddr9,
  185. myUdpAddr10,
  186. myUdpAddr11, // This should get cut
  187. }, lhh)
  188. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  189. assertIp4InArray(
  190. t,
  191. r.msg.Details.Ip4AndPorts,
  192. myUdpAddr1, myUdpAddr2, myUdpAddr3, myUdpAddr4, myUdpAddr5, myUdpAddr5, myUdpAddr6, myUdpAddr7, myUdpAddr8, myUdpAddr9,
  193. )
  194. // Make sure we won't add ips in our vpn network
  195. bad1 := &udp.Addr{IP: net.ParseIP("10.128.0.99"), Port: 4242}
  196. bad2 := &udp.Addr{IP: net.ParseIP("10.128.0.100"), Port: 4242}
  197. good := &udp.Addr{IP: net.ParseIP("1.128.0.99"), Port: 4242}
  198. newLHHostUpdate(myUdpAddr0, myVpnIp, []*udp.Addr{bad1, bad2, good}, lhh)
  199. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  200. assertIp4InArray(t, r.msg.Details.Ip4AndPorts, good)
  201. }
  202. func TestLighthouse_reload(t *testing.T) {
  203. l := test.NewLogger()
  204. c := config.NewC(l)
  205. c.Settings["lighthouse"] = map[interface{}]interface{}{"am_lighthouse": true}
  206. c.Settings["listen"] = map[interface{}]interface{}{"port": 4242}
  207. lh, err := NewLightHouseFromConfig(context.Background(), l, c, &net.IPNet{IP: net.IP{10, 128, 0, 1}, Mask: net.IPMask{255, 255, 255, 0}}, nil, nil)
  208. assert.NoError(t, err)
  209. c.Settings["static_host_map"] = map[interface{}]interface{}{"10.128.0.2": []interface{}{"1.1.1.1:4242"}}
  210. lh.reload(c, false)
  211. }
  212. func newLHHostRequest(fromAddr *udp.Addr, myVpnIp, queryVpnIp iputil.VpnIp, lhh *LightHouseHandler) testLhReply {
  213. req := &NebulaMeta{
  214. Type: NebulaMeta_HostQuery,
  215. Details: &NebulaMetaDetails{
  216. VpnIp: uint32(queryVpnIp),
  217. },
  218. }
  219. b, err := req.Marshal()
  220. if err != nil {
  221. panic(err)
  222. }
  223. filter := NebulaMeta_HostQueryReply
  224. w := &testEncWriter{
  225. metaFilter: &filter,
  226. }
  227. lhh.HandleRequest(fromAddr, myVpnIp, b, w)
  228. return w.lastReply
  229. }
  230. func newLHHostUpdate(fromAddr *udp.Addr, vpnIp iputil.VpnIp, addrs []*udp.Addr, lhh *LightHouseHandler) {
  231. req := &NebulaMeta{
  232. Type: NebulaMeta_HostUpdateNotification,
  233. Details: &NebulaMetaDetails{
  234. VpnIp: uint32(vpnIp),
  235. Ip4AndPorts: make([]*Ip4AndPort, len(addrs)),
  236. },
  237. }
  238. for k, v := range addrs {
  239. req.Details.Ip4AndPorts[k] = &Ip4AndPort{Ip: uint32(iputil.Ip2VpnIp(v.IP)), Port: uint32(v.Port)}
  240. }
  241. b, err := req.Marshal()
  242. if err != nil {
  243. panic(err)
  244. }
  245. w := &testEncWriter{}
  246. lhh.HandleRequest(fromAddr, vpnIp, b, w)
  247. }
  248. //TODO: this is a RemoteList test
  249. //func Test_lhRemoteAllowList(t *testing.T) {
  250. // l := NewLogger()
  251. // c := NewConfig(l)
  252. // c.Settings["remoteallowlist"] = map[interface{}]interface{}{
  253. // "10.20.0.0/12": false,
  254. // }
  255. // allowList, err := c.GetAllowList("remoteallowlist", false)
  256. // assert.Nil(t, err)
  257. //
  258. // lh1 := "10.128.0.2"
  259. // lh1IP := net.ParseIP(lh1)
  260. //
  261. // udpServer, _ := NewListener(l, "0.0.0.0", 0, true)
  262. //
  263. // lh := NewLightHouse(l, true, &net.IPNet{IP: net.IP{0, 0, 0, 1}, Mask: net.IPMask{255, 255, 255, 0}}, []uint32{ip2int(lh1IP)}, 10, 10003, udpServer, false, 1, false)
  264. // lh.SetRemoteAllowList(allowList)
  265. //
  266. // // A disallowed ip should not enter the cache but we should end up with an empty entry in the addrMap
  267. // remote1IP := net.ParseIP("10.20.0.3")
  268. // remotes := lh.unlockedGetRemoteList(ip2int(remote1IP))
  269. // remotes.unlockedPrependV4(ip2int(remote1IP), NewIp4AndPort(remote1IP, 4242))
  270. // assert.NotNil(t, lh.addrMap[ip2int(remote1IP)])
  271. // assert.Empty(t, lh.addrMap[ip2int(remote1IP)].CopyAddrs([]*net.IPNet{}))
  272. //
  273. // // Make sure a good ip enters the cache and addrMap
  274. // remote2IP := net.ParseIP("10.128.0.3")
  275. // remote2UDPAddr := NewUDPAddr(remote2IP, uint16(4242))
  276. // lh.addRemoteV4(ip2int(remote2IP), ip2int(remote2IP), NewIp4AndPort(remote2UDPAddr.IP, uint32(remote2UDPAddr.Port)), false, false)
  277. // assertUdpAddrInArray(t, lh.addrMap[ip2int(remote2IP)].CopyAddrs([]*net.IPNet{}), remote2UDPAddr)
  278. //
  279. // // Another good ip gets into the cache, ordering is inverted
  280. // remote3IP := net.ParseIP("10.128.0.4")
  281. // remote3UDPAddr := NewUDPAddr(remote3IP, uint16(4243))
  282. // lh.addRemoteV4(ip2int(remote2IP), ip2int(remote2IP), NewIp4AndPort(remote3UDPAddr.IP, uint32(remote3UDPAddr.Port)), false, false)
  283. // assertUdpAddrInArray(t, lh.addrMap[ip2int(remote2IP)].CopyAddrs([]*net.IPNet{}), remote2UDPAddr, remote3UDPAddr)
  284. //
  285. // // If we exceed the length limit we should only have the most recent addresses
  286. // addedAddrs := []*udpAddr{}
  287. // for i := 0; i < 11; i++ {
  288. // remoteUDPAddr := NewUDPAddr(net.IP{10, 128, 0, 4}, uint16(4243+i))
  289. // lh.addRemoteV4(ip2int(remote2IP), ip2int(remote2IP), NewIp4AndPort(remoteUDPAddr.IP, uint32(remoteUDPAddr.Port)), false, false)
  290. // // The first entry here is a duplicate, don't add it to the assert list
  291. // if i != 0 {
  292. // addedAddrs = append(addedAddrs, remoteUDPAddr)
  293. // }
  294. // }
  295. //
  296. // // We should only have the last 10 of what we tried to add
  297. // assert.True(t, len(addedAddrs) >= 10, "We should have tried to add at least 10 addresses")
  298. // assertUdpAddrInArray(
  299. // t,
  300. // lh.addrMap[ip2int(remote2IP)].CopyAddrs([]*net.IPNet{}),
  301. // addedAddrs[0],
  302. // addedAddrs[1],
  303. // addedAddrs[2],
  304. // addedAddrs[3],
  305. // addedAddrs[4],
  306. // addedAddrs[5],
  307. // addedAddrs[6],
  308. // addedAddrs[7],
  309. // addedAddrs[8],
  310. // addedAddrs[9],
  311. // )
  312. //}
  313. func Test_ipMaskContains(t *testing.T) {
  314. assert.True(t, ipMaskContains(iputil.Ip2VpnIp(net.ParseIP("10.0.0.1")), 32-24, iputil.Ip2VpnIp(net.ParseIP("10.0.0.255"))))
  315. assert.False(t, ipMaskContains(iputil.Ip2VpnIp(net.ParseIP("10.0.0.1")), 32-24, iputil.Ip2VpnIp(net.ParseIP("10.0.1.1"))))
  316. assert.True(t, ipMaskContains(iputil.Ip2VpnIp(net.ParseIP("10.0.0.1")), 32, iputil.Ip2VpnIp(net.ParseIP("10.0.1.1"))))
  317. }
  318. type testLhReply struct {
  319. nebType header.MessageType
  320. nebSubType header.MessageSubType
  321. vpnIp iputil.VpnIp
  322. msg *NebulaMeta
  323. }
  324. type testEncWriter struct {
  325. lastReply testLhReply
  326. metaFilter *NebulaMeta_MessageType
  327. }
  328. func (tw *testEncWriter) SendVia(via *HostInfo, relay *Relay, ad, nb, out []byte, nocopy bool) {
  329. }
  330. func (tw *testEncWriter) Handshake(vpnIp iputil.VpnIp) {
  331. }
  332. func (tw *testEncWriter) SendMessageToHostInfo(t header.MessageType, st header.MessageSubType, hostinfo *HostInfo, p, _, _ []byte) {
  333. msg := &NebulaMeta{}
  334. err := msg.Unmarshal(p)
  335. if tw.metaFilter == nil || msg.Type == *tw.metaFilter {
  336. tw.lastReply = testLhReply{
  337. nebType: t,
  338. nebSubType: st,
  339. vpnIp: hostinfo.vpnIp,
  340. msg: msg,
  341. }
  342. }
  343. if err != nil {
  344. panic(err)
  345. }
  346. }
  347. func (tw *testEncWriter) SendMessageToVpnIp(t header.MessageType, st header.MessageSubType, vpnIp iputil.VpnIp, p, _, _ []byte) {
  348. msg := &NebulaMeta{}
  349. err := msg.Unmarshal(p)
  350. if tw.metaFilter == nil || msg.Type == *tw.metaFilter {
  351. tw.lastReply = testLhReply{
  352. nebType: t,
  353. nebSubType: st,
  354. vpnIp: vpnIp,
  355. msg: msg,
  356. }
  357. }
  358. if err != nil {
  359. panic(err)
  360. }
  361. }
  362. // assertIp4InArray asserts every address in want is at the same position in have and that the lengths match
  363. func assertIp4InArray(t *testing.T, have []*Ip4AndPort, want ...*udp.Addr) {
  364. if !assert.Len(t, have, len(want)) {
  365. return
  366. }
  367. for k, w := range want {
  368. if !(have[k].Ip == uint32(iputil.Ip2VpnIp(w.IP)) && have[k].Port == uint32(w.Port)) {
  369. assert.Fail(t, fmt.Sprintf("Response did not contain: %v:%v at %v; %v", w.IP, w.Port, k, translateV4toUdpAddr(have)))
  370. }
  371. }
  372. }
  373. // assertUdpAddrInArray asserts every address in want is at the same position in have and that the lengths match
  374. func assertUdpAddrInArray(t *testing.T, have []*udp.Addr, want ...*udp.Addr) {
  375. if !assert.Len(t, have, len(want)) {
  376. return
  377. }
  378. for k, w := range want {
  379. if !(have[k].IP.Equal(w.IP) && have[k].Port == w.Port) {
  380. assert.Fail(t, fmt.Sprintf("Response did not contain: %v at %v; %v", w, k, have))
  381. }
  382. }
  383. }
  384. func translateV4toUdpAddr(ips []*Ip4AndPort) []*udp.Addr {
  385. addrs := make([]*udp.Addr, len(ips))
  386. for k, v := range ips {
  387. addrs[k] = NewUDPAddrFromLH4(v)
  388. }
  389. return addrs
  390. }