15954: Start own postgresql server.
[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 func createCertificates(ctx context.Context, boot *Booter, ready chan<- bool) error {
14         // Generate root key
15         err := boot.RunProgram(ctx, boot.tempdir, nil, nil, "openssl", "genrsa", "-out", "rootCA.key", "4096")
16         if err != nil {
17                 return err
18         }
19         // Generate a self-signed root certificate
20         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")
21         if err != nil {
22                 return err
23         }
24         // Generate server key
25         err = boot.RunProgram(ctx, boot.tempdir, nil, nil, "openssl", "genrsa", "-out", "server.key", "2048")
26         if err != nil {
27                 return err
28         }
29         // Build config file for signing request
30         defaultconf, err := ioutil.ReadFile("/etc/ssl/openssl.cnf")
31         if err != nil {
32                 return err
33         }
34         err = ioutil.WriteFile(filepath.Join(boot.tempdir, "server.cfg"), append(defaultconf, []byte(`
35 [SAN]
36 subjectAltName=DNS:localhost,DNS:localhost.localdomain
37 `)...), 0777)
38         if err != nil {
39                 return err
40         }
41         // Generate signing request
42         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")
43         if err != nil {
44                 return err
45         }
46         // Sign certificate
47         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")
48         if err != nil {
49                 return err
50         }
51
52         close(ready)
53         <-ctx.Done()
54         return nil
55 }