handshakes_test.go 39 KB

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