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"
|
|
|
|
"log"
|
2022-11-06 19:22:21 +00:00
|
|
|
"math/big"
|
|
|
|
"net"
|
2022-10-13 14:01:23 +00:00
|
|
|
"net/http"
|
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
|
|
|
"github.com/juanfont/headscale"
|
|
|
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
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")
|
|
|
|
|
|
|
|
type HeadscaleInContainer struct {
|
|
|
|
hostname string
|
|
|
|
|
|
|
|
pool *dockertest.Pool
|
|
|
|
container *dockertest.Resource
|
|
|
|
network *dockertest.Network
|
2022-11-02 10:08:54 +00:00
|
|
|
|
|
|
|
// optional config
|
2022-11-06 19:22:21 +00:00
|
|
|
port int
|
2022-11-02 10:08:54 +00:00
|
|
|
aclPolicy *headscale.ACLPolicy
|
2023-01-05 11:44:28 +00:00
|
|
|
env map[string]string
|
2022-11-06 19:22:21 +00:00
|
|
|
tlsCert []byte
|
|
|
|
tlsKey []byte
|
2022-11-02 10:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Option = func(c *HeadscaleInContainer)
|
|
|
|
|
|
|
|
func WithACLPolicy(acl *headscale.ACLPolicy) Option {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-06 19:22:21 +00: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
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-11-06 19:22:21 +00:00
|
|
|
func WithPort(port int) Option {
|
|
|
|
return func(hsic *HeadscaleInContainer) {
|
|
|
|
hsic.port = port
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-14 14:01:31 +00: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 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2022-10-13 14:01:23 +00:00
|
|
|
hash, err := headscale.GenerateRandomStringDNSSafe(hsicHashLength)
|
|
|
|
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
|
|
|
|
|
|
|
env: DefaultConfigEnv(),
|
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
|
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2023-01-05 11:44:28 +00:00
|
|
|
env := []string{}
|
|
|
|
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,
|
2022-10-13 14:01:23 +00:00
|
|
|
ExposedPorts: []string{portProto},
|
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.
|
|
|
|
Entrypoint: []string{"/bin/bash", "-c", "/bin/sleep 3 ; headscale serve"},
|
2023-01-05 11:44:28 +00:00
|
|
|
Env: env,
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-02 08:55:48 +00:00
|
|
|
return hsic, nil
|
2022-10-13 14:01:23 +00:00
|
|
|
}
|
|
|
|
|
2022-11-06 19:22:21 +00:00
|
|
|
func (t *HeadscaleInContainer) hasTLS() bool {
|
|
|
|
return len(t.tlsCert) != 0 && len(t.tlsKey) != 0
|
|
|
|
}
|
|
|
|
|
2022-10-13 14:01:23 +00:00
|
|
|
func (t *HeadscaleInContainer) Shutdown() error {
|
|
|
|
return t.pool.Purge(t.container)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-10-13 14:01:23 +00:00
|
|
|
func (t *HeadscaleInContainer) GetIP() string {
|
|
|
|
return t.container.GetIPInNetwork(t.network)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-11-06 19:22:21 +00:00
|
|
|
func (t *HeadscaleInContainer) GetCert() []byte {
|
|
|
|
return t.tlsCert
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *HeadscaleInContainer) GetHostname() string {
|
|
|
|
return t.hostname
|
|
|
|
}
|
|
|
|
|
2022-10-13 14:01:23 +00:00
|
|
|
func (t *HeadscaleInContainer) WaitForReady() error {
|
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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 19:05:21 +00:00
|
|
|
reusable bool,
|
|
|
|
ephemeral bool,
|
2022-10-13 14:01:23 +00:00
|
|
|
) (*v1.PreAuthKey, error) {
|
|
|
|
command := []string{
|
|
|
|
"headscale",
|
|
|
|
"--namespace",
|
|
|
|
namespace,
|
|
|
|
"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
|
|
|
|
}
|
|
|
|
|
2022-10-23 10:41:35 +00:00
|
|
|
func (t *HeadscaleInContainer) ListMachinesInNamespace(
|
2022-10-13 14:01:23 +00: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 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
|
|
|
|
2022-11-19 10:33:15 +00:00
|
|
|
// nolint
|
2022-11-06 19:22:21 +00:00
|
|
|
func createCertificate() ([]byte, []byte, error) {
|
|
|
|
// 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(),
|
|
|
|
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 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{
|
|
|
|
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 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
|
|
|
}
|