1
0

tunnels_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. //go:build e2e_testing
  2. // +build e2e_testing
  3. package e2e
  4. import (
  5. "net/netip"
  6. "testing"
  7. "time"
  8. "github.com/slackhq/nebula/cert"
  9. "github.com/slackhq/nebula/cert_test"
  10. "github.com/slackhq/nebula/e2e/router"
  11. )
  12. func TestDropInactiveTunnels(t *testing.T) {
  13. // The goal of this test is to ensure the shortest inactivity timeout will close the tunnel on both sides
  14. // under ideal conditions
  15. ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  16. myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", m{"tunnels": m{"drop_inactive": true, "inactivity_timeout": "5s"}})
  17. theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", m{"tunnels": m{"drop_inactive": true, "inactivity_timeout": "10m"}})
  18. // Share our underlay information
  19. myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
  20. theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
  21. // Start the servers
  22. myControl.Start()
  23. theirControl.Start()
  24. r := router.NewR(t, myControl, theirControl)
  25. r.Log("Assert the tunnel between me and them works")
  26. assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
  27. r.Log("Go inactive and wait for the tunnels to get dropped")
  28. waitStart := time.Now()
  29. for {
  30. myIndexes := len(myControl.GetHostmap().Indexes)
  31. theirIndexes := len(theirControl.GetHostmap().Indexes)
  32. if myIndexes == 0 && theirIndexes == 0 {
  33. break
  34. }
  35. since := time.Since(waitStart)
  36. r.Logf("my tunnels: %v; their tunnels: %v; duration: %v", myIndexes, theirIndexes, since)
  37. if since > time.Second*30 {
  38. t.Fatal("Tunnel should have been declared inactive after 5 seconds and before 30 seconds")
  39. }
  40. time.Sleep(1 * time.Second)
  41. r.FlushAll()
  42. }
  43. r.Logf("Inactive tunnels were dropped within %v", time.Since(waitStart))
  44. myControl.Stop()
  45. theirControl.Stop()
  46. }
  47. func TestCrossStackRelaysWork(t *testing.T) {
  48. ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  49. myControl, myVpnIpNet, _, _ := newSimpleServer(cert.Version2, ca, caKey, "me ", "10.128.0.1/24,fc00::1/64", m{"relay": m{"use_relays": true}})
  50. relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(cert.Version2, ca, caKey, "relay ", "10.128.0.128/24,fc00::128/64", m{"relay": m{"am_relay": true}})
  51. theirUdp := netip.MustParseAddrPort("10.0.0.2:4242")
  52. theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServerWithUdp(cert.Version2, ca, caKey, "them ", "fc00::2/64", theirUdp, m{"relay": m{"use_relays": true}})
  53. //myVpnV4 := myVpnIpNet[0]
  54. myVpnV6 := myVpnIpNet[1]
  55. relayVpnV4 := relayVpnIpNet[0]
  56. relayVpnV6 := relayVpnIpNet[1]
  57. theirVpnV6 := theirVpnIpNet[0]
  58. // Teach my how to get to the relay and that their can be reached via the relay
  59. myControl.InjectLightHouseAddr(relayVpnV4.Addr(), relayUdpAddr)
  60. myControl.InjectLightHouseAddr(relayVpnV6.Addr(), relayUdpAddr)
  61. myControl.InjectRelays(theirVpnV6.Addr(), []netip.Addr{relayVpnV6.Addr()})
  62. relayControl.InjectLightHouseAddr(theirVpnV6.Addr(), theirUdpAddr)
  63. // Build a router so we don't have to reason who gets which packet
  64. r := router.NewR(t, myControl, relayControl, theirControl)
  65. defer r.RenderFlow()
  66. // Start the servers
  67. myControl.Start()
  68. relayControl.Start()
  69. theirControl.Start()
  70. t.Log("Trigger a handshake from me to them via the relay")
  71. myControl.InjectTunUDPPacket(theirVpnV6.Addr(), 80, myVpnV6.Addr(), 80, []byte("Hi from me"))
  72. p := r.RouteForAllUntilTxTun(theirControl)
  73. r.Log("Assert the tunnel works")
  74. assertUdpPacket(t, []byte("Hi from me"), p, myVpnV6.Addr(), theirVpnV6.Addr(), 80, 80)
  75. t.Log("reply?")
  76. theirControl.InjectTunUDPPacket(myVpnV6.Addr(), 80, theirVpnV6.Addr(), 80, []byte("Hi from them"))
  77. p = r.RouteForAllUntilTxTun(myControl)
  78. assertUdpPacket(t, []byte("Hi from them"), p, theirVpnV6.Addr(), myVpnV6.Addr(), 80, 80)
  79. r.RenderHostmaps("Final hostmaps", myControl, relayControl, theirControl)
  80. //t.Log("finish up")
  81. //myControl.Stop()
  82. //theirControl.Stop()
  83. //relayControl.Stop()
  84. }