From d22db7c786b0bf323fbf5fae75d3b40059ce7bcc Mon Sep 17 00:00:00 2001 From: Aayush garg Date: Mon, 12 Feb 2024 18:14:27 +0530 Subject: [PATCH 1/4] Added support to send response header --- v3/integrations/nrecho-v3/nrecho.go | 4 +- v3/integrations/nrecho-v4/nrecho.go | 4 +- v3/integrations/nrfasthttp/instrumentation.go | 6 ++- v3/integrations/nrgin/nrgin.go | 3 ++ v3/integrations/nrgorilla/nrgorilla.go | 3 ++ v3/integrations/nrhttprouter/nrhttprouter.go | 47 ++++++++++--------- v3/newrelic/instrumentation.go | 3 ++ 7 files changed, 45 insertions(+), 25 deletions(-) diff --git a/v3/integrations/nrecho-v3/nrecho.go b/v3/integrations/nrecho-v3/nrecho.go index ad5f862b8..bf7c718bc 100644 --- a/v3/integrations/nrecho-v3/nrecho.go +++ b/v3/integrations/nrecho-v3/nrecho.go @@ -51,7 +51,6 @@ func transactionName(c echo.Context) string { // e := echo.New() // // Add the nrecho middleware before other middlewares or routes: // e.Use(nrecho.Middleware(app)) -// func Middleware(app *newrelic.Application) func(echo.HandlerFunc) echo.HandlerFunc { if nil == app { @@ -87,6 +86,9 @@ func Middleware(app *newrelic.Application) func(echo.HandlerFunc) echo.HandlerFu } else { txn.SetWebResponse(nil).WriteHeader(http.StatusInternalServerError) } + if newrelic.IsSecurityAgentPresent() { + newrelic.GetSecurityAgentInterface().SendEvent("RESPONSE_HEADER", c.Response().Header()) + } } return diff --git a/v3/integrations/nrecho-v4/nrecho.go b/v3/integrations/nrecho-v4/nrecho.go index 4c3b8cd18..e6738005b 100644 --- a/v3/integrations/nrecho-v4/nrecho.go +++ b/v3/integrations/nrecho-v4/nrecho.go @@ -71,7 +71,6 @@ func WithSkipper(skipper Skipper) ConfigOption { // e := echo.New() // // Add the nrecho middleware before other middlewares or routes: // e.Use(nrecho.MiddlewareWithConfig(nrecho.Config{App: app})) -// func Middleware(app *newrelic.Application, opts ...ConfigOption) func(echo.HandlerFunc) echo.HandlerFunc { if app == nil { return func(next echo.HandlerFunc) echo.HandlerFunc { @@ -125,6 +124,9 @@ func Middleware(app *newrelic.Application, opts ...ConfigOption) func(echo.Handl } else { txn.SetWebResponse(nil).WriteHeader(http.StatusInternalServerError) } + if newrelic.IsSecurityAgentPresent() { + newrelic.GetSecurityAgentInterface().SendEvent("RESPONSE_HEADER", c.Response().Header()) + } } return diff --git a/v3/integrations/nrfasthttp/instrumentation.go b/v3/integrations/nrfasthttp/instrumentation.go index abefb8460..89f2bcdbf 100644 --- a/v3/integrations/nrfasthttp/instrumentation.go +++ b/v3/integrations/nrfasthttp/instrumentation.go @@ -71,7 +71,11 @@ func WrapHandle(app *newrelic.Application, pattern string, handler fasthttp.Requ handler(ctx) if newrelic.IsSecurityAgentPresent() { - newrelic.GetSecurityAgentInterface().SendEvent("INBOUND_WRITE", resp.Body(), resp.Header()) + header := resp.Header() + ctx.Response.Header.VisitAllCookie(func(key, value []byte) { + header.Add("Set-Cookie", string(value)) + }) + newrelic.GetSecurityAgentInterface().SendEvent("INBOUND_WRITE", resp.Body(), header) } } } diff --git a/v3/integrations/nrgin/nrgin.go b/v3/integrations/nrgin/nrgin.go index 88ef11b24..c29e50a2f 100644 --- a/v3/integrations/nrgin/nrgin.go +++ b/v3/integrations/nrgin/nrgin.go @@ -165,5 +165,8 @@ func middleware(app *newrelic.Application, useNewNames bool) gin.HandlerFunc { c.Set(internal.GinTransactionContextKey, txn) } c.Next() + if newrelic.IsSecurityAgentPresent() { + newrelic.GetSecurityAgentInterface().SendEvent("RESPONSE_HEADER", c.Writer.Header()) + } } } diff --git a/v3/integrations/nrgorilla/nrgorilla.go b/v3/integrations/nrgorilla/nrgorilla.go index dd2088a25..b5acb36b9 100644 --- a/v3/integrations/nrgorilla/nrgorilla.go +++ b/v3/integrations/nrgorilla/nrgorilla.go @@ -108,6 +108,9 @@ func Middleware(app *newrelic.Application) mux.MiddlewareFunc { w = txn.SetWebResponse(w) r = newrelic.RequestWithTransactionContext(r, txn) next.ServeHTTP(w, r) + if newrelic.IsSecurityAgentPresent() { + newrelic.GetSecurityAgentInterface().SendEvent("RESPONSE_HEADER", w.Header()) + } }) } } diff --git a/v3/integrations/nrhttprouter/nrhttprouter.go b/v3/integrations/nrhttprouter/nrhttprouter.go index 75cb73ff4..a1c5d8c30 100644 --- a/v3/integrations/nrhttprouter/nrhttprouter.go +++ b/v3/integrations/nrhttprouter/nrhttprouter.go @@ -8,33 +8,33 @@ // httprouter.Router. Use an *nrhttprouter.Router in place of your // *httprouter.Router. Example: // -// package main +// package main // -// import ( -// "fmt" -// "net/http" -// "os" +// import ( +// "fmt" +// "net/http" +// "os" // -// "github.com/julienschmidt/httprouter" -// newrelic "github.com/newrelic/go-agent/v3/newrelic" -// "github.com/newrelic/go-agent/v3/integrations/nrhttprouter" -// ) +// "github.com/julienschmidt/httprouter" +// newrelic "github.com/newrelic/go-agent/v3/newrelic" +// "github.com/newrelic/go-agent/v3/integrations/nrhttprouter" +// ) // -// func main() { -// cfg := newrelic.NewConfig("httprouter App", os.Getenv("NEW_RELIC_LICENSE_KEY")) -// app, _ := newrelic.NewApplication(cfg) +// func main() { +// cfg := newrelic.NewConfig("httprouter App", os.Getenv("NEW_RELIC_LICENSE_KEY")) +// app, _ := newrelic.NewApplication(cfg) // -// // Create the Router replacement: -// router := nrhttprouter.New(app) +// // Create the Router replacement: +// router := nrhttprouter.New(app) // -// router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { -// w.Write([]byte("welcome\n")) -// }) -// router.GET("/hello/:name", (w http.ResponseWriter, r *http.Request, ps httprouter.Params) { -// w.Write([]byte(fmt.Sprintf("hello %s\n", ps.ByName("name")))) -// }) -// http.ListenAndServe(":8000", router) -// } +// router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) { +// w.Write([]byte("welcome\n")) +// }) +// router.GET("/hello/:name", (w http.ResponseWriter, r *http.Request, ps httprouter.Params) { +// w.Write([]byte(fmt.Sprintf("hello %s\n", ps.ByName("name")))) +// }) +// http.ListenAndServe(":8000", router) +// } // // Runnable example: https://github.com/newrelic/go-agent/tree/master/v3/integrations/nrhttprouter/example/main.go package nrhttprouter @@ -153,4 +153,7 @@ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) { } r.Router.ServeHTTP(w, req) + if newrelic.IsSecurityAgentPresent() { + newrelic.GetSecurityAgentInterface().SendEvent("RESPONSE_HEADER", w.Header()) + } } diff --git a/v3/newrelic/instrumentation.go b/v3/newrelic/instrumentation.go index d0ffd7379..5eec4f6f0 100644 --- a/v3/newrelic/instrumentation.go +++ b/v3/newrelic/instrumentation.go @@ -73,6 +73,9 @@ func WrapHandle(app *Application, pattern string, handler http.Handler, options r = RequestWithTransactionContext(r, txn) handler.ServeHTTP(w, r) + if IsSecurityAgentPresent() { + secureAgent.SendEvent("RESPONSE_HEADER", w.Header()) + } }) } From e330a5e8e3c5ea3e53365cf24e921e347d0912cf Mon Sep 17 00:00:00 2001 From: Aayush garg Date: Tue, 23 Jul 2024 16:50:59 +0530 Subject: [PATCH 2/4] added customDataType for graphql --- v3/integrations/nrgraphqlgo/nrgraphqlgo.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/v3/integrations/nrgraphqlgo/nrgraphqlgo.go b/v3/integrations/nrgraphqlgo/nrgraphqlgo.go index 99560f9d6..06a5dfbdd 100644 --- a/v3/integrations/nrgraphqlgo/nrgraphqlgo.go +++ b/v3/integrations/nrgraphqlgo/nrgraphqlgo.go @@ -42,7 +42,10 @@ type Extension struct{} var _ graphql.Extension = Extension{} // Init is used to help you initialize the extension - in this case, a noop -func (Extension) Init(ctx context.Context, _ *graphql.Params) context.Context { +func (Extension) Init(ctx context.Context, params *graphql.Params) context.Context { + if params != nil && newrelic.IsSecurityAgentPresent() { + newrelic.GetSecurityAgentInterface().SendEvent("GRAPHQL", params.RequestString != "", len(params.VariableValues) != 0) + } return ctx } From f1b5be7cdbb2a90365c0bdc8403ecf9b18c49234 Mon Sep 17 00:00:00 2001 From: Aayush garg Date: Tue, 20 Aug 2024 10:00:55 +0530 Subject: [PATCH 3/4] remove graphql changes --- v3/integrations/nrgraphqlgo/nrgraphqlgo.go | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/v3/integrations/nrgraphqlgo/nrgraphqlgo.go b/v3/integrations/nrgraphqlgo/nrgraphqlgo.go index 06a5dfbdd..99560f9d6 100644 --- a/v3/integrations/nrgraphqlgo/nrgraphqlgo.go +++ b/v3/integrations/nrgraphqlgo/nrgraphqlgo.go @@ -42,10 +42,7 @@ type Extension struct{} var _ graphql.Extension = Extension{} // Init is used to help you initialize the extension - in this case, a noop -func (Extension) Init(ctx context.Context, params *graphql.Params) context.Context { - if params != nil && newrelic.IsSecurityAgentPresent() { - newrelic.GetSecurityAgentInterface().SendEvent("GRAPHQL", params.RequestString != "", len(params.VariableValues) != 0) - } +func (Extension) Init(ctx context.Context, _ *graphql.Params) context.Context { return ctx } From c180aa25ff3e0822d36aa559fcb012e2441bf350 Mon Sep 17 00:00:00 2001 From: Aayush garg Date: Wed, 28 Aug 2024 08:41:50 +0530 Subject: [PATCH 4/4] update csec-go-agent version --- v3/integrations/nrsecurityagent/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/v3/integrations/nrsecurityagent/go.mod b/v3/integrations/nrsecurityagent/go.mod index aae7f2977..d963fe5e8 100644 --- a/v3/integrations/nrsecurityagent/go.mod +++ b/v3/integrations/nrsecurityagent/go.mod @@ -3,7 +3,7 @@ module github.com/newrelic/go-agent/v3/integrations/nrsecurityagent go 1.20 require ( - github.com/newrelic/csec-go-agent v1.3.0 + github.com/newrelic/csec-go-agent v1.4.0 github.com/newrelic/go-agent/v3 v3.33.1 github.com/newrelic/go-agent/v3/integrations/nrsqlite3 v1.2.0 gopkg.in/yaml.v2 v2.4.0