Skip to content

Commit

Permalink
implement waf.Close method, based on the PR corazawaf#1200
Browse files Browse the repository at this point in the history
  • Loading branch information
tty2 committed Dec 31, 2024
1 parent f5ed9bc commit baf203e
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 22 deletions.
4 changes: 2 additions & 2 deletions examples/http-server/persistence_collection/custom/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func exampleHandler(w http.ResponseWriter, req *http.Request) {
}

func main() {
directivesFile := "./session.conf"
waf := createWAF(directivesFile)
directiveFile := "./session.conf"
waf := createWAF(directiveFile)

http.Handle("/", txhttp.WrapHandler(waf, http.HandlerFunc(exampleHandler)))

Expand Down
19 changes: 12 additions & 7 deletions experimental/waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@
package experimental

import (
"github.com/corazawaf/coraza/v3/internal/corazawaf"
"io"

"github.com/corazawaf/coraza/v3"
"github.com/corazawaf/coraza/v3/types"
)

type Options = corazawaf.Options

// WAFWithOptions is an interface that allows to create transactions
// with options
type WAFWithOptions interface {
NewTransactionWithOptions(Options) types.Transaction
// WAF IMPORTANT: This interface is experimental and may change in the future
// WAF v4 interface supports creating transactions with options and
// closing the WAF instance to release resources
// This interface will replace coraza.WAF in v4
type WAF interface {
coraza.WAF
io.Closer
// NewTransactionWithOptions creates a new initialized transaction for this WAF instance
NewTransactionWithOptions(coraza.Options) types.Transaction
}
19 changes: 10 additions & 9 deletions experimental/waf_test.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
// Copyright 2024 Juan Pablo Tosso and the OWASP Coraza contributors
// SPDX-License-Identifier: Apache-2.0

package experimental_test
package experimental

import (
"fmt"
"testing"

"github.com/corazawaf/coraza/v3"
"github.com/corazawaf/coraza/v3/experimental"
)

func ExampleWAFWithOptions_NewTransactionWithOptions() {
func TestWAFWithOptions(t *testing.T) {
waf, err := coraza.NewWAF(coraza.NewWAFConfig())
if err != nil {
panic(err)
t.Fatal(err)
}

oWAF, ok := waf.(experimental.WAFWithOptions)
oWAF, ok := waf.(WAF)
if !ok {
panic("WAF does not implement WAFWithOptions")
t.Fatal("WAF does not implement WAF v4")
}

tx := oWAF.NewTransactionWithOptions(experimental.Options{
tx := oWAF.NewTransactionWithOptions(coraza.Options{
ID: "abc123",
})

fmt.Println("Transaction ID:", tx.ID())
if tx.ID() != "abc123" {
t.Error("Transaction ID not set")
}

// Output:
// Transaction ID: abc123
Expand Down
4 changes: 2 additions & 2 deletions http/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ func WrapHandler(waf coraza.WAF, h http.Handler) http.Handler {
return waf.NewTransaction()
}

if ctxwaf, ok := waf.(experimental.WAFWithOptions); ok {
if ctxwaf, ok := waf.(experimental.WAF); ok {
newTX = func(r *http.Request) types.Transaction {
return ctxwaf.NewTransactionWithOptions(experimental.Options{
return ctxwaf.NewTransactionWithOptions(coraza.Options{
Context: r.Context(),
})
}
Expand Down
13 changes: 13 additions & 0 deletions internal/corazawaf/waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,16 @@ func (w *WAF) Validate() error {

return nil
}

// Close will release resources used by the WAF instance
func (w *WAF) Close() error {
err := w.PersistenceEngine.Close()
if err != nil {
return fmt.Errorf("failed to close persitence engine: %w", err)
}
err = w.AuditLogWriter().Close()
if err != nil {
return fmt.Errorf("failed to close audit log writer: %w", err)
}
return nil
}
13 changes: 11 additions & 2 deletions waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ import (
"fmt"
"strings"

"github.com/corazawaf/coraza/v3/experimental"
"github.com/corazawaf/coraza/v3/internal/corazawaf"
"github.com/corazawaf/coraza/v3/internal/environment"
"github.com/corazawaf/coraza/v3/internal/seclang"
"github.com/corazawaf/coraza/v3/types"
)

// Options is used to create tranactions with context and ID
// This is only supported as part of the experimental package
// experimental.WAF.NewTransactionWithOptions(Options)
type Options = corazawaf.Options

// WAF instance is used to store configurations and rules
// Every web application should have a different WAF instance,
// but you can share an instance if you are ok with sharing
Expand Down Expand Up @@ -152,6 +156,11 @@ func (w wafWrapper) NewTransactionWithID(id string) types.Transaction {
}

// NewTransaction implements the same method on WAF.
func (w wafWrapper) NewTransactionWithOptions(opts experimental.Options) types.Transaction {
func (w wafWrapper) NewTransactionWithOptions(opts Options) types.Transaction {
return w.waf.NewTransactionWithOptions(opts)
}

// Close implements the same method on WAF.
func (w wafWrapper) Close() error {
return w.waf.Close()
}

0 comments on commit baf203e

Please sign in to comment.