-
Notifications
You must be signed in to change notification settings - Fork 2k
/
suite.go
525 lines (445 loc) · 17.9 KB
/
suite.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
/*
Copyright 2019 The Jetstack cert-manager contributors.
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 certificates
import (
"context"
"strings"
"time"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
apiutil "github.com/jetstack/cert-manager/pkg/api/util"
cmapi "github.com/jetstack/cert-manager/pkg/apis/certmanager/v1alpha2"
cmmeta "github.com/jetstack/cert-manager/pkg/apis/meta/v1"
"github.com/jetstack/cert-manager/pkg/util"
"github.com/jetstack/cert-manager/pkg/util/pki"
"github.com/jetstack/cert-manager/test/e2e/framework"
e2eutil "github.com/jetstack/cert-manager/test/e2e/util"
)
// Suite defines a reusable conformance test suite that can be used against any
// Issuer implementation.
type Suite struct {
// Name is the name of the issuer being tested, e.g. SelfSigned, CA, ACME
// This field must be provided.
Name string
// CreateIssuerFunc is a function that provisions a new issuer resource and
// returns an ObjectReference to that Issuer that will be used as the
// IssuerRef on Certificate resources that this suite creates.
// This field must be provided.
CreateIssuerFunc func(*framework.Framework) cmmeta.ObjectReference
// DeleteIssuerFunc is a function that is run after the test has completed
// in order to clean up resources created for a test (e.g. the resources
// created in CreateIssuerFunc).
// This function will be run regardless whether the test passes or fails.
// If not specified, this function will be skipped.
DeleteIssuerFunc func(*framework.Framework, cmmeta.ObjectReference)
// DomainSuffix is a suffix used on all domain requests.
// This is useful when the issuer being tested requires special
// configuration for a set of domains in order for certificates to be
// issued, such as the ACME issuer.
// If not set, this will be defaulted to the configured 'domain' for the
// nginx-ingress addon.
DomainSuffix string
// UnsupportedFeatures is a list of features that are not supported by this
// invocation of the test suite.
// This is useful if a particular issuers explicitly does not support
// certain features due to restrictions in their implementation.
UnsupportedFeatures FeatureSet
// completed is used internally to track whether Complete() has been called
completed bool
}
// complete will validate configuration and set default values.
func (s *Suite) complete(f *framework.Framework) {
// TODO: work out how to fail an entire 'Describe' block so we can validate these are correctly set
//Expect(s.Name).NotTo(Equal(""), "Name must be set")
//Expect(s.CreateIssuerFunc).NotTo(BeNil(), "CreateIssuerFunc must be set")
if s.DomainSuffix == "" {
s.DomainSuffix = f.Config.Addons.Nginx.Global.Domain
}
if s.UnsupportedFeatures == nil {
s.UnsupportedFeatures = make(FeatureSet)
}
s.completed = true
}
// Defines simple conformance tests that can be run against any issuer type.
// If Complete has not been called on this Suite before Define, it will be
// automatically called.
func (s *Suite) Define() {
Describe("with issuer type "+s.Name, func() {
f := framework.NewDefaultFramework("certificates")
// wrap this in a BeforeEach else flags will not have been parsed at
// the time that the `complete` function is called.
BeforeEach(func() {
if !s.completed {
s.complete(f)
}
})
By("Running test suite with the following unsupported features: " + s.UnsupportedFeatures.String())
ctx := context.Background()
var issuerRef cmmeta.ObjectReference
JustBeforeEach(func() {
By("Creating an issuer resource")
issuerRef = s.CreateIssuerFunc(f)
})
JustAfterEach(func() {
if s.DeleteIssuerFunc == nil {
By("Skipping cleanup as no DeleteIssuerFunc provided")
return
}
By("Cleaning up the issuer resource")
s.DeleteIssuerFunc(f, issuerRef)
})
It("should issue a basic, defaulted certificate for a single distinct DNS Name", func() {
testCertificate := &cmapi.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: "testcert",
Namespace: f.Namespace.Name,
},
Spec: cmapi.CertificateSpec{
SecretName: "testcert-tls",
IssuerRef: issuerRef,
DNSNames: []string{s.newDomain()},
},
}
By("Creating a Certificate")
err := f.CRClient.Create(ctx, testCertificate)
Expect(err).NotTo(HaveOccurred())
By("Waiting for the Certificate to be issued...")
err = f.Helper().WaitCertificateIssuedValid(f.Namespace.Name, "testcert", time.Minute*5)
Expect(err).NotTo(HaveOccurred())
})
It("should issue an ECDSA, defaulted certificate for a single distinct dnsName", func() {
s.checkFeatures(ECDSAFeature)
testCertificate := &cmapi.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: "testcert",
Namespace: f.Namespace.Name,
},
Spec: cmapi.CertificateSpec{
SecretName: "testcert-tls",
KeyAlgorithm: cmapi.ECDSAKeyAlgorithm,
DNSNames: []string{s.newDomain()},
IssuerRef: issuerRef,
},
}
By("Creating a Certificate")
err := f.CRClient.Create(ctx, testCertificate)
Expect(err).NotTo(HaveOccurred())
By("Waiting for the Certificate to be issued...")
err = f.Helper().WaitCertificateIssuedValid(f.Namespace.Name, "testcert", time.Minute*5)
Expect(err).NotTo(HaveOccurred())
})
It("should issue a basic, defaulted certificate for a single Common Name", func() {
s.checkFeatures(CommonNameFeature)
testCertificate := &cmapi.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: "testcert",
Namespace: f.Namespace.Name,
},
Spec: cmapi.CertificateSpec{
SecretName: "testcert-tls",
IssuerRef: issuerRef,
CommonName: "test-common-name",
},
}
By("Creating a Certificate")
err := f.CRClient.Create(ctx, testCertificate)
Expect(err).NotTo(HaveOccurred())
By("Waiting for the Certificate to be issued...")
err = f.Helper().WaitCertificateIssuedValid(f.Namespace.Name, "testcert", time.Minute*5)
Expect(err).NotTo(HaveOccurred())
})
It("should issue an ECDSA, defaulted certificate for a single Common Name", func() {
s.checkFeatures(ECDSAFeature)
s.checkFeatures(CommonNameFeature)
testCertificate := &cmapi.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: "testcert",
Namespace: f.Namespace.Name,
},
Spec: cmapi.CertificateSpec{
SecretName: "testcert-tls",
KeyAlgorithm: cmapi.ECDSAKeyAlgorithm,
CommonName: "test-common-name",
IssuerRef: issuerRef,
},
}
By("Creating a Certificate")
err := f.CRClient.Create(ctx, testCertificate)
Expect(err).NotTo(HaveOccurred())
By("Waiting for the Certificate to be issued...")
err = f.Helper().WaitCertificateIssuedValid(f.Namespace.Name, "testcert", time.Minute*5)
Expect(err).NotTo(HaveOccurred())
})
It("should issue a certificate that defines a Common Name and IP Address", func() {
s.checkFeatures(CommonNameFeature)
s.checkFeatures(IPAddressFeature)
testCertificate := &cmapi.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: "testcert",
Namespace: f.Namespace.Name,
},
Spec: cmapi.CertificateSpec{
SecretName: "testcert-tls",
CommonName: "test-common-name",
IPAddresses: []string{"127.0.0.1"},
IssuerRef: issuerRef,
},
}
By("Creating a Certificate")
err := f.CRClient.Create(ctx, testCertificate)
Expect(err).NotTo(HaveOccurred())
By("Waiting for the Certificate to be issued...")
err = f.Helper().WaitCertificateIssuedValid(f.Namespace.Name, "testcert", time.Minute*5)
Expect(err).NotTo(HaveOccurred())
})
It("should issue a certificate that defines a URI Name and URI SAN", func() {
s.checkFeatures(URISANsFeature)
s.checkFeatures(CommonNameFeature)
testCertificate := &cmapi.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: "testcert",
Namespace: f.Namespace.Name,
},
Spec: cmapi.CertificateSpec{
SecretName: "testcert-tls",
CommonName: "test-common-name",
URISANs: []string{"spiffe://cluster.local/ns/sandbox/sa/foo"},
IssuerRef: issuerRef,
},
}
By("Creating a Certificate")
err := f.CRClient.Create(ctx, testCertificate)
Expect(err).NotTo(HaveOccurred())
By("Waiting for the Certificate to be issued...")
err = f.Helper().WaitCertificateIssuedValid(f.Namespace.Name, "testcert", time.Minute*5)
Expect(err).NotTo(HaveOccurred())
})
It("should issue a certificate that defines a 2 distinct DNS Name with one copied to the Common Name", func() {
s.checkFeatures(CommonNameFeature)
testCertificate := &cmapi.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: "testcert",
Namespace: f.Namespace.Name,
},
Spec: cmapi.CertificateSpec{
SecretName: "testcert-tls",
CommonName: s.newDomain(),
IssuerRef: issuerRef,
},
}
testCertificate.Spec.DNSNames = []string{
testCertificate.Spec.CommonName, s.newDomain(),
}
By("Creating a Certificate")
err := f.CRClient.Create(ctx, testCertificate)
Expect(err).NotTo(HaveOccurred())
By("Waiting for the Certificate to be issued...")
err = f.Helper().WaitCertificateIssuedValid(f.Namespace.Name, "testcert", time.Minute*5)
Expect(err).NotTo(HaveOccurred())
})
It("should issue a certificate that defines a distinct DNS Name and another distinct Common Name", func() {
s.checkFeatures(CommonNameFeature)
testCertificate := &cmapi.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: "testcert",
Namespace: f.Namespace.Name,
},
Spec: cmapi.CertificateSpec{
SecretName: "testcert-tls",
CommonName: s.newDomain(),
IssuerRef: issuerRef,
DNSNames: []string{s.newDomain()},
},
}
By("Creating a Certificate")
err := f.CRClient.Create(ctx, testCertificate)
Expect(err).NotTo(HaveOccurred())
By("Waiting for the Certificate to be issued...")
err = f.Helper().WaitCertificateIssuedValid(f.Namespace.Name, "testcert", time.Minute*5)
Expect(err).NotTo(HaveOccurred())
})
It("should issue a certificate that defines a DNS Name and sets a duration", func() {
s.checkFeatures(DurationFeature)
testCertificate := &cmapi.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: "testcert",
Namespace: f.Namespace.Name,
},
Spec: cmapi.CertificateSpec{
SecretName: "testcert-tls",
IssuerRef: issuerRef,
DNSNames: []string{s.newDomain()},
Duration: &metav1.Duration{
Duration: time.Hour * 896,
},
},
}
By("Creating a Certificate")
err := f.CRClient.Create(ctx, testCertificate)
Expect(err).NotTo(HaveOccurred())
By("Waiting for the Certificate to be issued...")
err = f.Helper().WaitCertificateIssuedValid(f.Namespace.Name, "testcert", time.Minute*5)
Expect(err).NotTo(HaveOccurred())
// We set a weird time here as the duration with should never be used as
// a default by an issuer. This lets us test issuers are using our given
// duration.
// We set a 30 second buffer time here since Vault issues certificates
// with an extra 30 seconds on its duration.
f.CertificateDurationValid(testCertificate, time.Hour*896, 30*time.Second)
})
It("should issue a certificate which has a wildcard DNS name defined", func() {
s.checkFeatures(WildcardsFeature)
testCertificate := &cmapi.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: "testcert",
Namespace: f.Namespace.Name,
},
Spec: cmapi.CertificateSpec{
SecretName: "testcert-tls",
IssuerRef: issuerRef,
DNSNames: []string{"foo." + s.newDomain()},
},
}
By("Creating a Certificate")
err := f.CRClient.Create(ctx, testCertificate)
Expect(err).NotTo(HaveOccurred())
By("Waiting for the Certificate to be issued...")
err = f.Helper().WaitCertificateIssuedValid(f.Namespace.Name, "testcert", time.Minute*5)
Expect(err).NotTo(HaveOccurred())
})
It("should issue a certificate that includes only a URISANs name", func() {
s.checkFeatures(URISANsFeature)
testCertificate := &cmapi.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: "testcert",
Namespace: f.Namespace.Name,
},
Spec: cmapi.CertificateSpec{
SecretName: "testcert-tls",
URISANs: []string{
"spiffe://cluster.local/ns/sandbox/sa/foo",
},
IssuerRef: issuerRef,
},
}
By("Creating a Certificate")
err := f.CRClient.Create(ctx, testCertificate)
Expect(err).NotTo(HaveOccurred())
By("Waiting for the Certificate to be issued...")
err = f.Helper().WaitCertificateIssuedValid(f.Namespace.Name, "testcert", time.Minute*5)
Expect(err).NotTo(HaveOccurred())
})
It("should issue another certificate with the same private key if the existing certificate and CertificateRequest are deleted", func() {
s.checkFeatures(ReusePrivateKeyFeature)
testCertificate := &cmapi.Certificate{
ObjectMeta: metav1.ObjectMeta{
Name: "testcert",
Namespace: f.Namespace.Name,
},
Spec: cmapi.CertificateSpec{
SecretName: "testcert-tls",
DNSNames: []string{s.newDomain()},
IssuerRef: issuerRef,
},
}
By("Creating a Certificate")
err := f.CRClient.Create(ctx, testCertificate)
Expect(err).NotTo(HaveOccurred())
By("Waiting for the Certificate to be issued...")
err = f.Helper().WaitCertificateIssuedValid(f.Namespace.Name, "testcert", time.Minute*5)
Expect(err).NotTo(HaveOccurred())
By("Deleting existing certificate data in Secret and owned CertificateRequest")
expectedReqName, err := apiutil.ComputeCertificateRequestName(testCertificate)
Expect(err).NotTo(HaveOccurred(), "failed to generate expected name for created Certificate")
Expect(f.CertManagerClientSet.CertmanagerV1alpha2().
CertificateRequests(f.Namespace.Name).Delete(expectedReqName, &metav1.DeleteOptions{})).
NotTo(HaveOccurred(), "failed to delete owned CertificateRequest")
sec, err := f.KubeClientSet.CoreV1().Secrets(f.Namespace.Name).
Get(testCertificate.Spec.SecretName, metav1.GetOptions{})
Expect(err).NotTo(HaveOccurred(), "failed to get secret containing signed certificate key pair data")
sec = sec.DeepCopy()
crtPEM1 := sec.Data[corev1.TLSCertKey]
crt1, err := pki.DecodeX509CertificateBytes(crtPEM1)
Expect(err).NotTo(HaveOccurred(), "failed to get decode first signed certificate data")
sec.Data[corev1.TLSCertKey] = []byte{}
_, err = f.KubeClientSet.CoreV1().Secrets(f.Namespace.Name).Update(sec)
Expect(err).NotTo(HaveOccurred(), "failed to update secret by deleting the signed certificate data")
By("Waiting for the Certificate to re-issue a certificate")
sec, err = f.Helper().WaitForSecretCertificateData(f.Namespace.Name, sec.Name, time.Minute*5)
Expect(err).NotTo(HaveOccurred(), "failed to wait for secret to have a valid 2nd certificate")
crtPEM2 := sec.Data[corev1.TLSCertKey]
crt2, err := pki.DecodeX509CertificateBytes(crtPEM2)
Expect(err).NotTo(HaveOccurred(), "failed to get decode second signed certificate data")
By("Ensuing both certificates are signed by same private key")
match, err := pki.PublicKeysEqual(crt1.PublicKey, crt2.PublicKey)
Expect(err).NotTo(HaveOccurred(), "failed to check public keys of both signed certificates")
if !match {
Fail("Both signed certificates not signed by same private key")
}
})
It("should issue a basic, defaulted certificate for a single commonName and distinct dnsName defined by an ingress with annotations", func() {
ingClient := f.KubeClientSet.ExtensionsV1beta1().Ingresses(f.Namespace.Name)
name := "testcert-ingress"
secretName := "testcert-ingress-tls"
By("Creating an Ingress with the issuer name annotation set")
ingress, err := ingClient.Create(e2eutil.NewIngress(name, secretName, map[string]string{
"cert-manager.io/issuer": issuerRef.Name,
"cert-manager.io/issuer-kind": issuerRef.Kind,
"cert-manager.io/issuer-group": issuerRef.Group,
}, s.newDomain()))
Expect(err).NotTo(HaveOccurred())
certName := ingress.Spec.TLS[0].SecretName
By("Waiting for the Certificate to exist...")
Expect(e2eutil.WaitForCertificateToExist(
f.CertManagerClientSet.CertmanagerV1alpha2().Certificates(f.Namespace.Name), certName, time.Minute,
)).NotTo(HaveOccurred())
By("Waiting for the Certificate to be issued...")
err = f.Helper().WaitCertificateIssuedValid(f.Namespace.Name, certName, time.Minute*5)
Expect(err).NotTo(HaveOccurred())
})
})
}
// checkFeatures is a helper function that is used to ensure that the features
// required for a given test case are supported by the suite.
func (s *Suite) checkFeatures(fs ...Feature) {
unsupported := make(FeatureSet)
for _, f := range fs {
if s.UnsupportedFeatures.Contains(f) {
unsupported.Add(f)
}
}
// all features supported, return early!
if len(unsupported) == 0 {
return
}
Skip("skipping due to the following unsupported features: " + unsupported.String())
}
// newDomain will generate a new random subdomain of the DomainSuffix
func (s *Suite) newDomain() string {
return s.newDomainDepth(1)
}
// newDomainDepth return a new domain name with the given number of subdomains
// beneath the domain suffix.
// If depth is zero, the domain suffix will be returned,
// If depth is one, a random subdomain will be returned e.g. abcd.example.com,
// If depth is two, a random sub-subdomain will be returned e.g. abcd.efgh.example.com,
// and so on
func (s *Suite) newDomainDepth(depth int) string {
subdomains := make([]string, depth)
for i := 0; i < depth; i++ {
subdomains[i] = util.RandStringRunes(4)
}
return strings.Join(append(subdomains, s.DomainSuffix), ".")
}