-
Notifications
You must be signed in to change notification settings - Fork 7
/
webhook.go
291 lines (253 loc) · 8.31 KB
/
webhook.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
package webhook
import (
"bytes"
"context"
"encoding/json"
"fmt"
"github.com/openshift-pipelines/manual-approval-gate/pkg/apis/approvaltask/v1alpha1"
"go.uber.org/zap"
admissionv1 "k8s.io/api/admission/v1"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
admissionlisters "k8s.io/client-go/listers/admissionregistration/v1"
corelisters "k8s.io/client-go/listers/core/v1"
"knative.dev/pkg/controller"
"knative.dev/pkg/kmp"
"knative.dev/pkg/logging"
"knative.dev/pkg/ptr"
pkgreconciler "knative.dev/pkg/reconciler"
"knative.dev/pkg/system"
"knative.dev/pkg/webhook"
certresources "knative.dev/pkg/webhook/certificates/resources"
)
const (
Group = "openshift-pipelines.org"
Version = "v1alpha1"
Kind = "ApprovalTask"
)
// reconciler implements the AdmissionController for resources
type reconciler struct {
webhook.StatelessAdmissionImpl
pkgreconciler.LeaderAwareFuncs
key types.NamespacedName
path string
withContext func(context.Context) context.Context
client kubernetes.Interface
vwhlister admissionlisters.ValidatingWebhookConfigurationLister
secretlister corelisters.SecretLister
disallowUnknownFields bool
secretName string
}
var _ controller.Reconciler = (*reconciler)(nil)
var _ pkgreconciler.LeaderAware = (*reconciler)(nil)
var _ webhook.AdmissionController = (*reconciler)(nil)
var _ webhook.StatelessAdmissionController = (*reconciler)(nil)
// Reconcile implements controller.Reconciler
func (r *reconciler) Reconcile(ctx context.Context, key string) error {
logger := logging.FromContext(ctx)
if !r.IsLeaderFor(r.key) {
return controller.NewSkipKey(key)
}
// Look up the webhook secret, and fetch the CA cert bundle.
secret, err := r.secretlister.Secrets(system.Namespace()).Get(r.secretName)
if err != nil {
logger.Errorw("Error fetching secret", zap.Error(err))
return err
}
caCert, ok := secret.Data[certresources.CACert]
if !ok {
return fmt.Errorf("secret %q is missing %q key", r.secretName, certresources.CACert)
}
// Reconcile the webhook configuration.
return r.reconcileValidatingWebhook(ctx, caCert)
}
func (r *reconciler) Admit(ctx context.Context, request *admissionv1.AdmissionRequest) *admissionv1.AdmissionResponse {
if r.withContext != nil {
ctx = r.withContext(ctx)
}
logger := logging.FromContext(ctx)
kind := request.Kind
newBytes := request.Object.Raw
gvk := schema.GroupVersionKind{
Group: kind.Group,
Version: kind.Version,
Kind: kind.Kind,
}
if gvk.Group != Group || gvk.Version != Version || gvk.Kind != Kind {
logger.Error("Unhandled kind: ", gvk)
}
oldBytes := request.OldObject.Raw
var oldObj v1alpha1.ApprovalTask
if len(newBytes) != 0 {
newDecoder := json.NewDecoder(bytes.NewBuffer(oldBytes))
if r.disallowUnknownFields {
newDecoder.DisallowUnknownFields()
}
if err := newDecoder.Decode(&oldObj); err != nil {
return webhook.MakeErrorStatus("cannot decode incoming new object: %v", err)
}
}
// Check if approval is required by the approver
if !isApprovalRequired(oldObj) {
return &admissionv1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Message: "ApprovalTask has already reached it's final state",
},
}
}
// Check if username is mentioned in the approval task
if !ifUserExists(oldObj.Spec.Approvers, request.UserInfo.Username) {
return &admissionv1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Message: "User does not exist in the in the approval list",
},
}
}
var newObj v1alpha1.ApprovalTask
if len(newBytes) != 0 {
newDecoder := json.NewDecoder(bytes.NewBuffer(newBytes))
if r.disallowUnknownFields {
newDecoder.DisallowUnknownFields()
}
if err := newDecoder.Decode(&newObj); err != nil {
return webhook.MakeErrorStatus("cannot decode incoming new object: %v", err)
}
}
// Check if user is updating the input for his name only
var userApprovalChanged bool
errMsg := fmt.Errorf("User can only update their own approval input")
changed, err := IsUserApprovalChanged(oldObj.Spec.Approvers, newObj.Spec.Approvers, request.UserInfo.Username)
if err != nil {
userApprovalChanged = false
errMsg = err
} else if changed {
if CheckOtherUsersForInvalidChanges(oldObj.Spec.Approvers, newObj.Spec.Approvers, request.UserInfo.Username) {
userApprovalChanged = true
}
} else {
userApprovalChanged = false
}
if !userApprovalChanged {
return &admissionv1.AdmissionResponse{
Allowed: false,
Result: &metav1.Status{
Message: errMsg.Error(),
},
}
}
return &admissionv1.AdmissionResponse{
Allowed: true,
}
}
func (ac *reconciler) reconcileValidatingWebhook(ctx context.Context, caCert []byte) error {
logger := logging.FromContext(ctx)
rules := []admissionregistrationv1.RuleWithOperations{
{
Operations: []admissionregistrationv1.OperationType{
admissionregistrationv1.Update,
// admissionregistrationv1.Create,
},
Rule: admissionregistrationv1.Rule{
APIGroups: []string{"openshift-pipelines.org"},
APIVersions: []string{"v1alpha1"},
Resources: []string{"approvaltask", "approvaltasks"},
},
},
}
configuredWebhook, err := ac.vwhlister.Get(ac.key.Name)
if err != nil {
return err
}
webhook := configuredWebhook.DeepCopy()
webhook.OwnerReferences = nil
for i, wh := range webhook.Webhooks {
if wh.Name != webhook.Name {
continue
}
webhook.Webhooks[i].Rules = rules
webhook.Webhooks[i].ClientConfig.CABundle = caCert
if webhook.Webhooks[i].ClientConfig.Service == nil {
return fmt.Errorf("missing service reference for webhook: %s", wh.Name)
}
webhook.Webhooks[i].ClientConfig.Service.Path = ptr.String(ac.Path())
}
if ok, err := kmp.SafeEqual(configuredWebhook, webhook); err != nil {
return fmt.Errorf("error diffing webhooks: %w", err)
} else if !ok {
logger.Info("Updating webhook")
vwhclient := ac.client.AdmissionregistrationV1().ValidatingWebhookConfigurations()
if _, err := vwhclient.Update(ctx, webhook, metav1.UpdateOptions{}); err != nil {
return fmt.Errorf("failed to update webhook: %w", err)
}
} else {
logger.Info("Webhook is valid")
}
return nil
}
// Path implements AdmissionController
func (ac *reconciler) Path() string {
return ac.path
}
func ifUserExists(approvals []v1alpha1.ApproverDetails, currentUser string) bool {
if len(approvals) == 0 {
return true
}
for _, approval := range approvals {
if approval.Name == currentUser {
return true
}
}
return false
}
func isApprovalRequired(approvaltask v1alpha1.ApprovalTask) bool {
if approvaltask.Status.State == "rejected" || approvaltask.Status.State == "approved" {
return false
}
if len(approvaltask.Status.ApproversResponse) == approvaltask.Spec.NumberOfApprovalsRequired {
return false
}
return true
}
// hasValidInputValue checks if the input value is either "approve" or "reject".
func hasValidInputValue(input string) error {
if input == "approve" || input == "reject" {
return nil
}
return fmt.Errorf("invalid input value: '%s'. Supported values are 'approve' or 'reject'", input)
}
// hasOnlyInputChanged checks if only the input field has changed for the current approver
// and if the new input value is valid
func hasOnlyInputChanged(oldObjApprover, newObjApprover v1alpha1.ApproverDetails) (bool, error) {
if oldObjApprover.Name == newObjApprover.Name && oldObjApprover.Input != newObjApprover.Input {
if err := hasValidInputValue(newObjApprover.Input); err != nil {
return false, err
}
return true, nil
}
return false, nil
}
// IsUserApprovalChanged checks if there is a valid input change for the current user.
func IsUserApprovalChanged(oldObjApprovers, newObjApprovers []v1alpha1.ApproverDetails, currentUser string) (bool, error) {
for i, approver := range oldObjApprovers {
if approver.Name == currentUser {
return hasOnlyInputChanged(approver, newObjApprovers[i])
}
}
return false, nil
}
// CheckOtherUsersForInvalidChanges validates that no other approvers inputs have been changed
func CheckOtherUsersForInvalidChanges(oldObjApprovers, newObjApprover []v1alpha1.ApproverDetails, currentUser string) bool {
for i, approver := range oldObjApprovers {
if approver.Name != currentUser {
if oldObjApprovers[i].Input != newObjApprover[i].Input {
return false
}
}
}
return true
}