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 279
/
config.go
361 lines (305 loc) · 14.3 KB
/
config.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
package providers
import (
"context"
"fmt"
"time"
cmmeta "github.com/jetstack/cert-manager/pkg/apis/meta/v1"
cmversionedclient "github.com/jetstack/cert-manager/pkg/client/clientset/versioned"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"github.com/openservicemesh/osm/pkg/certificate"
"github.com/openservicemesh/osm/pkg/certificate/providers/certmanager"
"github.com/openservicemesh/osm/pkg/certificate/providers/tresor"
"github.com/openservicemesh/osm/pkg/certificate/providers/vault"
"github.com/openservicemesh/osm/pkg/configurator"
"github.com/openservicemesh/osm/pkg/constants"
"github.com/openservicemesh/osm/pkg/debugger"
"github.com/openservicemesh/osm/pkg/errcode"
"github.com/openservicemesh/osm/pkg/version"
)
const (
// Additional values for the root certificate
rootCertCountry = "US"
rootCertLocality = "CA"
rootCertOrganization = "Open Service Mesh"
)
// NewCertificateProvider returns a new certificate provider and associated config
func NewCertificateProvider(kubeClient kubernetes.Interface, kubeConfig *rest.Config, cfg configurator.Configurator, providerKind Kind,
providerNamespace string, caBundleSecretName string, tresorOptions TresorOptions, vaultOptions VaultOptions,
certManagerOptions CertManagerOptions) (certificate.Manager, debugger.CertificateManagerDebugger, *Config, error) {
config := &Config{
kubeClient: kubeClient,
kubeConfig: kubeConfig,
cfg: cfg,
providerKind: providerKind,
providerNamespace: providerNamespace,
caBundleSecretName: caBundleSecretName,
tresorOptions: tresorOptions,
vaultOptions: vaultOptions,
certManagerOptions: certManagerOptions,
}
if err := config.Validate(); err != nil {
return nil, nil, nil, err
}
certManager, certDebugger, err := config.GetCertificateManager()
if err != nil {
return nil, nil, nil, err
}
return certManager, certDebugger, config, nil
}
// NewCertificateProviderConfig returns a new certificate provider config
func NewCertificateProviderConfig(kubeClient kubernetes.Interface, kubeConfig *rest.Config, cfg configurator.Configurator, providerKind Kind,
providerNamespace string, caBundleSecretName string, tresorOptions TresorOptions, vaultOptions VaultOptions,
certManagerOptions CertManagerOptions) *Config {
return &Config{
kubeClient: kubeClient,
kubeConfig: kubeConfig,
cfg: cfg,
providerKind: providerKind,
providerNamespace: providerNamespace,
caBundleSecretName: caBundleSecretName,
tresorOptions: tresorOptions,
vaultOptions: vaultOptions,
certManagerOptions: certManagerOptions,
}
}
// Validate validates the certificate provider config
func (c *Config) Validate() error {
switch c.providerKind {
case TresorKind:
// nothing to validate
return nil
case VaultKind:
return ValidateVaultOptions(c.vaultOptions)
case CertManagerKind:
return ValidateCertManagerOptions(c.certManagerOptions)
default:
return errors.Errorf("Invalid certificate manager kind %s. Specify a valid certificate manager, one of: [%v]",
c.providerKind, ValidCertificateProviders)
}
}
// ValidateTresorOptions validates the options for Tresor certificate provider
func ValidateTresorOptions(options TresorOptions) error {
// Nothing to validate at the moment
return nil
}
// ValidateVaultOptions validates the options for Hashi Vault certificate provider
func ValidateVaultOptions(options VaultOptions) error {
if options.VaultHost == "" {
return errors.New("VaultHost not specified in Hashi Vault options")
}
if options.VaultToken == "" {
return errors.New("VaultToken not specified in Hashi Vault options")
}
if options.VaultRole == "" {
return errors.New("VaultRole not specified in Hashi Vault options")
}
if _, ok := map[string]interface{}{"http": nil, "https": nil}[options.VaultProtocol]; !ok {
return errors.Errorf("VaultProtocol in Hashi Vault options must be one of [http, https], got %s", options.VaultProtocol)
}
return nil
}
// ValidateCertManagerOptions validates the options for cert-manager.io certificate provider
func ValidateCertManagerOptions(options CertManagerOptions) error {
if options.IssuerName == "" {
return errors.New("IssuerName not specified in cert-manager.io options")
}
if options.IssuerKind == "" {
return errors.New("IssuerKind not specified in cert-manager.io options")
}
if options.IssuerGroup == "" {
return errors.New("IssuerGroup not specified in cert-manager.io options")
}
return nil
}
// GetCertificateManager returns the certificate manager/provider instance
func (c *Config) GetCertificateManager() (certificate.Manager, debugger.CertificateManagerDebugger, error) {
switch c.providerKind {
case TresorKind:
return c.getTresorOSMCertificateManager()
case VaultKind:
return c.getHashiVaultOSMCertificateManager(c.vaultOptions)
case CertManagerKind:
return c.getCertManagerOSMCertificateManager(c.certManagerOptions)
default:
return nil, nil, fmt.Errorf("Unsupported Certificate Manager %s", c.providerKind)
}
}
// GetCertificateFromSecret is a helper function that ensures creation and synchronization of a certificate
// using Kubernetes Secrets backend and API atomicity.
func GetCertificateFromSecret(ns string, secretName string, cert certificate.Certificater, kubeClient kubernetes.Interface) (certificate.Certificater, error) {
// Attempt to create it in Kubernetes. When multiple agents attempt to create, only one of them will succeed.
// All others will get "AlreadyExists" error back.
secretData := map[string][]byte{
constants.KubernetesOpaqueSecretCAKey: cert.GetCertificateChain(),
constants.KubernetesOpaqueSecretCAExpiration: []byte(cert.GetExpiration().Format(constants.TimeDateLayout)),
constants.KubernetesOpaqueSecretRootPrivateKeyKey: cert.GetPrivateKey(),
}
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: secretName,
Namespace: ns,
Labels: map[string]string{
constants.OSMAppNameLabelKey: constants.OSMAppNameLabelValue,
constants.OSMAppVersionLabelKey: version.Version,
},
},
Data: secretData,
}
if _, err := kubeClient.CoreV1().Secrets(ns).Create(context.TODO(), secret, metav1.CreateOptions{}); err == nil {
log.Info().Msg("CA created in kubernetes")
} else if apierrors.IsAlreadyExists(err) {
log.Info().Msg("CA already exists in kubernetes, loading.")
} else {
// TODO(#3962): metric might not be scraped before process restart resulting from this error
log.Error().Err(err).Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrCreatingCertSecret)).
Msgf("Error creating/retrieving root certificate from secret %s/%s", ns, secretName)
return nil, err
}
// For simplicity, we will load the certificate for all of them, this way the intance which created it
// and the ones that didn't share the same code.
cert, err := GetCertFromKubernetes(ns, secretName, kubeClient)
if err != nil {
log.Error().Err(err).Msg("Failed to fetch certificate from Kubernetes")
return nil, err
}
return cert, nil
}
// getTresorOSMCertificateManager returns a certificate manager instance with Tresor as the certificate provider
func (c *Config) getTresorOSMCertificateManager() (certificate.Manager, debugger.CertificateManagerDebugger, error) {
var err error
var rootCert certificate.Certificater
// This part synchronizes CA creation using the inherent atomicity of kubernetes API backend
// Assuming multiple instances of Tresor are instantiated at the same time, only one of them will
// succeed to issue a "Create" of the secret. All other Creates will fail with "AlreadyExists".
// Regardless of success or failure, all instances can proceed to load the same CA.
rootCert, err = tresor.NewCA(constants.CertificationAuthorityCommonName, constants.CertificationAuthorityRootValidityPeriod, rootCertCountry, rootCertLocality, rootCertOrganization)
if err != nil {
return nil, nil, errors.Errorf("Failed to create new Certificate Authority with cert issuer %s", c.providerKind)
}
if rootCert == nil {
return nil, nil, errors.Errorf("Invalid root certificate created by cert issuer %s", c.providerKind)
}
if rootCert.GetPrivateKey() == nil {
return nil, nil, errors.Errorf("Root cert does not have a private key")
}
rootCert, err = GetCertificateFromSecret(c.providerNamespace, c.caBundleSecretName, rootCert, c.kubeClient)
if err != nil {
return nil, nil, errors.Errorf("Failed to synchronize certificate on Secrets API : %v", err)
}
certManager, err := tresor.NewCertManager(
rootCert,
rootCertOrganization,
c.cfg,
c.cfg.GetServiceCertValidityPeriod(),
c.cfg.GetCertKeyBitSize(),
)
if err != nil {
return nil, nil, errors.Errorf("Failed to instantiate Tresor as a Certificate Manager")
}
return certManager, certManager, nil
}
// GetCertFromKubernetes is a helper function that loads a certificate from a Kubernetes secret
// The function returns an error only if a secret is found with invalid data.
func GetCertFromKubernetes(ns string, secretName string, kubeClient kubernetes.Interface) (certificate.Certificater, error) {
certSecret, err := kubeClient.CoreV1().Secrets(ns).Get(context.Background(), secretName, metav1.GetOptions{})
if err != nil {
// TODO(#3962): metric might not be scraped before process restart resulting from this error
log.Error().Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrFetchingCertSecret)).
Msgf("Could not retrieve certificate secret %q from namespace %q", secretName, ns)
return nil, errSecretNotFound
}
pemCert, ok := certSecret.Data[constants.KubernetesOpaqueSecretCAKey]
if !ok {
// TODO(#3962): metric might not be scraped before process restart resulting from this error
log.Error().Err(errInvalidCertSecret).Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrObtainingCertFromSecret)).
Msgf("Opaque k8s secret %s/%s does not have required field %q", ns, secretName, constants.KubernetesOpaqueSecretCAKey)
return nil, errInvalidCertSecret
}
pemKey, ok := certSecret.Data[constants.KubernetesOpaqueSecretRootPrivateKeyKey]
if !ok {
// TODO(#3962): metric might not be scraped before process restart resulting from this error
log.Error().Err(errInvalidCertSecret).Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrObtainingPrivateKeyFromSecret)).
Msgf("Opaque k8s secret %s/%s does not have required field %q", ns, secretName, constants.KubernetesOpaqueSecretRootPrivateKeyKey)
return nil, errInvalidCertSecret
}
expirationBytes, ok := certSecret.Data[constants.KubernetesOpaqueSecretCAExpiration]
if !ok {
// TODO(#3962): metric might not be scraped before process restart resulting from this error
log.Error().Err(errInvalidCertSecret).Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrObtainingCertExpirationFromSecret)).
Msgf("Opaque k8s secret %s/%s does not have required field %q", ns, secretName, constants.KubernetesOpaqueSecretCAExpiration)
return nil, errInvalidCertSecret
}
expiration, err := time.Parse(constants.TimeDateLayout, string(expirationBytes))
if err != nil {
// TODO(#3962): metric might not be scraped before process restart resulting from this error
log.Error().Err(err).Str(errcode.Kind, errcode.GetErrCodeWithMetric(errcode.ErrParsingCertExpiration)).
Msgf("Error parsing cert expiration %q from Kubernetes rootCertSecret %q from namespace %q", string(expirationBytes), secretName, ns)
return nil, err
}
cert, err := tresor.NewCertificateFromPEM(pemCert, pemKey, expiration)
if err != nil {
log.Error().Err(err).Msg("Failed to create new Certificate from PEM")
return nil, err
}
return cert, nil
}
// getHashiVaultOSMCertificateManager returns a certificate manager instance with Hashi Vault as the certificate provider
func (c *Config) getHashiVaultOSMCertificateManager(options VaultOptions) (certificate.Manager, debugger.CertificateManagerDebugger, error) {
if _, ok := map[string]interface{}{"http": nil, "https": nil}[options.VaultProtocol]; !ok {
return nil, nil, errors.Errorf("Value %s is not a valid Hashi Vault protocol", options.VaultProtocol)
}
// A Vault address would have the following shape: "http://vault.default.svc.cluster.local:8200"
vaultAddr := fmt.Sprintf("%s://%s:%d", options.VaultProtocol, options.VaultHost, options.VaultPort)
vaultCertManager, err := vault.NewCertManager(
vaultAddr,
options.VaultToken,
options.VaultRole,
c.cfg,
c.cfg.GetServiceCertValidityPeriod(),
)
if err != nil {
return nil, nil, errors.Errorf("Error instantiating Hashicorp Vault as a Certificate Manager: %+v", err)
}
return vaultCertManager, vaultCertManager, nil
}
// getCertManagerOSMCertificateManager returns a certificate manager instance with cert-manager as the certificate provider
func (c *Config) getCertManagerOSMCertificateManager(options CertManagerOptions) (certificate.Manager, debugger.CertificateManagerDebugger, error) {
rootCertSecret, err := c.kubeClient.CoreV1().Secrets(c.providerNamespace).Get(context.TODO(), c.caBundleSecretName, metav1.GetOptions{})
if err != nil {
return nil, nil, fmt.Errorf("Failed to get cert-manager CA secret %s/%s: %s", c.providerNamespace, c.caBundleSecretName, err)
}
pemCert, ok := rootCertSecret.Data[constants.KubernetesOpaqueSecretCAKey]
if !ok {
return nil, nil, fmt.Errorf("Opaque k8s secret %s/%s does not have required field %q", c.providerNamespace, c.caBundleSecretName, constants.KubernetesOpaqueSecretCAKey)
}
rootCert, err := certmanager.NewRootCertificateFromPEM(pemCert)
if err != nil {
return nil, nil, fmt.Errorf("Failed to decode cert-manager CA certificate from secret %s/%s: %s", c.providerNamespace, c.caBundleSecretName, err)
}
client, err := cmversionedclient.NewForConfig(c.kubeConfig)
if err != nil {
return nil, nil, fmt.Errorf("Failed to build cert-manager client set: %s", err)
}
certmanagerCertManager, err := certmanager.NewCertManager(
rootCert,
client,
c.providerNamespace,
cmmeta.ObjectReference{
Name: options.IssuerName,
Kind: options.IssuerKind,
Group: options.IssuerGroup,
},
c.cfg,
c.cfg.GetServiceCertValidityPeriod(),
c.cfg.GetCertKeyBitSize(),
)
if err != nil {
return nil, nil, errors.Errorf("Error instantiating Jetstack cert-manager as a Certificate Manager: %+v", err)
}
return certmanagerCertManager, certmanagerCertManager, nil
}