-
Notifications
You must be signed in to change notification settings - Fork 450
/
warnings_test.go
322 lines (290 loc) · 16.6 KB
/
warnings_test.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
// Copyright (c) 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 shoot_test
import (
"context"
"time"
. "github.com/gardener/gardener/pkg/api/core/shoot"
"github.com/gardener/gardener/pkg/apis/core"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
gomegatypes "github.com/onsi/gomega/types"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/utils/pointer"
)
var _ = Describe("Warnings", func() {
Describe("#GetWarnings", func() {
var (
ctx = context.TODO()
shoot *core.Shoot
credentialsRotationInterval = time.Hour
)
BeforeEach(func() {
shoot = &core.Shoot{
Spec: core.ShootSpec{
Kubernetes: core.Kubernetes{
Version: "1.22.11",
EnableStaticTokenKubeconfig: pointer.Bool(false),
},
},
}
})
It("should return nil when shoot is nil", func() {
Expect(GetWarnings(ctx, nil, nil, credentialsRotationInterval)).To(BeEmpty())
})
It("should return nil when shoot does not have any problematic configuration", func() {
Expect(GetWarnings(ctx, shoot, nil, credentialsRotationInterval)).To(BeEmpty())
})
It("should return a warning when static token kubeconfig is nil", func() {
shoot.Spec.Kubernetes.EnableStaticTokenKubeconfig = nil
Expect(GetWarnings(ctx, shoot, nil, credentialsRotationInterval)).To(ContainElement(ContainSubstring("you should consider disabling the static token kubeconfig")))
})
It("should return a warning when static token kubeconfig is explicitly enabled", func() {
shoot.Spec.Kubernetes.EnableStaticTokenKubeconfig = pointer.Bool(true)
Expect(GetWarnings(ctx, shoot, nil, credentialsRotationInterval)).To(ContainElement(ContainSubstring("you should consider disabling the static token kubeconfig")))
})
Context("credentials rotation", func() {
BeforeEach(func() {
shoot.CreationTimestamp = metav1.Time{Time: time.Now().Add(-credentialsRotationInterval * 2)}
})
It("should not return a warning when credentials rotation is due in case shoot is too young", func() {
shoot.CreationTimestamp = metav1.Now()
Expect(GetWarnings(ctx, shoot, shoot, credentialsRotationInterval)).To(BeEmpty())
})
It("should return a warning when credentials rotation is due", func() {
Expect(GetWarnings(ctx, shoot, shoot, credentialsRotationInterval)).To(ContainElement(ContainSubstring("you should consider rotating the shoot credentials")))
})
DescribeTable("warnings for specific credentials rotations",
func(matcher gomegatypes.GomegaMatcher, mutateShoot func(*core.Shoot), mutateRotation func(rotation *core.ShootCredentialsRotation)) {
if mutateShoot != nil {
mutateShoot(shoot)
}
rotation := &core.ShootCredentialsRotation{
CertificateAuthorities: &core.CARotation{},
Kubeconfig: &core.ShootKubeconfigRotation{},
SSHKeypair: &core.ShootSSHKeypairRotation{},
Observability: &core.ShootObservabilityRotation{},
ServiceAccountKey: &core.ShootServiceAccountKeyRotation{},
ETCDEncryptionKey: &core.ShootETCDEncryptionKeyRotation{},
}
mutateRotation(rotation)
shoot.Status.Credentials = &core.ShootCredentials{Rotation: rotation}
Expect(GetWarnings(ctx, shoot, shoot, credentialsRotationInterval)).To(matcher)
},
Entry("ca nil", ContainElement(ContainSubstring("you should consider rotating the certificate authorities")), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.CertificateAuthorities = nil
},
),
Entry("ca last initiated too long ago", ContainElement(ContainSubstring("you should consider rotating the certificate authorities")), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.CertificateAuthorities.LastInitiationTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval * 2)}
},
),
Entry("ca last initiated not too long ago", Not(ContainElement(ContainSubstring("you should consider rotating the certificate authorities"))), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.CertificateAuthorities.LastInitiationTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval / 2)}
},
),
Entry("ca completion is due (never completed yet)", ContainElement(ContainSubstring("the certificate authorities rotation initiation was finished more than")), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.CertificateAuthorities.LastInitiationFinishedTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval / 2)}
rotation.CertificateAuthorities.LastCompletionTriggeredTime = nil
},
),
Entry("ca completion is due (current rotation not completed)", ContainElement(ContainSubstring("the certificate authorities rotation initiation was finished more than")), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.CertificateAuthorities.LastInitiationFinishedTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval / 2)}
rotation.CertificateAuthorities.LastCompletionTriggeredTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval)}
},
),
Entry("ca completion is not due (current rotation not completed)", Not(ContainElement(ContainSubstring("the certificate authorities rotation initiation was finished more than"))), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.CertificateAuthorities.LastInitiationFinishedTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval / 2)}
rotation.CertificateAuthorities.LastCompletionTriggeredTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval / 3)}
},
),
Entry("etcdEncryptionKey nil", ContainElement(ContainSubstring("you should consider rotating the ETCD encryption key")), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.ETCDEncryptionKey = nil
},
),
Entry("etcdEncryptionKey last initiated too long ago", ContainElement(ContainSubstring("you should consider rotating the ETCD encryption key")), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.ETCDEncryptionKey.LastInitiationTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval * 2)}
},
),
Entry("etcdEncryptionKey last initiated not too long ago", Not(ContainElement(ContainSubstring("you should consider rotating the ETCD encryption key"))), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.ETCDEncryptionKey.LastInitiationTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval / 2)}
},
),
Entry("etcdEncryptionKey completion is due (never completed yet)", ContainElement(ContainSubstring("the ETCD encryption key rotation initiation was finished more than")), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.ETCDEncryptionKey.LastInitiationFinishedTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval / 2)}
rotation.ETCDEncryptionKey.LastCompletionTriggeredTime = nil
},
),
Entry("etcdEncryptionKey completion is due (current rotation not completed)", ContainElement(ContainSubstring("the ETCD encryption key rotation initiation was finished more than")), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.ETCDEncryptionKey.LastInitiationFinishedTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval / 2)}
rotation.ETCDEncryptionKey.LastCompletionTriggeredTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval)}
},
),
Entry("etcdEncryptionKey completion is not due (current rotation not completed)", Not(ContainElement(ContainSubstring("the ETCD encryption key rotation initiation was finished more than"))), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.ETCDEncryptionKey.LastInitiationFinishedTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval / 2)}
rotation.ETCDEncryptionKey.LastCompletionTriggeredTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval / 3)}
},
),
Entry("kubeconfig nil", ContainElement(ContainSubstring("you should consider rotating the static token kubeconfig")),
func(shoot *core.Shoot) {
shoot.Spec.Kubernetes.EnableStaticTokenKubeconfig = pointer.Bool(true)
},
func(rotation *core.ShootCredentialsRotation) {
rotation.Kubeconfig = nil
},
),
Entry("kubeconfig last initiated too long ago", ContainElement(ContainSubstring("you should consider rotating the static token kubeconfig")),
func(shoot *core.Shoot) {
shoot.Spec.Kubernetes.EnableStaticTokenKubeconfig = pointer.Bool(true)
},
func(rotation *core.ShootCredentialsRotation) {
rotation.Kubeconfig.LastInitiationTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval * 2)}
},
),
Entry("kubeconfig last initiated too long ago but disabled", Not(ContainElement(ContainSubstring("you should consider rotating the static token kubeconfig"))),
func(shoot *core.Shoot) {
shoot.Spec.Kubernetes.EnableStaticTokenKubeconfig = pointer.Bool(false)
},
func(rotation *core.ShootCredentialsRotation) {
rotation.Kubeconfig.LastInitiationTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval * 2)}
},
),
Entry("kubeconfig last initiated not too long ago", Not(ContainElement(ContainSubstring("you should consider rotating the static token kubeconfig"))), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.Kubeconfig.LastInitiationTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval / 2)}
},
),
Entry("observability nil", ContainElement(ContainSubstring("you should consider rotating the observability passwords")), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.Observability = nil
},
),
Entry("observability last initiated too long ago", ContainElement(ContainSubstring("you should consider rotating the observability passwords")), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.Observability.LastInitiationTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval * 2)}
},
),
Entry("observability last initiated too long ago but shoot purpose is testing", Not(ContainElement(ContainSubstring("you should consider rotating the observability passwords"))),
func(shoot *core.Shoot) {
p := core.ShootPurposeTesting
shoot.Spec.Purpose = &p
},
func(rotation *core.ShootCredentialsRotation) {
rotation.Observability.LastInitiationTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval * 2)}
},
),
Entry("observability last initiated not too long ago", Not(ContainElement(ContainSubstring("you should consider rotating the observability passwords"))), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.Observability.LastInitiationTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval / 2)}
},
),
Entry("serviceAccountKey nil", ContainElement(ContainSubstring("you should consider rotating the ServiceAccount token signing key")), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.ServiceAccountKey = nil
},
),
Entry("serviceAccountKey last initiated too long ago", ContainElement(ContainSubstring("you should consider rotating the ServiceAccount token signing key")), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.ServiceAccountKey.LastInitiationTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval * 2)}
},
),
Entry("serviceAccountKey last initiated not too long ago", Not(ContainElement(ContainSubstring("you should consider rotating the ServiceAccount token signing key"))), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.ServiceAccountKey.LastInitiationTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval / 2)}
},
),
Entry("serviceAccountKey completion is due (never completed yet)", ContainElement(ContainSubstring("the ServiceAccount token signing key rotation initiation was finished more than")), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.ServiceAccountKey.LastInitiationFinishedTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval / 2)}
rotation.ServiceAccountKey.LastCompletionTriggeredTime = nil
},
),
Entry("serviceAccountKey completion is due (current rotation not completed)", ContainElement(ContainSubstring("the ServiceAccount token signing key rotation initiation was finished more than")), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.ServiceAccountKey.LastInitiationFinishedTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval / 2)}
rotation.ServiceAccountKey.LastCompletionTriggeredTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval)}
},
),
Entry("serviceAccountKey completion is not due (current rotation not completed)", Not(ContainElement(ContainSubstring("the ServiceAccount token signing key rotation initiation was finished more than"))), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.ServiceAccountKey.LastInitiationFinishedTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval / 2)}
rotation.ServiceAccountKey.LastCompletionTriggeredTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval / 3)}
},
),
Entry("sshKeypair nil", ContainElement(ContainSubstring("you should consider rotating the SSH keypair")), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.SSHKeypair = nil
},
),
Entry("sshKeypair last initiated too long ago", ContainElement(ContainSubstring("you should consider rotating the SSH keypair")), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.SSHKeypair.LastInitiationTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval * 2)}
},
),
Entry("sshKeypair last initiated not too long ago", Not(ContainElement(ContainSubstring("you should consider rotating the SSH keypair"))), nil,
func(rotation *core.ShootCredentialsRotation) {
rotation.SSHKeypair.LastInitiationTime = &metav1.Time{Time: time.Now().Add(-credentialsRotationInterval / 2)}
},
),
)
})
Context("PodSecurityPolicy", func() {
BeforeEach(func() {
shoot.CreationTimestamp = metav1.Now()
})
It("should return a warning when the PodSecurity admission plugin is not disabled for shoots >= 1.23 and < 1.25", func() {
shoot.Spec.Kubernetes.Version = "1.24.2"
warnings := GetWarnings(ctx, shoot, shoot, credentialsRotationInterval)
Expect(warnings).To(ContainElement(
ContainSubstring("you should consider migrating to PodSecurity, see https://github.com/gardener/gardener/blob/master/docs/usage/pod-security.md#migrating-from-podsecuritypolicys-to-podsecurity-admission-controller for details"),
))
})
It("should not return a warning when the PodSecurity admission plugin is disabled for shoots >= 1.23 and < 1.25", func() {
shoot.Spec.Kubernetes.Version = "1.24.2"
shoot.Spec.Kubernetes.KubeAPIServer = &core.KubeAPIServerConfig{
AdmissionPlugins: []core.AdmissionPlugin{
{
Name: "PodSecurityPolicy",
Disabled: pointer.Bool(true),
},
},
}
warnings := GetWarnings(ctx, shoot, shoot, credentialsRotationInterval)
Expect(warnings).To(BeEmpty())
})
It("should not return a warning when the PodSecurity admission plugin is not disabled for shoots < 1.23", func() {
shoot.Spec.Kubernetes.Version = "1.22.11"
warnings := GetWarnings(ctx, shoot, shoot, credentialsRotationInterval)
Expect(warnings).To(BeEmpty())
})
It("should not return a warning when the PodSecurity admission plugin is not disabled for shoots >= 1.25", func() {
shoot.Spec.Kubernetes.Version = "1.25.0"
warnings := GetWarnings(ctx, shoot, shoot, credentialsRotationInterval)
Expect(warnings).To(BeEmpty())
})
})
})
})