2022-10-13 14:01:23 +00:00
|
|
|
package hsic
|
|
|
|
|
|
|
|
import (
|
2022-11-02 08:55:09 +00:00
|
|
|
"bytes"
|
2022-11-06 19:22:21 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
2022-10-13 14:01:23 +00:00
|
|
|
"encoding/json"
|
2022-11-06 19:22:21 +00:00
|
|
|
"encoding/pem"
|
2022-10-13 14:01:23 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-04-27 08:47:39 +00:00
|
|
|
"io"
|
2022-10-13 14:01:23 +00:00
|
|
|
"log"
|
2022-11-06 19:22:21 +00:00
|
|
|
"math/big"
|
|
|
|
"net"
|
2022-10-13 14:01:23 +00:00
|
|
|
"net/http"
|
2023-04-13 21:10:08 +00:00
|
|
|
"net/url"
|
2023-04-27 14:57:11 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2024-04-21 16:28:17 +00:00
|
|
|
"strconv"
|
2023-04-27 14:57:11 +00:00
|
|
|
"strings"
|
2022-11-06 19:22:21 +00:00
|
|
|
"time"
|
2022-10-13 14:01:23 +00:00
|
|
|
|
2023-01-05 11:44:28 +00:00
|
|
|
"github.com/davecgh/go-spew/spew"
|
2022-10-13 14:01:23 +00:00
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
2023-05-21 16:37:59 +00:00
|
|
|
"github.com/juanfont/headscale/hscontrol/policy"
|
2024-04-17 05:03:06 +00:00
|
|
|
"github.com/juanfont/headscale/hscontrol/types"
|
2023-05-11 07:09:18 +00:00
|
|
|
"github.com/juanfont/headscale/hscontrol/util"
|
2022-10-13 14:01:23 +00:00
|
|
|
"github.com/juanfont/headscale/integration/dockertestutil"
|
2022-11-06 19:22:21 +00:00
|
|
|
"github.com/juanfont/headscale/integration/integrationutil"
|
2022-10-13 14:01:23 +00:00
|
|
|
"github.com/ory/dockertest/v3"
|
2023-04-13 21:10:08 +00:00
|
|
|
"github.com/ory/dockertest/v3/docker"
|
2022-10-13 14:01:23 +00:00
|
|
|
)
|
|
|
|
|
2022-10-18 10:09:10 +00:00
|
|
|
const (
|
2022-11-06 19:22:21 +00:00
|
|
|
hsicHashLength = 6
|
|
|
|
dockerContextPath = "../."
|
|
|
|
aclPolicyPath = "/etc/headscale/acl.hujson"
|
|
|
|
tlsCertPath = "/etc/headscale/tls.cert"
|
|
|
|
tlsKeyPath = "/etc/headscale/tls.key"
|
|
|
|
headscaleDefaultPort = 8080
|
2022-10-18 10:09:10 +00:00
|
|
|
)
|
2022-10-13 14:01:23 +00:00
|
|
|
|
|
|
|
var errHeadscaleStatusCodeNotOk = errors.New("headscale status code not ok")
|
|
|
|
|
2023-01-10 11:46:42 +00:00
|
|
|
type fileInContainer struct {
|
|
|
|
path string
|
|
|
|
contents []byte
|
|
|
|
}
|
|
|
|
|
2023-02-03 11:24:27 +00:00
|
|
|
// HeadscaleInContainer is an implementation of ControlServer which
|
|
|
|
// sets up a Headscale instance inside a container.
|
2022-10-13 14:01:23 +00:00
|
|
|
type HeadscaleInContainer struct {
|
|
|
|
hostname string
|
|
|
|
|
|
|
|
pool *dockertest.Pool
|
|
|
|
container *dockertest.Resource
|
|
|
|
network *dockertest.Network
|
2022-11-02 10:08:54 +00:00
|
|
|
|
2024-02-18 18:31:29 +00:00
|
|
|
pgContainer *dockertest.Resource
|
|
|
|
|
2022-11-02 10:08:54 +00:00
|
|
|
// optional config
|
2023-01-10 11:46:42 +00:00
|
|
|
port int
|
2023-04-13 21:10:08 +00:00
|
|
|
extraPorts []string
|
|
|
|
hostPortBindings map[string][]string
|
2023-05-21 16:37:59 +00:00
|
|
|
aclPolicy *policy.ACLPolicy
|
2023-01-10 11:46:42 +00:00
|
|
|
env map[string]string
|
|
|
|
tlsCert []byte
|
|
|
|
tlsKey []byte
|
|
|
|
filesInContainer []fileInContainer
|
2024-02-18 18:31:29 +00:00
|
|
|
postgres bool
|
2022-11-02 10:08:54 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 11:24:27 +00:00
|
|
|
// Option represent optional settings that can be given to a
|
|
|
|
// Headscale instance.
|
2022-11-02 10:08:54 +00:00
|
|
|
type Option = func(c *HeadscaleInContainer)
|
|
|
|
|
2023-05-10 08:26:28 +00:00
|
|
|
// WithACLPolicy adds a hscontrol.ACLPolicy policy to the
|
2023-02-03 11:24:27 +00:00
|
|
|
// HeadscaleInContainer instance.
|
2023-05-21 16:37:59 +00:00
|
|
|
func WithACLPolicy(acl *policy.ACLPolicy) Option {
|
2022-11-02 10:08:54 +00:00
|
|
|
return func(hsic *HeadscaleInContainer) {
|
2022-11-06 19:22:21 +00:00
|
|
|
// TODO(kradalby): Move somewhere appropriate
|
2023-01-05 11:44:28 +00:00
|
|
|
hsic.env["HEADSCALE_ACL_POLICY_PATH"] = aclPolicyPath
|
2022-11-06 19:22:21 +00:00
|
|
|
|
2022-11-02 10:08:54 +00:00
|
|
|
hsic.aclPolicy = acl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 11:24:27 +00:00
|
|
|
// WithTLS creates certificates and enables HTTPS.
|
2022-11-06 19:22:21 +00: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 19:22:21 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to create certificates for headscale test: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(kradalby): Move somewhere appropriate
|
2023-01-05 11:44:28 +00:00
|
|
|
hsic.env["HEADSCALE_TLS_CERT_PATH"] = tlsCertPath
|
|
|
|
hsic.env["HEADSCALE_TLS_KEY_PATH"] = tlsKeyPath
|
2022-11-06 19:22:21 +00:00
|
|
|
|
|
|
|
hsic.tlsCert = cert
|
|
|
|
hsic.tlsKey = key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 11:24:27 +00:00
|
|
|
// WithConfigEnv takes a map of environment variables that
|
|
|
|
// can be used to override Headscale configuration.
|
2022-11-02 10:08:54 +00:00
|
|
|
func WithConfigEnv(configEnv map[string]string) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
for key, value := range configEnv {
|
2023-01-05 11:44:28 +00:00
|
|
|
hsic.env[key] = value
|
2022-11-02 10:08:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-13 14:01:23 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 11:24:27 +00:00
|
|
|
// WithPort sets the port on where to run Headscale.
|
2022-11-06 19:22:21 +00: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 17:22:47 +00:00
|
|
|
// WithTestName sets a name for the test, this will be reflected
|
2023-02-03 11:24:27 +00:00
|
|
|
// in the Docker container name.
|
2022-11-14 14:01:31 +00:00
|
|
|
func WithTestName(testName string) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
2023-05-11 07:09:18 +00:00
|
|
|
hash, _ := util.GenerateRandomStringDNSSafe(hsicHashLength)
|
2022-11-14 14:01:31 +00:00
|
|
|
|
|
|
|
hostname := fmt.Sprintf("hs-%s-%s", testName, hash)
|
|
|
|
hsic.hostname = hostname
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 11:24:27 +00:00
|
|
|
// WithHostnameAsServerURL sets the Headscale ServerURL based on
|
|
|
|
// the Hostname.
|
2022-11-21 20:51:54 +00:00
|
|
|
func WithHostnameAsServerURL() Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
2023-01-05 11:44:28 +00:00
|
|
|
hsic.env["HEADSCALE_SERVER_URL"] = fmt.Sprintf("http://%s",
|
|
|
|
net.JoinHostPort(hsic.GetHostname(),
|
|
|
|
fmt.Sprintf("%d", hsic.port)),
|
|
|
|
)
|
2022-11-21 20:51:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 11:24:27 +00:00
|
|
|
// WithFileInContainer adds a file to the container at the given path.
|
2023-01-10 11:46:42 +00: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 18:31:29 +00: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 05:03:06 +00:00
|
|
|
// WithIPAllocationStrategy sets the tests IP Allocation strategy.
|
2024-07-22 06:56:00 +00:00
|
|
|
func WithIPAllocationStrategy(strategy types.IPAllocationStrategy) Option {
|
2024-04-17 05:03:06 +00:00
|
|
|
return func(hsic *HeadscaleInContainer) {
|
2024-07-22 06:56:00 +00:00
|
|
|
hsic.env["HEADSCALE_PREFIXES_ALLOCATION"] = string(strategy)
|
2024-04-17 05:03:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-16 19:37:25 +00: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"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-21 16:28:17 +00:00
|
|
|
// WithTuning allows changing the tuning settings easily.
|
|
|
|
func WithTuning(batchTimeout time.Duration, mapSessionChanSize int) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
hsic.env["HEADSCALE_TUNING_BATCH_CHANGE_DELAY"] = batchTimeout.String()
|
|
|
|
hsic.env["HEADSCALE_TUNING_NODE_MAPSESSION_BUFFERED_CHAN_SIZE"] = strconv.Itoa(mapSessionChanSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-03 11:24:27 +00:00
|
|
|
// New returns a new HeadscaleInContainer instance.
|
2022-10-13 14:01:23 +00:00
|
|
|
func New(
|
|
|
|
pool *dockertest.Pool,
|
2022-10-18 10:09:10 +00:00
|
|
|
network *dockertest.Network,
|
2022-11-02 10:08:54 +00:00
|
|
|
opts ...Option,
|
2022-10-18 10:09:10 +00:00
|
|
|
) (*HeadscaleInContainer, error) {
|
2023-05-11 07:09:18 +00:00
|
|
|
hash, err := util.GenerateRandomStringDNSSafe(hsicHashLength)
|
2022-10-13 14:01:23 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-11-02 10:08:54 +00:00
|
|
|
hostname := fmt.Sprintf("hs-%s", hash)
|
|
|
|
|
|
|
|
hsic := &HeadscaleInContainer{
|
|
|
|
hostname: hostname,
|
2022-11-06 19:22:21 +00:00
|
|
|
port: headscaleDefaultPort,
|
2022-11-02 10:08:54 +00:00
|
|
|
|
|
|
|
pool: pool,
|
|
|
|
network: network,
|
2023-01-05 11:44:28 +00:00
|
|
|
|
2023-01-10 11:46:42 +00:00
|
|
|
env: DefaultConfigEnv(),
|
|
|
|
filesInContainer: []fileInContainer{},
|
2022-11-02 10:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(hsic)
|
|
|
|
}
|
|
|
|
|
2022-11-14 14:01:31 +00:00
|
|
|
log.Println("NAME: ", hsic.hostname)
|
|
|
|
|
2022-11-06 19:22:21 +00:00
|
|
|
portProto := fmt.Sprintf("%d/tcp", hsic.port)
|
2022-11-02 10:08:54 +00: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 14:01:23 +00:00
|
|
|
headscaleBuildOptions := &dockertest.BuildOptions{
|
2022-11-02 08:53:55 +00:00
|
|
|
Dockerfile: "Dockerfile.debug",
|
2022-10-13 14:01:23 +00:00
|
|
|
ContextDir: dockerContextPath,
|
|
|
|
}
|
|
|
|
|
2024-02-18 18:31:29 +00: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 14:57:11 +00:00
|
|
|
env := []string{
|
2024-05-24 08:15:34 +00:00
|
|
|
"HEADSCALE_DEBUG_PROFILING_ENABLED=1",
|
|
|
|
"HEADSCALE_DEBUG_PROFILING_PATH=/tmp/profile",
|
2023-07-17 09:13:48 +00:00
|
|
|
"HEADSCALE_DEBUG_DUMP_MAPRESPONSE_PATH=/tmp/mapresponses",
|
2024-05-24 08:15:34 +00:00
|
|
|
"HEADSCALE_DEBUG_DEADLOCK=1",
|
|
|
|
"HEADSCALE_DEBUG_DEADLOCK_TIMEOUT=5s",
|
|
|
|
"HEADSCALE_DEBUG_HIGH_CARDINALITY_METRICS=1",
|
|
|
|
"HEADSCALE_DEBUG_DUMP_CONFIG=1",
|
2023-04-27 14:57:11 +00:00
|
|
|
}
|
2023-01-05 11:44:28 +00: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 14:01:23 +00:00
|
|
|
runOptions := &dockertest.RunOptions{
|
2022-11-14 14:01:31 +00:00
|
|
|
Name: hsic.hostname,
|
2024-04-10 13:35:09 +00:00
|
|
|
ExposedPorts: append([]string{portProto, "9090/tcp"}, hsic.extraPorts...),
|
2022-11-02 08:55:48 +00: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 14:57:11 +00:00
|
|
|
Entrypoint: []string{"/bin/bash", "-c", "/bin/sleep 3 ; headscale serve ; /bin/sleep 30"},
|
2023-01-05 11:44:28 +00:00
|
|
|
Env: env,
|
2022-10-13 14:01:23 +00: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 14:01:23 +00: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 14:01:31 +00:00
|
|
|
err = pool.RemoveContainerByName(hsic.hostname)
|
2022-10-13 14:01:23 +00: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 14:01:31 +00:00
|
|
|
log.Printf("Created %s container\n", hsic.hostname)
|
2022-10-13 14:01:23 +00:00
|
|
|
|
2022-11-02 10:08:54 +00:00
|
|
|
hsic.container = container
|
2022-11-02 08:55:48 +00:00
|
|
|
|
2023-01-05 11:44:28 +00:00
|
|
|
err = hsic.WriteFile("/etc/headscale/config.yaml", []byte(MinimumConfigYAML()))
|
2022-11-02 08:55:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to write headscale config to container: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-02 10:08:54 +00: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 19:22:21 +00: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 11:46:42 +00: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 08:55:48 +00:00
|
|
|
return hsic, nil
|
2022-10-13 14:01:23 +00: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 19:22:21 +00:00
|
|
|
func (t *HeadscaleInContainer) hasTLS() bool {
|
|
|
|
return len(t.tlsCert) != 0 && len(t.tlsKey) != 0
|
|
|
|
}
|
|
|
|
|
2023-02-03 11:24:27 +00:00
|
|
|
// Shutdown stops and cleans up the Headscale container.
|
2022-10-13 14:01:23 +00:00
|
|
|
func (t *HeadscaleInContainer) Shutdown() error {
|
2023-04-27 14:57:11 +00: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),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-05-24 08:15:34 +00:00
|
|
|
err = t.SaveMetrics(fmt.Sprintf("/tmp/control/%s_metrics.txt", t.hostname))
|
2024-04-27 08:47:39 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf(
|
|
|
|
"Failed to metrics from control: %s",
|
|
|
|
err,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2023-04-27 14:57:11 +00:00
|
|
|
// 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 09:13:48 +00: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 18:31:29 +00: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 16:55:29 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 14:01:23 +00:00
|
|
|
return t.pool.Purge(t.container)
|
|
|
|
}
|
|
|
|
|
2023-02-03 11:24:27 +00:00
|
|
|
// SaveLog saves the current stdout log of the container to a path
|
|
|
|
// on the host system.
|
2023-01-30 09:20:08 +00:00
|
|
|
func (t *HeadscaleInContainer) SaveLog(path string) error {
|
|
|
|
return dockertestutil.SaveLog(t.pool, t.container, path)
|
|
|
|
}
|
|
|
|
|
2024-04-27 08:47:39 +00:00
|
|
|
func (t *HeadscaleInContainer) SaveMetrics(savePath string) error {
|
|
|
|
resp, err := http.Get(fmt.Sprintf("http://%s:9090/metrics", t.hostname))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("getting metrics: %w", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
out, err := os.Create(savePath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("creating file for metrics: %w", err)
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
_, err = io.Copy(out, resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("copy response to file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-27 14:57:11 +00:00
|
|
|
func (t *HeadscaleInContainer) SaveProfile(savePath string) error {
|
|
|
|
tarFile, err := t.FetchPath("/tmp/profile")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-17 09:13:48 +00:00
|
|
|
err = os.WriteFile(
|
2023-07-24 06:58:51 +00:00
|
|
|
path.Join(savePath, t.hostname+".pprof.tar"),
|
2023-07-17 09:13:48 +00: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 14:57:11 +00:00
|
|
|
err = os.WriteFile(
|
2023-07-24 06:58:51 +00:00
|
|
|
path.Join(savePath, t.hostname+".maps.tar"),
|
2023-04-27 14:57:11 +00:00
|
|
|
tarFile,
|
|
|
|
os.ModePerm,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-16 16:55:29 +00: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 11:24:27 +00:00
|
|
|
// Execute runs a command inside the Headscale container and returns the
|
|
|
|
// result of stdout as a string.
|
2022-10-24 14:40:49 +00: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 08:56:54 +00:00
|
|
|
if stdout != "" {
|
|
|
|
log.Printf("command stdout: %s\n", stdout)
|
|
|
|
}
|
2022-10-24 14:40:49 +00:00
|
|
|
|
2022-11-14 08:56:54 +00:00
|
|
|
return "", err
|
2022-10-24 14:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return stdout, nil
|
|
|
|
}
|
|
|
|
|
2023-02-03 11:24:27 +00:00
|
|
|
// GetIP returns the docker container IP as a string.
|
2022-10-13 14:01:23 +00:00
|
|
|
func (t *HeadscaleInContainer) GetIP() string {
|
|
|
|
return t.container.GetIPInNetwork(t.network)
|
|
|
|
}
|
|
|
|
|
2023-02-03 11:24:27 +00:00
|
|
|
// GetPort returns the docker container port as a string.
|
2022-10-13 14:01:23 +00:00
|
|
|
func (t *HeadscaleInContainer) GetPort() string {
|
2022-11-03 23:05:01 +00:00
|
|
|
return fmt.Sprintf("%d", t.port)
|
2022-10-13 14:01:23 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 11:24:27 +00:00
|
|
|
// GetHealthEndpoint returns a health endpoint for the HeadscaleInContainer
|
|
|
|
// instance.
|
2022-10-13 14:01:23 +00:00
|
|
|
func (t *HeadscaleInContainer) GetHealthEndpoint() string {
|
2022-11-06 19:22:21 +00:00
|
|
|
return fmt.Sprintf("%s/health", t.GetEndpoint())
|
2022-10-13 14:01:23 +00:00
|
|
|
}
|
|
|
|
|
2023-02-03 11:24:27 +00:00
|
|
|
// GetEndpoint returns the Headscale endpoint for the HeadscaleInContainer.
|
2022-10-13 14:01:23 +00:00
|
|
|
func (t *HeadscaleInContainer) GetEndpoint() string {
|
2022-10-18 09:58:15 +00:00
|
|
|
hostEndpoint := fmt.Sprintf("%s:%d",
|
2022-10-13 14:01:23 +00:00
|
|
|
t.GetIP(),
|
2022-10-18 09:58:15 +00:00
|
|
|
t.port)
|
2022-10-13 14:01:23 +00:00
|
|
|
|
2022-11-06 19:22:21 +00:00
|
|
|
if t.hasTLS() {
|
|
|
|
return fmt.Sprintf("https://%s", hostEndpoint)
|
|
|
|
}
|
|
|
|
|
2022-10-13 14:01:23 +00:00
|
|
|
return fmt.Sprintf("http://%s", hostEndpoint)
|
|
|
|
}
|
|
|
|
|
2023-02-03 11:24:27 +00:00
|
|
|
// GetCert returns the public certificate of the HeadscaleInContainer.
|
2022-11-06 19:22:21 +00:00
|
|
|
func (t *HeadscaleInContainer) GetCert() []byte {
|
|
|
|
return t.tlsCert
|
|
|
|
}
|
|
|
|
|
2023-02-03 11:24:27 +00:00
|
|
|
// GetHostname returns the hostname of the HeadscaleInContainer.
|
2022-11-06 19:22:21 +00:00
|
|
|
func (t *HeadscaleInContainer) GetHostname() string {
|
|
|
|
return t.hostname
|
|
|
|
}
|
|
|
|
|
2023-08-29 06:33:33 +00:00
|
|
|
// WaitForRunning blocks until the Headscale instance is ready to
|
2023-02-03 11:24:27 +00:00
|
|
|
// serve clients.
|
2023-08-29 06:33:33 +00:00
|
|
|
func (t *HeadscaleInContainer) WaitForRunning() error {
|
2022-10-13 14:01:23 +00:00
|
|
|
url := t.GetHealthEndpoint()
|
|
|
|
|
2022-10-18 09:58:15 +00:00
|
|
|
log.Printf("waiting for headscale to be ready at %s", url)
|
|
|
|
|
2022-11-06 19:22:21 +00: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 19:22:21 +00:00
|
|
|
client = &http.Client{Transport: insecureTransport}
|
|
|
|
}
|
|
|
|
|
2022-10-13 14:01:23 +00:00
|
|
|
return t.pool.Retry(func() error {
|
2022-11-06 19:22:21 +00:00
|
|
|
resp, err := client.Get(url) //nolint
|
2022-10-13 14:01:23 +00: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 11:24:27 +00:00
|
|
|
// CreateUser adds a new user to the Headscale instance.
|
2023-01-17 16:43:44 +00:00
|
|
|
func (t *HeadscaleInContainer) CreateUser(
|
|
|
|
user string,
|
2022-10-13 14:01:23 +00:00
|
|
|
) error {
|
2023-01-17 16:43:44 +00:00
|
|
|
command := []string{"headscale", "users", "create", user}
|
2022-10-13 14:01:23 +00:00
|
|
|
|
|
|
|
_, _, err := dockertestutil.ExecuteCommand(
|
|
|
|
t.container,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-03 11:24:27 +00: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 14:01:23 +00:00
|
|
|
func (t *HeadscaleInContainer) CreateAuthKey(
|
2023-01-17 16:43:44 +00:00
|
|
|
user string,
|
2022-12-27 19:05:21 +00:00
|
|
|
reusable bool,
|
|
|
|
ephemeral bool,
|
2022-10-13 14:01:23 +00:00
|
|
|
) (*v1.PreAuthKey, error) {
|
|
|
|
command := []string{
|
|
|
|
"headscale",
|
2023-01-17 16:43:44 +00:00
|
|
|
"--user",
|
|
|
|
user,
|
2022-10-13 14:01:23 +00: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 14:01:23 +00: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 11:42:05 +00:00
|
|
|
// ListNodesInUser list the TailscaleClients (Node, Headscale internal representation)
|
2023-02-03 11:24:27 +00:00
|
|
|
// associated with a user.
|
2023-09-24 11:42:05 +00:00
|
|
|
func (t *HeadscaleInContainer) ListNodesInUser(
|
2023-01-17 16:43:44 +00:00
|
|
|
user string,
|
2023-09-24 11:42:05 +00:00
|
|
|
) ([]*v1.Node, error) {
|
2023-01-17 16:43:44 +00:00
|
|
|
command := []string{"headscale", "--user", user, "nodes", "list", "--output", "json"}
|
2022-10-13 14:01:23 +00: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 11:42:05 +00:00
|
|
|
var nodes []*v1.Node
|
2022-10-13 14:01:23 +00: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 08:55:09 +00:00
|
|
|
|
2023-02-03 11:24:27 +00:00
|
|
|
// WriteFile save file inside the Headscale container.
|
2022-11-02 08:55:09 +00:00
|
|
|
func (t *HeadscaleInContainer) WriteFile(path string, data []byte) error {
|
2022-11-06 19:22:21 +00:00
|
|
|
return integrationutil.WriteFileToContainer(t.pool, t.container, path, data)
|
|
|
|
}
|
2022-11-02 08:55:09 +00:00
|
|
|
|
2023-04-27 14:57:11 +00: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 19:22:21 +00:00
|
|
|
// From:
|
|
|
|
// https://shaneutt.com/blog/golang-ca-and-signed-cert-go/
|
2022-11-02 08:55:09 +00:00
|
|
|
|
2022-11-06 19:22:21 +00: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(),
|
2024-05-24 08:15:34 +00:00
|
|
|
NotAfter: time.Now().Add(60 * time.Hour),
|
2022-11-06 19:22:21 +00:00
|
|
|
IsCA: true,
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{
|
|
|
|
x509.ExtKeyUsageClientAuth,
|
|
|
|
x509.ExtKeyUsageServerAuth,
|
|
|
|
},
|
|
|
|
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
|
|
|
|
BasicConstraintsValid: true,
|
2022-11-02 08:55:09 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 19:22:21 +00:00
|
|
|
caPrivKey, err := rsa.GenerateKey(rand.Reader, 4096)
|
2022-11-02 08:55:09 +00:00
|
|
|
if err != nil {
|
2022-11-06 19:22:21 +00:00
|
|
|
return nil, nil, err
|
2022-11-02 08:55:09 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 19:22:21 +00:00
|
|
|
cert := &x509.Certificate{
|
|
|
|
SerialNumber: big.NewInt(1658),
|
|
|
|
Subject: pkix.Name{
|
2023-04-13 21:10:08 +00:00
|
|
|
CommonName: hostname,
|
2022-11-06 19:22:21 +00: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 19:22:21 +00: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 19:22:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
certPrivKey, err := rsa.GenerateKey(rand.Reader, 4096)
|
2022-11-02 08:55:09 +00:00
|
|
|
if err != nil {
|
2022-11-06 19:22:21 +00:00
|
|
|
return nil, nil, err
|
2022-11-02 08:55:09 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 19:22:21 +00:00
|
|
|
certBytes, err := x509.CreateCertificate(
|
|
|
|
rand.Reader,
|
|
|
|
cert,
|
|
|
|
ca,
|
|
|
|
&certPrivKey.PublicKey,
|
|
|
|
caPrivKey,
|
|
|
|
)
|
2022-11-02 08:55:09 +00:00
|
|
|
if err != nil {
|
2022-11-06 19:22:21 +00:00
|
|
|
return nil, nil, err
|
2022-11-02 08:55:09 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 19:22:21 +00:00
|
|
|
certPEM := new(bytes.Buffer)
|
2022-11-02 08:55:09 +00:00
|
|
|
|
2022-11-06 19:22:21 +00:00
|
|
|
err = pem.Encode(certPEM, &pem.Block{
|
|
|
|
Type: "CERTIFICATE",
|
|
|
|
Bytes: certBytes,
|
|
|
|
})
|
2022-11-02 08:55:09 +00:00
|
|
|
if err != nil {
|
2022-11-06 19:22:21 +00:00
|
|
|
return nil, nil, err
|
2022-11-02 08:55:09 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 19:22:21 +00:00
|
|
|
certPrivKeyPEM := new(bytes.Buffer)
|
|
|
|
|
|
|
|
err = pem.Encode(certPrivKeyPEM, &pem.Block{
|
|
|
|
Type: "RSA PRIVATE KEY",
|
|
|
|
Bytes: x509.MarshalPKCS1PrivateKey(certPrivKey),
|
|
|
|
})
|
2022-11-02 08:55:09 +00:00
|
|
|
if err != nil {
|
2022-11-06 19:22:21 +00:00
|
|
|
return nil, nil, err
|
2022-11-02 08:55:09 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 19:22:21 +00:00
|
|
|
return certPEM.Bytes(), certPrivKeyPEM.Bytes(), nil
|
2022-11-02 08:55:09 +00:00
|
|
|
}
|