2023-01-27 21:37:20 +00:00
|
|
|
// Copyright (c) Tailscale Inc & AUTHORS
|
|
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
2022-11-09 01:29:35 +00:00
|
|
|
|
|
|
|
//go:build linux
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
_ "embed"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/pem"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/fs"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/netip"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
2022-11-10 06:01:34 +00:00
|
|
|
"strconv"
|
2022-11-09 01:29:35 +00:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-11-10 06:01:34 +00:00
|
|
|
"github.com/google/go-cmp/cmp"
|
2022-11-09 01:29:35 +00:00
|
|
|
"golang.org/x/sys/unix"
|
2022-12-07 20:29:45 +00:00
|
|
|
"tailscale.com/ipn"
|
2022-11-10 06:01:34 +00:00
|
|
|
"tailscale.com/tailcfg"
|
2022-12-06 03:11:27 +00:00
|
|
|
"tailscale.com/tstest"
|
2022-12-07 20:29:45 +00:00
|
|
|
"tailscale.com/types/netmap"
|
|
|
|
"tailscale.com/types/ptr"
|
2022-11-09 01:29:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestContainerBoot(t *testing.T) {
|
2022-11-10 06:01:34 +00:00
|
|
|
d := t.TempDir()
|
2022-11-09 01:29:35 +00:00
|
|
|
|
|
|
|
lapi := localAPI{FSRoot: d}
|
|
|
|
if err := lapi.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer lapi.Close()
|
|
|
|
|
|
|
|
kube := kubeServer{FSRoot: d}
|
|
|
|
if err := kube.Start(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer kube.Close()
|
|
|
|
|
2022-11-10 06:01:34 +00:00
|
|
|
dirs := []string{
|
|
|
|
"var/lib",
|
|
|
|
"usr/bin",
|
|
|
|
"tmp",
|
|
|
|
"dev/net",
|
|
|
|
"proc/sys/net/ipv4",
|
|
|
|
"proc/sys/net/ipv6/conf/all",
|
|
|
|
}
|
|
|
|
for _, path := range dirs {
|
2022-11-09 01:29:35 +00:00
|
|
|
if err := os.MkdirAll(filepath.Join(d, path), 0700); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2022-11-10 06:01:34 +00:00
|
|
|
files := map[string][]byte{
|
|
|
|
"usr/bin/tailscaled": fakeTailscaled,
|
|
|
|
"usr/bin/tailscale": fakeTailscale,
|
|
|
|
"usr/bin/iptables": fakeTailscale,
|
|
|
|
"usr/bin/ip6tables": fakeTailscale,
|
|
|
|
"dev/net/tun": []byte(""),
|
|
|
|
"proc/sys/net/ipv4/ip_forward": []byte("0"),
|
|
|
|
"proc/sys/net/ipv6/conf/all/forwarding": []byte("0"),
|
2022-11-09 01:29:35 +00:00
|
|
|
}
|
2022-11-10 06:01:34 +00:00
|
|
|
resetFiles := func() {
|
|
|
|
for path, content := range files {
|
|
|
|
// Making everything executable is a little weird, but the
|
|
|
|
// stuff that doesn't need to be executable doesn't care if we
|
|
|
|
// do make it executable.
|
|
|
|
if err := os.WriteFile(filepath.Join(d, path), content, 0700); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2022-11-09 01:29:35 +00:00
|
|
|
}
|
2022-11-10 06:01:34 +00:00
|
|
|
resetFiles()
|
2022-11-09 01:29:35 +00:00
|
|
|
|
|
|
|
boot := filepath.Join(d, "containerboot")
|
|
|
|
if err := exec.Command("go", "build", "-o", boot, "tailscale.com/cmd/containerboot").Run(); err != nil {
|
|
|
|
t.Fatalf("Building containerboot: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
argFile := filepath.Join(d, "args")
|
2022-11-10 06:01:34 +00:00
|
|
|
runningSockPath := filepath.Join(d, "tmp/tailscaled.sock")
|
|
|
|
|
2022-12-06 03:11:27 +00:00
|
|
|
type phase struct {
|
2022-12-07 20:29:45 +00:00
|
|
|
// If non-nil, send this IPN bus notification (and remember it as the
|
|
|
|
// initial update for any future new watchers, then wait for all the
|
|
|
|
// Waits below to be true before proceeding to the next phase.
|
|
|
|
Notify *ipn.Notify
|
2022-12-06 03:11:27 +00:00
|
|
|
|
|
|
|
// WantCmds is the commands that containerboot should run in this phase.
|
|
|
|
WantCmds []string
|
|
|
|
// WantKubeSecret is the secret keys/values that should exist in the
|
|
|
|
// kube secret.
|
2022-11-10 06:01:34 +00:00
|
|
|
WantKubeSecret map[string]string
|
2022-12-06 03:11:27 +00:00
|
|
|
// WantFiles files that should exist in the container and their
|
|
|
|
// contents.
|
|
|
|
WantFiles map[string]string
|
|
|
|
}
|
2022-12-07 20:29:45 +00:00
|
|
|
runningNotify := &ipn.Notify{
|
|
|
|
State: ptr.To(ipn.Running),
|
|
|
|
NetMap: &netmap.NetworkMap{
|
2023-08-21 17:53:57 +00:00
|
|
|
SelfNode: (&tailcfg.Node{
|
2023-08-08 23:03:08 +00:00
|
|
|
StableID: tailcfg.StableNodeID("myID"),
|
|
|
|
Name: "test-node.test.ts.net",
|
|
|
|
Addresses: []netip.Prefix{netip.MustParsePrefix("100.64.0.1/32")},
|
2023-08-21 17:53:57 +00:00
|
|
|
}).View(),
|
2022-12-07 20:29:45 +00:00
|
|
|
},
|
|
|
|
}
|
2022-12-06 03:11:27 +00:00
|
|
|
tests := []struct {
|
2022-12-07 01:03:53 +00:00
|
|
|
Name string
|
|
|
|
Env map[string]string
|
|
|
|
KubeSecret map[string]string
|
|
|
|
KubeDenyPatch bool
|
|
|
|
Phases []phase
|
2022-11-10 06:01:34 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
// Out of the box default: runs in userspace mode, ephemeral storage, interactive login.
|
|
|
|
Name: "no_args",
|
2023-10-16 01:41:28 +00:00
|
|
|
Env: nil,
|
2022-12-06 03:11:27 +00:00
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=mem: --statedir=/tmp --tun=userspace-networking",
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false",
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-12-07 20:29:45 +00:00
|
|
|
Notify: runningNotify,
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
2022-11-10 06:01:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Userspace mode, ephemeral storage, authkey provided on every run.
|
|
|
|
Name: "authkey",
|
2023-01-03 23:17:44 +00:00
|
|
|
Env: map[string]string{
|
2023-10-16 01:41:28 +00:00
|
|
|
"TS_AUTHKEY": "tskey-key",
|
2023-01-03 23:17:44 +00:00
|
|
|
},
|
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=mem: --statedir=/tmp --tun=userspace-networking",
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false --authkey=tskey-key",
|
2023-01-03 23:17:44 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Notify: runningNotify,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// Userspace mode, ephemeral storage, authkey provided on every run.
|
|
|
|
Name: "authkey-old-flag",
|
2022-11-10 06:01:34 +00:00
|
|
|
Env: map[string]string{
|
2023-10-16 01:41:28 +00:00
|
|
|
"TS_AUTH_KEY": "tskey-key",
|
2022-11-10 06:01:34 +00:00
|
|
|
},
|
2022-12-06 03:11:27 +00:00
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=mem: --statedir=/tmp --tun=userspace-networking",
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false --authkey=tskey-key",
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-12-07 20:29:45 +00:00
|
|
|
Notify: runningNotify,
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
2022-11-10 06:01:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "authkey_disk_state",
|
|
|
|
Env: map[string]string{
|
2023-01-03 23:17:44 +00:00
|
|
|
"TS_AUTHKEY": "tskey-key",
|
2022-11-10 06:01:34 +00:00
|
|
|
"TS_STATE_DIR": filepath.Join(d, "tmp"),
|
|
|
|
},
|
2022-12-06 03:11:27 +00:00
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --statedir=/tmp --tun=userspace-networking",
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false --authkey=tskey-key",
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-12-07 20:29:45 +00:00
|
|
|
Notify: runningNotify,
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
2022-11-10 06:01:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "routes",
|
|
|
|
Env: map[string]string{
|
2023-10-16 01:41:28 +00:00
|
|
|
"TS_AUTHKEY": "tskey-key",
|
|
|
|
"TS_ROUTES": "1.2.3.0/24,10.20.30.0/24",
|
2022-11-10 06:01:34 +00:00
|
|
|
},
|
2022-12-06 03:11:27 +00:00
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=mem: --statedir=/tmp --tun=userspace-networking",
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false --authkey=tskey-key --advertise-routes=1.2.3.0/24,10.20.30.0/24",
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-12-07 20:29:45 +00:00
|
|
|
Notify: runningNotify,
|
2022-12-06 03:11:27 +00:00
|
|
|
WantFiles: map[string]string{
|
2024-01-04 09:17:04 +00:00
|
|
|
"proc/sys/net/ipv4/ip_forward": "0",
|
|
|
|
"proc/sys/net/ipv6/conf/all/forwarding": "0",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "empty routes",
|
|
|
|
Env: map[string]string{
|
|
|
|
"TS_AUTHKEY": "tskey-key",
|
|
|
|
"TS_ROUTES": "",
|
|
|
|
},
|
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=mem: --statedir=/tmp --tun=userspace-networking",
|
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false --authkey=tskey-key --advertise-routes=",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Notify: runningNotify,
|
|
|
|
WantFiles: map[string]string{
|
2022-12-06 03:11:27 +00:00
|
|
|
"proc/sys/net/ipv4/ip_forward": "0",
|
|
|
|
"proc/sys/net/ipv6/conf/all/forwarding": "0",
|
|
|
|
},
|
|
|
|
},
|
2022-11-10 06:01:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "routes_kernel_ipv4",
|
|
|
|
Env: map[string]string{
|
2023-01-03 23:17:44 +00:00
|
|
|
"TS_AUTHKEY": "tskey-key",
|
2022-11-10 06:01:34 +00:00
|
|
|
"TS_ROUTES": "1.2.3.0/24,10.20.30.0/24",
|
|
|
|
"TS_USERSPACE": "false",
|
|
|
|
},
|
2022-12-06 03:11:27 +00:00
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=mem: --statedir=/tmp",
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false --authkey=tskey-key --advertise-routes=1.2.3.0/24,10.20.30.0/24",
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-12-07 20:29:45 +00:00
|
|
|
Notify: runningNotify,
|
2022-12-06 03:11:27 +00:00
|
|
|
WantFiles: map[string]string{
|
|
|
|
"proc/sys/net/ipv4/ip_forward": "1",
|
|
|
|
"proc/sys/net/ipv6/conf/all/forwarding": "0",
|
|
|
|
},
|
|
|
|
},
|
2022-11-10 06:01:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "routes_kernel_ipv6",
|
|
|
|
Env: map[string]string{
|
2023-01-03 23:17:44 +00:00
|
|
|
"TS_AUTHKEY": "tskey-key",
|
2022-11-10 06:01:34 +00:00
|
|
|
"TS_ROUTES": "::/64,1::/64",
|
|
|
|
"TS_USERSPACE": "false",
|
|
|
|
},
|
2022-12-06 03:11:27 +00:00
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=mem: --statedir=/tmp",
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false --authkey=tskey-key --advertise-routes=::/64,1::/64",
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-12-07 20:29:45 +00:00
|
|
|
Notify: runningNotify,
|
2022-12-06 03:11:27 +00:00
|
|
|
WantFiles: map[string]string{
|
|
|
|
"proc/sys/net/ipv4/ip_forward": "0",
|
|
|
|
"proc/sys/net/ipv6/conf/all/forwarding": "1",
|
|
|
|
},
|
|
|
|
},
|
2022-11-10 06:01:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "routes_kernel_all_families",
|
|
|
|
Env: map[string]string{
|
2023-01-03 23:17:44 +00:00
|
|
|
"TS_AUTHKEY": "tskey-key",
|
2022-11-10 06:01:34 +00:00
|
|
|
"TS_ROUTES": "::/64,1.2.3.0/24",
|
|
|
|
"TS_USERSPACE": "false",
|
|
|
|
},
|
2022-12-06 03:11:27 +00:00
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=mem: --statedir=/tmp",
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false --authkey=tskey-key --advertise-routes=::/64,1.2.3.0/24",
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-12-07 20:29:45 +00:00
|
|
|
Notify: runningNotify,
|
2022-12-06 03:11:27 +00:00
|
|
|
WantFiles: map[string]string{
|
|
|
|
"proc/sys/net/ipv4/ip_forward": "1",
|
|
|
|
"proc/sys/net/ipv6/conf/all/forwarding": "1",
|
|
|
|
},
|
|
|
|
},
|
2022-11-10 06:01:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2023-08-30 13:22:06 +00:00
|
|
|
Name: "ingres proxy",
|
2022-11-10 06:01:34 +00:00
|
|
|
Env: map[string]string{
|
2023-01-03 23:17:44 +00:00
|
|
|
"TS_AUTHKEY": "tskey-key",
|
2022-11-10 06:01:34 +00:00
|
|
|
"TS_DEST_IP": "1.2.3.4",
|
|
|
|
"TS_USERSPACE": "false",
|
|
|
|
},
|
2022-12-06 03:11:27 +00:00
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=mem: --statedir=/tmp",
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false --authkey=tskey-key",
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-12-07 20:29:45 +00:00
|
|
|
Notify: runningNotify,
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
2022-11-10 06:01:34 +00:00
|
|
|
},
|
|
|
|
},
|
2023-08-30 13:22:06 +00:00
|
|
|
{
|
|
|
|
Name: "egress proxy",
|
|
|
|
Env: map[string]string{
|
|
|
|
"TS_AUTHKEY": "tskey-key",
|
|
|
|
"TS_TAILNET_TARGET_IP": "100.99.99.99",
|
|
|
|
"TS_USERSPACE": "false",
|
|
|
|
},
|
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=mem: --statedir=/tmp",
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false --authkey=tskey-key",
|
2023-08-30 13:22:06 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Notify: runningNotify,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-11-10 06:01:34 +00:00
|
|
|
{
|
|
|
|
Name: "authkey_once",
|
|
|
|
Env: map[string]string{
|
2023-01-03 23:17:44 +00:00
|
|
|
"TS_AUTHKEY": "tskey-key",
|
2022-11-10 06:01:34 +00:00
|
|
|
"TS_AUTH_ONCE": "true",
|
|
|
|
},
|
2022-12-06 03:11:27 +00:00
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=mem: --statedir=/tmp --tun=userspace-networking",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-12-07 20:29:45 +00:00
|
|
|
Notify: &ipn.Notify{
|
|
|
|
State: ptr.To(ipn.NeedsLogin),
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
|
|
|
WantCmds: []string{
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false --authkey=tskey-key",
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-12-07 20:29:45 +00:00
|
|
|
Notify: runningNotify,
|
2023-08-30 00:24:25 +00:00
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock set --accept-dns=false",
|
|
|
|
},
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
2022-11-10 06:01:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "kube_storage",
|
|
|
|
Env: map[string]string{
|
|
|
|
"KUBERNETES_SERVICE_HOST": kube.Host,
|
|
|
|
"KUBERNETES_SERVICE_PORT_HTTPS": kube.Port,
|
|
|
|
},
|
|
|
|
KubeSecret: map[string]string{
|
|
|
|
"authkey": "tskey-key",
|
|
|
|
},
|
2022-12-06 03:11:27 +00:00
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=kube:tailscale --statedir=/tmp --tun=userspace-networking",
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false --authkey=tskey-key",
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
|
|
|
WantKubeSecret: map[string]string{
|
|
|
|
"authkey": "tskey-key",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-12-07 20:29:45 +00:00
|
|
|
Notify: runningNotify,
|
2022-12-06 03:11:27 +00:00
|
|
|
WantKubeSecret: map[string]string{
|
2022-12-07 20:29:45 +00:00
|
|
|
"authkey": "tskey-key",
|
|
|
|
"device_fqdn": "test-node.test.ts.net",
|
|
|
|
"device_id": "myID",
|
2023-08-08 23:03:08 +00:00
|
|
|
"device_ips": `["100.64.0.1"]`,
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
2022-11-10 06:01:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-12-16 22:09:46 +00:00
|
|
|
{
|
|
|
|
Name: "kube_disk_storage",
|
|
|
|
Env: map[string]string{
|
|
|
|
"KUBERNETES_SERVICE_HOST": kube.Host,
|
|
|
|
"KUBERNETES_SERVICE_PORT_HTTPS": kube.Port,
|
|
|
|
// Explicitly set to an empty value, to override the default of "tailscale".
|
|
|
|
"TS_KUBE_SECRET": "",
|
|
|
|
"TS_STATE_DIR": filepath.Join(d, "tmp"),
|
2023-01-03 23:17:44 +00:00
|
|
|
"TS_AUTHKEY": "tskey-key",
|
2022-12-16 22:09:46 +00:00
|
|
|
},
|
|
|
|
KubeSecret: map[string]string{},
|
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --statedir=/tmp --tun=userspace-networking",
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false --authkey=tskey-key",
|
2022-12-16 22:09:46 +00:00
|
|
|
},
|
|
|
|
WantKubeSecret: map[string]string{},
|
|
|
|
},
|
|
|
|
{
|
2023-10-16 01:41:28 +00:00
|
|
|
Notify: runningNotify,
|
2022-12-16 22:09:46 +00:00
|
|
|
WantKubeSecret: map[string]string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-12-07 01:03:53 +00:00
|
|
|
{
|
|
|
|
Name: "kube_storage_no_patch",
|
|
|
|
Env: map[string]string{
|
|
|
|
"KUBERNETES_SERVICE_HOST": kube.Host,
|
|
|
|
"KUBERNETES_SERVICE_PORT_HTTPS": kube.Port,
|
2023-01-03 23:17:44 +00:00
|
|
|
"TS_AUTHKEY": "tskey-key",
|
2022-12-07 01:03:53 +00:00
|
|
|
},
|
|
|
|
KubeSecret: map[string]string{},
|
|
|
|
KubeDenyPatch: true,
|
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=kube:tailscale --statedir=/tmp --tun=userspace-networking",
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false --authkey=tskey-key",
|
2022-12-07 01:03:53 +00:00
|
|
|
},
|
|
|
|
WantKubeSecret: map[string]string{},
|
|
|
|
},
|
|
|
|
{
|
2023-10-16 01:41:28 +00:00
|
|
|
Notify: runningNotify,
|
2022-12-07 01:03:53 +00:00
|
|
|
WantKubeSecret: map[string]string{},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2022-11-10 06:01:34 +00:00
|
|
|
{
|
|
|
|
// Same as previous, but deletes the authkey from the kube secret.
|
|
|
|
Name: "kube_storage_auth_once",
|
|
|
|
Env: map[string]string{
|
|
|
|
"KUBERNETES_SERVICE_HOST": kube.Host,
|
|
|
|
"KUBERNETES_SERVICE_PORT_HTTPS": kube.Port,
|
|
|
|
"TS_AUTH_ONCE": "true",
|
|
|
|
},
|
|
|
|
KubeSecret: map[string]string{
|
|
|
|
"authkey": "tskey-key",
|
|
|
|
},
|
2022-12-06 03:11:27 +00:00
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=kube:tailscale --statedir=/tmp --tun=userspace-networking",
|
|
|
|
},
|
|
|
|
WantKubeSecret: map[string]string{
|
|
|
|
"authkey": "tskey-key",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-12-07 20:29:45 +00:00
|
|
|
Notify: &ipn.Notify{
|
|
|
|
State: ptr.To(ipn.NeedsLogin),
|
|
|
|
},
|
|
|
|
WantCmds: []string{
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false --authkey=tskey-key",
|
2022-12-07 20:29:45 +00:00
|
|
|
},
|
|
|
|
WantKubeSecret: map[string]string{
|
|
|
|
"authkey": "tskey-key",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Notify: runningNotify,
|
2023-08-30 00:24:25 +00:00
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock set --accept-dns=false",
|
|
|
|
},
|
2022-12-07 20:29:45 +00:00
|
|
|
WantKubeSecret: map[string]string{
|
|
|
|
"device_fqdn": "test-node.test.ts.net",
|
|
|
|
"device_id": "myID",
|
2023-08-08 23:03:08 +00:00
|
|
|
"device_ips": `["100.64.0.1"]`,
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
2022-12-07 20:29:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "kube_storage_updates",
|
|
|
|
Env: map[string]string{
|
|
|
|
"KUBERNETES_SERVICE_HOST": kube.Host,
|
|
|
|
"KUBERNETES_SERVICE_PORT_HTTPS": kube.Port,
|
|
|
|
},
|
|
|
|
KubeSecret: map[string]string{
|
|
|
|
"authkey": "tskey-key",
|
|
|
|
},
|
|
|
|
Phases: []phase{
|
|
|
|
{
|
2022-12-06 03:11:27 +00:00
|
|
|
WantCmds: []string{
|
2022-12-07 20:29:45 +00:00
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=kube:tailscale --statedir=/tmp --tun=userspace-networking",
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false --authkey=tskey-key",
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
|
|
|
WantKubeSecret: map[string]string{
|
|
|
|
"authkey": "tskey-key",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-12-07 20:29:45 +00:00
|
|
|
Notify: runningNotify,
|
|
|
|
WantKubeSecret: map[string]string{
|
|
|
|
"authkey": "tskey-key",
|
|
|
|
"device_fqdn": "test-node.test.ts.net",
|
|
|
|
"device_id": "myID",
|
2023-08-08 23:03:08 +00:00
|
|
|
"device_ips": `["100.64.0.1"]`,
|
2022-12-07 20:29:45 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Notify: &ipn.Notify{
|
|
|
|
State: ptr.To(ipn.Running),
|
|
|
|
NetMap: &netmap.NetworkMap{
|
2023-08-21 17:53:57 +00:00
|
|
|
SelfNode: (&tailcfg.Node{
|
2023-08-08 23:03:08 +00:00
|
|
|
StableID: tailcfg.StableNodeID("newID"),
|
|
|
|
Name: "new-name.test.ts.net",
|
|
|
|
Addresses: []netip.Prefix{netip.MustParsePrefix("100.64.0.1/32")},
|
2023-08-21 17:53:57 +00:00
|
|
|
}).View(),
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
WantKubeSecret: map[string]string{
|
2022-12-07 20:29:45 +00:00
|
|
|
"authkey": "tskey-key",
|
|
|
|
"device_fqdn": "new-name.test.ts.net",
|
|
|
|
"device_id": "newID",
|
2023-08-08 23:03:08 +00:00
|
|
|
"device_ips": `["100.64.0.1"]`,
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
2022-11-10 06:01:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "proxies",
|
|
|
|
Env: map[string]string{
|
|
|
|
"TS_SOCKS5_SERVER": "localhost:1080",
|
|
|
|
"TS_OUTBOUND_HTTP_PROXY_LISTEN": "localhost:8080",
|
|
|
|
},
|
2022-12-06 03:11:27 +00:00
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=mem: --statedir=/tmp --tun=userspace-networking --socks5-server=localhost:1080 --outbound-http-proxy-listen=localhost:8080",
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false",
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-12-07 20:29:45 +00:00
|
|
|
Notify: runningNotify,
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
2022-11-10 06:01:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "dns",
|
|
|
|
Env: map[string]string{
|
|
|
|
"TS_ACCEPT_DNS": "true",
|
|
|
|
},
|
2022-12-06 03:11:27 +00:00
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=mem: --statedir=/tmp --tun=userspace-networking",
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=true",
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-12-07 20:29:45 +00:00
|
|
|
Notify: runningNotify,
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
2022-11-10 06:01:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "extra_args",
|
|
|
|
Env: map[string]string{
|
|
|
|
"TS_EXTRA_ARGS": "--widget=rotated",
|
|
|
|
"TS_TAILSCALED_EXTRA_ARGS": "--experiments=widgets",
|
|
|
|
},
|
2022-12-06 03:11:27 +00:00
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=mem: --statedir=/tmp --tun=userspace-networking --experiments=widgets",
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false --widget=rotated",
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
2023-10-16 01:41:28 +00:00
|
|
|
}, {
|
2022-12-07 20:29:45 +00:00
|
|
|
Notify: runningNotify,
|
2022-12-06 03:11:27 +00:00
|
|
|
},
|
2023-01-25 18:16:59 +00:00
|
|
|
},
|
|
|
|
},
|
2023-11-22 16:45:44 +00:00
|
|
|
{
|
|
|
|
Name: "extra_args_accept_routes",
|
|
|
|
Env: map[string]string{
|
|
|
|
"TS_EXTRA_ARGS": "--accept-routes",
|
|
|
|
},
|
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=mem: --statedir=/tmp --tun=userspace-networking",
|
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false --accept-routes",
|
|
|
|
},
|
|
|
|
}, {
|
|
|
|
Notify: runningNotify,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-01-25 18:16:59 +00:00
|
|
|
{
|
|
|
|
Name: "hostname",
|
|
|
|
Env: map[string]string{
|
2023-10-16 01:41:28 +00:00
|
|
|
"TS_HOSTNAME": "my-server",
|
2023-01-25 18:16:59 +00:00
|
|
|
},
|
|
|
|
Phases: []phase{
|
|
|
|
{
|
|
|
|
WantCmds: []string{
|
|
|
|
"/usr/bin/tailscaled --socket=/tmp/tailscaled.sock --state=mem: --statedir=/tmp --tun=userspace-networking",
|
2023-10-16 01:41:28 +00:00
|
|
|
"/usr/bin/tailscale --socket=/tmp/tailscaled.sock up --accept-dns=false --hostname=my-server",
|
2023-01-25 18:16:59 +00:00
|
|
|
},
|
2023-10-16 01:41:28 +00:00
|
|
|
}, {
|
2023-01-25 18:16:59 +00:00
|
|
|
Notify: runningNotify,
|
|
|
|
},
|
2022-11-10 06:01:34 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2022-11-09 01:29:35 +00:00
|
|
|
|
2022-11-10 06:01:34 +00:00
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(test.Name, func(t *testing.T) {
|
|
|
|
lapi.Reset()
|
|
|
|
kube.Reset()
|
|
|
|
os.Remove(argFile)
|
|
|
|
os.Remove(runningSockPath)
|
|
|
|
resetFiles()
|
2022-11-09 01:29:35 +00:00
|
|
|
|
2022-11-10 06:01:34 +00:00
|
|
|
for k, v := range test.KubeSecret {
|
|
|
|
kube.SetSecret(k, v)
|
|
|
|
}
|
2022-12-07 01:03:53 +00:00
|
|
|
kube.SetPatching(!test.KubeDenyPatch)
|
2022-11-09 01:29:35 +00:00
|
|
|
|
2022-11-10 06:01:34 +00:00
|
|
|
cmd := exec.Command(boot)
|
|
|
|
cmd.Env = []string{
|
|
|
|
fmt.Sprintf("PATH=%s/usr/bin:%s", d, os.Getenv("PATH")),
|
|
|
|
fmt.Sprintf("TS_TEST_RECORD_ARGS=%s", argFile),
|
|
|
|
fmt.Sprintf("TS_TEST_SOCKET=%s", lapi.Path),
|
|
|
|
fmt.Sprintf("TS_SOCKET=%s", runningSockPath),
|
|
|
|
fmt.Sprintf("TS_TEST_ONLY_ROOT=%s", d),
|
2023-10-11 14:26:40 +00:00
|
|
|
fmt.Sprint("TS_TEST_FAKE_NETFILTER=true"),
|
2022-11-10 06:01:34 +00:00
|
|
|
}
|
|
|
|
for k, v := range test.Env {
|
|
|
|
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
|
|
|
|
}
|
|
|
|
cbOut := &lockingBuffer{}
|
|
|
|
defer func() {
|
|
|
|
if t.Failed() {
|
|
|
|
t.Logf("containerboot output:\n%s", cbOut.String())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
cmd.Stderr = cbOut
|
|
|
|
if err := cmd.Start(); err != nil {
|
|
|
|
t.Fatalf("starting containerboot: %v", err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2023-09-21 20:02:36 +00:00
|
|
|
cmd.Process.Signal(unix.SIGTERM)
|
2022-11-10 06:01:34 +00:00
|
|
|
cmd.Process.Wait()
|
|
|
|
}()
|
|
|
|
|
2022-12-06 03:11:27 +00:00
|
|
|
var wantCmds []string
|
2023-03-02 16:10:36 +00:00
|
|
|
for i, p := range test.Phases {
|
2022-12-07 20:29:45 +00:00
|
|
|
lapi.Notify(p.Notify)
|
2022-12-06 03:11:27 +00:00
|
|
|
wantCmds = append(wantCmds, p.WantCmds...)
|
|
|
|
waitArgs(t, 2*time.Second, d, argFile, strings.Join(wantCmds, "\n"))
|
|
|
|
err := tstest.WaitFor(2*time.Second, func() error {
|
|
|
|
if p.WantKubeSecret != nil {
|
|
|
|
got := kube.Secret()
|
|
|
|
if diff := cmp.Diff(got, p.WantKubeSecret); diff != "" {
|
|
|
|
return fmt.Errorf("unexpected kube secret data (-got+want):\n%s", diff)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
got := kube.Secret()
|
|
|
|
if len(got) > 0 {
|
|
|
|
return fmt.Errorf("kube secret unexpectedly not empty, got %#v", got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-11-10 06:01:34 +00:00
|
|
|
if err != nil {
|
2023-03-02 16:10:36 +00:00
|
|
|
t.Fatalf("phase %d: %v", i, err)
|
2022-11-10 06:01:34 +00:00
|
|
|
}
|
2022-12-06 03:11:27 +00:00
|
|
|
err = tstest.WaitFor(2*time.Second, func() error {
|
|
|
|
for path, want := range p.WantFiles {
|
|
|
|
gotBs, err := os.ReadFile(filepath.Join(d, path))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("reading wanted file %q: %v", path, err)
|
|
|
|
}
|
|
|
|
if got := strings.TrimSpace(string(gotBs)); got != want {
|
|
|
|
return fmt.Errorf("wrong file contents for %q, got %q want %q", path, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
2022-11-10 06:01:34 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-06 03:11:27 +00:00
|
|
|
waitLogLine(t, 2*time.Second, cbOut, "Startup complete, waiting for shutdown signal")
|
2022-11-10 06:01:34 +00:00
|
|
|
})
|
|
|
|
}
|
2022-11-09 01:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type lockingBuffer struct {
|
|
|
|
sync.Mutex
|
|
|
|
b bytes.Buffer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *lockingBuffer) Write(bs []byte) (int, error) {
|
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
return b.b.Write(bs)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *lockingBuffer) String() string {
|
|
|
|
b.Lock()
|
|
|
|
defer b.Unlock()
|
|
|
|
return b.b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
// waitLogLine looks for want in the contents of b.
|
|
|
|
//
|
|
|
|
// Only lines starting with 'boot: ' (the output of containerboot
|
|
|
|
// itself) are considered, and the logged timestamp is ignored.
|
|
|
|
//
|
|
|
|
// waitLogLine fails the entire test if path doesn't contain want
|
|
|
|
// before the timeout.
|
|
|
|
func waitLogLine(t *testing.T, timeout time.Duration, b *lockingBuffer, want string) {
|
|
|
|
deadline := time.Now().Add(timeout)
|
|
|
|
for time.Now().Before(deadline) {
|
|
|
|
for _, line := range strings.Split(b.String(), "\n") {
|
|
|
|
if !strings.HasPrefix(line, "boot: ") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if strings.HasSuffix(line, " "+want) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
t.Fatalf("timed out waiting for wanted output line %q. Output:\n%s", want, b.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
// waitArgs waits until the contents of path matches wantArgs, a set
|
|
|
|
// of command lines recorded by test_tailscale.sh and
|
|
|
|
// test_tailscaled.sh.
|
|
|
|
//
|
|
|
|
// All occurrences of removeStr are removed from the file prior to
|
|
|
|
// comparison. This is used to remove the varying temporary root
|
|
|
|
// directory name from recorded commandlines, so that wantArgs can be
|
|
|
|
// a constant value.
|
|
|
|
//
|
|
|
|
// waitArgs fails the entire test if path doesn't contain wantArgs
|
|
|
|
// before the timeout.
|
|
|
|
func waitArgs(t *testing.T, timeout time.Duration, removeStr, path, wantArgs string) {
|
|
|
|
t.Helper()
|
|
|
|
wantArgs = strings.TrimSpace(wantArgs)
|
|
|
|
deadline := time.Now().Add(timeout)
|
|
|
|
var got string
|
|
|
|
for time.Now().Before(deadline) {
|
|
|
|
bs, err := os.ReadFile(path)
|
|
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
|
|
// Don't bother logging that the file doesn't exist, it
|
|
|
|
// should start existing soon.
|
|
|
|
goto loop
|
|
|
|
} else if err != nil {
|
|
|
|
t.Logf("reading %q: %v", path, err)
|
|
|
|
goto loop
|
|
|
|
}
|
|
|
|
got = strings.TrimSpace(string(bs))
|
|
|
|
got = strings.ReplaceAll(got, removeStr, "")
|
|
|
|
if got == wantArgs {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
loop:
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
}
|
|
|
|
t.Fatalf("waiting for args file %q to have expected output, got:\n%s\n\nWant: %s", path, got, wantArgs)
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:embed test_tailscaled.sh
|
|
|
|
var fakeTailscaled []byte
|
|
|
|
|
|
|
|
//go:embed test_tailscale.sh
|
|
|
|
var fakeTailscale []byte
|
|
|
|
|
|
|
|
// localAPI is a minimal fake tailscaled LocalAPI server that presents
|
|
|
|
// just enough functionality for containerboot to function
|
|
|
|
// correctly. In practice this means it only supports querying
|
|
|
|
// tailscaled status, and panics on all other uses to make it very
|
|
|
|
// obvious that something unexpected happened.
|
|
|
|
type localAPI struct {
|
|
|
|
FSRoot string
|
|
|
|
Path string // populated by Start
|
|
|
|
|
|
|
|
srv *http.Server
|
|
|
|
|
|
|
|
sync.Mutex
|
2022-12-07 20:29:45 +00:00
|
|
|
cond *sync.Cond
|
|
|
|
notify *ipn.Notify
|
2022-11-09 01:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *localAPI) Start() error {
|
|
|
|
path := filepath.Join(l.FSRoot, "tmp/tailscaled.sock.fake")
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ln, err := net.Listen("unix", path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
l.srv = &http.Server{
|
|
|
|
Handler: l,
|
|
|
|
}
|
|
|
|
l.Path = path
|
2022-12-07 20:29:45 +00:00
|
|
|
l.cond = sync.NewCond(&l.Mutex)
|
2022-11-09 01:29:35 +00:00
|
|
|
go l.srv.Serve(ln)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *localAPI) Close() {
|
|
|
|
l.srv.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l *localAPI) Reset() {
|
2022-12-07 20:29:45 +00:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
l.notify = nil
|
|
|
|
l.cond.Broadcast()
|
2022-11-09 01:29:35 +00:00
|
|
|
}
|
|
|
|
|
2022-12-07 20:29:45 +00:00
|
|
|
func (l *localAPI) Notify(n *ipn.Notify) {
|
|
|
|
if n == nil {
|
|
|
|
return
|
|
|
|
}
|
2022-11-09 01:29:35 +00:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
2022-12-07 20:29:45 +00:00
|
|
|
l.notify = n
|
|
|
|
l.cond.Broadcast()
|
2022-11-09 01:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *localAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2023-08-30 00:24:25 +00:00
|
|
|
switch r.URL.Path {
|
|
|
|
case "/localapi/v0/serve-config":
|
|
|
|
if r.Method != "POST" {
|
|
|
|
panic(fmt.Sprintf("unsupported method %q", r.Method))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
case "/localapi/v0/watch-ipn-bus":
|
|
|
|
if r.Method != "GET" {
|
|
|
|
panic(fmt.Sprintf("unsupported method %q", r.Method))
|
|
|
|
}
|
|
|
|
default:
|
2022-12-07 20:29:45 +00:00
|
|
|
panic(fmt.Sprintf("unsupported path %q", r.URL.Path))
|
2022-11-09 01:29:35 +00:00
|
|
|
}
|
2022-12-07 20:29:45 +00:00
|
|
|
|
2022-11-09 01:29:35 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2022-12-07 20:29:45 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
if f, ok := w.(http.Flusher); ok {
|
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
enc := json.NewEncoder(w)
|
2022-11-09 01:29:35 +00:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
2022-12-07 20:29:45 +00:00
|
|
|
for {
|
|
|
|
if l.notify != nil {
|
|
|
|
if err := enc.Encode(l.notify); err != nil {
|
|
|
|
// Usually broken pipe as the test client disconnects.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if f, ok := w.(http.Flusher); ok {
|
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
l.cond.Wait()
|
2022-11-09 01:29:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// kubeServer is a minimal fake Kubernetes server that presents just
|
|
|
|
// enough functionality for containerboot to function correctly. In
|
|
|
|
// practice this means it only supports reading and modifying a single
|
|
|
|
// kube secret, and panics on all other uses to make it very obvious
|
|
|
|
// that something unexpected happened.
|
|
|
|
type kubeServer struct {
|
2022-11-10 06:01:34 +00:00
|
|
|
FSRoot string
|
|
|
|
Host, Port string // populated by Start
|
2022-11-09 01:29:35 +00:00
|
|
|
|
|
|
|
srv *httptest.Server
|
|
|
|
|
|
|
|
sync.Mutex
|
2022-12-07 01:03:53 +00:00
|
|
|
secret map[string]string
|
|
|
|
canPatch bool
|
2022-11-09 01:29:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (k *kubeServer) Secret() map[string]string {
|
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
ret := map[string]string{}
|
|
|
|
for k, v := range k.secret {
|
|
|
|
ret[k] = v
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *kubeServer) SetSecret(key, val string) {
|
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
k.secret[key] = val
|
|
|
|
}
|
|
|
|
|
2022-12-07 01:03:53 +00:00
|
|
|
func (k *kubeServer) SetPatching(canPatch bool) {
|
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
k.canPatch = canPatch
|
|
|
|
}
|
|
|
|
|
2022-11-09 01:29:35 +00:00
|
|
|
func (k *kubeServer) Reset() {
|
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
k.secret = map[string]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *kubeServer) Start() error {
|
|
|
|
root := filepath.Join(k.FSRoot, "var/run/secrets/kubernetes.io/serviceaccount")
|
|
|
|
|
|
|
|
if err := os.MkdirAll(root, 0700); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := os.WriteFile(filepath.Join(root, "namespace"), []byte("default"), 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.WriteFile(filepath.Join(root, "token"), []byte("bearer_token"), 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
k.srv = httptest.NewTLSServer(k)
|
2022-11-10 06:01:34 +00:00
|
|
|
k.Host = k.srv.Listener.Addr().(*net.TCPAddr).IP.String()
|
|
|
|
k.Port = strconv.Itoa(k.srv.Listener.Addr().(*net.TCPAddr).Port)
|
2022-11-09 01:29:35 +00:00
|
|
|
|
|
|
|
var cert bytes.Buffer
|
|
|
|
if err := pem.Encode(&cert, &pem.Block{Type: "CERTIFICATE", Bytes: k.srv.Certificate().Raw}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := os.WriteFile(filepath.Join(root, "ca.crt"), cert.Bytes(), 0600); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *kubeServer) Close() {
|
|
|
|
k.srv.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *kubeServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Header.Get("Authorization") != "Bearer bearer_token" {
|
|
|
|
panic("client didn't provide bearer token in request")
|
|
|
|
}
|
2022-12-07 01:03:53 +00:00
|
|
|
switch r.URL.Path {
|
|
|
|
case "/api/v1/namespaces/default/secrets/tailscale":
|
|
|
|
k.serveSecret(w, r)
|
|
|
|
case "/apis/authorization.k8s.io/v1/selfsubjectaccessreviews":
|
|
|
|
k.serveSSAR(w, r)
|
|
|
|
default:
|
2022-11-09 01:29:35 +00:00
|
|
|
panic(fmt.Sprintf("unhandled fake kube api path %q", r.URL.Path))
|
|
|
|
}
|
2022-12-07 01:03:53 +00:00
|
|
|
}
|
2022-11-09 01:29:35 +00:00
|
|
|
|
2022-12-07 01:03:53 +00:00
|
|
|
func (k *kubeServer) serveSSAR(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var req struct {
|
|
|
|
Spec struct {
|
|
|
|
ResourceAttributes struct {
|
|
|
|
Verb string `json:"verb"`
|
|
|
|
} `json:"resourceAttributes"`
|
|
|
|
} `json:"spec"`
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
|
|
panic(fmt.Sprintf("decoding SSAR request: %v", err))
|
|
|
|
}
|
|
|
|
ok := true
|
|
|
|
if req.Spec.ResourceAttributes.Verb == "patch" {
|
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
ok = k.canPatch
|
|
|
|
}
|
|
|
|
// Just say yes to all SARs, we don't enforce RBAC.
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
fmt.Fprintf(w, `{"status":{"allowed":%v}}`, ok)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (k *kubeServer) serveSecret(w http.ResponseWriter, r *http.Request) {
|
2022-11-09 01:29:35 +00:00
|
|
|
bs, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, fmt.Sprintf("reading request body: %v", err), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
ret := map[string]map[string]string{
|
2022-12-05 23:38:36 +00:00
|
|
|
"data": {},
|
2022-11-09 01:29:35 +00:00
|
|
|
}
|
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
for k, v := range k.secret {
|
|
|
|
v := base64.StdEncoding.EncodeToString([]byte(v))
|
|
|
|
ret["data"][k] = v
|
|
|
|
}
|
|
|
|
if err := json.NewEncoder(w).Encode(ret); err != nil {
|
|
|
|
panic("encode failed")
|
|
|
|
}
|
|
|
|
case "PATCH":
|
2022-12-07 01:03:53 +00:00
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
if !k.canPatch {
|
|
|
|
panic("containerboot tried to patch despite not being allowed")
|
|
|
|
}
|
2022-11-09 01:29:35 +00:00
|
|
|
switch r.Header.Get("Content-Type") {
|
|
|
|
case "application/json-patch+json":
|
|
|
|
req := []struct {
|
|
|
|
Op string `json:"op"`
|
|
|
|
Path string `json:"path"`
|
|
|
|
}{}
|
|
|
|
if err := json.Unmarshal(bs, &req); err != nil {
|
|
|
|
panic(fmt.Sprintf("json decode failed: %v. Body:\n\n%s", err, string(bs)))
|
|
|
|
}
|
|
|
|
for _, op := range req {
|
|
|
|
if op.Op != "remove" {
|
|
|
|
panic(fmt.Sprintf("unsupported json-patch op %q", op.Op))
|
|
|
|
}
|
|
|
|
if !strings.HasPrefix(op.Path, "/data/") {
|
|
|
|
panic(fmt.Sprintf("unsupported json-patch path %q", op.Path))
|
|
|
|
}
|
|
|
|
delete(k.secret, strings.TrimPrefix(op.Path, "/data/"))
|
|
|
|
}
|
|
|
|
case "application/strategic-merge-patch+json":
|
|
|
|
req := struct {
|
2023-03-02 16:10:36 +00:00
|
|
|
Data map[string][]byte `json:"data"`
|
2022-11-09 01:29:35 +00:00
|
|
|
}{}
|
|
|
|
if err := json.Unmarshal(bs, &req); err != nil {
|
|
|
|
panic(fmt.Sprintf("json decode failed: %v. Body:\n\n%s", err, string(bs)))
|
|
|
|
}
|
|
|
|
for key, val := range req.Data {
|
2023-03-02 16:10:36 +00:00
|
|
|
k.secret[key] = string(val)
|
2022-11-09 01:29:35 +00:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unknown content type %q", r.Header.Get("Content-Type")))
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unhandled HTTP method %q", r.Method))
|
|
|
|
}
|
|
|
|
}
|