lighthouse_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. package nebula
  2. import (
  3. "context"
  4. "encoding/binary"
  5. "fmt"
  6. "net/netip"
  7. "testing"
  8. "github.com/gaissmai/bart"
  9. "github.com/slackhq/nebula/cert"
  10. "github.com/slackhq/nebula/config"
  11. "github.com/slackhq/nebula/header"
  12. "github.com/slackhq/nebula/test"
  13. "github.com/stretchr/testify/assert"
  14. "github.com/stretchr/testify/require"
  15. "gopkg.in/yaml.v3"
  16. )
  17. func TestOldIPv4Only(t *testing.T) {
  18. // This test ensures our new ipv6 enabled LH protobuf IpAndPorts works with the old style to enable backwards compatibility
  19. b := []byte{8, 129, 130, 132, 80, 16, 10}
  20. var m V4AddrPort
  21. err := m.Unmarshal(b)
  22. require.NoError(t, err)
  23. ip := netip.MustParseAddr("10.1.1.1")
  24. bp := ip.As4()
  25. assert.Equal(t, binary.BigEndian.Uint32(bp[:]), m.GetAddr())
  26. }
  27. func Test_lhStaticMapping(t *testing.T) {
  28. l := test.NewLogger()
  29. myVpnNet := netip.MustParsePrefix("10.128.0.1/16")
  30. nt := new(bart.Lite)
  31. nt.Insert(myVpnNet)
  32. cs := &CertState{
  33. myVpnNetworks: []netip.Prefix{myVpnNet},
  34. myVpnNetworksTable: nt,
  35. }
  36. lh1 := "10.128.0.2"
  37. c := config.NewC(l)
  38. c.Settings["lighthouse"] = map[string]any{"hosts": []any{lh1}}
  39. c.Settings["static_host_map"] = map[string]any{lh1: []any{"1.1.1.1:4242"}}
  40. _, err := NewLightHouseFromConfig(context.Background(), l, c, cs, nil, nil)
  41. require.NoError(t, err)
  42. lh2 := "10.128.0.3"
  43. c = config.NewC(l)
  44. c.Settings["lighthouse"] = map[string]any{"hosts": []any{lh1, lh2}}
  45. c.Settings["static_host_map"] = map[string]any{lh1: []any{"100.1.1.1:4242"}}
  46. _, err = NewLightHouseFromConfig(context.Background(), l, c, cs, nil, nil)
  47. require.EqualError(t, err, "lighthouse 10.128.0.3 does not have a static_host_map entry")
  48. }
  49. func TestReloadLighthouseInterval(t *testing.T) {
  50. l := test.NewLogger()
  51. myVpnNet := netip.MustParsePrefix("10.128.0.1/16")
  52. nt := new(bart.Lite)
  53. nt.Insert(myVpnNet)
  54. cs := &CertState{
  55. myVpnNetworks: []netip.Prefix{myVpnNet},
  56. myVpnNetworksTable: nt,
  57. }
  58. lh1 := "10.128.0.2"
  59. c := config.NewC(l)
  60. c.Settings["lighthouse"] = map[string]any{
  61. "hosts": []any{lh1},
  62. "interval": "1s",
  63. }
  64. c.Settings["static_host_map"] = map[string]any{lh1: []any{"1.1.1.1:4242"}}
  65. lh, err := NewLightHouseFromConfig(context.Background(), l, c, cs, nil, nil)
  66. require.NoError(t, err)
  67. lh.ifce = &mockEncWriter{}
  68. // The first one routine is kicked off by main.go currently, lets make sure that one dies
  69. require.NoError(t, c.ReloadConfigString("lighthouse:\n interval: 5"))
  70. assert.Equal(t, int64(5), lh.interval.Load())
  71. // Subsequent calls are killed off by the LightHouse.Reload function
  72. require.NoError(t, c.ReloadConfigString("lighthouse:\n interval: 10"))
  73. assert.Equal(t, int64(10), lh.interval.Load())
  74. // If this completes then nothing is stealing our reload routine
  75. require.NoError(t, c.ReloadConfigString("lighthouse:\n interval: 11"))
  76. assert.Equal(t, int64(11), lh.interval.Load())
  77. }
  78. func BenchmarkLighthouseHandleRequest(b *testing.B) {
  79. l := test.NewLogger()
  80. myVpnNet := netip.MustParsePrefix("10.128.0.1/0")
  81. nt := new(bart.Lite)
  82. nt.Insert(myVpnNet)
  83. cs := &CertState{
  84. myVpnNetworks: []netip.Prefix{myVpnNet},
  85. myVpnNetworksTable: nt,
  86. }
  87. c := config.NewC(l)
  88. lh, err := NewLightHouseFromConfig(context.Background(), l, c, cs, nil, nil)
  89. require.NoError(b, err)
  90. hAddr := netip.MustParseAddrPort("4.5.6.7:12345")
  91. hAddr2 := netip.MustParseAddrPort("4.5.6.7:12346")
  92. vpnIp3 := netip.MustParseAddr("0.0.0.3")
  93. lh.addrMap[vpnIp3] = NewRemoteList([]netip.Addr{vpnIp3}, nil)
  94. lh.addrMap[vpnIp3].unlockedSetV4(
  95. vpnIp3,
  96. vpnIp3,
  97. []*V4AddrPort{
  98. netAddrToProtoV4AddrPort(hAddr.Addr(), hAddr.Port()),
  99. netAddrToProtoV4AddrPort(hAddr2.Addr(), hAddr2.Port()),
  100. },
  101. func(netip.Addr, *V4AddrPort) bool { return true },
  102. )
  103. rAddr := netip.MustParseAddrPort("1.2.2.3:12345")
  104. rAddr2 := netip.MustParseAddrPort("1.2.2.3:12346")
  105. vpnIp2 := netip.MustParseAddr("0.0.0.3")
  106. lh.addrMap[vpnIp2] = NewRemoteList([]netip.Addr{vpnIp2}, nil)
  107. lh.addrMap[vpnIp2].unlockedSetV4(
  108. vpnIp3,
  109. vpnIp3,
  110. []*V4AddrPort{
  111. netAddrToProtoV4AddrPort(rAddr.Addr(), rAddr.Port()),
  112. netAddrToProtoV4AddrPort(rAddr2.Addr(), rAddr2.Port()),
  113. },
  114. func(netip.Addr, *V4AddrPort) bool { return true },
  115. )
  116. mw := &mockEncWriter{}
  117. hi := []netip.Addr{vpnIp2}
  118. b.Run("notfound", func(b *testing.B) {
  119. lhh := lh.NewRequestHandler()
  120. req := &NebulaMeta{
  121. Type: NebulaMeta_HostQuery,
  122. Details: &NebulaMetaDetails{
  123. OldVpnAddr: 4,
  124. V4AddrPorts: nil,
  125. },
  126. }
  127. p, err := req.Marshal()
  128. require.NoError(b, err)
  129. for n := 0; n < b.N; n++ {
  130. lhh.HandleRequest(rAddr, hi, p, mw)
  131. }
  132. })
  133. b.Run("found", func(b *testing.B) {
  134. lhh := lh.NewRequestHandler()
  135. req := &NebulaMeta{
  136. Type: NebulaMeta_HostQuery,
  137. Details: &NebulaMetaDetails{
  138. OldVpnAddr: 3,
  139. V4AddrPorts: nil,
  140. },
  141. }
  142. p, err := req.Marshal()
  143. require.NoError(b, err)
  144. for n := 0; n < b.N; n++ {
  145. lhh.HandleRequest(rAddr, hi, p, mw)
  146. }
  147. })
  148. }
  149. func TestLighthouse_Memory(t *testing.T) {
  150. l := test.NewLogger()
  151. myUdpAddr0 := netip.MustParseAddrPort("10.0.0.2:4242")
  152. myUdpAddr1 := netip.MustParseAddrPort("192.168.0.2:4242")
  153. myUdpAddr2 := netip.MustParseAddrPort("172.16.0.2:4242")
  154. myUdpAddr3 := netip.MustParseAddrPort("100.152.0.2:4242")
  155. myUdpAddr4 := netip.MustParseAddrPort("24.15.0.2:4242")
  156. myUdpAddr5 := netip.MustParseAddrPort("192.168.0.2:4243")
  157. myUdpAddr6 := netip.MustParseAddrPort("192.168.0.2:4244")
  158. myUdpAddr7 := netip.MustParseAddrPort("192.168.0.2:4245")
  159. myUdpAddr8 := netip.MustParseAddrPort("192.168.0.2:4246")
  160. myUdpAddr9 := netip.MustParseAddrPort("192.168.0.2:4247")
  161. myUdpAddr10 := netip.MustParseAddrPort("192.168.0.2:4248")
  162. myUdpAddr11 := netip.MustParseAddrPort("192.168.0.2:4249")
  163. myVpnIp := netip.MustParseAddr("10.128.0.2")
  164. theirUdpAddr0 := netip.MustParseAddrPort("10.0.0.3:4242")
  165. theirUdpAddr1 := netip.MustParseAddrPort("192.168.0.3:4242")
  166. theirUdpAddr2 := netip.MustParseAddrPort("172.16.0.3:4242")
  167. theirUdpAddr3 := netip.MustParseAddrPort("100.152.0.3:4242")
  168. theirUdpAddr4 := netip.MustParseAddrPort("24.15.0.3:4242")
  169. theirVpnIp := netip.MustParseAddr("10.128.0.3")
  170. c := config.NewC(l)
  171. c.Settings["lighthouse"] = map[string]any{"am_lighthouse": true}
  172. c.Settings["listen"] = map[string]any{"port": 4242}
  173. myVpnNet := netip.MustParsePrefix("10.128.0.1/24")
  174. nt := new(bart.Lite)
  175. nt.Insert(myVpnNet)
  176. cs := &CertState{
  177. myVpnNetworks: []netip.Prefix{myVpnNet},
  178. myVpnNetworksTable: nt,
  179. }
  180. lh, err := NewLightHouseFromConfig(context.Background(), l, c, cs, nil, nil)
  181. lh.ifce = &mockEncWriter{}
  182. require.NoError(t, err)
  183. lhh := lh.NewRequestHandler()
  184. // Test that my first update responds with just that
  185. newLHHostUpdate(myUdpAddr0, myVpnIp, []netip.AddrPort{myUdpAddr1, myUdpAddr2}, lhh)
  186. r := newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  187. assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr1, myUdpAddr2)
  188. // Ensure we don't accumulate addresses
  189. newLHHostUpdate(myUdpAddr0, myVpnIp, []netip.AddrPort{myUdpAddr3}, lhh)
  190. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  191. assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr3)
  192. // Grow it back to 2
  193. newLHHostUpdate(myUdpAddr0, myVpnIp, []netip.AddrPort{myUdpAddr1, myUdpAddr4}, lhh)
  194. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  195. assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr1, myUdpAddr4)
  196. // Update a different host and ask about it
  197. newLHHostUpdate(theirUdpAddr0, theirVpnIp, []netip.AddrPort{theirUdpAddr1, theirUdpAddr2, theirUdpAddr3, theirUdpAddr4}, lhh)
  198. r = newLHHostRequest(theirUdpAddr0, theirVpnIp, theirVpnIp, lhh)
  199. assertIp4InArray(t, r.msg.Details.V4AddrPorts, theirUdpAddr1, theirUdpAddr2, theirUdpAddr3, theirUdpAddr4)
  200. // Have both hosts ask about the other
  201. r = newLHHostRequest(theirUdpAddr0, theirVpnIp, myVpnIp, lhh)
  202. assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr1, myUdpAddr4)
  203. r = newLHHostRequest(myUdpAddr0, myVpnIp, theirVpnIp, lhh)
  204. assertIp4InArray(t, r.msg.Details.V4AddrPorts, theirUdpAddr1, theirUdpAddr2, theirUdpAddr3, theirUdpAddr4)
  205. // Make sure we didn't get changed
  206. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  207. assertIp4InArray(t, r.msg.Details.V4AddrPorts, myUdpAddr1, myUdpAddr4)
  208. // Ensure proper ordering and limiting
  209. // Send 12 addrs, get 10 back, the last 2 removed, allowing the duplicate to remain (clients dedupe)
  210. newLHHostUpdate(
  211. myUdpAddr0,
  212. myVpnIp,
  213. []netip.AddrPort{
  214. myUdpAddr1,
  215. myUdpAddr2,
  216. myUdpAddr3,
  217. myUdpAddr4,
  218. myUdpAddr5,
  219. myUdpAddr5, //Duplicated on purpose
  220. myUdpAddr6,
  221. myUdpAddr7,
  222. myUdpAddr8,
  223. myUdpAddr9,
  224. myUdpAddr10,
  225. myUdpAddr11, // This should get cut
  226. }, lhh)
  227. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  228. assertIp4InArray(
  229. t,
  230. r.msg.Details.V4AddrPorts,
  231. myUdpAddr1, myUdpAddr2, myUdpAddr3, myUdpAddr4, myUdpAddr5, myUdpAddr5, myUdpAddr6, myUdpAddr7, myUdpAddr8, myUdpAddr9,
  232. )
  233. // Make sure we won't add ips in our vpn network
  234. bad1 := netip.MustParseAddrPort("10.128.0.99:4242")
  235. bad2 := netip.MustParseAddrPort("10.128.0.100:4242")
  236. good := netip.MustParseAddrPort("1.128.0.99:4242")
  237. newLHHostUpdate(myUdpAddr0, myVpnIp, []netip.AddrPort{bad1, bad2, good}, lhh)
  238. r = newLHHostRequest(myUdpAddr0, myVpnIp, myVpnIp, lhh)
  239. assertIp4InArray(t, r.msg.Details.V4AddrPorts, good)
  240. }
  241. func TestLighthouse_reload(t *testing.T) {
  242. l := test.NewLogger()
  243. c := config.NewC(l)
  244. c.Settings["lighthouse"] = map[string]any{"am_lighthouse": true}
  245. c.Settings["listen"] = map[string]any{"port": 4242}
  246. myVpnNet := netip.MustParsePrefix("10.128.0.1/24")
  247. nt := new(bart.Lite)
  248. nt.Insert(myVpnNet)
  249. cs := &CertState{
  250. myVpnNetworks: []netip.Prefix{myVpnNet},
  251. myVpnNetworksTable: nt,
  252. }
  253. lh, err := NewLightHouseFromConfig(context.Background(), l, c, cs, nil, nil)
  254. require.NoError(t, err)
  255. nc := map[string]any{
  256. "static_host_map": map[string]any{
  257. "10.128.0.2": []any{"1.1.1.1:4242"},
  258. },
  259. }
  260. rc, err := yaml.Marshal(nc)
  261. require.NoError(t, err)
  262. c.ReloadConfigString(string(rc))
  263. err = lh.reload(c, false)
  264. require.NoError(t, err)
  265. }
  266. func newLHHostRequest(fromAddr netip.AddrPort, myVpnIp, queryVpnIp netip.Addr, lhh *LightHouseHandler) testLhReply {
  267. req := &NebulaMeta{
  268. Type: NebulaMeta_HostQuery,
  269. Details: &NebulaMetaDetails{},
  270. }
  271. if queryVpnIp.Is4() {
  272. bip := queryVpnIp.As4()
  273. req.Details.OldVpnAddr = binary.BigEndian.Uint32(bip[:])
  274. } else {
  275. req.Details.VpnAddr = netAddrToProtoAddr(queryVpnIp)
  276. }
  277. b, err := req.Marshal()
  278. if err != nil {
  279. panic(err)
  280. }
  281. filter := NebulaMeta_HostQueryReply
  282. w := &testEncWriter{
  283. metaFilter: &filter,
  284. }
  285. lhh.HandleRequest(fromAddr, []netip.Addr{myVpnIp}, b, w)
  286. return w.lastReply
  287. }
  288. func newLHHostUpdate(fromAddr netip.AddrPort, vpnIp netip.Addr, addrs []netip.AddrPort, lhh *LightHouseHandler) {
  289. req := &NebulaMeta{
  290. Type: NebulaMeta_HostUpdateNotification,
  291. Details: &NebulaMetaDetails{},
  292. }
  293. if vpnIp.Is4() {
  294. bip := vpnIp.As4()
  295. req.Details.OldVpnAddr = binary.BigEndian.Uint32(bip[:])
  296. } else {
  297. req.Details.VpnAddr = netAddrToProtoAddr(vpnIp)
  298. }
  299. for _, v := range addrs {
  300. if v.Addr().Is4() {
  301. req.Details.V4AddrPorts = append(req.Details.V4AddrPorts, netAddrToProtoV4AddrPort(v.Addr(), v.Port()))
  302. } else {
  303. req.Details.V6AddrPorts = append(req.Details.V6AddrPorts, netAddrToProtoV6AddrPort(v.Addr(), v.Port()))
  304. }
  305. }
  306. b, err := req.Marshal()
  307. if err != nil {
  308. panic(err)
  309. }
  310. w := &testEncWriter{}
  311. lhh.HandleRequest(fromAddr, []netip.Addr{vpnIp}, b, w)
  312. }
  313. type testLhReply struct {
  314. nebType header.MessageType
  315. nebSubType header.MessageSubType
  316. vpnIp netip.Addr
  317. msg *NebulaMeta
  318. }
  319. type testEncWriter struct {
  320. lastReply testLhReply
  321. metaFilter *NebulaMeta_MessageType
  322. protocolVersion cert.Version
  323. }
  324. func (tw *testEncWriter) SendVia(via *HostInfo, relay *Relay, ad, nb, out []byte, nocopy bool) {
  325. }
  326. func (tw *testEncWriter) Handshake(vpnIp netip.Addr) {
  327. }
  328. func (tw *testEncWriter) SendMessageToHostInfo(t header.MessageType, st header.MessageSubType, hostinfo *HostInfo, p, _, _ []byte) {
  329. msg := &NebulaMeta{}
  330. err := msg.Unmarshal(p)
  331. if tw.metaFilter == nil || msg.Type == *tw.metaFilter {
  332. tw.lastReply = testLhReply{
  333. nebType: t,
  334. nebSubType: st,
  335. vpnIp: hostinfo.vpnAddrs[0],
  336. msg: msg,
  337. }
  338. }
  339. if err != nil {
  340. panic(err)
  341. }
  342. }
  343. func (tw *testEncWriter) SendMessageToVpnAddr(t header.MessageType, st header.MessageSubType, vpnIp netip.Addr, p, _, _ []byte) {
  344. msg := &NebulaMeta{}
  345. err := msg.Unmarshal(p)
  346. if tw.metaFilter == nil || msg.Type == *tw.metaFilter {
  347. tw.lastReply = testLhReply{
  348. nebType: t,
  349. nebSubType: st,
  350. vpnIp: vpnIp,
  351. msg: msg,
  352. }
  353. }
  354. if err != nil {
  355. panic(err)
  356. }
  357. }
  358. func (tw *testEncWriter) GetHostInfo(vpnIp netip.Addr) *HostInfo {
  359. return nil
  360. }
  361. func (tw *testEncWriter) GetCertState() *CertState {
  362. return &CertState{initiatingVersion: tw.protocolVersion}
  363. }
  364. // assertIp4InArray asserts every address in want is at the same position in have and that the lengths match
  365. func assertIp4InArray(t *testing.T, have []*V4AddrPort, want ...netip.AddrPort) {
  366. if !assert.Len(t, have, len(want)) {
  367. return
  368. }
  369. for k, w := range want {
  370. h := protoV4AddrPortToNetAddrPort(have[k])
  371. if !(h == w) {
  372. assert.Fail(t, fmt.Sprintf("Response did not contain: %v at %v, found %v", w, k, h))
  373. }
  374. }
  375. }
  376. func Test_findNetworkUnion(t *testing.T) {
  377. var out netip.Addr
  378. var ok bool
  379. tenDot := netip.MustParsePrefix("10.0.0.0/8")
  380. oneSevenTwo := netip.MustParsePrefix("172.16.0.0/16")
  381. fe80 := netip.MustParsePrefix("fe80::/8")
  382. fc00 := netip.MustParsePrefix("fc00::/7")
  383. a1 := netip.MustParseAddr("10.0.0.1")
  384. afe81 := netip.MustParseAddr("fe80::1")
  385. //simple
  386. out, ok = findNetworkUnion([]netip.Prefix{tenDot}, []netip.Addr{a1})
  387. assert.True(t, ok)
  388. assert.Equal(t, out, a1)
  389. //mixed lengths
  390. out, ok = findNetworkUnion([]netip.Prefix{tenDot}, []netip.Addr{a1, afe81})
  391. assert.True(t, ok)
  392. assert.Equal(t, out, a1)
  393. out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo}, []netip.Addr{a1})
  394. assert.True(t, ok)
  395. assert.Equal(t, out, a1)
  396. //mixed family
  397. out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo, fe80}, []netip.Addr{a1})
  398. assert.True(t, ok)
  399. assert.Equal(t, out, a1)
  400. out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo, fe80}, []netip.Addr{a1, afe81})
  401. assert.True(t, ok)
  402. assert.Equal(t, out, a1)
  403. //ordering
  404. out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo, fe80}, []netip.Addr{afe81, a1})
  405. assert.True(t, ok)
  406. assert.Equal(t, out, a1)
  407. out, ok = findNetworkUnion([]netip.Prefix{fe80, tenDot, oneSevenTwo}, []netip.Addr{afe81, a1})
  408. assert.True(t, ok)
  409. assert.Equal(t, out, afe81)
  410. //some mismatches
  411. out, ok = findNetworkUnion([]netip.Prefix{tenDot, oneSevenTwo, fe80}, []netip.Addr{afe81})
  412. assert.True(t, ok)
  413. assert.Equal(t, out, afe81)
  414. out, ok = findNetworkUnion([]netip.Prefix{oneSevenTwo, fe80}, []netip.Addr{a1, afe81})
  415. assert.True(t, ok)
  416. assert.Equal(t, out, afe81)
  417. //falsey cases
  418. out, ok = findNetworkUnion([]netip.Prefix{oneSevenTwo, fe80}, []netip.Addr{a1})
  419. assert.False(t, ok)
  420. out, ok = findNetworkUnion([]netip.Prefix{fc00, fe80}, []netip.Addr{a1})
  421. assert.False(t, ok)
  422. out, ok = findNetworkUnion([]netip.Prefix{oneSevenTwo, fc00}, []netip.Addr{a1, afe81})
  423. assert.False(t, ok)
  424. out, ok = findNetworkUnion([]netip.Prefix{fc00}, []netip.Addr{a1, afe81})
  425. assert.False(t, ok)
  426. }