Skip to content

Commit

Permalink
feat: uses options instead of functional arguments for NewTransaction…
Browse files Browse the repository at this point in the history
…WithOptions.
  • Loading branch information
jcchavezs committed Jan 30, 2024
1 parent 15af95d commit 045ae73
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 28 deletions.
23 changes: 2 additions & 21 deletions experimental/waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
32 changes: 32 additions & 0 deletions experimental/waf_test.go
Original file line number Diff line number Diff line change
@@ -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
}
4 changes: 3 additions & 1 deletion http/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
})
}
}

Expand Down
2 changes: 2 additions & 0 deletions internal/corazawaf/waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 2 additions & 6 deletions waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 045ae73

Please sign in to comment.