lighthouse_test.go 14 KB

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