Merge branch '18700-boot-wb2'
[arvados.git] / lib / boot / cert.go
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 package boot
6
7 import (
8         "context"
9         "fmt"
10         "io/ioutil"
11         "net"
12         "os"
13         "path/filepath"
14 )
15
16 // Create a root CA key and use it to make a new server
17 // certificate+key pair.
18 //
19 // In future we'll make one root CA key per host instead of one per
20 // cluster, so it only needs to be imported to a browser once for
21 // ongoing dev/test usage.
22 type createCertificates struct{}
23
24 func (createCertificates) String() string {
25         return "certificates"
26 }
27
28 func (createCertificates) Run(ctx context.Context, fail func(error), super *Supervisor) error {
29         var san string
30         if net.ParseIP(super.ListenHost) != nil {
31                 san += fmt.Sprintf(",IP:%s", super.ListenHost)
32         } else {
33                 san += fmt.Sprintf(",DNS:%s", super.ListenHost)
34         }
35         hostname, err := os.Hostname()
36         if err != nil {
37                 return fmt.Errorf("hostname: %w", err)
38         }
39         san += ",DNS:" + hostname
40
41         // Generate root key
42         err = super.RunProgram(ctx, super.tempdir, runOptions{}, "openssl", "genrsa", "-out", "rootCA.key", "4096")
43         if err != nil {
44                 return err
45         }
46         // Generate a self-signed root certificate
47         err = super.RunProgram(ctx, super.tempdir, runOptions{}, "openssl", "req", "-x509", "-new", "-nodes", "-key", "rootCA.key", "-sha256", "-days", "3650", "-out", "rootCA.crt", "-subj", "/C=US/ST=MA/O=Example Org/CN=localhost")
48         if err != nil {
49                 return err
50         }
51         // Generate server key
52         err = super.RunProgram(ctx, super.tempdir, runOptions{}, "openssl", "genrsa", "-out", "server.key", "2048")
53         if err != nil {
54                 return err
55         }
56         // Build config file for signing request
57         defaultconf, err := ioutil.ReadFile("/etc/ssl/openssl.cnf")
58         if err != nil {
59                 return err
60         }
61         err = ioutil.WriteFile(filepath.Join(super.tempdir, "server.cfg"), append(defaultconf, []byte(fmt.Sprintf("\n[SAN]\nsubjectAltName=DNS:localhost,DNS:localhost.localdomain%s\n", san))...), 0644)
62         if err != nil {
63                 return err
64         }
65         // Generate signing request
66         err = super.RunProgram(ctx, super.tempdir, runOptions{}, "openssl", "req", "-new", "-sha256", "-key", "server.key", "-subj", "/C=US/ST=MA/O=Example Org/CN=localhost", "-reqexts", "SAN", "-config", "server.cfg", "-out", "server.csr")
67         if err != nil {
68                 return err
69         }
70         // Sign certificate
71         err = super.RunProgram(ctx, super.tempdir, runOptions{}, "openssl", "x509", "-req", "-in", "server.csr", "-CA", "rootCA.crt", "-CAkey", "rootCA.key", "-CAcreateserial", "-out", "server.crt", "-extfile", "server.cfg", "-extensions", "SAN", "-days", "3650", "-sha256")
72         if err != nil {
73                 return err
74         }
75         return nil
76 }