@newrelic/security-agent 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/CHANGELOG.md +0 -0
  2. package/CONTRIBUTING.md +23 -0
  3. package/LICENSE +43 -0
  4. package/README.md +98 -0
  5. package/THIRD_PARTY_NOTICES.md +2909 -0
  6. package/index.js +24 -0
  7. package/lib/instrumentation-security/core/constants.js +66 -0
  8. package/lib/instrumentation-security/core/event-constants.js +49 -0
  9. package/lib/instrumentation-security/core/request-manager.js +73 -0
  10. package/lib/instrumentation-security/core/route-manager.js +30 -0
  11. package/lib/instrumentation-security/core/sec-trace.js +134 -0
  12. package/lib/instrumentation-security/core/sec-utils.js +212 -0
  13. package/lib/instrumentation-security/core/security-metadata.js +34 -0
  14. package/lib/instrumentation-security/core/stack-trace.js +156 -0
  15. package/lib/instrumentation-security/hooks/director/nr-director.js +98 -0
  16. package/lib/instrumentation-security/hooks/express/nr-express.js +106 -0
  17. package/lib/instrumentation-security/hooks/fastify/nr-fastify.js +155 -0
  18. package/lib/instrumentation-security/hooks/fs/nr-fs.js +391 -0
  19. package/lib/instrumentation-security/hooks/hapi/nr-hapi.js +131 -0
  20. package/lib/instrumentation-security/hooks/http/nr-http.js +461 -0
  21. package/lib/instrumentation-security/hooks/koa/@koa/nr-@koa-router.js +110 -0
  22. package/lib/instrumentation-security/hooks/ldap/nr-ldap.js +81 -0
  23. package/lib/instrumentation-security/hooks/mongodb/common.js +239 -0
  24. package/lib/instrumentation-security/hooks/mongodb/constants.js +91 -0
  25. package/lib/instrumentation-security/hooks/mongodb/nr-mongodb.js +49 -0
  26. package/lib/instrumentation-security/hooks/mongodb/v2-mongo.js +91 -0
  27. package/lib/instrumentation-security/hooks/mongodb/v3-mongo.js +36 -0
  28. package/lib/instrumentation-security/hooks/mongodb/v4-mongo.js +17 -0
  29. package/lib/instrumentation-security/hooks/mysql/nr-mysql.js +232 -0
  30. package/lib/instrumentation-security/hooks/native/nr-childProcess.js +131 -0
  31. package/lib/instrumentation-security/hooks/postgres/nr-postgres.js +131 -0
  32. package/lib/instrumentation-security/hooks/require/nr-require.js +93 -0
  33. package/lib/instrumentation-security/hooks/restify/nr-restify.js +104 -0
  34. package/lib/instrumentation-security/hooks/xpath/nr-xpath.js +91 -0
  35. package/lib/instrumentation-security/index.js +171 -0
  36. package/lib/nr-security-agent/index.js +170 -0
  37. package/lib/nr-security-agent/lib/core/Auth-headers.js +58 -0
  38. package/lib/nr-security-agent/lib/core/ContainerProperties.js +42 -0
  39. package/lib/nr-security-agent/lib/core/ECSProperties.js +34 -0
  40. package/lib/nr-security-agent/lib/core/FuzzFailEvent.js +27 -0
  41. package/lib/nr-security-agent/lib/core/LinkingMetadata.js +21 -0
  42. package/lib/nr-security-agent/lib/core/LogMessage.js +47 -0
  43. package/lib/nr-security-agent/lib/core/PodProperties.js +36 -0
  44. package/lib/nr-security-agent/lib/core/Policy.js +105 -0
  45. package/lib/nr-security-agent/lib/core/agent-status.js +56 -0
  46. package/lib/nr-security-agent/lib/core/agent.js +146 -0
  47. package/lib/nr-security-agent/lib/core/applicationinfo.js +634 -0
  48. package/lib/nr-security-agent/lib/core/commonUtils.js +411 -0
  49. package/lib/nr-security-agent/lib/core/connections/aws-lambda-client.js +55 -0
  50. package/lib/nr-security-agent/lib/core/connections/restclient.js +20 -0
  51. package/lib/nr-security-agent/lib/core/connections/websocket/index.js +25 -0
  52. package/lib/nr-security-agent/lib/core/connections/websocket/response/collecotor-policy-error-handler.js +18 -0
  53. package/lib/nr-security-agent/lib/core/connections/websocket/response/collector-policy-handler.js +20 -0
  54. package/lib/nr-security-agent/lib/core/connections/websocket/response/fuzz-request-handler.js +171 -0
  55. package/lib/nr-security-agent/lib/core/connections/websocket/response/index.js +47 -0
  56. package/lib/nr-security-agent/lib/core/connections/websocket/response/no-op-handler.js +12 -0
  57. package/lib/nr-security-agent/lib/core/connections/websocket/response/startup-handshake-handler.js +27 -0
  58. package/lib/nr-security-agent/lib/core/connections/websocket/response/unsupported-agent-handler.js +18 -0
  59. package/lib/nr-security-agent/lib/core/connections/websocket/response/ws-reconnect.js +14 -0
  60. package/lib/nr-security-agent/lib/core/connections/websocket/websocket.js +443 -0
  61. package/lib/nr-security-agent/lib/core/event.js +186 -0
  62. package/lib/nr-security-agent/lib/core/health-check.js +159 -0
  63. package/lib/nr-security-agent/lib/core/json-file-handler.js +66 -0
  64. package/lib/nr-security-agent/lib/core/jsonValidator.js +149 -0
  65. package/lib/nr-security-agent/lib/core/lambda-delegated-task-service.js +90 -0
  66. package/lib/nr-security-agent/lib/core/logging.js +176 -0
  67. package/lib/nr-security-agent/lib/core/sec-agent-constants.js +173 -0
  68. package/lib/nr-security-agent/lib/core/sec-util.js +166 -0
  69. package/lib/nr-security-agent/lib/core/sha-size-util.js +338 -0
  70. package/lib/nr-security-agent/lib/core/sha_worker.js +24 -0
  71. package/lib/nr-security-agent/lib/core/startup-config.js +29 -0
  72. package/lib/nr-security-agent/lib/core/statusUtils.js +157 -0
  73. package/lib/nr-security-agent/lib/core/timeunit.js +44 -0
  74. package/lib/nr-security-agent/lib/core/websocket-client.js +335 -0
  75. package/lib/nr-security-agent/lib/core/worker-entities.js +82 -0
  76. package/lib/nr-security-agent/lib/core/worker.js +148 -0
  77. package/lib/nr-security-agent/lib/core/xss-utils.js +297 -0
  78. package/lib/nr-security-agent/lib/models/lambda-delegated-task/constants.js +12 -0
  79. package/lib/nr-security-agent/lib/models/lambda-delegated-task/index.js +15 -0
  80. package/lib/nr-security-agent/lib/models/lambda-delegated-task/lambda-delegated-sha-calculation.js +37 -0
  81. package/lib/nr-security-agent/lib/models/lambda-delegated-task/lambda-delegated-task-base.js +50 -0
  82. package/lib/nr-security-agent/lib/models/lambda-delegated-task/lambda-delegated-task-type.js +17 -0
  83. package/lib/nr-security-agent/resources/cert.pem +31 -0
  84. package/lib/nr-security-agent/resources/config.js +19 -0
  85. package/lib/nr-security-agent/resources/default-policy.yaml +36 -0
  86. package/lib/nr-security-api/index.js +111 -0
  87. package/package.json +95 -0
