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"
|
2022-11-06 20:22:21 +01:00
|
|
|
"time"
|
2022-10-13 16:01:23 +02:00
|
|
|
|
|
|
|
"github.com/juanfont/headscale"
|
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
type HeadscaleInContainer struct {
|
|
|
|
hostname string
|
|
|
|
|
|
|
|
pool *dockertest.Pool
|
|
|
|
container *dockertest.Resource
|
|
|
|
network *dockertest.Network
|
2022-11-02 11:08:54 +01:00
|
|
|
|
|
|
|
// optional config
|
2022-11-06 20:22:21 +01:00
|
|
|
port int
|
2022-11-02 11:08:54 +01:00
|
|
|
aclPolicy *headscale.ACLPolicy
|
|
|
|
env []string
|
2022-11-06 20:22:21 +01:00
|
|
|
tlsCert []byte
|
|
|
|
tlsKey []byte
|
2022-11-02 11:08:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option = func(c *HeadscaleInContainer)
|
|
|
|
|
|
|
|
func WithACLPolicy(acl *headscale.ACLPolicy) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
2022-11-06 20:22:21 +01:00
|
|
|
// TODO(kradalby): Move somewhere appropriate
|
|
|
|
hsic.env = append(hsic.env, fmt.Sprintf("HEADSCALE_ACL_POLICY_PATH=%s", aclPolicyPath))
|
|
|
|
|
2022-11-02 11:08:54 +01:00
|
|
|
hsic.aclPolicy = acl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 20:22:21 +01:00
|
|
|
func WithTLS() Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
cert, key, err := createCertificate()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("failed to create certificates for headscale test: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(kradalby): Move somewhere appropriate
|
|
|
|
hsic.env = append(hsic.env, fmt.Sprintf("HEADSCALE_TLS_CERT_PATH=%s", tlsCertPath))
|
|
|
|
hsic.env = append(hsic.env, fmt.Sprintf("HEADSCALE_TLS_KEY_PATH=%s", tlsKeyPath))
|
|
|
|
hsic.env = append(hsic.env, "HEADSCALE_TLS_CLIENT_AUTH_MODE=disabled")
|
|
|
|
|
|
|
|
hsic.tlsCert = cert
|
|
|
|
hsic.tlsKey = key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 11:08:54 +01:00
|
|
|
func WithConfigEnv(configEnv map[string]string) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
env := []string{}
|
|
|
|
|
|
|
|
for key, value := range configEnv {
|
|
|
|
env = append(env, fmt.Sprintf("%s=%s", key, value))
|
|
|
|
}
|
|
|
|
|
|
|
|
hsic.env = env
|
|
|
|
}
|
2022-10-13 16:01:23 +02:00
|
|
|
}
|
|
|
|
|
2022-11-06 20:22:21 +01:00
|
|
|
func WithPort(port int) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
hsic.port = port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 15:01:31 +01:00
|
|
|
func WithTestName(testName string) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
hash, _ := headscale.GenerateRandomStringDNSSafe(hsicHashLength)
|
|
|
|
|
|
|
|
hostname := fmt.Sprintf("hs-%s-%s", testName, hash)
|
|
|
|
hsic.hostname = hostname
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-10-13 16:01:23 +02:00
|
|
|
hash, err := headscale.GenerateRandomStringDNSSafe(hsicHashLength)
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
runOptions := &dockertest.RunOptions{
|
2022-11-14 15:01:31 +01:00
|
|
|
Name: hsic.hostname,
|
2022-10-13 16:01:23 +02:00
|
|
|
ExposedPorts: []string{portProto},
|
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.
|
|
|
|
Entrypoint: []string{"/bin/bash", "-c", "/bin/sleep 3 ; headscale serve"},
|
2022-11-02 11:08:54 +01:00
|
|
|
Env: hsic.env,
|
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
|
|
|
|
|
|
|
err = hsic.WriteFile("/etc/headscale/config.yaml", []byte(DefaultConfigYAML()))
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 09:55:48 +01:00
|
|
|
return hsic, nil
|
2022-10-13 16:01:23 +02:00
|
|
|
}
|
|
|
|
|
2022-11-06 20:22:21 +01:00
|
|
|
func (t *HeadscaleInContainer) hasTLS() bool {
|
|
|
|
return len(t.tlsCert) != 0 && len(t.tlsKey) != 0
|
|
|
|
}
|
|
|
|
|
2022-10-13 16:01:23 +02:00
|
|
|
func (t *HeadscaleInContainer) Shutdown() error {
|
|
|
|
return t.pool.Purge(t.container)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-13 16:01:23 +02:00
|
|
|
func (t *HeadscaleInContainer) GetIP() string {
|
|
|
|
return t.container.GetIPInNetwork(t.network)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-11-06 20:22:21 +01:00
|
|
|
func (t *HeadscaleInContainer) GetCert() []byte {
|
|
|
|
return t.tlsCert
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *HeadscaleInContainer) GetHostname() string {
|
|
|
|
return t.hostname
|
|
|
|
}
|
|
|
|
|
2022-10-13 16:01:23 +02:00
|
|
|
func (t *HeadscaleInContainer) WaitForReady() error {
|
|
|
|
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 {
|
2022-11-06 20:22:21 +01:00
|
|
|
log.Printf("ready err: %s", err)
|
|
|
|
|
2022-10-13 16:01:23 +02:00
|
|
|
return fmt.Errorf("headscale is not ready: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return errHeadscaleStatusCodeNotOk
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *HeadscaleInContainer) CreateNamespace(
|
|
|
|
namespace string,
|
|
|
|
) error {
|
|
|
|
command := []string{"headscale", "namespaces", "create", namespace}
|
|
|
|
|
|
|
|
_, _, err := dockertestutil.ExecuteCommand(
|
|
|
|
t.container,
|
|
|
|
command,
|
|
|
|
[]string{},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *HeadscaleInContainer) CreateAuthKey(
|
|
|
|
namespace string,
|
|
|
|
) (*v1.PreAuthKey, error) {
|
|
|
|
command := []string{
|
|
|
|
"headscale",
|
|
|
|
"--namespace",
|
|
|
|
namespace,
|
|
|
|
"preauthkeys",
|
|
|
|
"create",
|
|
|
|
"--reusable",
|
|
|
|
"--expiration",
|
|
|
|
"24h",
|
|
|
|
"--output",
|
|
|
|
"json",
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-23 12:41:35 +02:00
|
|
|
func (t *HeadscaleInContainer) ListMachinesInNamespace(
|
2022-10-13 16:01:23 +02:00
|
|
|
namespace string,
|
|
|
|
) ([]*v1.Machine, error) {
|
|
|
|
command := []string{"headscale", "--namespace", namespace, "nodes", "list", "--output", "json"}
|
|
|
|
|
|
|
|
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 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
|
|
|
|
2022-11-10 08:02:40 +00:00
|
|
|
//nolint
|
2022-11-06 20:22:21 +01:00
|
|
|
func createCertificate() ([]byte, []byte, error) {
|
|
|
|
// 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(),
|
|
|
|
NotAfter: time.Now().Add(30 * time.Minute),
|
|
|
|
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{
|
|
|
|
Organization: []string{"Headscale testing INC"},
|
|
|
|
Country: []string{"NL"},
|
|
|
|
Locality: []string{"Leiden"},
|
|
|
|
},
|
|
|
|
IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1), net.IPv6loopback},
|
|
|
|
NotBefore: time.Now(),
|
|
|
|
NotAfter: time.Now().Add(30 * time.Minute),
|
|
|
|
SubjectKeyId: []byte{1, 2, 3, 4, 6},
|
|
|
|
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
|
|
|
|
KeyUsage: x509.KeyUsageDigitalSignature,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|