handshakes_test.go 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. //go:build e2e_testing
  2. // +build e2e_testing
  3. package e2e
  4. import (
  5. "net"
  6. "testing"
  7. "time"
  8. "github.com/slackhq/nebula"
  9. "github.com/slackhq/nebula/e2e/router"
  10. "github.com/slackhq/nebula/header"
  11. "github.com/slackhq/nebula/iputil"
  12. "github.com/slackhq/nebula/udp"
  13. "github.com/stretchr/testify/assert"
  14. )
  15. func BenchmarkHotPath(b *testing.B) {
  16. ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
  17. myControl, _, _ := newSimpleServer(ca, caKey, "me", net.IP{10, 0, 0, 1}, nil)
  18. theirControl, theirVpnIp, theirUdpAddr := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2}, nil)
  19. // Put their info in our lighthouse
  20. myControl.InjectLightHouseAddr(theirVpnIp, theirUdpAddr)
  21. // Start the servers
  22. myControl.Start()
  23. theirControl.Start()
  24. r := router.NewR(b, myControl, theirControl)
  25. r.CancelFlowLogs()
  26. for n := 0; n < b.N; n++ {
  27. myControl.InjectTunUDPPacket(theirVpnIp, 80, 80, []byte("Hi from me"))
  28. _ = r.RouteForAllUntilTxTun(theirControl)
  29. }
  30. myControl.Stop()
  31. theirControl.Stop()
  32. }
  33. func TestGoodHandshake(t *testing.T) {
  34. ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
  35. myControl, myVpnIp, myUdpAddr := newSimpleServer(ca, caKey, "me", net.IP{10, 0, 0, 1}, nil)
  36. theirControl, theirVpnIp, theirUdpAddr := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2}, nil)
  37. // Put their info in our lighthouse
  38. myControl.InjectLightHouseAddr(theirVpnIp, theirUdpAddr)
  39. // Start the servers
  40. myControl.Start()
  41. theirControl.Start()
  42. t.Log("Send a udp packet through to begin standing up the tunnel, this should come out the other side")
  43. myControl.InjectTunUDPPacket(theirVpnIp, 80, 80, []byte("Hi from me"))
  44. t.Log("Have them consume my stage 0 packet. They have a tunnel now")
  45. theirControl.InjectUDPPacket(myControl.GetFromUDP(true))
  46. t.Log("Get their stage 1 packet so that we can play with it")
  47. stage1Packet := theirControl.GetFromUDP(true)
  48. t.Log("I consume a garbage packet with a proper nebula header for our tunnel")
  49. // this should log a statement and get ignored, allowing the real handshake packet to complete the tunnel
  50. badPacket := stage1Packet.Copy()
  51. badPacket.Data = badPacket.Data[:len(badPacket.Data)-header.Len]
  52. myControl.InjectUDPPacket(badPacket)
  53. t.Log("Have me consume their real stage 1 packet. I have a tunnel now")
  54. myControl.InjectUDPPacket(stage1Packet)
  55. t.Log("Wait until we see my cached packet come through")
  56. myControl.WaitForType(1, 0, theirControl)
  57. t.Log("Make sure our host infos are correct")
  58. assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIp, theirVpnIp, myControl, theirControl)
  59. t.Log("Get that cached packet and make sure it looks right")
  60. myCachedPacket := theirControl.GetFromTun(true)
  61. assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIp, theirVpnIp, 80, 80)
  62. t.Log("Do a bidirectional tunnel test")
  63. r := router.NewR(t, myControl, theirControl)
  64. defer r.RenderFlow()
  65. assertTunnel(t, myVpnIp, theirVpnIp, myControl, theirControl, r)
  66. r.RenderHostmaps("Final hostmaps", myControl, theirControl)
  67. myControl.Stop()
  68. theirControl.Stop()
  69. //TODO: assert hostmaps
  70. }
  71. func TestWrongResponderHandshake(t *testing.T) {
  72. ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
  73. // The IPs here are chosen on purpose:
  74. // The current remote handling will sort by preference, public, and then lexically.
  75. // So we need them to have a higher address than evil (we could apply a preference though)
  76. myControl, myVpnIp, myUdpAddr := newSimpleServer(ca, caKey, "me", net.IP{10, 0, 0, 100}, nil)
  77. theirControl, theirVpnIp, theirUdpAddr := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 99}, nil)
  78. evilControl, evilVpnIp, evilUdpAddr := newSimpleServer(ca, caKey, "evil", net.IP{10, 0, 0, 2}, nil)
  79. // Add their real udp addr, which should be tried after evil.
  80. myControl.InjectLightHouseAddr(theirVpnIp, theirUdpAddr)
  81. // Put the evil udp addr in for their vpn Ip, this is a case of being lied to by the lighthouse.
  82. myControl.InjectLightHouseAddr(theirVpnIp, evilUdpAddr)
  83. // Build a router so we don't have to reason who gets which packet
  84. r := router.NewR(t, myControl, theirControl, evilControl)
  85. defer r.RenderFlow()
  86. // Start the servers
  87. myControl.Start()
  88. theirControl.Start()
  89. evilControl.Start()
  90. t.Log("Start the handshake process, we will route until we see our cached packet get sent to them")
  91. myControl.InjectTunUDPPacket(theirVpnIp, 80, 80, []byte("Hi from me"))
  92. r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
  93. h := &header.H{}
  94. err := h.Parse(p.Data)
  95. if err != nil {
  96. panic(err)
  97. }
  98. if p.ToIp.Equal(theirUdpAddr.IP) && p.ToPort == uint16(theirUdpAddr.Port) && h.Type == 1 {
  99. return router.RouteAndExit
  100. }
  101. return router.KeepRouting
  102. })
  103. //TODO: Assert pending hostmap - I should have a correct hostinfo for them now
  104. t.Log("My cached packet should be received by them")
  105. myCachedPacket := theirControl.GetFromTun(true)
  106. assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIp, theirVpnIp, 80, 80)
  107. t.Log("Test the tunnel with them")
  108. assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIp, theirVpnIp, myControl, theirControl)
  109. assertTunnel(t, myVpnIp, theirVpnIp, myControl, theirControl, r)
  110. t.Log("Flush all packets from all controllers")
  111. r.FlushAll()
  112. t.Log("Ensure ensure I don't have any hostinfo artifacts from evil")
  113. assert.Nil(t, myControl.GetHostInfoByVpnIp(iputil.Ip2VpnIp(evilVpnIp), true), "My pending hostmap should not contain evil")
  114. assert.Nil(t, myControl.GetHostInfoByVpnIp(iputil.Ip2VpnIp(evilVpnIp), false), "My main hostmap should not contain evil")
  115. //NOTE: if evil lost the handshake race it may still have a tunnel since me would reject the handshake since the tunnel is complete
  116. //TODO: assert hostmaps for everyone
  117. r.RenderHostmaps("Final hostmaps", myControl, theirControl, evilControl)
  118. t.Log("Success!")
  119. myControl.Stop()
  120. theirControl.Stop()
  121. }
  122. func Test_Case1_Stage1Race(t *testing.T) {
  123. ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
  124. myControl, myVpnIp, myUdpAddr := newSimpleServer(ca, caKey, "me ", net.IP{10, 0, 0, 1}, nil)
  125. theirControl, theirVpnIp, theirUdpAddr := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2}, nil)
  126. // Put their info in our lighthouse and vice versa
  127. myControl.InjectLightHouseAddr(theirVpnIp, theirUdpAddr)
  128. theirControl.InjectLightHouseAddr(myVpnIp, myUdpAddr)
  129. // Build a router so we don't have to reason who gets which packet
  130. r := router.NewR(t, myControl, theirControl)
  131. defer r.RenderFlow()
  132. // Start the servers
  133. myControl.Start()
  134. theirControl.Start()
  135. t.Log("Trigger a handshake to start on both me and them")
  136. myControl.InjectTunUDPPacket(theirVpnIp, 80, 80, []byte("Hi from me"))
  137. theirControl.InjectTunUDPPacket(myVpnIp, 80, 80, []byte("Hi from them"))
  138. t.Log("Get both stage 1 handshake packets")
  139. myHsForThem := myControl.GetFromUDP(true)
  140. theirHsForMe := theirControl.GetFromUDP(true)
  141. r.Log("Now inject both stage 1 handshake packets")
  142. r.InjectUDPPacket(theirControl, myControl, theirHsForMe)
  143. r.InjectUDPPacket(myControl, theirControl, myHsForThem)
  144. //TODO: they should win, grab their index for me and make sure I use it in the end.
  145. r.Log("They should not have a stage 2 (won the race) but I should send one")
  146. r.InjectUDPPacket(myControl, theirControl, myControl.GetFromUDP(true))
  147. r.Log("Route for me until I send a message packet to them")
  148. r.RouteForAllUntilAfterMsgTypeTo(theirControl, header.Message, header.MessageNone)
  149. t.Log("My cached packet should be received by them")
  150. myCachedPacket := theirControl.GetFromTun(true)
  151. assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIp, theirVpnIp, 80, 80)
  152. t.Log("Route for them until I send a message packet to me")
  153. theirControl.WaitForType(1, 0, myControl)
  154. t.Log("Their cached packet should be received by me")
  155. theirCachedPacket := myControl.GetFromTun(true)
  156. assertUdpPacket(t, []byte("Hi from them"), theirCachedPacket, theirVpnIp, myVpnIp, 80, 80)
  157. t.Log("Do a bidirectional tunnel test")
  158. assertTunnel(t, myVpnIp, theirVpnIp, myControl, theirControl, r)
  159. r.RenderHostmaps("Final hostmaps", myControl, theirControl)
  160. myControl.Stop()
  161. theirControl.Stop()
  162. //TODO: assert hostmaps
  163. }
  164. func TestRelays(t *testing.T) {
  165. ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
  166. myControl, myVpnIp, _ := newSimpleServer(ca, caKey, "me ", net.IP{10, 0, 0, 1}, m{"relay": m{"use_relays": true}})
  167. relayControl, relayVpnIp, relayUdpAddr := newSimpleServer(ca, caKey, "relay ", net.IP{10, 0, 0, 128}, m{"relay": m{"am_relay": true}})
  168. theirControl, theirVpnIp, theirUdpAddr := newSimpleServer(ca, caKey, "them ", net.IP{10, 0, 0, 2}, m{"relay": m{"use_relays": true}})
  169. // Teach my how to get to the relay and that their can be reached via the relay
  170. myControl.InjectLightHouseAddr(relayVpnIp, relayUdpAddr)
  171. myControl.InjectRelays(theirVpnIp, []net.IP{relayVpnIp})
  172. relayControl.InjectLightHouseAddr(theirVpnIp, theirUdpAddr)
  173. // Build a router so we don't have to reason who gets which packet
  174. r := router.NewR(t, myControl, relayControl, theirControl)
  175. defer r.RenderFlow()
  176. // Start the servers
  177. myControl.Start()
  178. relayControl.Start()
  179. theirControl.Start()
  180. t.Log("Trigger a handshake from me to them via the relay")
  181. myControl.InjectTunUDPPacket(theirVpnIp, 80, 80, []byte("Hi from me"))
  182. p := r.RouteForAllUntilTxTun(theirControl)
  183. assertUdpPacket(t, []byte("Hi from me"), p, myVpnIp, theirVpnIp, 80, 80)
  184. r.RenderHostmaps("Final hostmaps", myControl, relayControl, theirControl)
  185. //TODO: assert we actually used the relay even though it should be impossible for a tunnel to have occurred without it
  186. }
  187. //TODO: add a test with many lies