/
etcd_encryption_key.go
213 lines (185 loc) · 9.71 KB
/
etcd_encryption_key.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
// Copyright 2022 SAP SE or an SAP affiliate company. All rights reserved. This file is licensed under the Apache Software License, v. 2 except as noted otherwise in the LICENSE file
//
// 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 rotation
import (
"context"
"sort"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
apiserverconfigv1 "k8s.io/apiserver/pkg/apis/config/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"
. "github.com/gardener/gardener/pkg/utils/test/matchers"
)
// ETCDEncryptionKeyVerifier verifies the etcd encryption key rotation.
type ETCDEncryptionKeyVerifier struct {
RuntimeClient client.Client
Namespace string
SecretsManagerLabelSelector client.MatchingLabels
GetETCDEncryptionKeyRotation func() *gardencorev1beta1.ETCDEncryptionKeyRotation
EncryptionKey string
RoleLabelValue string
secretsBefore SecretConfigNamesToSecrets
secretsPrepared SecretConfigNamesToSecrets
}
var decoder runtime.Decoder
func init() {
scheme := runtime.NewScheme()
utilruntime.Must(apiserverconfigv1.AddToScheme(scheme))
decoder = serializer.NewCodecFactory(scheme).UniversalDeserializer()
}
// Before is called before the rotation is started.
func (v *ETCDEncryptionKeyVerifier) Before(ctx context.Context) {
By("Verify old etcd encryption key secret")
Eventually(func(g Gomega) {
secretList := &corev1.SecretList{}
g.Expect(v.RuntimeClient.List(ctx, secretList, client.InNamespace(v.Namespace), v.SecretsManagerLabelSelector)).To(Succeed())
grouped := GroupByName(secretList.Items)
g.Expect(grouped[v.EncryptionKey]).To(HaveLen(1), "etcd encryption key should get created, but not rotated yet")
v.secretsBefore = grouped
}).Should(Succeed())
By("Verify old etcd encryption config secret")
Eventually(func(g Gomega) {
secretList := &corev1.SecretList{}
g.Expect(v.RuntimeClient.List(ctx, secretList, client.InNamespace(v.Namespace), client.MatchingLabels{v1beta1constants.LabelRole: v.RoleLabelValue})).To(Succeed())
g.Expect(secretList.Items).NotTo(BeEmpty())
sort.Sort(sort.Reverse(AgeSorter(secretList.Items)))
encryptionConfiguration := &apiserverconfigv1.EncryptionConfiguration{}
g.Expect(runtime.DecodeInto(decoder, secretList.Items[0].Data["encryption-configuration.yaml"], encryptionConfiguration)).To(Succeed())
g.Expect(encryptionConfiguration.Resources).To(HaveLen(1))
g.Expect(encryptionConfiguration.Resources[0].Providers).To(DeepEqual([]apiserverconfigv1.ProviderConfiguration{
{
AESCBC: &apiserverconfigv1.AESConfiguration{
Keys: []apiserverconfigv1.Key{{
// old key
Name: string(v.secretsBefore[v.EncryptionKey][0].Data["key"]),
Secret: string(v.secretsBefore[v.EncryptionKey][0].Data["secret"]),
}},
},
},
{
// identity is always added
Identity: &apiserverconfigv1.IdentityConfiguration{},
},
}))
}).Should(Succeed(), "etcd encryption config should only have old key")
}
// ExpectPreparingStatus is called while waiting for the Preparing status.
func (v *ETCDEncryptionKeyVerifier) ExpectPreparingStatus(g Gomega) {
etcdEncryptionKeyRotation := v.GetETCDEncryptionKeyRotation()
g.Expect(etcdEncryptionKeyRotation.Phase).To(Equal(gardencorev1beta1.RotationPreparing))
g.Expect(time.Now().UTC().Sub(etcdEncryptionKeyRotation.LastInitiationTime.Time.UTC())).To(BeNumerically("<=", time.Minute))
g.Expect(etcdEncryptionKeyRotation.LastInitiationFinishedTime).To(BeNil())
g.Expect(etcdEncryptionKeyRotation.LastCompletionTriggeredTime).To(BeNil())
}
// AfterPrepared is called when the Shoot is in Prepared status.
func (v *ETCDEncryptionKeyVerifier) AfterPrepared(ctx context.Context) {
etcdEncryptionKeyRotation := v.GetETCDEncryptionKeyRotation()
Expect(etcdEncryptionKeyRotation.Phase).To(Equal(gardencorev1beta1.RotationPrepared), "rotation phase should be 'Prepared'")
Expect(etcdEncryptionKeyRotation.LastInitiationFinishedTime).NotTo(BeNil())
Expect(etcdEncryptionKeyRotation.LastInitiationFinishedTime.After(etcdEncryptionKeyRotation.LastInitiationTime.Time)).To(BeTrue())
By("Verify etcd encryption key secrets")
Eventually(func(g Gomega) {
secretList := &corev1.SecretList{}
g.Expect(v.RuntimeClient.List(ctx, secretList, client.InNamespace(v.Namespace), v.SecretsManagerLabelSelector)).To(Succeed())
grouped := GroupByName(secretList.Items)
g.Expect(grouped[v.EncryptionKey]).To(HaveLen(2), "etcd encryption key should get rotated")
g.Expect(grouped[v.EncryptionKey]).To(ContainElement(v.secretsBefore[v.EncryptionKey][0]), "old etcd encryption key secret should be kept")
v.secretsPrepared = grouped
}).Should(Succeed())
By("Verify combined etcd encryption config secret")
Eventually(func(g Gomega) {
secretList := &corev1.SecretList{}
g.Expect(v.RuntimeClient.List(ctx, secretList, client.InNamespace(v.Namespace), client.MatchingLabels{v1beta1constants.LabelRole: v.RoleLabelValue})).To(Succeed())
g.Expect(secretList.Items).NotTo(BeEmpty())
sort.Sort(sort.Reverse(AgeSorter(secretList.Items)))
encryptionConfiguration := &apiserverconfigv1.EncryptionConfiguration{}
err := runtime.DecodeInto(decoder, secretList.Items[0].Data["encryption-configuration.yaml"], encryptionConfiguration)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(encryptionConfiguration.Resources).To(HaveLen(1))
g.Expect(encryptionConfiguration.Resources[0].Providers).To(DeepEqual([]apiserverconfigv1.ProviderConfiguration{
{
AESCBC: &apiserverconfigv1.AESConfiguration{
Keys: []apiserverconfigv1.Key{{
// new key
Name: string(v.secretsPrepared[v.EncryptionKey][1].Data["key"]),
Secret: string(v.secretsPrepared[v.EncryptionKey][1].Data["secret"]),
}, {
// old key
Name: string(v.secretsPrepared[v.EncryptionKey][0].Data["key"]),
Secret: string(v.secretsPrepared[v.EncryptionKey][0].Data["secret"]),
}},
},
},
{
Identity: &apiserverconfigv1.IdentityConfiguration{},
},
}))
}).Should(Succeed(), "etcd encryption config should have both old and new key, with new key as the first one")
}
// ExpectCompletingStatus is called while waiting for the Completing status.
func (v *ETCDEncryptionKeyVerifier) ExpectCompletingStatus(g Gomega) {
etcdEncryptionKeyRotation := v.GetETCDEncryptionKeyRotation()
g.Expect(etcdEncryptionKeyRotation.Phase).To(Equal(gardencorev1beta1.RotationCompleting))
Expect(etcdEncryptionKeyRotation.LastCompletionTriggeredTime).NotTo(BeNil())
Expect(etcdEncryptionKeyRotation.LastCompletionTriggeredTime.Time.Equal(etcdEncryptionKeyRotation.LastInitiationFinishedTime.Time) ||
etcdEncryptionKeyRotation.LastCompletionTriggeredTime.After(etcdEncryptionKeyRotation.LastInitiationFinishedTime.Time)).To(BeTrue())
}
// AfterCompleted is called when the Shoot is in Completed status.
func (v *ETCDEncryptionKeyVerifier) AfterCompleted(ctx context.Context) {
etcdEncryptionKeyRotation := v.GetETCDEncryptionKeyRotation()
Expect(etcdEncryptionKeyRotation.Phase).To(Equal(gardencorev1beta1.RotationCompleted))
Expect(etcdEncryptionKeyRotation.LastCompletionTime.Time.UTC().After(etcdEncryptionKeyRotation.LastInitiationTime.Time.UTC())).To(BeTrue())
Expect(etcdEncryptionKeyRotation.LastInitiationFinishedTime).To(BeNil())
Expect(etcdEncryptionKeyRotation.LastCompletionTriggeredTime).To(BeNil())
By("Verify new etcd encryption key secret")
Eventually(func(g Gomega) {
secretList := &corev1.SecretList{}
g.Expect(v.RuntimeClient.List(ctx, secretList, client.InNamespace(v.Namespace), v.SecretsManagerLabelSelector)).To(Succeed())
grouped := GroupByName(secretList.Items)
g.Expect(grouped[v.EncryptionKey]).To(HaveLen(1), "old etcd encryption key should get cleaned up")
g.Expect(grouped[v.EncryptionKey]).To(ContainElement(v.secretsPrepared[v.EncryptionKey][1]), "new etcd encryption key secret should be kept")
}).Should(Succeed())
By("Verify new etcd encryption config secret")
Eventually(func(g Gomega) {
secretList := &corev1.SecretList{}
g.Expect(v.RuntimeClient.List(ctx, secretList, client.InNamespace(v.Namespace), client.MatchingLabels{v1beta1constants.LabelRole: v.RoleLabelValue})).To(Succeed())
g.Expect(secretList.Items).NotTo(BeEmpty())
sort.Sort(sort.Reverse(AgeSorter(secretList.Items)))
encryptionConfiguration := &apiserverconfigv1.EncryptionConfiguration{}
err := runtime.DecodeInto(decoder, secretList.Items[0].Data["encryption-configuration.yaml"], encryptionConfiguration)
g.Expect(err).NotTo(HaveOccurred())
g.Expect(encryptionConfiguration.Resources).To(HaveLen(1))
g.Expect(encryptionConfiguration.Resources[0].Providers).To(DeepEqual([]apiserverconfigv1.ProviderConfiguration{
{
AESCBC: &apiserverconfigv1.AESConfiguration{
Keys: []apiserverconfigv1.Key{{
// new key
Name: string(v.secretsPrepared[v.EncryptionKey][1].Data["key"]),
Secret: string(v.secretsPrepared[v.EncryptionKey][1].Data["secret"]),
}},
},
},
{
Identity: &apiserverconfigv1.IdentityConfiguration{},
},
}))
}).Should(Succeed(), "etcd encryption config should only have new key")
}