2022-10-13 10:01:23 -04:00
|
|
|
package hsic
|
|
|
|
|
|
|
|
import (
|
2022-11-02 04:55:09 -04:00
|
|
|
"bytes"
|
2022-11-06 14:22:21 -05:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
2022-10-13 10:01:23 -04:00
|
|
|
"encoding/json"
|
2022-11-06 14:22:21 -05:00
|
|
|
"encoding/pem"
|
2022-10-13 10:01:23 -04:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2022-11-06 14:22:21 -05:00
|
|
|
"math/big"
|
|
|
|
"net"
|
2022-10-13 10:01:23 -04:00
|
|
|
"net/http"
|
2023-04-13 17:10:08 -04:00
|
|
|
"net/url"
|
2023-04-27 10:57:11 -04:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2022-11-06 14:22:21 -05:00
|
|
|
"time"
|
2022-10-13 10:01:23 -04:00
|
|
|
|
2023-01-05 06:44:28 -05:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2022-10-13 10:01:23 -04:00
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
2023-05-10 04:26:28 -04:00
|
|
|
"github.com/juanfont/headscale/hscontrol"
|
2022-10-13 10:01:23 -04:00
|
|
|
"github.com/juanfont/headscale/integration/dockertestutil"
|
2022-11-06 14:22:21 -05:00
|
|
|
"github.com/juanfont/headscale/integration/integrationutil"
|
2022-10-13 10:01:23 -04:00
|
|
|
"github.com/ory/dockertest/v3"
|
2023-04-13 17:10:08 -04:00
|
|
|
"github.com/ory/dockertest/v3/docker"
|
2022-10-13 10:01:23 -04:00
|
|
|
)
|
|
|
|
|
2022-10-18 06:09:10 -04:00
|
|
|
const (
|
2022-11-06 14:22:21 -05:00
|
|
|
hsicHashLength = 6
|
|
|
|
dockerContextPath = "../."
|
|
|
|
aclPolicyPath = "/etc/headscale/acl.hujson"
|
|
|
|
tlsCertPath = "/etc/headscale/tls.cert"
|
|
|
|
tlsKeyPath = "/etc/headscale/tls.key"
|
|
|
|
headscaleDefaultPort = 8080
|
2022-10-18 06:09:10 -04:00
|
|
|
)
|
2022-10-13 10:01:23 -04:00
|
|
|
|
|
|
|
var errHeadscaleStatusCodeNotOk = errors.New("headscale status code not ok")
|
|
|
|
|
2023-01-10 06:46:42 -05:00
|
|
|
type fileInContainer struct {
|
|
|
|
path string
|
|
|
|
contents []byte
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// HeadscaleInContainer is an implementation of ControlServer which
|
|
|
|
// sets up a Headscale instance inside a container.
|
2022-10-13 10:01:23 -04:00
|
|
|
type HeadscaleInContainer struct {
|
|
|
|
hostname string
|
|
|
|
|
|
|
|
pool *dockertest.Pool
|
|
|
|
container *dockertest.Resource
|
|
|
|
network *dockertest.Network
|
2022-11-02 06:08:54 -04:00
|
|
|
|
|
|
|
// optional config
|
2023-01-10 06:46:42 -05:00
|
|
|
port int
|
2023-04-13 17:10:08 -04:00
|
|
|
extraPorts []string
|
|
|
|
hostPortBindings map[string][]string
|
2023-05-10 04:26:28 -04:00
|
|
|
aclPolicy *hscontrol.ACLPolicy
|
2023-01-10 06:46:42 -05:00
|
|
|
env map[string]string
|
|
|
|
tlsCert []byte
|
|
|
|
tlsKey []byte
|
|
|
|
filesInContainer []fileInContainer
|
2022-11-02 06:08:54 -04:00
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// Option represent optional settings that can be given to a
|
|
|
|
// Headscale instance.
|
2022-11-02 06:08:54 -04:00
|
|
|
type Option = func(c *HeadscaleInContainer)
|
|
|
|
|
2023-05-10 04:26:28 -04:00
|
|
|
// WithACLPolicy adds a hscontrol.ACLPolicy policy to the
|
2023-02-03 06:24:27 -05:00
|
|
|
// HeadscaleInContainer instance.
|
2023-05-10 04:26:28 -04:00
|
|
|
func WithACLPolicy(acl *hscontrol.ACLPolicy) Option {
|
2022-11-02 06:08:54 -04:00
|
|
|
return func(hsic *HeadscaleInContainer) {
|
2022-11-06 14:22:21 -05:00
|
|
|
// TODO(kradalby): Move somewhere appropriate
|
2023-01-05 06:44:28 -05:00
|
|
|
hsic.env["HEADSCALE_ACL_POLICY_PATH"] = aclPolicyPath
|
2022-11-06 14:22:21 -05:00
|
|
|
|
2022-11-02 06:08:54 -04:00
|
|
|
hsic.aclPolicy = acl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// WithTLS creates certificates and enables HTTPS.
|
2022-11-06 14:22:21 -05:00
|
|
|
func WithTLS() Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
2023-04-13 17:10:08 -04:00
|
|
|
cert, key, err := createCertificate(hsic.hostname)
|
2022-11-06 14:22:21 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to create certificates for headscale test: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(kradalby): Move somewhere appropriate
|
2023-01-05 06:44:28 -05:00
|
|
|
hsic.env["HEADSCALE_TLS_CERT_PATH"] = tlsCertPath
|
|
|
|
hsic.env["HEADSCALE_TLS_KEY_PATH"] = tlsKeyPath
|
2022-11-06 14:22:21 -05:00
|
|
|
|
|
|
|
hsic.tlsCert = cert
|
|
|
|
hsic.tlsKey = key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// WithConfigEnv takes a map of environment variables that
|
|
|
|
// can be used to override Headscale configuration.
|
2022-11-02 06:08:54 -04:00
|
|
|
func WithConfigEnv(configEnv map[string]string) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
for key, value := range configEnv {
|
2023-01-05 06:44:28 -05:00
|
|
|
hsic.env[key] = value
|
2022-11-02 06:08:54 -04:00
|
|
|
}
|
|
|
|
}
|
2022-10-13 10:01:23 -04:00
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// WithPort sets the port on where to run Headscale.
|
2022-11-06 14:22:21 -05:00
|
|
|
func WithPort(port int) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
hsic.port = port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-13 17:10:47 -04:00
|
|
|
// WithExtraPorts exposes additional ports on the container (e.g. 3478/udp for STUN).
|
2023-04-13 17:10:08 -04: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 12:22:47 -05:00
|
|
|
// WithTestName sets a name for the test, this will be reflected
|
2023-02-03 06:24:27 -05:00
|
|
|
// in the Docker container name.
|
2022-11-14 09:01:31 -05:00
|
|
|
func WithTestName(testName string) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
2023-05-10 04:26:28 -04:00
|
|
|
hash, _ := hscontrol.GenerateRandomStringDNSSafe(hsicHashLength)
|
2022-11-14 09:01:31 -05:00
|
|
|
|
|
|
|
hostname := fmt.Sprintf("hs-%s-%s", testName, hash)
|
|
|
|
hsic.hostname = hostname
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// WithHostnameAsServerURL sets the Headscale ServerURL based on
|
|
|
|
// the Hostname.
|
2022-11-21 15:51:54 -05:00
|
|
|
func WithHostnameAsServerURL() Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
2023-01-05 06:44:28 -05:00
|
|
|
hsic.env["HEADSCALE_SERVER_URL"] = fmt.Sprintf("http://%s",
|
|
|
|
net.JoinHostPort(hsic.GetHostname(),
|
|
|
|
fmt.Sprintf("%d", hsic.port)),
|
|
|
|
)
|
2022-11-21 15:51:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// WithFileInContainer adds a file to the container at the given path.
|
2023-01-10 06:46:42 -05:00
|
|
|
func WithFileInContainer(path string, contents []byte) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
hsic.filesInContainer = append(hsic.filesInContainer,
|
|
|
|
fileInContainer{
|
|
|
|
path: path,
|
|
|
|
contents: contents,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// New returns a new HeadscaleInContainer instance.
|
2022-10-13 10:01:23 -04:00
|
|
|
func New(
|
|
|
|
pool *dockertest.Pool,
|
2022-10-18 06:09:10 -04:00
|
|
|
network *dockertest.Network,
|
2022-11-02 06:08:54 -04:00
|
|
|
opts ...Option,
|
2022-10-18 06:09:10 -04:00
|
|
|
) (*HeadscaleInContainer, error) {
|
2023-05-10 04:26:28 -04:00
|
|
|
hash, err := hscontrol.GenerateRandomStringDNSSafe(hsicHashLength)
|
2022-10-13 10:01:23 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-02 06:08:54 -04:00
|
|
|
hostname := fmt.Sprintf("hs-%s", hash)
|
|
|
|
|
|
|
|
hsic := &HeadscaleInContainer{
|
|
|
|
hostname: hostname,
|
2022-11-06 14:22:21 -05:00
|
|
|
port: headscaleDefaultPort,
|
2022-11-02 06:08:54 -04:00
|
|
|
|
|
|
|
pool: pool,
|
|
|
|
network: network,
|
2023-01-05 06:44:28 -05:00
|
|
|
|
2023-01-10 06:46:42 -05:00
|
|
|
env: DefaultConfigEnv(),
|
|
|
|
filesInContainer: []fileInContainer{},
|
2022-11-02 06:08:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(hsic)
|
|
|
|
}
|
|
|
|
|
2022-11-14 09:01:31 -05:00
|
|
|
log.Println("NAME: ", hsic.hostname)
|
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
portProto := fmt.Sprintf("%d/tcp", hsic.port)
|
2022-11-02 06:08:54 -04:00
|
|
|
|
2023-04-13 17:10:47 -04:00
|
|
|
serverURL, err := url.Parse(hsic.env["HEADSCALE_SERVER_URL"])
|
2023-04-13 17:10:08 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(hsic.tlsCert) != 0 && len(hsic.tlsKey) != 0 {
|
2023-04-13 17:10:47 -04:00
|
|
|
serverURL.Scheme = "https"
|
|
|
|
hsic.env["HEADSCALE_SERVER_URL"] = serverURL.String()
|
2023-04-13 17:10:08 -04:00
|
|
|
}
|
|
|
|
|
2022-10-13 10:01:23 -04:00
|
|
|
headscaleBuildOptions := &dockertest.BuildOptions{
|
2022-11-02 04:53:55 -04:00
|
|
|
Dockerfile: "Dockerfile.debug",
|
2022-10-13 10:01:23 -04:00
|
|
|
ContextDir: dockerContextPath,
|
|
|
|
}
|
|
|
|
|
2023-04-27 10:57:11 -04:00
|
|
|
env := []string{
|
|
|
|
"HEADSCALE_PROFILING_ENABLED=1",
|
|
|
|
"HEADSCALE_PROFILING_PATH=/tmp/profile",
|
|
|
|
}
|
2023-01-05 06:44:28 -05: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 10:01:23 -04:00
|
|
|
runOptions := &dockertest.RunOptions{
|
2022-11-14 09:01:31 -05:00
|
|
|
Name: hsic.hostname,
|
2023-04-13 17:10:08 -04:00
|
|
|
ExposedPorts: append([]string{portProto}, hsic.extraPorts...),
|
2022-11-02 04:55:48 -04: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 10:57:11 -04:00
|
|
|
Entrypoint: []string{"/bin/bash", "-c", "/bin/sleep 3 ; headscale serve ; /bin/sleep 30"},
|
2023-01-05 06:44:28 -05:00
|
|
|
Env: env,
|
2022-10-13 10:01:23 -04:00
|
|
|
}
|
|
|
|
|
2023-04-13 17:10:08 -04: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 10:01:23 -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.
|
2022-11-14 09:01:31 -05:00
|
|
|
err = pool.RemoveContainerByName(hsic.hostname)
|
2022-10-13 10:01:23 -04: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 09:01:31 -05:00
|
|
|
log.Printf("Created %s container\n", hsic.hostname)
|
2022-10-13 10:01:23 -04:00
|
|
|
|
2022-11-02 06:08:54 -04:00
|
|
|
hsic.container = container
|
2022-11-02 04:55:48 -04:00
|
|
|
|
2023-01-05 06:44:28 -05:00
|
|
|
err = hsic.WriteFile("/etc/headscale/config.yaml", []byte(MinimumConfigYAML()))
|
2022-11-02 04:55:48 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to write headscale config to container: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-02 06:08:54 -04: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 14:22:21 -05: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 06:46:42 -05: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 04:55:48 -04:00
|
|
|
return hsic, nil
|
2022-10-13 10:01:23 -04:00
|
|
|
}
|
|
|
|
|
2023-04-23 07:02:28 -04:00
|
|
|
func (t *HeadscaleInContainer) ConnectToNetwork(network *dockertest.Network) error {
|
|
|
|
return t.container.ConnectToNetwork(network)
|
|
|
|
}
|
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
func (t *HeadscaleInContainer) hasTLS() bool {
|
|
|
|
return len(t.tlsCert) != 0 && len(t.tlsKey) != 0
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// Shutdown stops and cleans up the Headscale container.
|
2022-10-13 10:01:23 -04:00
|
|
|
func (t *HeadscaleInContainer) Shutdown() error {
|
2023-04-27 10:57:11 -04: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),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:01:23 -04:00
|
|
|
return t.pool.Purge(t.container)
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// SaveLog saves the current stdout log of the container to a path
|
|
|
|
// on the host system.
|
2023-01-30 04:20:08 -05:00
|
|
|
func (t *HeadscaleInContainer) SaveLog(path string) error {
|
|
|
|
return dockertestutil.SaveLog(t.pool, t.container, path)
|
|
|
|
}
|
|
|
|
|
2023-04-27 10:57:11 -04:00
|
|
|
func (t *HeadscaleInContainer) SaveProfile(savePath string) error {
|
|
|
|
tarFile, err := t.FetchPath("/tmp/profile")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.WriteFile(
|
|
|
|
path.Join(savePath, t.hostname+".pprof.tar"),
|
|
|
|
tarFile,
|
|
|
|
os.ModePerm,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// Execute runs a command inside the Headscale container and returns the
|
|
|
|
// result of stdout as a string.
|
2022-10-24 10:40:49 -04: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 03:56:54 -05:00
|
|
|
if stdout != "" {
|
|
|
|
log.Printf("command stdout: %s\n", stdout)
|
|
|
|
}
|
2022-10-24 10:40:49 -04:00
|
|
|
|
2022-11-14 03:56:54 -05:00
|
|
|
return "", err
|
2022-10-24 10:40:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return stdout, nil
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// GetIP returns the docker container IP as a string.
|
2022-10-13 10:01:23 -04:00
|
|
|
func (t *HeadscaleInContainer) GetIP() string {
|
|
|
|
return t.container.GetIPInNetwork(t.network)
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// GetPort returns the docker container port as a string.
|
2022-10-13 10:01:23 -04:00
|
|
|
func (t *HeadscaleInContainer) GetPort() string {
|
2022-11-03 19:05:01 -04:00
|
|
|
return fmt.Sprintf("%d", t.port)
|
2022-10-13 10:01:23 -04:00
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// GetHealthEndpoint returns a health endpoint for the HeadscaleInContainer
|
|
|
|
// instance.
|
2022-10-13 10:01:23 -04:00
|
|
|
func (t *HeadscaleInContainer) GetHealthEndpoint() string {
|
2022-11-06 14:22:21 -05:00
|
|
|
return fmt.Sprintf("%s/health", t.GetEndpoint())
|
2022-10-13 10:01:23 -04:00
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// GetEndpoint returns the Headscale endpoint for the HeadscaleInContainer.
|
2022-10-13 10:01:23 -04:00
|
|
|
func (t *HeadscaleInContainer) GetEndpoint() string {
|
2022-10-18 05:58:15 -04:00
|
|
|
hostEndpoint := fmt.Sprintf("%s:%d",
|
2022-10-13 10:01:23 -04:00
|
|
|
t.GetIP(),
|
2022-10-18 05:58:15 -04:00
|
|
|
t.port)
|
2022-10-13 10:01:23 -04:00
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
if t.hasTLS() {
|
|
|
|
return fmt.Sprintf("https://%s", hostEndpoint)
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:01:23 -04:00
|
|
|
return fmt.Sprintf("http://%s", hostEndpoint)
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// GetCert returns the public certificate of the HeadscaleInContainer.
|
2022-11-06 14:22:21 -05:00
|
|
|
func (t *HeadscaleInContainer) GetCert() []byte {
|
|
|
|
return t.tlsCert
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// GetHostname returns the hostname of the HeadscaleInContainer.
|
2022-11-06 14:22:21 -05:00
|
|
|
func (t *HeadscaleInContainer) GetHostname() string {
|
|
|
|
return t.hostname
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// WaitForReady blocks until the Headscale instance is ready to
|
|
|
|
// serve clients.
|
2022-10-13 10:01:23 -04:00
|
|
|
func (t *HeadscaleInContainer) WaitForReady() error {
|
|
|
|
url := t.GetHealthEndpoint()
|
|
|
|
|
2022-10-18 05:58:15 -04:00
|
|
|
log.Printf("waiting for headscale to be ready at %s", url)
|
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
client := &http.Client{}
|
|
|
|
|
|
|
|
if t.hasTLS() {
|
2022-11-10 03:04:47 -05:00
|
|
|
insecureTransport := http.DefaultTransport.(*http.Transport).Clone() //nolint
|
|
|
|
insecureTransport.TLSClientConfig = &tls.Config{InsecureSkipVerify: true} //nolint
|
2022-11-06 14:22:21 -05:00
|
|
|
client = &http.Client{Transport: insecureTransport}
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:01:23 -04:00
|
|
|
return t.pool.Retry(func() error {
|
2022-11-06 14:22:21 -05:00
|
|
|
resp, err := client.Get(url) //nolint
|
2022-10-13 10:01:23 -04: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 06:24:27 -05:00
|
|
|
// CreateUser adds a new user to the Headscale instance.
|
2023-01-17 11:43:44 -05:00
|
|
|
func (t *HeadscaleInContainer) CreateUser(
|
|
|
|
user string,
|
2022-10-13 10:01:23 -04:00
|
|
|
) error {
|
2023-01-17 11:43:44 -05:00
|
|
|
command := []string{"headscale", "users", "create", user}
|
2022-10-13 10:01:23 -04:00
|
|
|
|
|
|
|
_, _, err := dockertestutil.ExecuteCommand(
|
|
|
|
t.container,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-03 06:24:27 -05: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 10:01:23 -04:00
|
|
|
func (t *HeadscaleInContainer) CreateAuthKey(
|
2023-01-17 11:43:44 -05:00
|
|
|
user string,
|
2022-12-27 14:05:21 -05:00
|
|
|
reusable bool,
|
|
|
|
ephemeral bool,
|
2022-10-13 10:01:23 -04:00
|
|
|
) (*v1.PreAuthKey, error) {
|
|
|
|
command := []string{
|
|
|
|
"headscale",
|
2023-01-17 11:43:44 -05:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-13 10:01:23 -04:00
|
|
|
"preauthkeys",
|
|
|
|
"create",
|
|
|
|
"--expiration",
|
|
|
|
"24h",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
}
|
|
|
|
|
2022-12-27 14:05:21 -05:00
|
|
|
if reusable {
|
|
|
|
command = append(command, "--reusable")
|
|
|
|
}
|
|
|
|
|
|
|
|
if ephemeral {
|
|
|
|
command = append(command, "--ephemeral")
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:01:23 -04: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-02-03 06:24:27 -05:00
|
|
|
// ListMachinesInUser list the TailscaleClients (Machine, Headscale internal representation)
|
|
|
|
// associated with a user.
|
2023-01-17 11:43:44 -05:00
|
|
|
func (t *HeadscaleInContainer) ListMachinesInUser(
|
|
|
|
user string,
|
2022-10-13 10:01:23 -04:00
|
|
|
) ([]*v1.Machine, error) {
|
2023-01-17 11:43:44 -05:00
|
|
|
command := []string{"headscale", "--user", user, "nodes", "list", "--output", "json"}
|
2022-10-13 10:01:23 -04:00
|
|
|
|
|
|
|
result, _, err := dockertestutil.ExecuteCommand(
|
|
|
|
t.container,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to execute list node command: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var nodes []*v1.Machine
|
|
|
|
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 04:55:09 -04:00
|
|
|
|
2023-02-03 06:24:27 -05:00
|
|
|
// WriteFile save file inside the Headscale container.
|
2022-11-02 04:55:09 -04:00
|
|
|
func (t *HeadscaleInContainer) WriteFile(path string, data []byte) error {
|
2022-11-06 14:22:21 -05:00
|
|
|
return integrationutil.WriteFileToContainer(t.pool, t.container, path, data)
|
|
|
|
}
|
2022-11-02 04:55:09 -04:00
|
|
|
|
2023-04-27 10:57:11 -04: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 05:33:15 -05:00
|
|
|
// nolint
|
2023-04-13 17:10:08 -04:00
|
|
|
func createCertificate(hostname string) ([]byte, []byte, error) {
|
2022-11-06 14:22:21 -05:00
|
|
|
// From:
|
|
|
|
// https://shaneutt.com/blog/golang-ca-and-signed-cert-go/
|
2022-11-02 04:55:09 -04:00
|
|
|
|
2022-11-06 14:22:21 -05: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 17:10:08 -04:00
|
|
|
NotAfter: time.Now().Add(60 * time.Minute),
|
2022-11-06 14:22:21 -05:00
|
|
|
IsCA: true,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{
|
|
|
|
x509.ExtKeyUsageClientAuth,
|
|
|
|
x509.ExtKeyUsageServerAuth,
|
|
|
|
},
|
|
|
|
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
|
|
|
|
BasicConstraintsValid: true,
|
2022-11-02 04:55:09 -04:00
|
|
|
}
|
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
caPrivKey, err := rsa.GenerateKey(rand.Reader, 4096)
|
2022-11-02 04:55:09 -04:00
|
|
|
if err != nil {
|
2022-11-06 14:22:21 -05:00
|
|
|
return nil, nil, err
|
2022-11-02 04:55:09 -04:00
|
|
|
}
|
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
cert := &x509.Certificate{
|
|
|
|
SerialNumber: big.NewInt(1658),
|
|
|
|
Subject: pkix.Name{
|
2023-04-13 17:10:08 -04:00
|
|
|
CommonName: hostname,
|
2022-11-06 14:22:21 -05:00
|
|
|
Organization: []string{"Headscale testing INC"},
|
|
|
|
Country: []string{"NL"},
|
|
|
|
Locality: []string{"Leiden"},
|
|
|
|
},
|
|
|
|
NotBefore: time.Now(),
|
2023-04-13 17:10:08 -04:00
|
|
|
NotAfter: time.Now().Add(60 * time.Minute),
|
2022-11-06 14:22:21 -05:00
|
|
|
SubjectKeyId: []byte{1, 2, 3, 4, 6},
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
|
|
|
|
KeyUsage: x509.KeyUsageDigitalSignature,
|
2023-04-13 17:10:08 -04:00
|
|
|
DNSNames: []string{hostname},
|
2022-11-06 14:22:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
certPrivKey, err := rsa.GenerateKey(rand.Reader, 4096)
|
2022-11-02 04:55:09 -04:00
|
|
|
if err != nil {
|
2022-11-06 14:22:21 -05:00
|
|
|
return nil, nil, err
|
2022-11-02 04:55:09 -04:00
|
|
|
}
|
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
certBytes, err := x509.CreateCertificate(
|
|
|
|
rand.Reader,
|
|
|
|
cert,
|
|
|
|
ca,
|
|
|
|
&certPrivKey.PublicKey,
|
|
|
|
caPrivKey,
|
|
|
|
)
|
2022-11-02 04:55:09 -04:00
|
|
|
if err != nil {
|
2022-11-06 14:22:21 -05:00
|
|
|
return nil, nil, err
|
2022-11-02 04:55:09 -04:00
|
|
|
}
|
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
certPEM := new(bytes.Buffer)
|
2022-11-02 04:55:09 -04:00
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
err = pem.Encode(certPEM, &pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: certBytes,
|
|
|
|
})
|
2022-11-02 04:55:09 -04:00
|
|
|
if err != nil {
|
2022-11-06 14:22:21 -05:00
|
|
|
return nil, nil, err
|
2022-11-02 04:55:09 -04:00
|
|
|
}
|
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
certPrivKeyPEM := new(bytes.Buffer)
|
|
|
|
|
|
|
|
err = pem.Encode(certPrivKeyPEM, &pem.Block{
|
|
|
|
Type: "RSA PRIVATE KEY",
|
|
|
|
Bytes: x509.MarshalPKCS1PrivateKey(certPrivKey),
|
|
|
|
})
|
2022-11-02 04:55:09 -04:00
|
|
|
if err != nil {
|
2022-11-06 14:22:21 -05:00
|
|
|
return nil, nil, err
|
2022-11-02 04:55:09 -04:00
|
|
|
}
|
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
return certPEM.Bytes(), certPrivKeyPEM.Bytes(), nil
|
2022-11-02 04:55:09 -04:00
|
|
|
}
|