-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathglobal_config.go
657 lines (551 loc) · 15.5 KB
/
global_config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
// Copyright 2023 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: New Relic Software License v1.0
package security_config
import (
"os"
"strconv"
"sync"
"time"
logging "github.com/newrelic/csec-go-agent/internal/security_logs"
secUtils "github.com/newrelic/csec-go-agent/internal/security_utils"
)
var GlobalInfo *Info_struct = new(Info_struct)
var Secure secUtils.Secureiface
var SecureWS secUtils.SecureWSiface
type Info_struct struct {
ApiData *sync.Map
EnvironmentInfo EnvironmentInfo
ApplicationInfo runningApplicationInfo
InstrumentationData Instrumentation
securityMutex sync.Mutex
security Security
ploicyMutex sync.Mutex
currentPolicy Policy
defaultPolicy Policy
isForceDisable bool
MetaData metaData
WebSocketConnectionStats WebSocketConnectionStats
IastReplayRequest IastReplayRequest
EventStats EventStats
DroppedEvent DroppedEvent
dealyAgentTill time.Time
}
func (info *Info_struct) GetCurrentPolicy() Policy {
info.ploicyMutex.Lock()
defer info.ploicyMutex.Unlock()
return info.currentPolicy
}
func (info *Info_struct) SetCurrentPolicy(policy Policy) {
info.ploicyMutex.Lock()
defer info.ploicyMutex.Unlock()
info.currentPolicy = policy
}
func (info *Info_struct) GetdefaultPolicy() Policy {
info.ploicyMutex.Lock()
defer info.ploicyMutex.Unlock()
return info.defaultPolicy
}
func (info *Info_struct) SetdefaultPolicy(policy Policy) {
info.ploicyMutex.Lock()
defer info.ploicyMutex.Unlock()
info.defaultPolicy = policy
}
func (info *Info_struct) IsForceDisable() bool {
info.ploicyMutex.Lock()
defer info.ploicyMutex.Unlock()
return info.isForceDisable
}
func (info *Info_struct) SetForceDisable(b bool) {
info.ploicyMutex.Lock()
defer info.ploicyMutex.Unlock()
info.isForceDisable = b
}
func (info *Info_struct) IsIASTEnable() bool {
info.ploicyMutex.Lock()
defer info.ploicyMutex.Unlock()
return info.currentPolicy.VulnerabilityScan.Enabled && GlobalInfo.currentPolicy.VulnerabilityScan.IastScan.Enabled
}
// for security config
func (info *Info_struct) IsSecurityEnabled() bool {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
return info.security.Enabled
}
func (info *Info_struct) SetSecurityEnabled(isenabled bool) {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
info.security.Enabled = isenabled
}
func (info *Info_struct) SetSecurityAgentEnabled(isenabled bool) {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
info.security.Agent.Enabled = isenabled
}
func (info *Info_struct) SetSecurity(security Security) {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
info.security = security
}
func (info *Info_struct) IsInsecureSettingsDisabled() bool {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
return info.security.ExcludeFromIastScan.IastDetectionCategory.InsecureSettings
}
func (info *Info_struct) IsInvalidFileAccessDisabled() bool {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
return info.security.ExcludeFromIastScan.IastDetectionCategory.InvalidFileAccess
}
func (info *Info_struct) IsSQLInjectionDisabled() bool {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
return info.security.ExcludeFromIastScan.IastDetectionCategory.SQLInjection
}
func (info *Info_struct) IsNosqlInjectionDisabled() bool {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
return info.security.ExcludeFromIastScan.IastDetectionCategory.NosqlInjection
}
func (info *Info_struct) IsLdapInjectionDisabled() bool {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
return info.security.ExcludeFromIastScan.IastDetectionCategory.LdapInjection
}
func (info *Info_struct) IsJavascriptInjectionDisabled() bool {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
return info.security.ExcludeFromIastScan.IastDetectionCategory.JavascriptInjection
}
func (info *Info_struct) IsCommandInjectionDisabled() bool {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
return info.security.ExcludeFromIastScan.IastDetectionCategory.CommandInjection
}
func (info *Info_struct) IsXpathInjectionDisabled() bool {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
return info.security.ExcludeFromIastScan.IastDetectionCategory.XpathInjection
}
func (info *Info_struct) IsSsrfDisabled() bool {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
return info.security.ExcludeFromIastScan.IastDetectionCategory.Ssrf
}
func (info *Info_struct) IsRxssDisabled() bool {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
return info.security.ExcludeFromIastScan.IastDetectionCategory.Rxss
}
func (info *Info_struct) SkipIastScanParameters() interface{} {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
return info.security.ExcludeFromIastScan.HttpRequestParameters
}
func (info *Info_struct) SkipIastScanApi() []string {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
return info.security.ExcludeFromIastScan.API
}
func (info *Info_struct) ScanScheduleDuration() int {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
return info.security.ScanSchedule.Duration
}
func (info *Info_struct) ScanScheduleDelay() int {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
return info.security.ScanSchedule.Delay
}
func (info *Info_struct) ScanScheduleAllowIastSampleCollection() bool {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
return info.security.ScanSchedule.AllowIastSampleCollection
}
func (info *Info_struct) ScanScheduleSchedule() string {
info.securityMutex.Lock()
defer info.securityMutex.Unlock()
return info.security.ScanSchedule.Schedule
}
func (info *Info_struct) SecurityHomePath() string {
return info.security.SecurityHomePath
}
func (info *Info_struct) SetSecurityHomePath(path string) {
info.security.SecurityHomePath = path
}
func (info *Info_struct) ValidatorServiceUrl() string {
if info.security.Validator_service_url != "" {
return info.security.Validator_service_url
}
return ValidatorDefaultEndpoint
}
func (info *Info_struct) SetValidatorServiceUrl(path string) {
info.security.Validator_service_url = path
}
func (info *Info_struct) SecurityMode() string {
return info.security.Mode
}
func (info *Info_struct) IsIastMode() bool {
return secUtils.CaseInsensitiveEquals(info.security.Mode, "IAST")
}
func (info *Info_struct) BodyLimit() int {
return info.security.Request.BodyLimit * 1000
}
func (info *Info_struct) SetBodyLimit(bodyLimit int) {
info.security.Request.BodyLimit = bodyLimit
}
func (info *Info_struct) ScanControllersIastLoadInterval() int {
return info.security.ScanControllers.IastScanRequestRateLimit
}
func (info *Info_struct) SetscanControllersIastLoadInterval(iastLoadInterval int) {
info.security.ScanControllers.IastScanRequestRateLimit = iastLoadInterval
}
func (info *Info_struct) ScanInstanceCount() int {
return info.security.ScanControllers.ScanInstanceCount
}
func (info *Info_struct) SetScanInstanceCount(scanInstanceCount int) {
info.security.ScanControllers.ScanInstanceCount = scanInstanceCount
}
func (info *Info_struct) GetIastTestIdentifier() string {
return info.security.IastTestIdentifier
}
func (info *Info_struct) GetApiData() []any {
urlmappings := []any{}
if info.ApiData != nil {
info.ApiData.Range(func(key, value interface{}) bool {
urlmappings = append(urlmappings, value)
return true
})
}
return urlmappings
}
func (info *Info_struct) SetApiData(data Urlmappings) {
key := data.Path + data.Method
if info.ApiData == nil {
info.ApiData = &sync.Map{}
}
if _, ok := info.ApiData.Load(key); !ok {
info.ApiData.Store(key, data)
return
}
}
func (info *Info_struct) IastProbingInterval() int {
info.ploicyMutex.Lock()
defer info.ploicyMutex.Unlock()
if GlobalInfo.currentPolicy.VulnerabilityScan.IastScan.Probing.Interval <= 0 {
return 5
} else {
return GlobalInfo.currentPolicy.VulnerabilityScan.IastScan.Probing.Interval
}
}
type metaData struct {
linkingMetadata map[string]string
accountID string
agentRunId string
entityGuid string
entityName string
sync.Mutex
}
func (m *metaData) GetEntityName() string {
m.Lock()
defer m.Unlock()
return m.entityName
}
func (m *metaData) SetEntityName(value string) {
m.Lock()
defer m.Unlock()
m.entityName = value
}
func (m *metaData) GetEntityGuid() string {
m.Lock()
defer m.Unlock()
return m.entityGuid
}
func (m *metaData) SetEntityGuid(value string) {
m.Lock()
defer m.Unlock()
m.entityGuid = value
}
func (m *metaData) GetAccountID() string {
m.Lock()
defer m.Unlock()
return m.accountID
}
func (m *metaData) SetAccountID(value string) {
m.Lock()
defer m.Unlock()
m.accountID = value
}
func (m *metaData) GetAgentRunId() string {
m.Lock()
defer m.Unlock()
return m.agentRunId
}
func (m *metaData) SetAgentRunId(value string) {
m.Lock()
defer m.Unlock()
m.agentRunId = value
}
func (m *metaData) GetLinkingMetadata() map[string]string {
m.Lock()
defer m.Unlock()
return m.linkingMetadata
}
func (m *metaData) SetLinkingMetadata(value map[string]string) {
m.Lock()
defer m.Unlock()
m.linkingMetadata = value
}
// EventData used to track number of request
type eventData struct {
iastEventStats EventStats
raspEventStats EventStats
exitEventStats EventStats
sync.Mutex
}
func (e *eventData) GetIastEventStats() *EventStats {
if e == nil {
return &EventStats{}
}
e.Lock()
defer e.Unlock()
return &e.iastEventStats
}
func (e *eventData) GetRaspEventStats() *EventStats {
if e == nil {
return &EventStats{}
}
e.Lock()
defer e.Unlock()
return &e.raspEventStats
}
func (e *eventData) GetExitEventStats() *EventStats {
if e == nil {
return &EventStats{}
}
e.Lock()
defer e.Unlock()
return &e.exitEventStats
}
func (e *eventData) ResetEventStats() {
if e == nil {
return
}
e.Lock()
defer e.Unlock()
e.iastEventStats = EventStats{}
e.raspEventStats = EventStats{}
e.exitEventStats = EventStats{}
}
type Urlmappings struct {
Method string `json:"method"`
Path string `json:"path"`
Handler string `json:"handler"`
}
type EnvironmentInfo struct {
ID string
NodeId string
NodeIp string
NodeName string
CollectorIp string
NodeGroupTags []string
RunningEnv string
Namespaces string
ContainerId string
PodId string
Wd string
Gopath string
Goarch string
Goos string
Goroot string
UserAppVersion string
UserAppTags string
EcsTaskId string
ImageId string
Image string
ContainerName string
EcsTaskDefinition string
}
type runningApplicationInfo struct {
sync.Mutex
appName string
apiAccessorToken string
protectedServer string
appUUID string
sha256 string
size string
contextPath string
pid string
Cmd string
cmdline []string
ports []int
ServerIp string
starttimestr time.Time
trafficStartedTime time.Time
scanStartTime time.Time
binaryPath string
serverName []string
}
func (r *runningApplicationInfo) GetAppName() string {
return r.appName
}
func (r *runningApplicationInfo) SetAppName(value string) {
r.appName = value
}
func (r *runningApplicationInfo) GetApiAccessorToken() string {
return r.apiAccessorToken
}
func (r *runningApplicationInfo) SetApiAccessorToken(value string) {
r.apiAccessorToken = value
}
func (r *runningApplicationInfo) GetProtectedServer() string {
return r.protectedServer
}
func (r *runningApplicationInfo) SetProtectedServer(value string) {
r.protectedServer = value
}
func (r *runningApplicationInfo) GetAppUUID() string {
return r.appUUID
}
func (r *runningApplicationInfo) SetAppUUID(value string) {
r.appUUID = value
}
func (r *runningApplicationInfo) GetSha256() string {
return r.sha256
}
func (r *runningApplicationInfo) SetSha256(value string) {
r.sha256 = value
}
func (r *runningApplicationInfo) GetSize() string {
var out string
if r == nil {
return out
}
return r.size
}
func (r *runningApplicationInfo) SetSize(value string) {
r.size = value
}
func (r *runningApplicationInfo) GetContextPath() string {
return r.contextPath
}
func (r *runningApplicationInfo) SetContextPath(value string) {
r.contextPath = value
}
func (r *runningApplicationInfo) GetPid() string {
return r.pid
}
func (r *runningApplicationInfo) SetPid(value string) {
r.pid = value
}
func (r *runningApplicationInfo) GetCmd() string {
return r.Cmd
}
func (r *runningApplicationInfo) SetCmd(value string) {
r.Cmd = value
}
func (r *runningApplicationInfo) GetCmdline() []string {
return r.cmdline
}
func (r *runningApplicationInfo) SetCmdline(value []string) {
r.cmdline = value
}
func (r *runningApplicationInfo) GetPorts() []int {
return r.ports
}
func (r *runningApplicationInfo) SetPorts(value int) {
r.ports = append(r.ports, value)
}
func (r *runningApplicationInfo) GetServerIp() string {
return r.ServerIp
}
func (r *runningApplicationInfo) SetServerIp(value string) {
r.ServerIp = value
}
func (r *runningApplicationInfo) GetStarttimestr() time.Time {
return r.starttimestr
}
func (r *runningApplicationInfo) SetStarttimestr(value time.Time) {
r.starttimestr = value
}
func (r *runningApplicationInfo) GetBinaryPath() string {
return r.binaryPath
}
func (r *runningApplicationInfo) SetBinaryPath(value string) {
r.binaryPath = value
}
func (r *runningApplicationInfo) GetServerName() []string {
r.Lock()
defer r.Unlock()
return r.serverName
}
func (r *runningApplicationInfo) SetServerName(value string) {
r.Lock()
defer r.Unlock()
r.serverName = append(r.serverName, value)
}
func (r *runningApplicationInfo) GetTrafficStartedTime() int64 {
if r.trafficStartedTime.IsZero() {
return 0
} else {
return r.trafficStartedTime.Unix() * 1000
}
}
func (r *runningApplicationInfo) SetTrafficStartedTime(value time.Time) {
r.trafficStartedTime = value
}
func (r *runningApplicationInfo) GetScanStartTime() int64 {
if r.trafficStartedTime.IsZero() {
return 0
} else {
return r.scanStartTime.Unix() * 1000
}
}
func (r *runningApplicationInfo) SetScanStartTime(value time.Time) {
r.scanStartTime = value
}
type Instrumentation struct {
HookCalledCount uint64
Hooked bool
TraceHooksApplied traceHooksApplied
}
type traceHooksApplied struct {
Sql bool
Mongo bool
}
func InitDefaultConfig() {
//init default info
GlobalInfo.EventStats = EventStats{}
GlobalInfo.InstrumentationData = Instrumentation{}
GlobalInfo.EnvironmentInfo = EnvironmentInfo{}
GlobalInfo.ApplicationInfo = runningApplicationInfo{}
GlobalInfo.MetaData = metaData{}
GlobalInfo.MetaData.linkingMetadata = map[string]string{}
GlobalInfo.WebSocketConnectionStats = WebSocketConnectionStats{}
}
func UpdateGlobalConf(policy Policy, arg string) Policy {
logging.EndStage("7", "Received and applied policy/configuration")
logging.PrintInitlog(arg)
GlobalInfo.SetdefaultPolicy(policy)
if !GlobalInfo.GetCurrentPolicy().Enforce {
GlobalInfo.SetCurrentPolicy(policy)
return policy
} else {
return GlobalInfo.GetCurrentPolicy()
}
}
func InstantiateDefaultPolicy() {
GlobalInfo.SetCurrentPolicy(GlobalInfo.GetdefaultPolicy())
}
const forceDisable = "NEW_RELIC_SECURITY_AGENT_ENABLED"
func isSecurityAgentEnabled() bool {
if env := os.Getenv(forceDisable); env != "" {
if b, err := strconv.ParseBool(env); nil == err {
return b
}
}
return true
}
func init() {
InitDefaultConfig()
GlobalInfo.SetForceDisable(!isSecurityAgentEnabled())
}