/
sync_certs.go
471 lines (420 loc) · 16.6 KB
/
sync_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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
package scyllacluster
import (
"context"
"crypto/x509/pkix"
"fmt"
"net"
"sort"
"strings"
"time"
scyllav1 "github.com/scylladb/scylla-operator/pkg/api/scylla/v1"
"github.com/scylladb/scylla-operator/pkg/controllerhelpers"
ocrypto "github.com/scylladb/scylla-operator/pkg/crypto"
"github.com/scylladb/scylla-operator/pkg/features"
"github.com/scylladb/scylla-operator/pkg/helpers"
"github.com/scylladb/scylla-operator/pkg/internalapi"
okubecrypto "github.com/scylladb/scylla-operator/pkg/kubecrypto"
"github.com/scylladb/scylla-operator/pkg/naming"
"github.com/scylladb/scylla-operator/pkg/resourceapply"
"github.com/scylladb/scylla-operator/pkg/scheme"
cqlclientv1alpha1 "github.com/scylladb/scylla-operator/pkg/scylla/api/cqlclient/v1alpha1"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/errors"
utilfeature "k8s.io/apiserver/pkg/util/feature"
"k8s.io/klog/v2"
)
func getHostPort(host string, port int) string {
if port == 0 {
return host
}
return fmt.Sprintf("%s:%d", host, port)
}
func makeScyllaConnectionConfig(
sc *scyllav1.ScyllaCluster,
secrets map[string]*corev1.Secret,
configMaps map[string]*corev1.ConfigMap,
cqlsIngressPort int,
) (*corev1.Secret, error) {
clientCertSecretName := naming.GetScyllaClusterLocalUserAdminCertName(sc.Name)
clientCertSecret, found := secrets[clientCertSecretName]
if !found {
return nil, fmt.Errorf("secret %q doesn't exist or is not own by this object", naming.ManualRef(sc.Namespace, clientCertSecretName))
}
clientCertsBytes, clientKeyBytes, err := okubecrypto.GetCertKeyDataFromSecret(clientCertSecret)
if err != nil {
return nil, fmt.Errorf("can't get cert and key bytes from secret %q: %w", clientCertSecretName, err)
}
servingCAConfigMapName := naming.GetScyllaClusterLocalServingCAName(sc.Name)
servingCAConfigMap, found := configMaps[servingCAConfigMapName]
if !found {
return nil, fmt.Errorf("configmap %q doesn't exist or is not own by this object", naming.ManualRef(sc.Namespace, servingCAConfigMapName))
}
servingCABytes, err := okubecrypto.GetCABundleDataFromConfigMap(servingCAConfigMap)
if err != nil {
return nil, fmt.Errorf("can't get ca bundle bytes from configmap %q: %w", servingCAConfigMapName, err)
}
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Namespace: sc.Namespace,
Name: naming.GetScyllaClusterLocalAdminCQLConnectionConfigsName(sc.Name),
Labels: naming.ClusterLabels(sc),
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(sc, scyllaClusterControllerGVK),
},
},
Data: make(map[string][]byte, len(sc.Spec.DNSDomains)),
Type: corev1.SecretTypeOpaque,
}
for _, domain := range sc.Spec.DNSDomains {
scyllaConnectionConfig := &cqlclientv1alpha1.CQLConnectionConfig{
AuthInfos: map[string]*cqlclientv1alpha1.AuthInfo{
"admin": {
ClientCertificateData: clientCertsBytes,
ClientKeyData: clientKeyBytes,
Username: "cassandra",
Password: "cassandra",
},
},
Datacenters: map[string]*cqlclientv1alpha1.Datacenter{
sc.Spec.Datacenter.Name: {
Server: getHostPort(naming.GetCQLProtocolSubDomain(domain), cqlsIngressPort),
NodeDomain: naming.GetCQLProtocolSubDomain(domain),
CertificateAuthorityData: servingCABytes,
},
},
Contexts: map[string]*cqlclientv1alpha1.Context{
"default": {
AuthInfoName: "admin",
DatacenterName: sc.Spec.Datacenter.Name,
},
},
CurrentContext: "default",
Parameters: &cqlclientv1alpha1.CQLParameters{
DefaultConsistency: cqlclientv1alpha1.CQLDefaultQuorumConsistency,
DefaultSerialConsistency: cqlclientv1alpha1.CQLDefaultSerialConsistency,
},
}
encoder := scheme.Codecs.EncoderForVersion(scheme.DefaultYamlSerializer, schema.GroupVersions(scheme.Scheme.PrioritizedVersionsAllGroups()))
scyllaConnectionConfigData, err := runtime.Encode(encoder, scyllaConnectionConfig)
if err != nil {
return nil, fmt.Errorf("can't encode scylla connection config: %w", err)
}
secret.Data[domain] = scyllaConnectionConfigData
}
return secret, nil
}
func (scc *Controller) syncCerts(
ctx context.Context,
sc *scyllav1.ScyllaCluster,
secrets map[string]*corev1.Secret,
configMaps map[string]*corev1.ConfigMap,
serviceMap map[string]*corev1.Service,
) ([]metav1.Condition, error) {
// Be careful to always apply signers first to be sure they have been persisted
// before applying any child certificate signed by it.
var err error
var errs []error
var progressingConditions []metav1.Condition
cm := okubecrypto.NewCertificateManager(
scc.keyGetter,
scc.kubeClient.CoreV1(),
scc.secretLister,
scc.kubeClient.CoreV1(),
scc.configMapLister,
scc.eventRecorder,
)
clusterLabels := naming.ClusterLabels(sc)
if utilfeature.DefaultMutableFeatureGate.Enabled(features.AutomaticTLSCertificates) {
// Manage client certificates.
errs = append(errs, cm.ManageCertificates(
ctx,
time.Now,
&sc.ObjectMeta,
scyllaClusterControllerGVK,
&okubecrypto.CAConfig{
MetaConfig: okubecrypto.MetaConfig{
Name: naming.GetScyllaClusterLocalClientCAName(sc.Name),
Labels: clusterLabels,
},
Validity: 10 * 365 * 24 * time.Hour,
Refresh: 8 * 365 * 24 * time.Hour,
},
&okubecrypto.CABundleConfig{
MetaConfig: okubecrypto.MetaConfig{
Name: naming.GetScyllaClusterLocalClientCAName(sc.Name),
Labels: clusterLabels,
},
},
[]*okubecrypto.CertificateConfig{
{
MetaConfig: okubecrypto.MetaConfig{
Name: naming.GetScyllaClusterLocalUserAdminCertName(sc.Name),
Labels: clusterLabels,
},
Validity: 10 * 365 * 24 * time.Hour,
Refresh: 8 * 365 * 24 * time.Hour,
CertCreator: (&ocrypto.ClientCertCreatorConfig{
Subject: pkix.Name{
CommonName: "",
},
DNSNames: []string{"admin"},
}).ToCreator(),
},
},
secrets,
configMaps,
))
}
// Manage serving certificates.
// We are currently using StatefulSet that allows only a uniform config so we have to create a multi-SAN certificate.
// TODO: Create dedicated certs when we get rid of StatefulSets.
// TODO: we need to somehow check the domain given so we don't start signing stuff for e.g. google.com.
// (maybe cluster domains CR, or forcing a suffix). On the other hand the CA should be trusted
// only for the connection, not by the system.
// For the clients not using the proxy we'll sign for IPs that are published by ScyllaDB.
// We should also sign the identity service ClusterIP and internal DNS for the
// initial discovery. (The initial contact point is especially important when using ephemeral IPs.)
var ipAddresses []net.IP
var servingDNSNames []string
if utilfeature.DefaultMutableFeatureGate.Enabled(features.AutomaticTLSCertificates) || sc.Spec.Alternator != nil {
for _, svc := range serviceMap {
svcType := svc.Labels[naming.ScyllaServiceTypeLabel]
if svcType != string(naming.ScyllaServiceTypeMember) && svcType != string(naming.ScyllaServiceTypeIdentity) {
continue
}
if svc.Spec.ClusterIP != corev1.ClusterIPNone {
parsedIP, err := helpers.ParseIP(svc.Spec.ClusterIP)
if err != nil {
return progressingConditions, fmt.Errorf("can't parse Service %q ClusterIP %q: %w", naming.ObjRef(svc), svc.Spec.ClusterIP, err)
}
ipAddresses = append(ipAddresses, parsedIP)
}
if svc.Spec.Type == corev1.ServiceTypeLoadBalancer {
hasExternalAddress := false
for _, ingressStatus := range svc.Status.LoadBalancer.Ingress {
if len(ingressStatus.IP) != 0 {
parsedIP := net.ParseIP(ingressStatus.IP)
if parsedIP == nil {
return progressingConditions, fmt.Errorf("can't parse ingress IP %q", ingressStatus.IP)
}
hasExternalAddress = true
ipAddresses = append(ipAddresses, parsedIP)
}
if len(ingressStatus.Hostname) != 0 {
hasExternalAddress = true
servingDNSNames = append(servingDNSNames, ingressStatus.Hostname)
}
}
// There should be at least one address for ServiceTypeLoadBalancer.
if !hasExternalAddress {
progressingConditions = append(progressingConditions, metav1.Condition{
Type: certControllerProgressingCondition,
Status: metav1.ConditionTrue,
Reason: internalapi.ProgressingReason,
Message: fmt.Sprintf("waiting for external address in Service %q to be available", naming.ObjRef(svc)),
ObservedGeneration: sc.Generation,
})
}
}
// Only member services have associated pods
if svcType != string(naming.ScyllaServiceTypeMember) {
continue
}
pod, err := scc.podLister.Pods(sc.Namespace).Get(svc.Name)
if err != nil {
if apierrors.IsNotFound(err) {
progressingConditions = append(progressingConditions, metav1.Condition{
Type: certControllerProgressingCondition,
Status: metav1.ConditionTrue,
Reason: internalapi.ProgressingReason,
Message: fmt.Sprintf("waiting for Pod %q to be created", naming.ManualRef(sc.Namespace, svc.Name)),
ObservedGeneration: sc.Generation,
})
continue
}
return progressingConditions, fmt.Errorf("can't get Pod %q: %w", naming.ManualRef(sc.Namespace, svc.Name), err)
}
if len(pod.Status.PodIP) == 0 {
progressingConditions = append(progressingConditions, metav1.Condition{
Type: certControllerProgressingCondition,
Status: metav1.ConditionTrue,
Reason: internalapi.ProgressingReason,
Message: fmt.Sprintf("waiting for Pod %q to have an IP address", naming.ManualRef(sc.Namespace, svc.Name)),
ObservedGeneration: sc.Generation,
})
continue
}
parsedIP := net.ParseIP(pod.Status.PodIP)
if parsedIP == nil {
return progressingConditions, fmt.Errorf("can't parse Pod %q IP %q", naming.ObjRef(pod), pod.Status.PodIP)
}
ipAddresses = append(ipAddresses, parsedIP)
}
// Make sure ipAddresses are always sorted and can be reconciled in a declarative way.
sort.SliceStable(ipAddresses, func(i, j int) bool {
return ipAddresses[i].String() < ipAddresses[j].String()
})
var hostIDs []string
var progressingMessages []string
for _, svc := range serviceMap {
if svc.Labels[naming.ScyllaServiceTypeLabel] != string(naming.ScyllaServiceTypeMember) {
continue
}
hostID, hasHostID := svc.Annotations[naming.HostIDAnnotation]
if len(hostID) == 0 {
if hasHostID {
klog.Warningf("service %q has empty hostID", klog.KObj(svc))
}
message := fmt.Sprintf("waiting for service %q to have a hostID set", naming.ObjRef(svc))
progressingMessages = append(progressingMessages, message)
continue
}
hostIDs = append(hostIDs, hostID)
}
// For every cluster domain we'll create "cql" subdomain and "UUID.cql" subdomains for every node.
for _, domain := range sc.Spec.DNSDomains {
servingDNSNames = append(servingDNSNames, naming.GetCQLProtocolSubDomain(domain))
for _, hostID := range hostIDs {
servingDNSNames = append(servingDNSNames, naming.GetCQLHostIDSubDomain(hostID, domain))
}
}
// Sign for every node DNS name and discovery endpoint.
for _, svc := range serviceMap {
svcType := svc.Labels[naming.ScyllaServiceTypeLabel]
if svcType != string(naming.ScyllaServiceTypeIdentity) && svcType != string(naming.ScyllaServiceTypeMember) {
return progressingConditions, fmt.Errorf("can't sign certificate for DNS name of unknown service type %q", svcType)
}
servingDNSNames = append(servingDNSNames, fmt.Sprintf("%s.%s.svc", svc.Name, svc.Namespace))
}
// Make sure servingDNSNames are always sorted and can be reconciled in a declarative way.
sort.Strings(servingDNSNames)
if len(progressingMessages) != 0 {
progressingConditions = append(progressingConditions, metav1.Condition{
Type: certControllerProgressingCondition,
Status: metav1.ConditionTrue,
Reason: internalapi.ProgressingReason,
Message: strings.Join(progressingMessages, "\n"),
ObservedGeneration: sc.Generation,
})
}
}
if utilfeature.DefaultMutableFeatureGate.Enabled(features.AutomaticTLSCertificates) {
errs = append(errs, cm.ManageCertificates(
ctx,
time.Now,
&sc.ObjectMeta,
scyllaClusterControllerGVK,
&okubecrypto.CAConfig{
MetaConfig: okubecrypto.MetaConfig{
Name: naming.GetScyllaClusterLocalServingCAName(sc.Name),
Labels: clusterLabels,
},
Validity: 10 * 365 * 24 * time.Hour,
Refresh: 8 * 365 * 24 * time.Hour,
},
&okubecrypto.CABundleConfig{
MetaConfig: okubecrypto.MetaConfig{
Name: naming.GetScyllaClusterLocalServingCAName(sc.Name),
Labels: clusterLabels,
},
},
[]*okubecrypto.CertificateConfig{
{
MetaConfig: okubecrypto.MetaConfig{
Name: naming.GetScyllaClusterLocalServingCertName(sc.Name),
Labels: clusterLabels,
},
Validity: 30 * 24 * time.Hour,
Refresh: 20 * 24 * time.Hour,
CertCreator: (&ocrypto.ServingCertCreatorConfig{
Subject: pkix.Name{
CommonName: "",
},
IPAddresses: ipAddresses,
DNSNames: servingDNSNames,
}).ToCreator(),
},
},
secrets,
configMaps,
))
// Build connection bundle.
scyllaConnectionConfigSecret, err := makeScyllaConnectionConfig(sc, secrets, configMaps, scc.cqlsIngressPort)
if err != nil {
errs = append(errs, err)
} else {
_, changed, err := resourceapply.ApplySecret(ctx, scc.kubeClient.CoreV1(), scc.secretLister, scc.eventRecorder, scyllaConnectionConfigSecret, resourceapply.ApplyOptions{})
if changed {
controllerhelpers.AddGenericProgressingStatusCondition(&progressingConditions, certControllerProgressingCondition, scyllaConnectionConfigSecret, "apply", sc.Generation)
}
if err != nil {
return progressingConditions, fmt.Errorf("can't apply secret %q: %w", naming.ObjRef(scyllaConnectionConfigSecret), err)
}
}
}
// Setup Alternator certificates.
if sc.Spec.Alternator != nil &&
(sc.Spec.Alternator.ServingCertificate == nil || sc.Spec.Alternator.ServingCertificate.Type == scyllav1.TLSCertificateTypeOperatorManaged) {
var additionalDNSNames []string
if sc.Spec.Alternator.ServingCertificate.OperatorManagedOptions != nil && sc.Spec.Alternator.ServingCertificate.OperatorManagedOptions.AdditionalDNSNames != nil {
additionalDNSNames = sc.Spec.Alternator.ServingCertificate.OperatorManagedOptions.AdditionalDNSNames
}
alternatorDNSNames := make([]string, 0, len(servingDNSNames)+len(additionalDNSNames))
alternatorDNSNames = append(alternatorDNSNames, servingDNSNames...)
alternatorDNSNames = append(alternatorDNSNames, additionalDNSNames...)
var additionalIPAddresses []net.IP
if sc.Spec.Alternator.ServingCertificate.OperatorManagedOptions != nil && sc.Spec.Alternator.ServingCertificate.OperatorManagedOptions.AdditionalIPAddresses != nil {
additionalIPAddresses, err = helpers.ParseIPs(sc.Spec.Alternator.ServingCertificate.OperatorManagedOptions.AdditionalIPAddresses)
if err != nil {
return nil, fmt.Errorf("can't parse additional IP addresses for alternator serving cert: %w", err)
}
}
alternatorIPAddresses := make([]net.IP, 0, len(ipAddresses)+len(additionalIPAddresses))
alternatorIPAddresses = append(alternatorIPAddresses, ipAddresses...)
alternatorIPAddresses = append(alternatorIPAddresses, additionalIPAddresses...)
errs = append(errs, cm.ManageCertificates(
ctx,
time.Now,
&sc.ObjectMeta,
scyllaClusterControllerGVK,
&okubecrypto.CAConfig{
MetaConfig: okubecrypto.MetaConfig{
Name: naming.GetScyllaClusterAlternatorLocalServingCAName(sc.Name),
Labels: clusterLabels,
},
Validity: 10 * 365 * 24 * time.Hour,
Refresh: 8 * 365 * 24 * time.Hour,
},
&okubecrypto.CABundleConfig{
MetaConfig: okubecrypto.MetaConfig{
Name: naming.GetScyllaClusterAlternatorLocalServingCAName(sc.Name),
Labels: clusterLabels,
},
},
[]*okubecrypto.CertificateConfig{
{
MetaConfig: okubecrypto.MetaConfig{
Name: naming.GetScyllaClusterAlternatorLocalServingCertName(sc.Name),
Labels: clusterLabels,
},
Validity: 30 * 24 * time.Hour,
Refresh: 20 * 24 * time.Hour,
CertCreator: (&ocrypto.ServingCertCreatorConfig{
Subject: pkix.Name{
CommonName: "",
},
IPAddresses: alternatorIPAddresses,
DNSNames: alternatorDNSNames,
}).ToCreator(),
},
},
secrets,
configMaps,
))
}
return progressingConditions, errors.NewAggregate(errs)
}