Skip to content

Commit baf203e

Browse files
committed
implement waf.Close method, based on the PR corazawaf#1200
1 parent f5ed9bc commit baf203e

File tree

6 files changed

+50
-22
lines changed

6 files changed

+50
-22
lines changed

examples/http-server/persistence_collection/custom/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ func exampleHandler(w http.ResponseWriter, req *http.Request) {
3131
}
3232

3333
func main() {
34-
directivesFile := "./session.conf"
35-
waf := createWAF(directivesFile)
34+
directiveFile := "./session.conf"
35+
waf := createWAF(directiveFile)
3636

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

experimental/waf.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,19 @@
44
package experimental
55

66
import (
7-
"github.com/corazawaf/coraza/v3/internal/corazawaf"
7+
"io"
8+
9+
"github.com/corazawaf/coraza/v3"
810
"github.com/corazawaf/coraza/v3/types"
911
)
1012

11-
type Options = corazawaf.Options
12-
13-
// WAFWithOptions is an interface that allows to create transactions
14-
// with options
15-
type WAFWithOptions interface {
16-
NewTransactionWithOptions(Options) types.Transaction
13+
// WAF IMPORTANT: This interface is experimental and may change in the future
14+
// WAF v4 interface supports creating transactions with options and
15+
// closing the WAF instance to release resources
16+
// This interface will replace coraza.WAF in v4
17+
type WAF interface {
18+
coraza.WAF
19+
io.Closer
20+
// NewTransactionWithOptions creates a new initialized transaction for this WAF instance
21+
NewTransactionWithOptions(coraza.Options) types.Transaction
1722
}

experimental/waf_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,32 @@
11
// Copyright 2024 Juan Pablo Tosso and the OWASP Coraza contributors
22
// SPDX-License-Identifier: Apache-2.0
33

4-
package experimental_test
4+
package experimental
55

66
import (
7-
"fmt"
7+
"testing"
88

99
"github.com/corazawaf/coraza/v3"
10-
"github.com/corazawaf/coraza/v3/experimental"
1110
)
1211

13-
func ExampleWAFWithOptions_NewTransactionWithOptions() {
12+
func TestWAFWithOptions(t *testing.T) {
1413
waf, err := coraza.NewWAF(coraza.NewWAFConfig())
1514
if err != nil {
16-
panic(err)
15+
t.Fatal(err)
1716
}
1817

19-
oWAF, ok := waf.(experimental.WAFWithOptions)
18+
oWAF, ok := waf.(WAF)
2019
if !ok {
21-
panic("WAF does not implement WAFWithOptions")
20+
t.Fatal("WAF does not implement WAF v4")
2221
}
2322

24-
tx := oWAF.NewTransactionWithOptions(experimental.Options{
23+
tx := oWAF.NewTransactionWithOptions(coraza.Options{
2524
ID: "abc123",
2625
})
2726

28-
fmt.Println("Transaction ID:", tx.ID())
27+
if tx.ID() != "abc123" {
28+
t.Error("Transaction ID not set")
29+
}
2930

3031
// Output:
3132
// Transaction ID: abc123

http/middleware.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,9 +105,9 @@ func WrapHandler(waf coraza.WAF, h http.Handler) http.Handler {
105105
return waf.NewTransaction()
106106
}
107107

108-
if ctxwaf, ok := waf.(experimental.WAFWithOptions); ok {
108+
if ctxwaf, ok := waf.(experimental.WAF); ok {
109109
newTX = func(r *http.Request) types.Transaction {
110-
return ctxwaf.NewTransactionWithOptions(experimental.Options{
110+
return ctxwaf.NewTransactionWithOptions(coraza.Options{
111111
Context: r.Context(),
112112
})
113113
}

internal/corazawaf/waf.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,3 +429,16 @@ func (w *WAF) Validate() error {
429429

430430
return nil
431431
}
432+
433+
// Close will release resources used by the WAF instance
434+
func (w *WAF) Close() error {
435+
err := w.PersistenceEngine.Close()
436+
if err != nil {
437+
return fmt.Errorf("failed to close persitence engine: %w", err)
438+
}
439+
err = w.AuditLogWriter().Close()
440+
if err != nil {
441+
return fmt.Errorf("failed to close audit log writer: %w", err)
442+
}
443+
return nil
444+
}

waf.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,17 @@ import (
88
"fmt"
99
"strings"
1010

11-
"github.com/corazawaf/coraza/v3/experimental"
1211
"github.com/corazawaf/coraza/v3/internal/corazawaf"
1312
"github.com/corazawaf/coraza/v3/internal/environment"
1413
"github.com/corazawaf/coraza/v3/internal/seclang"
1514
"github.com/corazawaf/coraza/v3/types"
1615
)
1716

17+
// Options is used to create tranactions with context and ID
18+
// This is only supported as part of the experimental package
19+
// experimental.WAF.NewTransactionWithOptions(Options)
20+
type Options = corazawaf.Options
21+
1822
// WAF instance is used to store configurations and rules
1923
// Every web application should have a different WAF instance,
2024
// but you can share an instance if you are ok with sharing
@@ -152,6 +156,11 @@ func (w wafWrapper) NewTransactionWithID(id string) types.Transaction {
152156
}
153157

154158
// NewTransaction implements the same method on WAF.
155-
func (w wafWrapper) NewTransactionWithOptions(opts experimental.Options) types.Transaction {
159+
func (w wafWrapper) NewTransactionWithOptions(opts Options) types.Transaction {
156160
return w.waf.NewTransactionWithOptions(opts)
157161
}
162+
163+
// Close implements the same method on WAF.
164+
func (w wafWrapper) Close() error {
165+
return w.waf.Close()
166+
}

0 commit comments

Comments
 (0)