@@ -0,0 +1,159 @@
1
+ /*
2
+ * Copyright 2023 New Relic Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ 'use strict';
7
+
8
+ const { Agent } = require('./agent');
9
+ const { JSON_NAME } = require('./sec-agent-constants');
10
+ const { BasicInfo } = require('./event');
11
+
12
+ const NODE_JS_LITERAL = 'Node.js:';
13
+
14
+ let NODE_SERVER;
15
+ let instance;
16
+ let logger;
17
+ const API = require('../../../nr-security-api');
18
+ const NRAgent = API.getNRAgent();
19
+
20
+ /**
21
+ * Retruns an instance of HC.
22
+ * Creates one if not already created.
23
+ *
24
+ * @returns {HC} instance
25
+ */
26
+ module.exports.getInstance = function () {
27
+ const agent = Agent.getAgent();
28
+ if (!instance) {
29
+ if (agent && !NODE_SERVER) {
30
+ NODE_SERVER = NODE_JS_LITERAL + agent.applicationInfo.binaryVersion;
31
+ }
32
+ instance = new HC(agent.applicationInfo);
33
+ }
34
+ return instance;
35
+ };
36
+
37
+ /**
38
+ * Creates a new HealthCheck object.
39
+ *
40
+ * @param {ApplicationInfo} info
41
+ */
42
+ function HC (info) {
43
+ BasicInfo.call(this);
44
+
45
+ this.applicationUUID = info.applicationUUID;
46
+ this.eventType = 'sec_health_check_lc';
47
+ if (NRAgent && NRAgent.config) {
48
+ this.entityGuid = NRAgent.config.entity_guid;
49
+ }
50
+ this.protectedServer = NODE_SERVER;
51
+ this.eventDropCount = 0;
52
+ this.eventProcessed = 0;
53
+ this.eventSentCount = 0;
54
+ this.httpRequestCount = 0;
55
+ this.isHost = info.identifier.isHost;
56
+ this.collectorVersion = info.collectorVersion;
57
+ this.jsonVersion = info.jsonVersion;
58
+ this.buildNumber = info.buildNumber;
59
+ this.jsonName = JSON_NAME.HC;
60
+ this.pid = info.pid;
61
+
62
+
63
+
64
+ /**
65
+ * registers an event drop.
66
+ */
67
+ this.registerEventDrop = function () {
68
+ this.eventDropCount++;
69
+ };
70
+
71
+ /**
72
+ * mark application to be running on host.
73
+ */
74
+ this.markHost = function () {
75
+ this.isHost = true;
76
+ };
77
+
78
+ /**
79
+ * resets the drop count.
80
+ */
81
+ this.resetDropCount = function () {
82
+ this.eventDropCount = 0;
83
+ };
84
+
85
+ /**
86
+ * registers a processed event.
87
+ */
88
+ this.registerEventProcessed = function () {
89
+ this.eventProcessed++;
90
+ };
91
+
92
+ /**
93
+ * registers a processed event.
94
+ */
95
+ this.registerEventSent = function () {
96
+ this.eventSentCount++;
97
+ };
98
+
99
+ /**
100
+ * registers n processed event.
101
+ */
102
+ this.registerEventsSent = function (n) {
103
+ this.eventSentCount += n;
104
+ };
105
+ /**
106
+ * resets the processed count.
107
+ */
108
+ this.resetProcessedCount = function () {
109
+ this.eventProcessed = 0;
110
+ };
111
+
112
+ /**
113
+ * resets the Event sent count.
114
+ */
115
+ this.resetEventSentCount = function () {
116
+ this.eventSentCount = 0;
117
+ };
118
+
119
+ /**
120
+ * registers a http request count
121
+ */
122
+ this.registerHttpRequestCount = function () {
123
+ this.httpRequestCount++;
124
+ };
125
+
126
+ /**
127
+ * resets the Event sent count.
128
+ */
129
+ this.resetHttpRequestCount = function () {
130
+ this.httpRequestCount = 0;
131
+ };
132
+
133
+
134
+ /**
135
+ * sets gerate hc timestamp.
136
+ */
137
+ this.registerGenerateTime = function () {
138
+ this.timestamp = Date.now();
139
+ };
140
+
141
+ this.get = function () {
142
+ this.registerGenerateTime();
143
+ return JSON.stringify(this);
144
+ };
145
+ }
146
+
147
+ HC.prototype = Object.create(BasicInfo.prototype);
148
+ HC.prototype.constructor = HC;
149
+
150
+
151
+ /**
152
+ * Sets the logger instance.
153
+ *
154
+ * @param {*} loggerInstance
155
+ */
156
+ module.exports.setLogger = loggerInstance => {
157
+ logger = loggerInstance;
158
+ };
159
+
@@ -0,0 +1,66 @@
1
+ /*
2
+ * Copyright 2023 New Relic Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ const fs = require('fs');
7
+ const path = require('path');
8
+
9
+ /**
10
+ * Entity which manages the json file.
11
+ *
12
+ * @param {String} file
13
+ * @param {String} base Takes current directory if not specified.
14
+ */
15
+ function JsonHandler(file, base = ".") {
16
+ if (this.prototype.constructor !== JsonHandler) {
17
+ return new JsonHandler(file, base);
18
+ }
19
+ this.fileName = base.concat(path.sep).concat(file);
20
+ path.normalize(this.fileName);
21
+ try {
22
+ const readJSON = fs.readFileSync(this.fileName, { encoding: 'utf8' });
23
+ this.json = JSON.parse(readJSON);
24
+
25
+ } catch (err) {
26
+ throw err
27
+ }
28
+
29
+ }
30
+ JsonHandler.prototype = Object.create(JsonHandler);
31
+ JsonHandler.prototype.constructor = JsonHandler;
32
+
33
+ /**
34
+ * get JSON Object
35
+ */
36
+ JsonHandler.prototype.getJSON = function () {
37
+ return this.json;
38
+ }
39
+
40
+
41
+ /**
42
+ * Update Source file synchronously
43
+ */
44
+ JsonHandler.prototype.updateFileSync = function () {
45
+ try {
46
+ fs.writeFileSync(this.fileName, JSON.stringify(this.json, null, 2), { encoding: 'utf8' })
47
+ } catch (err) {
48
+ throw err
49
+ }
50
+ }
51
+ /**
52
+ * Update Source file asynchronously
53
+ */
54
+ JsonHandler.prototype.updateFileAsync = function () {
55
+ fs.writeFile(this.fileName, JSON.stringify(this.json, null, 2), { encoding: 'utf8' }, function (err) {
56
+ if (err) {
57
+ throw err
58
+ }
59
+ })
60
+
61
+ }
62
+
63
+
64
+ module.exports = {
65
+ JsonHandler
66
+ }
@@ -0,0 +1,149 @@
1
+ /*
2
+ * Copyright 2023 New Relic Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ const Validator = require('jsonschema').Validator;
7
+ const v = new Validator();
8
+ const schema = {
9
+ title: 'LCPolicy',
10
+ description: 'A policy for language collectors',
11
+ type: 'object',
12
+ properties: {
13
+ version: {
14
+ type: 'string',
15
+ minLength: 1
16
+ },
17
+ policyPull: {
18
+ type: 'boolean'
19
+ },
20
+ policyPullInterval: {
21
+ type: 'integer',
22
+ minimum: 0,
23
+ maximum: 2678400
24
+ },
25
+ vulnerabilityScan: {
26
+ type: 'object',
27
+ properties: {
28
+ enabled: {
29
+ type: 'boolean'
30
+ },
31
+ iastScan: {
32
+ type: 'object',
33
+ properties: {
34
+ enabled: {
35
+ type: 'boolean'
36
+ },
37
+ probing: {
38
+ type: 'object',
39
+ properties: {
40
+ interval: {
41
+ type: 'integer',
42
+ minimum: 1,
43
+ maximum: 60
44
+ },
45
+ batchSize: {
46
+ type: 'integer',
47
+ minimum: 1,
48
+ maximum: 300
49
+ }
50
+ },
51
+ required: [
52
+ 'interval',
53
+ 'batchSize'
54
+ ]
55
+ }
56
+ },
57
+ required: [
58
+ 'enabled',
59
+ 'probing'
60
+ ]
61
+ }
62
+ },
63
+ required: [
64
+ 'enabled',
65
+ 'iastScan'
66
+ ]
67
+ },
68
+ protectionMode: {
69
+ type: 'object',
70
+ properties: {
71
+ enabled: {
72
+ type: 'boolean'
73
+ },
74
+ ipBlocking: {
75
+ type: 'object',
76
+ properties: {
77
+ enabled: {
78
+ type: 'boolean'
79
+ },
80
+ attackerIpBlocking: {
81
+ type: 'boolean'
82
+ },
83
+ ipDetectViaXFF: {
84
+ type: 'boolean'
85
+ }
86
+ },
87
+ required: [
88
+ 'enabled',
89
+ 'attackerIpBlocking',
90
+ 'ipDetectViaXFF'
91
+ ]
92
+ },
93
+ apiBlocking: {
94
+ type: 'object',
95
+ properties: {
96
+ enabled: {
97
+ type: 'boolean'
98
+ },
99
+ protectAllApis: {
100
+ type: 'boolean'
101
+ },
102
+ protectKnownVulnerableApis: {
103
+ type: 'boolean'
104
+ },
105
+ protectAttackedApis: {
106
+ type: 'boolean'
107
+ }
108
+ },
109
+ required: [
110
+ 'enabled',
111
+ 'protectAllApis',
112
+ 'protectKnownVulnerableApis',
113
+ 'protectAttackedApis'
114
+ ]
115
+ }
116
+ },
117
+ required: [
118
+ 'enabled',
119
+ 'ipBlocking',
120
+ 'apiBlocking'
121
+ ]
122
+ },
123
+ sendCompleteStackTrace: {
124
+ type: 'boolean'
125
+ },
126
+ enableHTTPRequestPrinting: {
127
+ type: 'boolean'
128
+ }
129
+ },
130
+ required: [
131
+ 'version',
132
+ 'policyPull',
133
+ 'policyPullInterval',
134
+ 'vulnerabilityScan',
135
+ 'protectionMode',
136
+ 'sendCompleteStackTrace',
137
+ 'enableHTTPRequestPrinting'
138
+ ]
139
+ };
140
+
141
+ function validate (data) {
142
+ const result = v.validate(data, schema);
143
+ return result;
144
+ }
145
+
146
+
147
+ module.exports = {
148
+ validate
149
+ };
@@ -0,0 +1,90 @@
1
+ /*
2
+ * Copyright 2023 New Relic Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ const { Agent } = require('./agent');
7
+ const { AWS_LAMBDA_FUNCTION_VERSION_ENV_IDENTIFIER } = require('./sec-agent-constants');
8
+ const { invokeLambda } = require('./connections/aws-lambda-client');
9
+ const {
10
+ LambdaDelegatedTaskType,
11
+ LambdaDelegatedTaskBase,
12
+ LambdaDelegatedTaskShaCalculation,
13
+ CONST
14
+ } = require('../models/lambda-delegated-task');
15
+
16
+ const CSEC_TASK_PAYLOAD_IDENTIFIER = 'CSEC_TASK_PAYLOAD';
17
+
18
+ let instance;
19
+ function LambdaDelegatedTaskService () {}
20
+
21
+ LambdaDelegatedTaskService.prototype = Object.create(null);
22
+ LambdaDelegatedTaskService.prototype.constructor = LambdaDelegatedTaskService;
23
+
24
+ /**
25
+ * generates a lambda delegated task object
26
+ * @param {LambdaDelegatedTaskType} type
27
+ * @param {...any} args
28
+ * @returns {LambdaDelegatedTaskBase} obj
29
+ */
30
+ LambdaDelegatedTaskService.prototype.generateHandler = function generateHandler (type, ...args) {
31
+ switch (type) {
32
+ case LambdaDelegatedTaskType.SHA_CALCULATION:
33
+ return new LambdaDelegatedTaskShaCalculation(type, ...args);
34
+ default:
35
+ return new LambdaDelegatedTaskBase(type, ...args);
36
+ }
37
+ };
38
+
39
+ /**
40
+ * Detectects if the lambda event contains a delegated task
41
+ * payload.
42
+ * @param {JSON} event
43
+ * @returns {LambdaDelegatedTaskBase} obj
44
+ */
45
+ LambdaDelegatedTaskService.prototype.detectInEvent = function detectInEvent (event) {
46
+ if (event && event[CSEC_TASK_PAYLOAD_IDENTIFIER]) {
47
+ const { type, args } = event[CSEC_TASK_PAYLOAD_IDENTIFIER];
48
+ if (!Array.isArray(args)) return undefined;
49
+
50
+ return this.generateHandler(type, ...args);
51
+ }
52
+ return undefined;
53
+ };
54
+
55
+ /**
56
+ * invokes current lambda function with passed lambda
57
+ * delegated task object as the payload.
58
+ * @param {LambdaDelegatedTaskBase} lambdaDelegatedTaskObject
59
+ */
60
+ LambdaDelegatedTaskService.prototype.delegate = function delegate (lambdaDelegatedTaskObject) {
61
+ const payload = lambdaDelegatedTaskObject.addToPayload();
62
+ const qualifiedARN = Agent.getAgent().applicationInfo.applicationUUID;
63
+ const splitArn = String.prototype.split.call(qualifiedARN, ':');
64
+ const arn = splitArn.slice(0, splitArn.length - 1).join(':');
65
+ const ver = process.env[AWS_LAMBDA_FUNCTION_VERSION_ENV_IDENTIFIER];
66
+ invokeLambda(arn, ver, payload);
67
+ };
68
+
69
+ /**
70
+ * Returns a singleton instance of LambdaDelegatedTaskService
71
+ * @returns {LambdaDelegatedTaskService} instance
72
+ */
73
+ LambdaDelegatedTaskService.getInstance = function () {
74
+ return instance || LambdaDelegatedTaskService.createInstance();
75
+ };
76
+
77
+ /**
78
+ * Creates and returns a new instance of LambdaDelegatedTaskService
79
+ * @returns {LambdaDelegatedTaskService} instance
80
+ */
81
+ LambdaDelegatedTaskService.createInstance = function () {
82
+ instance = new LambdaDelegatedTaskService();
83
+ return instance;
84
+ };
85
+
86
+ module.exports = {
87
+ LambdaDelegatedTaskService,
88
+ LambdaDelegatedTaskType,
89
+ CONST
90
+ };
@@ -0,0 +1,176 @@
1
+ /*
2
+ * Copyright 2023 New Relic Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ 'use strict';
7
+
8
+ const fs = require('fs');
9
+
10
+ const log4js = require('log4js');
11
+ const { ENV: env } = require('../../resources/config');
12
+ const cp = require('child_process');
13
+
14
+ const INFO = 'info';
15
+ const LOG_FILE_SIZE = 50485760 * 10000;
16
+ const LOG_PATTERN_OBJ = {
17
+ type: 'pattern',
18
+ pattern: '%[%d[%p][%c][%h][%z]%] %m'
19
+ };
20
+ const { LOG_MESSAGES, LOG_DIR } = require('./sec-agent-constants');
21
+ let logger;
22
+ const API = require('../../../nr-security-api');
23
+ const NRAgent = API.getNRAgent();
24
+
25
+ function createLogPathIfNotExist (dir) {
26
+ try {
27
+ if (!fs.existsSync(dir)) {
28
+ fs.mkdirSync(dir, { recursive: true });
29
+ }
30
+ } catch (error) {
31
+ }
32
+ }
33
+
34
+ /**
35
+ * Initializes the logger appenders.
36
+ * @param {v4uuid} applicationUUID
37
+ */
38
+ function initialize (applicationUUID) {
39
+ const appenders = {};
40
+ appenders.NR_CSEC = {
41
+ type: 'file',
42
+ compress: false,
43
+ layout: LOG_PATTERN_OBJ
44
+ };
45
+ appenders.NR_CSEC_INIT = {
46
+ type: 'file',
47
+ compress: false,
48
+ layout: LOG_PATTERN_OBJ
49
+ };
50
+
51
+ const logLevel = NRAgent && NRAgent.config && NRAgent.config.logging.enabled ? NRAgent.config.logging.level : 'info';
52
+
53
+ try {
54
+ if (!fs.existsSync(LOG_DIR)) {
55
+ createLogPathIfNotExist(LOG_DIR);
56
+ }
57
+ } catch (error) {
58
+ }
59
+
60
+ const logFileName = `${LOG_DIR}node-security-collector.log`;
61
+ const initLogFileName = `${LOG_DIR}node-security-collector-init.log`;
62
+
63
+ Object.assign(appenders.NR_CSEC, {
64
+ filename: logFileName,
65
+ maxLogSize: LOG_FILE_SIZE,
66
+ backups: 0
67
+ });
68
+
69
+ Object.assign(appenders.NR_CSEC_INIT, {
70
+ filename: initLogFileName,
71
+ maxLogSize: LOG_FILE_SIZE,
72
+ backups: 0
73
+ });
74
+
75
+ const categories = {
76
+ default: {
77
+ appenders: [
78
+ 'NR_CSEC'
79
+ ],
80
+ level: logLevel
81
+ },
82
+ NR_CSEC_INIT: {
83
+ appenders: [
84
+ 'NR_CSEC_INIT',
85
+ 'NR_CSEC'
86
+ ],
87
+ level: logLevel
88
+ },
89
+ };
90
+
91
+ if (env === 'dev' || env === 'trace') {
92
+ appenders.STDOUT = {
93
+ type: 'stdout',
94
+ layout: LOG_PATTERN_OBJ
95
+ };
96
+ categories.default.appenders.push('STDOUT');
97
+ categories.NR_CSEC_INIT.appenders.push('STDOUT');
98
+ }
99
+
100
+ log4js.configure({
101
+ appenders: appenders,
102
+ categories: categories,
103
+ disableClustering: true
104
+ });
105
+ process.env.loglevel = logLevel;
106
+ }
107
+
108
+ /**
109
+ * sets the logging level of current instance.
110
+ * @param {String} level
111
+ */
112
+ function setLevel (level = INFO) {
113
+ level = level.trim();
114
+ if (logger) {
115
+ logger.level = level;
116
+ } else {
117
+ throw new Error('CSEC-Loggers Not yet Initialized!');
118
+ }
119
+ }
120
+
121
+ /**
122
+ * get the logging level of current instance.
123
+ */
124
+ function getLevel () {
125
+ if (logger) {
126
+ return logger.level;
127
+ } else {
128
+ throw new Error('CSEC-Loggers Not yet Initialized!');
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Returns an instance of Loggers.
134
+ * Initializes an instace if not instance is
135
+ * already made.
136
+ * @param {v4uuid} applicationUUID
137
+ */
138
+ function getLogger (applicationUUID = process.env.applicationUUID) {
139
+ if (logger) {
140
+ return logger;
141
+ }
142
+ if (applicationUUID) {
143
+ initialize(applicationUUID);
144
+ logger = log4js.getLogger('NR_CSEC');
145
+ const initLogger = getInitLogger();
146
+ if (NRAgent) {
147
+ setLevel(NRAgent.config.logging.level);
148
+ }
149
+
150
+ return logger;
151
+ } else {
152
+ throw new Error('applicationUUID is required to initialize loggers.');
153
+ }
154
+ }
155
+
156
+ function reCreateLogger (applicationUUID) {
157
+ initialize(applicationUUID);
158
+ logger = log4js.getLogger('NR_CSEC');
159
+ setLevel(logger.level);
160
+ updateModuleLoggers(logger);
161
+ }
162
+
163
+ function getInitLogger () {
164
+ const initLogger = log4js.getLogger('NR_CSEC_INIT');
165
+ return initLogger;
166
+ }
167
+
168
+
169
+
170
+ module.exports = {
171
+ getLogger,
172
+ setLevel,
173
+ getLevel,
174
+ getInitLogger,
175
+ reCreateLogger
176
+ };