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"
|
|
|
|
"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"
|
|
|
|
"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-10-13 14:00:08 +00:00
|
|
|
|
2022-10-18 10:09:10 +00:00
|
|
|
var (
|
2022-10-18 10:19:43 +00:00
|
|
|
errTailscalePingFailed = errors.New("ping failed")
|
|
|
|
errTailscaleNotLoggedIn = errors.New("tailscale not logged in")
|
|
|
|
errTailscaleWrongPeerCount = errors.New("wrong peer count")
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(
|
|
|
|
pool *dockertest.Pool,
|
|
|
|
version string,
|
2022-10-18 10:09:10 +00:00
|
|
|
network *dockertest.Network,
|
|
|
|
) (*TailscaleInContainer, error) {
|
2022-10-13 14:00:08 +00:00
|
|
|
hash, err := headscale.GenerateRandomStringDNSSafe(tsicHashLength)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hostname := fmt.Sprintf("ts-%s-%s", version, hash)
|
|
|
|
|
|
|
|
// TODO(kradalby): figure out why we need to "refresh" the network here.
|
|
|
|
// network, err = dockertestutil.GetFirstOrCreateNetwork(pool, network.Network.Name)
|
|
|
|
// if err != nil {
|
|
|
|
// return nil, err
|
|
|
|
// }
|
|
|
|
|
|
|
|
tailscaleOptions := &dockertest.RunOptions{
|
|
|
|
Name: hostname,
|
|
|
|
Networks: []*dockertest.Network{network},
|
|
|
|
Cmd: []string{
|
|
|
|
"tailscaled", "--tun=tsdev",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
return &TailscaleInContainer{
|
|
|
|
version: version,
|
2022-10-21 11:17:54 +00:00
|
|
|
hostname: hostname,
|
2022-10-13 14:00:08 +00:00
|
|
|
|
|
|
|
pool: pool,
|
|
|
|
container: container,
|
|
|
|
network: network,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Join command:", command)
|
2022-10-21 11:17:54 +00:00
|
|
|
log.Printf("Running join command for %s\n", t.hostname)
|
2022-10-14 10:17:59 +00:00
|
|
|
stdout, stderr, err := dockertestutil.ExecuteCommand(
|
2022-10-13 14:00:08 +00:00
|
|
|
t.container,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-10-14 10:17:59 +00:00
|
|
|
log.Printf("tailscale join stderr: %s\n", stderr)
|
|
|
|
|
2022-10-13 14:00:08 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-10-18 09:58:49 +00:00
|
|
|
|
|
|
|
if stdout != "" {
|
|
|
|
log.Printf("tailscale join stdout: %s\n", stdout)
|
|
|
|
}
|
|
|
|
|
2022-10-21 11:17:54 +00:00
|
|
|
log.Printf("%s joined\n", t.hostname)
|
2022-10-13 14:00:08 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-18 10:09:10 +00:00
|
|
|
// TODO(kradalby): Make cached/lazy.
|
2022-10-13 14:00:08 +00:00
|
|
|
func (t *TailscaleInContainer) IPs() ([]netip.Addr, error) {
|
|
|
|
ips := make([]netip.Addr, 0)
|
|
|
|
|
|
|
|
command := []string{
|
|
|
|
"tailscale",
|
|
|
|
"ip",
|
|
|
|
}
|
|
|
|
|
2022-10-14 10:17:59 +00:00
|
|
|
result, stderr, err := dockertestutil.ExecuteCommand(
|
2022-10-13 14:00:08 +00:00
|
|
|
t.container,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-10-14 10:17:59 +00:00
|
|
|
log.Printf("failed commands stderr: %s\n", stderr)
|
|
|
|
|
|
|
|
if strings.Contains(stderr, "NeedsLogin") {
|
|
|
|
return []netip.Addr{}, errTailscaleNotLoggedIn
|
|
|
|
}
|
|
|
|
|
2022-10-13 14:00:08 +00:00
|
|
|
return []netip.Addr{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
result, _, err := dockertestutil.ExecuteCommand(
|
|
|
|
t.container,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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-18 09:58:49 +00:00
|
|
|
func (t *TailscaleInContainer) Ping(ip netip.Addr) error {
|
|
|
|
return t.pool.Retry(func() error {
|
|
|
|
command := []string{
|
|
|
|
"tailscale", "ping",
|
|
|
|
"--timeout=1s",
|
|
|
|
"--c=10",
|
|
|
|
"--until-direct=true",
|
|
|
|
ip.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
result, _, err := dockertestutil.ExecuteCommand(
|
|
|
|
t.container,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(
|
|
|
|
"failed to run ping command from %s to %s, err: %s",
|
2022-10-21 11:17:54 +00:00
|
|
|
t.hostname,
|
2022-10-18 09:58:49 +00:00
|
|
|
ip.String(),
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|