From 045ae73afabe6bba29becc97fbb41be61a6893a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Ch=C3=A1vez?= Date: Tue, 30 Jan 2024 09:54:35 +0100 Subject: [PATCH] feat: uses options instead of functional arguments for NewTransactionWithOptions. --- experimental/waf.go | 23 ++--------------------- experimental/waf_test.go | 32 ++++++++++++++++++++++++++++++++ http/middleware.go | 4 +++- internal/corazawaf/waf.go | 2 ++ waf.go | 8 ++------ 5 files changed, 41 insertions(+), 28 deletions(-) create mode 100644 experimental/waf_test.go diff --git a/experimental/waf.go b/experimental/waf.go index 2ef03e549..bc167a2a9 100644 --- a/experimental/waf.go +++ b/experimental/waf.go @@ -4,33 +4,14 @@ package experimental import ( - "context" - "strings" - "github.com/corazawaf/coraza/v3/internal/corazawaf" "github.com/corazawaf/coraza/v3/types" ) -type Option func(*corazawaf.Options) - -// WithID sets the transaction ID -func WithID(id string) Option { - return func(o *corazawaf.Options) { - o.ID = strings.TrimSpace(id) - } -} - -// WithContext sets the transaction context, this is useful for passing -// a context into the logger. -// The transaction lifecycle isn't tied to the context lifecycle. -func WithContext(ctx context.Context) Option { - return func(o *corazawaf.Options) { - o.Context = ctx - } -} +type Options = corazawaf.Options // WAFWithOptions is an interface that allows to create transactions // with options type WAFWithOptions interface { - NewTransactionWithOptions(opts ...Option) types.Transaction + NewTransactionWithOptions(Options) types.Transaction } diff --git a/experimental/waf_test.go b/experimental/waf_test.go new file mode 100644 index 000000000..6d97c22fc --- /dev/null +++ b/experimental/waf_test.go @@ -0,0 +1,32 @@ +// Copyright 2024 Juan Pablo Tosso and the OWASP Coraza contributors +// SPDX-License-Identifier: Apache-2.0 + +package experimental_test + +import ( + "fmt" + + "github.com/corazawaf/coraza/v3" + "github.com/corazawaf/coraza/v3/experimental" +) + +func ExampleWAFWithOptions_NewTransactionWithOptions() { + waf, err := coraza.NewWAF(coraza.NewWAFConfig()) + if err != nil { + panic(err) + } + + oWAF, ok := waf.(experimental.WAFWithOptions) + if !ok { + panic("WAF does not implement WAFWithOptions") + } + + tx := oWAF.NewTransactionWithOptions(experimental.Options{ + ID: "abc123", + }) + + fmt.Println("Transaction ID:", tx.ID()) + + // Output: + // Transaction ID: abc123 +} diff --git a/http/middleware.go b/http/middleware.go index e4bff8915..f8c4477ee 100644 --- a/http/middleware.go +++ b/http/middleware.go @@ -124,7 +124,9 @@ func WrapHandler(waf coraza.WAF, h http.Handler) http.Handler { if ctxwaf, ok := waf.(experimental.WAFWithOptions); ok { newTX = func(r *http.Request) types.Transaction { - return ctxwaf.NewTransactionWithOptions(experimental.WithContext(r.Context())) + return ctxwaf.NewTransactionWithOptions(experimental.Options{ + Context: r.Context(), + }) } } diff --git a/internal/corazawaf/waf.go b/internal/corazawaf/waf.go index 5d280ddc6..233bdd0be 100644 --- a/internal/corazawaf/waf.go +++ b/internal/corazawaf/waf.go @@ -147,6 +147,8 @@ func (w *WAF) NewTransaction() *Transaction { }) } +// NewTransactionWithOptions Creates a new initialized transaction for this WAF +// instance with the provided options func (w *WAF) NewTransactionWithOptions(opts Options) *Transaction { if opts.ID == "" { opts.ID = stringutils.RandomString(19) diff --git a/waf.go b/waf.go index 741c37b0a..219519018 100644 --- a/waf.go +++ b/waf.go @@ -141,10 +141,6 @@ func (w wafWrapper) NewTransactionWithID(id string) types.Transaction { } // NewTransaction implements the same method on WAF. -func (w wafWrapper) NewTransactionWithOptions(opts ...experimental.Option) types.Transaction { - o := corazawaf.Options{} - for _, opt := range opts { - opt(&o) - } - return w.waf.NewTransactionWithOptions(o) +func (w wafWrapper) NewTransactionWithOptions(opts experimental.Options) types.Transaction { + return w.waf.NewTransactionWithOptions(opts) }