-
Notifications
You must be signed in to change notification settings - Fork 23
/
util.go
350 lines (310 loc) · 12.8 KB
/
util.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
/*
Copyright 2021 The cert-manager Authors.
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 test
import (
"context"
"crypto/x509"
"fmt"
"sync"
"time"
apiutil "github.com/cert-manager/cert-manager/pkg/api/util"
cmapi "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
"github.com/cert-manager/cert-manager/test/unit/gen"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2/ktesting"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/metrics/server"
policyapi "github.com/cert-manager/approver-policy/pkg/apis/policy/v1alpha1"
"github.com/cert-manager/approver-policy/pkg/internal/controllers"
"github.com/cert-manager/approver-policy/pkg/registry"
testenv "github.com/cert-manager/approver-policy/test/env"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
// waitForApproval will wait for the CertificateRequest, given by namespace and
// name, to become in an Approved state.
func waitForApproval(ctx context.Context, cl client.Client, ns, name string) {
Eventually(func() bool {
cr := new(cmapi.CertificateRequest)
Eventually(func() error {
return cl.Get(ctx, client.ObjectKey{Namespace: ns, Name: name}, cr)
}).WithTimeout(time.Second * 10).WithPolling(time.Millisecond * 10).Should(BeNil())
return apiutil.CertificateRequestIsApproved(cr)
}).WithTimeout(time.Second*10).WithPolling(time.Millisecond*10).Should(BeTrue(), "expected approval")
}
// waitForDenial will wait for the CertificateRequest, given by namespace and
// name, to become in an Denied state.
func waitForDenial(ctx context.Context, cl client.Client, ns, name string) {
Eventually(func() bool {
cr := new(cmapi.CertificateRequest)
Eventually(func() error {
return cl.Get(ctx, client.ObjectKey{Namespace: ns, Name: name}, cr)
}).WithTimeout(time.Second * 10).WithPolling(time.Millisecond * 10).Should(BeNil())
return apiutil.CertificateRequestIsDenied(cr)
}).WithTimeout(time.Second*10).WithPolling(time.Millisecond*10).Should(BeTrue(), "expected denial")
}
// waitForNoApproveOrDeny will wait a reasonable amount of time (3 seconds) for
// the CertificateRequest to be marked as neither Approved or Denied.
func waitForNoApproveOrDeny(ctx context.Context, cl client.Client, ns, name string) {
Consistently(func() bool {
cr := new(cmapi.CertificateRequest)
Eventually(func() error {
return cl.Get(ctx, client.ObjectKey{Namespace: ns, Name: name}, cr)
}).WithTimeout(time.Second * 10).WithPolling(time.Millisecond * 10).Should(BeNil())
return apiutil.CertificateRequestIsApproved(cr) || apiutil.CertificateRequestIsDenied(cr)
}).WithTimeout(time.Second*10).WithPolling(time.Millisecond*10).Should(BeFalse(), "expected neither approved not denied")
}
// waitForReady will wait for the CertificateRequestPolicy, given by name, to
// become in an Ready state. Will ensure the Ready condition has the same
// observed Generation as the object's Generation.
func waitForReady(ctx context.Context, cl client.Client, name string) {
Eventually(func() bool {
var policy policyapi.CertificateRequestPolicy
Eventually(func() error {
return cl.Get(ctx, client.ObjectKey{Name: name}, &policy)
}).WithTimeout(time.Second * 10).WithPolling(time.Millisecond * 10).Should(BeNil())
for _, condition := range policy.Status.Conditions {
if condition.ObservedGeneration != policy.Generation {
return false
}
if condition.Type == policyapi.CertificateRequestPolicyConditionReady && condition.Status == corev1.ConditionTrue {
return true
}
}
return false
}).WithTimeout(time.Second*10).WithPolling(time.Millisecond*10).Should(BeTrue(), "expected policy to become ready")
}
// waitForNotReady will wait for the CertificateRequestPolicy, given by name,
// become in an Not-Ready state. Will ensure the Ready condition has the same
// observed Generation as the object's Generation.
func waitForNotReady(ctx context.Context, cl client.Client, name string) {
Eventually(func() bool {
var policy policyapi.CertificateRequestPolicy
Eventually(func() error {
return cl.Get(ctx, client.ObjectKey{Name: name}, &policy)
}).WithTimeout(time.Second * 10).WithPolling(time.Millisecond * 10).Should(BeNil())
for _, condition := range policy.Status.Conditions {
if condition.ObservedGeneration != policy.Generation {
return false
}
if condition.Type == policyapi.CertificateRequestPolicyConditionReady && condition.Status == corev1.ConditionFalse {
return true
}
}
return false
}).WithTimeout(time.Second*10).WithPolling(time.Millisecond*10).Should(BeTrue(), "expected policy to become not-ready")
}
var controllerLock sync.Mutex
// startControllers will create a test Namespace and start the approver-policy
// controllers and ensure they are active and ready. This function is intended
// to be run in a JustBefore block before any test logic has started. The
// created namespace as well as a shutdown function to stop the controllers is
// returned.
func startControllers(registry *registry.Registry) (context.Context, func(), corev1.Namespace) {
if !controllerLock.TryLock() {
// The ginkgo tests should only run one at a time, so this should never
// happen.
panic("startControllers called twice without calling stopControllers")
}
// A logr which will print log messages interspersed with the Ginkgo
// progress messages to make it easy to understand the context of the log
// messages.
// The logger is also added to the context so that it will be used by code
// that uses logr.FromContext.
log, ctx := ktesting.NewTestContext(GinkgoT())
ctx, cancel := context.WithCancel(ctx)
ctrl.SetLogger(log)
namespace := corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-policy-",
},
}
By("Creating Policy Namespace: " + namespace.Name)
Expect(env.AdminClient.Create(ctx, &namespace)).NotTo(HaveOccurred())
// A channel which will be closed after the controller-manager stops and
// just before its goroutine exits.
mgrStopped := make(chan struct{})
// shutdown stops the controller-manager and then cleans up most of the
// resources from previous tests.
// NB: The test namespace can not be deleted because envtest does not run
// the garbage collection controller which is required to empty the
// namespace before it is deleted. See:
// * https://github.com/kubernetes-sigs/controller-runtime/issues/880
// * https://book.kubebuilder.io/reference/envtest.html#testing-considerations
shutdown := func() {
defer controllerLock.Unlock()
// Cancel the context and wait for the manager goroutine to exit before
// cleaning up the test resources to avoid distracting messages from the
// controllers when they attempt to re-reconcile the deleted resources.
cancel()
<-mgrStopped
// A new context for use by the cleanup clients because the previous
// context has already been cancelled.
ctx := context.Background()
By("Waiting for all CertificateRequest resources to be deleted")
{
// DeleteAllOf can't be used to delete resources in all namespaces,
// but List does return resources from all namespaces by default,
// so we delete each item in that list.
// https://github.com/kubernetes-sigs/controller-runtime/issues/1842
var l cmapi.CertificateRequestList
Expect(env.AdminClient.List(ctx, &l)).To(Succeed())
for _, obj := range l.Items {
By(fmt.Sprintf("Deleting: %s", client.ObjectKeyFromObject(&obj). /* #nosec G601 -- Func drops pointer at end of call. */ String()))
Expect(env.AdminClient.Delete(ctx, &obj) /* #nosec G601 -- Func drops pointer at end of call. */).To(Succeed())
}
}
By("Waiting for all CertificateRequestPolicy resources to be deleted")
// CertificateRequestPolicy is a cluster-scoped resource, so DeleteAllOf
// can be used in this case.
Expect(
client.IgnoreNotFound(
env.AdminClient.DeleteAllOf(ctx, new(policyapi.CertificateRequestPolicy)),
),
).To(Succeed())
}
mgr, err := ctrl.NewManager(env.Config, ctrl.Options{
Scheme: policyapi.GlobalScheme,
Metrics: server.Options{
BindAddress: "0",
},
// we don't need leader election for this test,
// there should only be one test running at a time
// the controllerLock ensures that
LeaderElection: false,
Logger: log.WithName("manager"),
})
Expect(err).NotTo(HaveOccurred())
Expect(controllers.AddControllers(ctx, controllers.Options{
Log: log.WithName("controllers"),
Manager: mgr,
Evaluators: registry.Evaluators(),
Reconcilers: registry.Reconcilers(),
})).NotTo(HaveOccurred())
By("Running Policy controller")
go func() {
Expect(mgr.Start(ctx)).To(Succeed())
close(mgrStopped)
}()
By("Waiting for Informers to Sync")
Expect(mgr.GetCache().WaitForCacheSync(ctx)).Should(BeTrue())
return ctx, shutdown, namespace
}
// bindUserToUseCertificateRequestPolicies creates an RBAC Role and RoleBinding
// that binds to the given user to use the CertificateRequestPolicies in the
// given Namespace. The name of the Role and RoleBinding is returned, which
// should be deleted after the test has completed by the consumer.
func bindUserToUseCertificateRequestPolicies(ctx context.Context, cl client.Client, ns string, policyNames ...string) string {
role := rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-policy-use-",
Namespace: ns,
},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{"policy.cert-manager.io"},
Resources: []string{"certificaterequestpolicies"},
Verbs: []string{"use"},
ResourceNames: policyNames,
},
},
}
Expect(cl.Create(ctx, &role)).NotTo(HaveOccurred())
binding := rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: role.Name,
Namespace: ns,
},
Subjects: []rbacv1.Subject{
{
Kind: "User",
Name: testenv.UserClientName,
APIGroup: "rbac.authorization.k8s.io",
},
},
RoleRef: rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "Role",
Name: role.Name,
},
}
Expect(cl.Create(ctx, &binding)).NotTo(HaveOccurred())
return role.Name
}
// bindUserToCreateCertificateRequest creates an RBAC Role and RoleBinding that
// binds to the given user to create CertificateRequests in the given
// Namespace. The name of the Role and RoleBinding is returned, which should be
// deleted after the test has completed by the consumer.
func bindUserToCreateCertificateRequest(ctx context.Context, cl client.Client, ns string) string {
role := rbacv1.Role{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-cr-create-",
Namespace: ns,
},
Rules: []rbacv1.PolicyRule{
{
APIGroups: []string{"cert-manager.io"},
Resources: []string{"certificaterequests"},
Verbs: []string{"create"},
},
},
}
Expect(cl.Create(ctx, &role)).NotTo(HaveOccurred())
binding := rbacv1.RoleBinding{
ObjectMeta: metav1.ObjectMeta{
Name: role.Name,
Namespace: ns,
},
Subjects: []rbacv1.Subject{
{
Kind: "User",
Name: testenv.UserClientName,
APIGroup: "rbac.authorization.k8s.io",
},
},
RoleRef: rbacv1.RoleRef{
APIGroup: "rbac.authorization.k8s.io",
Kind: "Role",
Name: role.Name,
},
}
Expect(cl.Create(ctx, &binding)).NotTo(HaveOccurred())
return role.Name
}
// deleteRoleAndRoleBindings deletes the Role and RoleBindings that have the
// given name.
func deleteRoleAndRoleBindings(ctx context.Context, ns string, names ...string) {
for _, name := range names {
Expect(env.AdminClient.Delete(ctx, &rbacv1.Role{ObjectMeta: metav1.ObjectMeta{Namespace: ns, Name: name}})).ToNot(HaveOccurred())
Expect(env.AdminClient.Delete(ctx, &rbacv1.RoleBinding{ObjectMeta: metav1.ObjectMeta{Namespace: ns, Name: name}})).ToNot(HaveOccurred())
}
}
// createCertificateRequest will create a CertificateRequest with an X.509
// certificate request using and RSA key, which includes the modifiers
// provided. The name of the created CertificateRequest is returned.
func createCertificateRequest(ctx context.Context, cl client.Client, ns string, csrMod gen.CSRModifier, crMods ...gen.CertificateRequestModifier) string {
csr, _, err := gen.CSR(x509.RSA, csrMod)
Expect(err).ToNot(HaveOccurred())
cr := gen.CertificateRequest("", append(crMods,
func(cr *cmapi.CertificateRequest) {
cr.GenerateName = "test-"
},
gen.SetCertificateRequestCSR(csr),
gen.SetCertificateRequestNamespace(ns),
)...)
Expect(cl.Create(ctx, cr)).ToNot(HaveOccurred())
return cr.Name
}