/
certificaterequests.go
307 lines (258 loc) · 11.4 KB
/
certificaterequests.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
/*
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 controllers
import (
"context"
"errors"
"fmt"
"os"
"time"
apiutil "github.com/cert-manager/cert-manager/pkg/api/util"
cmapi "github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1"
cmmeta "github.com/cert-manager/cert-manager/pkg/apis/meta/v1"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
utilerrors "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/tools/record"
"k8s.io/utils/clock"
"k8s.io/utils/ptr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/predicate"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
policyapi "github.com/cert-manager/approver-policy/pkg/apis/policy/v1alpha1"
"github.com/cert-manager/approver-policy/pkg/approver/manager"
internalmanager "github.com/cert-manager/approver-policy/pkg/internal/approver/manager"
"github.com/cert-manager/approver-policy/pkg/internal/controllers/ssa_client"
)
// certificaterequests is a controller-runtime Reconciler which evaluates
// whether reconciled CertificateRequests should be Approved or Denied based on
// registered policy evaluators.
type certificaterequests struct {
// log is logger for the certificaterequests controller.
log logr.Logger
// clock returns time which can be overwritten for testing.
clock clock.Clock
// recorder is used for creating Kubernetes events on resources.
recorder record.EventRecorder
// client is a Kubernetes REST client to interact with objects in the API
// server.
client client.Client
// lister makes requests to the informer cache for getting and listing
// objects.
lister client.Reader
// manager is a Manager that is responsible for reviewing whether a
// CertificateRequest should be approved or denied. This manager is expected
// to manage all approvers which have been registered and active for this
// controller.
manager manager.Interface
}
// addCertificateRequestController will register the certificaterequests
// controller with the controller-runtime Manager.
func addCertificateRequestController(ctx context.Context, opts Options) error {
c := &certificaterequests{
log: opts.Log.WithName("certificaterequests"),
clock: clock.RealClock{},
recorder: opts.Manager.GetEventRecorderFor("policy.cert-manager.io"),
client: opts.Manager.GetClient(),
lister: opts.Manager.GetCache(),
manager: internalmanager.New(opts.Manager.GetCache(), opts.Manager.GetClient(), opts.Evaluators),
}
enqueueRequestFromMapFunc := func(_ context.Context, _ client.Object) []reconcile.Request {
// If an error happens here and we do nothing, we run the risk of not
// processing CertificateRequests.
// Exiting error is the safest option, as it will force a resync on all
// CertificateRequests on start.
var crList cmapi.CertificateRequestList
if err := c.lister.List(ctx, &crList); err != nil {
c.log.Error(err, "failed to list all CertificateRequests, exiting error")
os.Exit(-1)
}
var requests []reconcile.Request
for _, cr := range crList.Items {
// Check for approval status early, rather than relying on the
// predicate or doing it in the actual Reconcile func.
if apiutil.CertificateRequestIsApproved(&cr) || /* #nosec G601 -- Func drops pointer at end of call. */
apiutil.CertificateRequestIsDenied(&cr) /* #nosec G601 -- Func drops pointer at end of call. */ {
continue
}
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{Namespace: cr.Namespace, Name: cr.Name}},
)
}
return requests
}
return ctrl.NewControllerManagedBy(opts.Manager).
For(&cmapi.CertificateRequest{}, builder.WithPredicates(
// Only process CertificateRequests which have not yet got an approval
// status.
predicate.NewPredicateFuncs(func(obj client.Object) bool {
cr := obj.(*cmapi.CertificateRequest)
return !apiutil.CertificateRequestIsApproved(cr) && !apiutil.CertificateRequestIsDenied(cr)
}),
)).
// Watch CertificateRequestPolicies. If a policy is created or updated,
// then we need to process all CertificateRequests that do not yet have an
// approved or denied condition since they may be relevant for the policy.
Watches(&policyapi.CertificateRequestPolicy{}, handler.EnqueueRequestsFromMapFunc(enqueueRequestFromMapFunc)).
// Watch Roles, RoleBindings, ClusterRoles, and ClusterRoleBindings. If
// RBAC changes in the cluster then CertificateRequestPolicies may become
// appropriate for a CertificateRequest. On RBAC events, Reconcile all
// CertificateRequests that are neither Approved or Denied.
// Only need to cache metadata for RBAC resources since we do not need any
// information in the spec.
WatchesMetadata(&rbacv1.Role{}, handler.EnqueueRequestsFromMapFunc(enqueueRequestFromMapFunc)).
WatchesMetadata(&rbacv1.RoleBinding{}, handler.EnqueueRequestsFromMapFunc(enqueueRequestFromMapFunc)).
WatchesMetadata(&rbacv1.ClusterRole{}, handler.EnqueueRequestsFromMapFunc(enqueueRequestFromMapFunc)).
WatchesMetadata(&rbacv1.ClusterRoleBinding{}, handler.EnqueueRequestsFromMapFunc(enqueueRequestFromMapFunc)).
WatchesMetadata(&corev1.Namespace{}, handler.EnqueueRequestsFromMapFunc(enqueueRequestFromMapFunc)).
// Complete the controller builder.
Complete(c)
}
// Reconcile is the top level function for reconciling over synced
// CertificateRequests.
// Reconcile will be called whenever a CertificateRequest event happens. This
// function will call the approver manager to evaluate whether a
// CertificateRequest should be approved, denied, or left alone.
func (c *certificaterequests) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
result, patch, resultErr := c.reconcileStatusPatch(ctx, req)
if patch != nil {
cr, patch, err := ssa_client.GenerateCertificateRequestStatusPatch(req.Name, req.Namespace, patch)
if err != nil {
err = fmt.Errorf("failed to generate CertificateRequest.Status patch: %w", err)
return ctrl.Result{}, utilerrors.NewAggregate([]error{resultErr, err})
}
if err := c.client.Status().Patch(ctx, cr, patch, &client.SubResourcePatchOptions{
PatchOptions: client.PatchOptions{
FieldManager: "approver-policy",
Force: ptr.To(true),
},
}); err != nil {
err = fmt.Errorf("failed to apply CertificateRequest.Status patch: %w", err)
return ctrl.Result{}, utilerrors.NewAggregate([]error{resultErr, err})
}
}
return result, resultErr
}
func (c *certificaterequests) reconcileStatusPatch(ctx context.Context, req ctrl.Request) (ctrl.Result, *cmapi.CertificateRequestStatus, error) {
log := c.log.WithValues("namespace", req.NamespacedName.Namespace, "name", req.NamespacedName.Name)
log.V(2).Info("syncing certificaterequest")
cr := new(cmapi.CertificateRequest)
if err := c.lister.Get(ctx, req.NamespacedName, cr); err != nil {
return ctrl.Result{}, nil, client.IgnoreNotFound(err)
}
if apiutil.CertificateRequestIsApproved(cr) || apiutil.CertificateRequestIsDenied(cr) {
// Return early if already approved/denied as this is decision is final for requests.
return ctrl.Result{}, nil, nil
}
// Query review on the approver manager.
response, err := c.manager.Review(ctx, cr)
if err != nil {
// If an error occurs when evaluating, we fire an event on the
// CertificateRequest and return err to try again.
// Here we don't send the error context in the Kubernetes Event to protect
// information about the approver configuration being exposed to the
// client.
c.recorder.Eventf(cr, corev1.EventTypeWarning, "EvaluationError", "approver-policy failed to review the request and will retry")
return ctrl.Result{}, nil, err
}
crPatch := &cmapi.CertificateRequestStatus{}
switch response.Result {
case manager.ResultApproved:
log.V(2).Info("approving request")
c.recorder.Event(cr, corev1.EventTypeNormal, "Approved", response.Message)
setCertificateRequestStatusCondition(
c.clock,
cr.Status.Conditions,
&crPatch.Conditions,
cmapi.CertificateRequestConditionApproved,
cmmeta.ConditionTrue,
"policy.cert-manager.io",
response.Message,
)
return ctrl.Result{}, crPatch, nil
case manager.ResultDenied:
log.V(2).Info("denying request")
c.recorder.Event(cr, corev1.EventTypeWarning, "Denied", response.Message)
setCertificateRequestStatusCondition(
c.clock,
cr.Status.Conditions,
&crPatch.Conditions,
cmapi.CertificateRequestConditionDenied,
cmmeta.ConditionTrue,
"policy.cert-manager.io",
response.Message,
)
return ctrl.Result{}, crPatch, nil
case manager.ResultUnprocessed:
log.V(2).Info("request was unprocessed")
c.recorder.Event(cr, corev1.EventTypeNormal, "Unprocessed", "Request is not applicable for any policy so ignoring")
return ctrl.Result{}, nil, nil
default:
log.Error(errors.New(response.Message), "manager responded with an unknown result", "result", response.Result)
c.recorder.Event(cr, corev1.EventTypeWarning, "UnknownResponse", "Policy returned an unknown result. This is a bug. Please check the approver-policy logs and file an issue")
// We can do nothing but keep retrying the review here.
return ctrl.Result{Requeue: true, RequeueAfter: time.Second * 5}, nil, nil
}
}
// Update the status with the provided condition details & return
// the added condition.
// This function is copied from https://github.com/cert-manager/issuer-lib/blob/main/conditions/certificaterequest.go
// nolint:unparam
func setCertificateRequestStatusCondition(
clock clock.PassiveClock,
existingConditions []cmapi.CertificateRequestCondition,
patchConditions *[]cmapi.CertificateRequestCondition,
conditionType cmapi.CertificateRequestConditionType,
status cmmeta.ConditionStatus,
reason, message string,
) (*cmapi.CertificateRequestCondition, *metav1.Time) {
newCondition := cmapi.CertificateRequestCondition{
Type: conditionType,
Status: status,
Reason: reason,
Message: message,
}
nowTime := metav1.NewTime(clock.Now())
newCondition.LastTransitionTime = &nowTime
// Reset the LastTransitionTime if the status hasn't changed
for _, cond := range existingConditions {
if cond.Type != conditionType {
continue
}
// If this update doesn't contain a state transition, we don't update
// the conditions LastTransitionTime to Now()
if cond.Status == status {
newCondition.LastTransitionTime = cond.LastTransitionTime
}
}
// Search through existing conditions
for idx, cond := range *patchConditions {
// Skip unrelated conditions
if cond.Type != conditionType {
continue
}
// Overwrite the existing condition
(*patchConditions)[idx] = newCondition
return &newCondition, &nowTime
}
// If we've not found an existing condition of this type, we simply insert
// the new condition into the slice.
*patchConditions = append(*patchConditions, newCondition)
return &newCondition, &nowTime
}