Skip to content

Consistently handle filters with empty string as value on optional attributes and extensions #4847

@pierDipi

Description

@pierDipi

Describe the bug

// TriggerFilterAttributes is a map of context attribute names to values for
// filtering by equality. Only exact matches will pass the filter. You can use the value ''
// to indicate all strings match.

Our implementation:

func (attrs attributesFilter) Filter(ctx context.Context, event cloudevents.Event) eventfilter.FilterResult {
// Set standard context attributes. The attributes available may not be
// exactly the same as the attributes defined in the current version of the
// CloudEvents spec.
ce := map[string]interface{}{
"specversion": event.SpecVersion(),
"type": event.Type(),
"source": event.Source(),
"subject": event.Subject(),
"id": event.ID(),
"time": event.Time().String(),
"schemaurl": event.DataSchema(),
"datacontenttype": event.DataContentType(),
"datamediatype": event.DataMediaType(),
// TODO: use data_base64 when SDK supports it.
"datacontentencoding": event.DeprecatedDataContentEncoding(),
}
ext := event.Extensions()
for k, v := range ext {
ce[k] = v
}
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 {
logging.FromContext(ctx).Debug("Attribute not found", zap.String("attribute", k))
return eventfilter.FailFilter
}
// If the attribute is not set to any and is different than the one from the event, return false.
if v != eventingv1beta1.TriggerAnyFilter && v != value {
logging.FromContext(ctx).Debug("Attribute had non-matching value", zap.String("attribute", k), zap.String("filter", v), zap.Any("received", value))
return eventfilter.FailFilter
}
}
return eventfilter.PassFilter
}


Given a filter like:

subject: ""

and an event like:

# ... all required attributes

the filter passes even though there is no subject.


Given a filter like:

myextension: ""

and an event like:

# ... all required attributes

the filter doesn't pass because there is no extension myextension.

Additional context

Metadata

Metadata

Assignees

No one assigned

    Labels

    area/apiarea/eventingThe Eventing api groupgood first issueDenotes an issue ready for a new contributor, according to the "help wanted" guidelines.help wantedDenotes an issue that needs help from a contributor. Must meet "help wanted" guidelines.kind/bugCategorizes issue or PR as related to a bug.priority/important-soonMust be staffed and worked on either currently, or very soon, ideally in time for the next release.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions