2023-01-17 20:36:46 +01:00
|
|
|
// nolint
|
2021-11-04 23:10:57 +01:00
|
|
|
package headscale
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-05-17 22:11:51 +02:00
|
|
|
"encoding/json"
|
2022-06-27 13:56:37 +02:00
|
|
|
"errors"
|
2021-11-04 23:10:57 +01:00
|
|
|
"fmt"
|
2022-09-02 09:22:34 +02:00
|
|
|
"net/netip"
|
2022-06-27 13:56:37 +02:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2021-11-04 23:10:57 +01:00
|
|
|
"time"
|
|
|
|
|
2022-05-17 22:11:51 +02:00
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
2021-11-04 23:10:57 +01:00
|
|
|
"github.com/ory/dockertest/v3"
|
|
|
|
"github.com/ory/dockertest/v3/docker"
|
|
|
|
)
|
|
|
|
|
2022-06-27 13:56:37 +02:00
|
|
|
const (
|
2022-10-07 22:10:33 +02:00
|
|
|
headscaleNetwork = "headscale-test"
|
2022-10-03 13:27:18 +02:00
|
|
|
headscaleHostname = "headscale"
|
2022-06-27 13:56:37 +02:00
|
|
|
DOCKER_EXECUTE_TIMEOUT = 10 * time.Second
|
|
|
|
)
|
2021-11-14 18:35:49 +01:00
|
|
|
|
2022-01-25 23:11:15 +01:00
|
|
|
var (
|
2022-06-27 13:56:37 +02:00
|
|
|
errEnvVarEmpty = errors.New("getenv: environment variable empty")
|
|
|
|
|
2022-09-02 09:22:34 +02:00
|
|
|
IpPrefix4 = netip.MustParsePrefix("100.64.0.0/10")
|
|
|
|
IpPrefix6 = netip.MustParsePrefix("fd7a:115c:a1e0::/48")
|
2022-03-04 13:26:45 +01:00
|
|
|
|
2022-04-23 10:30:13 +02:00
|
|
|
tailscaleVersions = []string{
|
2022-10-14 09:12:13 +02:00
|
|
|
"head",
|
|
|
|
"unstable",
|
2023-02-26 14:54:41 +01:00
|
|
|
"1.36.2",
|
|
|
|
"1.34.2",
|
2022-12-05 15:06:55 +01:00
|
|
|
"1.32.3",
|
2022-10-03 14:41:20 +02:00
|
|
|
"1.30.2",
|
2022-08-09 13:53:02 +02:00
|
|
|
"1.28.0",
|
|
|
|
"1.26.2",
|
2022-06-11 15:45:32 +02:00
|
|
|
"1.24.2",
|
2022-04-23 10:30:13 +02:00
|
|
|
"1.22.2",
|
|
|
|
"1.20.4",
|
|
|
|
"1.18.2",
|
|
|
|
"1.16.2",
|
|
|
|
"1.14.3",
|
|
|
|
"1.12.3",
|
|
|
|
}
|
2022-01-25 23:11:15 +01:00
|
|
|
)
|
2022-01-15 16:25:38 +01:00
|
|
|
|
|
|
|
type ExecuteCommandConfig struct {
|
|
|
|
timeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
type ExecuteCommandOption func(*ExecuteCommandConfig) error
|
|
|
|
|
|
|
|
func ExecuteCommandTimeout(timeout time.Duration) ExecuteCommandOption {
|
|
|
|
return ExecuteCommandOption(func(conf *ExecuteCommandConfig) error {
|
|
|
|
conf.timeout = timeout
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-13 09:36:45 +01:00
|
|
|
func ExecuteCommand(
|
|
|
|
resource *dockertest.Resource,
|
|
|
|
cmd []string,
|
|
|
|
env []string,
|
2022-01-15 16:25:38 +01:00
|
|
|
options ...ExecuteCommandOption,
|
2022-09-21 17:01:26 +02:00
|
|
|
) (string, string, error) {
|
2021-11-04 23:10:57 +01:00
|
|
|
var stdout bytes.Buffer
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
|
2022-01-15 16:25:38 +01:00
|
|
|
execConfig := ExecuteCommandConfig{
|
|
|
|
timeout: DOCKER_EXECUTE_TIMEOUT,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range options {
|
|
|
|
if err := opt(&execConfig); err != nil {
|
2022-09-21 17:01:26 +02:00
|
|
|
return "", "", fmt.Errorf("execute-command/options: %w", err)
|
2022-01-15 16:25:38 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-04 23:10:57 +01:00
|
|
|
|
|
|
|
type result struct {
|
|
|
|
exitCode int
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
resultChan := make(chan result, 1)
|
|
|
|
|
|
|
|
// Run your long running function in it's own goroutine and pass back it's
|
|
|
|
// response into our channel.
|
|
|
|
go func() {
|
|
|
|
exitCode, err := resource.Exec(
|
|
|
|
cmd,
|
|
|
|
dockertest.ExecOptions{
|
2021-11-07 11:40:05 +01:00
|
|
|
Env: append(env, "HEADSCALE_LOG_LEVEL=disabled"),
|
2021-11-04 23:10:57 +01:00
|
|
|
StdOut: &stdout,
|
|
|
|
StdErr: &stderr,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
resultChan <- result{exitCode, err}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Listen on our channel AND a timeout channel - which ever happens first.
|
|
|
|
select {
|
|
|
|
case res := <-resultChan:
|
|
|
|
if res.err != nil {
|
2022-09-21 17:01:26 +02:00
|
|
|
return stdout.String(), stderr.String(), res.err
|
2021-11-04 23:10:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if res.exitCode != 0 {
|
|
|
|
fmt.Println("Command: ", cmd)
|
|
|
|
fmt.Println("stdout: ", stdout.String())
|
|
|
|
fmt.Println("stderr: ", stderr.String())
|
2021-11-14 18:35:49 +01:00
|
|
|
|
2022-10-07 10:45:41 +02:00
|
|
|
return stdout.String(), stderr.String(), fmt.Errorf(
|
|
|
|
"command failed with: %s",
|
|
|
|
stderr.String(),
|
|
|
|
)
|
2021-11-04 23:10:57 +01:00
|
|
|
}
|
|
|
|
|
2022-09-21 17:01:26 +02:00
|
|
|
return stdout.String(), stderr.String(), nil
|
2022-01-15 16:25:38 +01:00
|
|
|
case <-time.After(execConfig.timeout):
|
2021-11-14 18:35:49 +01:00
|
|
|
|
2022-10-07 10:45:41 +02:00
|
|
|
return stdout.String(), stderr.String(), fmt.Errorf(
|
|
|
|
"command timed out after %s",
|
|
|
|
execConfig.timeout,
|
|
|
|
)
|
2021-11-04 23:10:57 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func DockerRestartPolicy(config *docker.HostConfig) {
|
2022-01-15 16:25:38 +01:00
|
|
|
// set AutoRemove to true so that stopped container goes away by itself on error *immediately*.
|
|
|
|
// when set to false, containers remain until the end of the integration test.
|
|
|
|
config.AutoRemove = false
|
2021-11-04 23:10:57 +01:00
|
|
|
config.RestartPolicy = docker.RestartPolicy{
|
|
|
|
Name: "no",
|
|
|
|
}
|
|
|
|
}
|
2022-01-15 16:25:38 +01:00
|
|
|
|
|
|
|
func DockerAllowLocalIPv6(config *docker.HostConfig) {
|
|
|
|
if config.Sysctls == nil {
|
|
|
|
config.Sysctls = make(map[string]string, 1)
|
|
|
|
}
|
|
|
|
config.Sysctls["net.ipv6.conf.all.disable_ipv6"] = "0"
|
|
|
|
}
|
|
|
|
|
|
|
|
func DockerAllowNetworkAdministration(config *docker.HostConfig) {
|
|
|
|
config.CapAdd = append(config.CapAdd, "NET_ADMIN")
|
|
|
|
config.Mounts = append(config.Mounts, docker.HostMount{
|
|
|
|
Type: "bind",
|
|
|
|
Source: "/dev/net/tun",
|
|
|
|
Target: "/dev/net/tun",
|
|
|
|
})
|
|
|
|
}
|
2022-03-04 13:54:59 +01:00
|
|
|
|
2022-03-18 17:05:28 +01:00
|
|
|
func getDockerBuildOptions(version string) *dockertest.BuildOptions {
|
|
|
|
var tailscaleBuildOptions *dockertest.BuildOptions
|
|
|
|
switch version {
|
2022-03-20 12:30:56 +01:00
|
|
|
case "head":
|
2022-03-18 17:05:28 +01:00
|
|
|
tailscaleBuildOptions = &dockertest.BuildOptions{
|
|
|
|
Dockerfile: "Dockerfile.tailscale-HEAD",
|
|
|
|
ContextDir: ".",
|
|
|
|
BuildArgs: []docker.BuildArg{},
|
|
|
|
}
|
|
|
|
case "unstable":
|
|
|
|
tailscaleBuildOptions = &dockertest.BuildOptions{
|
|
|
|
Dockerfile: "Dockerfile.tailscale",
|
|
|
|
ContextDir: ".",
|
|
|
|
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: ".",
|
|
|
|
BuildArgs: []docker.BuildArg{
|
|
|
|
{
|
|
|
|
Name: "TAILSCALE_VERSION",
|
|
|
|
Value: version,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "TAILSCALE_CHANNEL",
|
|
|
|
Value: "stable",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return tailscaleBuildOptions
|
|
|
|
}
|
|
|
|
|
2022-05-17 22:11:51 +02:00
|
|
|
func getDNSNames(
|
|
|
|
headscale *dockertest.Resource,
|
|
|
|
) ([]string, error) {
|
2022-09-21 17:01:26 +02:00
|
|
|
listAllResult, _, err := ExecuteCommand(
|
2022-05-17 22:11:51 +02:00
|
|
|
headscale,
|
|
|
|
[]string{
|
|
|
|
"headscale",
|
|
|
|
"nodes",
|
|
|
|
"list",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
},
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var listAll []v1.Machine
|
|
|
|
err = json.Unmarshal([]byte(listAllResult), &listAll)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
hostnames := make([]string, len(listAll))
|
|
|
|
|
|
|
|
for index := range listAll {
|
|
|
|
hostnames[index] = listAll[index].GetGivenName()
|
|
|
|
}
|
|
|
|
|
|
|
|
return hostnames, nil
|
|
|
|
}
|
2022-05-18 21:18:03 +02:00
|
|
|
|
2022-06-27 13:56:37 +02:00
|
|
|
func GetEnvStr(key string) (string, error) {
|
|
|
|
v := os.Getenv(key)
|
|
|
|
if v == "" {
|
|
|
|
return v, errEnvVarEmpty
|
|
|
|
}
|
|
|
|
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetEnvBool(key string) (bool, error) {
|
|
|
|
s, err := GetEnvStr(key)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
v, err := strconv.ParseBool(s)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return v, nil
|
|
|
|
}
|
2022-10-07 10:45:23 +02:00
|
|
|
|
|
|
|
func GetFirstOrCreateNetwork(pool *dockertest.Pool, name string) (dockertest.Network, error) {
|
|
|
|
networks, err := pool.NetworksByName(name)
|
|
|
|
if err != nil || len(networks) == 0 {
|
|
|
|
if _, err := pool.CreateNetwork(name); err == nil {
|
|
|
|
// Create does not give us an updated version of the resource, so we need to
|
|
|
|
// get it again.
|
|
|
|
networks, err := pool.NetworksByName(name)
|
|
|
|
if err != nil {
|
|
|
|
return dockertest.Network{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return networks[0], nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return networks[0], nil
|
|
|
|
}
|