2022-10-13 14:00:08 +00:00
|
|
|
package tsic
|
|
|
|
|
|
|
|
import (
|
2022-10-18 09:58:49 +00:00
|
|
|
"encoding/json"
|
2022-10-13 14:00:08 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/netip"
|
2022-11-03 15:56:19 +00:00
|
|
|
"net/url"
|
2022-10-13 14:00:08 +00:00
|
|
|
"strings"
|
|
|
|
|
2022-10-18 09:58:49 +00:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2022-10-13 14:00:08 +00:00
|
|
|
"github.com/juanfont/headscale"
|
|
|
|
"github.com/juanfont/headscale/integration/dockertestutil"
|
2022-11-06 19:22:21 +00:00
|
|
|
"github.com/juanfont/headscale/integration/integrationutil"
|
2022-10-13 14:00:08 +00:00
|
|
|
"github.com/ory/dockertest/v3"
|
|
|
|
"github.com/ory/dockertest/v3/docker"
|
2022-10-18 09:58:49 +00:00
|
|
|
"tailscale.com/ipn/ipnstate"
|
2022-10-13 14:00:08 +00:00
|
|
|
)
|
|
|
|
|
2022-10-18 10:09:10 +00:00
|
|
|
const (
|
|
|
|
tsicHashLength = 6
|
|
|
|
dockerContextPath = "../."
|
2022-11-06 19:22:21 +00:00
|
|
|
headscaleCertPath = "/usr/local/share/ca-certificates/headscale.crt"
|
2022-10-18 10:09:10 +00:00
|
|
|
)
|
2022-10-13 14:00:08 +00:00
|
|
|
|
2022-10-18 10:09:10 +00:00
|
|
|
var (
|
2022-11-03 15:56:19 +00:00
|
|
|
errTailscalePingFailed = errors.New("ping failed")
|
|
|
|
errTailscaleNotLoggedIn = errors.New("tailscale not logged in")
|
|
|
|
errTailscaleWrongPeerCount = errors.New("wrong peer count")
|
|
|
|
errTailscaleCannotUpWithoutAuthkey = errors.New("cannot up without authkey")
|
|
|
|
errTailscaleNotConnected = errors.New("tailscale not connected")
|
2022-10-18 10:09:10 +00:00
|
|
|
)
|
2022-10-13 14:00:08 +00:00
|
|
|
|
|
|
|
type TailscaleInContainer struct {
|
|
|
|
version string
|
2022-10-21 11:17:54 +00:00
|
|
|
hostname string
|
2022-10-13 14:00:08 +00:00
|
|
|
|
|
|
|
pool *dockertest.Pool
|
|
|
|
container *dockertest.Resource
|
|
|
|
network *dockertest.Network
|
2022-10-23 12:13:22 +00:00
|
|
|
|
|
|
|
// "cache"
|
|
|
|
ips []netip.Addr
|
|
|
|
fqdn string
|
2022-11-06 19:22:21 +00:00
|
|
|
|
|
|
|
// optional config
|
|
|
|
headscaleCert []byte
|
|
|
|
headscaleHostname string
|
|
|
|
}
|
|
|
|
|
|
|
|
type Option = func(c *TailscaleInContainer)
|
|
|
|
|
|
|
|
func WithHeadscaleTLS(cert []byte) Option {
|
|
|
|
return func(tsic *TailscaleInContainer) {
|
|
|
|
tsic.headscaleCert = cert
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithOrCreateNetwork(network *dockertest.Network) Option {
|
|
|
|
return func(tsic *TailscaleInContainer) {
|
|
|
|
if network != nil {
|
|
|
|
tsic.network = network
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
network, err := dockertestutil.GetFirstOrCreateNetwork(
|
|
|
|
tsic.pool,
|
|
|
|
fmt.Sprintf("%s-network", tsic.hostname),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to create network: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tsic.network = network
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithHeadscaleName(hsName string) Option {
|
|
|
|
return func(tsic *TailscaleInContainer) {
|
|
|
|
tsic.headscaleHostname = hsName
|
|
|
|
}
|
2022-10-13 14:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(
|
|
|
|
pool *dockertest.Pool,
|
|
|
|
version string,
|
2022-10-18 10:09:10 +00:00
|
|
|
network *dockertest.Network,
|
2022-11-06 19:22:21 +00:00
|
|
|
opts ...Option,
|
2022-10-18 10:09:10 +00:00
|
|
|
) (*TailscaleInContainer, error) {
|
2022-10-13 14:00:08 +00:00
|
|
|
hash, err := headscale.GenerateRandomStringDNSSafe(tsicHashLength)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-21 12:07:46 +00:00
|
|
|
hostname := fmt.Sprintf("ts-%s-%s", strings.ReplaceAll(version, ".", "-"), hash)
|
2022-10-13 14:00:08 +00:00
|
|
|
|
2022-11-06 19:22:21 +00:00
|
|
|
tsic := &TailscaleInContainer{
|
|
|
|
version: version,
|
|
|
|
hostname: hostname,
|
|
|
|
|
|
|
|
pool: pool,
|
|
|
|
network: network,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(tsic)
|
|
|
|
}
|
2022-10-13 14:00:08 +00:00
|
|
|
|
|
|
|
tailscaleOptions := &dockertest.RunOptions{
|
|
|
|
Name: hostname,
|
|
|
|
Networks: []*dockertest.Network{network},
|
2022-11-06 19:22:21 +00:00
|
|
|
// Cmd: []string{
|
|
|
|
// "tailscaled", "--tun=tsdev",
|
|
|
|
// },
|
|
|
|
Entrypoint: []string{
|
|
|
|
"/bin/bash",
|
|
|
|
"-c",
|
|
|
|
"/bin/sleep 3 ; update-ca-certificates ; tailscaled --tun=tsdev",
|
2022-10-13 14:00:08 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-06 19:22:21 +00:00
|
|
|
if tsic.headscaleHostname != "" {
|
|
|
|
tailscaleOptions.ExtraHosts = []string{
|
|
|
|
"host.docker.internal:host-gateway",
|
|
|
|
fmt.Sprintf("%s:host-gateway", tsic.headscaleHostname),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 14:00:08 +00:00
|
|
|
// dockertest isnt very good at handling containers that has already
|
|
|
|
// been created, this is an attempt to make sure this container isnt
|
|
|
|
// present.
|
|
|
|
err = pool.RemoveContainerByName(hostname)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
container, err := pool.BuildAndRunWithBuildOptions(
|
|
|
|
createTailscaleBuildOptions(version),
|
|
|
|
tailscaleOptions,
|
|
|
|
dockertestutil.DockerRestartPolicy,
|
|
|
|
dockertestutil.DockerAllowLocalIPv6,
|
|
|
|
dockertestutil.DockerAllowNetworkAdministration,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not start tailscale container: %w", err)
|
|
|
|
}
|
|
|
|
log.Printf("Created %s container\n", hostname)
|
|
|
|
|
2022-11-06 19:22:21 +00:00
|
|
|
tsic.container = container
|
|
|
|
|
|
|
|
if tsic.hasTLS() {
|
|
|
|
err = tsic.WriteFile(headscaleCertPath, tsic.headscaleCert)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to write TLS certificate to container: %w", err)
|
|
|
|
}
|
|
|
|
}
|
2022-10-13 14:00:08 +00:00
|
|
|
|
2022-11-06 19:22:21 +00:00
|
|
|
return tsic, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TailscaleInContainer) hasTLS() bool {
|
|
|
|
return len(t.headscaleCert) != 0
|
2022-10-13 14:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TailscaleInContainer) Shutdown() error {
|
|
|
|
return t.pool.Purge(t.container)
|
|
|
|
}
|
|
|
|
|
2022-10-21 11:17:54 +00:00
|
|
|
func (t *TailscaleInContainer) Hostname() string {
|
|
|
|
return t.hostname
|
|
|
|
}
|
|
|
|
|
2022-10-18 09:58:49 +00:00
|
|
|
func (t *TailscaleInContainer) Version() string {
|
|
|
|
return t.version
|
|
|
|
}
|
|
|
|
|
2022-10-23 12:13:22 +00:00
|
|
|
func (t *TailscaleInContainer) Execute(
|
|
|
|
command []string,
|
2022-11-03 15:50:20 +00:00
|
|
|
) (string, string, error) {
|
2022-10-23 12:13:22 +00:00
|
|
|
log.Println("command", command)
|
|
|
|
log.Printf("running command for %s\n", t.hostname)
|
|
|
|
stdout, stderr, err := dockertestutil.ExecuteCommand(
|
|
|
|
t.container,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("command stderr: %s\n", stderr)
|
|
|
|
|
2022-10-25 07:24:05 +00:00
|
|
|
if stdout != "" {
|
|
|
|
log.Printf("command stdout: %s\n", stdout)
|
|
|
|
}
|
|
|
|
|
2022-10-23 12:13:22 +00:00
|
|
|
if strings.Contains(stderr, "NeedsLogin") {
|
2022-11-03 15:50:20 +00:00
|
|
|
return stdout, stderr, errTailscaleNotLoggedIn
|
2022-10-23 12:13:22 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 15:50:20 +00:00
|
|
|
return stdout, stderr, err
|
2022-10-23 12:13:22 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 15:50:20 +00:00
|
|
|
return stdout, stderr, nil
|
2022-10-23 12:13:22 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 14:00:08 +00:00
|
|
|
func (t *TailscaleInContainer) Up(
|
|
|
|
loginServer, authKey string,
|
|
|
|
) error {
|
|
|
|
command := []string{
|
|
|
|
"tailscale",
|
|
|
|
"up",
|
|
|
|
"-login-server",
|
|
|
|
loginServer,
|
|
|
|
"--authkey",
|
|
|
|
authKey,
|
|
|
|
"--hostname",
|
2022-10-21 11:17:54 +00:00
|
|
|
t.hostname,
|
2022-10-13 14:00:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 15:50:20 +00:00
|
|
|
if _, _, err := t.Execute(command); err != nil {
|
2022-10-23 12:13:22 +00:00
|
|
|
return fmt.Errorf("failed to join tailscale client: %w", err)
|
2022-10-18 09:58:49 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 14:00:08 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-03 15:56:19 +00:00
|
|
|
func (t *TailscaleInContainer) UpWithLoginURL(
|
|
|
|
loginServer string,
|
|
|
|
) (*url.URL, error) {
|
|
|
|
command := []string{
|
|
|
|
"tailscale",
|
|
|
|
"up",
|
|
|
|
"-login-server",
|
|
|
|
loginServer,
|
|
|
|
"--hostname",
|
|
|
|
t.hostname,
|
|
|
|
}
|
|
|
|
|
|
|
|
_, stderr, err := t.Execute(command)
|
2022-11-13 20:25:19 +00:00
|
|
|
if errors.Is(err, errTailscaleNotLoggedIn) {
|
2022-11-03 15:56:19 +00:00
|
|
|
return nil, errTailscaleCannotUpWithoutAuthkey
|
|
|
|
}
|
|
|
|
|
|
|
|
urlStr := strings.ReplaceAll(stderr, "\nTo authenticate, visit:\n\n\t", "")
|
|
|
|
urlStr = strings.TrimSpace(urlStr)
|
|
|
|
|
|
|
|
// parse URL
|
2022-11-13 20:25:19 +00:00
|
|
|
loginURL, err := url.Parse(urlStr)
|
2022-11-03 15:56:19 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Could not parse login URL: %s", err)
|
|
|
|
log.Printf("Original join command result: %s", stderr)
|
2022-11-13 20:25:19 +00:00
|
|
|
|
2022-11-03 15:56:19 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-13 20:25:19 +00:00
|
|
|
return loginURL, nil
|
2022-11-03 15:56:19 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 14:00:08 +00:00
|
|
|
func (t *TailscaleInContainer) IPs() ([]netip.Addr, error) {
|
2022-10-23 12:13:22 +00:00
|
|
|
if t.ips != nil && len(t.ips) != 0 {
|
|
|
|
return t.ips, nil
|
|
|
|
}
|
|
|
|
|
2022-10-13 14:00:08 +00:00
|
|
|
ips := make([]netip.Addr, 0)
|
|
|
|
|
|
|
|
command := []string{
|
|
|
|
"tailscale",
|
|
|
|
"ip",
|
|
|
|
}
|
|
|
|
|
2022-11-03 15:50:20 +00:00
|
|
|
result, _, err := t.Execute(command)
|
2022-10-13 14:00:08 +00:00
|
|
|
if err != nil {
|
2022-10-23 12:13:22 +00:00
|
|
|
return []netip.Addr{}, fmt.Errorf("failed to join tailscale client: %w", err)
|
2022-10-13 14:00:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, address := range strings.Split(result, "\n") {
|
|
|
|
address = strings.TrimSuffix(address, "\n")
|
|
|
|
if len(address) < 1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ip, err := netip.ParseAddr(address)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ips = append(ips, ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ips, nil
|
|
|
|
}
|
|
|
|
|
2022-10-18 09:58:49 +00:00
|
|
|
func (t *TailscaleInContainer) Status() (*ipnstate.Status, error) {
|
2022-10-13 14:00:08 +00:00
|
|
|
command := []string{
|
2022-10-18 09:58:49 +00:00
|
|
|
"tailscale",
|
|
|
|
"status",
|
|
|
|
"--json",
|
2022-10-13 14:00:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 15:50:20 +00:00
|
|
|
result, _, err := t.Execute(command)
|
2022-10-13 14:00:08 +00:00
|
|
|
if err != nil {
|
2022-10-18 09:58:49 +00:00
|
|
|
return nil, fmt.Errorf("failed to execute tailscale status command: %w", err)
|
2022-10-13 14:00:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 09:58:49 +00:00
|
|
|
var status ipnstate.Status
|
|
|
|
err = json.Unmarshal([]byte(result), &status)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal tailscale status: %w", err)
|
2022-10-13 14:00:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-18 09:58:49 +00:00
|
|
|
return &status, err
|
|
|
|
}
|
|
|
|
|
2022-10-21 15:44:40 +00:00
|
|
|
func (t *TailscaleInContainer) FQDN() (string, error) {
|
2022-10-23 12:13:22 +00:00
|
|
|
if t.fqdn != "" {
|
|
|
|
return t.fqdn, nil
|
|
|
|
}
|
|
|
|
|
2022-10-21 15:44:40 +00:00
|
|
|
status, err := t.Status()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to get FQDN: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return status.Self.DNSName, nil
|
|
|
|
}
|
|
|
|
|
2022-11-13 12:06:53 +00:00
|
|
|
func (t *TailscaleInContainer) WaitForReady() error {
|
|
|
|
return t.pool.Retry(func() error {
|
|
|
|
status, err := t.Status()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to fetch tailscale status: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if status.CurrentTailnet != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errTailscaleNotConnected
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-18 09:58:49 +00:00
|
|
|
func (t *TailscaleInContainer) WaitForPeers(expected int) error {
|
|
|
|
return t.pool.Retry(func() error {
|
|
|
|
status, err := t.Status()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to fetch tailscale status: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if peers := status.Peers(); len(peers) != expected {
|
2022-10-18 10:19:43 +00:00
|
|
|
return errTailscaleWrongPeerCount
|
2022-10-18 09:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-18 10:09:10 +00:00
|
|
|
// TODO(kradalby): Make multiping, go routine magic.
|
2022-10-21 12:07:46 +00:00
|
|
|
func (t *TailscaleInContainer) Ping(hostnameOrIP string) error {
|
2022-10-18 09:58:49 +00:00
|
|
|
return t.pool.Retry(func() error {
|
|
|
|
command := []string{
|
|
|
|
"tailscale", "ping",
|
|
|
|
"--timeout=1s",
|
|
|
|
"--c=10",
|
|
|
|
"--until-direct=true",
|
2022-10-21 12:07:46 +00:00
|
|
|
hostnameOrIP,
|
2022-10-18 09:58:49 +00:00
|
|
|
}
|
|
|
|
|
2022-11-03 15:50:20 +00:00
|
|
|
result, _, err := t.Execute(command)
|
2022-10-18 09:58:49 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf(
|
|
|
|
"failed to run ping command from %s to %s, err: %s",
|
2022-10-21 12:07:46 +00:00
|
|
|
t.Hostname(),
|
|
|
|
hostnameOrIP,
|
2022-10-18 09:58:49 +00:00
|
|
|
err,
|
|
|
|
)
|
2022-10-18 10:19:43 +00:00
|
|
|
|
2022-10-18 09:58:49 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(result, "pong") && !strings.Contains(result, "is local") {
|
|
|
|
return backoff.Permanent(errTailscalePingFailed)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2022-10-13 14:00:08 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 19:22:21 +00:00
|
|
|
func (t *TailscaleInContainer) WriteFile(path string, data []byte) error {
|
|
|
|
return integrationutil.WriteFileToContainer(t.pool, t.container, path, data)
|
|
|
|
}
|
|
|
|
|
2022-10-13 14:00:08 +00:00
|
|
|
func createTailscaleBuildOptions(version string) *dockertest.BuildOptions {
|
|
|
|
var tailscaleBuildOptions *dockertest.BuildOptions
|
|
|
|
switch version {
|
|
|
|
case "head":
|
|
|
|
tailscaleBuildOptions = &dockertest.BuildOptions{
|
|
|
|
Dockerfile: "Dockerfile.tailscale-HEAD",
|
|
|
|
ContextDir: dockerContextPath,
|
|
|
|
BuildArgs: []docker.BuildArg{},
|
|
|
|
}
|
|
|
|
case "unstable":
|
|
|
|
tailscaleBuildOptions = &dockertest.BuildOptions{
|
|
|
|
Dockerfile: "Dockerfile.tailscale",
|
|
|
|
ContextDir: dockerContextPath,
|
|
|
|
BuildArgs: []docker.BuildArg{
|
|
|
|
{
|
|
|
|
Name: "TAILSCALE_VERSION",
|
|
|
|
Value: "*", // Installs the latest version https://askubuntu.com/a/824926
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "TAILSCALE_CHANNEL",
|
|
|
|
Value: "unstable",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
tailscaleBuildOptions = &dockertest.BuildOptions{
|
|
|
|
Dockerfile: "Dockerfile.tailscale",
|
|
|
|
ContextDir: dockerContextPath,
|
|
|
|
BuildArgs: []docker.BuildArg{
|
|
|
|
{
|
|
|
|
Name: "TAILSCALE_VERSION",
|
|
|
|
Value: version,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "TAILSCALE_CHANNEL",
|
|
|
|
Value: "stable",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tailscaleBuildOptions
|
|
|
|
}
|