-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecret_controller_test.go
More file actions
446 lines (410 loc) · 15.5 KB
/
secret_controller_test.go
File metadata and controls
446 lines (410 loc) · 15.5 KB
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
// SPDX-FileCopyrightText: 2025 Deutsche Telekom IT GmbH
//
// SPDX-License-Identifier: Apache-2.0
package controller_test
import (
"context"
"time"
"github.com/google/uuid"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"gw.ei.telekom.de/rotator/internal/controller"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
)
func generateUuid(input string) []byte {
return []byte(uuid.NewSHA1(uuid.Nil, []byte(input)).String())
}
var _ = Describe("Secret Controller", Serial, func() {
var source *corev1.Secret = &corev1.Secret{}
var target *corev1.Secret = &corev1.Secret{}
const (
timeout = time.Second * 10
duration = time.Second * 10
interval = time.Millisecond * 250
)
AfterEach(func() {
// delete secrets
err := k8sClient.DeleteAllOf(ctx, &corev1.Secret{}, client.InNamespace(namespace))
Expect(err).NotTo(HaveOccurred(), "deletion of secret failed during cleanup")
Eventually(func(g Gomega) {
secrets := &corev1.SecretList{}
err = k8sClient.List(ctx, secrets, client.InNamespace(namespace))
g.Expect(err).NotTo(HaveOccurred())
g.Expect(secrets.Items).To(BeEmpty())
}, timeout, interval).Should(Succeed(), "secrets were not deleted within timeout during cleanup")
})
When("a source secret is created", func() {
BeforeEach(func() {
source = &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"rotator.gw.ei.telekom.de/source": "true",
"rotator.gw.ei.telekom.de/destination-secret-name": "target",
},
Name: "source",
Namespace: namespace,
},
Type: corev1.SecretTypeTLS,
Data: map[string][]byte{
"tls.crt": []byte("cert"),
"tls.key": []byte("key"),
},
}
Expect(k8sClient.Create(ctx, source)).To(Succeed(), "creation of source secret failed")
// wait for the target secret to be created
Eventually(func(g Gomega) {
err := k8sClient.Get(
ctx,
types.NamespacedName{Name: "target", Namespace: namespace},
target,
)
g.Expect(err).ShouldNot(HaveOccurred())
}, timeout, interval).Should(Succeed(), "controller did not create target secret within timeout")
})
It("creates a target secret with the correct name and namespace", func() {
By("setting the key and crt from the source in next-tls.*", func() {
Expect(target.Data["next-tls.crt"]).To(Equal([]byte("cert")))
Expect(target.Data["next-tls.key"]).To(Equal([]byte("key")))
})
By("leaving tls.* and prev-tls.* empty", func() {
Expect(target.Data["tls.crt"]).To(BeEmpty())
Expect(target.Data["tls.key"]).To(BeEmpty())
Expect(target.Data["tls.kid"]).To(BeEmpty())
Expect(target.Data["prev-tls.crt"]).To(BeEmpty())
Expect(target.Data["prev-tls.key"]).To(BeEmpty())
Expect(target.Data["prev-tls.kid"]).To(BeEmpty())
})
By("generating a UUID based on the cert and setting it as a kid", func() {
Expect(
target.Data["next-tls.kid"],
).To(Equal(generateUuid(string(source.Data["tls.crt"]))))
})
By("setting secret type tls", func() {
Expect(target.Type).To(Equal(corev1.SecretTypeTLS))
})
By("setting an owner reference to the source secret", func() {
Expect(target.OwnerReferences).To(HaveLen(1))
Expect(target.OwnerReferences[0].Name).To(Equal("source"))
Expect(target.OwnerReferences[0].Kind).To(Equal("Secret"))
})
})
Context("and the source secret changes", func() {
Context("and the cert in source and next-tls.crt are equal", func() {
BeforeEach(func() {
// we will just update an annotation to trigger a change
err := k8sClient.Get(
ctx,
types.NamespacedName{Name: "source", Namespace: namespace},
source,
)
Expect(err).ToNot(HaveOccurred())
source.Annotations["some-new-annotation"] = "some-new-value"
Expect(
k8sClient.Update(ctx, source),
).To(Succeed(), "update of source secret failed")
})
It("does not update the target secret", func() {
time.Sleep(time.Second * 2) // give controller a chance to reconcile
Consistently(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, types.NamespacedName{Name: "target", Namespace: namespace}, target)).
To(Succeed())
_, ok := target.Annotations["some-new-annotation"]
g.Expect(ok).To(BeFalse(), "target secret should not have been touched")
}, timeout, interval).Should(Succeed(), "the target secret should not have been updated")
})
})
Context("and the cert in source and next-tls.crt are different", func() {
It("is able to perform a full rotation", func() {
By("(1) manually changing the source", func() {
err := k8sClient.Get(
ctx,
types.NamespacedName{Name: "source", Namespace: namespace},
source,
)
Expect(err).ToNot(HaveOccurred())
source.Data["tls.crt"] = []byte("cert-rotation-1")
source.Data["tls.key"] = []byte("key-rotation-1")
Expect(
k8sClient.Update(ctx, source),
).To(Succeed(), "update of source secret by test runner failed")
})
By("(1) the values are rotated", func() {
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, types.NamespacedName{Name: "target", Namespace: namespace}, target)).
To(Succeed())
g.Expect(target.Data["next-tls.crt"]).
To(Equal([]byte("cert-rotation-1")))
g.Expect(target.Data["next-tls.key"]).
To(Equal([]byte("key-rotation-1")))
g.Expect(target.Data["next-tls.kid"]).
To(Equal(generateUuid("cert-rotation-1")))
g.Expect(target.Data["tls.crt"]).To(Equal([]byte("cert")))
g.Expect(target.Data["tls.key"]).To(Equal([]byte("key")))
g.Expect(target.Data["tls.kid"]).To(Equal(generateUuid("cert")))
g.Expect(target.Data["prev-tls.crt"]).To(BeEmpty())
g.Expect(target.Data["prev-tls.key"]).To(BeEmpty())
g.Expect(target.Data["prev-tls.kid"]).To(BeEmpty())
}, timeout, interval).Should(Succeed(), "controller did not produce the expected target secret within timeout")
})
By("(2) manually changing the source", func() {
err := k8sClient.Get(
ctx,
types.NamespacedName{Name: "source", Namespace: namespace},
source,
)
Expect(err).ToNot(HaveOccurred())
source.Data["tls.crt"] = []byte("cert-rotation-2")
source.Data["tls.key"] = []byte("key-rotation-2")
Expect(
k8sClient.Update(ctx, source),
).To(Succeed(), "update of source secret by test runner failed")
})
By("(2) the values are rotated", func() {
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, types.NamespacedName{Name: "target", Namespace: namespace}, target)).
To(Succeed())
g.Expect(target.Data["next-tls.crt"]).
To(Equal([]byte("cert-rotation-2")))
g.Expect(target.Data["next-tls.key"]).
To(Equal([]byte("key-rotation-2")))
g.Expect(target.Data["next-tls.kid"]).
To(Equal(generateUuid("cert-rotation-2")))
g.Expect(target.Data["tls.crt"]).To(Equal([]byte("cert-rotation-1")))
g.Expect(target.Data["tls.key"]).To(Equal([]byte("key-rotation-1")))
g.Expect(target.Data["tls.kid"]).
To(Equal(generateUuid("cert-rotation-1")))
g.Expect(target.Data["prev-tls.crt"]).To(Equal([]byte("cert")))
g.Expect(target.Data["prev-tls.key"]).To(Equal([]byte("key")))
g.Expect(target.Data["prev-tls.kid"]).To(Equal(generateUuid("cert")))
}, timeout, interval).Should(Succeed(), "controller did not produce the expected target secret within timeout")
})
By("(3) manually changing the source", func() {
err := k8sClient.Get(
ctx,
types.NamespacedName{Name: "source", Namespace: namespace},
source,
)
Expect(err).ToNot(HaveOccurred())
source.Data["tls.crt"] = []byte("cert-rotation-3")
source.Data["tls.key"] = []byte("key-rotation-3")
Expect(
k8sClient.Update(ctx, source),
).To(Succeed(), "update of source secret by test runner failed")
})
By("(3) the values are rotated", func() {
Eventually(func(g Gomega) {
g.Expect(k8sClient.Get(ctx, types.NamespacedName{Name: "target", Namespace: namespace}, target)).
To(Succeed())
g.Expect(target.Data["next-tls.crt"]).
To(Equal([]byte("cert-rotation-3")))
g.Expect(target.Data["next-tls.key"]).
To(Equal([]byte("key-rotation-3")))
g.Expect(target.Data["next-tls.kid"]).
To(Equal(generateUuid("cert-rotation-3")))
g.Expect(target.Data["tls.crt"]).To(Equal([]byte("cert-rotation-2")))
g.Expect(target.Data["tls.key"]).To(Equal([]byte("key-rotation-2")))
g.Expect(target.Data["tls.kid"]).
To(Equal(generateUuid("cert-rotation-2")))
g.Expect(target.Data["prev-tls.crt"]).
To(Equal([]byte("cert-rotation-1")))
g.Expect(target.Data["prev-tls.key"]).
To(Equal([]byte("key-rotation-1")))
g.Expect(target.Data["prev-tls.kid"]).
To(Equal(generateUuid("cert-rotation-1")))
}, timeout, interval).Should(Succeed(), "controller did not produce the expected target secret within timeout")
})
})
})
})
Context("and the source is deleted", func() {
BeforeEach(func() {
err := k8sClient.Delete(ctx, source)
Expect(err).To(Succeed(), "deletion of source secret failed")
})
It("keeps the target secret", func() {
Eventually(func(g Gomega) {
err := k8sClient.Get(
ctx,
types.NamespacedName{Name: "target", Namespace: namespace},
target,
)
g.Expect(err).ShouldNot(HaveOccurred())
By("removing the owner reference")
g.Expect(target.OwnerReferences).To(BeNil())
By("removing the deletionTimestamp")
g.Expect(target.DeletionTimestamp.IsZero()).To(BeTrue())
}, timeout, interval).Should(Succeed(), "controller did not process target correctly")
})
})
})
When("a secret is created without the source and target-name annotations", func() {
BeforeEach(func() {
source = &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "some-other-secret",
Namespace: namespace,
},
Type: corev1.SecretTypeTLS,
Data: map[string][]byte{
"tls.crt": []byte("cert"),
"tls.key": []byte("key"),
},
}
Expect(k8sClient.Create(ctx, source)).To(Succeed(), "creation of source secret failed")
})
It("does not reconcile the secret", func() {
Eventually(func(g Gomega) {
secrets := &corev1.SecretList{}
err := k8sClient.List(ctx, secrets, client.InNamespace(namespace))
Expect(err).ToNot(HaveOccurred(), "failed to list secrets")
Expect(
secrets.Items,
).To(HaveLen(1), "controller should not have created a target secret")
}, timeout, interval).Should(Succeed())
})
})
When("a source secret is created with empty tls.key or tls.crt values", func() {
BeforeEach(func() {
source = &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"rotator.gw.ei.telekom.de/source": "true",
"rotator.gw.ei.telekom.de/destination-secret-name": "target",
},
Name: "source",
Namespace: namespace,
},
Type: corev1.SecretTypeTLS,
Data: map[string][]byte{
"tls.crt": []byte(""),
"tls.key": []byte(""),
},
}
Expect(k8sClient.Create(ctx, source)).To(Succeed(), "creation of source secret failed")
})
It("does nothing", func() {
Eventually(func(g Gomega) {
secrets := &corev1.SecretList{}
err := k8sClient.List(ctx, secrets, client.InNamespace(namespace))
Expect(err).ToNot(HaveOccurred(), "failed to list secrets")
Expect(
secrets.Items,
).To(HaveLen(1), "controller should not have created a target secret")
}, timeout, interval).Should(Succeed())
})
})
When("a source secret is created without the expected fields", func() {
BeforeEach(func() {
source = &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Annotations: map[string]string{
"rotator.gw.ei.telekom.de/source": "true",
"rotator.gw.ei.telekom.de/destination-secret-name": "target",
},
Name: "source",
Namespace: namespace,
},
Data: map[string][]byte{
"other-key": []byte("other-val"),
},
}
Expect(k8sClient.Create(ctx, source)).To(Succeed(), "creation of source secret failed")
})
It("does nothing", func() {
Eventually(func(g Gomega) {
secrets := &corev1.SecretList{}
err := k8sClient.List(ctx, secrets, client.InNamespace(namespace))
Expect(err).ToNot(HaveOccurred(), "failed to list secrets")
Expect(
secrets.Items,
).To(HaveLen(1), "controller should not have created a target secret")
}, timeout, interval).Should(Succeed())
})
})
Context("error handling and edge cases", func() {
var err error
var erroringClient errorInjectingClient
var reconciler controller.SecretReconciler
JustBeforeEach(func() {
erroringClient = errorInjectingClient{
Client: k8sClient,
errorToThrow: err,
}
reconciler = controller.SecretReconciler{
Client: erroringClient,
Scheme: k8sClient.Scheme(),
SourceAnnotation: "rotator.gw.ei.telekom.de/source",
TargetNameAnnotation: "rotator.gw.ei.telekom.de/destination-secret-name",
Finalizer: "rotator.gw.ei.telekom.de/finalizer",
}
})
When("a reconcile is triggered and the resource is not found", func() {
BeforeEach(func() {
err = errors.NewNotFound(schema.GroupResource{}, "not found error")
})
It("returns no error and does nothing", func() {
var val ctrl.Result
val, err = reconciler.Reconcile(ctx, ctrl.Request{
NamespacedName: types.NamespacedName{
Name: "test-secret",
Namespace: "default",
},
})
Expect(err).NotTo(HaveOccurred())
Expect(val).To(BeEquivalentTo(ctrl.Result{}))
Eventually(func(g Gomega) {
secrets := &corev1.SecretList{}
err = k8sClient.List(ctx, secrets, client.InNamespace(namespace))
g.Expect(err).ToNot(HaveOccurred(), "failed to list secrets")
g.Expect(secrets.Items).
To(BeEmpty(), "controller should not have created a target secret")
}, timeout, interval).Should(Succeed())
})
})
When("there is different error while fetching the resource", func() {
BeforeEach(func() {
err = errors.NewBadRequest("some undefined err")
})
It("returns the error and does nothing", func() {
var val ctrl.Result
val, err = reconciler.Reconcile(ctx, ctrl.Request{
NamespacedName: types.NamespacedName{
Name: "test-secret",
Namespace: "default",
},
})
Expect(val).To(BeEquivalentTo(ctrl.Result{}))
Expect(err).To(BeEquivalentTo(errors.NewBadRequest("some undefined err")))
Eventually(func(g Gomega) {
secrets := &corev1.SecretList{}
err = k8sClient.List(ctx, secrets, client.InNamespace(namespace))
g.Expect(err).ToNot(HaveOccurred(), "failed to list secrets")
g.Expect(secrets.Items).
To(BeEmpty(), "controller should not have created a target secret")
}, timeout, interval).Should(Succeed())
})
})
})
})
// Simple extension of the client.Client interface that is able to inject errors.
type errorInjectingClient struct {
client.Client
errorToThrow error
}
func (c errorInjectingClient) Get(
ctx context.Context,
key client.ObjectKey,
obj client.Object,
opts ...client.GetOption,
) error {
if c.errorToThrow != nil {
return c.errorToThrow
}
return c.Client.Get(ctx, key, obj)
}