File tree Expand file tree Collapse file tree 6 files changed +50
-22
lines changed
examples/http-server/persistence_collection/custom Expand file tree Collapse file tree 6 files changed +50
-22
lines changed Original file line number Diff line number Diff line change @@ -31,8 +31,8 @@ func exampleHandler(w http.ResponseWriter, req *http.Request) {
31
31
}
32
32
33
33
func main () {
34
- directivesFile := "./session.conf"
35
- waf := createWAF (directivesFile )
34
+ directiveFile := "./session.conf"
35
+ waf := createWAF (directiveFile )
36
36
37
37
http .Handle ("/" , txhttp .WrapHandler (waf , http .HandlerFunc (exampleHandler )))
38
38
Original file line number Diff line number Diff line change 4
4
package experimental
5
5
6
6
import (
7
- "github.com/corazawaf/coraza/v3/internal/corazawaf"
7
+ "io"
8
+
9
+ "github.com/corazawaf/coraza/v3"
8
10
"github.com/corazawaf/coraza/v3/types"
9
11
)
10
12
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
17
22
}
Original file line number Diff line number Diff line change 1
1
// Copyright 2024 Juan Pablo Tosso and the OWASP Coraza contributors
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
4
- package experimental_test
4
+ package experimental
5
5
6
6
import (
7
- "fmt "
7
+ "testing "
8
8
9
9
"github.com/corazawaf/coraza/v3"
10
- "github.com/corazawaf/coraza/v3/experimental"
11
10
)
12
11
13
- func ExampleWAFWithOptions_NewTransactionWithOptions ( ) {
12
+ func TestWAFWithOptions ( t * testing. T ) {
14
13
waf , err := coraza .NewWAF (coraza .NewWAFConfig ())
15
14
if err != nil {
16
- panic (err )
15
+ t . Fatal (err )
17
16
}
18
17
19
- oWAF , ok := waf .(experimental. WAFWithOptions )
18
+ oWAF , ok := waf .(WAF )
20
19
if ! ok {
21
- panic ("WAF does not implement WAFWithOptions " )
20
+ t . Fatal ("WAF does not implement WAF v4 " )
22
21
}
23
22
24
- tx := oWAF .NewTransactionWithOptions (experimental .Options {
23
+ tx := oWAF .NewTransactionWithOptions (coraza .Options {
25
24
ID : "abc123" ,
26
25
})
27
26
28
- fmt .Println ("Transaction ID:" , tx .ID ())
27
+ if tx .ID () != "abc123" {
28
+ t .Error ("Transaction ID not set" )
29
+ }
29
30
30
31
// Output:
31
32
// Transaction ID: abc123
Original file line number Diff line number Diff line change @@ -105,9 +105,9 @@ func WrapHandler(waf coraza.WAF, h http.Handler) http.Handler {
105
105
return waf .NewTransaction ()
106
106
}
107
107
108
- if ctxwaf , ok := waf .(experimental.WAFWithOptions ); ok {
108
+ if ctxwaf , ok := waf .(experimental.WAF ); ok {
109
109
newTX = func (r * http.Request ) types.Transaction {
110
- return ctxwaf .NewTransactionWithOptions (experimental .Options {
110
+ return ctxwaf .NewTransactionWithOptions (coraza .Options {
111
111
Context : r .Context (),
112
112
})
113
113
}
Original file line number Diff line number Diff line change @@ -429,3 +429,16 @@ func (w *WAF) Validate() error {
429
429
430
430
return nil
431
431
}
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
+ }
Original file line number Diff line number Diff line change @@ -8,13 +8,17 @@ import (
8
8
"fmt"
9
9
"strings"
10
10
11
- "github.com/corazawaf/coraza/v3/experimental"
12
11
"github.com/corazawaf/coraza/v3/internal/corazawaf"
13
12
"github.com/corazawaf/coraza/v3/internal/environment"
14
13
"github.com/corazawaf/coraza/v3/internal/seclang"
15
14
"github.com/corazawaf/coraza/v3/types"
16
15
)
17
16
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
+
18
22
// WAF instance is used to store configurations and rules
19
23
// Every web application should have a different WAF instance,
20
24
// but you can share an instance if you are ok with sharing
@@ -152,6 +156,11 @@ func (w wafWrapper) NewTransactionWithID(id string) types.Transaction {
152
156
}
153
157
154
158
// 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 {
156
160
return w .waf .NewTransactionWithOptions (opts )
157
161
}
162
+
163
+ // Close implements the same method on WAF.
164
+ func (w wafWrapper ) Close () error {
165
+ return w .waf .Close ()
166
+ }
You can’t perform that action at this time.
0 commit comments