-
Notifications
You must be signed in to change notification settings - Fork 462
/
certificate_authorities.go
130 lines (112 loc) · 6.14 KB
/
certificate_authorities.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
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Gardener contributors
//
// SPDX-License-Identifier: Apache-2.0
package rotation
import (
"context"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1"
v1beta1constants "github.com/gardener/gardener/pkg/apis/core/v1beta1/constants"
operatorv1alpha1 "github.com/gardener/gardener/pkg/apis/operator/v1alpha1"
"github.com/gardener/gardener/pkg/apis/operator/v1alpha1/helper"
"github.com/gardener/gardener/test/utils/rotation"
)
// CAVerifier verifies the certificate authorities rotation.
type CAVerifier struct {
RuntimeClient client.Client
Garden *operatorv1alpha1.Garden
secretsBefore rotation.SecretConfigNamesToSecrets
secretsPrepared rotation.SecretConfigNamesToSecrets
secretsCompleted rotation.SecretConfigNamesToSecrets
}
var allCAs = []string{
caCluster,
caClient,
caETCD,
caETCDPeer,
caFrontProxy,
caGardener,
}
const (
caCluster = "ca"
caClient = "ca-client"
caETCD = "ca-etcd"
caETCDPeer = "ca-etcd-peer"
caFrontProxy = "ca-front-proxy"
caGardener = "ca-gardener"
)
// Before is called before the rotation is started.
func (v *CAVerifier) Before(ctx context.Context) {
By("Verify CA secrets of gardener-operator before rotation")
Eventually(func(g Gomega) {
secretList := &corev1.SecretList{}
g.Expect(v.RuntimeClient.List(ctx, secretList, client.InNamespace(v1beta1constants.GardenNamespace), ManagedByGardenerOperatorSecretsManager)).To(Succeed())
grouped := rotation.GroupByName(secretList.Items)
for _, ca := range allCAs {
bundle := ca + "-bundle"
g.Expect(grouped[ca]).To(HaveLen(1), ca+" secret should get created, but not rotated yet")
g.Expect(grouped[bundle]).To(HaveLen(1), ca+" bundle secret should get created, but not rotated yet")
}
v.secretsBefore = grouped
}).Should(Succeed())
}
// ExpectPreparingStatus is called while waiting for the Preparing status.
func (v *CAVerifier) ExpectPreparingStatus(g Gomega) {
g.Expect(helper.GetCARotationPhase(v.Garden.Status.Credentials)).To(Equal(gardencorev1beta1.RotationPreparing))
g.Expect(time.Now().UTC().Sub(v.Garden.Status.Credentials.Rotation.CertificateAuthorities.LastInitiationTime.Time.UTC())).To(BeNumerically("<=", time.Minute))
g.Expect(v.Garden.Status.Credentials.Rotation.CertificateAuthorities.LastInitiationFinishedTime).To(BeNil())
g.Expect(v.Garden.Status.Credentials.Rotation.CertificateAuthorities.LastCompletionTriggeredTime).To(BeNil())
}
// AfterPrepared is called when the Shoot is in Prepared status.
func (v *CAVerifier) AfterPrepared(ctx context.Context) {
Expect(v.Garden.Status.Credentials.Rotation.CertificateAuthorities.Phase).To(Equal(gardencorev1beta1.RotationPrepared), "ca rotation phase should be 'Prepared'")
Expect(v.Garden.Status.Credentials.Rotation.CertificateAuthorities.LastInitiationFinishedTime).NotTo(BeNil())
Expect(v.Garden.Status.Credentials.Rotation.CertificateAuthorities.LastInitiationFinishedTime.After(v.Garden.Status.Credentials.Rotation.CertificateAuthorities.LastInitiationTime.Time)).To(BeTrue())
By("Verify CA secrets of gardener-operator after preparation")
Eventually(func(g Gomega) {
secretList := &corev1.SecretList{}
g.Expect(v.RuntimeClient.List(ctx, secretList, client.InNamespace(v1beta1constants.GardenNamespace), ManagedByGardenerOperatorSecretsManager)).To(Succeed())
grouped := rotation.GroupByName(secretList.Items)
for _, ca := range allCAs {
bundle := ca + "-bundle"
g.Expect(grouped[ca]).To(HaveLen(2), ca+" secret should get rotated, but old CA is kept")
g.Expect(grouped[bundle]).To(HaveLen(1), ca+" bundle secret should have changed")
g.Expect(grouped[ca]).To(ContainElement(v.secretsBefore[ca][0]), "old "+ca+" secret should be kept")
g.Expect(grouped[bundle]).To(Not(ContainElement(v.secretsBefore[bundle][0])), "old "+ca+" bundle should get cleaned up")
}
v.secretsPrepared = grouped
}).Should(Succeed())
}
// ExpectCompletingStatus is called while waiting for the Completing status.
func (v *CAVerifier) ExpectCompletingStatus(g Gomega) {
g.Expect(helper.GetCARotationPhase(v.Garden.Status.Credentials)).To(Equal(gardencorev1beta1.RotationCompleting))
Expect(v.Garden.Status.Credentials.Rotation.CertificateAuthorities.LastCompletionTriggeredTime).NotTo(BeNil())
Expect(v.Garden.Status.Credentials.Rotation.CertificateAuthorities.LastCompletionTriggeredTime.Time.Equal(v.Garden.Status.Credentials.Rotation.CertificateAuthorities.LastInitiationFinishedTime.Time) ||
v.Garden.Status.Credentials.Rotation.CertificateAuthorities.LastCompletionTriggeredTime.After(v.Garden.Status.Credentials.Rotation.CertificateAuthorities.LastInitiationFinishedTime.Time)).To(BeTrue())
}
// AfterCompleted is called when the Shoot is in Completed status.
func (v *CAVerifier) AfterCompleted(ctx context.Context) {
caRotation := v.Garden.Status.Credentials.Rotation.CertificateAuthorities
Expect(helper.GetCARotationPhase(v.Garden.Status.Credentials)).To(Equal(gardencorev1beta1.RotationCompleted))
Expect(caRotation.LastCompletionTime.Time.UTC().After(caRotation.LastInitiationTime.Time.UTC())).To(BeTrue())
Expect(caRotation.LastInitiationFinishedTime).To(BeNil())
Expect(caRotation.LastCompletionTriggeredTime).To(BeNil())
By("Verify CA secrets of gardener-operator after completion")
Eventually(func(g Gomega) {
secretList := &corev1.SecretList{}
g.Expect(v.RuntimeClient.List(ctx, secretList, client.InNamespace(v1beta1constants.GardenNamespace), ManagedByGardenerOperatorSecretsManager)).To(Succeed())
grouped := rotation.GroupByName(secretList.Items)
for _, ca := range allCAs {
bundle := ca + "-bundle"
g.Expect(grouped[ca]).To(HaveLen(1), "old "+ca+" secret should get cleaned up")
g.Expect(grouped[bundle]).To(HaveLen(1), ca+" bundle secret should have changed")
g.Expect(grouped[ca]).To(ContainElement(v.secretsPrepared[ca][1]), "new "+ca+" secret should be kept")
g.Expect(grouped[bundle]).To(Not(ContainElement(v.secretsPrepared[bundle][0])), "combined "+ca+" bundle should get cleaned up")
}
v.secretsCompleted = grouped
}).Should(Succeed())
}