This repository has been archived by the owner on Jul 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 277
/
certificate_manager.go
248 lines (197 loc) · 7.97 KB
/
certificate_manager.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package vault
import (
"time"
"github.com/hashicorp/vault/api"
"github.com/pkg/errors"
"github.com/openservicemesh/osm/pkg/announcements"
"github.com/openservicemesh/osm/pkg/certificate"
"github.com/openservicemesh/osm/pkg/certificate/pem"
"github.com/openservicemesh/osm/pkg/certificate/rotor"
"github.com/openservicemesh/osm/pkg/configurator"
"github.com/openservicemesh/osm/pkg/constants"
"github.com/openservicemesh/osm/pkg/kubernetes/events"
"github.com/openservicemesh/osm/pkg/logger"
)
var log = logger.New("vault")
const (
// The string value of the JSON key containing the certificate's Serial Number.
// See: https://www.vaultproject.io/api-docs/secret/pki#sample-response-8
serialNumberField = "serial_number"
certificateField = "certificate"
privateKeyField = "private_key"
issuingCAField = "issuing_ca"
commonNameField = "common_name"
ttlField = "ttl"
checkCertificateExpirationInterval = 5 * time.Second
decade = 8765 * time.Hour
)
// NewCertManager implements certificate.Manager and wraps a Hashi Vault with methods to allow easy certificate issuance.
func NewCertManager(vaultAddr, token string, role string, cfg configurator.Configurator) (*CertManager, error) {
c := &CertManager{
role: vaultRole(role),
cfg: cfg,
}
config := api.DefaultConfig()
config.Address = vaultAddr
var err error
if c.client, err = api.NewClient(config); err != nil {
return nil, errors.Errorf("Error creating Vault CertManager without TLS at %s", vaultAddr)
}
log.Info().Msgf("Created Vault CertManager, with role=%q at %v", role, vaultAddr)
c.client.SetToken(token)
issuingCA, serialNumber, err := c.getIssuingCA(c.issue)
if err != nil {
return nil, err
}
c.ca = &Certificate{
commonName: constants.CertificationAuthorityCommonName,
serialNumber: serialNumber,
expiration: time.Now().Add(decade),
certChain: issuingCA,
issuingCA: issuingCA,
}
// Instantiating a new certificate rotation mechanism will start a goroutine for certificate rotation.
rotor.New(c).Start(checkCertificateExpirationInterval)
return c, nil
}
func (cm *CertManager) getIssuingCA(issue func(certificate.CommonName, time.Duration) (certificate.Certificater, error)) ([]byte, certificate.SerialNumber, error) {
// Create a temp certificate to determine the public part of the issuing CA
cert, err := issue("localhost", decade)
if err != nil {
return nil, "", err
}
issuingCA := cert.GetIssuingCA()
// We are not going to need this certificate - remove it
cm.ReleaseCertificate(cert.GetCommonName())
return issuingCA, cert.GetSerialNumber(), err
}
func (cm *CertManager) issue(cn certificate.CommonName, validityPeriod time.Duration) (certificate.Certificater, error) {
secret, err := cm.client.Logical().Write(getIssueURL(cm.role).String(), getIssuanceData(cn, validityPeriod))
if err != nil {
log.Error().Err(err).Msgf("Error issuing new certificate for CN=%s", cn)
return nil, err
}
return newCert(cn, secret, time.Now().Add(validityPeriod)), nil
}
func (cm *CertManager) deleteFromCache(cn certificate.CommonName) {
cm.cache.Delete(cn)
}
func (cm *CertManager) getFromCache(cn certificate.CommonName) certificate.Certificater {
if certificateInterface, exists := cm.cache.Load(cn); exists {
cert := certificateInterface.(certificate.Certificater)
log.Trace().Msgf("Certificate found in cache SerialNumber=%s", cert.GetSerialNumber())
if rotor.ShouldRotate(cert) {
log.Trace().Msgf("Certificate found in cache but has expired SerialNumber=%s", cert.GetSerialNumber())
return nil
}
return cert
}
return nil
}
// IssueCertificate issues a certificate by leveraging the Hashi Vault CertManager.
func (cm *CertManager) IssueCertificate(cn certificate.CommonName, validityPeriod time.Duration) (certificate.Certificater, error) {
start := time.Now()
if cert := cm.getFromCache(cn); cert != nil {
return cert, nil
}
cert, err := cm.issue(cn, validityPeriod)
if err != nil {
return cert, err
}
cm.cache.Store(cn, cert)
log.Trace().Msgf("Issued new certificate with SerialNumber=%s took %+v", cert.GetSerialNumber(), time.Since(start))
return cert, nil
}
// ReleaseCertificate is called when a cert will no longer be needed and should be removed from the system.
func (cm *CertManager) ReleaseCertificate(cn certificate.CommonName) {
// TODO(draychev): implement Hashicorp Vault delete-cert API here: https://github.com/openservicemesh/osm/issues/2068
cm.deleteFromCache(cn)
}
// ListCertificates lists all certificates issued
func (cm *CertManager) ListCertificates() ([]certificate.Certificater, error) {
var certs []certificate.Certificater
cm.cache.Range(func(cnInterface interface{}, certInterface interface{}) bool {
certs = append(certs, certInterface.(certificate.Certificater))
return true // continue the iteration
})
return certs, nil
}
// GetCertificate returns a certificate given its Common Name (CN)
func (cm *CertManager) GetCertificate(cn certificate.CommonName) (certificate.Certificater, error) {
if cert := cm.getFromCache(cn); cert != nil {
return cert, nil
}
return nil, errCertNotFound
}
// GetRootCertificate returns the root certificate.
func (cm *CertManager) GetRootCertificate() (certificate.Certificater, error) {
return cm.ca, nil
}
// RotateCertificate implements certificate.Manager and rotates an existing certificate.
func (cm *CertManager) RotateCertificate(cn certificate.CommonName) (certificate.Certificater, error) {
start := time.Now()
oldCert, ok := cm.cache.Load(cn)
if !ok {
return nil, errors.Errorf("Old certificate does not exist for CN=%s", cn)
}
newCert, err := cm.issue(cn, cm.cfg.GetServiceCertValidityPeriod())
if err != nil {
return nil, err
}
cm.cache.Store(cn, newCert)
events.GetPubSubInstance().Publish(events.PubSubMessage{
AnnouncementType: announcements.CertificateRotated,
NewObj: newCert,
OldObj: oldCert.(certificate.Certificater),
})
log.Debug().Msgf("Rotated certificate (old SerialNumber=%s) with new SerialNumber=%s took %+v", oldCert.(certificate.Certificater).GetSerialNumber(), newCert.GetSerialNumber(), time.Since(start))
return newCert, nil
}
// Certificate implements certificate.Certificater
type Certificate struct {
// The commonName of the certificate
commonName certificate.CommonName
// When the cert expires
expiration time.Time
// PEM encoded Certificate and Key (byte arrays)
certChain pem.Certificate
privateKey pem.PrivateKey
// Certificate authority signing this certificate.
issuingCA pem.RootCertificate
// serialNumber is the serial_number value in the Data field assigned to the Certificate Hashicorp Vault issued
serialNumber certificate.SerialNumber
}
// GetCommonName returns the common name of the given certificate.
func (c Certificate) GetCommonName() certificate.CommonName {
return c.commonName
}
// GetCertificateChain returns the PEM encoded certificate.
func (c Certificate) GetCertificateChain() []byte {
return c.certChain
}
// GetPrivateKey returns the PEM encoded private key of the given certificate.
func (c Certificate) GetPrivateKey() []byte {
return c.privateKey
}
// GetIssuingCA returns the root certificate signing the given cert.
func (c Certificate) GetIssuingCA() []byte {
return c.issuingCA
}
// GetExpiration implements certificate.Certificater and returns the time the given certificate expires.
func (c Certificate) GetExpiration() time.Time {
return c.expiration
}
func newCert(cn certificate.CommonName, secret *api.Secret, expiration time.Time) *Certificate {
return &Certificate{
commonName: cn,
serialNumber: certificate.SerialNumber(secret.Data[serialNumberField].(string)),
expiration: expiration,
certChain: pem.Certificate(secret.Data[certificateField].(string)),
privateKey: []byte(secret.Data[privateKeyField].(string)),
issuingCA: pem.RootCertificate(secret.Data[issuingCAField].(string)),
}
}
// GetSerialNumber returns the serial number of the given certificate.
func (c Certificate) GetSerialNumber() certificate.SerialNumber {
return c.serialNumber
}