Skip to content

Commit

Permalink
Filtering fix for consistency in handling empty string as value (#5273)
Browse files Browse the repository at this point in the history
* filter-fix for empty string filters, dataschema added

* merged 2 similar if cases

* test added to check for consistency in filtering optional attributes and extensions

* comment added for this change

* gofmt done
  • Loading branch information
Shashankft9 authored Apr 19, 2021
1 parent 96a4cc3 commit 0452204
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
7 changes: 5 additions & 2 deletions pkg/eventfilter/attributes/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (attrs attributesFilter) Filter(ctx context.Context, event cloudevents.Even
"subject": event.Subject(),
"id": event.ID(),
"time": event.Time().String(),
"dataschema": event.DataSchema(),
"schemaurl": event.DataSchema(),
"datacontenttype": event.DataContentType(),
"datamediatype": event.DataMediaType(),
Expand All @@ -59,8 +60,10 @@ func (attrs attributesFilter) Filter(ctx context.Context, event cloudevents.Even
for k, v := range attrs {
var value interface{}
value, ok := ce[k]
// If the attribute does not exist in the event, return false.
if !ok {
// If the attribute does not exist in the event (extension context attributes) or if the event attribute
// has an empty string value (optional attributes) - which means it was never set in the incoming event,
// return false.
if !ok || (v == eventingv1.TriggerAnyFilter && value == "") {
logging.FromContext(ctx).Debug("Attribute not found", zap.String("attribute", k))
return eventfilter.FailFilter
}
Expand Down
38 changes: 38 additions & 0 deletions pkg/eventfilter/attributes/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const (
eventSource = `/mycontext`
extensionName = `myextension`
extensionValue = `my-extension-value`
subjectValue = `mysubject`
)

func TestAttributesFilter_Filter(t *testing.T) {
Expand Down Expand Up @@ -72,6 +73,29 @@ func TestAttributesFilter_Filter(t *testing.T) {
event: makeEventWithExtension(extensionName, extensionValue),
want: eventfilter.PassFilter,
},
"Any Extension with attribs - without Extension in Event": {
filter: attributesWithExtension(eventType, eventSource, ""),
want: eventfilter.FailFilter,
},
"Any Extension with attribs - with Extension in Event": {
filter: attributesWithExtension(eventType, eventSource, ""),
event: makeEventWithExtension(extensionName, extensionValue),
want: eventfilter.PassFilter,
},
"Subject with attribs": {
filter: attributesWithSubject(eventType, eventSource, subjectValue),
event: makeEventWithSubject(subjectValue),
want: eventfilter.PassFilter,
},
"Any Subject with attribs - without Subject in Event": {
filter: attributesWithSubject(eventType, eventSource, ""),
want: eventfilter.FailFilter,
},
"Any Subject with attribs - with Subject in Event": {
filter: attributesWithSubject(eventType, eventSource, ""),
event: makeEventWithSubject(subjectValue),
want: eventfilter.PassFilter,
},
"Any with attribs - Arrival extension": {
filter: attributes("", ""),
event: makeEventWithExtension(broker.EventArrivalTime, "2019-08-26T23:38:17.834384404Z"),
Expand Down Expand Up @@ -111,6 +135,12 @@ func makeEventWithExtension(extName, extValue string) *cloudevents.Event {
return e
}

func makeEventWithSubject(sub string) *cloudevents.Event {
e := makeEvent()
e.SetSubject(sub)
return e
}

func attributes(t, s string) map[string]string {
return map[string]string{
"type": t,
Expand All @@ -125,3 +155,11 @@ func attributesWithExtension(t, s, e string) map[string]string {
extensionName: e,
}
}

func attributesWithSubject(t, s, sub string) map[string]string {
return map[string]string{
"type": t,
"source": s,
"subject": sub,
}
}

0 comments on commit 0452204

Please sign in to comment.