-
Notifications
You must be signed in to change notification settings - Fork 9
/
cfssl.go
567 lines (491 loc) · 17.7 KB
/
cfssl.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
/*
This file is part of Astarte.
Copyright 2020 Ispirata Srl
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package reconcile
import (
"context"
"encoding/json"
"errors"
"fmt"
cfsslcsr "github.com/cloudflare/cfssl/csr"
"github.com/cloudflare/cfssl/initca"
"github.com/openlyinc/pointy"
appsv1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
kerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
commontypes "github.com/astarte-platform/astarte-kubernetes-operator/apis/api/commontypes"
apiv1alpha1 "github.com/astarte-platform/astarte-kubernetes-operator/apis/api/v1alpha1"
"github.com/astarte-platform/astarte-kubernetes-operator/lib/deps"
"github.com/astarte-platform/astarte-kubernetes-operator/lib/misc"
"github.com/astarte-platform/astarte-kubernetes-operator/version"
)
// EnsureCFSSL reconciles CFSSL
func EnsureCFSSL(cr *apiv1alpha1.Astarte, c client.Client, scheme *runtime.Scheme) error {
// Validate where necessary
if err := validateCFSSLDefinition(cr.Spec.CFSSL); err != nil {
return err
}
if version.CheckConstraintAgainstAstarteVersion("< 1.0.0", cr.Spec.Version) == nil {
// Then it's a statefulset
return ensureCFSSLStatefulSet(cr, c, scheme)
}
// Otherwise, reconcile the deployment
return ensureCFSSLDeployment(cr, c, scheme)
}
func ensureCFSSLDeployment(cr *apiv1alpha1.Astarte, c client.Client, scheme *runtime.Scheme) error {
deploymentName := cr.Name + "-cfssl"
labels := map[string]string{"app": deploymentName}
// Ok. Shall we deploy?
if !pointy.BoolValue(cr.Spec.CFSSL.Deploy, true) {
log.Info("Skipping CFSSL Deployment")
// Before returning - check if we shall clean up the Deployment.
// It is the only thing actually requiring resources, the rest will be cleaned up eventually when the
// Astarte resource is deleted.
theDeployment := &appsv1.Deployment{}
err := c.Get(context.TODO(), types.NamespacedName{Name: deploymentName, Namespace: cr.Namespace}, theDeployment)
if err == nil {
log.Info("Deleting previously existing CFSSL Deployment, which is no longer needed")
if err = c.Delete(context.TODO(), theDeployment); err != nil {
return err
}
}
// That would be all for today.
return nil
}
// Add common sidecars
if err := ensureCFSSLCommonSidecars(deploymentName, labels, cr, c, scheme); err != nil {
return err
}
caSecretName := cr.Name + "-devices-ca"
if cr.Spec.CFSSL.CASecret.Name != "" {
// Don't even try creating it
caSecretName = cr.Spec.CFSSL.CASecret.Name
} else if err := ensureCFSSLCASecret(caSecretName, cr, c, scheme); err != nil {
return err
}
// Ensure the proxy secret for TLS authentication
if err := ensureCFSSLCAProxySecret(caSecretName, cr.Name+"-cfssl-ca", cr, c, scheme); err != nil {
return err
}
// Compute and prepare all data for building the StatefulSet
deploymentSpec := appsv1.DeploymentSpec{
Selector: &metav1.LabelSelector{
MatchLabels: labels,
},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
Spec: getCFSSLPodSpec(deploymentName, "", caSecretName, cr),
},
}
// Build the Deployment
cfsslDeployment := &appsv1.Deployment{ObjectMeta: metav1.ObjectMeta{Name: deploymentName, Namespace: cr.Namespace}}
result, err := controllerutil.CreateOrUpdate(context.TODO(), c, cfsslDeployment, func() error {
if e := controllerutil.SetControllerReference(cr, cfsslDeployment, scheme); e != nil {
return e
}
// Assign the Spec.
cfsslDeployment.Spec = deploymentSpec
cfsslDeployment.Spec.Replicas = pointy.Int32(1)
return nil
})
if err != nil {
return err
}
misc.LogCreateOrUpdateOperationResult(log, result, cr, cfsslDeployment)
return nil
}
func ensureCFSSLStatefulSet(cr *apiv1alpha1.Astarte, c client.Client, scheme *runtime.Scheme) error {
statefulSetName := cr.Name + "-cfssl"
labels := map[string]string{"app": statefulSetName}
// Ok. Shall we deploy?
if !pointy.BoolValue(cr.Spec.CFSSL.Deploy, true) {
log.Info("Skipping CFSSL Deployment")
// Before returning - check if we shall clean up the StatefulSet.
// It is the only thing actually requiring resources, the rest will be cleaned up eventually when the
// Astarte resource is deleted.
theStatefulSet := &appsv1.StatefulSet{}
err := c.Get(context.TODO(), types.NamespacedName{Name: statefulSetName, Namespace: cr.Namespace}, theStatefulSet)
if err == nil {
log.Info("Deleting previously existing CFSSL StatefulSet, which is no longer needed")
if err = c.Delete(context.TODO(), theStatefulSet); err != nil {
return err
}
}
// That would be all for today.
return nil
}
// Add common sidecars
if err := ensureCFSSLCommonSidecars(statefulSetName, labels, cr, c, scheme); err != nil {
return err
}
// Let's check upon Storage now.
dataVolumeName, persistentVolumeClaim := computePersistentVolumeClaim(statefulSetName+"-data", resource.NewScaledQuantity(4, resource.Giga),
cr.Spec.CFSSL.Storage, cr)
// Compute and prepare all data for building the StatefulSet
statefulSetSpec := appsv1.StatefulSetSpec{
ServiceName: statefulSetName,
Selector: &metav1.LabelSelector{
MatchLabels: labels,
},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: computePodLabels(cr.Spec.CFSSL, labels),
},
Spec: getCFSSLPodSpec(statefulSetName, dataVolumeName, "", cr),
},
}
if persistentVolumeClaim != nil {
statefulSetSpec.VolumeClaimTemplates = []v1.PersistentVolumeClaim{*persistentVolumeClaim}
}
// Build the StatefulSet
cfsslStatefulSet := &appsv1.StatefulSet{ObjectMeta: metav1.ObjectMeta{Name: statefulSetName, Namespace: cr.Namespace}}
result, err := controllerutil.CreateOrUpdate(context.TODO(), c, cfsslStatefulSet, func() error {
if e := controllerutil.SetControllerReference(cr, cfsslStatefulSet, scheme); e != nil {
return e
}
// Assign the Spec.
cfsslStatefulSet.Spec = statefulSetSpec
cfsslStatefulSet.Spec.Replicas = pointy.Int32(1)
return nil
})
if err != nil {
return err
}
misc.LogCreateOrUpdateOperationResult(log, result, cr, cfsslStatefulSet)
return nil
}
func ensureCFSSLCommonSidecars(resourceName string, labels map[string]string, cr *apiv1alpha1.Astarte, c client.Client, scheme *runtime.Scheme) error {
// Good. Now, reconcile the service first of all.
service := &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: resourceName, Namespace: cr.Namespace}}
if result, err := controllerutil.CreateOrUpdate(context.TODO(), c, service, func() error {
if err := controllerutil.SetControllerReference(cr, service, scheme); err != nil {
return err
}
// Always set everything to what we require.
service.Spec.Type = v1.ServiceTypeClusterIP
service.Spec.Ports = []v1.ServicePort{
{
Name: "http",
Port: 80,
TargetPort: intstr.FromString("http"),
Protocol: v1.ProtocolTCP,
},
}
service.Spec.Selector = labels
return nil
}); err == nil {
misc.LogCreateOrUpdateOperationResult(log, result, cr, service)
} else {
return err
}
// Reconcile the ConfigMap
configMap, err := getCFSSLConfigMapData(cr)
if err != nil {
return err
}
if _, e := misc.ReconcileConfigMap(resourceName+"-config", configMap, cr, c, scheme, log); e != nil {
return e
}
// All good!
return nil
}
func validateCFSSLDefinition(cfssl commontypes.AstarteCFSSLSpec) error {
if !pointy.BoolValue(cfssl.Deploy, true) && cfssl.URL == "" {
return errors.New("When not deploying CFSSL, the 'url' must be specified")
}
// All is good.
return nil
}
func getCFSSLProbe(cr *apiv1alpha1.Astarte) *v1.Probe {
// HTTP Health is supported only from 0.11 on
if version.CheckConstraintAgainstAstarteComponentVersion("< 0.11.0", cr.Spec.CFSSL.Version, cr.Spec.Version) == nil {
return &v1.Probe{
Handler: v1.Handler{TCPSocket: &v1.TCPSocketAction{Port: intstr.FromString("http")}},
InitialDelaySeconds: 10,
TimeoutSeconds: 5,
PeriodSeconds: 30,
FailureThreshold: 3,
}
}
// Start checking after 10 seconds, every 20 seconds, fail after the 3rd attempt
return &v1.Probe{
Handler: v1.Handler{HTTPGet: &v1.HTTPGetAction{Path: "/api/v1/cfssl/health", Port: intstr.FromString("http")}},
InitialDelaySeconds: 10,
TimeoutSeconds: 5,
PeriodSeconds: 20,
FailureThreshold: 3,
}
}
// TODO: Deprecate dataVolumeName and all the jargon when we won't support < 1.0 anymore
func getCFSSLPodSpec(statefulSetName, dataVolumeName, secretName string, cr *apiv1alpha1.Astarte) v1.PodSpec {
// Defaults to the custom image built in Astarte
cfsslImage := getAstarteImageFromChannel("cfssl", deps.GetDefaultVersionForCFSSL(cr.Spec.Version), cr)
if cr.Spec.CFSSL.Image != "" {
cfsslImage = cr.Spec.CFSSL.Image
} else if cr.Spec.CFSSL.Version != "" {
cfsslImage = getAstarteImageFromChannel("cfssl", cr.Spec.CFSSL.Version, cr)
}
resources := v1.ResourceRequirements{}
if cr.Spec.CFSSL.Resources != nil {
resources = *cr.Spec.CFSSL.Resources
}
volumeMounts := []v1.VolumeMount{
{
Name: "config",
MountPath: "/etc/cfssl",
},
}
volumes := []v1.Volume{
{
Name: "config",
VolumeSource: v1.VolumeSource{
ConfigMap: &v1.ConfigMapVolumeSource{
LocalObjectReference: v1.LocalObjectReference{Name: statefulSetName + "-config"},
},
},
},
}
env := []v1.EnvVar{}
if dataVolumeName != "" {
volumeMounts = append(volumeMounts, v1.VolumeMount{
Name: dataVolumeName,
MountPath: "/data",
})
} else {
volumeMounts = append(volumeMounts, v1.VolumeMount{
Name: "devices-ca",
MountPath: "/devices-ca",
ReadOnly: true,
})
volumes = append(volumes, v1.Volume{
Name: "devices-ca",
VolumeSource: v1.VolumeSource{
Secret: &v1.SecretVolumeSource{
SecretName: secretName,
},
},
})
env = append(env, v1.EnvVar{
Name: "KUBERNETES",
Value: "1",
}, v1.EnvVar{
Name: "CFSSL_CA_CERTIFICATE",
Value: "/devices-ca/" + v1.TLSCertKey,
}, v1.EnvVar{
Name: "CFSSL_CA_PRIVATE_KEY",
Value: "/devices-ca/" + v1.TLSPrivateKeyKey,
})
if cr.Spec.CFSSL.DBConfig != nil {
env = append(env, v1.EnvVar{
Name: "CFSSL_USE_DB",
Value: "1",
})
}
}
ps := v1.PodSpec{
TerminationGracePeriodSeconds: pointy.Int64(30),
ImagePullSecrets: cr.Spec.ImagePullSecrets,
Containers: []v1.Container{
{
Name: "cfssl",
VolumeMounts: volumeMounts,
Env: env,
Image: cfsslImage,
ImagePullPolicy: getImagePullPolicy(cr),
Ports: []v1.ContainerPort{
{Name: "http", ContainerPort: 8080},
},
ReadinessProbe: getCFSSLProbe(cr),
LivenessProbe: getCFSSLProbe(cr),
Resources: resources,
},
},
Volumes: volumes,
}
return ps
}
func getCFSSLConfigMapData(cr *apiv1alpha1.Astarte) (map[string]string, error) {
// First of all, build the default maps.
caRootConfig, csrRootCa, e := getCFSSLConfigMapDataDefaults()
if e != nil {
return nil, e
}
dbConfig, e := getCFSSLDBConfig(cr)
if e != nil {
return nil, e
}
// Now. Do we have any overrides?
if cr.Spec.CFSSL.CaExpiry != "" {
csrRootCa["ca"] = map[string]string{"expiry": cr.Spec.CFSSL.CaExpiry}
}
if cr.Spec.CFSSL.CertificateExpiry != "" {
defaultObj := caRootConfig["signing"].(map[string]interface{})["default"].(map[string]interface{})
defaultObj["expiry"] = cr.Spec.CFSSL.CertificateExpiry
caRootConfig["signing"] = map[string]interface{}{"default": defaultObj}
}
// Are there any full overrides?
// CSR Root CA
if overriddenCSRRootCa, err := getCFSSLFullOverride(cr.Spec.CFSSL.CSRRootCa); err == nil && overriddenCSRRootCa != nil {
csrRootCa = overriddenCSRRootCa
} else if err != nil {
return nil, err
}
// Root CA Configuration
if overriddenCARootConfig, err := getCFSSLFullOverride(cr.Spec.CFSSL.CARootConfig); err == nil && overriddenCARootConfig != nil {
caRootConfig = overriddenCARootConfig
} else if err != nil {
return nil, err
}
// All good. Now, let's convert them to JSON and set the ConfigMap data.
dbConfigJSON, csrRootCaJSON, caRootConfigJSON, err := getCFSSLJSONFormattedConfigMapData(dbConfig, csrRootCa, caRootConfig)
if err != nil {
return nil, err
}
configMapData := map[string]string{
"ca_root_config.json": string(caRootConfigJSON),
"csr_root_ca.json": string(csrRootCaJSON),
}
// Add DB Config only if we have it, to adhere to all CFSSL variants
if dbConfigJSON != nil {
configMapData["db_config.json"] = string(dbConfigJSON)
}
return configMapData, nil
}
func getCFSSLDBConfig(cr *apiv1alpha1.Astarte) (map[string]interface{}, error) {
// By default, this one has to be nil...
var dbConfig map[string]interface{}
// ...unless we're < 1.0.0.
if version.CheckConstraintAgainstAstarteVersion("< 1.0.0", cr.Spec.Version) == nil {
// Then it's a statefulset
dbConfig = map[string]interface{}{"data_source": "/data/certs.db", "driver": "sqlite3"}
}
// DB Configuration
if overriddenDBConfig, err := getCFSSLFullOverride(cr.Spec.CFSSL.DBConfig); err == nil && overriddenDBConfig != nil {
dbConfig = overriddenDBConfig
// Switch in for retrocompatibility
if val, ok := dbConfig["dataSource"]; ok {
dbConfig["data_source"] = val
delete(dbConfig, "dataSource")
}
} else if err != nil {
return nil, err
}
return dbConfig, nil
}
func getCFSSLJSONFormattedConfigMapData(dbConfig, csrRootCa, caRootConfig map[string]interface{}) ([]byte, []byte, []byte, error) {
// dbConfig might be as well nil
var dbConfigJSON []byte
if dbConfig != nil {
var err error
dbConfigJSON, err = json.Marshal(dbConfig)
if err != nil {
return nil, nil, nil, err
}
}
csrRootCaJSON, err := json.Marshal(csrRootCa)
if err != nil {
return nil, nil, nil, err
}
caRootConfigJSON, err := json.Marshal(caRootConfig)
if err != nil {
return nil, nil, nil, err
}
return dbConfigJSON, csrRootCaJSON, caRootConfigJSON, nil
}
func getCFSSLFullOverride(field interface{}) (map[string]interface{}, error) {
if field != nil {
overriddenPayload, err := json.Marshal(field)
if err != nil {
return nil, err
}
newMap := make(map[string]interface{})
if err := json.Unmarshal(overriddenPayload, &newMap); err != nil {
return nil, err
}
return newMap, nil
}
return nil, nil
}
func getCFSSLConfigMapDataDefaults() (map[string]interface{}, map[string]interface{}, error) {
caRootConfigDefaultJSON := `{"signing": {"default": {"usages": ["digital signature", "cert sign", "crl sign", "signing"], "ca_constraint": {"max_path_len_zero": true, "max_path_len": 0, "is_ca": true}, "expiry": "2190h"}}}`
csrRootCaDefaultJSON := `{"ca": {"expiry": "262800h"}, "CN": "Astarte Root CA", "key": {"algo": "rsa", "size": 2048}, "names": [{"C": "IT", "ST": "Lombardia", "L": "Milan", "O": "Astarte User", "OU": "IoT Division"}]}`
var caRootConfig, csrRootCa map[string]interface{}
if err := json.Unmarshal([]byte(caRootConfigDefaultJSON), &caRootConfig); err != nil {
return nil, nil, err
}
if err := json.Unmarshal([]byte(csrRootCaDefaultJSON), &csrRootCa); err != nil {
return nil, nil, err
}
return caRootConfig, csrRootCa, nil
}
// This stuff is useful for other components which need to interact with CFSSL
func getCFSSLURL(cr *apiv1alpha1.Astarte) string {
if cr.Spec.CFSSL.URL != "" {
return cr.Spec.CFSSL.URL
}
// We're on defaults then. Give the standard hostname + port for our service
return fmt.Sprintf("http://%s-cfssl.%s.svc.cluster.local", cr.Name, cr.Namespace)
}
func ensureCFSSLCAProxySecret(secretName, proxySecretName string, cr *apiv1alpha1.Astarte, c client.Client, scheme *runtime.Scheme) error {
// Grab the real secret (the TLS one)
s := &v1.Secret{}
if err := c.Get(context.TODO(), types.NamespacedName{Name: secretName, Namespace: cr.Namespace}, s); err != nil {
return err
}
// Reconcile the proxy secret with ca.crt in place of our standard key
_, err := misc.ReconcileSecret(proxySecretName, map[string][]byte{"ca.crt": s.Data[v1.TLSCertKey]}, cr, c, scheme, log)
return err
}
func ensureCFSSLCASecret(secretName string, cr *apiv1alpha1.Astarte, c client.Client, scheme *runtime.Scheme) error {
// We want to ensure that we create / update the secret ONLY if it doesn't exist. So check that first.
s := &v1.Secret{}
if err := c.Get(context.TODO(), types.NamespacedName{Name: secretName, Namespace: cr.Namespace}, s); err == nil {
// A Secret exists. So we're good.
return nil
} else if !kerrors.IsNotFound(err) {
// An error of different nature occurred, so report it
return err
}
// If we got here, it doesn't exist. So go for it.
return createCFSSLCASecret(secretName, cr, c, scheme)
}
func createCFSSLCASecret(secretName string, cr *apiv1alpha1.Astarte, c client.Client, scheme *runtime.Scheme) error {
// Get our configuration
cfsslConfig, err := getCFSSLConfigMapData(cr)
if err != nil {
return err
}
// Prepare the request
req := cfsslcsr.CertificateRequest{
KeyRequest: cfsslcsr.NewKeyRequest(),
}
if e := json.Unmarshal([]byte(cfsslConfig["csr_root_ca.json"]), &req); e != nil {
return err
}
cert, _, key, err := initca.New(&req)
if err != nil {
return err
}
// Reconcile our secret
_, err = misc.ReconcileTLSSecret(secretName, string(cert), string(key), cr, c, scheme, log)
return err
}