From 1bc5ed1ef1f1508c88ca25995b881d698b024260 Mon Sep 17 00:00:00 2001 From: Emilio Garcia Date: Thu, 24 Oct 2024 10:01:17 -0400 Subject: [PATCH 1/2] hotfix security agent: only refresh state when connected to new relic --- v3/internal/connect_reply.go | 13 ++++++++++--- v3/internal/connect_reply_test.go | 14 ++++++++++++++ v3/newrelic/secure_agent.go | 2 +- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/v3/internal/connect_reply.go b/v3/internal/connect_reply.go index b5ffaf9fe..ba276e7cf 100644 --- a/v3/internal/connect_reply.go +++ b/v3/internal/connect_reply.go @@ -229,9 +229,9 @@ const ( // consumer. func CreateFullTxnName(input string, reply *ConnectReply, isWeb bool) string { var afterURLRules string - if "" != input { + if input != "" { afterURLRules = reply.URLRules.Apply(input) - if "" == afterURLRules { + if afterURLRules == "" { return "" } } @@ -249,7 +249,7 @@ func CreateFullTxnName(input string, reply *ConnectReply, isWeb bool) string { } afterNameRules := reply.TxnNameRules.Apply(beforeNameRules) - if "" == afterNameRules { + if afterNameRules == "" { return "" } @@ -266,6 +266,13 @@ const ( CustomEventHarvestsPerMinute = 5 ) +// IsConnectedToNewRelic returns true if the connect reply is a valid connect reply +// from a New Relic connect endpoint. This is determined by the presence of a RunID +// and an EntityGUID which the agent needs to send data to a collector. +func (r *ConnectReply) IsConnectedToNewRelic() bool { + return r.RunID != "" && r.EntityGUID != "" +} + // MockConnectReplyEventLimits sets up a mock connect reply to test event limits // currently only verifies custom insights events func (r *ConnectReply) MockConnectReplyEventLimits(limits *RequestEventLimits) { diff --git a/v3/internal/connect_reply_test.go b/v3/internal/connect_reply_test.go index 0ef0deeed..87718c8c4 100644 --- a/v3/internal/connect_reply_test.go +++ b/v3/internal/connect_reply_test.go @@ -184,3 +184,17 @@ func TestDefaultEventHarvestConfigJSON(t *testing.T) { t.Errorf("DefaultEventHarvestConfig does not match expected valued:\nExpected:\t%s\nActual:\t\t%s", expect, string(js)) } } + +func TestConnectReply_IsConnectedToNewRelic(t *testing.T) { + reply := ConnectReplyDefaults() + if reply.IsConnectedToNewRelic() { + t.Error("Connect Reply Defaults should not be considered connected to New Relic") + } + + reply = ConnectReplyDefaults() + reply.RunID = "foo" + reply.EntityGUID = "bar" + if !reply.IsConnectedToNewRelic() { + t.Error("Connect Reply with RunID and EntityGUID should be considered connected to New Relic") + } +} diff --git a/v3/newrelic/secure_agent.go b/v3/newrelic/secure_agent.go index 86aa8390b..457430cb4 100644 --- a/v3/newrelic/secure_agent.go +++ b/v3/newrelic/secure_agent.go @@ -43,7 +43,7 @@ func (app *Application) RegisterSecurityAgent(s securityAgent) { if app != nil && app.app != nil && s != nil { secureAgent = s run, _ := app.app.getState() - if run != nil { + if run.Reply.IsConnectedToNewRelic() { secureAgent.RefreshState(getLinkedMetaData(app.app)) } } From c9b52de66297fc9b09e9be55dcafb0b49c07fefd Mon Sep 17 00:00:00 2001 From: Emilio Garcia Date: Thu, 24 Oct 2024 13:37:39 -0400 Subject: [PATCH 2/2] defensive nil check IsConnectedToNewRelic() --- v3/internal/connect_reply.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/v3/internal/connect_reply.go b/v3/internal/connect_reply.go index ba276e7cf..2e5484722 100644 --- a/v3/internal/connect_reply.go +++ b/v3/internal/connect_reply.go @@ -270,7 +270,7 @@ const ( // from a New Relic connect endpoint. This is determined by the presence of a RunID // and an EntityGUID which the agent needs to send data to a collector. func (r *ConnectReply) IsConnectedToNewRelic() bool { - return r.RunID != "" && r.EntityGUID != "" + return r != nil && r.RunID != "" && r.EntityGUID != "" } // MockConnectReplyEventLimits sets up a mock connect reply to test event limits