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
241 lines (206 loc) · 8.72 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
package providers
import (
"context"
"errors"
"fmt"
"time"
cmmeta "github.com/jetstack/cert-manager/pkg/apis/meta/v1"
cmversionedclient "github.com/jetstack/cert-manager/pkg/client/clientset/versioned"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
"github.com/openservicemesh/osm/pkg/apis/config/v1alpha2"
"github.com/openservicemesh/osm/pkg/certificate"
"github.com/openservicemesh/osm/pkg/certificate/castorage/k8s"
"github.com/openservicemesh/osm/pkg/certificate/pem"
"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/k8s/informers"
"github.com/openservicemesh/osm/pkg/messaging"
)
const (
// Additional values for the root certificate
rootCertCountry = "US"
rootCertLocality = "CA"
rootCertOrganization = "Open Service Mesh"
)
var getCA = func(i certificate.Issuer) (pem.RootCertificate, error) {
cert, err := i.IssueCertificate("init-cert", 1*time.Second)
if err != nil {
return nil, err
}
return cert.GetIssuingCA(), nil
}
// NewCertificateManager returns a new certificate manager with a MRC compat client.
// TODO(4713): Remove and use NewCertificateManagerFromMRC
func NewCertificateManager(ctx context.Context, kubeClient kubernetes.Interface, kubeConfig *rest.Config, cfg configurator.Configurator,
providerNamespace string, option Options, msgBroker *messaging.Broker, checkInterval time.Duration, trustDomain string) (*certificate.Manager, error) {
if err := option.Validate(); err != nil {
return nil, err
}
mrcClient := &MRCCompatClient{
MRCProviderGenerator: MRCProviderGenerator{
kubeClient: kubeClient,
kubeConfig: kubeConfig,
KeyBitSize: cfg.GetCertKeyBitSize(),
caExtractorFunc: getCA,
},
mrc: &v1alpha2.MeshRootCertificate{
ObjectMeta: metav1.ObjectMeta{
Name: "legacy-compat",
Namespace: providerNamespace,
},
Spec: v1alpha2.MeshRootCertificateSpec{
Provider: option.AsProviderSpec(),
TrustDomain: trustDomain,
},
Status: v1alpha2.MeshRootCertificateStatus{
State: constants.MRCStateActive,
},
},
}
// TODO(#4745): Remove after deprecating the osm.vault.token option.
if vaultOption, ok := option.(VaultOptions); ok {
mrcClient.MRCProviderGenerator.DefaultVaultToken = vaultOption.VaultToken
}
return certificate.NewManager(ctx, mrcClient, cfg.GetServiceCertValidityPeriod, cfg.GetIngressGatewayCertValidityPeriod, msgBroker, checkInterval)
}
// NewCertificateManagerFromMRC returns a new certificate manager.
func NewCertificateManagerFromMRC(ctx context.Context, kubeClient kubernetes.Interface, kubeConfig *rest.Config, cfg configurator.Configurator,
providerNamespace string, option Options, msgBroker *messaging.Broker, ic *informers.InformerCollection, checkInterval time.Duration) (*certificate.Manager, error) {
if err := option.Validate(); err != nil {
return nil, err
}
mrcClient := &MRCComposer{
MRCProviderGenerator: MRCProviderGenerator{
kubeClient: kubeClient,
kubeConfig: kubeConfig,
KeyBitSize: cfg.GetCertKeyBitSize(),
caExtractorFunc: getCA,
},
informerCollection: ic,
}
// TODO(#4745): Remove after deprecating the osm.vault.token option.
if vaultOption, ok := option.(VaultOptions); ok {
mrcClient.MRCProviderGenerator.DefaultVaultToken = vaultOption.VaultToken
}
return certificate.NewManager(ctx, mrcClient, cfg.GetServiceCertValidityPeriod, cfg.GetIngressGatewayCertValidityPeriod, msgBroker, checkInterval)
}
// GetCertIssuerForMRC returns a certificate.Issuer generated from the provided MRC.
func (c *MRCProviderGenerator) GetCertIssuerForMRC(mrc *v1alpha2.MeshRootCertificate) (certificate.Issuer, pem.RootCertificate, error) {
p := mrc.Spec.Provider
var issuer certificate.Issuer
var err error
switch {
case p.Tresor != nil:
issuer, err = c.getTresorOSMCertificateManager(mrc)
case p.Vault != nil:
issuer, err = c.getHashiVaultOSMCertificateManager(mrc)
case p.CertManager != nil:
issuer, err = c.getCertManagerOSMCertificateManager(mrc)
default:
return nil, nil, fmt.Errorf("Unknown certificate provider: %+v", p)
}
if err != nil {
return nil, nil, err
}
ca, err := c.caExtractorFunc(issuer)
if err != nil {
return nil, nil, fmt.Errorf("error generating init cert: %w", err)
}
return issuer, ca, nil
}
// getTresorOSMCertificateManager returns a certificate manager instance with Tresor as the certificate provider
func (c *MRCProviderGenerator) getTresorOSMCertificateManager(mrc *v1alpha2.MeshRootCertificate) (certificate.Issuer, error) {
var err error
var rootCert *certificate.Certificate
// 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, errors.New("Failed to create new Certificate Authority with cert issuer tresor")
}
if rootCert.GetPrivateKey() == nil {
return nil, errors.New("Root cert does not have a private key")
}
rootCert, err = k8s.GetCertificateFromSecret(mrc.Namespace, mrc.Spec.Provider.Tresor.CA.SecretRef.Name, rootCert, c.kubeClient)
if err != nil {
return nil, fmt.Errorf("Failed to synchronize certificate on Secrets API : %w", err)
}
if rootCert.GetPrivateKey() == nil {
return nil, fmt.Errorf("Root cert does not have a private key: %w", certificate.ErrInvalidCertSecret)
}
tresorClient, err := tresor.New(
rootCert,
rootCertOrganization,
c.KeyBitSize,
)
if err != nil {
return nil, fmt.Errorf("failed to instantiate Tresor as a Certificate Manager: %w", err)
}
return tresorClient, nil
}
// getHashiVaultOSMCertificateManager returns a certificate manager instance with Hashi Vault as the certificate provider
func (c *MRCProviderGenerator) getHashiVaultOSMCertificateManager(mrc *v1alpha2.MeshRootCertificate) (certificate.Issuer, error) {
provider := mrc.Spec.Provider.Vault
// A Vault address would have the following shape: "http://vault.default.svc.cluster.local:8200"
vaultAddr := fmt.Sprintf("%s://%s:%d", provider.Protocol, provider.Host, provider.Port)
// If the DefaultVaultToken is empty, query Vault token secret
var err error
vaultToken := c.DefaultVaultToken
if vaultToken == "" {
log.Debug().Msgf("Attempting to get Vault token from secret %s", provider.Token.SecretKeyRef.Name)
vaultToken, err = getHashiVaultOSMToken(&provider.Token.SecretKeyRef, c.kubeClient)
if err != nil {
return nil, err
}
}
vaultClient, err := vault.New(
vaultAddr,
vaultToken,
provider.Role,
)
if err != nil {
return nil, fmt.Errorf("error instantiating Hashicorp Vault as a Certificate Manager: %w", err)
}
return vaultClient, nil
}
// getHashiVaultOSMToken returns the Hashi Vault token from the secret specified in the provided secret key reference
func getHashiVaultOSMToken(secretKeyRef *v1alpha2.SecretKeyReferenceSpec, kubeClient kubernetes.Interface) (string, error) {
tokenSecret, err := kubeClient.CoreV1().Secrets(secretKeyRef.Namespace).Get(context.TODO(), secretKeyRef.Name, metav1.GetOptions{})
if err != nil {
return "", fmt.Errorf("error retrieving Hashi Vault token secret %s/%s: %w", secretKeyRef.Namespace, secretKeyRef.Name, err)
}
token, ok := tokenSecret.Data[secretKeyRef.Key]
if !ok {
return "", fmt.Errorf("key %s not found in Hashi Vault token secret %s/%s", secretKeyRef.Key, secretKeyRef.Namespace, secretKeyRef.Name)
}
return string(token), nil
}
// getCertManagerOSMCertificateManager returns a certificate manager instance with cert-manager as the certificate provider
func (c *MRCProviderGenerator) getCertManagerOSMCertificateManager(mrc *v1alpha2.MeshRootCertificate) (certificate.Issuer, error) {
provider := mrc.Spec.Provider.CertManager
client, err := cmversionedclient.NewForConfig(c.kubeConfig)
if err != nil {
return nil, fmt.Errorf("Failed to build cert-manager client set: %w", err)
}
cmClient, err := certmanager.New(
client,
mrc.Namespace,
cmmeta.ObjectReference{
Name: provider.IssuerName,
Kind: provider.IssuerKind,
Group: provider.IssuerGroup,
},
c.KeyBitSize,
)
if err != nil {
return nil, fmt.Errorf("error instantiating Jetstack cert-manager client: %w", err)
}
return cmClient, nil
}