helpers_test.go 6.0 KB

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