560579b77716bcbcd252d981c5da4cc396ab9ef4
[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         "io/ioutil"
10         "path/filepath"
11 )
12
13 type createCertificates struct{}
14
15 func (createCertificates) String() string {
16         return "certificates"
17 }
18
19 func (createCertificates) Run(ctx context.Context, fail func(error), boot *Booter) error {
20         // Generate root key
21         err := boot.RunProgram(ctx, boot.tempdir, nil, nil, "openssl", "genrsa", "-out", "rootCA.key", "4096")
22         if err != nil {
23                 return err
24         }
25         // Generate a self-signed root certificate
26         err = boot.RunProgram(ctx, boot.tempdir, nil, nil, "openssl", "req", "-x509", "-new", "-nodes", "-key", "rootCA.key", "-sha256", "-days", "3650", "-out", "rootCA.crt", "-subj", "/C=US/ST=MA/O=Example Org/CN=localhost")
27         if err != nil {
28                 return err
29         }
30         // Generate server key
31         err = boot.RunProgram(ctx, boot.tempdir, nil, nil, "openssl", "genrsa", "-out", "server.key", "2048")
32         if err != nil {
33                 return err
34         }
35         // Build config file for signing request
36         defaultconf, err := ioutil.ReadFile("/etc/ssl/openssl.cnf")
37         if err != nil {
38                 return err
39         }
40         err = ioutil.WriteFile(filepath.Join(boot.tempdir, "server.cfg"), append(defaultconf, []byte(`
41 [SAN]
42 subjectAltName=DNS:localhost,DNS:localhost.localdomain
43 `)...), 0777)
44         if err != nil {
45                 return err
46         }
47         // Generate signing request
48         err = boot.RunProgram(ctx, boot.tempdir, nil, nil, "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")
49         if err != nil {
50                 return err
51         }
52         // Sign certificate
53         err = boot.RunProgram(ctx, boot.tempdir, nil, nil, "openssl", "x509", "-req", "-in", "server.csr", "-CA", "rootCA.crt", "-CAkey", "rootCA.key", "-CAcreateserial", "-out", "server.crt", "-days", "3650", "-sha256")
54         if err != nil {
55                 return err
56         }
57         return nil
58 }