Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(example): adds support for request ID propagation in logs. #1121

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion examples/http-server/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions examples/http-server/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
23 changes: 19 additions & 4 deletions examples/http-server/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"log"
"net/http"
Expand All @@ -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) {
Expand All @@ -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")

Expand All @@ -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)
}
Loading