-
Notifications
You must be signed in to change notification settings - Fork 0
/
gencerts.go
174 lines (154 loc) · 5.47 KB
/
gencerts.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package etcd
import (
"crypto/rsa"
"crypto/x509"
"fmt"
"net"
log "github.com/Sirupsen/logrus"
certutil "github.com/UKHomeOffice/keto-k8/pkg/client-go/util/cert"
"github.com/UKHomeOffice/keto-k8/pkg/fileutil"
"github.com/UKHomeOffice/keto-k8/pkg/kubeadm/pkiutil"
)
// ServerConfig - Params for configuring an etcd cluster
type ServerConfig struct {
CaKeyFileName string
ServerCertFileName string
ServerKeyFileName string
PeerCertFileName string
PeerKeyFileName string
LocalHostNames []string
ClusterHostNames []string
ClientConfig Client
}
// ExtKeyUsage - contains a mapping of string names to extended key
// usages.
var ExtKeyUsage = map[string]x509.ExtKeyUsage{
"any": x509.ExtKeyUsageAny,
"server auth": x509.ExtKeyUsageServerAuth,
"client auth": x509.ExtKeyUsageClientAuth,
"code signing": x509.ExtKeyUsageCodeSigning,
"email protection": x509.ExtKeyUsageEmailProtection,
"s/mime": x509.ExtKeyUsageEmailProtection,
"ipsec end system": x509.ExtKeyUsageIPSECEndSystem,
"ipsec tunnel": x509.ExtKeyUsageIPSECTunnel,
"ipsec user": x509.ExtKeyUsageIPSECUser,
"timestamping": x509.ExtKeyUsageTimeStamping,
"ocsp signing": x509.ExtKeyUsageOCSPSigning,
"microsoft sgc": x509.ExtKeyUsageMicrosoftServerGatedCrypto,
"netscape sgc": x509.ExtKeyUsageNetscapeServerGatedCrypto,
}
// GenCerts - Will generate etcd server and client certs from appropriate CA and key
func GenCerts(cfg ServerConfig) (err error) {
var caCert *x509.Certificate
var caKey *rsa.PrivateKey
// Load the CA files...
if fileutil.ExistFile(cfg.CaKeyFileName) && fileutil.ExistFile(cfg.ClientConfig.CaFileName) {
// Try to load cert and key...
caCert, err = pkiutil.TryLoadAnyCertFromDisk(cfg.ClientConfig.CaFileName)
if err != nil || caCert == nil {
return fmt.Errorf("CA certificate existed but could not be loaded properly %q", cfg.ClientConfig.CaFileName)
}
// The certificate and key could be loaded, but the certificate is not a CA
if !caCert.IsCA {
return fmt.Errorf("certificate and key could be loaded but the certificate is not a CA")
}
caKey, err = pkiutil.TryLoadAnyKeyFromDisk(cfg.CaKeyFileName)
if err != nil || caKey == nil {
return fmt.Errorf("CA key existed but could not be loaded properly %q", cfg.CaKeyFileName)
}
log.Printf("Found and verified CA certificate %q and key %q", cfg.ClientConfig.CaFileName, cfg.CaKeyFileName)
} else {
return fmt.Errorf("etcd CA key %q and cert %q must both exist before certs can be created", cfg.CaKeyFileName, cfg.ClientConfig.CaFileName)
}
// Generate the ETCD server cert and key file (if required)
serverCertCfg := certutil.Config{
CommonName: cfg.ClusterHostNames[0],
AltNames: getAltNames(cfg.ClusterHostNames),
Usages: []x509.ExtKeyUsage{
ExtKeyUsage["server auth"],
},
}
if err = checkOrCreateCert(
cfg.ServerCertFileName,
cfg.ServerKeyFileName,
caCert,
caKey,
serverCertCfg); err != nil {
return err
}
// Generate ETCD peer cert and key (if required)
peerCertCfg := certutil.Config{
CommonName: cfg.ClusterHostNames[0],
AltNames: getAltNames(cfg.LocalHostNames),
Usages: []x509.ExtKeyUsage{
ExtKeyUsage["server auth"],
ExtKeyUsage["client auth"],
},
}
if err = checkOrCreateCert(
cfg.PeerCertFileName,
cfg.PeerKeyFileName,
caCert,
caKey,
peerCertCfg); err != nil {
return err
}
// Generate ETCD client certs
clientCertCfg := certutil.Config{
CommonName: cfg.ClusterHostNames[0],
Usages: []x509.ExtKeyUsage{
ExtKeyUsage["client auth"],
},
}
if err = checkOrCreateCert(
cfg.ClientConfig.ClientCertFileName,
cfg.ClientConfig.ClientKeyFileName,
caCert,
caKey,
clientCertCfg); err != nil {
return err
}
return err
}
func checkOrCreateCert(certFile, keyFile string, caCert *x509.Certificate, caKey *rsa.PrivateKey, config certutil.Config) error {
if fileutil.ExistFile(certFile) && fileutil.ExistFile(keyFile) {
// Try to load cert and key...
cert, err := pkiutil.TryLoadAnyCertFromDisk(certFile)
if err != nil || cert == nil {
return fmt.Errorf("certificate existed but they could not be loaded properly %q", certFile)
}
key, err := pkiutil.TryLoadAnyKeyFromDisk(keyFile)
if err != nil || key == nil {
return fmt.Errorf("key existed but they could not be loaded properly %q", keyFile)
}
log.Printf("Using cert:%q and key %q", certFile, keyFile)
} else {
// The certificate and / or the key did NOT exist, let's generate them now
cert, key, err := pkiutil.NewCertAndKey(caCert, caKey, config)
if err != nil {
return fmt.Errorf("failure while creating key %q and cert %q [%v]", certFile, keyFile, err)
}
if err = certutil.WriteCert(certFile, certutil.EncodeCertPEM(cert)); err != nil {
return fmt.Errorf("failure while saving certificate %q [%v]", certFile, err)
}
if err = certutil.WriteKey(keyFile, certutil.EncodePrivateKeyPEM(key)); err != nil {
return fmt.Errorf("failure while saving key %q [%v]", keyFile, err)
}
log.Printf("Generated cert %q.", certFile)
log.Printf("Generated key %q.", keyFile)
}
return nil
}
// getAltNames builds an AltNames object for the certutil to use when generating the certificates
func getAltNames(cfgAltNames []string) certutil.AltNames {
altNames := certutil.AltNames{}
// Populate IPs/DNSNames from AltNames
for _, altname := range cfgAltNames {
if ip := net.ParseIP(altname); ip != nil {
altNames.IPs = append(altNames.IPs, ip)
} else {
altNames.DNSNames = append(altNames.DNSNames, altname)
}
}
return altNames
}