From eda629b9ab09a9c09a4115bae59dde7d6e603676 Mon Sep 17 00:00:00 2001 From: Shashankft9 Date: Mon, 19 Apr 2021 03:20:44 -0400 Subject: [PATCH 1/5] filter-fix for empty string filters, dataschema added --- pkg/eventfilter/attributes/filter.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pkg/eventfilter/attributes/filter.go b/pkg/eventfilter/attributes/filter.go index 3ecac19c575..18d1370240d 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(), @@ -69,6 +70,11 @@ func (attrs attributesFilter) Filter(ctx context.Context, event cloudevents.Even logging.FromContext(ctx).Debug("Attribute had non-matching value", zap.String("attribute", k), zap.String("filter", v), zap.Any("received", value)) return eventfilter.FailFilter } + // If the attribute is set to any and is not set in the event itself, return false. + if v == eventingv1.TriggerAnyFilter && value == "" { + logging.FromContext(ctx).Debug("Attribute not set in the event", zap.String("attribute", k)) + return eventfilter.FailFilter + } } return eventfilter.PassFilter } From 81da6248dfe46ec029f445f49acbf38649e740c2 Mon Sep 17 00:00:00 2001 From: Shashankft9 Date: Mon, 19 Apr 2021 04:28:32 -0400 Subject: [PATCH 2/5] merged 2 similar if cases --- pkg/eventfilter/attributes/filter.go | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/pkg/eventfilter/attributes/filter.go b/pkg/eventfilter/attributes/filter.go index 18d1370240d..9f8a6b3c1ab 100644 --- a/pkg/eventfilter/attributes/filter.go +++ b/pkg/eventfilter/attributes/filter.go @@ -61,7 +61,7 @@ func (attrs attributesFilter) Filter(ctx context.Context, event cloudevents.Even var value interface{} value, ok := ce[k] // If the attribute does not exist in the event, return false. - if !ok { + if !ok || (v == eventingv1.TriggerAnyFilter && value == ""){ logging.FromContext(ctx).Debug("Attribute not found", zap.String("attribute", k)) return eventfilter.FailFilter } @@ -70,11 +70,6 @@ func (attrs attributesFilter) Filter(ctx context.Context, event cloudevents.Even logging.FromContext(ctx).Debug("Attribute had non-matching value", zap.String("attribute", k), zap.String("filter", v), zap.Any("received", value)) return eventfilter.FailFilter } - // If the attribute is set to any and is not set in the event itself, return false. - if v == eventingv1.TriggerAnyFilter && value == "" { - logging.FromContext(ctx).Debug("Attribute not set in the event", zap.String("attribute", k)) - return eventfilter.FailFilter - } } return eventfilter.PassFilter } From 7c609560499b43597fa21a8d78eae97baff88f9c Mon Sep 17 00:00:00 2001 From: Shashankft9 Date: Mon, 19 Apr 2021 04:44:32 -0400 Subject: [PATCH 3/5] test added to check for consistency in filtering optional attributes and extensions --- pkg/eventfilter/attributes/filter_test.go | 38 +++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/pkg/eventfilter/attributes/filter_test.go b/pkg/eventfilter/attributes/filter_test.go index 2b0b19af771..3a33502703b 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, + } +} From 75c455dff0d85e691a928bb3988429ea17875065 Mon Sep 17 00:00:00 2001 From: Shashankft9 Date: Mon, 19 Apr 2021 05:17:11 -0400 Subject: [PATCH 4/5] comment added for this change --- pkg/eventfilter/attributes/filter.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pkg/eventfilter/attributes/filter.go b/pkg/eventfilter/attributes/filter.go index 9f8a6b3c1ab..7d86599ffb6 100644 --- a/pkg/eventfilter/attributes/filter.go +++ b/pkg/eventfilter/attributes/filter.go @@ -60,7 +60,9 @@ 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 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 From 983878147606fe35e891ce4c083d8fc1c9d395a1 Mon Sep 17 00:00:00 2001 From: Shashankft9 Date: Mon, 19 Apr 2021 05:24:02 -0400 Subject: [PATCH 5/5] gofmt done --- pkg/eventfilter/attributes/filter.go | 2 +- pkg/eventfilter/attributes/filter_test.go | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pkg/eventfilter/attributes/filter.go b/pkg/eventfilter/attributes/filter.go index 7d86599ffb6..73fef82b1e8 100644 --- a/pkg/eventfilter/attributes/filter.go +++ b/pkg/eventfilter/attributes/filter.go @@ -63,7 +63,7 @@ func (attrs attributesFilter) Filter(ctx context.Context, event cloudevents.Even // 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 == ""){ + 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 3a33502703b..e4e044cbfe8 100644 --- a/pkg/eventfilter/attributes/filter_test.go +++ b/pkg/eventfilter/attributes/filter_test.go @@ -158,8 +158,8 @@ func attributesWithExtension(t, s, e string) map[string]string { func attributesWithSubject(t, s, sub string) map[string]string { return map[string]string{ - "type": t, - "source": s, - "subject": sub, + "type": t, + "source": s, + "subject": sub, } }