tunnels_test.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. //go:build e2e_testing
  2. // +build e2e_testing
  3. package e2e
  4. import (
  5. "fmt"
  6. "net/netip"
  7. "testing"
  8. "time"
  9. "github.com/slackhq/nebula/cert"
  10. "github.com/slackhq/nebula/cert_test"
  11. "github.com/slackhq/nebula/e2e/router"
  12. "github.com/stretchr/testify/assert"
  13. "gopkg.in/yaml.v3"
  14. )
  15. func TestDropInactiveTunnels(t *testing.T) {
  16. // The goal of this test is to ensure the shortest inactivity timeout will close the tunnel on both sides
  17. // under ideal conditions
  18. ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  19. myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "me", "10.128.0.1/24", m{"tunnels": m{"drop_inactive": true, "inactivity_timeout": "5s"}})
  20. theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(cert.Version1, ca, caKey, "them", "10.128.0.2/24", m{"tunnels": m{"drop_inactive": true, "inactivity_timeout": "10m"}})
  21. // Share our underlay information
  22. myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
  23. theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
  24. // Start the servers
  25. myControl.Start()
  26. theirControl.Start()
  27. r := router.NewR(t, myControl, theirControl)
  28. r.Log("Assert the tunnel between me and them works")
  29. assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
  30. r.Log("Go inactive and wait for the tunnels to get dropped")
  31. waitStart := time.Now()
  32. for {
  33. myIndexes := len(myControl.GetHostmap().Indexes)
  34. theirIndexes := len(theirControl.GetHostmap().Indexes)
  35. if myIndexes == 0 && theirIndexes == 0 {
  36. break
  37. }
  38. since := time.Since(waitStart)
  39. r.Logf("my tunnels: %v; their tunnels: %v; duration: %v", myIndexes, theirIndexes, since)
  40. if since > time.Second*30 {
  41. t.Fatal("Tunnel should have been declared inactive after 5 seconds and before 30 seconds")
  42. }
  43. time.Sleep(1 * time.Second)
  44. r.FlushAll()
  45. }
  46. r.Logf("Inactive tunnels were dropped within %v", time.Since(waitStart))
  47. myControl.Stop()
  48. theirControl.Stop()
  49. }
  50. func TestCertUpgrade(t *testing.T) {
  51. // The goal of this test is to ensure the shortest inactivity timeout will close the tunnel on both sides
  52. // under ideal conditions
  53. ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  54. caB, err := ca.MarshalPEM()
  55. if err != nil {
  56. panic(err)
  57. }
  58. ca2, _, caKey2, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  59. ca2B, err := ca2.MarshalPEM()
  60. if err != nil {
  61. panic(err)
  62. }
  63. caStr := fmt.Sprintf("%s\n%s", caB, ca2B)
  64. myCert, _, myPrivKey, _ := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "me", time.Now(), time.Now().Add(5*time.Minute), []netip.Prefix{netip.MustParsePrefix("10.128.0.1/24")}, nil, []string{})
  65. _, myCert2Pem := cert_test.NewTestCertDifferentVersion(myCert, cert.Version2, ca2, caKey2)
  66. theirCert, _, theirPrivKey, _ := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "them", time.Now(), time.Now().Add(5*time.Minute), []netip.Prefix{netip.MustParsePrefix("10.128.0.2/24")}, nil, []string{})
  67. theirCert2, _ := cert_test.NewTestCertDifferentVersion(theirCert, cert.Version2, ca2, caKey2)
  68. myControl, myVpnIpNet, myUdpAddr, myC := newServer([]cert.Certificate{ca, ca2}, []cert.Certificate{myCert}, myPrivKey, m{})
  69. theirControl, theirVpnIpNet, theirUdpAddr, _ := newServer([]cert.Certificate{ca, ca2}, []cert.Certificate{theirCert, theirCert2}, theirPrivKey, m{})
  70. // Share our underlay information
  71. myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
  72. theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
  73. // Start the servers
  74. myControl.Start()
  75. theirControl.Start()
  76. r := router.NewR(t, myControl, theirControl)
  77. defer r.RenderFlow()
  78. r.Log("Assert the tunnel between me and them works")
  79. assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
  80. r.Log("yay")
  81. //todo ???
  82. time.Sleep(1 * time.Second)
  83. r.FlushAll()
  84. mc := m{
  85. "pki": m{
  86. "ca": caStr,
  87. "cert": string(myCert2Pem),
  88. "key": string(myPrivKey),
  89. },
  90. //"tun": m{"disabled": true},
  91. "firewall": myC.Settings["firewall"],
  92. //"handshakes": m{
  93. // "try_interval": "1s",
  94. //},
  95. "listen": myC.Settings["listen"],
  96. "logging": myC.Settings["logging"],
  97. "timers": myC.Settings["timers"],
  98. }
  99. cb, err := yaml.Marshal(mc)
  100. if err != nil {
  101. panic(err)
  102. }
  103. r.Logf("reload new v2-only config")
  104. err = myC.ReloadConfigString(string(cb))
  105. assert.NoError(t, err)
  106. r.Log("yay, spin until their sees it")
  107. waitStart := time.Now()
  108. for {
  109. assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
  110. c := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
  111. if c == nil {
  112. r.Log("nil")
  113. } else {
  114. version := c.Cert.Version()
  115. r.Logf("version %d", version)
  116. if version == cert.Version2 {
  117. break
  118. }
  119. }
  120. since := time.Since(waitStart)
  121. if since > time.Second*10 {
  122. t.Fatal("Cert should be new by now")
  123. }
  124. time.Sleep(time.Second)
  125. }
  126. r.RenderHostmaps("Final hostmaps", myControl, theirControl)
  127. myControl.Stop()
  128. theirControl.Stop()
  129. }
  130. func TestCertDowngrade(t *testing.T) {
  131. // The goal of this test is to ensure the shortest inactivity timeout will close the tunnel on both sides
  132. // under ideal conditions
  133. ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  134. caB, err := ca.MarshalPEM()
  135. if err != nil {
  136. panic(err)
  137. }
  138. ca2, _, caKey2, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  139. ca2B, err := ca2.MarshalPEM()
  140. if err != nil {
  141. panic(err)
  142. }
  143. caStr := fmt.Sprintf("%s\n%s", caB, ca2B)
  144. myCert, _, myPrivKey, myCertPem := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "me", time.Now(), time.Now().Add(5*time.Minute), []netip.Prefix{netip.MustParsePrefix("10.128.0.1/24")}, nil, []string{})
  145. myCert2, _ := cert_test.NewTestCertDifferentVersion(myCert, cert.Version2, ca2, caKey2)
  146. theirCert, _, theirPrivKey, _ := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "them", time.Now(), time.Now().Add(5*time.Minute), []netip.Prefix{netip.MustParsePrefix("10.128.0.2/24")}, nil, []string{})
  147. theirCert2, _ := cert_test.NewTestCertDifferentVersion(theirCert, cert.Version2, ca2, caKey2)
  148. myControl, myVpnIpNet, myUdpAddr, myC := newServer([]cert.Certificate{ca, ca2}, []cert.Certificate{myCert2}, myPrivKey, m{})
  149. theirControl, theirVpnIpNet, theirUdpAddr, _ := newServer([]cert.Certificate{ca, ca2}, []cert.Certificate{theirCert, theirCert2}, theirPrivKey, m{})
  150. // Share our underlay information
  151. myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
  152. theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
  153. // Start the servers
  154. myControl.Start()
  155. theirControl.Start()
  156. r := router.NewR(t, myControl, theirControl)
  157. defer r.RenderFlow()
  158. r.Log("Assert the tunnel between me and them works")
  159. //assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
  160. //r.Log("yay")
  161. assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
  162. r.Log("yay")
  163. //todo ???
  164. time.Sleep(1 * time.Second)
  165. r.FlushAll()
  166. mc := m{
  167. "pki": m{
  168. "ca": caStr,
  169. "cert": string(myCertPem),
  170. "key": string(myPrivKey),
  171. },
  172. "firewall": myC.Settings["firewall"],
  173. "listen": myC.Settings["listen"],
  174. "logging": myC.Settings["logging"],
  175. "timers": myC.Settings["timers"],
  176. }
  177. cb, err := yaml.Marshal(mc)
  178. if err != nil {
  179. panic(err)
  180. }
  181. r.Logf("reload new v1-only config")
  182. err = myC.ReloadConfigString(string(cb))
  183. assert.NoError(t, err)
  184. r.Log("yay, spin until their sees it")
  185. waitStart := time.Now()
  186. for {
  187. assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
  188. c := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
  189. c2 := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false)
  190. if c == nil || c2 == nil {
  191. r.Log("nil")
  192. } else {
  193. version := c.Cert.Version()
  194. theirVersion := c2.Cert.Version()
  195. r.Logf("version %d,%d", version, theirVersion)
  196. if version == cert.Version1 {
  197. break
  198. }
  199. }
  200. since := time.Since(waitStart)
  201. if since > time.Second*5 {
  202. r.Log("it is unusual that the cert is not new yet, but not a failure yet")
  203. }
  204. if since > time.Second*10 {
  205. r.Log("wtf")
  206. t.Fatal("Cert should be new by now")
  207. }
  208. time.Sleep(time.Second)
  209. }
  210. r.RenderHostmaps("Final hostmaps", myControl, theirControl)
  211. myControl.Stop()
  212. theirControl.Stop()
  213. }
  214. func TestCertMismatchCorrection(t *testing.T) {
  215. // The goal of this test is to ensure the shortest inactivity timeout will close the tunnel on both sides
  216. // under ideal conditions
  217. ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version1, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  218. ca2, _, caKey2, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  219. myCert, _, myPrivKey, _ := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "me", time.Now(), time.Now().Add(5*time.Minute), []netip.Prefix{netip.MustParsePrefix("10.128.0.1/24")}, nil, []string{})
  220. myCert2, _ := cert_test.NewTestCertDifferentVersion(myCert, cert.Version2, ca2, caKey2)
  221. theirCert, _, theirPrivKey, _ := cert_test.NewTestCert(cert.Version1, cert.Curve_CURVE25519, ca, caKey, "them", time.Now(), time.Now().Add(5*time.Minute), []netip.Prefix{netip.MustParsePrefix("10.128.0.2/24")}, nil, []string{})
  222. theirCert2, _ := cert_test.NewTestCertDifferentVersion(theirCert, cert.Version2, ca2, caKey2)
  223. myControl, myVpnIpNet, myUdpAddr, _ := newServer([]cert.Certificate{ca, ca2}, []cert.Certificate{myCert2}, myPrivKey, m{})
  224. theirControl, theirVpnIpNet, theirUdpAddr, _ := newServer([]cert.Certificate{ca, ca2}, []cert.Certificate{theirCert, theirCert2}, theirPrivKey, m{})
  225. // Share our underlay information
  226. myControl.InjectLightHouseAddr(theirVpnIpNet[0].Addr(), theirUdpAddr)
  227. theirControl.InjectLightHouseAddr(myVpnIpNet[0].Addr(), myUdpAddr)
  228. // Start the servers
  229. myControl.Start()
  230. theirControl.Start()
  231. r := router.NewR(t, myControl, theirControl)
  232. defer r.RenderFlow()
  233. r.Log("Assert the tunnel between me and them works")
  234. //assertTunnel(t, theirVpnIpNet[0].Addr(), myVpnIpNet[0].Addr(), theirControl, myControl, r)
  235. //r.Log("yay")
  236. assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
  237. r.Log("yay")
  238. //todo ???
  239. time.Sleep(1 * time.Second)
  240. r.FlushAll()
  241. waitStart := time.Now()
  242. for {
  243. assertTunnel(t, myVpnIpNet[0].Addr(), theirVpnIpNet[0].Addr(), myControl, theirControl, r)
  244. c := theirControl.GetHostInfoByVpnAddr(myVpnIpNet[0].Addr(), false)
  245. c2 := myControl.GetHostInfoByVpnAddr(theirVpnIpNet[0].Addr(), false)
  246. if c == nil || c2 == nil {
  247. r.Log("nil")
  248. } else {
  249. version := c.Cert.Version()
  250. theirVersion := c2.Cert.Version()
  251. r.Logf("version %d,%d", version, theirVersion)
  252. if version == theirVersion {
  253. break
  254. }
  255. }
  256. since := time.Since(waitStart)
  257. if since > time.Second*5 {
  258. r.Log("wtf")
  259. }
  260. if since > time.Second*10 {
  261. r.Log("wtf")
  262. t.Fatal("Cert should be new by now")
  263. }
  264. time.Sleep(time.Second)
  265. }
  266. r.RenderHostmaps("Final hostmaps", myControl, theirControl)
  267. myControl.Stop()
  268. theirControl.Stop()
  269. }