|
@@ -4,6 +4,7 @@
|
|
|
package e2e
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
"net"
|
|
|
"testing"
|
|
|
"time"
|
|
@@ -15,12 +16,13 @@ import (
|
|
|
"github.com/slackhq/nebula/iputil"
|
|
|
"github.com/slackhq/nebula/udp"
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
+ "gopkg.in/yaml.v2"
|
|
|
)
|
|
|
|
|
|
func BenchmarkHotPath(b *testing.B) {
|
|
|
ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
|
|
|
- myControl, _, _ := newSimpleServer(ca, caKey, "me", net.IP{10, 0, 0, 1}, nil)
|
|
|
- theirControl, theirVpnIpNet, theirUdpAddr := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2}, nil)
|
|
|
+ myControl, _, _, _ := newSimpleServer(ca, caKey, "me", net.IP{10, 0, 0, 1}, nil)
|
|
|
+ theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2}, nil)
|
|
|
|
|
|
// Put their info in our lighthouse
|
|
|
myControl.InjectLightHouseAddr(theirVpnIpNet.IP, theirUdpAddr)
|
|
@@ -43,8 +45,8 @@ func BenchmarkHotPath(b *testing.B) {
|
|
|
|
|
|
func TestGoodHandshake(t *testing.T) {
|
|
|
ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
|
|
|
- myControl, myVpnIpNet, myUdpAddr := newSimpleServer(ca, caKey, "me", net.IP{10, 0, 0, 1}, nil)
|
|
|
- theirControl, theirVpnIpNet, theirUdpAddr := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2}, nil)
|
|
|
+ myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(ca, caKey, "me", net.IP{10, 0, 0, 1}, nil)
|
|
|
+ theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2}, nil)
|
|
|
|
|
|
// Put their info in our lighthouse
|
|
|
myControl.InjectLightHouseAddr(theirVpnIpNet.IP, theirUdpAddr)
|
|
@@ -98,9 +100,9 @@ func TestWrongResponderHandshake(t *testing.T) {
|
|
|
// The IPs here are chosen on purpose:
|
|
|
// The current remote handling will sort by preference, public, and then lexically.
|
|
|
// So we need them to have a higher address than evil (we could apply a preference though)
|
|
|
- myControl, myVpnIpNet, myUdpAddr := newSimpleServer(ca, caKey, "me", net.IP{10, 0, 0, 100}, nil)
|
|
|
- theirControl, theirVpnIpNet, theirUdpAddr := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 99}, nil)
|
|
|
- evilControl, evilVpnIp, evilUdpAddr := newSimpleServer(ca, caKey, "evil", net.IP{10, 0, 0, 2}, nil)
|
|
|
+ myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(ca, caKey, "me", net.IP{10, 0, 0, 100}, nil)
|
|
|
+ theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 99}, nil)
|
|
|
+ evilControl, evilVpnIp, evilUdpAddr, _ := newSimpleServer(ca, caKey, "evil", net.IP{10, 0, 0, 2}, nil)
|
|
|
|
|
|
// Add their real udp addr, which should be tried after evil.
|
|
|
myControl.InjectLightHouseAddr(theirVpnIpNet.IP, theirUdpAddr)
|
|
@@ -163,8 +165,8 @@ func TestStage1Race(t *testing.T) {
|
|
|
// But will eventually collapse down to a single tunnel
|
|
|
|
|
|
ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
|
|
|
- myControl, myVpnIpNet, myUdpAddr := newSimpleServer(ca, caKey, "me ", net.IP{10, 0, 0, 1}, nil)
|
|
|
- theirControl, theirVpnIpNet, theirUdpAddr := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2}, nil)
|
|
|
+ myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(ca, caKey, "me ", net.IP{10, 0, 0, 1}, nil)
|
|
|
+ theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2}, nil)
|
|
|
|
|
|
// Put their info in our lighthouse and vice versa
|
|
|
myControl.InjectLightHouseAddr(theirVpnIpNet.IP, theirUdpAddr)
|
|
@@ -240,8 +242,8 @@ func TestStage1Race(t *testing.T) {
|
|
|
|
|
|
func TestUncleanShutdownRaceLoser(t *testing.T) {
|
|
|
ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
|
|
|
- myControl, myVpnIpNet, myUdpAddr := newSimpleServer(ca, caKey, "me ", net.IP{10, 0, 0, 1}, nil)
|
|
|
- theirControl, theirVpnIpNet, theirUdpAddr := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2}, nil)
|
|
|
+ myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(ca, caKey, "me ", net.IP{10, 0, 0, 1}, nil)
|
|
|
+ theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2}, nil)
|
|
|
|
|
|
// Teach my how to get to the relay and that their can be reached via the relay
|
|
|
myControl.InjectLightHouseAddr(theirVpnIpNet.IP, theirUdpAddr)
|
|
@@ -289,8 +291,8 @@ func TestUncleanShutdownRaceLoser(t *testing.T) {
|
|
|
|
|
|
func TestUncleanShutdownRaceWinner(t *testing.T) {
|
|
|
ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
|
|
|
- myControl, myVpnIpNet, myUdpAddr := newSimpleServer(ca, caKey, "me ", net.IP{10, 0, 0, 1}, nil)
|
|
|
- theirControl, theirVpnIpNet, theirUdpAddr := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2}, nil)
|
|
|
+ myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(ca, caKey, "me ", net.IP{10, 0, 0, 1}, nil)
|
|
|
+ theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2}, nil)
|
|
|
|
|
|
// Teach my how to get to the relay and that their can be reached via the relay
|
|
|
myControl.InjectLightHouseAddr(theirVpnIpNet.IP, theirUdpAddr)
|
|
@@ -340,9 +342,9 @@ func TestUncleanShutdownRaceWinner(t *testing.T) {
|
|
|
|
|
|
func TestRelays(t *testing.T) {
|
|
|
ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
|
|
|
- myControl, myVpnIpNet, _ := newSimpleServer(ca, caKey, "me ", net.IP{10, 0, 0, 1}, m{"relay": m{"use_relays": true}})
|
|
|
- relayControl, relayVpnIpNet, relayUdpAddr := newSimpleServer(ca, caKey, "relay ", net.IP{10, 0, 0, 128}, m{"relay": m{"am_relay": true}})
|
|
|
- theirControl, theirVpnIpNet, theirUdpAddr := newSimpleServer(ca, caKey, "them ", net.IP{10, 0, 0, 2}, m{"relay": m{"use_relays": true}})
|
|
|
+ myControl, myVpnIpNet, _, _ := newSimpleServer(ca, caKey, "me ", net.IP{10, 0, 0, 1}, m{"relay": m{"use_relays": true}})
|
|
|
+ relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(ca, caKey, "relay ", net.IP{10, 0, 0, 128}, m{"relay": m{"am_relay": true}})
|
|
|
+ theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them ", net.IP{10, 0, 0, 2}, m{"relay": m{"use_relays": true}})
|
|
|
|
|
|
// Teach my how to get to the relay and that their can be reached via the relay
|
|
|
myControl.InjectLightHouseAddr(relayVpnIpNet.IP, relayUdpAddr)
|
|
@@ -371,9 +373,9 @@ func TestRelays(t *testing.T) {
|
|
|
func TestStage1RaceRelays(t *testing.T) {
|
|
|
//NOTE: this is a race between me and relay resulting in a full tunnel from me to them via relay
|
|
|
ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
|
|
|
- myControl, myVpnIpNet, myUdpAddr := newSimpleServer(ca, caKey, "me ", net.IP{10, 0, 0, 1}, m{"relay": m{"use_relays": true}})
|
|
|
- relayControl, relayVpnIpNet, relayUdpAddr := newSimpleServer(ca, caKey, "relay ", net.IP{10, 0, 0, 128}, m{"relay": m{"am_relay": true}})
|
|
|
- theirControl, theirVpnIpNet, theirUdpAddr := newSimpleServer(ca, caKey, "them ", net.IP{10, 0, 0, 2}, m{"relay": m{"use_relays": true}})
|
|
|
+ myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(ca, caKey, "me ", net.IP{10, 0, 0, 1}, m{"relay": m{"use_relays": true}})
|
|
|
+ relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(ca, caKey, "relay ", net.IP{10, 0, 0, 128}, m{"relay": m{"am_relay": true}})
|
|
|
+ theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them ", net.IP{10, 0, 0, 2}, m{"relay": m{"use_relays": true}})
|
|
|
|
|
|
// Teach my how to get to the relay and that their can be reached via the relay
|
|
|
myControl.InjectLightHouseAddr(relayVpnIpNet.IP, relayUdpAddr)
|
|
@@ -418,9 +420,9 @@ func TestStage1RaceRelays(t *testing.T) {
|
|
|
func TestStage1RaceRelays2(t *testing.T) {
|
|
|
//NOTE: this is a race between me and relay resulting in a full tunnel from me to them via relay
|
|
|
ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
|
|
|
- myControl, myVpnIpNet, myUdpAddr := newSimpleServer(ca, caKey, "me ", net.IP{10, 0, 0, 1}, m{"relay": m{"use_relays": true}})
|
|
|
- relayControl, relayVpnIpNet, relayUdpAddr := newSimpleServer(ca, caKey, "relay ", net.IP{10, 0, 0, 128}, m{"relay": m{"am_relay": true}})
|
|
|
- theirControl, theirVpnIpNet, theirUdpAddr := newSimpleServer(ca, caKey, "them ", net.IP{10, 0, 0, 2}, m{"relay": m{"use_relays": true}})
|
|
|
+ myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(ca, caKey, "me ", net.IP{10, 0, 0, 1}, m{"relay": m{"use_relays": true}})
|
|
|
+ relayControl, relayVpnIpNet, relayUdpAddr, _ := newSimpleServer(ca, caKey, "relay ", net.IP{10, 0, 0, 128}, m{"relay": m{"am_relay": true}})
|
|
|
+ theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them ", net.IP{10, 0, 0, 2}, m{"relay": m{"use_relays": true}})
|
|
|
l := NewTestLogger()
|
|
|
|
|
|
// Teach my how to get to the relay and that their can be reached via the relay
|
|
@@ -503,5 +505,366 @@ func TestStage1RaceRelays2(t *testing.T) {
|
|
|
//
|
|
|
////TODO: assert hostmaps
|
|
|
}
|
|
|
+func TestRehandshakingRelays(t *testing.T) {
|
|
|
+ ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
|
|
|
+ myControl, myVpnIpNet, _, _ := newSimpleServer(ca, caKey, "me ", net.IP{10, 0, 0, 1}, m{"relay": m{"use_relays": true}})
|
|
|
+ relayControl, relayVpnIpNet, relayUdpAddr, relayConfig := newSimpleServer(ca, caKey, "relay ", net.IP{10, 0, 0, 128}, m{"relay": m{"am_relay": true}})
|
|
|
+ theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them ", net.IP{10, 0, 0, 2}, m{"relay": m{"use_relays": true}})
|
|
|
+
|
|
|
+ // Teach my how to get to the relay and that their can be reached via the relay
|
|
|
+ myControl.InjectLightHouseAddr(relayVpnIpNet.IP, relayUdpAddr)
|
|
|
+ myControl.InjectRelays(theirVpnIpNet.IP, []net.IP{relayVpnIpNet.IP})
|
|
|
+ relayControl.InjectLightHouseAddr(theirVpnIpNet.IP, theirUdpAddr)
|
|
|
+
|
|
|
+ // Build a router so we don't have to reason who gets which packet
|
|
|
+ r := router.NewR(t, myControl, relayControl, theirControl)
|
|
|
+ defer r.RenderFlow()
|
|
|
+
|
|
|
+ // Start the servers
|
|
|
+ myControl.Start()
|
|
|
+ relayControl.Start()
|
|
|
+ theirControl.Start()
|
|
|
+
|
|
|
+ t.Log("Trigger a handshake from me to them via the relay")
|
|
|
+ myControl.InjectTunUDPPacket(theirVpnIpNet.IP, 80, 80, []byte("Hi from me"))
|
|
|
+
|
|
|
+ p := r.RouteForAllUntilTxTun(theirControl)
|
|
|
+ r.Log("Assert the tunnel works")
|
|
|
+ assertUdpPacket(t, []byte("Hi from me"), p, myVpnIpNet.IP, theirVpnIpNet.IP, 80, 80)
|
|
|
+ r.RenderHostmaps("working hostmaps", myControl, relayControl, theirControl)
|
|
|
+
|
|
|
+ // When I update the certificate for the relay, both me and them will have 2 host infos for the relay,
|
|
|
+ // and the main host infos will not have any relay state to handle the me<->relay<->them tunnel.
|
|
|
+ r.Log("Renew relay certificate and spin until me and them sees it")
|
|
|
+ _, _, myNextPrivKey, myNextPEM := newTestCert(ca, caKey, "relay", time.Now(), time.Now().Add(5*time.Minute), relayVpnIpNet, nil, []string{"new group"})
|
|
|
+
|
|
|
+ caB, err := ca.MarshalToPEM()
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ relayConfig.Settings["pki"] = m{
|
|
|
+ "ca": string(caB),
|
|
|
+ "cert": string(myNextPEM),
|
|
|
+ "key": string(myNextPrivKey),
|
|
|
+ }
|
|
|
+ rc, err := yaml.Marshal(relayConfig.Settings)
|
|
|
+ assert.NoError(t, err)
|
|
|
+ relayConfig.ReloadConfigString(string(rc))
|
|
|
+
|
|
|
+ for {
|
|
|
+ r.Log("Assert the tunnel works between myVpnIpNet and relayVpnIpNet")
|
|
|
+ assertTunnel(t, myVpnIpNet.IP, relayVpnIpNet.IP, myControl, relayControl, r)
|
|
|
+ c := myControl.GetHostInfoByVpnIp(iputil.Ip2VpnIp(relayVpnIpNet.IP), false)
|
|
|
+ if len(c.Cert.Details.Groups) != 0 {
|
|
|
+ // We have a new certificate now
|
|
|
+ r.Log("Certificate between my and relay is updated!")
|
|
|
+ break
|
|
|
+ }
|
|
|
+
|
|
|
+ time.Sleep(time.Second)
|
|
|
+ }
|
|
|
+
|
|
|
+ for {
|
|
|
+ r.Log("Assert the tunnel works between theirVpnIpNet and relayVpnIpNet")
|
|
|
+ assertTunnel(t, theirVpnIpNet.IP, relayVpnIpNet.IP, theirControl, relayControl, r)
|
|
|
+ c := theirControl.GetHostInfoByVpnIp(iputil.Ip2VpnIp(relayVpnIpNet.IP), false)
|
|
|
+ if len(c.Cert.Details.Groups) != 0 {
|
|
|
+ // We have a new certificate now
|
|
|
+ r.Log("Certificate between their and relay is updated!")
|
|
|
+ break
|
|
|
+ }
|
|
|
+
|
|
|
+ time.Sleep(time.Second)
|
|
|
+ }
|
|
|
+
|
|
|
+ r.Log("Assert the relay tunnel still works")
|
|
|
+ assertTunnel(t, theirVpnIpNet.IP, myVpnIpNet.IP, theirControl, myControl, r)
|
|
|
+ r.RenderHostmaps("working hostmaps", myControl, relayControl, theirControl)
|
|
|
+ // We should have two hostinfos on all sides
|
|
|
+ for len(myControl.GetHostmap().Indexes) != 2 {
|
|
|
+ t.Logf("Waiting for myControl hostinfos (%v != 2) to get cleaned up from lack of use...", len(myControl.GetHostmap().Indexes))
|
|
|
+ r.Log("Assert the relay tunnel still works")
|
|
|
+ assertTunnel(t, theirVpnIpNet.IP, myVpnIpNet.IP, theirControl, myControl, r)
|
|
|
+ r.Log("yupitdoes")
|
|
|
+ time.Sleep(time.Second)
|
|
|
+ }
|
|
|
+ t.Logf("myControl hostinfos got cleaned up!")
|
|
|
+ for len(theirControl.GetHostmap().Indexes) != 2 {
|
|
|
+ t.Logf("Waiting for theirControl hostinfos (%v != 2) to get cleaned up from lack of use...", len(theirControl.GetHostmap().Indexes))
|
|
|
+ r.Log("Assert the relay tunnel still works")
|
|
|
+ assertTunnel(t, theirVpnIpNet.IP, myVpnIpNet.IP, theirControl, myControl, r)
|
|
|
+ r.Log("yupitdoes")
|
|
|
+ time.Sleep(time.Second)
|
|
|
+ }
|
|
|
+ t.Logf("theirControl hostinfos got cleaned up!")
|
|
|
+ for len(relayControl.GetHostmap().Indexes) != 2 {
|
|
|
+ t.Logf("Waiting for relayControl hostinfos (%v != 2) to get cleaned up from lack of use...", len(relayControl.GetHostmap().Indexes))
|
|
|
+ r.Log("Assert the relay tunnel still works")
|
|
|
+ assertTunnel(t, theirVpnIpNet.IP, myVpnIpNet.IP, theirControl, myControl, r)
|
|
|
+ r.Log("yupitdoes")
|
|
|
+ time.Sleep(time.Second)
|
|
|
+ }
|
|
|
+ t.Logf("relayControl hostinfos got cleaned up!")
|
|
|
+}
|
|
|
+
|
|
|
+func TestRehandshaking(t *testing.T) {
|
|
|
+ ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
|
|
|
+ myControl, myVpnIpNet, myUdpAddr, myConfig := newSimpleServer(ca, caKey, "me ", net.IP{10, 0, 0, 2}, nil)
|
|
|
+ theirControl, theirVpnIpNet, theirUdpAddr, theirConfig := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 1}, nil)
|
|
|
+
|
|
|
+ // Put their info in our lighthouse and vice versa
|
|
|
+ myControl.InjectLightHouseAddr(theirVpnIpNet.IP, theirUdpAddr)
|
|
|
+ theirControl.InjectLightHouseAddr(myVpnIpNet.IP, myUdpAddr)
|
|
|
+
|
|
|
+ // Build a router so we don't have to reason who gets which packet
|
|
|
+ r := router.NewR(t, myControl, theirControl)
|
|
|
+ defer r.RenderFlow()
|
|
|
+
|
|
|
+ // Start the servers
|
|
|
+ myControl.Start()
|
|
|
+ theirControl.Start()
|
|
|
+
|
|
|
+ t.Log("Stand up a tunnel between me and them")
|
|
|
+ assertTunnel(t, myVpnIpNet.IP, theirVpnIpNet.IP, myControl, theirControl, r)
|
|
|
+
|
|
|
+ r.RenderHostmaps("Starting hostmaps", myControl, theirControl)
|
|
|
+
|
|
|
+ r.Log("Renew my certificate and spin until their sees it")
|
|
|
+ _, _, myNextPrivKey, myNextPEM := newTestCert(ca, caKey, "me", time.Now(), time.Now().Add(5*time.Minute), myVpnIpNet, nil, []string{"new group"})
|
|
|
+
|
|
|
+ caB, err := ca.MarshalToPEM()
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ myConfig.Settings["pki"] = m{
|
|
|
+ "ca": string(caB),
|
|
|
+ "cert": string(myNextPEM),
|
|
|
+ "key": string(myNextPrivKey),
|
|
|
+ }
|
|
|
+ rc, err := yaml.Marshal(myConfig.Settings)
|
|
|
+ assert.NoError(t, err)
|
|
|
+ myConfig.ReloadConfigString(string(rc))
|
|
|
+
|
|
|
+ for {
|
|
|
+ assertTunnel(t, myVpnIpNet.IP, theirVpnIpNet.IP, myControl, theirControl, r)
|
|
|
+ c := theirControl.GetHostInfoByVpnIp(iputil.Ip2VpnIp(myVpnIpNet.IP), false)
|
|
|
+ if len(c.Cert.Details.Groups) != 0 {
|
|
|
+ // We have a new certificate now
|
|
|
+ break
|
|
|
+ }
|
|
|
+
|
|
|
+ time.Sleep(time.Second)
|
|
|
+ }
|
|
|
+
|
|
|
+ // Flip their firewall to only allowing the new group to catch the tunnels reverting incorrectly
|
|
|
+ rc, err = yaml.Marshal(theirConfig.Settings)
|
|
|
+ assert.NoError(t, err)
|
|
|
+ var theirNewConfig m
|
|
|
+ assert.NoError(t, yaml.Unmarshal(rc, &theirNewConfig))
|
|
|
+ theirFirewall := theirNewConfig["firewall"].(map[interface{}]interface{})
|
|
|
+ theirFirewall["inbound"] = []m{{
|
|
|
+ "proto": "any",
|
|
|
+ "port": "any",
|
|
|
+ "group": "new group",
|
|
|
+ }}
|
|
|
+ rc, err = yaml.Marshal(theirNewConfig)
|
|
|
+ assert.NoError(t, err)
|
|
|
+ theirConfig.ReloadConfigString(string(rc))
|
|
|
+
|
|
|
+ r.Log("Spin until there is only 1 tunnel")
|
|
|
+ for len(myControl.GetHostmap().Indexes)+len(theirControl.GetHostmap().Indexes) > 2 {
|
|
|
+ assertTunnel(t, myVpnIpNet.IP, theirVpnIpNet.IP, myControl, theirControl, r)
|
|
|
+ t.Log("Connection manager hasn't ticked yet")
|
|
|
+ time.Sleep(time.Second)
|
|
|
+ }
|
|
|
+
|
|
|
+ assertTunnel(t, myVpnIpNet.IP, theirVpnIpNet.IP, myControl, theirControl, r)
|
|
|
+ myFinalHostmapHosts := myControl.ListHostmapHosts(false)
|
|
|
+ myFinalHostmapIndexes := myControl.ListHostmapIndexes(false)
|
|
|
+ theirFinalHostmapHosts := theirControl.ListHostmapHosts(false)
|
|
|
+ theirFinalHostmapIndexes := theirControl.ListHostmapIndexes(false)
|
|
|
+
|
|
|
+ // Make sure the correct tunnel won
|
|
|
+ c := theirControl.GetHostInfoByVpnIp(iputil.Ip2VpnIp(myVpnIpNet.IP), false)
|
|
|
+ assert.Contains(t, c.Cert.Details.Groups, "new group")
|
|
|
+
|
|
|
+ // We should only have a single tunnel now on both sides
|
|
|
+ assert.Len(t, myFinalHostmapHosts, 1)
|
|
|
+ assert.Len(t, theirFinalHostmapHosts, 1)
|
|
|
+ assert.Len(t, myFinalHostmapIndexes, 1)
|
|
|
+ assert.Len(t, theirFinalHostmapIndexes, 1)
|
|
|
+
|
|
|
+ r.RenderHostmaps("Final hostmaps", myControl, theirControl)
|
|
|
+
|
|
|
+ myControl.Stop()
|
|
|
+ theirControl.Stop()
|
|
|
+}
|
|
|
+
|
|
|
+func TestRehandshakingLoser(t *testing.T) {
|
|
|
+ // The purpose of this test is that the race loser renews their certificate and rehandshakes. The final tunnel
|
|
|
+ // Should be the one with the new certificate
|
|
|
+ ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
|
|
|
+ myControl, myVpnIpNet, myUdpAddr, myConfig := newSimpleServer(ca, caKey, "me ", net.IP{10, 0, 0, 2}, nil)
|
|
|
+ theirControl, theirVpnIpNet, theirUdpAddr, theirConfig := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 1}, nil)
|
|
|
+
|
|
|
+ // Put their info in our lighthouse and vice versa
|
|
|
+ myControl.InjectLightHouseAddr(theirVpnIpNet.IP, theirUdpAddr)
|
|
|
+ theirControl.InjectLightHouseAddr(myVpnIpNet.IP, myUdpAddr)
|
|
|
+
|
|
|
+ // Build a router so we don't have to reason who gets which packet
|
|
|
+ r := router.NewR(t, myControl, theirControl)
|
|
|
+ defer r.RenderFlow()
|
|
|
+
|
|
|
+ // Start the servers
|
|
|
+ myControl.Start()
|
|
|
+ theirControl.Start()
|
|
|
+
|
|
|
+ t.Log("Stand up a tunnel between me and them")
|
|
|
+ assertTunnel(t, myVpnIpNet.IP, theirVpnIpNet.IP, myControl, theirControl, r)
|
|
|
+
|
|
|
+ tt1 := myControl.GetHostInfoByVpnIp(iputil.Ip2VpnIp(theirVpnIpNet.IP), false)
|
|
|
+ tt2 := theirControl.GetHostInfoByVpnIp(iputil.Ip2VpnIp(myVpnIpNet.IP), false)
|
|
|
+ fmt.Println(tt1.LocalIndex, tt2.LocalIndex)
|
|
|
+
|
|
|
+ r.RenderHostmaps("Starting hostmaps", myControl, theirControl)
|
|
|
+
|
|
|
+ r.Log("Renew their certificate and spin until mine sees it")
|
|
|
+ _, _, theirNextPrivKey, theirNextPEM := newTestCert(ca, caKey, "them", time.Now(), time.Now().Add(5*time.Minute), theirVpnIpNet, nil, []string{"their new group"})
|
|
|
+
|
|
|
+ caB, err := ca.MarshalToPEM()
|
|
|
+ if err != nil {
|
|
|
+ panic(err)
|
|
|
+ }
|
|
|
+
|
|
|
+ theirConfig.Settings["pki"] = m{
|
|
|
+ "ca": string(caB),
|
|
|
+ "cert": string(theirNextPEM),
|
|
|
+ "key": string(theirNextPrivKey),
|
|
|
+ }
|
|
|
+ rc, err := yaml.Marshal(theirConfig.Settings)
|
|
|
+ assert.NoError(t, err)
|
|
|
+ theirConfig.ReloadConfigString(string(rc))
|
|
|
+
|
|
|
+ for {
|
|
|
+ assertTunnel(t, myVpnIpNet.IP, theirVpnIpNet.IP, myControl, theirControl, r)
|
|
|
+ theirCertInMe := myControl.GetHostInfoByVpnIp(iputil.Ip2VpnIp(theirVpnIpNet.IP), false)
|
|
|
+
|
|
|
+ _, theirNewGroup := theirCertInMe.Cert.Details.InvertedGroups["their new group"]
|
|
|
+ if theirNewGroup {
|
|
|
+ break
|
|
|
+ }
|
|
|
+
|
|
|
+ time.Sleep(time.Second)
|
|
|
+ }
|
|
|
+
|
|
|
+ // Flip my firewall to only allowing the new group to catch the tunnels reverting incorrectly
|
|
|
+ rc, err = yaml.Marshal(myConfig.Settings)
|
|
|
+ assert.NoError(t, err)
|
|
|
+ var myNewConfig m
|
|
|
+ assert.NoError(t, yaml.Unmarshal(rc, &myNewConfig))
|
|
|
+ theirFirewall := myNewConfig["firewall"].(map[interface{}]interface{})
|
|
|
+ theirFirewall["inbound"] = []m{{
|
|
|
+ "proto": "any",
|
|
|
+ "port": "any",
|
|
|
+ "group": "their new group",
|
|
|
+ }}
|
|
|
+ rc, err = yaml.Marshal(myNewConfig)
|
|
|
+ assert.NoError(t, err)
|
|
|
+ myConfig.ReloadConfigString(string(rc))
|
|
|
+
|
|
|
+ r.Log("Spin until there is only 1 tunnel")
|
|
|
+ for len(myControl.GetHostmap().Indexes)+len(theirControl.GetHostmap().Indexes) > 2 {
|
|
|
+ assertTunnel(t, myVpnIpNet.IP, theirVpnIpNet.IP, myControl, theirControl, r)
|
|
|
+ t.Log("Connection manager hasn't ticked yet")
|
|
|
+ time.Sleep(time.Second)
|
|
|
+ }
|
|
|
+
|
|
|
+ assertTunnel(t, myVpnIpNet.IP, theirVpnIpNet.IP, myControl, theirControl, r)
|
|
|
+ myFinalHostmapHosts := myControl.ListHostmapHosts(false)
|
|
|
+ myFinalHostmapIndexes := myControl.ListHostmapIndexes(false)
|
|
|
+ theirFinalHostmapHosts := theirControl.ListHostmapHosts(false)
|
|
|
+ theirFinalHostmapIndexes := theirControl.ListHostmapIndexes(false)
|
|
|
+
|
|
|
+ // Make sure the correct tunnel won
|
|
|
+ theirCertInMe := myControl.GetHostInfoByVpnIp(iputil.Ip2VpnIp(theirVpnIpNet.IP), false)
|
|
|
+ assert.Contains(t, theirCertInMe.Cert.Details.Groups, "their new group")
|
|
|
+
|
|
|
+ // We should only have a single tunnel now on both sides
|
|
|
+ assert.Len(t, myFinalHostmapHosts, 1)
|
|
|
+ assert.Len(t, theirFinalHostmapHosts, 1)
|
|
|
+ assert.Len(t, myFinalHostmapIndexes, 1)
|
|
|
+ assert.Len(t, theirFinalHostmapIndexes, 1)
|
|
|
+
|
|
|
+ r.RenderHostmaps("Final hostmaps", myControl, theirControl)
|
|
|
+ myControl.Stop()
|
|
|
+ theirControl.Stop()
|
|
|
+}
|
|
|
+
|
|
|
+func TestRaceRegression(t *testing.T) {
|
|
|
+ // This test forces stage 1, stage 2, stage 1 to be received by me from them
|
|
|
+ // We had a bug where we were not finding the duplicate handshake and responding to the final stage 1 which
|
|
|
+ // caused a cross-linked hostinfo
|
|
|
+ ca, _, caKey, _ := newTestCaCert(time.Now(), time.Now().Add(10*time.Minute), []*net.IPNet{}, []*net.IPNet{}, []string{})
|
|
|
+ myControl, myVpnIpNet, myUdpAddr, _ := newSimpleServer(ca, caKey, "me", net.IP{10, 0, 0, 1}, nil)
|
|
|
+ theirControl, theirVpnIpNet, theirUdpAddr, _ := newSimpleServer(ca, caKey, "them", net.IP{10, 0, 0, 2}, nil)
|
|
|
+
|
|
|
+ // Put their info in our lighthouse
|
|
|
+ myControl.InjectLightHouseAddr(theirVpnIpNet.IP, theirUdpAddr)
|
|
|
+ theirControl.InjectLightHouseAddr(myVpnIpNet.IP, myUdpAddr)
|
|
|
+
|
|
|
+ // Start the servers
|
|
|
+ myControl.Start()
|
|
|
+ theirControl.Start()
|
|
|
+
|
|
|
+ //them rx stage:1 initiatorIndex=642843150 responderIndex=0
|
|
|
+ //me rx stage:1 initiatorIndex=120607833 responderIndex=0
|
|
|
+ //them rx stage:1 initiatorIndex=642843150 responderIndex=0
|
|
|
+ //me rx stage:2 initiatorIndex=642843150 responderIndex=3701775874
|
|
|
+ //me rx stage:1 initiatorIndex=120607833 responderIndex=0
|
|
|
+ //them rx stage:2 initiatorIndex=120607833 responderIndex=4209862089
|
|
|
+
|
|
|
+ t.Log("Start both handshakes")
|
|
|
+ myControl.InjectTunUDPPacket(theirVpnIpNet.IP, 80, 80, []byte("Hi from me"))
|
|
|
+ theirControl.InjectTunUDPPacket(myVpnIpNet.IP, 80, 80, []byte("Hi from them"))
|
|
|
+
|
|
|
+ t.Log("Get both stage 1")
|
|
|
+ myStage1ForThem := myControl.GetFromUDP(true)
|
|
|
+ theirStage1ForMe := theirControl.GetFromUDP(true)
|
|
|
+
|
|
|
+ t.Log("Inject them in a special way")
|
|
|
+ theirControl.InjectUDPPacket(myStage1ForThem)
|
|
|
+ myControl.InjectUDPPacket(theirStage1ForMe)
|
|
|
+ theirControl.InjectUDPPacket(myStage1ForThem)
|
|
|
+
|
|
|
+ //TODO: ensure stage 2
|
|
|
+ t.Log("Get both stage 2")
|
|
|
+ myStage2ForThem := myControl.GetFromUDP(true)
|
|
|
+ theirStage2ForMe := theirControl.GetFromUDP(true)
|
|
|
+
|
|
|
+ t.Log("Inject them in a special way again")
|
|
|
+ myControl.InjectUDPPacket(theirStage2ForMe)
|
|
|
+ myControl.InjectUDPPacket(theirStage1ForMe)
|
|
|
+ theirControl.InjectUDPPacket(myStage2ForThem)
|
|
|
+
|
|
|
+ r := router.NewR(t, myControl, theirControl)
|
|
|
+ defer r.RenderFlow()
|
|
|
+
|
|
|
+ t.Log("Flush the packets")
|
|
|
+ r.RouteForAllUntilTxTun(myControl)
|
|
|
+ r.RouteForAllUntilTxTun(theirControl)
|
|
|
+ r.RenderHostmaps("Starting hostmaps", myControl, theirControl)
|
|
|
+
|
|
|
+ t.Log("Make sure the tunnel still works")
|
|
|
+ assertTunnel(t, myVpnIpNet.IP, theirVpnIpNet.IP, myControl, theirControl, r)
|
|
|
+
|
|
|
+ myControl.Stop()
|
|
|
+ theirControl.Stop()
|
|
|
+}
|
|
|
|
|
|
+//TODO: test
|
|
|
+// Race winner renews and handshakes
|
|
|
+// Race loser renews and handshakes
|
|
|
+// Does race winner repin the cert to old?
|
|
|
//TODO: add a test with many lies
|