Skip to content

Commit b8458f1

Browse files
Merge pull request #984 from k2io/develop
New config for nrsecurityagent
2 parents 4e0598e + 45f11f2 commit b8458f1

File tree

3 files changed

+97
-4
lines changed

3 files changed

+97
-4
lines changed

v3/integrations/nrsecurityagent/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/newrelic/go-agent/v3/integrations/nrsecurityagent
33
go 1.21
44

55
require (
6-
github.com/newrelic/csec-go-agent v1.4.0
6+
github.com/newrelic/csec-go-agent v1.5.0
77
github.com/newrelic/go-agent/v3 v3.35.0
88
github.com/newrelic/go-agent/v3/integrations/nrsqlite3 v1.2.0
99
gopkg.in/yaml.v2 v2.4.0

v3/integrations/nrsecurityagent/nrsecurityagent.go

+95-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"io/ioutil"
99
"os"
1010
"strconv"
11+
"strings"
1112

1213
securityAgent "github.com/newrelic/csec-go-agent"
1314
"github.com/newrelic/go-agent/v3/internal"
@@ -31,6 +32,11 @@ func defaultSecurityConfig() SecurityConfig {
3132
cfg.Security.Agent.Enabled = true
3233
cfg.Security.Detection.Rxss.Enabled = true
3334
cfg.Security.Request.BodyLimit = 300
35+
cfg.Security.ExcludeFromIastScan.HttpRequestParameters.Header = make([]string, 0)
36+
cfg.Security.ExcludeFromIastScan.HttpRequestParameters.Body = make([]string, 0)
37+
cfg.Security.ExcludeFromIastScan.HttpRequestParameters.Query = make([]string, 0)
38+
cfg.Security.ExcludeFromIastScan.API = make([]string, 0)
39+
cfg.Security.ScanControllers.IastScanRequestRateLimit = 3600
3440
return cfg
3541
}
3642

@@ -63,7 +69,7 @@ func InitSecurityAgent(app *newrelic.Application, opts ...ConfigOption) error {
6369

6470
appConfig, isValid := app.Config()
6571
if !isValid {
66-
return fmt.Errorf("Newrelic application value cannot be read; did you call newrelic.NewApplication?")
72+
return fmt.Errorf("Newrelic application value cannot be read; did you call newrelic.NewApplication?")
6773
}
6874
app.UpdateSecurityConfig(c.Security)
6975
if !appConfig.HighSecurity && isSecurityAgentEnabled() {
@@ -109,8 +115,24 @@ func ConfigSecurityFromYaml() ConfigOption {
109115
// NEW_RELIC_SECURITY_VALIDATOR_SERVICE_URL provides URL for the security validator service
110116
// NEW_RELIC_SECURITY_MODE scanning mode: "IAST" for now
111117
// NEW_RELIC_SECURITY_AGENT_ENABLED (boolean)
112-
// NEW_RELIC_SECURITY_DETECTION_RXSS_ENABLED (boolean)
113118
// NEW_RELIC_SECURITY_REQUEST_BODY_LIMIT (integer) set limit on read request body in kb. By default, this is "300"
119+
//
120+
// NEW_RELIC_SECURITY_SCAN_SCHEDULE_DELAY (integer) The delay field indicated time in minutes before the IAST scan starts after the application starts. By default is 0 min.
121+
// NEW_RELIC_SECURITY_SCAN_SCHEDULE_DURATION (integer) The duration field specifies the duration of the IAST scan in minutes. This determines how long the scan will run. By default is forever.
122+
// NEW_RELIC_SECURITY_SCAN_SCHEDULE_SCHEDULE (string) The schedule field specifies a cron expression that defines when the IAST scan should run.
123+
// NEW_RELIC_SECURITY_SCAN_SCHEDULE_ALWAYS_SAMPLE_TRACES (boolean) always_sample_traces permits IAST to actively gather trace data in the background, and the collected data will be used by Security Agent to perform an IAST Scan at the scheduled time.
124+
// NEW_RELIC_SECURITY_SCAN_CONTROLLERS_IAST_SCAN_REQUEST_RATE_LIMIT (integer) The IAST Scan Rate Limit settings limit the maximum number of analysis probes or requests that can be sent to the application in a minute, By default is 3600.
125+
//
126+
// NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_INSECURE_SETTINGS (boolean)
127+
// NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_INVALID_FILE_ACCESS (boolean)
128+
// NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_SQL_INJECTION (boolean)
129+
// NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_NOSQL_INJECTION (boolean)
130+
// NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_LDAP_INJECTION (boolean)
131+
// NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_JAVASCRIPT_INJECTION (boolean)
132+
// NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_COMMAND_INJECTION (boolean)
133+
// NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_XPATH_INJECTION (boolean)
134+
// NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_SSRF (boolean)
135+
// NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_RXSS (boolean)
114136

115137
func ConfigSecurityFromEnvironment() ConfigOption {
116138
return func(cfg *SecurityConfig) {
@@ -145,6 +167,37 @@ func ConfigSecurityFromEnvironment() ConfigOption {
145167
assignBool(&cfg.Security.Agent.Enabled, "NEW_RELIC_SECURITY_AGENT_ENABLED")
146168
assignBool(&cfg.Security.Detection.Rxss.Enabled, "NEW_RELIC_SECURITY_DETECTION_RXSS_ENABLED")
147169
assignInt(&cfg.Security.Request.BodyLimit, "NEW_RELIC_SECURITY_REQUEST_BODY_LIMIT")
170+
171+
assignInt(&cfg.Security.ScanSchedule.Delay, "NEW_RELIC_SECURITY_SCAN_SCHEDULE_DELAY")
172+
assignInt(&cfg.Security.ScanSchedule.Duration, "NEW_RELIC_SECURITY_SCAN_SCHEDULE_DURATION")
173+
assignString(&cfg.Security.ScanSchedule.Schedule, "NEW_RELIC_SECURITY_SCAN_SCHEDULE_SCHEDULE")
174+
assignBool(&cfg.Security.ScanSchedule.AllowIastSampleCollection, "NEW_RELIC_SECURITY_SCAN_SCHEDULE_ALWAYS_SAMPLE_TRACES")
175+
assignInt(&cfg.Security.ScanControllers.IastScanRequestRateLimit, "NEW_RELIC_SECURITY_SCAN_CONTROLLERS_IAST_SCAN_REQUEST_RATE_LIMIT")
176+
177+
assignBool(&cfg.Security.ExcludeFromIastScan.IastDetectionCategory.InsecureSettings, "NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_INSECURE_SETTINGS")
178+
assignBool(&cfg.Security.ExcludeFromIastScan.IastDetectionCategory.InvalidFileAccess, "NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_INVALID_FILE_ACCESS")
179+
assignBool(&cfg.Security.ExcludeFromIastScan.IastDetectionCategory.SQLInjection, "NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_SQL_INJECTION")
180+
assignBool(&cfg.Security.ExcludeFromIastScan.IastDetectionCategory.NosqlInjection, "NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_NOSQL_INJECTION")
181+
assignBool(&cfg.Security.ExcludeFromIastScan.IastDetectionCategory.LdapInjection, "NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_LDAP_INJECTION")
182+
assignBool(&cfg.Security.ExcludeFromIastScan.IastDetectionCategory.JavascriptInjection, "NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_JAVASCRIPT_INJECTION")
183+
assignBool(&cfg.Security.ExcludeFromIastScan.IastDetectionCategory.CommandInjection, "NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_COMMAND_INJECTION")
184+
assignBool(&cfg.Security.ExcludeFromIastScan.IastDetectionCategory.XpathInjection, "NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_XPATH_INJECTION")
185+
assignBool(&cfg.Security.ExcludeFromIastScan.IastDetectionCategory.Ssrf, "NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_SSRF")
186+
assignBool(&cfg.Security.ExcludeFromIastScan.IastDetectionCategory.Rxss, "NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_IAST_DETECTION_CATEGORY_RXSS")
187+
188+
if env := os.Getenv("NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_API"); env != "" {
189+
cfg.Security.ExcludeFromIastScan.API = strings.Split(env, ",")
190+
}
191+
if env := os.Getenv("NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_HTTP_REQUEST_PARAMETERS_HEADER"); env != "" {
192+
cfg.Security.ExcludeFromIastScan.HttpRequestParameters.Header = strings.Split(env, ",")
193+
}
194+
if env := os.Getenv("NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_HTTP_REQUEST_PARAMETERS_QUERY"); env != "" {
195+
cfg.Security.ExcludeFromIastScan.HttpRequestParameters.Query = strings.Split(env, ",")
196+
}
197+
if env := os.Getenv("NEW_RELIC_SECURITY_EXCLUDE_FROM_IAST_SCAN_HTTP_REQUEST_PARAMETERS_BODY"); env != "" {
198+
cfg.Security.ExcludeFromIastScan.HttpRequestParameters.Body = strings.Split(env, ",")
199+
}
200+
148201
}
149202
}
150203

@@ -182,3 +235,43 @@ func ConfigSecurityRequestBodyLimit(bodyLimit int) ConfigOption {
182235
cfg.Security.Request.BodyLimit = bodyLimit
183236
}
184237
}
238+
239+
// ConfigScanScheduleDelay is used to set delay for scan schedule.
240+
// The delay field indicated time in minutes before the IAST scan starts after the application starts
241+
func ConfigScanScheduleDelay(delay int) ConfigOption {
242+
return func(cfg *SecurityConfig) {
243+
cfg.Security.ScanSchedule.Delay = delay
244+
}
245+
}
246+
247+
// ConfigScanScheduleDuration is used to set duration for scan schedule.
248+
// The duration field specifies the duration of the IAST scan in minutes. This determines how long the scan will run.
249+
func ConfigScanScheduleDuration(duration int) ConfigOption {
250+
return func(cfg *SecurityConfig) {
251+
cfg.Security.ScanSchedule.Duration = duration
252+
}
253+
}
254+
255+
// ConfigScanScheduleSetSchedule is used to set schedule for scan schedule.
256+
// The schedule field specifies a cron expression that defines when the IAST scan should run.
257+
func ConfigScanScheduleSetSchedule(schedule string) ConfigOption {
258+
return func(cfg *SecurityConfig) {
259+
cfg.Security.ScanSchedule.Schedule = schedule
260+
}
261+
}
262+
263+
// ConfigScanScheduleAllowIastSampleCollection is used to allow or disallow IAST sample collection
264+
// always_sample_traces permits IAST to actively gather trace data in the background, and the collected data will be used by Security Agent to perform an IAST Scan at the scheduled time.
265+
func ConfigScanScheduleAllowIastSampleCollection(isAllowed bool) ConfigOption {
266+
return func(cfg *SecurityConfig) {
267+
cfg.Security.ScanSchedule.AllowIastSampleCollection = isAllowed
268+
}
269+
}
270+
271+
// ConfigScanControllersIastScanRequestRateLimit is used to set IAST scan request rate limit.
272+
// The IAST Scan Rate Limit settings limit the maximum number of analysis probes or requests that can be sent to the application in a minute
273+
func ConfigIastScanRequestRateLimit(limit int) ConfigOption {
274+
return func(cfg *SecurityConfig) {
275+
cfg.Security.ScanControllers.IastScanRequestRateLimit = limit
276+
}
277+
}

v3/newrelic/transaction.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ func (txn *Transaction) SetWebRequest(r WebRequest) {
269269
return
270270
}
271271
if IsSecurityAgentPresent() {
272-
secureAgent.SendEvent("INBOUND", r, txn.GetCsecAttributes())
272+
secureAgent.SendEvent("INBOUND", r, txn.GetCsecAttributes(), txn.GetLinkingMetadata().TraceID)
273273
}
274274
txn.thread.logAPIError(txn.thread.SetWebRequest(r), "set web request", nil)
275275
}

0 commit comments

Comments
 (0)