diff --git a/pkg/eventfilter/attributes/filter.go b/pkg/eventfilter/attributes/filter.go index 3ecac19c575..73fef82b1e8 100644 --- a/pkg/eventfilter/attributes/filter.go +++ b/pkg/eventfilter/attributes/filter.go @@ -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(), @@ -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 } diff --git a/pkg/eventfilter/attributes/filter_test.go b/pkg/eventfilter/attributes/filter_test.go index 2b0b19af771..e4e044cbfe8 100644 --- a/pkg/eventfilter/attributes/filter_test.go +++ b/pkg/eventfilter/attributes/filter_test.go @@ -31,6 +31,7 @@ const ( eventSource = `/mycontext` extensionName = `myextension` extensionValue = `my-extension-value` + subjectValue = `mysubject` ) func TestAttributesFilter_Filter(t *testing.T) { @@ -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"), @@ -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, @@ -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, + } +}