2021-10-21 15:24:11 -06:00
|
|
|
//go:build e2e_testing
|
2021-03-29 13:29:20 -06:00
|
|
|
// +build e2e_testing
|
|
|
|
|
|
|
|
package e2e
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2024-07-31 09:18:56 -06:00
|
|
|
"net/netip"
|
2021-03-31 09:26:35 -06:00
|
|
|
"os"
|
2021-03-29 13:29:20 -06:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-08-02 12:00:20 -06:00
|
|
|
"dario.cat/mergo"
|
2021-03-29 13:29:20 -06:00
|
|
|
"github.com/google/gopacket"
|
|
|
|
"github.com/google/gopacket/layers"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"github.com/slackhq/nebula"
|
|
|
|
"github.com/slackhq/nebula/cert"
|
2021-11-03 19:54:04 -06:00
|
|
|
"github.com/slackhq/nebula/config"
|
2021-03-31 09:26:35 -06:00
|
|
|
"github.com/slackhq/nebula/e2e/router"
|
2021-03-29 13:29:20 -06:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
type m map[string]interface{}
|
|
|
|
|
|
|
|
// newSimpleServer creates a nebula instance with many assumptions
|
2024-10-10 17:00:22 -06:00
|
|
|
func newSimpleServer(caCrt cert.Certificate, caKey []byte, name string, sVpnIpNet string, overrides m) (*nebula.Control, netip.Prefix, netip.AddrPort, *config.C) {
|
2021-03-31 09:26:35 -06:00
|
|
|
l := NewTestLogger()
|
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
vpnIpNet, err := netip.ParsePrefix(sVpnIpNet)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var udpAddr netip.AddrPort
|
|
|
|
if vpnIpNet.Addr().Is4() {
|
|
|
|
budpIp := vpnIpNet.Addr().As4()
|
|
|
|
budpIp[1] -= 128
|
|
|
|
udpAddr = netip.AddrPortFrom(netip.AddrFrom4(budpIp), 4242)
|
|
|
|
} else {
|
|
|
|
budpIp := vpnIpNet.Addr().As16()
|
|
|
|
budpIp[13] -= 128
|
|
|
|
udpAddr = netip.AddrPortFrom(netip.AddrFrom16(budpIp), 4242)
|
2021-03-31 09:26:35 -06:00
|
|
|
}
|
2024-10-10 17:00:22 -06:00
|
|
|
_, _, myPrivKey, myPEM := NewTestCert(caCrt, caKey, name, time.Now(), time.Now().Add(5*time.Minute), []netip.Prefix{vpnIpNet}, nil, []string{})
|
2021-03-29 13:29:20 -06:00
|
|
|
|
2024-10-10 17:00:22 -06:00
|
|
|
caB, err := caCrt.MarshalPEM()
|
2021-03-29 13:29:20 -06:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mc := m{
|
|
|
|
"pki": m{
|
|
|
|
"ca": string(caB),
|
|
|
|
"cert": string(myPEM),
|
|
|
|
"key": string(myPrivKey),
|
|
|
|
},
|
|
|
|
//"tun": m{"disabled": true},
|
|
|
|
"firewall": m{
|
|
|
|
"outbound": []m{{
|
|
|
|
"proto": "any",
|
|
|
|
"port": "any",
|
|
|
|
"host": "any",
|
|
|
|
}},
|
|
|
|
"inbound": []m{{
|
|
|
|
"proto": "any",
|
|
|
|
"port": "any",
|
|
|
|
"host": "any",
|
|
|
|
}},
|
|
|
|
},
|
2021-04-14 12:50:09 -06:00
|
|
|
//"handshakes": m{
|
|
|
|
// "try_interval": "1s",
|
|
|
|
//},
|
2021-03-29 13:29:20 -06:00
|
|
|
"listen": m{
|
2024-07-31 09:18:56 -06:00
|
|
|
"host": udpAddr.Addr().String(),
|
|
|
|
"port": udpAddr.Port(),
|
2021-03-29 13:29:20 -06:00
|
|
|
},
|
|
|
|
"logging": m{
|
|
|
|
"timestamp_format": fmt.Sprintf("%v 15:04:05.000000", name),
|
2021-03-31 09:26:35 -06:00
|
|
|
"level": l.Level.String(),
|
2021-03-29 13:29:20 -06:00
|
|
|
},
|
2023-03-30 10:09:20 -06:00
|
|
|
"timers": m{
|
2023-05-04 14:16:37 -06:00
|
|
|
"pending_deletion_interval": 2,
|
|
|
|
"connection_alive_interval": 2,
|
2023-03-30 10:09:20 -06:00
|
|
|
},
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
2022-06-27 11:33:29 -06:00
|
|
|
|
|
|
|
if overrides != nil {
|
|
|
|
err = mergo.Merge(&overrides, mc, mergo.WithAppendSlice)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
mc = overrides
|
|
|
|
}
|
|
|
|
|
2021-03-29 13:29:20 -06:00
|
|
|
cb, err := yaml.Marshal(mc)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
c := config.NewC(l)
|
|
|
|
c.LoadString(string(cb))
|
2021-03-29 13:29:20 -06:00
|
|
|
|
2021-11-03 19:54:04 -06:00
|
|
|
control, err := nebula.Main(c, false, "e2e-test", l, nil)
|
2021-03-29 13:29:20 -06:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
return control, vpnIpNet, udpAddr, c
|
2021-03-29 13:29:20 -06:00
|
|
|
}
|
|
|
|
|
2021-03-31 09:26:35 -06:00
|
|
|
type doneCb func()
|
|
|
|
|
|
|
|
func deadline(t *testing.T, seconds time.Duration) doneCb {
|
|
|
|
timeout := time.After(seconds * time.Second)
|
|
|
|
done := make(chan bool)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case <-timeout:
|
|
|
|
t.Fatal("Test did not finish in time")
|
|
|
|
case <-done:
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return func() {
|
|
|
|
done <- true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
func assertTunnel(t *testing.T, vpnIpA, vpnIpB netip.Addr, controlA, controlB *nebula.Control, r *router.R) {
|
2021-03-31 09:26:35 -06:00
|
|
|
// Send a packet from them to me
|
|
|
|
controlB.InjectTunUDPPacket(vpnIpA, 80, 90, []byte("Hi from B"))
|
2023-03-13 11:35:14 -06:00
|
|
|
bPacket := r.RouteForAllUntilTxTun(controlA)
|
2021-03-31 09:26:35 -06:00
|
|
|
assertUdpPacket(t, []byte("Hi from B"), bPacket, vpnIpB, vpnIpA, 90, 80)
|
|
|
|
|
|
|
|
// And once more from me to them
|
|
|
|
controlA.InjectTunUDPPacket(vpnIpB, 80, 90, []byte("Hello from A"))
|
2023-03-13 11:35:14 -06:00
|
|
|
aPacket := r.RouteForAllUntilTxTun(controlB)
|
2021-03-31 09:26:35 -06:00
|
|
|
assertUdpPacket(t, []byte("Hello from A"), aPacket, vpnIpA, vpnIpB, 90, 80)
|
|
|
|
}
|
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
func assertHostInfoPair(t *testing.T, addrA, addrB netip.AddrPort, vpnIpA, vpnIpB netip.Addr, controlA, controlB *nebula.Control) {
|
2021-03-29 13:29:20 -06:00
|
|
|
// Get both host infos
|
2024-07-31 09:18:56 -06:00
|
|
|
hBinA := controlA.GetHostInfoByVpnIp(vpnIpB, false)
|
2021-11-03 19:54:04 -06:00
|
|
|
assert.NotNil(t, hBinA, "Host B was not found by vpnIp in controlA")
|
2021-03-29 13:29:20 -06:00
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
hAinB := controlB.GetHostInfoByVpnIp(vpnIpA, false)
|
2021-11-03 19:54:04 -06:00
|
|
|
assert.NotNil(t, hAinB, "Host A was not found by vpnIp in controlB")
|
2021-03-29 13:29:20 -06:00
|
|
|
|
|
|
|
// Check that both vpn and real addr are correct
|
2021-11-03 19:54:04 -06:00
|
|
|
assert.Equal(t, vpnIpB, hBinA.VpnIp, "Host B VpnIp is wrong in control A")
|
|
|
|
assert.Equal(t, vpnIpA, hAinB.VpnIp, "Host A VpnIp is wrong in control B")
|
2021-03-29 13:29:20 -06:00
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
assert.Equal(t, addrB, hBinA.CurrentRemote, "Host B remote is wrong in control A")
|
|
|
|
assert.Equal(t, addrA, hAinB.CurrentRemote, "Host A remote is wrong in control B")
|
2021-03-29 13:29:20 -06:00
|
|
|
|
|
|
|
// Check that our indexes match
|
|
|
|
assert.Equal(t, hBinA.LocalIndex, hAinB.RemoteIndex, "Host B local index does not match host A remote index")
|
|
|
|
assert.Equal(t, hBinA.RemoteIndex, hAinB.LocalIndex, "Host B remote index does not match host A local index")
|
|
|
|
|
|
|
|
//TODO: Would be nice to assert this memory
|
|
|
|
//checkIndexes := func(name string, hm *HostMap, hi *HostInfo) {
|
|
|
|
// hBbyIndex := hmA.Indexes[hBinA.localIndexId]
|
|
|
|
// assert.NotNil(t, hBbyIndex, "Could not host info by local index in %s", name)
|
|
|
|
// assert.Equal(t, &hBbyIndex, &hBinA, "%s Indexes map did not point to the right host info", name)
|
|
|
|
//
|
|
|
|
// //TODO: remote indexes are susceptible to collision
|
|
|
|
// hBbyRemoteIndex := hmA.RemoteIndexes[hBinA.remoteIndexId]
|
|
|
|
// assert.NotNil(t, hBbyIndex, "Could not host info by remote index in %s", name)
|
|
|
|
// assert.Equal(t, &hBbyRemoteIndex, &hBinA, "%s RemoteIndexes did not point to the right host info", name)
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//// Check hostmap indexes too
|
|
|
|
//checkIndexes("hmA", hmA, hBinA)
|
|
|
|
//checkIndexes("hmB", hmB, hAinB)
|
|
|
|
}
|
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
func assertUdpPacket(t *testing.T, expected, b []byte, fromIp, toIp netip.Addr, fromPort, toPort uint16) {
|
2021-03-29 13:29:20 -06:00
|
|
|
packet := gopacket.NewPacket(b, layers.LayerTypeIPv4, gopacket.Lazy)
|
|
|
|
v4 := packet.Layer(layers.LayerTypeIPv4).(*layers.IPv4)
|
|
|
|
assert.NotNil(t, v4, "No ipv4 data found")
|
|
|
|
|
2024-07-31 09:18:56 -06:00
|
|
|
assert.Equal(t, fromIp.AsSlice(), []byte(v4.SrcIP), "Source ip was incorrect")
|
|
|
|
assert.Equal(t, toIp.AsSlice(), []byte(v4.DstIP), "Dest ip was incorrect")
|
2021-03-29 13:29:20 -06:00
|
|
|
|
|
|
|
udp := packet.Layer(layers.LayerTypeUDP).(*layers.UDP)
|
|
|
|
assert.NotNil(t, udp, "No udp data found")
|
|
|
|
|
|
|
|
assert.Equal(t, fromPort, uint16(udp.SrcPort), "Source port was incorrect")
|
|
|
|
assert.Equal(t, toPort, uint16(udp.DstPort), "Dest port was incorrect")
|
|
|
|
|
|
|
|
data := packet.ApplicationLayer()
|
|
|
|
assert.NotNil(t, data)
|
|
|
|
assert.Equal(t, expected, data.Payload(), "Data was incorrect")
|
|
|
|
}
|
2021-03-31 09:26:35 -06:00
|
|
|
|
|
|
|
func NewTestLogger() *logrus.Logger {
|
|
|
|
l := logrus.New()
|
|
|
|
|
|
|
|
v := os.Getenv("TEST_LOGS")
|
|
|
|
if v == "" {
|
2022-09-01 08:44:58 -06:00
|
|
|
l.SetOutput(io.Discard)
|
|
|
|
l.SetLevel(logrus.PanicLevel)
|
2021-03-31 09:26:35 -06:00
|
|
|
return l
|
|
|
|
}
|
|
|
|
|
|
|
|
switch v {
|
|
|
|
case "2":
|
|
|
|
l.SetLevel(logrus.DebugLevel)
|
|
|
|
case "3":
|
|
|
|
l.SetLevel(logrus.TraceLevel)
|
|
|
|
default:
|
|
|
|
l.SetLevel(logrus.InfoLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
return l
|
|
|
|
}
|