handshakes_test.go 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. //go:build e2e_testing
  2. // +build e2e_testing
  3. package e2e
  4. import (
  5. "net/netip"
  6. "slices"
  7. "testing"
  8. "time"
  9. "github.com/google/gopacket"
  10. "github.com/google/gopacket/layers"
  11. "github.com/sirupsen/logrus"
  12. "github.com/slackhq/nebula"
  13. "github.com/slackhq/nebula/e2e/router"
  14. "github.com/slackhq/nebula/header"
  15. "github.com/slackhq/nebula/udp"
  16. "github.com/stretchr/testify/assert"
  17. "gopkg.in/yaml.v2"
  18. )
  19. func BenchmarkHotPath(b *testing.B) {
  20. ca, _, caKey, _ := NewTestCaCert(time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  21. myControl, _, _, _ := newSimpleServer(ca, caKey, "me", "10.128.0.1/24", nil)
  22. theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them", "10.128.0.2/24", nil)
  23. // Put their info in our lighthouse
  24. myControl.InjectLightHouseAddr(theirVpnIpNet.Addr(), theirUdpAddr)
  25. // Start the servers
  26. myControl.Start()
  27. theirControl.Start()
  28. r := router.NewR(b, myControl, theirControl)
  29. r.CancelFlowLogs()
  30. for n := 0; n < b.N; n++ {
  31. myControl.InjectTunUDPPacket(theirVpnIpNet.Addr(), 80, 80, []byte("Hi from me"))
  32. _ = r.RouteForAllUntilTxTun(theirControl)
  33. }
  34. myControl.Stop()
  35. theirControl.Stop()
  36. }
  37. func TestGoodHandshake(t *testing.T) {
  38. ca, _, caKey, _ := NewTestCaCert(time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  39. myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(ca, caKey, "me", "10.128.0.1/24", nil)
  40. theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them", "10.128.0.2/24", nil)
  41. // Put their info in our lighthouse
  42. myControl.InjectLightHouseAddr(theirVpnIpNet.Addr(), theirUdpAddr)
  43. // Start the servers
  44. myControl.Start()
  45. theirControl.Start()
  46. t.Log("Send a udp packet through to begin standing up the tunnel, this should come out the other side")
  47. myControl.InjectTunUDPPacket(theirVpnIpNet.Addr(), 80, 80, []byte("Hi from me"))
  48. t.Log("Have them consume my stage 0 packet. They have a tunnel now")
  49. theirControl.InjectUDPPacket(myControl.GetFromUDP(true))
  50. t.Log("Get their stage 1 packet so that we can play with it")
  51. stage1Packet := theirControl.GetFromUDP(true)
  52. t.Log("I consume a garbage packet with a proper nebula header for our tunnel")
  53. // this should log a statement and get ignored, allowing the real handshake packet to complete the tunnel
  54. badPacket := stage1Packet.Copy()
  55. badPacket.Data = badPacket.Data[:len(badPacket.Data)-header.Len]
  56. myControl.InjectUDPPacket(badPacket)
  57. t.Log("Have me consume their real stage 1 packet. I have a tunnel now")
  58. myControl.InjectUDPPacket(stage1Packet)
  59. t.Log("Wait until we see my cached packet come through")
  60. myControl.WaitForType(1, 0, theirControl)
  61. t.Log("Make sure our host infos are correct")
  62. assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl)
  63. t.Log("Get that cached packet and make sure it looks right")
  64. myCachedPacket := theirControl.GetFromTun(true)
  65. assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), 80, 80)
  66. t.Log("Do a bidirectional tunnel test")
  67. r := router.NewR(t, myControl, theirControl)
  68. defer r.RenderFlow()
  69. assertTunnel(t, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl, r)
  70. r.RenderHostmaps("Final hostmaps", myControl, theirControl)
  71. myControl.Stop()
  72. theirControl.Stop()
  73. //TODO: assert hostmaps
  74. }
  75. func TestWrongResponderHandshake(t *testing.T) {
  76. ca, _, caKey, _ := NewTestCaCert(time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  77. // The IPs here are chosen on purpose:
  78. // The current remote handling will sort by preference, public, and then lexically.
  79. // So we need them to have a higher address than evil (we could apply a preference though)
  80. myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(ca, caKey, "me", "10.128.0.100/24", nil)
  81. theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them", "10.128.0.99/24", nil)
  82. evilControl, evilVpnIp, evilUdpAddr, _ := newSimpleServer(ca, caKey, "evil", "10.128.0.2/24", nil)
  83. // Add their real udp addr, which should be tried after evil.
  84. myControl.InjectLightHouseAddr(theirVpnIpNet.Addr(), theirUdpAddr)
  85. // Put the evil udp addr in for their vpn Ip, this is a case of being lied to by the lighthouse.
  86. myControl.InjectLightHouseAddr(theirVpnIpNet.Addr(), evilUdpAddr)
  87. // Build a router so we don't have to reason who gets which packet
  88. r := router.NewR(t, myControl, theirControl, evilControl)
  89. defer r.RenderFlow()
  90. // Start the servers
  91. myControl.Start()
  92. theirControl.Start()
  93. evilControl.Start()
  94. t.Log("Start the handshake process, we will route until we see our cached packet get sent to them")
  95. myControl.InjectTunUDPPacket(theirVpnIpNet.Addr(), 80, 80, []byte("Hi from me"))
  96. r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
  97. h := &header.H{}
  98. err := h.Parse(p.Data)
  99. if err != nil {
  100. panic(err)
  101. }
  102. if p.To == theirUdpAddr && h.Type == 1 {
  103. return router.RouteAndExit
  104. }
  105. return router.KeepRouting
  106. })
  107. //TODO: Assert pending hostmap - I should have a correct hostinfo for them now
  108. t.Log("My cached packet should be received by them")
  109. myCachedPacket := theirControl.GetFromTun(true)
  110. assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), 80, 80)
  111. t.Log("Test the tunnel with them")
  112. assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl)
  113. assertTunnel(t, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl, r)
  114. t.Log("Flush all packets from all controllers")
  115. r.FlushAll()
  116. t.Log("Ensure ensure I don't have any hostinfo artifacts from evil")
  117. assert.Nil(t, myControl.GetHostInfoByVpnIp(evilVpnIp.Addr(), true), "My pending hostmap should not contain evil")
  118. assert.Nil(t, myControl.GetHostInfoByVpnIp(evilVpnIp.Addr(), false), "My main hostmap should not contain evil")
  119. //NOTE: if evil lost the handshake race it may still have a tunnel since me would reject the handshake since the tunnel is complete
  120. //TODO: assert hostmaps for everyone
  121. r.RenderHostmaps("Final hostmaps", myControl, theirControl, evilControl)
  122. t.Log("Success!")
  123. myControl.Stop()
  124. theirControl.Stop()
  125. }
  126. func TestStage1Race(t *testing.T) {
  127. // This tests ensures that two hosts handshaking with each other at the same time will allow traffic to flow
  128. // But will eventually collapse down to a single tunnel
  129. ca, _, caKey, _ := NewTestCaCert(time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  130. myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(ca, caKey, "me ", "10.128.0.1/24", nil)
  131. theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them", "10.128.0.2/24", nil)
  132. // Put their info in our lighthouse and vice versa
  133. myControl.InjectLightHouseAddr(theirVpnIpNet.Addr(), theirUdpAddr)
  134. theirControl.InjectLightHouseAddr(myVpnIpNet.Addr(), myUdpAddr)
  135. // Build a router so we don't have to reason who gets which packet
  136. r := router.NewR(t, myControl, theirControl)
  137. defer r.RenderFlow()
  138. // Start the servers
  139. myControl.Start()
  140. theirControl.Start()
  141. t.Log("Trigger a handshake to start on both me and them")
  142. myControl.InjectTunUDPPacket(theirVpnIpNet.Addr(), 80, 80, []byte("Hi from me"))
  143. theirControl.InjectTunUDPPacket(myVpnIpNet.Addr(), 80, 80, []byte("Hi from them"))
  144. t.Log("Get both stage 1 handshake packets")
  145. myHsForThem := myControl.GetFromUDP(true)
  146. theirHsForMe := theirControl.GetFromUDP(true)
  147. r.Log("Now inject both stage 1 handshake packets")
  148. r.InjectUDPPacket(theirControl, myControl, theirHsForMe)
  149. r.InjectUDPPacket(myControl, theirControl, myHsForThem)
  150. r.Log("Route until they receive a message packet")
  151. myCachedPacket := r.RouteForAllUntilTxTun(theirControl)
  152. assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), 80, 80)
  153. r.Log("Their cached packet should be received by me")
  154. theirCachedPacket := r.RouteForAllUntilTxTun(myControl)
  155. assertUdpPacket(t, []byte("Hi from them"), theirCachedPacket, theirVpnIpNet.Addr(), myVpnIpNet.Addr(), 80, 80)
  156. r.Log("Do a bidirectional tunnel test")
  157. assertTunnel(t, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl, r)
  158. myHostmapHosts := myControl.ListHostmapHosts(false)
  159. myHostmapIndexes := myControl.ListHostmapIndexes(false)
  160. theirHostmapHosts := theirControl.ListHostmapHosts(false)
  161. theirHostmapIndexes := theirControl.ListHostmapIndexes(false)
  162. // We should have two tunnels on both sides
  163. assert.Len(t, myHostmapHosts, 1)
  164. assert.Len(t, theirHostmapHosts, 1)
  165. assert.Len(t, myHostmapIndexes, 2)
  166. assert.Len(t, theirHostmapIndexes, 2)
  167. r.RenderHostmaps("Starting hostmaps", myControl, theirControl)
  168. r.Log("Spin until connection manager tears down a tunnel")
  169. for len(myControl.GetHostmap().Indexes)+len(theirControl.GetHostmap().Indexes) > 2 {
  170. assertTunnel(t, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl, r)
  171. t.Log("Connection manager hasn't ticked yet")
  172. time.Sleep(time.Second)
  173. }
  174. myFinalHostmapHosts := myControl.ListHostmapHosts(false)
  175. myFinalHostmapIndexes := myControl.ListHostmapIndexes(false)
  176. theirFinalHostmapHosts := theirControl.ListHostmapHosts(false)
  177. theirFinalHostmapIndexes := theirControl.ListHostmapIndexes(false)
  178. // We should only have a single tunnel now on both sides
  179. assert.Len(t, myFinalHostmapHosts, 1)
  180. assert.Len(t, theirFinalHostmapHosts, 1)
  181. assert.Len(t, myFinalHostmapIndexes, 1)
  182. assert.Len(t, theirFinalHostmapIndexes, 1)
  183. r.RenderHostmaps("Final hostmaps", myControl, theirControl)
  184. myControl.Stop()
  185. theirControl.Stop()
  186. }
  187. func TestUncleanShutdownRaceLoser(t *testing.T) {
  188. ca, _, caKey, _ := NewTestCaCert(time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  189. myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(ca, caKey, "me ", "10.128.0.1/24", nil)
  190. theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them", "10.128.0.2/24", nil)
  191. // Teach my how to get to the relay and that their can be reached via the relay
  192. myControl.InjectLightHouseAddr(theirVpnIpNet.Addr(), theirUdpAddr)
  193. theirControl.InjectLightHouseAddr(myVpnIpNet.Addr(), myUdpAddr)
  194. // Build a router so we don't have to reason who gets which packet
  195. r := router.NewR(t, myControl, theirControl)
  196. defer r.RenderFlow()
  197. // Start the servers
  198. myControl.Start()
  199. theirControl.Start()
  200. r.Log("Trigger a handshake from me to them")
  201. myControl.InjectTunUDPPacket(theirVpnIpNet.Addr(), 80, 80, []byte("Hi from me"))
  202. p := r.RouteForAllUntilTxTun(theirControl)
  203. assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), 80, 80)
  204. r.Log("Nuke my hostmap")
  205. myHostmap := myControl.GetHostmap()
  206. myHostmap.Hosts = map[netip.Addr]*nebula.HostInfo{}
  207. myHostmap.Indexes = map[uint32]*nebula.HostInfo{}
  208. myHostmap.RemoteIndexes = map[uint32]*nebula.HostInfo{}
  209. myControl.InjectTunUDPPacket(theirVpnIpNet.Addr(), 80, 80, []byte("Hi from me again"))
  210. p = r.RouteForAllUntilTxTun(theirControl)
  211. assertUdpPacket(t, []byte("Hi from me again"), p, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), 80, 80)
  212. r.Log("Assert the tunnel works")
  213. assertTunnel(t, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl, r)
  214. r.Log("Wait for the dead index to go away")
  215. start := len(theirControl.GetHostmap().Indexes)
  216. for {
  217. assertTunnel(t, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl, r)
  218. if len(theirControl.GetHostmap().Indexes) < start {
  219. break
  220. }
  221. time.Sleep(time.Second)
  222. }
  223. r.RenderHostmaps("Final hostmaps", myControl, theirControl)
  224. }
  225. func TestUncleanShutdownRaceWinner(t *testing.T) {
  226. ca, _, caKey, _ := NewTestCaCert(time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  227. myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(ca, caKey, "me ", "10.128.0.1/24", nil)
  228. theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them", "10.128.0.2/24", nil)
  229. // Teach my how to get to the relay and that their can be reached via the relay
  230. myControl.InjectLightHouseAddr(theirVpnIpNet.Addr(), theirUdpAddr)
  231. theirControl.InjectLightHouseAddr(myVpnIpNet.Addr(), myUdpAddr)
  232. // Build a router so we don't have to reason who gets which packet
  233. r := router.NewR(t, myControl, theirControl)
  234. defer r.RenderFlow()
  235. // Start the servers
  236. myControl.Start()
  237. theirControl.Start()
  238. r.Log("Trigger a handshake from me to them")
  239. myControl.InjectTunUDPPacket(theirVpnIpNet.Addr(), 80, 80, []byte("Hi from me"))
  240. p := r.RouteForAllUntilTxTun(theirControl)
  241. assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), 80, 80)
  242. r.RenderHostmaps("Final hostmaps", myControl, theirControl)
  243. r.Log("Nuke my hostmap")
  244. theirHostmap := theirControl.GetHostmap()
  245. theirHostmap.Hosts = map[netip.Addr]*nebula.HostInfo{}
  246. theirHostmap.Indexes = map[uint32]*nebula.HostInfo{}
  247. theirHostmap.RemoteIndexes = map[uint32]*nebula.HostInfo{}
  248. theirControl.InjectTunUDPPacket(myVpnIpNet.Addr(), 80, 80, []byte("Hi from them again"))
  249. p = r.RouteForAllUntilTxTun(myControl)
  250. assertUdpPacket(t, []byte("Hi from them again"), p, theirVpnIpNet.Addr(), myVpnIpNet.Addr(), 80, 80)
  251. r.RenderHostmaps("Derp hostmaps", myControl, theirControl)
  252. r.Log("Assert the tunnel works")
  253. assertTunnel(t, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl, r)
  254. r.Log("Wait for the dead index to go away")
  255. start := len(myControl.GetHostmap().Indexes)
  256. for {
  257. assertTunnel(t, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl, r)
  258. if len(myControl.GetHostmap().Indexes) < start {
  259. break
  260. }
  261. time.Sleep(time.Second)
  262. }
  263. r.RenderHostmaps("Final hostmaps", myControl, theirControl)
  264. }
  265. func TestRelays(t *testing.T) {
  266. ca, _, caKey, _ := NewTestCaCert(time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  267. myControl, myVpnIpNet, _, _ := newSimpleServer(ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
  268. relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
  269. theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
  270. // Teach my how to get to the relay and that their can be reached via the relay
  271. myControl.InjectLightHouseAddr(relayVpnIpNet.Addr(), relayUdpAddr)
  272. myControl.InjectRelays(theirVpnIpNet.Addr(), []netip.Addr{relayVpnIpNet.Addr()})
  273. relayControl.InjectLightHouseAddr(theirVpnIpNet.Addr(), theirUdpAddr)
  274. // Build a router so we don't have to reason who gets which packet
  275. r := router.NewR(t, myControl, relayControl, theirControl)
  276. defer r.RenderFlow()
  277. // Start the servers
  278. myControl.Start()
  279. relayControl.Start()
  280. theirControl.Start()
  281. t.Log("Trigger a handshake from me to them via the relay")
  282. myControl.InjectTunUDPPacket(theirVpnIpNet.Addr(), 80, 80, []byte("Hi from me"))
  283. p := r.RouteForAllUntilTxTun(theirControl)
  284. r.Log("Assert the tunnel works")
  285. assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), 80, 80)
  286. r.RenderHostmaps("Final hostmaps", myControl, relayControl, theirControl)
  287. //TODO: assert we actually used the relay even though it should be impossible for a tunnel to have occurred without it
  288. }
  289. func TestReestablishRelays(t *testing.T) {
  290. ca, _, caKey, _ := NewTestCaCert(time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  291. myControl, myVpnIpNet, _, _ := newSimpleServer(ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
  292. relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
  293. theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
  294. // Teach my how to get to the relay and that their can be reached via the relay
  295. myControl.InjectLightHouseAddr(relayVpnIpNet.Addr(), relayUdpAddr)
  296. myControl.InjectRelays(theirVpnIpNet.Addr(), []netip.Addr{relayVpnIpNet.Addr()})
  297. relayControl.InjectLightHouseAddr(theirVpnIpNet.Addr(), theirUdpAddr)
  298. // Build a router so we don't have to reason who gets which packet
  299. r := router.NewR(t, myControl, relayControl, theirControl)
  300. defer r.RenderFlow()
  301. // Start the servers
  302. myControl.Start()
  303. relayControl.Start()
  304. theirControl.Start()
  305. t.Log("Trigger a handshake from me to them via the relay")
  306. myControl.InjectTunUDPPacket(theirVpnIpNet.Addr(), 80, 80, []byte("Hi from me"))
  307. p := r.RouteForAllUntilTxTun(theirControl)
  308. r.Log("Assert the tunnel works")
  309. assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), 80, 80)
  310. t.Log("Ensure packet traversal from them to me via the relay")
  311. theirControl.InjectTunUDPPacket(myVpnIpNet.Addr(), 80, 80, []byte("Hi from them"))
  312. p = r.RouteForAllUntilTxTun(myControl)
  313. r.Log("Assert the tunnel works")
  314. assertUdpPacket(t, []byte("Hi from them"), p, theirVpnIpNet.Addr(), myVpnIpNet.Addr(), 80, 80)
  315. // If we break the relay's connection to 'them', 'me' needs to detect and recover the connection
  316. r.Log("Close the tunnel")
  317. relayControl.CloseTunnel(theirVpnIpNet.Addr(), true)
  318. start := len(myControl.GetHostmap().Indexes)
  319. curIndexes := len(myControl.GetHostmap().Indexes)
  320. for curIndexes >= start {
  321. curIndexes = len(myControl.GetHostmap().Indexes)
  322. r.Logf("Wait for the dead index to go away:start=%v indexes, current=%v indexes", start, curIndexes)
  323. myControl.InjectTunUDPPacket(theirVpnIpNet.Addr(), 80, 80, []byte("Hi from me should fail"))
  324. r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
  325. return router.RouteAndExit
  326. })
  327. time.Sleep(2 * time.Second)
  328. }
  329. r.Log("Dead index went away. Woot!")
  330. r.RenderHostmaps("Me removed hostinfo", myControl, relayControl, theirControl)
  331. // Next packet should re-establish a relayed connection and work just great.
  332. t.Logf("Assert the tunnel...")
  333. for {
  334. t.Log("RouteForAllUntilTxTun")
  335. myControl.InjectLightHouseAddr(relayVpnIpNet.Addr(), relayUdpAddr)
  336. myControl.InjectRelays(theirVpnIpNet.Addr(), []netip.Addr{relayVpnIpNet.Addr()})
  337. relayControl.InjectLightHouseAddr(theirVpnIpNet.Addr(), theirUdpAddr)
  338. myControl.InjectTunUDPPacket(theirVpnIpNet.Addr(), 80, 80, []byte("Hi from me"))
  339. p = r.RouteForAllUntilTxTun(theirControl)
  340. r.Log("Assert the tunnel works")
  341. packet := gopacket.NewPacket(p, layers.LayerTypeIPv4, gopacket.Lazy)
  342. v4 := packet.Layer(layers.LayerTypeIPv4).(*layers.IPv4)
  343. if slices.Compare(v4.SrcIP, myVpnIpNet.Addr().AsSlice()) != 0 {
  344. t.Logf("SrcIP is unexpected...this is not the packet I'm looking for. Keep looking")
  345. continue
  346. }
  347. if slices.Compare(v4.DstIP, theirVpnIpNet.Addr().AsSlice()) != 0 {
  348. t.Logf("DstIP is unexpected...this is not the packet I'm looking for. Keep looking")
  349. continue
  350. }
  351. udp := packet.Layer(layers.LayerTypeUDP).(*layers.UDP)
  352. if udp == nil {
  353. t.Log("Not a UDP packet. This is not the packet I'm looking for. Keep looking")
  354. continue
  355. }
  356. data := packet.ApplicationLayer()
  357. if data == nil {
  358. t.Log("No data found in packet. This is not the packet I'm looking for. Keep looking.")
  359. continue
  360. }
  361. if string(data.Payload()) != "Hi from me" {
  362. t.Logf("Unexpected payload: '%v', keep looking", string(data.Payload()))
  363. continue
  364. }
  365. t.Log("I found my lost packet. I am so happy.")
  366. break
  367. }
  368. t.Log("Assert the tunnel works the other way, too")
  369. for {
  370. t.Log("RouteForAllUntilTxTun")
  371. theirControl.InjectTunUDPPacket(myVpnIpNet.Addr(), 80, 80, []byte("Hi from them"))
  372. p = r.RouteForAllUntilTxTun(myControl)
  373. r.Log("Assert the tunnel works")
  374. packet := gopacket.NewPacket(p, layers.LayerTypeIPv4, gopacket.Lazy)
  375. v4 := packet.Layer(layers.LayerTypeIPv4).(*layers.IPv4)
  376. if slices.Compare(v4.DstIP, myVpnIpNet.Addr().AsSlice()) != 0 {
  377. t.Logf("Dst is unexpected...this is not the packet I'm looking for. Keep looking")
  378. continue
  379. }
  380. if slices.Compare(v4.SrcIP, theirVpnIpNet.Addr().AsSlice()) != 0 {
  381. t.Logf("SrcIP is unexpected...this is not the packet I'm looking for. Keep looking")
  382. continue
  383. }
  384. udp := packet.Layer(layers.LayerTypeUDP).(*layers.UDP)
  385. if udp == nil {
  386. t.Log("Not a UDP packet. This is not the packet I'm looking for. Keep looking")
  387. continue
  388. }
  389. data := packet.ApplicationLayer()
  390. if data == nil {
  391. t.Log("No data found in packet. This is not the packet I'm looking for. Keep looking.")
  392. continue
  393. }
  394. if string(data.Payload()) != "Hi from them" {
  395. t.Logf("Unexpected payload: '%v', keep looking", string(data.Payload()))
  396. continue
  397. }
  398. t.Log("I found my lost packet. I am so happy.")
  399. break
  400. }
  401. r.RenderHostmaps("Final hostmaps", myControl, relayControl, theirControl)
  402. }
  403. func TestStage1RaceRelays(t *testing.T) {
  404. //NOTE: this is a race between me and relay resulting in a full tunnel from me to them via relay
  405. ca, _, caKey, _ := NewTestCaCert(time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  406. myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
  407. relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
  408. theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
  409. // Teach my how to get to the relay and that their can be reached via the relay
  410. myControl.InjectLightHouseAddr(relayVpnIpNet.Addr(), relayUdpAddr)
  411. theirControl.InjectLightHouseAddr(relayVpnIpNet.Addr(), relayUdpAddr)
  412. myControl.InjectRelays(theirVpnIpNet.Addr(), []netip.Addr{relayVpnIpNet.Addr()})
  413. theirControl.InjectRelays(myVpnIpNet.Addr(), []netip.Addr{relayVpnIpNet.Addr()})
  414. relayControl.InjectLightHouseAddr(theirVpnIpNet.Addr(), theirUdpAddr)
  415. relayControl.InjectLightHouseAddr(myVpnIpNet.Addr(), myUdpAddr)
  416. // Build a router so we don't have to reason who gets which packet
  417. r := router.NewR(t, myControl, relayControl, theirControl)
  418. defer r.RenderFlow()
  419. // Start the servers
  420. myControl.Start()
  421. relayControl.Start()
  422. theirControl.Start()
  423. r.Log("Get a tunnel between me and relay")
  424. assertTunnel(t, myVpnIpNet.Addr(), relayVpnIpNet.Addr(), myControl, relayControl, r)
  425. r.Log("Get a tunnel between them and relay")
  426. assertTunnel(t, theirVpnIpNet.Addr(), relayVpnIpNet.Addr(), theirControl, relayControl, r)
  427. r.Log("Trigger a handshake from both them and me via relay to them and me")
  428. myControl.InjectTunUDPPacket(theirVpnIpNet.Addr(), 80, 80, []byte("Hi from me"))
  429. theirControl.InjectTunUDPPacket(myVpnIpNet.Addr(), 80, 80, []byte("Hi from them"))
  430. r.Log("Wait for a packet from them to me")
  431. p := r.RouteForAllUntilTxTun(myControl)
  432. _ = p
  433. r.FlushAll()
  434. myControl.Stop()
  435. theirControl.Stop()
  436. relayControl.Stop()
  437. //
  438. ////TODO: assert hostmaps
  439. }
  440. func TestStage1RaceRelays2(t *testing.T) {
  441. //NOTE: this is a race between me and relay resulting in a full tunnel from me to them via relay
  442. ca, _, caKey, _ := NewTestCaCert(time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  443. myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
  444. relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
  445. theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
  446. l := NewTestLogger()
  447. // Teach my how to get to the relay and that their can be reached via the relay
  448. myControl.InjectLightHouseAddr(relayVpnIpNet.Addr(), relayUdpAddr)
  449. theirControl.InjectLightHouseAddr(relayVpnIpNet.Addr(), relayUdpAddr)
  450. myControl.InjectRelays(theirVpnIpNet.Addr(), []netip.Addr{relayVpnIpNet.Addr()})
  451. theirControl.InjectRelays(myVpnIpNet.Addr(), []netip.Addr{relayVpnIpNet.Addr()})
  452. relayControl.InjectLightHouseAddr(theirVpnIpNet.Addr(), theirUdpAddr)
  453. relayControl.InjectLightHouseAddr(myVpnIpNet.Addr(), myUdpAddr)
  454. // Build a router so we don't have to reason who gets which packet
  455. r := router.NewR(t, myControl, relayControl, theirControl)
  456. defer r.RenderFlow()
  457. // Start the servers
  458. myControl.Start()
  459. relayControl.Start()
  460. theirControl.Start()
  461. r.Log("Get a tunnel between me and relay")
  462. l.Info("Get a tunnel between me and relay")
  463. assertTunnel(t, myVpnIpNet.Addr(), relayVpnIpNet.Addr(), myControl, relayControl, r)
  464. r.Log("Get a tunnel between them and relay")
  465. l.Info("Get a tunnel between them and relay")
  466. assertTunnel(t, theirVpnIpNet.Addr(), relayVpnIpNet.Addr(), theirControl, relayControl, r)
  467. r.Log("Trigger a handshake from both them and me via relay to them and me")
  468. l.Info("Trigger a handshake from both them and me via relay to them and me")
  469. myControl.InjectTunUDPPacket(theirVpnIpNet.Addr(), 80, 80, []byte("Hi from me"))
  470. theirControl.InjectTunUDPPacket(myVpnIpNet.Addr(), 80, 80, []byte("Hi from them"))
  471. //r.RouteUntilAfterMsgType(myControl, header.Control, header.MessageNone)
  472. //r.RouteUntilAfterMsgType(theirControl, header.Control, header.MessageNone)
  473. r.Log("Wait for a packet from them to me")
  474. l.Info("Wait for a packet from them to me; myControl")
  475. r.RouteForAllUntilTxTun(myControl)
  476. l.Info("Wait for a packet from them to me; theirControl")
  477. r.RouteForAllUntilTxTun(theirControl)
  478. r.Log("Assert the tunnel works")
  479. l.Info("Assert the tunnel works")
  480. assertTunnel(t, theirVpnIpNet.Addr(), myVpnIpNet.Addr(), theirControl, myControl, r)
  481. t.Log("Wait until we remove extra tunnels")
  482. l.Info("Wait until we remove extra tunnels")
  483. l.WithFields(
  484. logrus.Fields{
  485. "myControl": len(myControl.GetHostmap().Indexes),
  486. "theirControl": len(theirControl.GetHostmap().Indexes),
  487. "relayControl": len(relayControl.GetHostmap().Indexes),
  488. }).Info("Waiting for hostinfos to be removed...")
  489. hostInfos := len(myControl.GetHostmap().Indexes) + len(theirControl.GetHostmap().Indexes) + len(relayControl.GetHostmap().Indexes)
  490. retries := 60
  491. for hostInfos > 6 && retries > 0 {
  492. hostInfos = len(myControl.GetHostmap().Indexes) + len(theirControl.GetHostmap().Indexes) + len(relayControl.GetHostmap().Indexes)
  493. l.WithFields(
  494. logrus.Fields{
  495. "myControl": len(myControl.GetHostmap().Indexes),
  496. "theirControl": len(theirControl.GetHostmap().Indexes),
  497. "relayControl": len(relayControl.GetHostmap().Indexes),
  498. }).Info("Waiting for hostinfos to be removed...")
  499. assertTunnel(t, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl, r)
  500. t.Log("Connection manager hasn't ticked yet")
  501. time.Sleep(time.Second)
  502. retries--
  503. }
  504. r.Log("Assert the tunnel works")
  505. l.Info("Assert the tunnel works")
  506. assertTunnel(t, theirVpnIpNet.Addr(), myVpnIpNet.Addr(), theirControl, myControl, r)
  507. myControl.Stop()
  508. theirControl.Stop()
  509. relayControl.Stop()
  510. //
  511. ////TODO: assert hostmaps
  512. }
  513. func TestRehandshakingRelays(t *testing.T) {
  514. ca, _, caKey, _ := NewTestCaCert(time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  515. myControl, myVpnIpNet, _, _ := newSimpleServer(ca, caKey, "me ", "10.128.0.1/24", m{"relay": m{"use_relays": true}})
  516. relayControl, relayVpnIpNet, relayUdpAddr, relayConfig := newSimpleServer(ca, caKey, "relay ", "10.128.0.128/24", m{"relay": m{"am_relay": true}})
  517. theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
  518. // Teach my how to get to the relay and that their can be reached via the relay
  519. myControl.InjectLightHouseAddr(relayVpnIpNet.Addr(), relayUdpAddr)
  520. myControl.InjectRelays(theirVpnIpNet.Addr(), []netip.Addr{relayVpnIpNet.Addr()})
  521. relayControl.InjectLightHouseAddr(theirVpnIpNet.Addr(), theirUdpAddr)
  522. // Build a router so we don't have to reason who gets which packet
  523. r := router.NewR(t, myControl, relayControl, theirControl)
  524. defer r.RenderFlow()
  525. // Start the servers
  526. myControl.Start()
  527. relayControl.Start()
  528. theirControl.Start()
  529. t.Log("Trigger a handshake from me to them via the relay")
  530. myControl.InjectTunUDPPacket(theirVpnIpNet.Addr(), 80, 80, []byte("Hi from me"))
  531. p := r.RouteForAllUntilTxTun(theirControl)
  532. r.Log("Assert the tunnel works")
  533. assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), 80, 80)
  534. r.RenderHostmaps("working hostmaps", myControl, relayControl, theirControl)
  535. // When I update the certificate for the relay, both me and them will have 2 host infos for the relay,
  536. // and the main host infos will not have any relay state to handle the me<->relay<->them tunnel.
  537. r.Log("Renew relay certificate and spin until me and them sees it")
  538. _, _, myNextPrivKey, myNextPEM := NewTestCert(ca, caKey, "relay", time.Now(), time.Now().Add(5*time.Minute), relayVpnIpNet, nil, []string{"new group"})
  539. caB, err := ca.MarshalToPEM()
  540. if err != nil {
  541. panic(err)
  542. }
  543. relayConfig.Settings["pki"] = m{
  544. "ca": string(caB),
  545. "cert": string(myNextPEM),
  546. "key": string(myNextPrivKey),
  547. }
  548. rc, err := yaml.Marshal(relayConfig.Settings)
  549. assert.NoError(t, err)
  550. relayConfig.ReloadConfigString(string(rc))
  551. for {
  552. r.Log("Assert the tunnel works between myVpnIpNet and relayVpnIpNet")
  553. assertTunnel(t, myVpnIpNet.Addr(), relayVpnIpNet.Addr(), myControl, relayControl, r)
  554. c := myControl.GetHostInfoByVpnIp(relayVpnIpNet.Addr(), false)
  555. if len(c.Cert.Details.Groups) != 0 {
  556. // We have a new certificate now
  557. r.Log("Certificate between my and relay is updated!")
  558. break
  559. }
  560. time.Sleep(time.Second)
  561. }
  562. for {
  563. r.Log("Assert the tunnel works between theirVpnIpNet and relayVpnIpNet")
  564. assertTunnel(t, theirVpnIpNet.Addr(), relayVpnIpNet.Addr(), theirControl, relayControl, r)
  565. c := theirControl.GetHostInfoByVpnIp(relayVpnIpNet.Addr(), false)
  566. if len(c.Cert.Details.Groups) != 0 {
  567. // We have a new certificate now
  568. r.Log("Certificate between their and relay is updated!")
  569. break
  570. }
  571. time.Sleep(time.Second)
  572. }
  573. r.Log("Assert the relay tunnel still works")
  574. assertTunnel(t, theirVpnIpNet.Addr(), myVpnIpNet.Addr(), theirControl, myControl, r)
  575. r.RenderHostmaps("working hostmaps", myControl, relayControl, theirControl)
  576. // We should have two hostinfos on all sides
  577. for len(myControl.GetHostmap().Indexes) != 2 {
  578. t.Logf("Waiting for myControl hostinfos (%v != 2) to get cleaned up from lack of use...", len(myControl.GetHostmap().Indexes))
  579. r.Log("Assert the relay tunnel still works")
  580. assertTunnel(t, theirVpnIpNet.Addr(), myVpnIpNet.Addr(), theirControl, myControl, r)
  581. r.Log("yupitdoes")
  582. time.Sleep(time.Second)
  583. }
  584. t.Logf("myControl hostinfos got cleaned up!")
  585. for len(theirControl.GetHostmap().Indexes) != 2 {
  586. t.Logf("Waiting for theirControl hostinfos (%v != 2) to get cleaned up from lack of use...", len(theirControl.GetHostmap().Indexes))
  587. r.Log("Assert the relay tunnel still works")
  588. assertTunnel(t, theirVpnIpNet.Addr(), myVpnIpNet.Addr(), theirControl, myControl, r)
  589. r.Log("yupitdoes")
  590. time.Sleep(time.Second)
  591. }
  592. t.Logf("theirControl hostinfos got cleaned up!")
  593. for len(relayControl.GetHostmap().Indexes) != 2 {
  594. t.Logf("Waiting for relayControl hostinfos (%v != 2) to get cleaned up from lack of use...", len(relayControl.GetHostmap().Indexes))
  595. r.Log("Assert the relay tunnel still works")
  596. assertTunnel(t, theirVpnIpNet.Addr(), myVpnIpNet.Addr(), theirControl, myControl, r)
  597. r.Log("yupitdoes")
  598. time.Sleep(time.Second)
  599. }
  600. t.Logf("relayControl hostinfos got cleaned up!")
  601. }
  602. func TestRehandshakingRelaysPrimary(t *testing.T) {
  603. // This test is the same as TestRehandshakingRelays but one of the terminal types is a primary swap winner
  604. ca, _, caKey, _ := NewTestCaCert(time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  605. myControl, myVpnIpNet, _, _ := newSimpleServer(ca, caKey, "me ", "10.128.0.128/24", m{"relay": m{"use_relays": true}})
  606. relayControl, relayVpnIpNet, relayUdpAddr, relayConfig := newSimpleServer(ca, caKey, "relay ", "10.128.0.1/24", m{"relay": m{"am_relay": true}})
  607. theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them ", "10.128.0.2/24", m{"relay": m{"use_relays": true}})
  608. // Teach my how to get to the relay and that their can be reached via the relay
  609. myControl.InjectLightHouseAddr(relayVpnIpNet.Addr(), relayUdpAddr)
  610. myControl.InjectRelays(theirVpnIpNet.Addr(), []netip.Addr{relayVpnIpNet.Addr()})
  611. relayControl.InjectLightHouseAddr(theirVpnIpNet.Addr(), theirUdpAddr)
  612. // Build a router so we don't have to reason who gets which packet
  613. r := router.NewR(t, myControl, relayControl, theirControl)
  614. defer r.RenderFlow()
  615. // Start the servers
  616. myControl.Start()
  617. relayControl.Start()
  618. theirControl.Start()
  619. t.Log("Trigger a handshake from me to them via the relay")
  620. myControl.InjectTunUDPPacket(theirVpnIpNet.Addr(), 80, 80, []byte("Hi from me"))
  621. p := r.RouteForAllUntilTxTun(theirControl)
  622. r.Log("Assert the tunnel works")
  623. assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), 80, 80)
  624. r.RenderHostmaps("working hostmaps", myControl, relayControl, theirControl)
  625. // When I update the certificate for the relay, both me and them will have 2 host infos for the relay,
  626. // and the main host infos will not have any relay state to handle the me<->relay<->them tunnel.
  627. r.Log("Renew relay certificate and spin until me and them sees it")
  628. _, _, myNextPrivKey, myNextPEM := NewTestCert(ca, caKey, "relay", time.Now(), time.Now().Add(5*time.Minute), relayVpnIpNet, nil, []string{"new group"})
  629. caB, err := ca.MarshalToPEM()
  630. if err != nil {
  631. panic(err)
  632. }
  633. relayConfig.Settings["pki"] = m{
  634. "ca": string(caB),
  635. "cert": string(myNextPEM),
  636. "key": string(myNextPrivKey),
  637. }
  638. rc, err := yaml.Marshal(relayConfig.Settings)
  639. assert.NoError(t, err)
  640. relayConfig.ReloadConfigString(string(rc))
  641. for {
  642. r.Log("Assert the tunnel works between myVpnIpNet and relayVpnIpNet")
  643. assertTunnel(t, myVpnIpNet.Addr(), relayVpnIpNet.Addr(), myControl, relayControl, r)
  644. c := myControl.GetHostInfoByVpnIp(relayVpnIpNet.Addr(), false)
  645. if len(c.Cert.Details.Groups) != 0 {
  646. // We have a new certificate now
  647. r.Log("Certificate between my and relay is updated!")
  648. break
  649. }
  650. time.Sleep(time.Second)
  651. }
  652. for {
  653. r.Log("Assert the tunnel works between theirVpnIpNet and relayVpnIpNet")
  654. assertTunnel(t, theirVpnIpNet.Addr(), relayVpnIpNet.Addr(), theirControl, relayControl, r)
  655. c := theirControl.GetHostInfoByVpnIp(relayVpnIpNet.Addr(), false)
  656. if len(c.Cert.Details.Groups) != 0 {
  657. // We have a new certificate now
  658. r.Log("Certificate between their and relay is updated!")
  659. break
  660. }
  661. time.Sleep(time.Second)
  662. }
  663. r.Log("Assert the relay tunnel still works")
  664. assertTunnel(t, theirVpnIpNet.Addr(), myVpnIpNet.Addr(), theirControl, myControl, r)
  665. r.RenderHostmaps("working hostmaps", myControl, relayControl, theirControl)
  666. // We should have two hostinfos on all sides
  667. for len(myControl.GetHostmap().Indexes) != 2 {
  668. t.Logf("Waiting for myControl hostinfos (%v != 2) to get cleaned up from lack of use...", len(myControl.GetHostmap().Indexes))
  669. r.Log("Assert the relay tunnel still works")
  670. assertTunnel(t, theirVpnIpNet.Addr(), myVpnIpNet.Addr(), theirControl, myControl, r)
  671. r.Log("yupitdoes")
  672. time.Sleep(time.Second)
  673. }
  674. t.Logf("myControl hostinfos got cleaned up!")
  675. for len(theirControl.GetHostmap().Indexes) != 2 {
  676. t.Logf("Waiting for theirControl hostinfos (%v != 2) to get cleaned up from lack of use...", len(theirControl.GetHostmap().Indexes))
  677. r.Log("Assert the relay tunnel still works")
  678. assertTunnel(t, theirVpnIpNet.Addr(), myVpnIpNet.Addr(), theirControl, myControl, r)
  679. r.Log("yupitdoes")
  680. time.Sleep(time.Second)
  681. }
  682. t.Logf("theirControl hostinfos got cleaned up!")
  683. for len(relayControl.GetHostmap().Indexes) != 2 {
  684. t.Logf("Waiting for relayControl hostinfos (%v != 2) to get cleaned up from lack of use...", len(relayControl.GetHostmap().Indexes))
  685. r.Log("Assert the relay tunnel still works")
  686. assertTunnel(t, theirVpnIpNet.Addr(), myVpnIpNet.Addr(), theirControl, myControl, r)
  687. r.Log("yupitdoes")
  688. time.Sleep(time.Second)
  689. }
  690. t.Logf("relayControl hostinfos got cleaned up!")
  691. }
  692. func TestRehandshaking(t *testing.T) {
  693. ca, _, caKey, _ := NewTestCaCert(time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  694. myControl, myVpnIpNet, myUdpAddr, myConfig := newSimpleServer(ca, caKey, "me ", "10.128.0.2/24", nil)
  695. theirControl, theirVpnIpNet, theirUdpAddr, theirConfig := newSimpleServer(ca, caKey, "them", "10.128.0.1/24", nil)
  696. // Put their info in our lighthouse and vice versa
  697. myControl.InjectLightHouseAddr(theirVpnIpNet.Addr(), theirUdpAddr)
  698. theirControl.InjectLightHouseAddr(myVpnIpNet.Addr(), myUdpAddr)
  699. // Build a router so we don't have to reason who gets which packet
  700. r := router.NewR(t, myControl, theirControl)
  701. defer r.RenderFlow()
  702. // Start the servers
  703. myControl.Start()
  704. theirControl.Start()
  705. t.Log("Stand up a tunnel between me and them")
  706. assertTunnel(t, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl, r)
  707. r.RenderHostmaps("Starting hostmaps", myControl, theirControl)
  708. r.Log("Renew my certificate and spin until their sees it")
  709. _, _, myNextPrivKey, myNextPEM := NewTestCert(ca, caKey, "me", time.Now(), time.Now().Add(5*time.Minute), myVpnIpNet, nil, []string{"new group"})
  710. caB, err := ca.MarshalToPEM()
  711. if err != nil {
  712. panic(err)
  713. }
  714. myConfig.Settings["pki"] = m{
  715. "ca": string(caB),
  716. "cert": string(myNextPEM),
  717. "key": string(myNextPrivKey),
  718. }
  719. rc, err := yaml.Marshal(myConfig.Settings)
  720. assert.NoError(t, err)
  721. myConfig.ReloadConfigString(string(rc))
  722. for {
  723. assertTunnel(t, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl, r)
  724. c := theirControl.GetHostInfoByVpnIp(myVpnIpNet.Addr(), false)
  725. if len(c.Cert.Details.Groups) != 0 {
  726. // We have a new certificate now
  727. break
  728. }
  729. time.Sleep(time.Second)
  730. }
  731. // Flip their firewall to only allowing the new group to catch the tunnels reverting incorrectly
  732. rc, err = yaml.Marshal(theirConfig.Settings)
  733. assert.NoError(t, err)
  734. var theirNewConfig m
  735. assert.NoError(t, yaml.Unmarshal(rc, &theirNewConfig))
  736. theirFirewall := theirNewConfig["firewall"].(map[interface{}]interface{})
  737. theirFirewall["inbound"] = []m{{
  738. "proto": "any",
  739. "port": "any",
  740. "group": "new group",
  741. }}
  742. rc, err = yaml.Marshal(theirNewConfig)
  743. assert.NoError(t, err)
  744. theirConfig.ReloadConfigString(string(rc))
  745. r.Log("Spin until there is only 1 tunnel")
  746. for len(myControl.GetHostmap().Indexes)+len(theirControl.GetHostmap().Indexes) > 2 {
  747. assertTunnel(t, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl, r)
  748. t.Log("Connection manager hasn't ticked yet")
  749. time.Sleep(time.Second)
  750. }
  751. assertTunnel(t, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl, r)
  752. myFinalHostmapHosts := myControl.ListHostmapHosts(false)
  753. myFinalHostmapIndexes := myControl.ListHostmapIndexes(false)
  754. theirFinalHostmapHosts := theirControl.ListHostmapHosts(false)
  755. theirFinalHostmapIndexes := theirControl.ListHostmapIndexes(false)
  756. // Make sure the correct tunnel won
  757. c := theirControl.GetHostInfoByVpnIp(myVpnIpNet.Addr(), false)
  758. assert.Contains(t, c.Cert.Details.Groups, "new group")
  759. // We should only have a single tunnel now on both sides
  760. assert.Len(t, myFinalHostmapHosts, 1)
  761. assert.Len(t, theirFinalHostmapHosts, 1)
  762. assert.Len(t, myFinalHostmapIndexes, 1)
  763. assert.Len(t, theirFinalHostmapIndexes, 1)
  764. r.RenderHostmaps("Final hostmaps", myControl, theirControl)
  765. myControl.Stop()
  766. theirControl.Stop()
  767. }
  768. func TestRehandshakingLoser(t *testing.T) {
  769. // The purpose of this test is that the race loser renews their certificate and rehandshakes. The final tunnel
  770. // Should be the one with the new certificate
  771. ca, _, caKey, _ := NewTestCaCert(time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  772. myControl, myVpnIpNet, myUdpAddr, myConfig := newSimpleServer(ca, caKey, "me ", "10.128.0.2/24", nil)
  773. theirControl, theirVpnIpNet, theirUdpAddr, theirConfig := newSimpleServer(ca, caKey, "them", "10.128.0.1/24", nil)
  774. // Put their info in our lighthouse and vice versa
  775. myControl.InjectLightHouseAddr(theirVpnIpNet.Addr(), theirUdpAddr)
  776. theirControl.InjectLightHouseAddr(myVpnIpNet.Addr(), myUdpAddr)
  777. // Build a router so we don't have to reason who gets which packet
  778. r := router.NewR(t, myControl, theirControl)
  779. defer r.RenderFlow()
  780. // Start the servers
  781. myControl.Start()
  782. theirControl.Start()
  783. t.Log("Stand up a tunnel between me and them")
  784. assertTunnel(t, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl, r)
  785. myControl.GetHostInfoByVpnIp(theirVpnIpNet.Addr(), false)
  786. theirControl.GetHostInfoByVpnIp(myVpnIpNet.Addr(), false)
  787. r.RenderHostmaps("Starting hostmaps", myControl, theirControl)
  788. r.Log("Renew their certificate and spin until mine sees it")
  789. _, _, theirNextPrivKey, theirNextPEM := NewTestCert(ca, caKey, "them", time.Now(), time.Now().Add(5*time.Minute), theirVpnIpNet, nil, []string{"their new group"})
  790. caB, err := ca.MarshalToPEM()
  791. if err != nil {
  792. panic(err)
  793. }
  794. theirConfig.Settings["pki"] = m{
  795. "ca": string(caB),
  796. "cert": string(theirNextPEM),
  797. "key": string(theirNextPrivKey),
  798. }
  799. rc, err := yaml.Marshal(theirConfig.Settings)
  800. assert.NoError(t, err)
  801. theirConfig.ReloadConfigString(string(rc))
  802. for {
  803. assertTunnel(t, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl, r)
  804. theirCertInMe := myControl.GetHostInfoByVpnIp(theirVpnIpNet.Addr(), false)
  805. _, theirNewGroup := theirCertInMe.Cert.Details.InvertedGroups["their new group"]
  806. if theirNewGroup {
  807. break
  808. }
  809. time.Sleep(time.Second)
  810. }
  811. // Flip my firewall to only allowing the new group to catch the tunnels reverting incorrectly
  812. rc, err = yaml.Marshal(myConfig.Settings)
  813. assert.NoError(t, err)
  814. var myNewConfig m
  815. assert.NoError(t, yaml.Unmarshal(rc, &myNewConfig))
  816. theirFirewall := myNewConfig["firewall"].(map[interface{}]interface{})
  817. theirFirewall["inbound"] = []m{{
  818. "proto": "any",
  819. "port": "any",
  820. "group": "their new group",
  821. }}
  822. rc, err = yaml.Marshal(myNewConfig)
  823. assert.NoError(t, err)
  824. myConfig.ReloadConfigString(string(rc))
  825. r.Log("Spin until there is only 1 tunnel")
  826. for len(myControl.GetHostmap().Indexes)+len(theirControl.GetHostmap().Indexes) > 2 {
  827. assertTunnel(t, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl, r)
  828. t.Log("Connection manager hasn't ticked yet")
  829. time.Sleep(time.Second)
  830. }
  831. assertTunnel(t, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl, r)
  832. myFinalHostmapHosts := myControl.ListHostmapHosts(false)
  833. myFinalHostmapIndexes := myControl.ListHostmapIndexes(false)
  834. theirFinalHostmapHosts := theirControl.ListHostmapHosts(false)
  835. theirFinalHostmapIndexes := theirControl.ListHostmapIndexes(false)
  836. // Make sure the correct tunnel won
  837. theirCertInMe := myControl.GetHostInfoByVpnIp(theirVpnIpNet.Addr(), false)
  838. assert.Contains(t, theirCertInMe.Cert.Details.Groups, "their new group")
  839. // We should only have a single tunnel now on both sides
  840. assert.Len(t, myFinalHostmapHosts, 1)
  841. assert.Len(t, theirFinalHostmapHosts, 1)
  842. assert.Len(t, myFinalHostmapIndexes, 1)
  843. assert.Len(t, theirFinalHostmapIndexes, 1)
  844. r.RenderHostmaps("Final hostmaps", myControl, theirControl)
  845. myControl.Stop()
  846. theirControl.Stop()
  847. }
  848. func TestRaceRegression(t *testing.T) {
  849. // This test forces stage 1, stage 2, stage 1 to be received by me from them
  850. // We had a bug where we were not finding the duplicate handshake and responding to the final stage 1 which
  851. // caused a cross-linked hostinfo
  852. ca, _, caKey, _ := NewTestCaCert(time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
  853. myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(ca, caKey, "me", "10.128.0.1/24", nil)
  854. theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them", "10.128.0.2/24", nil)
  855. // Put their info in our lighthouse
  856. myControl.InjectLightHouseAddr(theirVpnIpNet.Addr(), theirUdpAddr)
  857. theirControl.InjectLightHouseAddr(myVpnIpNet.Addr(), myUdpAddr)
  858. // Start the servers
  859. myControl.Start()
  860. theirControl.Start()
  861. //them rx stage:1 initiatorIndex=642843150 responderIndex=0
  862. //me rx stage:1 initiatorIndex=120607833 responderIndex=0
  863. //them rx stage:1 initiatorIndex=642843150 responderIndex=0
  864. //me rx stage:2 initiatorIndex=642843150 responderIndex=3701775874
  865. //me rx stage:1 initiatorIndex=120607833 responderIndex=0
  866. //them rx stage:2 initiatorIndex=120607833 responderIndex=4209862089
  867. t.Log("Start both handshakes")
  868. myControl.InjectTunUDPPacket(theirVpnIpNet.Addr(), 80, 80, []byte("Hi from me"))
  869. theirControl.InjectTunUDPPacket(myVpnIpNet.Addr(), 80, 80, []byte("Hi from them"))
  870. t.Log("Get both stage 1")
  871. myStage1ForThem := myControl.GetFromUDP(true)
  872. theirStage1ForMe := theirControl.GetFromUDP(true)
  873. t.Log("Inject them in a special way")
  874. theirControl.InjectUDPPacket(myStage1ForThem)
  875. myControl.InjectUDPPacket(theirStage1ForMe)
  876. theirControl.InjectUDPPacket(myStage1ForThem)
  877. //TODO: ensure stage 2
  878. t.Log("Get both stage 2")
  879. myStage2ForThem := myControl.GetFromUDP(true)
  880. theirStage2ForMe := theirControl.GetFromUDP(true)
  881. t.Log("Inject them in a special way again")
  882. myControl.InjectUDPPacket(theirStage2ForMe)
  883. myControl.InjectUDPPacket(theirStage1ForMe)
  884. theirControl.InjectUDPPacket(myStage2ForThem)
  885. r := router.NewR(t, myControl, theirControl)
  886. defer r.RenderFlow()
  887. t.Log("Flush the packets")
  888. r.RouteForAllUntilTxTun(myControl)
  889. r.RouteForAllUntilTxTun(theirControl)
  890. r.RenderHostmaps("Starting hostmaps", myControl, theirControl)
  891. t.Log("Make sure the tunnel still works")
  892. assertTunnel(t, myVpnIpNet.Addr(), theirVpnIpNet.Addr(), myControl, theirControl, r)
  893. myControl.Stop()
  894. theirControl.Stop()
  895. }
  896. //TODO: test
  897. // Race winner renews and handshakes
  898. // Race loser renews and handshakes
  899. // Does race winner repin the cert to old?
  900. //TODO: add a test with many lies