-
Notifications
You must be signed in to change notification settings - Fork 22
/
basics.go
487 lines (447 loc) · 12.9 KB
/
basics.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
/*
* SPDX-FileCopyrightText: 2019 SAP SE or an SAP affiliate company and Gardener contributors
*
* SPDX-License-Identifier: Apache-2.0
*/
package functional
import (
"context"
"crypto/x509"
"time"
"github.com/gardener/cert-management/pkg/apis/cert/v1alpha1"
"github.com/gardener/cert-management/pkg/cert/legobridge"
"github.com/gardener/cert-management/test/functional/config"
"github.com/gardener/controller-manager-library/pkg/resources"
"github.com/gardener/controller-manager-library/pkg/utils"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
)
var basicTemplate = `
apiVersion: cert.gardener.cloud/v1alpha1
kind: Issuer
metadata:
name: {{.Name}}
namespace: {{.Namespace}}
spec:
{{if eq .Type "acme"}}
acme:
server: {{.Server}}
email: {{.Email}}
{{if not .PrivateKey }}
autoRegistration: {{.AutoRegistration}}
{{end}}
privateKeySecretRef:
name: {{.Name}}-secret
namespace: {{.Namespace}}
{{if .ExternalAccountBinding }}
externalAccountBinding:
keyID: {{ .ExternalAccountBinding.KeyID }}
keySecretRef:
name: {{.Name}}-eab-secret
namespace: {{.Namespace}}
{{end}}
{{if .SkipDNSChallengeValidation }}
skipDNSChallengeValidation: true
{{end}}
{{end}}
{{if .ExternalAccountBinding }}
---
apiVersion: v1
kind: Secret
metadata:
name: {{.Name}}-eab-secret
namespace: {{.Namespace}}
type: Opaque
data:
hmacKey: {{ .ExternalAccountBinding.HmacKey }}
{{end}}
{{if .PrivateKey }}
---
apiVersion: v1
kind: Secret
metadata:
name: {{.Name}}-secret
namespace: {{.Namespace}}
type: Opaque
data:
privateKey: {{ .PrivateKey }}
{{end}}
---
apiVersion: cert.gardener.cloud/v1alpha1
kind: Certificate
metadata:
name: cert1
namespace: {{.Namespace}}
spec:
commonName: cert1.{{.Domain}}
dnsNames:
- cert1a.{{.Domain}}
- cert1b.{{.Domain}}
secretName: cert1-secret
issuerRef:
name: {{.Name}}
---
apiVersion: cert.gardener.cloud/v1alpha1
kind: Certificate
metadata:
name: cert2
namespace: {{.Namespace}}
spec:
commonName: cert2.{{.Domain}}
issuerRef:
name: {{.Name}}
---
apiVersion: cert.gardener.cloud/v1alpha1
kind: Certificate
metadata:
name: cert2b
namespace: {{.Namespace}}
spec:
commonName: cert2.{{.Domain}}
issuerRef:
name: {{.Name}}
---
apiVersion: cert.gardener.cloud/v1alpha1
kind: Certificate
metadata:
name: cert3
namespace: {{.Namespace}}
spec:
commonName: cert3.{{.Domain}}
dnsNames:
- "*.cert3.{{.Domain}}"
issuerRef:
name: {{.Name}}
secretName: cert3-secret
secretLabels:
foo: bar
some.gardener.cloud/thing: "true"
---
apiVersion: v1
kind: Secret
metadata:
name: cert4-secret
namespace: {{.Namespace}}
type: Opaque
---
apiVersion: cert.gardener.cloud/v1alpha1
kind: Certificate
metadata:
name: cert4
namespace: {{.Namespace}}
labels:
cert.gardener.cloud/hash: a74c0e0a617fd1499cddac5136b8e09e3ca30edd4f173f7e73d8910b
spec:
commonName: cert4.{{.Domain}}
secretName: cert4-secret
issuerRef:
name: {{.Name}}
---
apiVersion: cert.gardener.cloud/v1alpha1
kind: Certificate
metadata:
name: cert5
namespace: {{.Namespace}}
spec:
commonName:
dnsNames:
- cert5.very-very-very-very-very-very-very-very-very-very-very-long.{{.Domain}} # more than 64 chars
- cert5.{{.Domain}}
secretName: cert5-secret
issuerRef:
name: {{.Name}}
---
apiVersion: cert.gardener.cloud/v1alpha1
kind: Certificate
metadata:
name: cert6
namespace: {{.Namespace}}
spec:
commonName: cert6.{{.Domain}}
secretName: cert6-secret
issuerRef:
name: {{.Name}}
privateKey:
algorithm: RSA
size: 3072
---
apiVersion: cert.gardener.cloud/v1alpha1
kind: Certificate
metadata:
name: cert7
namespace: {{.Namespace}}
spec:
commonName: cert7.{{.Domain}}
secretName: cert7-secret
issuerRef:
name: {{.Name}}
privateKey:
algorithm: RSA
size: 4096
---
apiVersion: cert.gardener.cloud/v1alpha1
kind: Certificate
metadata:
name: cert8
namespace: {{.Namespace}}
spec:
commonName: cert8.{{.Domain}}
secretName: cert8-secret
issuerRef:
name: {{.Name}}
privateKey:
algorithm: ECDSA
size: 256
---
apiVersion: cert.gardener.cloud/v1alpha1
kind: Certificate
metadata:
name: cert9
namespace: {{.Namespace}}
spec:
commonName: cert9.{{.Domain}}
secretName: cert9-secret
issuerRef:
name: {{.Name}}
privateKey:
algorithm: ECDSA
size: 384
`
var revoke2Template = `
apiVersion: cert.gardener.cloud/v1alpha1
kind: CertificateRevocation
metadata:
name: revoke-cert2
namespace: {{.Namespace}}
spec:
certificateRef:
name: cert2
namespace: {{.Namespace}}
`
var revoke3Template = `
apiVersion: cert.gardener.cloud/v1alpha1
kind: CertificateRevocation
metadata:
name: revoke-cert3
namespace: {{.Namespace}}
spec:
certificateRef:
name: cert3
namespace: {{.Namespace}}
renew: true
`
var cert3WithKeystores = `
apiVersion: v1
kind: Secret
metadata:
name: keystore-password
namespace: {{.Namespace}}
data:
password: cGFzcw== # pass
---
apiVersion: cert.gardener.cloud/v1alpha1
kind: Certificate
metadata:
name: cert3
namespace: {{.Namespace}}
spec:
commonName: cert3.{{.Domain}}
dnsNames:
- "*.cert3.{{.Domain}}"
issuerRef:
name: {{.Name}}
keystores:
jks:
create: true
passwordSecretRef:
key: password
secretName: keystore-password
pkcs12:
create: true
passwordSecretRef:
key: password
secretName: keystore-password
secretName: cert3-secret
`
func init() {
utils.Must(resources.Register(v1alpha1.SchemeBuilder))
addIssuerTests(functestbasics)
}
func functestbasics(cfg *config.Config, iss *config.IssuerConfig) {
_ = Describe("basics-"+iss.Name, func() {
It("should work with "+iss.Name, func(_ context.Context) {
manifestFilename, err := iss.CreateTempManifest("Manifest", basicTemplate)
defer iss.DeleteTempManifest(manifestFilename)
Ω(err).ShouldNot(HaveOccurred())
u := cfg.Utils
err = u.AwaitKubectlGetCRDs("issuers.cert.gardener.cloud", "certificates.cert.gardener.cloud")
Ω(err).ShouldNot(HaveOccurred())
err = u.KubectlApply(manifestFilename)
Ω(err).ShouldNot(HaveOccurred())
err = u.AwaitIssuerReady(iss.Name)
Ω(err).ShouldNot(HaveOccurred())
entryNames := []string{}
for _, name := range []string{"1", "2", "2b", "3", "5", "6", "7", "8", "9"} {
entryNames = append(entryNames, entryName(iss, name))
}
err = u.AwaitCertReady(entryNames...)
Ω(err).ShouldNot(HaveOccurred())
itemMap, err := u.KubectlGetAllCertificates()
Ω(err).ShouldNot(HaveOccurred())
Ω(itemMap).Should(MatchKeys(IgnoreExtras, Keys{
entryName(iss, "1"): MatchKeys(IgnoreExtras, Keys{
"metadata": MatchKeys(IgnoreExtras, Keys{
"labels": MatchKeys(IgnoreExtras, Keys{
"cert.gardener.cloud/hash": HavePrefix(""),
}),
}),
"spec": MatchKeys(IgnoreExtras, Keys{
"secretRef": MatchKeys(IgnoreExtras, Keys{
"name": HavePrefix(entryName(iss, "1") + "-"),
"namespace": Equal(iss.Namespace),
}),
}),
"status": MatchKeys(IgnoreExtras, Keys{
"commonName": Equal(dnsName(iss, "cert1")),
"dnsNames": And(HaveLen(2), ContainElement(dnsName(iss, "cert1a")), ContainElement(dnsName(iss, "cert1b"))),
"state": Equal("Ready"),
"expirationDate": HavePrefix("20"),
}),
}),
entryName(iss, "2"): MatchKeys(IgnoreExtras, Keys{
"status": MatchKeys(IgnoreExtras, Keys{
"commonName": Equal(dnsName(iss, "cert2")),
"state": Equal("Ready"),
"expirationDate": HavePrefix("20"),
}),
}),
entryName(iss, "2b"): MatchKeys(IgnoreExtras, Keys{
"status": MatchKeys(IgnoreExtras, Keys{
"commonName": Equal(dnsName(iss, "cert2")),
"state": Equal("Ready"),
"expirationDate": HavePrefix("20"),
}),
}),
entryName(iss, "3"): MatchKeys(IgnoreExtras, Keys{
"status": MatchKeys(IgnoreExtras, Keys{
"commonName": Equal(dnsName(iss, "cert3")),
"dnsNames": And(HaveLen(1), ContainElement(dnsName(iss, "*.cert3"))),
"state": Equal("Ready"),
"expirationDate": HavePrefix("20"),
}),
}),
entryName(iss, "5"): MatchKeys(IgnoreExtras, Keys{
"status": MatchKeys(IgnoreExtras, Keys{
"dnsNames": And(HaveLen(2), ContainElements(dnsName(iss, "cert5.very-very-very-very-very-very-very-very-very-very-very-long"), dnsName(iss, "cert5"))),
"state": Equal("Ready"),
"expirationDate": HavePrefix("20"),
}),
}),
entryName(iss, "6"): MatchKeys(IgnoreExtras, Keys{
"status": MatchKeys(IgnoreExtras, Keys{
"commonName": Equal(dnsName(iss, "cert6")),
"state": Equal("Ready"),
"expirationDate": HavePrefix("20"),
}),
}),
entryName(iss, "7"): MatchKeys(IgnoreExtras, Keys{
"status": MatchKeys(IgnoreExtras, Keys{
"commonName": Equal(dnsName(iss, "cert7")),
"state": Equal("Ready"),
"expirationDate": HavePrefix("20"),
}),
}),
entryName(iss, "8"): MatchKeys(IgnoreExtras, Keys{
"status": MatchKeys(IgnoreExtras, Keys{
"commonName": Equal(dnsName(iss, "cert8")),
"state": Equal("Ready"),
"expirationDate": HavePrefix("20"),
}),
}),
entryName(iss, "9"): MatchKeys(IgnoreExtras, Keys{
"status": MatchKeys(IgnoreExtras, Keys{
"commonName": Equal(dnsName(iss, "cert9")),
"state": Equal("Ready"),
"expirationDate": HavePrefix("20"),
}),
}),
}))
Ω(u.CheckCertificatePrivateKey("cert3-secret", x509.RSA, 2048)).ShouldNot(HaveOccurred())
Ω(u.CheckCertificatePrivateKey("cert6-secret", x509.RSA, 3072)).ShouldNot(HaveOccurred())
Ω(u.CheckCertificatePrivateKey("cert7-secret", x509.RSA, 4096)).ShouldNot(HaveOccurred())
Ω(u.CheckCertificatePrivateKey("cert8-secret", x509.ECDSA, 256)).ShouldNot(HaveOccurred())
Ω(u.CheckCertificatePrivateKey("cert9-secret", x509.ECDSA, 384)).ShouldNot(HaveOccurred())
By("check keystores in cert3", func() {
secret, err := u.KubectlGetSecret("cert3-secret")
Ω(err).ShouldNot(HaveOccurred())
Ω(secret.Data).Should(HaveLen(3))
manifestFilename, err := iss.CreateTempManifest("Manifest", cert3WithKeystores)
defer iss.DeleteTempManifest(manifestFilename)
Ω(err).ShouldNot(HaveOccurred())
err = u.KubectlApply(manifestFilename)
Ω(err).ShouldNot(HaveOccurred())
time.Sleep(3 * time.Second) // wait for reconciliation
secret, err = u.KubectlGetSecret("cert3-secret")
Ω(err).ShouldNot(HaveOccurred())
Ω(secret.Data).Should(HaveLen(7))
Ω(secret.Data[legobridge.JKSTruststoreKey]).ShouldNot(BeNil())
Ω(secret.Data[legobridge.JKSSecretKey]).ShouldNot(BeNil())
Ω(secret.Data[legobridge.PKCS12TruststoreKey]).ShouldNot(BeNil())
Ω(secret.Data[legobridge.PKCS12SecretKey]).ShouldNot(BeNil())
})
By("check secret labels in cert3", func() {
secret, err := u.KubectlGetSecret("cert3-secret")
Ω(err).ShouldNot(HaveOccurred())
Ω(secret.Labels["foo"]).Should(Equal("bar"))
Ω(secret.Labels["some.gardener.cloud/thing"]).Should(Equal("true"))
})
By("check starting from invalid state in cert4", func() {
err = u.AwaitCertReady("cert4")
Ω(err).ShouldNot(HaveOccurred())
})
By("revoking without renewal", func() {
// need to wait 30 seconds after certificate creation because of time drift (see func WasRequestedBefore() for details)
time.Sleep(30 * time.Second)
filename, err := iss.CreateTempManifest("revoke2", revoke2Template)
defer iss.DeleteTempManifest(filename)
Ω(err).ShouldNot(HaveOccurred())
err = u.KubectlApply(filename)
Ω(err).ShouldNot(HaveOccurred())
err = u.AwaitCertRevocationApplied("revoke-cert2")
Ω(err).ShouldNot(HaveOccurred())
err = u.AwaitCertRevoked(entryName(iss, "2"), entryName(iss, "2b"))
Ω(err).ShouldNot(HaveOccurred())
err = u.KubectlDelete(filename)
Ω(err).ShouldNot(HaveOccurred())
})
if !iss.SkipRevokeWithRenewal {
By("revoking with renewal", func() {
filename, err := iss.CreateTempManifest("revoke3", revoke3Template)
defer iss.DeleteTempManifest(filename)
Ω(err).ShouldNot(HaveOccurred())
err = u.KubectlApply(filename)
Ω(err).ShouldNot(HaveOccurred())
err = u.AwaitCertRevocationApplied("revoke-cert3")
Ω(err).ShouldNot(HaveOccurred())
err = u.AwaitCertReady(entryName(iss, "3"))
Ω(err).ShouldNot(HaveOccurred())
err = u.KubectlDelete(filename)
Ω(err).ShouldNot(HaveOccurred())
})
}
err = u.KubectlDelete(manifestFilename)
Ω(err).ShouldNot(HaveOccurred())
err = u.AwaitCertDeleted(entryNames...)
Ω(err).ShouldNot(HaveOccurred())
err = u.AwaitIssuerDeleted(iss.Name)
Ω(err).ShouldNot(HaveOccurred())
}, SpecTimeout(180*time.Second))
})
}
func dnsName(iss *config.IssuerConfig, name string) string {
return name + "." + iss.Domain
}
func entryName(_ *config.IssuerConfig, name string) string {
return "cert" + name
}