2022-10-13 16:01:23 +02:00
|
|
|
package hsic
|
|
|
|
|
|
|
|
import (
|
2022-11-02 09:55:09 +01:00
|
|
|
"bytes"
|
2022-11-06 20:22:21 +01:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
2022-10-13 16:01:23 +02:00
|
|
|
"encoding/json"
|
2022-11-06 20:22:21 +01:00
|
|
|
"encoding/pem"
|
2022-10-13 16:01:23 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2022-11-06 20:22:21 +01:00
|
|
|
"math/big"
|
|
|
|
"net"
|
2022-10-13 16:01:23 +02:00
|
|
|
"net/http"
|
2023-04-13 21:10:08 +00:00
|
|
|
"net/url"
|
2023-04-27 16:57:11 +02:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2022-11-06 20:22:21 +01:00
|
|
|
"time"
|
2022-10-13 16:01:23 +02:00
|
|
|
|
2023-01-05 12:44:28 +01:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2022-10-13 16:01:23 +02:00
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
2023-05-21 19:37:59 +03:00
|
|
|
"github.com/juanfont/headscale/hscontrol/policy"
|
2024-04-17 07:03:06 +02:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2023-05-11 09:09:18 +02:00
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2022-10-13 16:01:23 +02:00
|
|
|
"github.com/juanfont/headscale/integration/dockertestutil"
|
2022-11-06 20:22:21 +01:00
|
|
|
"github.com/juanfont/headscale/integration/integrationutil"
|
2022-10-13 16:01:23 +02:00
|
|
|
"github.com/ory/dockertest/v3"
|
2023-04-13 21:10:08 +00:00
|
|
|
"github.com/ory/dockertest/v3/docker"
|
2022-10-13 16:01:23 +02:00
|
|
|
)
|
|
|
|
|
2022-10-18 12:09:10 +02:00
|
|
|
const (
|
2022-11-06 20:22:21 +01:00
|
|
|
hsicHashLength = 6
|
|
|
|
dockerContextPath = "../."
|
|
|
|
aclPolicyPath = "/etc/headscale/acl.hujson"
|
|
|
|
tlsCertPath = "/etc/headscale/tls.cert"
|
|
|
|
tlsKeyPath = "/etc/headscale/tls.key"
|
|
|
|
headscaleDefaultPort = 8080
|
2022-10-18 12:09:10 +02:00
|
|
|
)
|
2022-10-13 16:01:23 +02:00
|
|
|
|
|
|
|
var errHeadscaleStatusCodeNotOk = errors.New("headscale status code not ok")
|
|
|
|
|
2023-01-10 13:46:42 +02:00
|
|
|
type fileInContainer struct {
|
|
|
|
path string
|
|
|
|
contents []byte
|
|
|
|
}
|
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// HeadscaleInContainer is an implementation of ControlServer which
|
|
|
|
// sets up a Headscale instance inside a container.
|
2022-10-13 16:01:23 +02:00
|
|
|
type HeadscaleInContainer struct {
|
|
|
|
hostname string
|
|
|
|
|
|
|
|
pool *dockertest.Pool
|
|
|
|
container *dockertest.Resource
|
|
|
|
network *dockertest.Network
|
2022-11-02 11:08:54 +01:00
|
|
|
|
2024-02-18 19:31:29 +01:00
|
|
|
pgContainer *dockertest.Resource
|
|
|
|
|
2022-11-02 11:08:54 +01:00
|
|
|
// optional config
|
2023-01-10 13:46:42 +02:00
|
|
|
port int
|
2023-04-13 21:10:08 +00:00
|
|
|
extraPorts []string
|
|
|
|
hostPortBindings map[string][]string
|
2023-05-21 19:37:59 +03:00
|
|
|
aclPolicy *policy.ACLPolicy
|
2023-01-10 13:46:42 +02:00
|
|
|
env map[string]string
|
|
|
|
tlsCert []byte
|
|
|
|
tlsKey []byte
|
|
|
|
filesInContainer []fileInContainer
|
2024-02-18 19:31:29 +01:00
|
|
|
postgres bool
|
2022-11-02 11:08:54 +01:00
|
|
|
}
|
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// Option represent optional settings that can be given to a
|
|
|
|
// Headscale instance.
|
2022-11-02 11:08:54 +01:00
|
|
|
type Option = func(c *HeadscaleInContainer)
|
|
|
|
|
2023-05-10 10:26:28 +02:00
|
|
|
// WithACLPolicy adds a hscontrol.ACLPolicy policy to the
|
2023-02-03 12:24:27 +01:00
|
|
|
// HeadscaleInContainer instance.
|
2023-05-21 19:37:59 +03:00
|
|
|
func WithACLPolicy(acl *policy.ACLPolicy) Option {
|
2022-11-02 11:08:54 +01:00
|
|
|
return func(hsic *HeadscaleInContainer) {
|
2022-11-06 20:22:21 +01:00
|
|
|
// TODO(kradalby): Move somewhere appropriate
|
2023-01-05 12:44:28 +01:00
|
|
|
hsic.env["HEADSCALE_ACL_POLICY_PATH"] = aclPolicyPath
|
2022-11-06 20:22:21 +01:00
|
|
|
|
2022-11-02 11:08:54 +01:00
|
|
|
hsic.aclPolicy = acl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// WithTLS creates certificates and enables HTTPS.
|
2022-11-06 20:22:21 +01:00
|
|
|
func WithTLS() Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
2023-04-13 21:10:08 +00:00
|
|
|
cert, key, err := createCertificate(hsic.hostname)
|
2022-11-06 20:22:21 +01:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to create certificates for headscale test: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(kradalby): Move somewhere appropriate
|
2023-01-05 12:44:28 +01:00
|
|
|
hsic.env["HEADSCALE_TLS_CERT_PATH"] = tlsCertPath
|
|
|
|
hsic.env["HEADSCALE_TLS_KEY_PATH"] = tlsKeyPath
|
2022-11-06 20:22:21 +01:00
|
|
|
|
|
|
|
hsic.tlsCert = cert
|
|
|
|
hsic.tlsKey = key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// WithConfigEnv takes a map of environment variables that
|
|
|
|
// can be used to override Headscale configuration.
|
2022-11-02 11:08:54 +01:00
|
|
|
func WithConfigEnv(configEnv map[string]string) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
for key, value := range configEnv {
|
2023-01-05 12:44:28 +01:00
|
|
|
hsic.env[key] = value
|
2022-11-02 11:08:54 +01:00
|
|
|
}
|
|
|
|
}
|
2022-10-13 16:01:23 +02:00
|
|
|
}
|
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// WithPort sets the port on where to run Headscale.
|
2022-11-06 20:22:21 +01:00
|
|
|
func WithPort(port int) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
hsic.port = port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 21:10:47 +00:00
|
|
|
// WithExtraPorts exposes additional ports on the container (e.g. 3478/udp for STUN).
|
2023-04-13 21:10:08 +00:00
|
|
|
func WithExtraPorts(ports []string) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
hsic.extraPorts = ports
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithHostPortBindings(bindings map[string][]string) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
hsic.hostPortBindings = bindings
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-03 18:22:47 +01:00
|
|
|
// WithTestName sets a name for the test, this will be reflected
|
2023-02-03 12:24:27 +01:00
|
|
|
// in the Docker container name.
|
2022-11-14 15:01:31 +01:00
|
|
|
func WithTestName(testName string) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
2023-05-11 09:09:18 +02:00
|
|
|
hash, _ := util.GenerateRandomStringDNSSafe(hsicHashLength)
|
2022-11-14 15:01:31 +01:00
|
|
|
|
|
|
|
hostname := fmt.Sprintf("hs-%s-%s", testName, hash)
|
|
|
|
hsic.hostname = hostname
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// WithHostnameAsServerURL sets the Headscale ServerURL based on
|
|
|
|
// the Hostname.
|
2022-11-21 21:51:54 +01:00
|
|
|
func WithHostnameAsServerURL() Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
2023-01-05 12:44:28 +01:00
|
|
|
hsic.env["HEADSCALE_SERVER_URL"] = fmt.Sprintf("http://%s",
|
|
|
|
net.JoinHostPort(hsic.GetHostname(),
|
|
|
|
fmt.Sprintf("%d", hsic.port)),
|
|
|
|
)
|
2022-11-21 21:51:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// WithFileInContainer adds a file to the container at the given path.
|
2023-01-10 13:46:42 +02:00
|
|
|
func WithFileInContainer(path string, contents []byte) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
hsic.filesInContainer = append(hsic.filesInContainer,
|
|
|
|
fileInContainer{
|
|
|
|
path: path,
|
|
|
|
contents: contents,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-18 19:31:29 +01:00
|
|
|
// WithPostgres spins up a Postgres container and
|
|
|
|
// sets it as the main database.
|
|
|
|
func WithPostgres() Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
hsic.postgres = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-17 07:03:06 +02:00
|
|
|
// WithIPAllocationStrategy sets the tests IP Allocation strategy.
|
|
|
|
func WithIPAllocationStrategy(strat types.IPAllocationStrategy) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
hsic.env["HEADSCALE_PREFIXES_ALLOCATION"] = string(strat)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-16 21:37:25 +02:00
|
|
|
// WithEmbeddedDERPServerOnly configures Headscale to start
|
|
|
|
// and only use the embedded DERP server.
|
|
|
|
// It requires WithTLS and WithHostnameAsServerURL to be
|
|
|
|
// set.
|
|
|
|
func WithEmbeddedDERPServerOnly() Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
hsic.env["HEADSCALE_DERP_URLS"] = ""
|
|
|
|
hsic.env["HEADSCALE_DERP_SERVER_ENABLED"] = "true"
|
|
|
|
hsic.env["HEADSCALE_DERP_SERVER_REGION_ID"] = "999"
|
|
|
|
hsic.env["HEADSCALE_DERP_SERVER_REGION_CODE"] = "headscale"
|
|
|
|
hsic.env["HEADSCALE_DERP_SERVER_REGION_NAME"] = "Headscale Embedded DERP"
|
|
|
|
hsic.env["HEADSCALE_DERP_SERVER_STUN_LISTEN_ADDR"] = "0.0.0.0:3478"
|
|
|
|
hsic.env["HEADSCALE_DERP_SERVER_PRIVATE_KEY_PATH"] = "/tmp/derp.key"
|
|
|
|
|
|
|
|
// Envknob for enabling DERP debug logs
|
|
|
|
hsic.env["DERP_DEBUG_LOGS"] = "true"
|
|
|
|
hsic.env["DERP_PROBER_DEBUG_LOGS"] = "true"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// New returns a new HeadscaleInContainer instance.
|
2022-10-13 16:01:23 +02:00
|
|
|
func New(
|
|
|
|
pool *dockertest.Pool,
|
2022-10-18 12:09:10 +02:00
|
|
|
network *dockertest.Network,
|
2022-11-02 11:08:54 +01:00
|
|
|
opts ...Option,
|
2022-10-18 12:09:10 +02:00
|
|
|
) (*HeadscaleInContainer, error) {
|
2023-05-11 09:09:18 +02:00
|
|
|
hash, err := util.GenerateRandomStringDNSSafe(hsicHashLength)
|
2022-10-13 16:01:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-02 11:08:54 +01:00
|
|
|
hostname := fmt.Sprintf("hs-%s", hash)
|
|
|
|
|
|
|
|
hsic := &HeadscaleInContainer{
|
|
|
|
hostname: hostname,
|
2022-11-06 20:22:21 +01:00
|
|
|
port: headscaleDefaultPort,
|
2022-11-02 11:08:54 +01:00
|
|
|
|
|
|
|
pool: pool,
|
|
|
|
network: network,
|
2023-01-05 12:44:28 +01:00
|
|
|
|
2023-01-10 13:46:42 +02:00
|
|
|
env: DefaultConfigEnv(),
|
|
|
|
filesInContainer: []fileInContainer{},
|
2022-11-02 11:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(hsic)
|
|
|
|
}
|
|
|
|
|
2022-11-14 15:01:31 +01:00
|
|
|
log.Println("NAME: ", hsic.hostname)
|
|
|
|
|
2022-11-06 20:22:21 +01:00
|
|
|
portProto := fmt.Sprintf("%d/tcp", hsic.port)
|
2022-11-02 11:08:54 +01:00
|
|
|
|
2023-04-13 21:10:47 +00:00
|
|
|
serverURL, err := url.Parse(hsic.env["HEADSCALE_SERVER_URL"])
|
2023-04-13 21:10:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(hsic.tlsCert) != 0 && len(hsic.tlsKey) != 0 {
|
2023-04-13 21:10:47 +00:00
|
|
|
serverURL.Scheme = "https"
|
|
|
|
hsic.env["HEADSCALE_SERVER_URL"] = serverURL.String()
|
2023-04-13 21:10:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 16:01:23 +02:00
|
|
|
headscaleBuildOptions := &dockertest.BuildOptions{
|
2022-11-02 09:53:55 +01:00
|
|
|
Dockerfile: "Dockerfile.debug",
|
2022-10-13 16:01:23 +02:00
|
|
|
ContextDir: dockerContextPath,
|
|
|
|
}
|
|
|
|
|
2024-02-18 19:31:29 +01:00
|
|
|
if hsic.postgres {
|
|
|
|
hsic.env["HEADSCALE_DATABASE_TYPE"] = "postgres"
|
|
|
|
hsic.env["HEADSCALE_DATABASE_POSTGRES_HOST"] = fmt.Sprintf("postgres-%s", hash)
|
|
|
|
hsic.env["HEADSCALE_DATABASE_POSTGRES_USER"] = "headscale"
|
|
|
|
hsic.env["HEADSCALE_DATABASE_POSTGRES_PASS"] = "headscale"
|
|
|
|
hsic.env["HEADSCALE_DATABASE_POSTGRES_NAME"] = "headscale"
|
|
|
|
delete(hsic.env, "HEADSCALE_DATABASE_SQLITE_PATH")
|
|
|
|
|
|
|
|
pg, err := pool.RunWithOptions(
|
|
|
|
&dockertest.RunOptions{
|
|
|
|
Name: fmt.Sprintf("postgres-%s", hash),
|
|
|
|
Repository: "postgres",
|
|
|
|
Tag: "latest",
|
|
|
|
Networks: []*dockertest.Network{network},
|
|
|
|
Env: []string{
|
|
|
|
"POSTGRES_USER=headscale",
|
|
|
|
"POSTGRES_PASSWORD=headscale",
|
|
|
|
"POSTGRES_DB=headscale",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("starting postgres container: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
hsic.pgContainer = pg
|
|
|
|
}
|
|
|
|
|
2023-04-27 16:57:11 +02:00
|
|
|
env := []string{
|
|
|
|
"HEADSCALE_PROFILING_ENABLED=1",
|
|
|
|
"HEADSCALE_PROFILING_PATH=/tmp/profile",
|
2023-07-17 11:13:48 +02:00
|
|
|
"HEADSCALE_DEBUG_DUMP_MAPRESPONSE_PATH=/tmp/mapresponses",
|
2023-04-27 16:57:11 +02:00
|
|
|
}
|
2023-01-05 12:44:28 +01:00
|
|
|
for key, value := range hsic.env {
|
|
|
|
env = append(env, fmt.Sprintf("%s=%s", key, value))
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("ENV: \n%s", spew.Sdump(hsic.env))
|
|
|
|
|
2022-10-13 16:01:23 +02:00
|
|
|
runOptions := &dockertest.RunOptions{
|
2022-11-14 15:01:31 +01:00
|
|
|
Name: hsic.hostname,
|
2024-04-10 15:35:09 +02:00
|
|
|
ExposedPorts: append([]string{portProto, "9090/tcp"}, hsic.extraPorts...),
|
2022-11-02 09:55:48 +01:00
|
|
|
Networks: []*dockertest.Network{network},
|
|
|
|
// Cmd: []string{"headscale", "serve"},
|
|
|
|
// TODO(kradalby): Get rid of this hack, we currently need to give us some
|
|
|
|
// to inject the headscale configuration further down.
|
2023-04-27 16:57:11 +02:00
|
|
|
Entrypoint: []string{"/bin/bash", "-c", "/bin/sleep 3 ; headscale serve ; /bin/sleep 30"},
|
2023-01-05 12:44:28 +01:00
|
|
|
Env: env,
|
2022-10-13 16:01:23 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 21:10:08 +00:00
|
|
|
if len(hsic.hostPortBindings) > 0 {
|
|
|
|
runOptions.PortBindings = map[docker.Port][]docker.PortBinding{}
|
|
|
|
for port, hostPorts := range hsic.hostPortBindings {
|
|
|
|
runOptions.PortBindings[docker.Port(port)] = []docker.PortBinding{}
|
|
|
|
for _, hostPort := range hostPorts {
|
|
|
|
runOptions.PortBindings[docker.Port(port)] = append(
|
|
|
|
runOptions.PortBindings[docker.Port(port)],
|
|
|
|
docker.PortBinding{HostPort: hostPort})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 16:01:23 +02:00
|
|
|
// dockertest isnt very good at handling containers that has already
|
|
|
|
// been created, this is an attempt to make sure this container isnt
|
|
|
|
// present.
|
2022-11-14 15:01:31 +01:00
|
|
|
err = pool.RemoveContainerByName(hsic.hostname)
|
2022-10-13 16:01:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
container, err := pool.BuildAndRunWithBuildOptions(
|
|
|
|
headscaleBuildOptions,
|
|
|
|
runOptions,
|
|
|
|
dockertestutil.DockerRestartPolicy,
|
|
|
|
dockertestutil.DockerAllowLocalIPv6,
|
|
|
|
dockertestutil.DockerAllowNetworkAdministration,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not start headscale container: %w", err)
|
|
|
|
}
|
2022-11-14 15:01:31 +01:00
|
|
|
log.Printf("Created %s container\n", hsic.hostname)
|
2022-10-13 16:01:23 +02:00
|
|
|
|
2022-11-02 11:08:54 +01:00
|
|
|
hsic.container = container
|
2022-11-02 09:55:48 +01:00
|
|
|
|
2023-01-05 12:44:28 +01:00
|
|
|
err = hsic.WriteFile("/etc/headscale/config.yaml", []byte(MinimumConfigYAML()))
|
2022-11-02 09:55:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to write headscale config to container: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-02 11:08:54 +01:00
|
|
|
if hsic.aclPolicy != nil {
|
|
|
|
data, err := json.Marshal(hsic.aclPolicy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to marshal ACL Policy to JSON: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = hsic.WriteFile(aclPolicyPath, data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to write ACL policy to container: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 20:22:21 +01:00
|
|
|
if hsic.hasTLS() {
|
|
|
|
err = hsic.WriteFile(tlsCertPath, hsic.tlsCert)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to write TLS certificate to container: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = hsic.WriteFile(tlsKeyPath, hsic.tlsKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to write TLS key to container: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 13:46:42 +02:00
|
|
|
for _, f := range hsic.filesInContainer {
|
|
|
|
if err := hsic.WriteFile(f.path, f.contents); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to write %q: %w", f.path, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 09:55:48 +01:00
|
|
|
return hsic, nil
|
2022-10-13 16:01:23 +02:00
|
|
|
}
|
|
|
|
|
2023-04-23 11:02:28 +00:00
|
|
|
func (t *HeadscaleInContainer) ConnectToNetwork(network *dockertest.Network) error {
|
|
|
|
return t.container.ConnectToNetwork(network)
|
|
|
|
}
|
|
|
|
|
2022-11-06 20:22:21 +01:00
|
|
|
func (t *HeadscaleInContainer) hasTLS() bool {
|
|
|
|
return len(t.tlsCert) != 0 && len(t.tlsKey) != 0
|
|
|
|
}
|
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// Shutdown stops and cleans up the Headscale container.
|
2022-10-13 16:01:23 +02:00
|
|
|
func (t *HeadscaleInContainer) Shutdown() error {
|
2023-04-27 16:57:11 +02:00
|
|
|
err := t.SaveLog("/tmp/control")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(
|
|
|
|
"Failed to save log from control: %s",
|
|
|
|
fmt.Errorf("failed to save log from control: %w", err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send a interrupt signal to the "headscale" process inside the container
|
|
|
|
// allowing it to shut down gracefully and flush the profile to disk.
|
|
|
|
// The container will live for a bit longer due to the sleep at the end.
|
|
|
|
err = t.SendInterrupt()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(
|
|
|
|
"Failed to send graceful interrupt to control: %s",
|
|
|
|
fmt.Errorf("failed to send graceful interrupt to control: %w", err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = t.SaveProfile("/tmp/control")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(
|
|
|
|
"Failed to save profile from control: %s",
|
|
|
|
fmt.Errorf("failed to save profile from control: %w", err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-07-17 11:13:48 +02:00
|
|
|
err = t.SaveMapResponses("/tmp/control")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(
|
|
|
|
"Failed to save mapresponses from control: %s",
|
|
|
|
fmt.Errorf("failed to save mapresponses from control: %w", err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-02-18 19:31:29 +01:00
|
|
|
// We dont have a database to save if we use postgres
|
|
|
|
if !t.postgres {
|
|
|
|
err = t.SaveDatabase("/tmp/control")
|
|
|
|
if err != nil {
|
|
|
|
log.Printf(
|
|
|
|
"Failed to save database from control: %s",
|
|
|
|
fmt.Errorf("failed to save database from control: %w", err),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanup postgres container if enabled.
|
|
|
|
if t.postgres {
|
|
|
|
t.pool.Purge(t.pgContainer)
|
2023-11-16 17:55:29 +01:00
|
|
|
}
|
|
|
|
|
2022-10-13 16:01:23 +02:00
|
|
|
return t.pool.Purge(t.container)
|
|
|
|
}
|
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// SaveLog saves the current stdout log of the container to a path
|
|
|
|
// on the host system.
|
2023-01-30 10:20:08 +01:00
|
|
|
func (t *HeadscaleInContainer) SaveLog(path string) error {
|
|
|
|
return dockertestutil.SaveLog(t.pool, t.container, path)
|
|
|
|
}
|
|
|
|
|
2023-04-27 16:57:11 +02:00
|
|
|
func (t *HeadscaleInContainer) SaveProfile(savePath string) error {
|
|
|
|
tarFile, err := t.FetchPath("/tmp/profile")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-17 11:13:48 +02:00
|
|
|
err = os.WriteFile(
|
2023-07-24 08:58:51 +02:00
|
|
|
path.Join(savePath, t.hostname+".pprof.tar"),
|
2023-07-17 11:13:48 +02:00
|
|
|
tarFile,
|
|
|
|
os.ModePerm,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *HeadscaleInContainer) SaveMapResponses(savePath string) error {
|
|
|
|
tarFile, err := t.FetchPath("/tmp/mapresponses")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-27 16:57:11 +02:00
|
|
|
err = os.WriteFile(
|
2023-07-24 08:58:51 +02:00
|
|
|
path.Join(savePath, t.hostname+".maps.tar"),
|
2023-04-27 16:57:11 +02:00
|
|
|
tarFile,
|
|
|
|
os.ModePerm,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-16 17:55:29 +01:00
|
|
|
func (t *HeadscaleInContainer) SaveDatabase(savePath string) error {
|
|
|
|
tarFile, err := t.FetchPath("/tmp/integration_test_db.sqlite3")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.WriteFile(
|
|
|
|
path.Join(savePath, t.hostname+".db.tar"),
|
|
|
|
tarFile,
|
|
|
|
os.ModePerm,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// Execute runs a command inside the Headscale container and returns the
|
|
|
|
// result of stdout as a string.
|
2022-10-24 16:40:49 +02:00
|
|
|
func (t *HeadscaleInContainer) Execute(
|
|
|
|
command []string,
|
|
|
|
) (string, error) {
|
|
|
|
stdout, stderr, err := dockertestutil.ExecuteCommand(
|
|
|
|
t.container,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("command stderr: %s\n", stderr)
|
|
|
|
|
2022-11-14 09:56:54 +01:00
|
|
|
if stdout != "" {
|
|
|
|
log.Printf("command stdout: %s\n", stdout)
|
|
|
|
}
|
2022-10-24 16:40:49 +02:00
|
|
|
|
2022-11-14 09:56:54 +01:00
|
|
|
return "", err
|
2022-10-24 16:40:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return stdout, nil
|
|
|
|
}
|
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// GetIP returns the docker container IP as a string.
|
2022-10-13 16:01:23 +02:00
|
|
|
func (t *HeadscaleInContainer) GetIP() string {
|
|
|
|
return t.container.GetIPInNetwork(t.network)
|
|
|
|
}
|
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// GetPort returns the docker container port as a string.
|
2022-10-13 16:01:23 +02:00
|
|
|
func (t *HeadscaleInContainer) GetPort() string {
|
2022-11-04 00:05:01 +01:00
|
|
|
return fmt.Sprintf("%d", t.port)
|
2022-10-13 16:01:23 +02:00
|
|
|
}
|
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// GetHealthEndpoint returns a health endpoint for the HeadscaleInContainer
|
|
|
|
// instance.
|
2022-10-13 16:01:23 +02:00
|
|
|
func (t *HeadscaleInContainer) GetHealthEndpoint() string {
|
2022-11-06 20:22:21 +01:00
|
|
|
return fmt.Sprintf("%s/health", t.GetEndpoint())
|
2022-10-13 16:01:23 +02:00
|
|
|
}
|
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// GetEndpoint returns the Headscale endpoint for the HeadscaleInContainer.
|
2022-10-13 16:01:23 +02:00
|
|
|
func (t *HeadscaleInContainer) GetEndpoint() string {
|
2022-10-18 11:58:15 +02:00
|
|
|
hostEndpoint := fmt.Sprintf("%s:%d",
|
2022-10-13 16:01:23 +02:00
|
|
|
t.GetIP(),
|
2022-10-18 11:58:15 +02:00
|
|
|
t.port)
|
2022-10-13 16:01:23 +02:00
|
|
|
|
2022-11-06 20:22:21 +01:00
|
|
|
if t.hasTLS() {
|
|
|
|
return fmt.Sprintf("https://%s", hostEndpoint)
|
|
|
|
}
|
|
|
|
|
2022-10-13 16:01:23 +02:00
|
|
|
return fmt.Sprintf("http://%s", hostEndpoint)
|
|
|
|
}
|
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// GetCert returns the public certificate of the HeadscaleInContainer.
|
2022-11-06 20:22:21 +01:00
|
|
|
func (t *HeadscaleInContainer) GetCert() []byte {
|
|
|
|
return t.tlsCert
|
|
|
|
}
|
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// GetHostname returns the hostname of the HeadscaleInContainer.
|
2022-11-06 20:22:21 +01:00
|
|
|
func (t *HeadscaleInContainer) GetHostname() string {
|
|
|
|
return t.hostname
|
|
|
|
}
|
|
|
|
|
2023-08-29 08:33:33 +02:00
|
|
|
// WaitForRunning blocks until the Headscale instance is ready to
|
2023-02-03 12:24:27 +01:00
|
|
|
// serve clients.
|
2023-08-29 08:33:33 +02:00
|
|
|
func (t *HeadscaleInContainer) WaitForRunning() error {
|
2022-10-13 16:01:23 +02:00
|
|
|
url := t.GetHealthEndpoint()
|
|
|
|
|
2022-10-18 11:58:15 +02:00
|
|
|
log.Printf("waiting for headscale to be ready at %s", url)
|
|
|
|
|
2022-11-06 20:22:21 +01:00
|
|
|
client := &http.Client{}
|
|
|
|
|
|
|
|
if t.hasTLS() {
|
2022-11-10 08:04:47 +00:00
|
|
|
insecureTransport := http.DefaultTransport.(*http.Transport).Clone() //nolint
|
|
|
|
insecureTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} //nolint
|
2022-11-06 20:22:21 +01:00
|
|
|
client = &http.Client{Transport: insecureTransport}
|
|
|
|
}
|
|
|
|
|
2022-10-13 16:01:23 +02:00
|
|
|
return t.pool.Retry(func() error {
|
2022-11-06 20:22:21 +01:00
|
|
|
resp, err := client.Get(url) //nolint
|
2022-10-13 16:01:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("headscale is not ready: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return errHeadscaleStatusCodeNotOk
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// CreateUser adds a new user to the Headscale instance.
|
2023-01-17 17:43:44 +01:00
|
|
|
func (t *HeadscaleInContainer) CreateUser(
|
|
|
|
user string,
|
2022-10-13 16:01:23 +02:00
|
|
|
) error {
|
2023-01-17 17:43:44 +01:00
|
|
|
command := []string{"headscale", "users", "create", user}
|
2022-10-13 16:01:23 +02:00
|
|
|
|
|
|
|
_, _, err := dockertestutil.ExecuteCommand(
|
|
|
|
t.container,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// CreateAuthKey creates a new "authorisation key" for a User that can be used
|
|
|
|
// to authorise a TailscaleClient with the Headscale instance.
|
2022-10-13 16:01:23 +02:00
|
|
|
func (t *HeadscaleInContainer) CreateAuthKey(
|
2023-01-17 17:43:44 +01:00
|
|
|
user string,
|
2022-12-27 19:05:21 +00:00
|
|
|
reusable bool,
|
|
|
|
ephemeral bool,
|
2022-10-13 16:01:23 +02:00
|
|
|
) (*v1.PreAuthKey, error) {
|
|
|
|
command := []string{
|
|
|
|
"headscale",
|
2023-01-17 17:43:44 +01:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-13 16:01:23 +02:00
|
|
|
"preauthkeys",
|
|
|
|
"create",
|
|
|
|
"--expiration",
|
|
|
|
"24h",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
}
|
|
|
|
|
2022-12-27 19:05:21 +00:00
|
|
|
if reusable {
|
|
|
|
command = append(command, "--reusable")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ephemeral {
|
|
|
|
command = append(command, "--ephemeral")
|
|
|
|
}
|
|
|
|
|
2022-10-13 16:01:23 +02:00
|
|
|
result, _, err := dockertestutil.ExecuteCommand(
|
|
|
|
t.container,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to execute create auth key command: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var preAuthKey v1.PreAuthKey
|
|
|
|
err = json.Unmarshal([]byte(result), &preAuthKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal auth key: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &preAuthKey, nil
|
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
// ListNodesInUser list the TailscaleClients (Node, Headscale internal representation)
|
2023-02-03 12:24:27 +01:00
|
|
|
// associated with a user.
|
2023-09-24 13:42:05 +02:00
|
|
|
func (t *HeadscaleInContainer) ListNodesInUser(
|
2023-01-17 17:43:44 +01:00
|
|
|
user string,
|
2023-09-24 13:42:05 +02:00
|
|
|
) ([]*v1.Node, error) {
|
2023-01-17 17:43:44 +01:00
|
|
|
command := []string{"headscale", "--user", user, "nodes", "list", "--output", "json"}
|
2022-10-13 16:01:23 +02:00
|
|
|
|
|
|
|
result, _, err := dockertestutil.ExecuteCommand(
|
|
|
|
t.container,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to execute list node command: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-09-24 13:42:05 +02:00
|
|
|
var nodes []*v1.Node
|
2022-10-13 16:01:23 +02:00
|
|
|
err = json.Unmarshal([]byte(result), &nodes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to unmarshal nodes: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes, nil
|
|
|
|
}
|
2022-11-02 09:55:09 +01:00
|
|
|
|
2023-02-03 12:24:27 +01:00
|
|
|
// WriteFile save file inside the Headscale container.
|
2022-11-02 09:55:09 +01:00
|
|
|
func (t *HeadscaleInContainer) WriteFile(path string, data []byte) error {
|
2022-11-06 20:22:21 +01:00
|
|
|
return integrationutil.WriteFileToContainer(t.pool, t.container, path, data)
|
|
|
|
}
|
2022-11-02 09:55:09 +01:00
|
|
|
|
2023-04-27 16:57:11 +02:00
|
|
|
// FetchPath gets a path from inside the Headscale container and returns a tar
|
|
|
|
// file as byte array.
|
|
|
|
func (t *HeadscaleInContainer) FetchPath(path string) ([]byte, error) {
|
|
|
|
return integrationutil.FetchPathFromContainer(t.pool, t.container, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *HeadscaleInContainer) SendInterrupt() error {
|
|
|
|
pid, err := t.Execute([]string{"pidof", "headscale"})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = t.Execute([]string{"kill", "-2", strings.Trim(pid, "'\n")})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-19 10:33:15 +00:00
|
|
|
// nolint
|
2023-04-13 21:10:08 +00:00
|
|
|
func createCertificate(hostname string) ([]byte, []byte, error) {
|
2022-11-06 20:22:21 +01:00
|
|
|
// From:
|
|
|
|
// https://shaneutt.com/blog/golang-ca-and-signed-cert-go/
|
2022-11-02 09:55:09 +01:00
|
|
|
|
2022-11-06 20:22:21 +01:00
|
|
|
ca := &x509.Certificate{
|
|
|
|
SerialNumber: big.NewInt(2019),
|
|
|
|
Subject: pkix.Name{
|
|
|
|
Organization: []string{"Headscale testing INC"},
|
|
|
|
Country: []string{"NL"},
|
|
|
|
Locality: []string{"Leiden"},
|
|
|
|
},
|
|
|
|
NotBefore: time.Now(),
|
2023-04-13 21:10:08 +00:00
|
|
|
NotAfter: time.Now().Add(60 * time.Minute),
|
2022-11-06 20:22:21 +01:00
|
|
|
IsCA: true,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{
|
|
|
|
x509.ExtKeyUsageClientAuth,
|
|
|
|
x509.ExtKeyUsageServerAuth,
|
|
|
|
},
|
|
|
|
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
|
|
|
|
BasicConstraintsValid: true,
|
2022-11-02 09:55:09 +01:00
|
|
|
}
|
|
|
|
|
2022-11-06 20:22:21 +01:00
|
|
|
caPrivKey, err := rsa.GenerateKey(rand.Reader, 4096)
|
2022-11-02 09:55:09 +01:00
|
|
|
if err != nil {
|
2022-11-06 20:22:21 +01:00
|
|
|
return nil, nil, err
|
2022-11-02 09:55:09 +01:00
|
|
|
}
|
|
|
|
|
2022-11-06 20:22:21 +01:00
|
|
|
cert := &x509.Certificate{
|
|
|
|
SerialNumber: big.NewInt(1658),
|
|
|
|
Subject: pkix.Name{
|
2023-04-13 21:10:08 +00:00
|
|
|
CommonName: hostname,
|
2022-11-06 20:22:21 +01:00
|
|
|
Organization: []string{"Headscale testing INC"},
|
|
|
|
Country: []string{"NL"},
|
|
|
|
Locality: []string{"Leiden"},
|
|
|
|
},
|
|
|
|
NotBefore: time.Now(),
|
2023-04-13 21:10:08 +00:00
|
|
|
NotAfter: time.Now().Add(60 * time.Minute),
|
2022-11-06 20:22:21 +01:00
|
|
|
SubjectKeyId: []byte{1, 2, 3, 4, 6},
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
|
|
|
|
KeyUsage: x509.KeyUsageDigitalSignature,
|
2023-04-13 21:10:08 +00:00
|
|
|
DNSNames: []string{hostname},
|
2022-11-06 20:22:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
certPrivKey, err := rsa.GenerateKey(rand.Reader, 4096)
|
2022-11-02 09:55:09 +01:00
|
|
|
if err != nil {
|
2022-11-06 20:22:21 +01:00
|
|
|
return nil, nil, err
|
2022-11-02 09:55:09 +01:00
|
|
|
}
|
|
|
|
|
2022-11-06 20:22:21 +01:00
|
|
|
certBytes, err := x509.CreateCertificate(
|
|
|
|
rand.Reader,
|
|
|
|
cert,
|
|
|
|
ca,
|
|
|
|
&certPrivKey.PublicKey,
|
|
|
|
caPrivKey,
|
|
|
|
)
|
2022-11-02 09:55:09 +01:00
|
|
|
if err != nil {
|
2022-11-06 20:22:21 +01:00
|
|
|
return nil, nil, err
|
2022-11-02 09:55:09 +01:00
|
|
|
}
|
|
|
|
|
2022-11-06 20:22:21 +01:00
|
|
|
certPEM := new(bytes.Buffer)
|
2022-11-02 09:55:09 +01:00
|
|
|
|
2022-11-06 20:22:21 +01:00
|
|
|
err = pem.Encode(certPEM, &pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: certBytes,
|
|
|
|
})
|
2022-11-02 09:55:09 +01:00
|
|
|
if err != nil {
|
2022-11-06 20:22:21 +01:00
|
|
|
return nil, nil, err
|
2022-11-02 09:55:09 +01:00
|
|
|
}
|
|
|
|
|
2022-11-06 20:22:21 +01:00
|
|
|
certPrivKeyPEM := new(bytes.Buffer)
|
|
|
|
|
|
|
|
err = pem.Encode(certPrivKeyPEM, &pem.Block{
|
|
|
|
Type: "RSA PRIVATE KEY",
|
|
|
|
Bytes: x509.MarshalPKCS1PrivateKey(certPrivKey),
|
|
|
|
})
|
2022-11-02 09:55:09 +01:00
|
|
|
if err != nil {
|
2022-11-06 20:22:21 +01:00
|
|
|
return nil, nil, err
|
2022-11-02 09:55:09 +01:00
|
|
|
}
|
|
|
|
|
2022-11-06 20:22:21 +01:00
|
|
|
return certPEM.Bytes(), certPrivKeyPEM.Bytes(), nil
|
2022-11-02 09:55:09 +01:00
|
|
|
}
|