Skip to content

Commit

Permalink
Merge pull request #687 from fluxcd/backport-686-to-release/v1.2.x
Browse files Browse the repository at this point in the history
[release/v1.2.x] Exclude eventv1.MetaTokenKey from event metadata
  • Loading branch information
darkowlzz authored Dec 14, 2023
2 parents 12ff284 + ba6510d commit d7492e9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
14 changes: 14 additions & 0 deletions internal/server/event_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ func (s *EventServer) handleEvent() func(w http.ResponseWriter, r *http.Request)
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")
Expand Down Expand Up @@ -440,3 +443,14 @@ func (s *EventServer) enhanceEventWithAlertMetadata(ctx context.Context, event *
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)
}
}
32 changes: 32 additions & 0 deletions internal/server/event_handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1058,3 +1058,35 @@ func TestEnhanceEventWithAlertMetadata(t *testing.T) {
})
}
}

func Test_excludeInternalMetadata(t *testing.T) {
tests := []struct {
name string
event eventv1.Event
wantMetadata map[string]string
}{
{
name: "no metadata",
},
{
name: "internal metadata",
event: eventv1.Event{
Metadata: map[string]string{
eventv1.MetaTokenKey: "aaaa",
eventv1.MetaRevisionKey: "bbbb",
},
},
wantMetadata: map[string]string{
eventv1.MetaRevisionKey: "bbbb",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)

excludeInternalMetadata(&tt.event)
g.Expect(tt.event.Metadata).To(BeEquivalentTo(tt.wantMetadata))
})
}
}

0 comments on commit d7492e9

Please sign in to comment.