-
Notifications
You must be signed in to change notification settings - Fork 133
/
event_handlers.go
457 lines (388 loc) · 14.8 KB
/
event_handlers.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
Copyright 2020 The Flux 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 server
import (
"context"
"crypto/x509"
"errors"
"fmt"
"net/http"
"net/url"
"regexp"
"strings"
"time"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/yaml"
eventv1 "github.com/fluxcd/pkg/apis/event/v1beta1"
"github.com/fluxcd/pkg/masktoken"
apiv1 "github.com/fluxcd/notification-controller/api/v1"
apiv1beta3 "github.com/fluxcd/notification-controller/api/v1beta3"
"github.com/fluxcd/notification-controller/internal/notifier"
)
func involvedObjectString(o corev1.ObjectReference) string {
return fmt.Sprintf("%s/%s/%s", o.Kind, o.Namespace, o.Name)
}
func crossNSObjectRefString(o apiv1.CrossNamespaceObjectReference) string {
return fmt.Sprintf("%s/%s/%s", o.Kind, o.Namespace, o.Name)
}
func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
event := r.Context().Value(eventContextKey{}).(*eventv1.Event)
eventLogger := log.FromContext(r.Context())
ctx, cancel := context.WithTimeout(r.Context(), 15*time.Second)
defer cancel()
// Remove any internal metadata before further processing the event.
excludeInternalMetadata(event)
alerts, err := s.getAllAlertsForEvent(ctx, event)
if err != nil {
eventLogger.Error(err, "failed to get alerts for the event")
}
if len(alerts) == 0 {
eventLogger.Info("discarding event, no alerts found for the involved object")
w.WriteHeader(http.StatusAccepted)
return
}
eventLogger.Info("dispatching event", "message", event.Message)
// Dispatch notifications.
for i := range alerts {
alert := &alerts[i]
alertLogger := eventLogger.WithValues(alert.Kind, client.ObjectKeyFromObject(alert))
ctx := log.IntoContext(ctx, alertLogger)
if err := s.dispatchNotification(ctx, event, alert); err != nil {
alertLogger.Error(err, "failed to dispatch notification")
s.Eventf(alert, corev1.EventTypeWarning, "NotificationDispatchFailed",
"failed to dispatch notification for %s: %s", involvedObjectString(event.InvolvedObject), err)
}
}
w.WriteHeader(http.StatusAccepted)
}
}
func (s *EventServer) getAllAlertsForEvent(ctx context.Context, event *eventv1.Event) ([]apiv1beta3.Alert, error) {
var allAlerts apiv1beta3.AlertList
err := s.kubeClient.List(ctx, &allAlerts)
if err != nil {
return nil, fmt.Errorf("failed listing alerts: %w", err)
}
return s.filterAlertsForEvent(ctx, allAlerts.Items, event), nil
}
// filterAlertsForEvent filters a given set of alerts against a given event,
// checking if the event matches with any of the alert event sources and is
// allowed by the exclusion list.
func (s *EventServer) filterAlertsForEvent(ctx context.Context, alerts []apiv1beta3.Alert, event *eventv1.Event) []apiv1beta3.Alert {
logger := log.FromContext(ctx)
results := make([]apiv1beta3.Alert, 0)
for i := range alerts {
alert := &alerts[i]
// Skip suspended alert.
if alert.Spec.Suspend {
continue
}
alertLogger := logger.WithValues(alert.Kind, client.ObjectKeyFromObject(alert))
ctx := log.IntoContext(ctx, alertLogger)
// Check if the event matches any of the alert sources.
if !s.eventMatchesAlertSources(ctx, event, alert) {
continue
}
// Check if the event message is allowed for the alert based on the
// inclusion list.
if !s.messageIsIncluded(ctx, event.Message, alert) {
continue
}
// Check if the event message is allowed for the alert based on the
// exclusion list.
if s.messageIsExcluded(ctx, event.Message, alert) {
continue
}
results = append(results, *alert)
}
return results
}
// eventMatchesAlertSources returns if a given event matches with any of the
// alert sources.
func (s *EventServer) eventMatchesAlertSources(ctx context.Context, event *eventv1.Event, alert *apiv1beta3.Alert) bool {
for _, source := range alert.Spec.EventSources {
if source.Namespace == "" {
source.Namespace = alert.Namespace
}
if s.eventMatchesAlertSource(ctx, event, alert, source) {
return true
}
}
return false
}
// messageIsIncluded returns if the given message matches with the given alert's
// inclusion rules.
func (s *EventServer) messageIsIncluded(ctx context.Context, msg string, alert *apiv1beta3.Alert) bool {
if len(alert.Spec.InclusionList) == 0 {
return true
}
for _, exp := range alert.Spec.InclusionList {
if r, err := regexp.Compile(exp); err == nil {
if r.Match([]byte(msg)) {
return true
}
} else {
log.FromContext(ctx).Error(err, fmt.Sprintf("failed to compile inclusion regex: %s", exp))
s.Eventf(alert, corev1.EventTypeWarning,
"InvalidConfig", "failed to compile inclusion regex: %s", exp)
}
}
return false
}
// messageIsExcluded returns if the given message matches with the given alert's
// exclusion rules.
func (s *EventServer) messageIsExcluded(ctx context.Context, msg string, alert *apiv1beta3.Alert) bool {
if len(alert.Spec.ExclusionList) == 0 {
return false
}
for _, exp := range alert.Spec.ExclusionList {
if r, err := regexp.Compile(exp); err == nil {
if r.Match([]byte(msg)) {
return true
}
} else {
log.FromContext(ctx).Error(err, fmt.Sprintf("failed to compile exclusion regex: %s", exp))
s.Eventf(alert, corev1.EventTypeWarning, "InvalidConfig",
"failed to compile exclusion regex: %s", exp)
}
}
return false
}
// dispatchNotification constructs and sends notification from the given event
// and alert data.
func (s *EventServer) dispatchNotification(ctx context.Context, event *eventv1.Event, alert *apiv1beta3.Alert) error {
sender, notification, token, timeout, err := s.getNotificationParams(ctx, event, alert)
if err != nil {
return err
}
// Skip when either sender or notification couldn't be created.
if sender == nil || notification == nil {
return nil
}
go func(n notifier.Interface, e eventv1.Event) {
pctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
if err := n.Post(pctx, e); err != nil {
maskedErrStr, maskErr := masktoken.MaskTokenFromString(err.Error(), token)
if maskErr != nil {
err = maskErr
} else {
err = errors.New(maskedErrStr)
}
log.FromContext(ctx).Error(err, "failed to send notification")
s.Eventf(alert, corev1.EventTypeWarning, "NotificationDispatchFailed",
"failed to send notification for %s: %s", involvedObjectString(event.InvolvedObject), err)
}
}(sender, *notification)
return nil
}
// getNotificationParams constructs the notification parameters from the given
// event and alert, and returns a notifier, event, token and timeout for sending
// the notification. The returned event is a mutated form of the input event
// based on the alert configuration.
func (s *EventServer) getNotificationParams(ctx context.Context, event *eventv1.Event, alert *apiv1beta3.Alert) (notifier.Interface, *eventv1.Event, string, time.Duration, error) {
// Check if event comes from a different namespace.
if s.noCrossNamespaceRefs && event.InvolvedObject.Namespace != alert.Namespace {
accessDenied := fmt.Errorf(
"alert '%s/%s' can't process event from '%s', cross-namespace references have been blocked",
alert.Namespace, alert.Name, involvedObjectString(event.InvolvedObject))
return nil, nil, "", 0, fmt.Errorf("discarding event, access denied to cross-namespace sources: %w", accessDenied)
}
var provider apiv1beta3.Provider
providerName := types.NamespacedName{Namespace: alert.Namespace, Name: alert.Spec.ProviderRef.Name}
err := s.kubeClient.Get(ctx, providerName, &provider)
if err != nil {
return nil, nil, "", 0, fmt.Errorf("failed to read provider: %w", err)
}
// Skip if the provider is suspended.
if provider.Spec.Suspend {
return nil, nil, "", 0, nil
}
sender, token, err := createNotifier(ctx, s.kubeClient, provider)
if err != nil {
return nil, nil, "", 0, fmt.Errorf("failed to initialize notifier for provider '%s': %w", provider.Name, err)
}
notification := *event.DeepCopy()
s.enhanceEventWithAlertMetadata(ctx, ¬ification, alert)
return sender, ¬ification, token, provider.GetTimeout(), nil
}
// createNotifier returns a notifier.Interface for the given Provider.
func createNotifier(ctx context.Context, kubeClient client.Client, provider apiv1beta3.Provider) (notifier.Interface, string, error) {
logger := log.FromContext(ctx)
webhook := provider.Spec.Address
username := provider.Spec.Username
proxy := provider.Spec.Proxy
token := ""
password := ""
headers := make(map[string]string)
if provider.Spec.SecretRef != nil {
var secret corev1.Secret
secretName := types.NamespacedName{Namespace: provider.Namespace, Name: provider.Spec.SecretRef.Name}
err := kubeClient.Get(ctx, secretName, &secret)
if err != nil {
return nil, "", fmt.Errorf("failed to read secret: %w", err)
}
if val, ok := secret.Data["address"]; ok {
if len(val) > 2048 {
return nil, "", fmt.Errorf("invalid address in secret: address exceeds maximum length of %d bytes", 2048)
}
webhook = strings.TrimSpace(string(val))
}
if val, ok := secret.Data["password"]; ok {
password = strings.TrimSpace(string(val))
}
if val, ok := secret.Data["proxy"]; ok {
proxy = strings.TrimSpace(string(val))
_, err := url.Parse(proxy)
if err != nil {
return nil, "", fmt.Errorf("invalid proxy in secret '%s': %w", proxy, err)
}
}
if val, ok := secret.Data["token"]; ok {
token = strings.TrimSpace(string(val))
}
if val, ok := secret.Data["username"]; ok {
username = strings.TrimSpace(string(val))
}
if h, ok := secret.Data["headers"]; ok {
err := yaml.Unmarshal(h, &headers)
if err != nil {
return nil, "", fmt.Errorf("failed to read headers from secret: %w", err)
}
}
}
var certPool *x509.CertPool
if provider.Spec.CertSecretRef != nil {
var secret corev1.Secret
secretName := types.NamespacedName{Namespace: provider.Namespace, Name: provider.Spec.CertSecretRef.Name}
err := kubeClient.Get(ctx, secretName, &secret)
if err != nil {
return nil, "", fmt.Errorf("failed to read cert secret: %w", err)
}
switch secret.Type {
case corev1.SecretTypeOpaque, corev1.SecretTypeTLS, "":
default:
return nil, "", fmt.Errorf("cannot use Secret '%s' to get TLS certificate: invalid Secret type: '%s'", secret.Name, secret.Type)
}
caFile, ok := secret.Data["ca.crt"]
if !ok {
// TODO: Drop support for "caFile" field in v1 Provider API.
caFile, ok = secret.Data["caFile"]
if !ok {
return nil, "", fmt.Errorf("no 'ca.crt' key found in Secret '%s'", secret.Name)
}
logger.Info("warning: specifying CA cert via 'caFile' is deprecated, please use 'ca.crt' instead")
}
certPool = x509.NewCertPool()
ok = certPool.AppendCertsFromPEM(caFile)
if !ok {
return nil, "", fmt.Errorf("could not append to cert pool")
}
}
if webhook == "" {
return nil, "", fmt.Errorf("provider has no address")
}
factory := notifier.NewFactory(webhook, proxy, username, provider.Spec.Channel, token, headers, certPool, password, string(provider.UID))
sender, err := factory.Notifier(provider.Spec.Type)
if err != nil {
return nil, "", fmt.Errorf("failed to initialize notifier: %w", err)
}
return sender, token, nil
}
// eventMatchesAlertSource returns if a given event matches with the given alert
// source configuration and severity.
func (s *EventServer) eventMatchesAlertSource(ctx context.Context, event *eventv1.Event, alert *apiv1beta3.Alert, source apiv1.CrossNamespaceObjectReference) bool {
logger := log.FromContext(ctx)
// No match if the event and source don't have the same namespace and kind.
if event.InvolvedObject.Namespace != source.Namespace ||
event.InvolvedObject.Kind != source.Kind {
return false
}
// No match if the alert severity doesn't match the event severity and
// the alert severity isn't info.
severity := alert.Spec.EventSeverity
if event.Severity != severity && severity != eventv1.EventSeverityInfo {
return false
}
// No match if the source name isn't wildcard, and source and event names
// don't match.
if source.Name != "*" && source.Name != event.InvolvedObject.Name {
return false
}
// Match if no match labels specified.
if source.MatchLabels == nil {
return true
}
// Perform label selector matching.
var obj metav1.PartialObjectMetadata
obj.SetGroupVersionKind(event.InvolvedObject.GroupVersionKind())
obj.SetName(event.InvolvedObject.Name)
obj.SetNamespace(event.InvolvedObject.Namespace)
if err := s.kubeClient.Get(ctx, types.NamespacedName{
Namespace: event.InvolvedObject.Namespace,
Name: event.InvolvedObject.Name,
}, &obj); err != nil {
logger.Error(err, "error getting the involved object")
s.Eventf(alert, corev1.EventTypeWarning, "SourceFetchFailed",
"error getting source object %s", involvedObjectString(event.InvolvedObject))
return false
}
sel, err := metav1.LabelSelectorAsSelector(&metav1.LabelSelector{
MatchLabels: source.MatchLabels,
})
if err != nil {
logger.Error(err, fmt.Sprintf("error using matchLabels from event source %s", crossNSObjectRefString(source)))
s.Eventf(alert, corev1.EventTypeWarning, "InvalidConfig",
"error using matchLabels from event source %s", crossNSObjectRefString(source))
return false
}
return sel.Matches(labels.Set(obj.GetLabels()))
}
// enhanceEventWithAlertMetadata enhances the event with Alert metadata.
func (s *EventServer) enhanceEventWithAlertMetadata(ctx context.Context, event *eventv1.Event, alert *apiv1beta3.Alert) {
meta := event.Metadata
if meta == nil {
meta = make(map[string]string)
}
for key, value := range alert.Spec.EventMetadata {
if _, alreadyPresent := meta[key]; !alreadyPresent {
meta[key] = value
} else {
log.FromContext(ctx).
Info("metadata key found in the existing set of metadata", "key", key)
s.Eventf(alert, corev1.EventTypeWarning, "MetadataAppendFailed",
"metadata key found in the existing set of metadata for '%s' in %s", key, involvedObjectString(event.InvolvedObject))
}
}
if alert.Spec.Summary != "" {
meta["summary"] = alert.Spec.Summary
}
if len(meta) > 0 {
event.Metadata = meta
}
}
// excludeInternalMetadata removes any internal metadata from the given event.
func excludeInternalMetadata(event *eventv1.Event) {
if len(event.Metadata) == 0 {
return
}
excludeList := []string{eventv1.MetaTokenKey}
for _, key := range excludeList {
delete(event.Metadata, key)
}
}