Skip to content

Commit

Permalink
chore: simplify transaction creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
jcchavezs committed Jan 25, 2024
1 parent 4b7a809 commit ab88652
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 23 deletions.
7 changes: 3 additions & 4 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package coraza

import (
"context"
"io/fs"

"github.com/corazawaf/coraza/v4/debuglog"
Expand Down Expand Up @@ -60,7 +59,7 @@ type WAFConfig interface {
// WithErrorCallback configures an error callback that can be used
// to log errors triggered by the WAF.
// It contains the severity so the cb can decide to skip it or not
WithErrorCallback(logger func(ctx context.Context, rule types.MatchedRule)) WAFConfig
WithErrorCallback(logger func(rule types.MatchedRule)) WAFConfig

// WithRootFS configures the root file system.
WithRootFS(fs fs.FS) WAFConfig
Expand Down Expand Up @@ -104,7 +103,7 @@ type wafConfig struct {
responseBodyLimit *int
responseBodyMimeTypes []string
debugLogger debuglog.Logger
errorCallback func(ctx context.Context, rule types.MatchedRule)
errorCallback func(rule types.MatchedRule)
fsRoot fs.FS
}

Expand Down Expand Up @@ -150,7 +149,7 @@ func (c *wafConfig) WithDebugLogger(logger debuglog.Logger) WAFConfig {
return ret
}

func (c *wafConfig) WithErrorCallback(logger func(context.Context, types.MatchedRule)) WAFConfig {
func (c *wafConfig) WithErrorCallback(logger func(types.MatchedRule)) WAFConfig {
ret := c.clone()
ret.errorCallback = logger
return ret
Expand Down
3 changes: 1 addition & 2 deletions examples/http-server/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"context"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -57,7 +56,7 @@ func createWAF() coraza.WAF {
return waf
}

func logError(ctx context.Context, error types.MatchedRule) {
func logError(error types.MatchedRule) {
msg := error.ErrorLog()
fmt.Printf("[logError][%s] %s\n", error.Rule().Severity(), msg)
}
9 changes: 9 additions & 0 deletions internal/corazarules/rule_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package corazarules

import (
"context"
"fmt"
"strconv"
"strings"
Expand All @@ -30,6 +31,8 @@ type MatchData struct {
ChainLevel_ int
}

var _ types.MatchData = (*MatchData)(nil)

func (m *MatchData) Variable() variables.RuleVariable {
return m.Variable_
}
Expand Down Expand Up @@ -99,6 +102,8 @@ type MatchedRule struct {
// A slice of matched variables
MatchedDatas_ []types.MatchData

Context_ context.Context

Rule_ types.RuleMetadata
}

Expand All @@ -114,6 +119,10 @@ func (mr *MatchedRule) URI() string {
return mr.URI_
}

func (mr *MatchedRule) Context() context.Context {
return mr.Context_
}

func (mr *MatchedRule) TransactionID() string {
return mr.TransactionID_
}
Expand Down
3 changes: 2 additions & 1 deletion internal/corazawaf/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ func (tx *Transaction) MatchRule(r *Rule, mds []types.MatchData) {
Rule_: &r.RuleMetadata,
Log_: r.Log,
MatchedDatas_: mds,
Context_: tx.ctx,
}
// Populate MatchedRule disruption related fields only if the Engine is capable of performing disruptive actions
if tx.RuleEngine == types.RuleEngineOn {
Expand Down Expand Up @@ -518,7 +519,7 @@ func (tx *Transaction) MatchRule(r *Rule, mds []types.MatchData) {

tx.matchedRules = append(tx.matchedRules, mr)
if tx.WAF.ErrorLogCb != nil && r.Log {
tx.WAF.ErrorLogCb(tx.ctx, mr)
tx.WAF.ErrorLogCb(mr)
}

}
Expand Down
2 changes: 1 addition & 1 deletion internal/corazawaf/transaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ func TestLogCallback(t *testing.T) {
buffer = mr.ErrorLog()
})
waf.RuleEngine = testCase.engineStatus
tx := waf.NewTransaction()
tx := waf.NewTransaction(&Options{})

Check failure on line 748 in internal/corazawaf/transaction_test.go

View workflow job for this annotation

GitHub Actions / test (1.19.x, ubuntu-latest)

too many arguments in call to waf.NewTransaction

Check failure on line 748 in internal/corazawaf/transaction_test.go

View workflow job for this annotation

GitHub Actions / test (1.20.x, ubuntu-latest)

too many arguments in call to waf.NewTransaction

Check failure on line 748 in internal/corazawaf/transaction_test.go

View workflow job for this annotation

GitHub Actions / test (1.21.x, ubuntu-latest)

too many arguments in call to waf.NewTransaction
rule := NewRule()
rule.ID_ = 1
rule.Phase_ = 1
Expand Down
26 changes: 16 additions & 10 deletions internal/corazawaf/waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ type WAF struct {
// Used for the debug logger
Logger debuglog.Logger

ErrorLogCb func(ctx context.Context, rule types.MatchedRule)
ErrorLogCb func(rule types.MatchedRule)

// Audit mode status
AuditEngine types.AuditEngineStatus
Expand Down Expand Up @@ -138,18 +138,24 @@ type Options struct {
Context context.Context
}

func (o *Options) Backfill() {
if o.ID == "" {
o.ID = stringutils.RandomString(19)
}
func (w *WAF) NewTransaction() *Transaction {
return w.newTransaction(&Options{})
}

if o.Context == nil {
o.Context = context.Background()
}
func (w *WAF) NewTransactionWithOptions(opts *Options) *Transaction {
return w.newTransaction(opts)
}

// NewTransaction Creates a new initialized transaction for this WAF instance
func (w *WAF) NewTransaction(opts *Options) *Transaction {
func (w *WAF) newTransaction(opts *Options) *Transaction {
if opts.ID == "" {
opts.ID = stringutils.RandomString(19)
}

if opts.Context == nil {
opts.Context = context.Background()
}

tx := w.txPool.Get().(*Transaction)
tx.id = opts.ID
tx.ctx = opts.Context
Expand Down Expand Up @@ -352,7 +358,7 @@ func (w *WAF) InitAuditLogWriter() error {
// SetErrorCallback sets the callback function for error logging
// The error callback receives all the error data and some
// helpers to write modsecurity style logs
func (w *WAF) SetErrorCallback(cb func(context.Context, types.MatchedRule)) {
func (w *WAF) SetErrorCallback(cb func(types.MatchedRule)) {
w.ErrorLogCb = cb
}

Expand Down
9 changes: 8 additions & 1 deletion types/rule_match.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

package types

import "github.com/corazawaf/coraza/v4/types/variables"
import (
"context"

"github.com/corazawaf/coraza/v4/types/variables"
)

// MatchData works like VariableKey but is used for logging,
// so it contains the collection as a string, and it's value
Expand Down Expand Up @@ -45,5 +49,8 @@ type MatchedRule interface {
Rule() RuleMetadata

AuditLog() string

ErrorLog() string

Context() context.Context
}
7 changes: 3 additions & 4 deletions waf.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package coraza
import (
"context"
"fmt"
"strings"

"github.com/corazawaf/coraza/v4/internal/corazawaf"
"github.com/corazawaf/coraza/v4/internal/seclang"
Expand All @@ -16,7 +17,7 @@ type Option func(*corazawaf.Options)

func WithID(id string) Option {
return func(o *corazawaf.Options) {
o.ID = id
o.ID = strings.TrimSpace(id)
}
}

Expand Down Expand Up @@ -142,7 +143,5 @@ func (w wafWrapper) NewTransaction(opts ...Option) types.Transaction {
for _, opt := range opts {
opt(o)
}
o.Backfill()

return w.waf.NewTransaction(o)
return w.waf.NewTransactionWithOptions(o)
}

0 comments on commit ab88652

Please sign in to comment.