2
0

handshakes_test.go 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. myControl.Stop()
  67. theirControl.Stop()
  68. //TODO: assert hostmaps
  69. }
  70. func TestWrongResponderHandshake(t *testing.T) {
  71. ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
  72. // The IPs here are chosen on purpose:
  73. // The current remote handling will sort by preference, public, and then lexically.
  74. // So we need them to have a higher address than evil (we could apply a preference though)
  75. myControl, myVpnIp, myUdpAddr := newSimpleServer(ca, caKey, "me", net.IP{10, 0, 0, 100}, nil)
  76. theirControl, theirVpnIp, theirUdpAddr := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 99}, nil)
  77. evilControl, evilVpnIp, evilUdpAddr := newSimpleServer(ca, caKey, "evil", net.IP{10, 0, 0, 2}, nil)
  78. // Add their real udp addr, which should be tried after evil.
  79. myControl.InjectLightHouseAddr(theirVpnIp, theirUdpAddr)
  80. // Put the evil udp addr in for their vpn Ip, this is a case of being lied to by the lighthouse.
  81. myControl.InjectLightHouseAddr(theirVpnIp, evilUdpAddr)
  82. // Build a router so we don't have to reason who gets which packet
  83. r := router.NewR(t, myControl, theirControl, evilControl)
  84. defer r.RenderFlow()
  85. // Start the servers
  86. myControl.Start()
  87. theirControl.Start()
  88. evilControl.Start()
  89. t.Log("Start the handshake process, we will route until we see our cached packet get sent to them")
  90. myControl.InjectTunUDPPacket(theirVpnIp, 80, 80, []byte("Hi from me"))
  91. r.RouteForAllExitFunc(func(p *udp.Packet, c *nebula.Control) router.ExitType {
  92. h := &header.H{}
  93. err := h.Parse(p.Data)
  94. if err != nil {
  95. panic(err)
  96. }
  97. if p.ToIp.Equal(theirUdpAddr.IP) && p.ToPort == uint16(theirUdpAddr.Port) && h.Type == 1 {
  98. return router.RouteAndExit
  99. }
  100. return router.KeepRouting
  101. })
  102. //TODO: Assert pending hostmap - I should have a correct hostinfo for them now
  103. t.Log("My cached packet should be received by them")
  104. myCachedPacket := theirControl.GetFromTun(true)
  105. assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIp, theirVpnIp, 80, 80)
  106. t.Log("Test the tunnel with them")
  107. assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIp, theirVpnIp, myControl, theirControl)
  108. assertTunnel(t, myVpnIp, theirVpnIp, myControl, theirControl, r)
  109. t.Log("Flush all packets from all controllers")
  110. r.FlushAll()
  111. t.Log("Ensure ensure I don't have any hostinfo artifacts from evil")
  112. assert.Nil(t, myControl.GetHostInfoByVpnIp(iputil.Ip2VpnIp(evilVpnIp), true), "My pending hostmap should not contain evil")
  113. assert.Nil(t, myControl.GetHostInfoByVpnIp(iputil.Ip2VpnIp(evilVpnIp), false), "My main hostmap should not contain evil")
  114. //NOTE: if evil lost the handshake race it may still have a tunnel since me would reject the handshake since the tunnel is complete
  115. //TODO: assert hostmaps for everyone
  116. t.Log("Success!")
  117. myControl.Stop()
  118. theirControl.Stop()
  119. }
  120. func Test_Case1_Stage1Race(t *testing.T) {
  121. ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
  122. myControl, myVpnIp, myUdpAddr := newSimpleServer(ca, caKey, "me ", net.IP{10, 0, 0, 1}, nil)
  123. theirControl, theirVpnIp, theirUdpAddr := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2}, nil)
  124. // Put their info in our lighthouse and vice versa
  125. myControl.InjectLightHouseAddr(theirVpnIp, theirUdpAddr)
  126. theirControl.InjectLightHouseAddr(myVpnIp, myUdpAddr)
  127. // Build a router so we don't have to reason who gets which packet
  128. r := router.NewR(t, myControl, theirControl)
  129. defer r.RenderFlow()
  130. // Start the servers
  131. myControl.Start()
  132. theirControl.Start()
  133. t.Log("Trigger a handshake to start on both me and them")
  134. myControl.InjectTunUDPPacket(theirVpnIp, 80, 80, []byte("Hi from me"))
  135. theirControl.InjectTunUDPPacket(myVpnIp, 80, 80, []byte("Hi from them"))
  136. t.Log("Get both stage 1 handshake packets")
  137. myHsForThem := myControl.GetFromUDP(true)
  138. theirHsForMe := theirControl.GetFromUDP(true)
  139. r.Log("Now inject both stage 1 handshake packets")
  140. r.InjectUDPPacket(theirControl, myControl, theirHsForMe)
  141. r.InjectUDPPacket(myControl, theirControl, myHsForThem)
  142. //TODO: they should win, grab their index for me and make sure I use it in the end.
  143. r.Log("They should not have a stage 2 (won the race) but I should send one")
  144. r.InjectUDPPacket(myControl, theirControl, myControl.GetFromUDP(true))
  145. r.Log("Route for me until I send a message packet to them")
  146. r.RouteForAllUntilAfterMsgTypeTo(theirControl, header.Message, header.MessageNone)
  147. t.Log("My cached packet should be received by them")
  148. myCachedPacket := theirControl.GetFromTun(true)
  149. assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIp, theirVpnIp, 80, 80)
  150. t.Log("Route for them until I send a message packet to me")
  151. theirControl.WaitForType(1, 0, myControl)
  152. t.Log("Their cached packet should be received by me")
  153. theirCachedPacket := myControl.GetFromTun(true)
  154. assertUdpPacket(t, []byte("Hi from them"), theirCachedPacket, theirVpnIp, myVpnIp, 80, 80)
  155. t.Log("Do a bidirectional tunnel test")
  156. assertTunnel(t, myVpnIp, theirVpnIp, myControl, theirControl, r)
  157. myControl.Stop()
  158. theirControl.Stop()
  159. //TODO: assert hostmaps
  160. }
  161. func TestRelays(t *testing.T) {
  162. ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
  163. myControl, myVpnIp, _ := newSimpleServer(ca, caKey, "me ", net.IP{10, 0, 0, 1}, m{"relay": m{"use_relays": true}})
  164. relayControl, relayVpnIp, relayUdpAddr := newSimpleServer(ca, caKey, "relay ", net.IP{10, 0, 0, 128}, m{"relay": m{"am_relay": true}})
  165. theirControl, theirVpnIp, theirUdpAddr := newSimpleServer(ca, caKey, "them ", net.IP{10, 0, 0, 2}, m{"relay": m{"use_relays": true}})
  166. // Teach my how to get to the relay and that their can be reached via the relay
  167. myControl.InjectLightHouseAddr(relayVpnIp, relayUdpAddr)
  168. myControl.InjectRelays(theirVpnIp, []net.IP{relayVpnIp})
  169. relayControl.InjectLightHouseAddr(theirVpnIp, theirUdpAddr)
  170. // Build a router so we don't have to reason who gets which packet
  171. r := router.NewR(t, myControl, relayControl, theirControl)
  172. defer r.RenderFlow()
  173. // Start the servers
  174. myControl.Start()
  175. relayControl.Start()
  176. theirControl.Start()
  177. t.Log("Trigger a handshake from me to them via the relay")
  178. myControl.InjectTunUDPPacket(theirVpnIp, 80, 80, []byte("Hi from me"))
  179. p := r.RouteForAllUntilTxTun(theirControl)
  180. assertUdpPacket(t, []byte("Hi from me"), p, myVpnIp, theirVpnIp, 80, 80)
  181. //TODO: assert we actually used the relay even though it should be impossible for a tunnel to have occurred without it
  182. }
  183. //TODO: add a test with many lies