From 52e0e838e93343d95685ac74b06eab73ccca43fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=C5=BE=20Jerman?= Date: Mon, 27 Sep 2021 14:11:40 +0200 Subject: [PATCH] Prevent API GW filters from overwriting the same registry struct --- pkg/apigw/filter/postfilter.go | 8 ++++++++ pkg/apigw/filter/prefilter.go | 8 ++++++++ pkg/apigw/filter/processer.go | 8 ++++++++ pkg/apigw/filter/proxy/proxy.go | 4 ++++ pkg/apigw/registry/registry.go | 2 +- pkg/apigw/service_test.go | 1 + pkg/apigw/types/handler.go | 1 + pkg/apigw/types/test.go | 4 ++++ 8 files changed, 35 insertions(+), 1 deletion(-) diff --git a/pkg/apigw/filter/postfilter.go b/pkg/apigw/filter/postfilter.go index 0460eec71..7f983e01a 100644 --- a/pkg/apigw/filter/postfilter.go +++ b/pkg/apigw/filter/postfilter.go @@ -61,6 +61,10 @@ func NewRedirection() (e *redirection) { return } +func (h redirection) New() types.Handler { + return NewRedirection() +} + func (h redirection) String() string { return fmt.Sprintf("apigw filter %s (%s)", h.Name, h.Label) } @@ -109,6 +113,10 @@ func NewDefaultJsonResponse() (e *defaultJsonResponse) { return } +func (h defaultJsonResponse) New() types.Handler { + return NewDefaultJsonResponse() +} + func (h defaultJsonResponse) String() string { return fmt.Sprintf("apigw filter %s (%s)", h.Name, h.Label) } diff --git a/pkg/apigw/filter/prefilter.go b/pkg/apigw/filter/prefilter.go index 5f81a3984..964ee2bdc 100644 --- a/pkg/apigw/filter/prefilter.go +++ b/pkg/apigw/filter/prefilter.go @@ -56,6 +56,10 @@ func NewHeader() (v *header) { return } +func (h header) New() types.Handler { + return NewHeader() +} + func (h header) String() string { return fmt.Sprintf("apigw filter %s (%s)", h.Name, h.Label) } @@ -133,6 +137,10 @@ func NewQueryParam() (v *queryParam) { return } +func (h queryParam) New() types.Handler { + return NewQueryParam() +} + func (h queryParam) String() string { return fmt.Sprintf("apigw filter %s (%s)", h.Name, h.Label) } diff --git a/pkg/apigw/filter/processer.go b/pkg/apigw/filter/processer.go index 0d17ec5aa..00446bb3f 100644 --- a/pkg/apigw/filter/processer.go +++ b/pkg/apigw/filter/processer.go @@ -66,6 +66,10 @@ func NewWorkflow(wf WfExecer) (p *workflow) { return } +func (h workflow) New() types.Handler { + return NewWorkflow(h.d) +} + func (h workflow) String() string { return fmt.Sprintf("apigw filter %s (%s)", h.Name, h.Label) } @@ -185,6 +189,10 @@ func NewPayload(l *zap.Logger) (p *processerPayload) { return } +func (h processerPayload) New() types.Handler { + return NewPayload(h.log) +} + func (h processerPayload) String() string { return fmt.Sprintf("apigw filter %s (%s)", h.Name, h.Label) } diff --git a/pkg/apigw/filter/proxy/proxy.go b/pkg/apigw/filter/proxy/proxy.go index 6995b5dd3..861490c04 100644 --- a/pkg/apigw/filter/proxy/proxy.go +++ b/pkg/apigw/filter/proxy/proxy.go @@ -67,6 +67,10 @@ func New(l *zap.Logger, c *http.Client, s types.SecureStorager) (p *proxy) { return } +func (h proxy) New() types.Handler { + return New(h.log, h.c, h.s) +} + func (h proxy) String() string { return fmt.Sprintf("apigw filter %s (%s)", h.Name, h.Label) } diff --git a/pkg/apigw/registry/registry.go b/pkg/apigw/registry/registry.go index a3d1739f7..fd59c3899 100644 --- a/pkg/apigw/registry/registry.go +++ b/pkg/apigw/registry/registry.go @@ -45,7 +45,7 @@ func (r *Registry) Get(identifier string) (types.Handler, error) { return nil, fmt.Errorf("could not get element from registry: %s", identifier) } - return f, nil + return f.New(), nil } func (r *Registry) All() (list types.FilterMetaList) { diff --git a/pkg/apigw/service_test.go b/pkg/apigw/service_test.go index fe64da38b..a89901b6a 100644 --- a/pkg/apigw/service_test.go +++ b/pkg/apigw/service_test.go @@ -142,6 +142,7 @@ func Test_serviceInit(t *testing.T) { }, }, reg: map[string]types.Handler{"testExistingFilter": &mockExistingHandler{ + MockHandler: &types.MockHandler{}, merge: func(params []byte) (types.Handler, error) { return nil, errors.New("testttt") }, diff --git a/pkg/apigw/types/handler.go b/pkg/apigw/types/handler.go index 6ab71497b..3dddc5cea 100644 --- a/pkg/apigw/types/handler.go +++ b/pkg/apigw/types/handler.go @@ -18,6 +18,7 @@ type ( HTTPHandler fmt.Stringer + New() Handler Merge([]byte) (Handler, error) Meta() FilterMeta } diff --git a/pkg/apigw/types/test.go b/pkg/apigw/types/test.go index b4c812cd7..ba21a1ca1 100644 --- a/pkg/apigw/types/test.go +++ b/pkg/apigw/types/test.go @@ -32,6 +32,10 @@ type ( MockRoundTripper func(*http.Request) (*http.Response, error) ) +func (h MockHandler) New() Handler { + return MockHandler{} +} + func (h MockHandler) String() string { return "MockHandler" }