2022-10-13 10:00:08 -04:00
|
|
|
package tsic
|
|
|
|
|
|
|
|
import (
|
2022-10-18 05:58:49 -04:00
|
|
|
"encoding/json"
|
2022-10-13 10:00:08 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net/netip"
|
2022-11-03 11:56:19 -04:00
|
|
|
"net/url"
|
2022-10-13 10:00:08 -04:00
|
|
|
"strings"
|
|
|
|
|
2022-10-18 05:58:49 -04:00
|
|
|
"github.com/cenkalti/backoff/v4"
|
2022-10-13 10:00:08 -04:00
|
|
|
"github.com/juanfont/headscale"
|
|
|
|
"github.com/juanfont/headscale/integration/dockertestutil"
|
2022-11-06 14:22:21 -05:00
|
|
|
"github.com/juanfont/headscale/integration/integrationutil"
|
2022-10-13 10:00:08 -04:00
|
|
|
"github.com/ory/dockertest/v3"
|
|
|
|
"github.com/ory/dockertest/v3/docker"
|
2022-10-18 05:58:49 -04:00
|
|
|
"tailscale.com/ipn/ipnstate"
|
2022-10-13 10:00:08 -04:00
|
|
|
)
|
|
|
|
|
2022-10-18 06:09:10 -04:00
|
|
|
const (
|
|
|
|
tsicHashLength = 6
|
|
|
|
dockerContextPath = "../."
|
2022-11-06 14:22:21 -05:00
|
|
|
headscaleCertPath = "/usr/local/share/ca-certificates/headscale.crt"
|
2022-10-18 06:09:10 -04:00
|
|
|
)
|
2022-10-13 10:00:08 -04:00
|
|
|
|
2022-10-18 06:09:10 -04:00
|
|
|
var (
|
2022-11-03 11:56:19 -04: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 06:09:10 -04:00
|
|
|
)
|
2022-10-13 10:00:08 -04:00
|
|
|
|
|
|
|
type TailscaleInContainer struct {
|
|
|
|
version string
|
2022-10-21 07:17:54 -04:00
|
|
|
hostname string
|
2022-10-13 10:00:08 -04:00
|
|
|
|
|
|
|
pool *dockertest.Pool
|
|
|
|
container *dockertest.Resource
|
|
|
|
network *dockertest.Network
|
2022-10-23 08:13:22 -04:00
|
|
|
|
|
|
|
// "cache"
|
|
|
|
ips []netip.Addr
|
|
|
|
fqdn string
|
2022-11-06 14:22:21 -05: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 10:00:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func New(
|
|
|
|
pool *dockertest.Pool,
|
|
|
|
version string,
|
2022-10-18 06:09:10 -04:00
|
|
|
network *dockertest.Network,
|
2022-11-06 14:22:21 -05:00
|
|
|
opts ...Option,
|
2022-10-18 06:09:10 -04:00
|
|
|
) (*TailscaleInContainer, error) {
|
2022-10-13 10:00:08 -04:00
|
|
|
hash, err := headscale.GenerateRandomStringDNSSafe(tsicHashLength)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-10-21 08:07:46 -04:00
|
|
|
hostname := fmt.Sprintf("ts-%s-%s", strings.ReplaceAll(version, ".", "-"), hash)
|
2022-10-13 10:00:08 -04:00
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
tsic := &TailscaleInContainer{
|
|
|
|
version: version,
|
|
|
|
hostname: hostname,
|
|
|
|
|
|
|
|
pool: pool,
|
|
|
|
network: network,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(tsic)
|
|
|
|
}
|
2022-10-13 10:00:08 -04:00
|
|
|
|
|
|
|
tailscaleOptions := &dockertest.RunOptions{
|
|
|
|
Name: hostname,
|
|
|
|
Networks: []*dockertest.Network{network},
|
2022-11-06 14:22:21 -05:00
|
|
|
// Cmd: []string{
|
|
|
|
// "tailscaled", "--tun=tsdev",
|
|
|
|
// },
|
|
|
|
Entrypoint: []string{
|
|
|
|
"/bin/bash",
|
|
|
|
"-c",
|
|
|
|
"/bin/sleep 3 ; update-ca-certificates ; tailscaled --tun=tsdev",
|
2022-10-13 10:00:08 -04:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
if tsic.headscaleHostname != "" {
|
|
|
|
tailscaleOptions.ExtraHosts = []string{
|
|
|
|
"host.docker.internal:host-gateway",
|
|
|
|
fmt.Sprintf("%s:host-gateway", tsic.headscaleHostname),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:00:08 -04: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 14:22:21 -05: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 10:00:08 -04:00
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
return tsic, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TailscaleInContainer) hasTLS() bool {
|
|
|
|
return len(t.headscaleCert) != 0
|
2022-10-13 10:00:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TailscaleInContainer) Shutdown() error {
|
|
|
|
return t.pool.Purge(t.container)
|
|
|
|
}
|
|
|
|
|
2022-10-21 07:17:54 -04:00
|
|
|
func (t *TailscaleInContainer) Hostname() string {
|
|
|
|
return t.hostname
|
|
|
|
}
|
|
|
|
|
2022-10-18 05:58:49 -04:00
|
|
|
func (t *TailscaleInContainer) Version() string {
|
|
|
|
return t.version
|
|
|
|
}
|
|
|
|
|
2022-11-08 10:09:52 -05:00
|
|
|
func (t *TailscaleInContainer) ID() string {
|
|
|
|
return t.container.Container.ID
|
|
|
|
}
|
|
|
|
|
2022-10-23 08:13:22 -04:00
|
|
|
func (t *TailscaleInContainer) Execute(
|
|
|
|
command []string,
|
2022-11-03 11:50:20 -04:00
|
|
|
) (string, string, error) {
|
2022-10-23 08:13:22 -04:00
|
|
|
stdout, stderr, err := dockertestutil.ExecuteCommand(
|
|
|
|
t.container,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("command stderr: %s\n", stderr)
|
|
|
|
|
2022-10-25 03:24:05 -04:00
|
|
|
if stdout != "" {
|
|
|
|
log.Printf("command stdout: %s\n", stdout)
|
|
|
|
}
|
|
|
|
|
2022-10-23 08:13:22 -04:00
|
|
|
if strings.Contains(stderr, "NeedsLogin") {
|
2022-11-03 11:50:20 -04:00
|
|
|
return stdout, stderr, errTailscaleNotLoggedIn
|
2022-10-23 08:13:22 -04:00
|
|
|
}
|
|
|
|
|
2022-11-03 11:50:20 -04:00
|
|
|
return stdout, stderr, err
|
2022-10-23 08:13:22 -04:00
|
|
|
}
|
|
|
|
|
2022-11-03 11:50:20 -04:00
|
|
|
return stdout, stderr, nil
|
2022-10-23 08:13:22 -04:00
|
|
|
}
|
|
|
|
|
2022-10-13 10:00:08 -04:00
|
|
|
func (t *TailscaleInContainer) Up(
|
|
|
|
loginServer, authKey string,
|
|
|
|
) error {
|
|
|
|
command := []string{
|
|
|
|
"tailscale",
|
|
|
|
"up",
|
|
|
|
"-login-server",
|
|
|
|
loginServer,
|
|
|
|
"--authkey",
|
|
|
|
authKey,
|
|
|
|
"--hostname",
|
2022-10-21 07:17:54 -04:00
|
|
|
t.hostname,
|
2022-10-13 10:00:08 -04:00
|
|
|
}
|
|
|
|
|
2022-11-03 11:50:20 -04:00
|
|
|
if _, _, err := t.Execute(command); err != nil {
|
2022-10-23 08:13:22 -04:00
|
|
|
return fmt.Errorf("failed to join tailscale client: %w", err)
|
2022-10-18 05:58:49 -04:00
|
|
|
}
|
|
|
|
|
2022-10-13 10:00:08 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-03 11:56:19 -04: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 15:25:19 -05:00
|
|
|
if errors.Is(err, errTailscaleNotLoggedIn) {
|
2022-11-03 11:56:19 -04:00
|
|
|
return nil, errTailscaleCannotUpWithoutAuthkey
|
|
|
|
}
|
|
|
|
|
|
|
|
urlStr := strings.ReplaceAll(stderr, "\nTo authenticate, visit:\n\n\t", "")
|
|
|
|
urlStr = strings.TrimSpace(urlStr)
|
|
|
|
|
|
|
|
// parse URL
|
2022-11-13 15:25:19 -05:00
|
|
|
loginURL, err := url.Parse(urlStr)
|
2022-11-03 11:56:19 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Could not parse login URL: %s", err)
|
|
|
|
log.Printf("Original join command result: %s", stderr)
|
2022-11-13 15:25:19 -05:00
|
|
|
|
2022-11-03 11:56:19 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-13 15:25:19 -05:00
|
|
|
return loginURL, nil
|
2022-11-03 11:56:19 -04:00
|
|
|
}
|
|
|
|
|
2022-10-13 10:00:08 -04:00
|
|
|
func (t *TailscaleInContainer) IPs() ([]netip.Addr, error) {
|
2022-10-23 08:13:22 -04:00
|
|
|
if t.ips != nil && len(t.ips) != 0 {
|
|
|
|
return t.ips, nil
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:00:08 -04:00
|
|
|
ips := make([]netip.Addr, 0)
|
|
|
|
|
|
|
|
command := []string{
|
|
|
|
"tailscale",
|
|
|
|
"ip",
|
|
|
|
}
|
|
|
|
|
2022-11-03 11:50:20 -04:00
|
|
|
result, _, err := t.Execute(command)
|
2022-10-13 10:00:08 -04:00
|
|
|
if err != nil {
|
2022-10-23 08:13:22 -04:00
|
|
|
return []netip.Addr{}, fmt.Errorf("failed to join tailscale client: %w", err)
|
2022-10-13 10:00:08 -04: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 05:58:49 -04:00
|
|
|
func (t *TailscaleInContainer) Status() (*ipnstate.Status, error) {
|
2022-10-13 10:00:08 -04:00
|
|
|
command := []string{
|
2022-10-18 05:58:49 -04:00
|
|
|
"tailscale",
|
|
|
|
"status",
|
|
|
|
"--json",
|
2022-10-13 10:00:08 -04:00
|
|
|
}
|
|
|
|
|
2022-11-03 11:50:20 -04:00
|
|
|
result, _, err := t.Execute(command)
|
2022-10-13 10:00:08 -04:00
|
|
|
if err != nil {
|
2022-10-18 05:58:49 -04:00
|
|
|
return nil, fmt.Errorf("failed to execute tailscale status command: %w", err)
|
2022-10-13 10:00:08 -04:00
|
|
|
}
|
|
|
|
|
2022-10-18 05:58:49 -04: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 10:00:08 -04:00
|
|
|
}
|
|
|
|
|
2022-10-18 05:58:49 -04:00
|
|
|
return &status, err
|
|
|
|
}
|
|
|
|
|
2022-10-21 11:44:40 -04:00
|
|
|
func (t *TailscaleInContainer) FQDN() (string, error) {
|
2022-10-23 08:13:22 -04:00
|
|
|
if t.fqdn != "" {
|
|
|
|
return t.fqdn, nil
|
|
|
|
}
|
|
|
|
|
2022-10-21 11:44:40 -04: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 07:06:53 -05: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 05:58:49 -04: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 06:19:43 -04:00
|
|
|
return errTailscaleWrongPeerCount
|
2022-10-18 05:58:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-18 06:09:10 -04:00
|
|
|
// TODO(kradalby): Make multiping, go routine magic.
|
2022-10-21 08:07:46 -04:00
|
|
|
func (t *TailscaleInContainer) Ping(hostnameOrIP string) error {
|
2022-10-18 05:58:49 -04:00
|
|
|
return t.pool.Retry(func() error {
|
|
|
|
command := []string{
|
|
|
|
"tailscale", "ping",
|
|
|
|
"--timeout=1s",
|
|
|
|
"--c=10",
|
|
|
|
"--until-direct=true",
|
2022-10-21 08:07:46 -04:00
|
|
|
hostnameOrIP,
|
2022-10-18 05:58:49 -04:00
|
|
|
}
|
|
|
|
|
2022-11-03 11:50:20 -04:00
|
|
|
result, _, err := t.Execute(command)
|
2022-10-18 05:58:49 -04:00
|
|
|
if err != nil {
|
|
|
|
log.Printf(
|
|
|
|
"failed to run ping command from %s to %s, err: %s",
|
2022-10-21 08:07:46 -04:00
|
|
|
t.Hostname(),
|
|
|
|
hostnameOrIP,
|
2022-10-18 05:58:49 -04:00
|
|
|
err,
|
|
|
|
)
|
2022-10-18 06:19:43 -04:00
|
|
|
|
2022-10-18 05:58:49 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(result, "pong") && !strings.Contains(result, "is local") {
|
|
|
|
return backoff.Permanent(errTailscalePingFailed)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2022-10-13 10:00:08 -04:00
|
|
|
}
|
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
func (t *TailscaleInContainer) WriteFile(path string, data []byte) error {
|
|
|
|
return integrationutil.WriteFileToContainer(t.pool, t.container, path, data)
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:00:08 -04: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
|
|
|
|
}
|