handshakes_test.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. // +build e2e_testing
  2. package e2e
  3. import (
  4. "net"
  5. "testing"
  6. "time"
  7. "github.com/slackhq/nebula"
  8. "github.com/slackhq/nebula/e2e/router"
  9. )
  10. func TestGoodHandshake(t *testing.T) {
  11. ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
  12. myControl, myVpnIp, myUdpAddr := newSimpleServer(ca, caKey, "me", net.IP{10, 0, 0, 1})
  13. theirControl, theirVpnIp, theirUdpAddr := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2})
  14. // Put their info in our lighthouse
  15. myControl.InjectLightHouseAddr(theirVpnIp, theirUdpAddr)
  16. // Start the servers
  17. myControl.Start()
  18. theirControl.Start()
  19. // Send a udp packet through to begin standing up the tunnel, this should come out the other side
  20. myControl.InjectTunUDPPacket(theirVpnIp, 80, 80, []byte("Hi from me"))
  21. // Have them consume my stage 0 packet. They have a tunnel now
  22. theirControl.InjectUDPPacket(myControl.GetFromUDP(true))
  23. // Get their stage 1 packet so that we can play with it
  24. stage1Packet := theirControl.GetFromUDP(true)
  25. // I consume a garbage packet with a proper nebula header for our tunnel
  26. // this should log a statement and get ignored, allowing the real handshake packet to complete the tunnel
  27. badPacket := stage1Packet.Copy()
  28. badPacket.Data = badPacket.Data[:len(badPacket.Data)-nebula.HeaderLen]
  29. myControl.InjectUDPPacket(badPacket)
  30. // Have me consume their real stage 1 packet. I have a tunnel now
  31. myControl.InjectUDPPacket(stage1Packet)
  32. // Wait until we see my cached packet come through
  33. myControl.WaitForType(1, 0, theirControl)
  34. // Make sure our host infos are correct
  35. assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIp, theirVpnIp, myControl, theirControl)
  36. // Get that cached packet and make sure it looks right
  37. myCachedPacket := theirControl.GetFromTun(true)
  38. assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIp, theirVpnIp, 80, 80)
  39. // Do a bidirectional tunnel test
  40. assertTunnel(t, myVpnIp, theirVpnIp, myControl, theirControl, router.NewR(myControl, theirControl))
  41. myControl.Stop()
  42. theirControl.Stop()
  43. //TODO: assert hostmaps
  44. }
  45. func TestWrongResponderHandshake(t *testing.T) {
  46. ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
  47. myControl, myVpnIp, myUdpAddr := newSimpleServer(ca, caKey, "me", net.IP{10, 0, 0, 1})
  48. theirControl, theirVpnIp, theirUdpAddr := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2})
  49. evilControl, evilVpnIp, evilUdpAddr := newSimpleServer(ca, caKey, "evil", net.IP{10, 0, 0, 99})
  50. // Put the evil udp addr in for their vpn Ip, this is a case of being lied to by the lighthouse
  51. myControl.InjectLightHouseAddr(theirVpnIp, evilUdpAddr)
  52. // But also add their real udp addr, which should be tried after evil
  53. myControl.InjectLightHouseAddr(theirVpnIp, theirUdpAddr)
  54. // Build a router so we don't have to reason who gets which packet
  55. r := router.NewR(myControl, theirControl, evilControl)
  56. // Start the servers
  57. myControl.Start()
  58. theirControl.Start()
  59. evilControl.Start()
  60. t.Log("Stand up the tunnel with evil (because the lighthouse cache is lying to us about who it is)")
  61. myControl.InjectTunUDPPacket(theirVpnIp, 80, 80, []byte("Hi from me"))
  62. r.OnceFrom(myControl)
  63. r.OnceFrom(evilControl)
  64. t.Log("I should have a tunnel with evil now and there should not be a cached packet waiting for us")
  65. assertTunnel(t, myVpnIp, evilVpnIp, myControl, evilControl, r)
  66. assertHostInfoPair(t, myUdpAddr, evilUdpAddr, myVpnIp, evilVpnIp, myControl, evilControl)
  67. //TODO: Assert pending hostmap - I should have a correct hostinfo for them now
  68. t.Log("Lets let the messages fly, this time we should have a tunnel with them")
  69. r.OnceFrom(myControl)
  70. r.OnceFrom(theirControl)
  71. t.Log("I should now have a tunnel with them now and my original packet should get there")
  72. r.RouteUntilAfterMsgType(myControl, 1, 0)
  73. myCachedPacket := theirControl.GetFromTun(true)
  74. assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIp, theirVpnIp, 80, 80)
  75. t.Log("I should now have a proper tunnel with them")
  76. assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIp, theirVpnIp, myControl, theirControl)
  77. assertTunnel(t, myVpnIp, theirVpnIp, myControl, theirControl, r)
  78. t.Log("Lets make sure evil is still good")
  79. assertTunnel(t, myVpnIp, evilVpnIp, myControl, evilControl, r)
  80. //TODO: assert hostmaps for everyone
  81. t.Log("Success!")
  82. //TODO: myControl is attempting to shut down 2 tunnels but is blocked on the udp txChan after the first close message
  83. // what we really need here is a way to exit all the go routines loops (there are many)
  84. //myControl.Stop()
  85. //theirControl.Stop()
  86. }
  87. ////TODO: We need to test lies both as the race winner and race loser
  88. //func TestManyWrongResponderHandshake(t *testing.T) {
  89. // ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
  90. //
  91. // myControl, myVpnIp, myUdpAddr := newSimpleServer(ca, caKey, "me", net.IP{10, 0, 0, 99})
  92. // theirControl, theirVpnIp, theirUdpAddr := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2})
  93. // evilControl, evilVpnIp, evilUdpAddr := newSimpleServer(ca, caKey, "evil", net.IP{10, 0, 0, 1})
  94. //
  95. // t.Log("Build a router so we don't have to reason who gets which packet")
  96. // r := newRouter(myControl, theirControl, evilControl)
  97. //
  98. // t.Log("Lets add more than 10 evil addresses, this exceeds the hostinfo remotes limit")
  99. // for i := 0; i < 10; i++ {
  100. // addr := net.UDPAddr{IP: evilUdpAddr.IP, Port: evilUdpAddr.Port + i}
  101. // myControl.InjectLightHouseAddr(theirVpnIp, &addr)
  102. // // We also need to tell our router about it
  103. // r.AddRoute(addr.IP, uint16(addr.Port), evilControl)
  104. // }
  105. //
  106. // // Start the servers
  107. // myControl.Start()
  108. // theirControl.Start()
  109. // evilControl.Start()
  110. //
  111. // t.Log("Stand up the tunnel with evil (because the lighthouse cache is lying to us about who it is)")
  112. // myControl.InjectTunUDPPacket(theirVpnIp, 80, 80, []byte("Hi from me"))
  113. //
  114. // t.Log("We need to spin until we get to the right remote for them")
  115. // getOut := false
  116. // injected := false
  117. // for {
  118. // t.Log("Routing for me and evil while we work through the bad ips")
  119. // r.RouteExitFunc(myControl, func(packet *nebula.UdpPacket, receiver *nebula.Control) exitType {
  120. // // We should stop routing right after we see a packet coming from us to them
  121. // if *receiver == *theirControl {
  122. // getOut = true
  123. // return drainAndExit
  124. // }
  125. //
  126. // // We need to poke our real ip in at some point, this is a well protected check looking for that moment
  127. // if *receiver == *evilControl {
  128. // hi := myControl.GetHostInfoByVpnIP(ip2int(theirVpnIp), true)
  129. // if !injected && len(hi.RemoteAddrs) == 1 {
  130. // t.Log("I am on my last ip for them, time to inject the real one into my lighthouse")
  131. // myControl.InjectLightHouseAddr(theirVpnIp, theirUdpAddr)
  132. // injected = true
  133. // }
  134. // return drainAndExit
  135. // }
  136. //
  137. // return keepRouting
  138. // })
  139. //
  140. // if getOut {
  141. // break
  142. // }
  143. //
  144. // r.RouteForUntilAfterToAddr(evilControl, myUdpAddr, drainAndExit)
  145. // }
  146. //
  147. // t.Log("I should have a tunnel with evil and them, evil should not have a cached packet")
  148. // assertTunnel(t, myVpnIp, evilVpnIp, myControl, evilControl, r)
  149. // evilHostInfo := myControl.GetHostInfoByVpnIP(ip2int(evilVpnIp), false)
  150. // realEvilUdpAddr := &net.UDPAddr{IP: evilHostInfo.CurrentRemote.IP, Port: int(evilHostInfo.CurrentRemote.Port)}
  151. //
  152. // t.Log("Assert mine and evil's host pairs", evilUdpAddr, realEvilUdpAddr)
  153. // assertHostInfoPair(t, myUdpAddr, realEvilUdpAddr, myVpnIp, evilVpnIp, myControl, evilControl)
  154. //
  155. // //t.Log("Draining everyones packets")
  156. // //r.Drain(theirControl)
  157. // //r.DrainAll(myControl, theirControl, evilControl)
  158. // //
  159. // //go func() {
  160. // // for {
  161. // // time.Sleep(10 * time.Millisecond)
  162. // // t.Log(len(theirControl.GetUDPTxChan()))
  163. // // t.Log(len(theirControl.GetTunTxChan()))
  164. // // t.Log(len(myControl.GetUDPTxChan()))
  165. // // t.Log(len(evilControl.GetUDPTxChan()))
  166. // // t.Log("=====")
  167. // // }
  168. // //}()
  169. //
  170. // t.Log("I should have a tunnel with them now and my original packet should get there")
  171. // r.RouteUntilAfterMsgType(myControl, 1, 0)
  172. // myCachedPacket := theirControl.GetFromTun(true)
  173. //
  174. // t.Log("Got the cached packet, lets test the tunnel")
  175. // assertUdpPacket(t, []byte("Hi from me"), myCachedPacket, myVpnIp, theirVpnIp, 80, 80)
  176. //
  177. // t.Log("Testing tunnels with them")
  178. // assertHostInfoPair(t, myUdpAddr, theirUdpAddr, myVpnIp, theirVpnIp, myControl, theirControl)
  179. // assertTunnel(t, myVpnIp, theirVpnIp, myControl, theirControl, r)
  180. //
  181. // t.Log("Testing tunnels with evil")
  182. // assertTunnel(t, myVpnIp, evilVpnIp, myControl, evilControl, r)
  183. //
  184. // //TODO: assert hostmaps for everyone
  185. //}