Merge branch '16265-security-updates' into dependabot/bundler/services/api/nokogiri...
[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 // Create a root CA key and use it to make a new server
14 // certificate+key pair.
15 //
16 // In future we'll make one root CA key per host instead of one per
17 // cluster, so it only needs to be imported to a browser once for
18 // ongoing dev/test usage.
19 type createCertificates struct{}
20
21 func (createCertificates) String() string {
22         return "certificates"
23 }
24
25 func (createCertificates) Run(ctx context.Context, fail func(error), super *Supervisor) error {
26         // Generate root key
27         err := super.RunProgram(ctx, super.tempdir, nil, nil, "openssl", "genrsa", "-out", "rootCA.key", "4096")
28         if err != nil {
29                 return err
30         }
31         // Generate a self-signed root certificate
32         err = super.RunProgram(ctx, super.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")
33         if err != nil {
34                 return err
35         }
36         // Generate server key
37         err = super.RunProgram(ctx, super.tempdir, nil, nil, "openssl", "genrsa", "-out", "server.key", "2048")
38         if err != nil {
39                 return err
40         }
41         // Build config file for signing request
42         defaultconf, err := ioutil.ReadFile("/etc/ssl/openssl.cnf")
43         if err != nil {
44                 return err
45         }
46         err = ioutil.WriteFile(filepath.Join(super.tempdir, "server.cfg"), append(defaultconf, []byte(`
47 [SAN]
48 subjectAltName=DNS:localhost,DNS:localhost.localdomain
49 `)...), 0644)
50         if err != nil {
51                 return err
52         }
53         // Generate signing request
54         err = super.RunProgram(ctx, super.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")
55         if err != nil {
56                 return err
57         }
58         // Sign certificate
59         err = super.RunProgram(ctx, super.tempdir, nil, nil, "openssl", "x509", "-req", "-in", "server.csr", "-CA", "rootCA.crt", "-CAkey", "rootCA.key", "-CAcreateserial", "-out", "server.crt", "-days", "3650", "-sha256")
60         if err != nil {
61                 return err
62         }
63         return nil
64 }