Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

options: Add RegisterFilters callback #3203

Merged
merged 1 commit into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion skipper.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ type Options struct {
// List of custom filter specifications.
CustomFilters []filters.Spec

// RegisterFilters callback can be used to register additional filters.
// Built-in and custom filters are registered before the callback is called.
RegisterFilters func(registry filters.Registry)

// Urls of nodes in an etcd cluster, storing route definitions.
EtcdUrls []string

Expand Down Expand Up @@ -1141,7 +1145,8 @@ func initLog(o Options) error {
}

// filterRegistry creates a filter registry with the builtin and
// custom filter specs registered excluding disabled filters
// custom filter specs registered excluding disabled filters.
// If [Options.RegisterFilters] callback is set, it will be called.
func (o *Options) filterRegistry() filters.Registry {
registry := make(filters.Registry)

Expand All @@ -1162,6 +1167,10 @@ func (o *Options) filterRegistry() filters.Registry {
}
}

if o.RegisterFilters != nil {
o.RegisterFilters(registry)
}

return registry
}

Expand Down
48 changes: 33 additions & 15 deletions skipper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,24 +89,42 @@ func findAddress() (string, error) {
}

func TestOptionsFilterRegistry(t *testing.T) {
o := &Options{
CustomFilters: []filters.Spec{auth.NewBearerInjector(nil)},
}
fr := o.filterRegistry()
t.Run("custom filters", func(t *testing.T) {
o := &Options{
CustomFilters: []filters.Spec{auth.NewBearerInjector(nil)},
}
fr := o.filterRegistry()

assert.Contains(t, fr, filters.SetRequestHeaderName)
assert.Contains(t, fr, filters.LuaName)
assert.Contains(t, fr, filters.BearerInjectorName)
assert.Contains(t, fr, filters.SetRequestHeaderName)
assert.Contains(t, fr, filters.LuaName)
assert.Contains(t, fr, filters.BearerInjectorName)
})

o = &Options{
CustomFilters: []filters.Spec{auth.NewBearerInjector(nil)},
DisabledFilters: []string{filters.LuaName, filters.BearerInjectorName},
}
fr = o.filterRegistry()
t.Run("disabled filters", func(t *testing.T) {
o := &Options{
CustomFilters: []filters.Spec{auth.NewBearerInjector(nil)},
DisabledFilters: []string{filters.LuaName, filters.BearerInjectorName},
}
fr := o.filterRegistry()

assert.Contains(t, fr, filters.SetRequestHeaderName)
assert.NotContains(t, fr, filters.LuaName)
assert.NotContains(t, fr, filters.BearerInjectorName)
assert.Contains(t, fr, filters.SetRequestHeaderName)
assert.NotContains(t, fr, filters.LuaName)
assert.NotContains(t, fr, filters.BearerInjectorName)
})

t.Run("register filters", func(t *testing.T) {
o := &Options{
CustomFilters: []filters.Spec{auth.NewBearerInjector(nil)},
RegisterFilters: func(registry filters.Registry) {
registry.Register(auth.NewWebhook(0))
},
}
fr := o.filterRegistry()

assert.Contains(t, fr, filters.SetRequestHeaderName)
assert.Contains(t, fr, filters.BearerInjectorName)
assert.Contains(t, fr, filters.WebhookName)
})
}

func TestOptionsOpenTracingTracerInstanceOverridesOpenTracing(t *testing.T) {
Expand Down
Loading