From 87c891508b2ede7574826532fe62bd0f8a904efd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carlos=20Ch=C3=A1vez?= Date: Sat, 3 Aug 2024 02:31:10 -0400 Subject: [PATCH] feat(example): adds support for request ID propagation in logs. --- examples/http-server/go.mod | 5 ++++- examples/http-server/go.sum | 2 ++ examples/http-server/main.go | 23 +++++++++++++++++++---- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/examples/http-server/go.mod b/examples/http-server/go.mod index 3024f3d0b..cdba4a36b 100644 --- a/examples/http-server/go.mod +++ b/examples/http-server/go.mod @@ -2,7 +2,10 @@ module github.com/corazawaf/coraza/v3/examples/http-server go 1.21 -require github.com/corazawaf/coraza/v3 v3.2.1 +require ( + github.com/corazawaf/coraza/v3 v3.2.1 + github.com/go-chi/chi v1.5.5 +) require ( github.com/corazawaf/libinjection-go v0.2.1 // indirect diff --git a/examples/http-server/go.sum b/examples/http-server/go.sum index f7bb7583a..fc3e0cf71 100644 --- a/examples/http-server/go.sum +++ b/examples/http-server/go.sum @@ -4,6 +4,8 @@ github.com/corazawaf/libinjection-go v0.2.1 h1:vNJ7L6c4xkhRgYU6sIO0Tl54TmeCQv/yf github.com/corazawaf/libinjection-go v0.2.1/go.mod h1:OP4TM7xdJ2skyXqNX1AN1wN5nNZEmJNuWbNPOItn7aw= github.com/foxcpp/go-mockdns v1.1.0 h1:jI0rD8M0wuYAxL7r/ynTrCQQq0BVqfB99Vgk7DlmewI= github.com/foxcpp/go-mockdns v1.1.0/go.mod h1:IhLeSFGed3mJIAXPH2aiRQB+kqz7oqu8ld2qVbOu7Wk= +github.com/go-chi/chi v1.5.5 h1:vOB/HbEMt9QqBqErz07QehcOKHaWFtuj87tTDVz2qXE= +github.com/go-chi/chi v1.5.5/go.mod h1:C9JqLr3tIYjDOZpzn+BCuxY8z8vmca43EeMgyZt7irw= github.com/magefile/mage v1.15.0 h1:BvGheCMAsG3bWUDbZ8AyXXpCNwU9u5CB6sM+HNb9HYg= github.com/magefile/mage v1.15.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= github.com/miekg/dns v1.1.57 h1:Jzi7ApEIzwEPLHWRcafCN9LZSBbqQpxjt/wpgvg7wcM= diff --git a/examples/http-server/main.go b/examples/http-server/main.go index c3e8bc122..e82b50fd6 100644 --- a/examples/http-server/main.go +++ b/examples/http-server/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "log" "net/http" @@ -10,6 +11,7 @@ import ( "github.com/corazawaf/coraza/v3" txhttp "github.com/corazawaf/coraza/v3/http" "github.com/corazawaf/coraza/v3/types" + "github.com/go-chi/chi/middleware" ) func exampleHandler(w http.ResponseWriter, req *http.Request) { @@ -32,7 +34,9 @@ func exampleHandler(w http.ResponseWriter, req *http.Request) { func main() { waf := createWAF() - http.Handle("/", txhttp.WrapHandler(waf, http.HandlerFunc(exampleHandler))) + http.Handle("/", middleware.RequestID( // makes sure the request ID is propagated in the context + txhttp.WrapHandler(waf, http.HandlerFunc(exampleHandler)), + )) fmt.Println("Server is running. Listening port: 8090") @@ -53,10 +57,21 @@ func createWAF() coraza.WAF { if err != nil { log.Fatal(err) } + return waf } -func logError(error types.MatchedRule) { - msg := error.ErrorLog() - fmt.Printf("[logError][%s] %s\n", error.Rule().Severity(), msg) +type Contexter interface { + Context() context.Context +} + +func logError(mr types.MatchedRule) { + msg := mr.ErrorLog() + if ctxMR, ok := mr.(Contexter); ok { + if requestID := middleware.GetReqID(ctxMR.Context()); requestID != "" { + msg = fmt.Sprintf("%s [request_id %q]", msg, requestID) + } + } + + fmt.Println(msg) }