Skip to content

Commit

Permalink
Prefix event key attributes with identifier
Browse files Browse the repository at this point in the history
This to prevent collisions between different event attributes.

For example when first an event with revision `foo` is received
without a token, after which a token `foo` is received without a
`revision`.

Signed-off-by: Hidde Beydals <hidde@hhh.computer>
  • Loading branch information
hiddeco committed May 24, 2023
1 parent 639d66d commit c34f48a
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions internal/server/event_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,28 +190,35 @@ func (s *EventServer) logRateLimitMiddleware(h http.Handler) http.Handler {
})
}

// eventKeyFunc generates a unique key for an event based on the provided HTTP
// request, which can be used to deduplicate events. The key is calculated by
// concatenating specific event attributes and hashing them using SHA-256.
// The key is then returned as a hex-encoded string.
//
// The event attributes are prefixed with an identifier to avoid collisions
// between different event attributes.
func eventKeyFunc(r *http.Request) (string, error) {
event := r.Context().Value(eventContextKey{}).(*eventv1.Event)

comps := []string{
"event",
event.InvolvedObject.Name,
event.InvolvedObject.Namespace,
event.InvolvedObject.Kind,
event.Message,
"name=" + event.InvolvedObject.Name,
"namespace=" + event.InvolvedObject.Namespace,
"kind=" + event.InvolvedObject.Kind,
"message=" + event.Message,
}

revision, ok := event.Metadata[eventv1.MetaRevisionKey]
if ok {
comps = append(comps, revision)
comps = append(comps, "revision="+revision)
}

token, ok := event.Metadata[eventv1.MetaTokenKey]
if ok {
comps = append(comps, token)
comps = append(comps, "token="+token)
}

val := strings.Join(comps, "/")
digest := sha256.Sum256([]byte(val))
key := strings.Join(comps, "/")
digest := sha256.Sum256([]byte(key))
return fmt.Sprintf("%x", digest), nil
}

0 comments on commit c34f48a

Please sign in to comment.