2021-04-29 21:44:08 +00:00
|
|
|
// Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-06-10 22:12:25 +00:00
|
|
|
"context"
|
2021-05-03 17:49:45 +00:00
|
|
|
crand "crypto/rand"
|
|
|
|
"crypto/tls"
|
2021-04-29 21:44:08 +00:00
|
|
|
"encoding/json"
|
2021-05-12 21:43:43 +00:00
|
|
|
"errors"
|
2021-05-12 04:57:25 +00:00
|
|
|
"flag"
|
2021-04-29 21:44:08 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2021-05-03 17:49:45 +00:00
|
|
|
"net"
|
2021-04-29 21:44:08 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2021-05-12 04:57:25 +00:00
|
|
|
"regexp"
|
2021-04-29 21:44:08 +00:00
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2021-05-03 17:49:45 +00:00
|
|
|
"sync/atomic"
|
2021-04-29 21:44:08 +00:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go4.org/mem"
|
2021-05-03 17:49:45 +00:00
|
|
|
"tailscale.com/derp"
|
|
|
|
"tailscale.com/derp/derphttp"
|
2021-05-03 21:22:18 +00:00
|
|
|
"tailscale.com/ipn/ipnstate"
|
2021-05-03 17:49:45 +00:00
|
|
|
"tailscale.com/net/stun/stuntest"
|
|
|
|
"tailscale.com/safesocket"
|
2021-04-29 21:44:08 +00:00
|
|
|
"tailscale.com/smallzstd"
|
2021-05-03 17:49:45 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2021-04-29 21:44:08 +00:00
|
|
|
"tailscale.com/tstest"
|
2021-04-30 04:52:31 +00:00
|
|
|
"tailscale.com/tstest/integration/testcontrol"
|
2021-05-03 17:49:45 +00:00
|
|
|
"tailscale.com/types/key"
|
|
|
|
"tailscale.com/types/logger"
|
|
|
|
"tailscale.com/types/nettype"
|
2021-04-29 21:44:08 +00:00
|
|
|
)
|
|
|
|
|
2021-06-10 18:23:53 +00:00
|
|
|
var (
|
|
|
|
verboseLogCatcher = flag.Bool("verbose-log-catcher", false, "verbose log catcher logging")
|
2021-06-10 22:12:25 +00:00
|
|
|
verboseTailscaled = flag.Bool("verbose-tailscaled", false, "verbose tailscaled logging")
|
2021-06-10 18:23:53 +00:00
|
|
|
)
|
2021-05-12 04:57:25 +00:00
|
|
|
|
2021-05-03 17:49:45 +00:00
|
|
|
var mainError atomic.Value // of error
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2021-06-10 22:12:25 +00:00
|
|
|
flag.Parse()
|
2021-05-03 17:49:45 +00:00
|
|
|
v := m.Run()
|
|
|
|
if v != 0 {
|
|
|
|
os.Exit(v)
|
|
|
|
}
|
|
|
|
if err, ok := mainError.Load().(error); ok {
|
|
|
|
fmt.Fprintf(os.Stderr, "FAIL: %v\n", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2021-05-12 04:57:25 +00:00
|
|
|
func TestOneNodeUp_NoAuth(t *testing.T) {
|
|
|
|
t.Parallel()
|
2021-05-31 15:35:01 +00:00
|
|
|
bins := BuildTestBinaries(t)
|
2021-04-30 04:52:31 +00:00
|
|
|
|
2021-05-03 17:49:45 +00:00
|
|
|
env := newTestEnv(t, bins)
|
2021-05-01 03:27:05 +00:00
|
|
|
defer env.Close()
|
2021-04-29 22:58:29 +00:00
|
|
|
|
2021-05-01 03:27:05 +00:00
|
|
|
n1 := newTestNode(t, env)
|
|
|
|
|
2021-05-12 04:57:25 +00:00
|
|
|
d1 := n1.StartDaemon(t)
|
|
|
|
defer d1.Kill()
|
2021-04-29 21:44:08 +00:00
|
|
|
|
2021-05-03 17:49:45 +00:00
|
|
|
n1.AwaitListening(t)
|
|
|
|
|
2021-05-03 21:22:18 +00:00
|
|
|
st := n1.MustStatus(t)
|
|
|
|
t.Logf("Status: %s", st.BackendState)
|
2021-04-29 21:44:08 +00:00
|
|
|
|
|
|
|
if err := tstest.WaitFor(20*time.Second, func() error {
|
|
|
|
const sub = `Program starting: `
|
2021-05-01 03:27:05 +00:00
|
|
|
if !env.LogCatcher.logsContains(mem.S(sub)) {
|
|
|
|
return fmt.Errorf("log catcher didn't see %#q; got %s", sub, env.LogCatcher.logsString())
|
2021-04-29 21:44:08 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
2021-05-12 21:43:43 +00:00
|
|
|
n1.MustUp()
|
2021-04-30 04:52:31 +00:00
|
|
|
|
2021-05-03 17:49:45 +00:00
|
|
|
if d, _ := time.ParseDuration(os.Getenv("TS_POST_UP_SLEEP")); d > 0 {
|
|
|
|
t.Logf("Sleeping for %v to give 'up' time to misbehave (https://github.com/tailscale/tailscale/issues/1840) ...", d)
|
|
|
|
time.Sleep(d)
|
|
|
|
}
|
|
|
|
|
2021-05-12 04:57:25 +00:00
|
|
|
t.Logf("Got IP: %v", n1.AwaitIP(t))
|
|
|
|
n1.AwaitRunning(t)
|
|
|
|
|
|
|
|
d1.MustCleanShutdown(t)
|
|
|
|
|
|
|
|
t.Logf("number of HTTP logcatcher requests: %v", env.LogCatcher.numRequests())
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOneNodeUp_Auth(t *testing.T) {
|
|
|
|
t.Parallel()
|
2021-05-31 15:35:01 +00:00
|
|
|
bins := BuildTestBinaries(t)
|
2021-05-12 04:57:25 +00:00
|
|
|
|
|
|
|
env := newTestEnv(t, bins)
|
|
|
|
defer env.Close()
|
|
|
|
env.Control.RequireAuth = true
|
|
|
|
|
|
|
|
n1 := newTestNode(t, env)
|
|
|
|
d1 := n1.StartDaemon(t)
|
|
|
|
defer d1.Kill()
|
|
|
|
|
|
|
|
n1.AwaitListening(t)
|
|
|
|
|
|
|
|
st := n1.MustStatus(t)
|
|
|
|
t.Logf("Status: %s", st.BackendState)
|
|
|
|
|
|
|
|
t.Logf("Running up --login-server=%s ...", env.ControlServer.URL)
|
|
|
|
|
|
|
|
cmd := n1.Tailscale("up", "--login-server="+env.ControlServer.URL)
|
|
|
|
var authCountAtomic int32
|
|
|
|
cmd.Stdout = &authURLParserWriter{fn: func(urlStr string) error {
|
|
|
|
if env.Control.CompleteAuth(urlStr) {
|
|
|
|
atomic.AddInt32(&authCountAtomic, 1)
|
|
|
|
t.Logf("completed auth path %s", urlStr)
|
|
|
|
return nil
|
2021-04-30 04:52:31 +00:00
|
|
|
}
|
2021-05-12 04:57:25 +00:00
|
|
|
err := fmt.Errorf("Failed to complete auth path to %q", urlStr)
|
|
|
|
t.Log(err)
|
|
|
|
return err
|
|
|
|
}}
|
|
|
|
cmd.Stderr = cmd.Stdout
|
|
|
|
if err := cmd.Run(); err != nil {
|
|
|
|
t.Fatalf("up: %v", err)
|
2021-04-30 04:52:31 +00:00
|
|
|
}
|
2021-05-12 04:57:25 +00:00
|
|
|
t.Logf("Got IP: %v", n1.AwaitIP(t))
|
2021-04-30 04:52:31 +00:00
|
|
|
|
2021-05-12 04:57:25 +00:00
|
|
|
n1.AwaitRunning(t)
|
2021-04-29 21:44:08 +00:00
|
|
|
|
2021-05-12 04:57:25 +00:00
|
|
|
if n := atomic.LoadInt32(&authCountAtomic); n != 1 {
|
|
|
|
t.Errorf("Auth URLs completed = %d; want 1", n)
|
2021-04-29 21:44:08 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 04:57:25 +00:00
|
|
|
d1.MustCleanShutdown(t)
|
|
|
|
|
2021-05-01 03:27:05 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 21:43:43 +00:00
|
|
|
func TestTwoNodes(t *testing.T) {
|
|
|
|
t.Parallel()
|
2021-05-31 15:35:01 +00:00
|
|
|
bins := BuildTestBinaries(t)
|
2021-05-12 21:43:43 +00:00
|
|
|
|
|
|
|
env := newTestEnv(t, bins)
|
|
|
|
defer env.Close()
|
|
|
|
|
|
|
|
// Create two nodes:
|
|
|
|
n1 := newTestNode(t, env)
|
|
|
|
d1 := n1.StartDaemon(t)
|
|
|
|
defer d1.Kill()
|
|
|
|
|
|
|
|
n2 := newTestNode(t, env)
|
|
|
|
d2 := n2.StartDaemon(t)
|
|
|
|
defer d2.Kill()
|
|
|
|
|
|
|
|
n1.AwaitListening(t)
|
|
|
|
n2.AwaitListening(t)
|
|
|
|
n1.MustUp()
|
|
|
|
n2.MustUp()
|
|
|
|
n1.AwaitRunning(t)
|
|
|
|
n2.AwaitRunning(t)
|
|
|
|
|
|
|
|
if err := tstest.WaitFor(2*time.Second, func() error {
|
|
|
|
st := n1.MustStatus(t)
|
|
|
|
if len(st.Peer) == 0 {
|
|
|
|
return errors.New("no peers")
|
|
|
|
}
|
|
|
|
if len(st.Peer) > 1 {
|
|
|
|
return fmt.Errorf("got %d peers; want 1", len(st.Peer))
|
|
|
|
}
|
|
|
|
peer := st.Peer[st.Peers()[0]]
|
|
|
|
if peer.ID == st.Self.ID {
|
|
|
|
return errors.New("peer is self")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d1.MustCleanShutdown(t)
|
|
|
|
d2.MustCleanShutdown(t)
|
|
|
|
}
|
|
|
|
|
2021-05-18 20:20:29 +00:00
|
|
|
func TestNodeAddressIPFields(t *testing.T) {
|
|
|
|
t.Parallel()
|
2021-05-31 15:35:01 +00:00
|
|
|
bins := BuildTestBinaries(t)
|
2021-05-18 20:20:29 +00:00
|
|
|
|
|
|
|
env := newTestEnv(t, bins)
|
|
|
|
defer env.Close()
|
|
|
|
|
|
|
|
n1 := newTestNode(t, env)
|
|
|
|
d1 := n1.StartDaemon(t)
|
|
|
|
defer d1.Kill()
|
|
|
|
|
|
|
|
n1.AwaitListening(t)
|
|
|
|
n1.MustUp()
|
|
|
|
n1.AwaitRunning(t)
|
|
|
|
|
|
|
|
testNodes := env.Control.AllNodes()
|
|
|
|
|
|
|
|
if len(testNodes) != 1 {
|
|
|
|
t.Errorf("Expected %d nodes, got %d", 1, len(testNodes))
|
|
|
|
}
|
|
|
|
node := testNodes[0]
|
|
|
|
if len(node.Addresses) == 0 {
|
|
|
|
t.Errorf("Empty Addresses field in node")
|
|
|
|
}
|
|
|
|
if len(node.AllowedIPs) == 0 {
|
|
|
|
t.Errorf("Empty AllowedIPs field in node")
|
|
|
|
}
|
|
|
|
|
|
|
|
d1.MustCleanShutdown(t)
|
|
|
|
}
|
|
|
|
|
2021-06-02 15:23:24 +00:00
|
|
|
func TestAddPingRequest(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
bins := BuildTestBinaries(t)
|
|
|
|
|
|
|
|
env := newTestEnv(t, bins)
|
|
|
|
defer env.Close()
|
|
|
|
|
|
|
|
n1 := newTestNode(t, env)
|
|
|
|
d1 := n1.StartDaemon(t)
|
|
|
|
defer d1.Kill()
|
|
|
|
|
|
|
|
n1.AwaitListening(t)
|
|
|
|
n1.MustUp()
|
|
|
|
n1.AwaitRunning(t)
|
|
|
|
|
|
|
|
gotPing := make(chan bool, 1)
|
|
|
|
waitPing := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
gotPing <- true
|
|
|
|
}))
|
|
|
|
defer waitPing.Close()
|
|
|
|
|
|
|
|
nodes := env.Control.AllNodes()
|
|
|
|
if len(nodes) != 1 {
|
|
|
|
t.Fatalf("expected 1 node, got %d nodes", len(nodes))
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeKey := nodes[0].Key
|
2021-06-10 22:12:25 +00:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
t.Logf("ping %v ...", i)
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
|
|
|
if err := env.Control.AwaitNodeInMapRequest(ctx, nodeKey); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
cancel()
|
|
|
|
pr := &tailcfg.PingRequest{URL: fmt.Sprintf("%s/ping-%d", waitPing.URL, i), Log: true}
|
|
|
|
ok := env.Control.AddPingRequest(nodeKey, pr)
|
|
|
|
if !ok {
|
|
|
|
t.Fatalf("no node found with NodeKey %v in AddPingRequest", nodeKey)
|
|
|
|
}
|
2021-06-02 15:23:24 +00:00
|
|
|
|
2021-06-10 22:12:25 +00:00
|
|
|
// Wait for PingRequest to come back
|
|
|
|
pingTimeout := time.NewTimer(2 * time.Second)
|
|
|
|
select {
|
|
|
|
case <-gotPing:
|
|
|
|
pingTimeout.Stop()
|
|
|
|
case <-pingTimeout.C:
|
|
|
|
t.Fatal("didn't get PingRequest from tailscaled")
|
|
|
|
}
|
2021-06-02 15:23:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 03:27:05 +00:00
|
|
|
// testEnv contains the test environment (set of servers) used by one
|
|
|
|
// or more nodes.
|
|
|
|
type testEnv struct {
|
2021-05-12 04:57:25 +00:00
|
|
|
t testing.TB
|
2021-05-31 15:35:01 +00:00
|
|
|
Binaries *Binaries
|
2021-05-01 03:27:05 +00:00
|
|
|
|
|
|
|
LogCatcher *logCatcher
|
|
|
|
LogCatcherServer *httptest.Server
|
|
|
|
|
|
|
|
Control *testcontrol.Server
|
|
|
|
ControlServer *httptest.Server
|
|
|
|
|
2021-05-03 21:22:18 +00:00
|
|
|
TrafficTrap *trafficTrap
|
|
|
|
TrafficTrapServer *httptest.Server
|
2021-05-03 17:49:45 +00:00
|
|
|
|
|
|
|
derpShutdown func()
|
2021-05-01 03:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newTestEnv starts a bunch of services and returns a new test
|
|
|
|
// environment.
|
|
|
|
//
|
|
|
|
// Call Close to shut everything down.
|
2021-05-31 15:35:01 +00:00
|
|
|
func newTestEnv(t testing.TB, bins *Binaries) *testEnv {
|
2021-05-12 04:57:25 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
t.Skip("not tested/working on Windows yet")
|
|
|
|
}
|
2021-05-03 21:22:18 +00:00
|
|
|
derpMap, derpShutdown := runDERPAndStun(t, logger.Discard)
|
2021-05-01 03:27:05 +00:00
|
|
|
logc := new(logCatcher)
|
2021-05-03 17:49:45 +00:00
|
|
|
control := &testcontrol.Server{
|
|
|
|
DERPMap: derpMap,
|
2021-05-01 03:27:05 +00:00
|
|
|
}
|
2021-05-03 21:22:18 +00:00
|
|
|
trafficTrap := new(trafficTrap)
|
2021-05-03 17:49:45 +00:00
|
|
|
e := &testEnv{
|
2021-05-12 04:57:25 +00:00
|
|
|
t: t,
|
2021-05-03 21:22:18 +00:00
|
|
|
Binaries: bins,
|
|
|
|
LogCatcher: logc,
|
|
|
|
LogCatcherServer: httptest.NewServer(logc),
|
|
|
|
Control: control,
|
|
|
|
ControlServer: httptest.NewServer(control),
|
|
|
|
TrafficTrap: trafficTrap,
|
|
|
|
TrafficTrapServer: httptest.NewServer(trafficTrap),
|
|
|
|
derpShutdown: derpShutdown,
|
2021-05-03 17:49:45 +00:00
|
|
|
}
|
2021-05-12 04:57:25 +00:00
|
|
|
e.Control.BaseURL = e.ControlServer.URL
|
2021-05-03 17:49:45 +00:00
|
|
|
return e
|
2021-05-01 03:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e *testEnv) Close() error {
|
2021-05-12 04:57:25 +00:00
|
|
|
if err := e.TrafficTrap.Err(); err != nil {
|
|
|
|
e.t.Errorf("traffic trap: %v", err)
|
|
|
|
e.t.Logf("logs: %s", e.LogCatcher.logsString())
|
|
|
|
}
|
|
|
|
|
2021-05-01 03:27:05 +00:00
|
|
|
e.LogCatcherServer.Close()
|
2021-05-03 21:22:18 +00:00
|
|
|
e.TrafficTrapServer.Close()
|
2021-05-01 03:27:05 +00:00
|
|
|
e.ControlServer.Close()
|
2021-05-03 17:49:45 +00:00
|
|
|
e.derpShutdown()
|
2021-05-01 03:27:05 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// testNode is a machine with a tailscale & tailscaled.
|
|
|
|
// Currently, the test is simplistic and user==node==machine.
|
|
|
|
// That may grow complexity later to test more.
|
|
|
|
type testNode struct {
|
|
|
|
env *testEnv
|
|
|
|
|
|
|
|
dir string // temp dir for sock & state
|
|
|
|
sockFile string
|
|
|
|
stateFile string
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTestNode allocates a temp directory for a new test node.
|
|
|
|
// The node is not started automatically.
|
|
|
|
func newTestNode(t *testing.T, env *testEnv) *testNode {
|
|
|
|
dir := t.TempDir()
|
|
|
|
return &testNode{
|
|
|
|
env: env,
|
|
|
|
dir: dir,
|
|
|
|
sockFile: filepath.Join(dir, "tailscale.sock"),
|
|
|
|
stateFile: filepath.Join(dir, "tailscale.state"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 04:57:25 +00:00
|
|
|
type Daemon struct {
|
|
|
|
Process *os.Process
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Daemon) Kill() {
|
|
|
|
d.Process.Kill()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Daemon) MustCleanShutdown(t testing.TB) {
|
|
|
|
d.Process.Signal(os.Interrupt)
|
|
|
|
ps, err := d.Process.Wait()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("tailscaled Wait: %v", err)
|
|
|
|
}
|
|
|
|
if ps.ExitCode() != 0 {
|
|
|
|
t.Errorf("tailscaled ExitCode = %d; want 0", ps.ExitCode())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 03:27:05 +00:00
|
|
|
// StartDaemon starts the node's tailscaled, failing if it fails to
|
|
|
|
// start.
|
2021-05-12 04:57:25 +00:00
|
|
|
func (n *testNode) StartDaemon(t testing.TB) *Daemon {
|
2021-05-31 15:35:01 +00:00
|
|
|
cmd := exec.Command(n.env.Binaries.Daemon,
|
2021-05-01 03:27:05 +00:00
|
|
|
"--tun=userspace-networking",
|
|
|
|
"--state="+n.stateFile,
|
|
|
|
"--socket="+n.sockFile,
|
|
|
|
)
|
|
|
|
cmd.Env = append(os.Environ(),
|
|
|
|
"TS_LOG_TARGET="+n.env.LogCatcherServer.URL,
|
2021-05-03 21:22:18 +00:00
|
|
|
"HTTP_PROXY="+n.env.TrafficTrapServer.URL,
|
|
|
|
"HTTPS_PROXY="+n.env.TrafficTrapServer.URL,
|
2021-05-01 03:27:05 +00:00
|
|
|
)
|
2021-06-10 22:12:25 +00:00
|
|
|
if *verboseTailscaled {
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stdout
|
|
|
|
}
|
2021-05-01 03:27:05 +00:00
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
t.Fatalf("starting tailscaled: %v", err)
|
|
|
|
}
|
2021-05-12 04:57:25 +00:00
|
|
|
return &Daemon{
|
|
|
|
Process: cmd.Process,
|
|
|
|
}
|
2021-05-01 03:27:05 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 21:43:43 +00:00
|
|
|
func (n *testNode) MustUp() {
|
|
|
|
t := n.env.t
|
|
|
|
t.Logf("Running up --login-server=%s ...", n.env.ControlServer.URL)
|
|
|
|
if err := n.Tailscale("up", "--login-server="+n.env.ControlServer.URL).Run(); err != nil {
|
|
|
|
t.Fatalf("up: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-03 17:49:45 +00:00
|
|
|
// AwaitListening waits for the tailscaled to be serving local clients
|
|
|
|
// over its localhost IPC mechanism. (Unix socket, etc)
|
|
|
|
func (n *testNode) AwaitListening(t testing.TB) {
|
|
|
|
if err := tstest.WaitFor(20*time.Second, func() (err error) {
|
|
|
|
c, err := safesocket.Connect(n.sockFile, 41112)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Close()
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-12 04:57:25 +00:00
|
|
|
func (n *testNode) AwaitIP(t testing.TB) (ips string) {
|
|
|
|
t.Helper()
|
|
|
|
if err := tstest.WaitFor(20*time.Second, func() error {
|
|
|
|
out, err := n.Tailscale("ip").Output()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ips = string(out)
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("awaiting an IP address: %v", err)
|
|
|
|
}
|
|
|
|
if ips == "" {
|
|
|
|
t.Fatalf("returned IP address was blank")
|
|
|
|
}
|
|
|
|
return ips
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *testNode) AwaitRunning(t testing.TB) {
|
|
|
|
t.Helper()
|
|
|
|
if err := tstest.WaitFor(20*time.Second, func() error {
|
|
|
|
st, err := n.Status()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if st.BackendState != "Running" {
|
|
|
|
return fmt.Errorf("in state %q", st.BackendState)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
t.Fatalf("failure/timeout waiting for transition to Running status: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-01 03:27:05 +00:00
|
|
|
// Tailscale returns a command that runs the tailscale CLI with the provided arguments.
|
|
|
|
// It does not start the process.
|
|
|
|
func (n *testNode) Tailscale(arg ...string) *exec.Cmd {
|
2021-05-31 15:35:01 +00:00
|
|
|
cmd := exec.Command(n.env.Binaries.CLI, "--socket="+n.sockFile)
|
2021-05-01 03:27:05 +00:00
|
|
|
cmd.Args = append(cmd.Args, arg...)
|
|
|
|
cmd.Dir = n.dir
|
|
|
|
return cmd
|
2021-04-29 21:44:08 +00:00
|
|
|
}
|
|
|
|
|
2021-05-12 04:57:25 +00:00
|
|
|
func (n *testNode) Status() (*ipnstate.Status, error) {
|
2021-05-03 21:22:18 +00:00
|
|
|
out, err := n.Tailscale("status", "--json").CombinedOutput()
|
|
|
|
if err != nil {
|
2021-05-12 04:57:25 +00:00
|
|
|
return nil, fmt.Errorf("running tailscale status: %v, %s", err, out)
|
2021-05-03 21:22:18 +00:00
|
|
|
}
|
|
|
|
st := new(ipnstate.Status)
|
|
|
|
if err := json.Unmarshal(out, st); err != nil {
|
2021-05-12 04:57:25 +00:00
|
|
|
return nil, fmt.Errorf("decoding tailscale status JSON: %w", err)
|
|
|
|
}
|
|
|
|
return st, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n *testNode) MustStatus(tb testing.TB) *ipnstate.Status {
|
|
|
|
tb.Helper()
|
|
|
|
st, err := n.Status()
|
|
|
|
if err != nil {
|
|
|
|
tb.Fatal(err)
|
2021-05-03 21:22:18 +00:00
|
|
|
}
|
|
|
|
return st
|
|
|
|
}
|
|
|
|
|
2021-05-01 03:27:05 +00:00
|
|
|
// logCatcher is a minimal logcatcher for the logtail upload client.
|
2021-04-29 21:44:08 +00:00
|
|
|
type logCatcher struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
buf bytes.Buffer
|
|
|
|
gotErr error
|
|
|
|
reqs int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lc *logCatcher) logsContains(sub mem.RO) bool {
|
|
|
|
lc.mu.Lock()
|
|
|
|
defer lc.mu.Unlock()
|
|
|
|
return mem.Contains(mem.B(lc.buf.Bytes()), sub)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lc *logCatcher) numRequests() int {
|
|
|
|
lc.mu.Lock()
|
|
|
|
defer lc.mu.Unlock()
|
|
|
|
return lc.reqs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lc *logCatcher) logsString() string {
|
|
|
|
lc.mu.Lock()
|
|
|
|
defer lc.mu.Unlock()
|
|
|
|
return lc.buf.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lc *logCatcher) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var body io.Reader = r.Body
|
|
|
|
if r.Header.Get("Content-Encoding") == "zstd" {
|
|
|
|
var err error
|
|
|
|
body, err = smallzstd.NewDecoder(body)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("bad caught zstd: %v", err)
|
|
|
|
http.Error(w, err.Error(), 400)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bodyBytes, _ := ioutil.ReadAll(body)
|
|
|
|
|
|
|
|
type Entry struct {
|
|
|
|
Logtail struct {
|
|
|
|
ClientTime time.Time `json:"client_time"`
|
|
|
|
ServerTime time.Time `json:"server_time"`
|
|
|
|
Error struct {
|
|
|
|
BadData string `json:"bad_data"`
|
|
|
|
} `json:"error"`
|
|
|
|
} `json:"logtail"`
|
|
|
|
Text string `json:"text"`
|
|
|
|
}
|
|
|
|
var jreq []Entry
|
|
|
|
var err error
|
|
|
|
if len(bodyBytes) > 0 && bodyBytes[0] == '[' {
|
|
|
|
err = json.Unmarshal(bodyBytes, &jreq)
|
|
|
|
} else {
|
|
|
|
var ent Entry
|
|
|
|
err = json.Unmarshal(bodyBytes, &ent)
|
|
|
|
jreq = append(jreq, ent)
|
|
|
|
}
|
|
|
|
|
|
|
|
lc.mu.Lock()
|
|
|
|
defer lc.mu.Unlock()
|
|
|
|
lc.reqs++
|
|
|
|
if lc.gotErr == nil && err != nil {
|
|
|
|
lc.gotErr = err
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(&lc.buf, "error from %s of %#q: %v\n", r.Method, bodyBytes, err)
|
|
|
|
} else {
|
|
|
|
for _, ent := range jreq {
|
|
|
|
fmt.Fprintf(&lc.buf, "%s\n", strings.TrimSpace(ent.Text))
|
2021-06-10 18:23:53 +00:00
|
|
|
if *verboseLogCatcher {
|
2021-05-12 04:57:25 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "%s\n", strings.TrimSpace(ent.Text))
|
|
|
|
}
|
2021-04-29 21:44:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
w.WriteHeader(200) // must have no content, but not a 204
|
|
|
|
}
|
2021-05-01 03:27:05 +00:00
|
|
|
|
2021-05-03 21:22:18 +00:00
|
|
|
// trafficTrap is an HTTP proxy handler to note whether any
|
2021-05-03 17:49:45 +00:00
|
|
|
// HTTP traffic tries to leave localhost from tailscaled. We don't
|
|
|
|
// expect any, so any request triggers a failure.
|
2021-05-03 21:22:18 +00:00
|
|
|
type trafficTrap struct {
|
|
|
|
atomicErr atomic.Value // of error
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tt *trafficTrap) Err() error {
|
|
|
|
if err, ok := tt.atomicErr.Load().(error); ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tt *trafficTrap) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2021-05-01 03:27:05 +00:00
|
|
|
var got bytes.Buffer
|
|
|
|
r.Write(&got)
|
|
|
|
err := fmt.Errorf("unexpected HTTP proxy via proxy: %s", got.Bytes())
|
2021-05-03 17:49:45 +00:00
|
|
|
mainError.Store(err)
|
2021-05-03 21:22:18 +00:00
|
|
|
if tt.Err() == nil {
|
|
|
|
// Best effort at remembering the first request.
|
|
|
|
tt.atomicErr.Store(err)
|
|
|
|
}
|
2021-05-03 17:49:45 +00:00
|
|
|
log.Printf("Error: %v", err)
|
2021-05-03 21:22:18 +00:00
|
|
|
w.WriteHeader(403)
|
2021-05-03 17:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func runDERPAndStun(t testing.TB, logf logger.Logf) (derpMap *tailcfg.DERPMap, cleanup func()) {
|
|
|
|
var serverPrivateKey key.Private
|
|
|
|
if _, err := crand.Read(serverPrivateKey[:]); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
d := derp.NewServer(serverPrivateKey, logf)
|
|
|
|
|
|
|
|
httpsrv := httptest.NewUnstartedServer(derphttp.Handler(d))
|
|
|
|
httpsrv.Config.ErrorLog = logger.StdLogger(logf)
|
|
|
|
httpsrv.Config.TLSNextProto = make(map[string]func(*http.Server, *tls.Conn, http.Handler))
|
|
|
|
httpsrv.StartTLS()
|
|
|
|
|
|
|
|
stunAddr, stunCleanup := stuntest.ServeWithPacketListener(t, nettype.Std{})
|
|
|
|
|
|
|
|
m := &tailcfg.DERPMap{
|
|
|
|
Regions: map[int]*tailcfg.DERPRegion{
|
|
|
|
1: {
|
|
|
|
RegionID: 1,
|
|
|
|
RegionCode: "test",
|
|
|
|
Nodes: []*tailcfg.DERPNode{
|
|
|
|
{
|
|
|
|
Name: "t1",
|
|
|
|
RegionID: 1,
|
|
|
|
HostName: "127.0.0.1", // to bypass HTTP proxy
|
|
|
|
IPv4: "127.0.0.1",
|
|
|
|
IPv6: "none",
|
|
|
|
STUNPort: stunAddr.Port,
|
|
|
|
DERPTestPort: httpsrv.Listener.Addr().(*net.TCPAddr).Port,
|
|
|
|
STUNTestIP: stunAddr.IP.String(),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanup = func() {
|
|
|
|
httpsrv.CloseClientConnections()
|
|
|
|
httpsrv.Close()
|
|
|
|
d.Close()
|
|
|
|
stunCleanup()
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, cleanup
|
2021-05-01 03:27:05 +00:00
|
|
|
}
|
2021-05-12 04:57:25 +00:00
|
|
|
|
|
|
|
type authURLParserWriter struct {
|
|
|
|
buf bytes.Buffer
|
|
|
|
fn func(urlStr string) error
|
|
|
|
}
|
|
|
|
|
|
|
|
var authURLRx = regexp.MustCompile(`(https?://\S+/auth/\S+)`)
|
|
|
|
|
|
|
|
func (w *authURLParserWriter) Write(p []byte) (n int, err error) {
|
|
|
|
n, err = w.buf.Write(p)
|
|
|
|
m := authURLRx.FindSubmatch(w.buf.Bytes())
|
|
|
|
if m != nil {
|
|
|
|
urlStr := string(m[1])
|
|
|
|
w.buf.Reset() // so it's not matched again
|
|
|
|
if err := w.fn(urlStr); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|