/
certs.go
60 lines (51 loc) · 1.36 KB
/
certs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package main
import (
"os"
"path"
"strings"
)
type CertStore struct {
DomainFile string
RootCAFile string
RootCAKeyFile string
TLSCertFile string
TLSKeyFile string
}
func certStore(certDir string) CertStore {
return CertStore{
DomainFile: path.Join(certDir, "domain.pem"),
RootCAFile: path.Join(certDir, "rootCA.pem"),
RootCAKeyFile: path.Join(certDir, "rootCA-key.pem"),
TLSKeyFile: path.Join(certDir, "tls.key"),
TLSCertFile: path.Join(certDir, "tls.crt"),
}
}
func writeCertificates(dir string, certs *Certificates) (*CertStore, error) {
if err := os.RemoveAll(dir); err != nil {
return nil, err
}
if err := os.MkdirAll(dir, 0755); err != nil {
return nil, err
}
certPath := certStore(dir)
domainPEM := strings.Join([]string{
strings.TrimSuffix(certs.LeafCertPEM, "\n"),
strings.TrimSuffix(certs.LeafKeyPEM, "\n"),
strings.TrimSuffix(certs.RootCACertPEM, "\n"),
}, "\n")
for _, cert := range []struct {
filename string
pemData string
}{
{certPath.DomainFile, domainPEM},
{certPath.RootCAFile, certs.RootCACertPEM},
{certPath.RootCAKeyFile, certs.RootCAKeyPEM},
{certPath.TLSCertFile, certs.LeafCertPEM},
{certPath.TLSKeyFile, certs.LeafKeyPEM},
} {
if err := createFile(cert.filename, []byte(strings.TrimSuffix(cert.pemData, "\n"))); err != nil {
return nil, err
}
}
return &certPath, nil
}