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"
|
2022-11-06 14:22:21 -05:00
|
|
|
"time"
|
2022-10-13 10:01:23 -04:00
|
|
|
|
|
|
|
"github.com/juanfont/headscale"
|
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
type HeadscaleInContainer struct {
|
|
|
|
hostname string
|
|
|
|
|
|
|
|
pool *dockertest.Pool
|
|
|
|
container *dockertest.Resource
|
|
|
|
network *dockertest.Network
|
2022-11-02 06:08:54 -04:00
|
|
|
|
|
|
|
// optional config
|
2022-11-06 14:22:21 -05:00
|
|
|
port int
|
2022-11-02 06:08:54 -04:00
|
|
|
aclPolicy *headscale.ACLPolicy
|
|
|
|
env []string
|
2022-11-06 14:22:21 -05:00
|
|
|
tlsCert []byte
|
|
|
|
tlsKey []byte
|
2022-11-02 06:08:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option = func(c *HeadscaleInContainer)
|
|
|
|
|
|
|
|
func WithACLPolicy(acl *headscale.ACLPolicy) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
2022-11-06 14:22:21 -05:00
|
|
|
// TODO(kradalby): Move somewhere appropriate
|
|
|
|
hsic.env = append(hsic.env, fmt.Sprintf("HEADSCALE_ACL_POLICY_PATH=%s", aclPolicyPath))
|
|
|
|
|
2022-11-02 06:08:54 -04:00
|
|
|
hsic.aclPolicy = acl
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 14:22:21 -05: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.tlsCert = cert
|
|
|
|
hsic.tlsKey = key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 06:08:54 -04:00
|
|
|
func WithConfigEnv(configEnv map[string]string) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
for key, value := range configEnv {
|
2022-11-24 10:35:30 -05:00
|
|
|
hsic.env = append(hsic.env, fmt.Sprintf("%s=%s", key, value))
|
2022-11-02 06:08:54 -04:00
|
|
|
}
|
|
|
|
}
|
2022-10-13 10:01:23 -04:00
|
|
|
}
|
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
func WithPort(port int) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
hsic.port = port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 09:01:31 -05: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-11-21 15:51:54 -05:00
|
|
|
func WithHostnameAsServerURL() Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
hsic.env = append(
|
|
|
|
hsic.env,
|
|
|
|
fmt.Sprintf("HEADSCALE_SERVER_URL=http://%s:%d",
|
|
|
|
hsic.GetHostname(),
|
|
|
|
hsic.port,
|
|
|
|
))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-10-13 10:01:23 -04:00
|
|
|
hash, err := headscale.GenerateRandomStringDNSSafe(hsicHashLength)
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
|
|
|
runOptions := &dockertest.RunOptions{
|
2022-11-14 09:01:31 -05:00
|
|
|
Name: hsic.hostname,
|
2022-10-13 10:01:23 -04:00
|
|
|
ExposedPorts: []string{portProto},
|
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.
|
|
|
|
Entrypoint: []string{"/bin/bash", "-c", "/bin/sleep 3 ; headscale serve"},
|
2022-11-02 06:08:54 -04:00
|
|
|
Env: hsic.env,
|
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
|
|
|
|
|
|
|
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 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 04:55:48 -04:00
|
|
|
return hsic, nil
|
2022-10-13 10:01:23 -04:00
|
|
|
}
|
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
func (t *HeadscaleInContainer) hasTLS() bool {
|
|
|
|
return len(t.tlsCert) != 0 && len(t.tlsKey) != 0
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:01:23 -04:00
|
|
|
func (t *HeadscaleInContainer) Shutdown() error {
|
|
|
|
return t.pool.Purge(t.container)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-13 10:01:23 -04:00
|
|
|
func (t *HeadscaleInContainer) GetIP() string {
|
|
|
|
return t.container.GetIPInNetwork(t.network)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-11-06 14:22:21 -05:00
|
|
|
func (t *HeadscaleInContainer) GetCert() []byte {
|
|
|
|
return t.tlsCert
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *HeadscaleInContainer) GetHostname() string {
|
|
|
|
return t.hostname
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
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",
|
|
|
|
"--namespace",
|
|
|
|
namespace,
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2022-10-23 06:41:35 -04:00
|
|
|
func (t *HeadscaleInContainer) ListMachinesInNamespace(
|
2022-10-13 10:01:23 -04: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 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
|
|
|
|
2022-11-19 05:33:15 -05:00
|
|
|
// nolint
|
2022-11-06 14:22:21 -05:00
|
|
|
func createCertificate() ([]byte, []byte, error) {
|
|
|
|
// 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(),
|
|
|
|
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 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{
|
|
|
|
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 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
|
|
|
}
|