handshakes_test.go 42 KB

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