helpers_test.go 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //go:build e2e_testing
  2. // +build e2e_testing
  3. package e2e
  4. import (
  5. "fmt"
  6. "io"
  7. "net/netip"
  8. "os"
  9. "strings"
  10. "testing"
  11. "time"
  12. "dario.cat/mergo"
  13. "github.com/google/gopacket"
  14. "github.com/google/gopacket/layers"
  15. "github.com/sirupsen/logrus"
  16. "github.com/slackhq/nebula"
  17. "github.com/slackhq/nebula/cert"
  18. "github.com/slackhq/nebula/config"
  19. "github.com/slackhq/nebula/e2e/router"
  20. "github.com/stretchr/testify/assert"
  21. "gopkg.in/yaml.v2"
  22. )
  23. type m map[string]interface{}
  24. // newSimpleServer creates a nebula instance with many assumptions
  25. func newSimpleServer(v cert.Version, caCrt cert.Certificate, caKey []byte, name string, sVpnNetworks string, overrides m) (*nebula.Control, []netip.Prefix, netip.AddrPort, *config.C) {
  26. l := NewTestLogger()
  27. var vpnNetworks []netip.Prefix
  28. for _, sn := range strings.Split(sVpnNetworks, ",") {
  29. vpnIpNet, err := netip.ParsePrefix(strings.TrimSpace(sn))
  30. if err != nil {
  31. panic(err)
  32. }
  33. vpnNetworks = append(vpnNetworks, vpnIpNet)
  34. }
  35. if len(vpnNetworks) == 0 {
  36. panic("no vpn networks")
  37. }
  38. var udpAddr netip.AddrPort
  39. if vpnNetworks[0].Addr().Is4() {
  40. budpIp := vpnNetworks[0].Addr().As4()
  41. budpIp[1] -= 128
  42. udpAddr = netip.AddrPortFrom(netip.AddrFrom4(budpIp), 4242)
  43. } else {
  44. budpIp := vpnNetworks[0].Addr().As16()
  45. // beef for funsies
  46. budpIp[2] = 190
  47. budpIp[3] = 239
  48. udpAddr = netip.AddrPortFrom(netip.AddrFrom16(budpIp), 4242)
  49. }
  50. _, _, myPrivKey, myPEM := NewTestCert(v, caCrt, caKey, name, time.Now(), time.Now().Add(5*time.Minute), vpnNetworks, nil, []string{})
  51. caB, err := caCrt.MarshalPEM()
  52. if err != nil {
  53. panic(err)
  54. }
  55. mc := m{
  56. "pki": m{
  57. "ca": string(caB),
  58. "cert": string(myPEM),
  59. "key": string(myPrivKey),
  60. },
  61. //"tun": m{"disabled": true},
  62. "firewall": m{
  63. "outbound": []m{{
  64. "proto": "any",
  65. "port": "any",
  66. "host": "any",
  67. }},
  68. "inbound": []m{{
  69. "proto": "any",
  70. "port": "any",
  71. "host": "any",
  72. }},
  73. },
  74. //"handshakes": m{
  75. // "try_interval": "1s",
  76. //},
  77. "listen": m{
  78. "host": udpAddr.Addr().String(),
  79. "port": udpAddr.Port(),
  80. },
  81. "logging": m{
  82. "timestamp_format": fmt.Sprintf("%v 15:04:05.000000", name),
  83. "level": l.Level.String(),
  84. },
  85. "timers": m{
  86. "pending_deletion_interval": 2,
  87. "connection_alive_interval": 2,
  88. },
  89. }
  90. if overrides != nil {
  91. final := m{}
  92. err = mergo.Merge(&final, overrides, mergo.WithAppendSlice)
  93. if err != nil {
  94. panic(err)
  95. }
  96. err = mergo.Merge(&final, mc, mergo.WithAppendSlice)
  97. if err != nil {
  98. panic(err)
  99. }
  100. mc = final
  101. }
  102. cb, err := yaml.Marshal(mc)
  103. if err != nil {
  104. panic(err)
  105. }
  106. c := config.NewC(l)
  107. c.LoadString(string(cb))
  108. control, err := nebula.Main(c, false, "e2e-test", l, nil)
  109. if err != nil {
  110. panic(err)
  111. }
  112. return control, vpnNetworks, udpAddr, c
  113. }
  114. type doneCb func()
  115. func deadline(t *testing.T, seconds time.Duration) doneCb {
  116. timeout := time.After(seconds * time.Second)
  117. done := make(chan bool)
  118. go func() {
  119. select {
  120. case <-timeout:
  121. t.Fatal("Test did not finish in time")
  122. case <-done:
  123. }
  124. }()
  125. return func() {
  126. done <- true
  127. }
  128. }
  129. func assertTunnel(t *testing.T, vpnIpA, vpnIpB netip.Addr, controlA, controlB *nebula.Control, r *router.R) {
  130. // Send a packet from them to me
  131. controlB.InjectTunUDPPacket(vpnIpA, 80, vpnIpB, 90, []byte("Hi from B"))
  132. bPacket := r.RouteForAllUntilTxTun(controlA)
  133. assertUdpPacket(t, []byte("Hi from B"), bPacket, vpnIpB, vpnIpA, 90, 80)
  134. // And once more from me to them
  135. controlA.InjectTunUDPPacket(vpnIpB, 80, vpnIpA, 90, []byte("Hello from A"))
  136. aPacket := r.RouteForAllUntilTxTun(controlB)
  137. assertUdpPacket(t, []byte("Hello from A"), aPacket, vpnIpA, vpnIpB, 90, 80)
  138. }
  139. func assertHostInfoPair(t *testing.T, addrA, addrB netip.AddrPort, vpnNetsA, vpnNetsB []netip.Prefix, controlA, controlB *nebula.Control) {
  140. // Get both host infos
  141. //TODO: we may want to loop over each vpnAddr and assert all the things
  142. hBinA := controlA.GetHostInfoByVpnAddr(vpnNetsB[0].Addr(), false)
  143. assert.NotNil(t, hBinA, "Host B was not found by vpnAddr in controlA")
  144. hAinB := controlB.GetHostInfoByVpnAddr(vpnNetsA[0].Addr(), false)
  145. assert.NotNil(t, hAinB, "Host A was not found by vpnAddr in controlB")
  146. // Check that both vpn and real addr are correct
  147. assert.EqualValues(t, getAddrs(vpnNetsB), hBinA.VpnAddrs, "Host B VpnIp is wrong in control A")
  148. assert.EqualValues(t, getAddrs(vpnNetsA), hAinB.VpnAddrs, "Host A VpnIp is wrong in control B")
  149. assert.Equal(t, addrB, hBinA.CurrentRemote, "Host B remote is wrong in control A")
  150. assert.Equal(t, addrA, hAinB.CurrentRemote, "Host A remote is wrong in control B")
  151. // Check that our indexes match
  152. assert.Equal(t, hBinA.LocalIndex, hAinB.RemoteIndex, "Host B local index does not match host A remote index")
  153. assert.Equal(t, hBinA.RemoteIndex, hAinB.LocalIndex, "Host B remote index does not match host A local index")
  154. //TODO: Would be nice to assert this memory
  155. //checkIndexes := func(name string, hm *HostMap, hi *HostInfo) {
  156. // hBbyIndex := hmA.Indexes[hBinA.localIndexId]
  157. // assert.NotNil(t, hBbyIndex, "Could not host info by local index in %s", name)
  158. // assert.Equal(t, &hBbyIndex, &hBinA, "%s Indexes map did not point to the right host info", name)
  159. //
  160. // //TODO: remote indexes are susceptible to collision
  161. // hBbyRemoteIndex := hmA.RemoteIndexes[hBinA.remoteIndexId]
  162. // assert.NotNil(t, hBbyIndex, "Could not host info by remote index in %s", name)
  163. // assert.Equal(t, &hBbyRemoteIndex, &hBinA, "%s RemoteIndexes did not point to the right host info", name)
  164. //}
  165. //
  166. //// Check hostmap indexes too
  167. //checkIndexes("hmA", hmA, hBinA)
  168. //checkIndexes("hmB", hmB, hAinB)
  169. }
  170. func assertUdpPacket(t *testing.T, expected, b []byte, fromIp, toIp netip.Addr, fromPort, toPort uint16) {
  171. if toIp.Is6() {
  172. assertUdpPacket6(t, expected, b, fromIp, toIp, fromPort, toPort)
  173. } else {
  174. assertUdpPacket4(t, expected, b, fromIp, toIp, fromPort, toPort)
  175. }
  176. }
  177. func assertUdpPacket6(t *testing.T, expected, b []byte, fromIp, toIp netip.Addr, fromPort, toPort uint16) {
  178. packet := gopacket.NewPacket(b, layers.LayerTypeIPv6, gopacket.Lazy)
  179. v6 := packet.Layer(layers.LayerTypeIPv6).(*layers.IPv6)
  180. assert.NotNil(t, v6, "No ipv6 data found")
  181. assert.Equal(t, fromIp.AsSlice(), []byte(v6.SrcIP), "Source ip was incorrect")
  182. assert.Equal(t, toIp.AsSlice(), []byte(v6.DstIP), "Dest ip was incorrect")
  183. udp := packet.Layer(layers.LayerTypeUDP).(*layers.UDP)
  184. assert.NotNil(t, udp, "No udp data found")
  185. assert.Equal(t, fromPort, uint16(udp.SrcPort), "Source port was incorrect")
  186. assert.Equal(t, toPort, uint16(udp.DstPort), "Dest port was incorrect")
  187. data := packet.ApplicationLayer()
  188. assert.NotNil(t, data)
  189. assert.Equal(t, expected, data.Payload(), "Data was incorrect")
  190. }
  191. func assertUdpPacket4(t *testing.T, expected, b []byte, fromIp, toIp netip.Addr, fromPort, toPort uint16) {
  192. packet := gopacket.NewPacket(b, layers.LayerTypeIPv4, gopacket.Lazy)
  193. v4 := packet.Layer(layers.LayerTypeIPv4).(*layers.IPv4)
  194. assert.NotNil(t, v4, "No ipv4 data found")
  195. assert.Equal(t, fromIp.AsSlice(), []byte(v4.SrcIP), "Source ip was incorrect")
  196. assert.Equal(t, toIp.AsSlice(), []byte(v4.DstIP), "Dest ip was incorrect")
  197. udp := packet.Layer(layers.LayerTypeUDP).(*layers.UDP)
  198. assert.NotNil(t, udp, "No udp data found")
  199. assert.Equal(t, fromPort, uint16(udp.SrcPort), "Source port was incorrect")
  200. assert.Equal(t, toPort, uint16(udp.DstPort), "Dest port was incorrect")
  201. data := packet.ApplicationLayer()
  202. assert.NotNil(t, data)
  203. assert.Equal(t, expected, data.Payload(), "Data was incorrect")
  204. }
  205. func getAddrs(ns []netip.Prefix) []netip.Addr {
  206. var a []netip.Addr
  207. for _, n := range ns {
  208. a = append(a, n.Addr())
  209. }
  210. return a
  211. }
  212. func NewTestLogger() *logrus.Logger {
  213. l := logrus.New()
  214. v := os.Getenv("TEST_LOGS")
  215. if v == "" {
  216. l.SetOutput(io.Discard)
  217. l.SetLevel(logrus.PanicLevel)
  218. return l
  219. }
  220. switch v {
  221. case "2":
  222. l.SetLevel(logrus.DebugLevel)
  223. case "3":
  224. l.SetLevel(logrus.TraceLevel)
  225. default:
  226. l.SetLevel(logrus.InfoLevel)
  227. }
  228. return l
  229. }