@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.
- package/CHANGELOG.md +0 -0
- package/CONTRIBUTING.md +23 -0
- package/LICENSE +43 -0
- package/README.md +98 -0
- package/THIRD_PARTY_NOTICES.md +2909 -0
- package/index.js +24 -0
- package/lib/instrumentation-security/core/constants.js +66 -0
- package/lib/instrumentation-security/core/event-constants.js +49 -0
- package/lib/instrumentation-security/core/request-manager.js +73 -0
- package/lib/instrumentation-security/core/route-manager.js +30 -0
- package/lib/instrumentation-security/core/sec-trace.js +134 -0
- package/lib/instrumentation-security/core/sec-utils.js +212 -0
- package/lib/instrumentation-security/core/security-metadata.js +34 -0
- package/lib/instrumentation-security/core/stack-trace.js +156 -0
- package/lib/instrumentation-security/hooks/director/nr-director.js +98 -0
- package/lib/instrumentation-security/hooks/express/nr-express.js +106 -0
- package/lib/instrumentation-security/hooks/fastify/nr-fastify.js +155 -0
- package/lib/instrumentation-security/hooks/fs/nr-fs.js +391 -0
- package/lib/instrumentation-security/hooks/hapi/nr-hapi.js +131 -0
- package/lib/instrumentation-security/hooks/http/nr-http.js +461 -0
- package/lib/instrumentation-security/hooks/koa/@koa/nr-@koa-router.js +110 -0
- package/lib/instrumentation-security/hooks/ldap/nr-ldap.js +81 -0
- package/lib/instrumentation-security/hooks/mongodb/common.js +239 -0
- package/lib/instrumentation-security/hooks/mongodb/constants.js +91 -0
- package/lib/instrumentation-security/hooks/mongodb/nr-mongodb.js +49 -0
- package/lib/instrumentation-security/hooks/mongodb/v2-mongo.js +91 -0
- package/lib/instrumentation-security/hooks/mongodb/v3-mongo.js +36 -0
- package/lib/instrumentation-security/hooks/mongodb/v4-mongo.js +17 -0
- package/lib/instrumentation-security/hooks/mysql/nr-mysql.js +232 -0
- package/lib/instrumentation-security/hooks/native/nr-childProcess.js +131 -0
- package/lib/instrumentation-security/hooks/postgres/nr-postgres.js +131 -0
- package/lib/instrumentation-security/hooks/require/nr-require.js +93 -0
- package/lib/instrumentation-security/hooks/restify/nr-restify.js +104 -0
- package/lib/instrumentation-security/hooks/xpath/nr-xpath.js +91 -0
- package/lib/instrumentation-security/index.js +171 -0
- package/lib/nr-security-agent/index.js +170 -0
- package/lib/nr-security-agent/lib/core/Auth-headers.js +58 -0
- package/lib/nr-security-agent/lib/core/ContainerProperties.js +42 -0
- package/lib/nr-security-agent/lib/core/ECSProperties.js +34 -0
- package/lib/nr-security-agent/lib/core/FuzzFailEvent.js +27 -0
- package/lib/nr-security-agent/lib/core/LinkingMetadata.js +21 -0
- package/lib/nr-security-agent/lib/core/LogMessage.js +47 -0
- package/lib/nr-security-agent/lib/core/PodProperties.js +36 -0
- package/lib/nr-security-agent/lib/core/Policy.js +105 -0
- package/lib/nr-security-agent/lib/core/agent-status.js +56 -0
- package/lib/nr-security-agent/lib/core/agent.js +146 -0
- package/lib/nr-security-agent/lib/core/applicationinfo.js +634 -0
- package/lib/nr-security-agent/lib/core/commonUtils.js +411 -0
- package/lib/nr-security-agent/lib/core/connections/aws-lambda-client.js +55 -0
- package/lib/nr-security-agent/lib/core/connections/restclient.js +20 -0
- package/lib/nr-security-agent/lib/core/connections/websocket/index.js +25 -0
- package/lib/nr-security-agent/lib/core/connections/websocket/response/collecotor-policy-error-handler.js +18 -0
- package/lib/nr-security-agent/lib/core/connections/websocket/response/collector-policy-handler.js +20 -0
- package/lib/nr-security-agent/lib/core/connections/websocket/response/fuzz-request-handler.js +171 -0
- package/lib/nr-security-agent/lib/core/connections/websocket/response/index.js +47 -0
- package/lib/nr-security-agent/lib/core/connections/websocket/response/no-op-handler.js +12 -0
- package/lib/nr-security-agent/lib/core/connections/websocket/response/startup-handshake-handler.js +27 -0
- package/lib/nr-security-agent/lib/core/connections/websocket/response/unsupported-agent-handler.js +18 -0
- package/lib/nr-security-agent/lib/core/connections/websocket/response/ws-reconnect.js +14 -0
- package/lib/nr-security-agent/lib/core/connections/websocket/websocket.js +443 -0
- package/lib/nr-security-agent/lib/core/event.js +186 -0
- package/lib/nr-security-agent/lib/core/health-check.js +159 -0
- package/lib/nr-security-agent/lib/core/json-file-handler.js +66 -0
- package/lib/nr-security-agent/lib/core/jsonValidator.js +149 -0
- package/lib/nr-security-agent/lib/core/lambda-delegated-task-service.js +90 -0
- package/lib/nr-security-agent/lib/core/logging.js +176 -0
- package/lib/nr-security-agent/lib/core/sec-agent-constants.js +173 -0
- package/lib/nr-security-agent/lib/core/sec-util.js +166 -0
- package/lib/nr-security-agent/lib/core/sha-size-util.js +338 -0
- package/lib/nr-security-agent/lib/core/sha_worker.js +24 -0
- package/lib/nr-security-agent/lib/core/startup-config.js +29 -0
- package/lib/nr-security-agent/lib/core/statusUtils.js +157 -0
- package/lib/nr-security-agent/lib/core/timeunit.js +44 -0
- package/lib/nr-security-agent/lib/core/websocket-client.js +335 -0
- package/lib/nr-security-agent/lib/core/worker-entities.js +82 -0
- package/lib/nr-security-agent/lib/core/worker.js +148 -0
- package/lib/nr-security-agent/lib/core/xss-utils.js +297 -0
- package/lib/nr-security-agent/lib/models/lambda-delegated-task/constants.js +12 -0
- package/lib/nr-security-agent/lib/models/lambda-delegated-task/index.js +15 -0
- package/lib/nr-security-agent/lib/models/lambda-delegated-task/lambda-delegated-sha-calculation.js +37 -0
- package/lib/nr-security-agent/lib/models/lambda-delegated-task/lambda-delegated-task-base.js +50 -0
- package/lib/nr-security-agent/lib/models/lambda-delegated-task/lambda-delegated-task-type.js +17 -0
- package/lib/nr-security-agent/resources/cert.pem +31 -0
- package/lib/nr-security-agent/resources/config.js +19 -0
- package/lib/nr-security-agent/resources/default-policy.yaml +36 -0
- package/lib/nr-security-api/index.js +111 -0
- package/package.json +95 -0
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 New Relic Corporation. All rights reserved.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/* eslint-disable prefer-promise-reject-errors */
|
|
7
|
+
const fs = require('fs');
|
|
8
|
+
const uuidv4 = require('uuid/v4');
|
|
9
|
+
const logs = require('./logging');
|
|
10
|
+
const logger = logs.getLogger();
|
|
11
|
+
const cp = require('child_process');
|
|
12
|
+
const OS = require('os');
|
|
13
|
+
const path = require('path');
|
|
14
|
+
const checkDiskSpace = require('check-disk-space').default;
|
|
15
|
+
const AgentStatus = require('../core/agent-status');
|
|
16
|
+
const { LOG_MESSAGES, CSEC_HOME } = require('./sec-agent-constants');
|
|
17
|
+
|
|
18
|
+
const prettyBytes = require('pretty-bytes');
|
|
19
|
+
const njsAgentConstants = require('./sec-agent-constants');
|
|
20
|
+
const LinkingMetaData = require('./LinkingMetadata');
|
|
21
|
+
const API = require('../../../nr-security-api');
|
|
22
|
+
const NRAgent = API.getNRAgent();
|
|
23
|
+
const lodash = require('lodash')
|
|
24
|
+
const backupLogFileRegex = /.log.\d/gm;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Utility to generate UUID
|
|
28
|
+
* @returns UUID
|
|
29
|
+
*/
|
|
30
|
+
function getUUID(){
|
|
31
|
+
let uuid = process.env.applicationUUID || uuidv4();
|
|
32
|
+
process.env.applicationUUID = uuid;
|
|
33
|
+
return uuid;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function getKindIdPair (identifier, hostId) {
|
|
37
|
+
const obj = {};
|
|
38
|
+
if (identifier.isContainer) {
|
|
39
|
+
obj.first = 'CONTAINER';
|
|
40
|
+
obj.second = identifier.containerId;
|
|
41
|
+
} else if (identifier.isECSContainer) {
|
|
42
|
+
obj.first = 'ECS';
|
|
43
|
+
obj.second = identifier.ecsTaskId;
|
|
44
|
+
} else if (identifier.isPod) {
|
|
45
|
+
obj.first = 'CONTAINER';
|
|
46
|
+
obj.second = identifier.podId;
|
|
47
|
+
} else {
|
|
48
|
+
obj.first = 'HOST';
|
|
49
|
+
obj.second = hostId;
|
|
50
|
+
}
|
|
51
|
+
return obj;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Utility to check worker thread support
|
|
56
|
+
* @returns
|
|
57
|
+
*/
|
|
58
|
+
function hasWorker () {
|
|
59
|
+
process.hasWorker = false;
|
|
60
|
+
return process.hasWorker;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function runtimeSupportsFunctionGenerators () {
|
|
64
|
+
try {
|
|
65
|
+
// eslint-disable-next-line no-eval
|
|
66
|
+
eval('"use strict"; (function* () {})');
|
|
67
|
+
process.csecRuntimeSupportsFunctionGenerators = true;
|
|
68
|
+
} catch (err) {
|
|
69
|
+
process.csecRuntimeSupportsFunctionGenerators = false;
|
|
70
|
+
}
|
|
71
|
+
return process.csecRuntimeSupportsFunctionGenerators;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function runtimeSupportsAsyncFunctionGenerators () {
|
|
75
|
+
try {
|
|
76
|
+
// eslint-disable-next-line no-eval
|
|
77
|
+
eval('"use strict"; (async function* () {})');
|
|
78
|
+
process.csecRuntimeSupportsAsyncFunctionGenerators = true;
|
|
79
|
+
} catch (err) {
|
|
80
|
+
process.csecRuntimeSupportsAsyncFunctionGenerators = false;
|
|
81
|
+
}
|
|
82
|
+
return process.csecRuntimeSupportsAsyncFunctionGenerators;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Utility to create directory and set permissions
|
|
87
|
+
* @param {*} dir
|
|
88
|
+
*/
|
|
89
|
+
function createPathIfNotExist (dir) {
|
|
90
|
+
try {
|
|
91
|
+
if (!fs.existsSync(dir)) {
|
|
92
|
+
fs.mkdirSync(dir, 777, { recursive: true });
|
|
93
|
+
logger.info(dir + ' Created');
|
|
94
|
+
fs.chmodSync(dir, 0o777);
|
|
95
|
+
} else {
|
|
96
|
+
fs.chmodSync(dir, 0o777);
|
|
97
|
+
logger.debug(dir + ' Already Exists');
|
|
98
|
+
}
|
|
99
|
+
} catch (error) {
|
|
100
|
+
logger.debug(dir + ' Not Created', error);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Utility to get OS
|
|
106
|
+
* @returns
|
|
107
|
+
*/
|
|
108
|
+
function getOS () {
|
|
109
|
+
if (process.platform === 'darwin') {
|
|
110
|
+
return 'mac';
|
|
111
|
+
}
|
|
112
|
+
if (process.platform === 'win32') {
|
|
113
|
+
return 'windows';
|
|
114
|
+
} else {
|
|
115
|
+
return 'linux';
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Utility to create directories
|
|
121
|
+
*/
|
|
122
|
+
function createDirectories () {
|
|
123
|
+
createPathIfNotExist(`${CSEC_HOME}/nr-security-home`);
|
|
124
|
+
createPathIfNotExist(`${CSEC_HOME}/nr-security-home/logs`);
|
|
125
|
+
createPathIfNotExist(`${CSEC_HOME}/nr-security-home/logs/snapshots`);
|
|
126
|
+
createPathIfNotExist(`${CSEC_HOME}/nr-security-home/tmp`);
|
|
127
|
+
createPathIfNotExist(`${CSEC_HOME}/nr-security-home/tmp/language-agent`);
|
|
128
|
+
createPathIfNotExist(`${CSEC_HOME}/nr-security-home/tmp/language-agent/${getUUID()}`);
|
|
129
|
+
createPathIfNotExist(`${CSEC_HOME}/nr-security-home/tmp/language-agent/${getUUID()}/ds-tmp`);
|
|
130
|
+
try {
|
|
131
|
+
fs.chmodSync(`${CSEC_HOME}/nr-security-home`, 0o777);
|
|
132
|
+
} catch (error) {
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Utility to check init setup and disk space
|
|
137
|
+
*/
|
|
138
|
+
async function initialSetup () {
|
|
139
|
+
try {
|
|
140
|
+
checkDiskSpace(`${CSEC_HOME}`).then((diskSpace) => {
|
|
141
|
+
if (diskSpace.free < 1000000000) {
|
|
142
|
+
logger.error(LOG_MESSAGES.AVAIL_DISK_ERROR, `${CSEC_HOME}`, prettyBytes(diskSpace.free));
|
|
143
|
+
process.exit();
|
|
144
|
+
}
|
|
145
|
+
}).catch(() => {
|
|
146
|
+
});
|
|
147
|
+
createDirectories();
|
|
148
|
+
} catch (err) {
|
|
149
|
+
logger.error(err);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Utility function to check if the logfile is accessible or not.
|
|
155
|
+
*/
|
|
156
|
+
function isLogAccessible (logFileName) {
|
|
157
|
+
return new Promise((resolve) => {
|
|
158
|
+
if (logFileName === njsAgentConstants.STATUS_LOG_FILE && require('./statusUtils').getBufferedHC().length === 0) {
|
|
159
|
+
resolve('OK');
|
|
160
|
+
}
|
|
161
|
+
fs.access(logFileName, fs.constants.F_OK | fs.constants.W_OK, (err) => {
|
|
162
|
+
if (err) {
|
|
163
|
+
resolve('Error');
|
|
164
|
+
} else {
|
|
165
|
+
resolve('OK');
|
|
166
|
+
}
|
|
167
|
+
});
|
|
168
|
+
}).catch(() => {
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Utility function to get CPU Usage in percentage
|
|
175
|
+
* @param {*} oldUsage
|
|
176
|
+
*/
|
|
177
|
+
function getCpuUsage () {
|
|
178
|
+
const usageObj = process.cpuUsage();
|
|
179
|
+
const userUsage = usageObj.user / (usageObj.user + usageObj.system);
|
|
180
|
+
return userUsage;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Utility to convert Bytes to MB
|
|
185
|
+
* @param {*} bytes
|
|
186
|
+
* @param {*} decimals
|
|
187
|
+
*/
|
|
188
|
+
function formatBytesToMB (bytes, decimals = 2) {
|
|
189
|
+
return parseFloat((bytes / (1024 * 1024)).toFixed(decimals));
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Utility to get diskSpace infor for the give path
|
|
194
|
+
* @param {*} path
|
|
195
|
+
*/
|
|
196
|
+
async function getDiskSpace (path) {
|
|
197
|
+
try {
|
|
198
|
+
const x = await checkDiskSpace(path).catch(() => {});
|
|
199
|
+
return x;
|
|
200
|
+
} catch (error) {
|
|
201
|
+
logger.error("Error in getting diskpace:",error)
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Utility to get HC stats
|
|
207
|
+
*/
|
|
208
|
+
async function getHCStats () {
|
|
209
|
+
const stats = {};
|
|
210
|
+
try {
|
|
211
|
+
const appInfo = require('./applicationinfo').getInstance();
|
|
212
|
+
const rootDiskSpace = await getDiskSpace(path.parse(process.cwd()).root);
|
|
213
|
+
if (appInfo && appInfo.serverInfo && appInfo.serverInfo.deployedApplications[0] && appInfo.serverInfo.deployedApplications[0].deployedPath) {
|
|
214
|
+
const processDirDiskFreeSpace = await getDiskSpace(appInfo.serverInfo.deployedApplications[0].deployedPath);
|
|
215
|
+
if(processDirDiskFreeSpace){
|
|
216
|
+
stats.processDirDiskFreeSpaceMB = formatBytesToMB(processDirDiskFreeSpace.free);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
const memoryObj = process.memoryUsage();
|
|
220
|
+
if(rootDiskSpace){
|
|
221
|
+
stats.rootDiskFreeSpaceMB = formatBytesToMB(rootDiskSpace.free);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
stats.processHeapUsageMB = formatBytesToMB(memoryObj.heapUsed);
|
|
225
|
+
stats.processRssMB = formatBytesToMB(memoryObj.rss);
|
|
226
|
+
stats.processMaxHeapMB = formatBytesToMB(memoryObj.heapTotal);
|
|
227
|
+
stats.systemFreeMemoryMB = formatBytesToMB(OS.freemem());
|
|
228
|
+
stats.systemTotalMemoryMB = formatBytesToMB(OS.totalmem());
|
|
229
|
+
stats.nCores = OS.cpus().length;
|
|
230
|
+
stats.processCpuUsage = parseFloat(getCpuUsage().toFixed(2));
|
|
231
|
+
stats.systemCpuLoad = parseFloat(OS.loadavg()[1].toFixed(2));
|
|
232
|
+
} catch (error) {
|
|
233
|
+
logger.error('Error in generating stats:', error);
|
|
234
|
+
}
|
|
235
|
+
return stats;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Utility to check Agent status
|
|
239
|
+
*/
|
|
240
|
+
function isAgentActiveState () {
|
|
241
|
+
const linkingMetaData = LinkingMetaData.getLinkingMetadata();
|
|
242
|
+
if (AgentStatus.getInstance().getStatus() === AgentStatus.CSECAgentStatus.codes.ACTIVE && (NRAgent ? (NRAgent.config.security.enabled && NRAgent.canCollectData()) : true)) {
|
|
243
|
+
return 'OK';
|
|
244
|
+
} else {
|
|
245
|
+
return 'Error';
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function iastRestClientStatus () {
|
|
250
|
+
return 'OK';
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* Utiltity to remove older snapshots
|
|
254
|
+
*/
|
|
255
|
+
function removeOlderSnapshots () {
|
|
256
|
+
try {
|
|
257
|
+
const basePath = njsAgentConstants.LOG_DIR + 'snapshots/';
|
|
258
|
+
const directoryContent = fs.readdirSync(basePath);
|
|
259
|
+
const files = directoryContent.filter((filename) => {
|
|
260
|
+
return fs.statSync(`${basePath}/${filename}`).isFile();
|
|
261
|
+
});
|
|
262
|
+
const sorted = files.sort((a, b) => {
|
|
263
|
+
const firstStat = fs.statSync(`${basePath}/${a}`);
|
|
264
|
+
const secondStat = fs.statSync(`${basePath}/${b}`);
|
|
265
|
+
return new Date(secondStat.mtime).getTime() - new Date(firstStat.mtime).getTime();
|
|
266
|
+
});
|
|
267
|
+
for (let i = 99; i < sorted.length; i++) {
|
|
268
|
+
const fileToDelete = basePath + sorted[i];
|
|
269
|
+
try {
|
|
270
|
+
fs.unlink(fileToDelete, (err) => {
|
|
271
|
+
if (err) {
|
|
272
|
+
logger.error(err);
|
|
273
|
+
} else {
|
|
274
|
+
logger.debug('Snapshot deleted:', fileToDelete);
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
} catch (error) {
|
|
278
|
+
logger.error(error);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
} catch (error) {
|
|
282
|
+
logger.debug(error);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Utilty to remove older log files.
|
|
287
|
+
*/
|
|
288
|
+
function removeOlderlogfiles () {
|
|
289
|
+
try {
|
|
290
|
+
const basePath = njsAgentConstants.LOG_DIR;
|
|
291
|
+
const directoryContent = fs.readdirSync(basePath);
|
|
292
|
+
// eslint-disable-next-line array-callback-return
|
|
293
|
+
const files = directoryContent.filter((filename) => {
|
|
294
|
+
if (filename.match(backupLogFileRegex)) {
|
|
295
|
+
return fs.statSync(`${basePath}/${filename}`).isFile();
|
|
296
|
+
}
|
|
297
|
+
});
|
|
298
|
+
const sorted = files.sort((a, b) => {
|
|
299
|
+
const firstStat = fs.statSync(`${basePath}/${a}`);
|
|
300
|
+
const secondStat = fs.statSync(`${basePath}/${b}`);
|
|
301
|
+
return new Date(secondStat.mtime).getTime() - new Date(firstStat.mtime).getTime();
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
for (let i = 2; i < sorted.length; i++) {
|
|
305
|
+
const fileToDelete = basePath + sorted[i];
|
|
306
|
+
try {
|
|
307
|
+
fs.unlink(fileToDelete, (err) => {
|
|
308
|
+
if (err) {
|
|
309
|
+
logger.error(err);
|
|
310
|
+
} else {
|
|
311
|
+
logger.debug('Logfile deleted:', fileToDelete);
|
|
312
|
+
}
|
|
313
|
+
});
|
|
314
|
+
} catch (error) {
|
|
315
|
+
logger.error(error);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
} catch (error) {
|
|
319
|
+
logger.debug(error);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* utilty to roll over the log file. Not in use
|
|
325
|
+
*/
|
|
326
|
+
function logRollOver () {
|
|
327
|
+
const logFileName = `${njsAgentConstants.LOG_DIR}node-security-collector.log`;
|
|
328
|
+
try {
|
|
329
|
+
const stats = fs.statSync(logFileName);
|
|
330
|
+
if (stats.size < 50485760) {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
} catch (error) {
|
|
334
|
+
logger.error('Error in getting stats of log file:', error);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
try {
|
|
338
|
+
const lockFile = `${njsAgentConstants.LOG_DIR}lock-file.lock`;
|
|
339
|
+
const pid = process.pid;
|
|
340
|
+
if (!fs.existsSync(lockFile)) {
|
|
341
|
+
fs.writeFileSync(lockFile, pid.toString());
|
|
342
|
+
}
|
|
343
|
+
const fileData = fs.readFileSync(lockFile).toString();
|
|
344
|
+
if (pid.toString() === fileData) {
|
|
345
|
+
try {
|
|
346
|
+
const newLogFile = logFileName + '.' + Date.now();
|
|
347
|
+
fs.copyFileSync(logFileName, newLogFile);
|
|
348
|
+
fs.truncateSync(logFileName);
|
|
349
|
+
logger.info('logfile rolled over successfully', newLogFile);
|
|
350
|
+
removeOlderlogfiles();
|
|
351
|
+
} catch (error) {
|
|
352
|
+
logger.error(error);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
fs.unlinkSync(lockFile);
|
|
356
|
+
} catch (error) {
|
|
357
|
+
logger.error(error);
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Utility to get validator service endpoint
|
|
363
|
+
* @returns
|
|
364
|
+
*/
|
|
365
|
+
function getValidatorServiceEndpointURL(){
|
|
366
|
+
let validatorServiceEndpointURL = NRAgent ? NRAgent.config.security.validator_service_url: "wss://csec.nr-data.net";
|
|
367
|
+
if(lodash.isEmpty(validatorServiceEndpointURL)){
|
|
368
|
+
validatorServiceEndpointURL = "wss://csec.nr-data.net";
|
|
369
|
+
}
|
|
370
|
+
return validatorServiceEndpointURL
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Utility to get security mode
|
|
375
|
+
* @returns
|
|
376
|
+
*/
|
|
377
|
+
function getCSECmode(){
|
|
378
|
+
const mode = NRAgent ? NRAgent.config.security.mode : 'IAST';
|
|
379
|
+
return mode
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
/**
|
|
383
|
+
* Utility to get path of CA Cert
|
|
384
|
+
* @returns
|
|
385
|
+
*/
|
|
386
|
+
function getPathOfCACert(){
|
|
387
|
+
const certPath = NRAgent && NRAgent.config.security.ca_bundle_path ? NRAgent.config.security.ca_bundle_path : path.resolve(__dirname, '../../resources/cert.pem');
|
|
388
|
+
return certPath
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
module.exports = {
|
|
392
|
+
getUUID,
|
|
393
|
+
getKindIdPair,
|
|
394
|
+
hasWorker,
|
|
395
|
+
runtimeSupportsFunctionGenerators,
|
|
396
|
+
runtimeSupportsAsyncFunctionGenerators,
|
|
397
|
+
createPathIfNotExist,
|
|
398
|
+
getOS,
|
|
399
|
+
initialSetup,
|
|
400
|
+
createDirectories,
|
|
401
|
+
isLogAccessible,
|
|
402
|
+
getCpuUsage,
|
|
403
|
+
getHCStats,
|
|
404
|
+
isAgentActiveState,
|
|
405
|
+
iastRestClientStatus,
|
|
406
|
+
removeOlderSnapshots,
|
|
407
|
+
logRollOver,
|
|
408
|
+
getValidatorServiceEndpointURL,
|
|
409
|
+
getCSECmode,
|
|
410
|
+
getPathOfCACert
|
|
411
|
+
};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 New Relic Corporation. All rights reserved.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const _ = require('lodash');
|
|
7
|
+
|
|
8
|
+
const logger = require('../logging').getLogger();
|
|
9
|
+
|
|
10
|
+
let lambdaClient;
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Invokes lambda specified by arn, version with
|
|
14
|
+
* given data
|
|
15
|
+
* @param {String} arn
|
|
16
|
+
* @param {String} version
|
|
17
|
+
* @param {JSON} data
|
|
18
|
+
* @returns Invoke Result Promise
|
|
19
|
+
*/
|
|
20
|
+
function invokeLambda (arn, version, data) {
|
|
21
|
+
if (!lambdaClient) {
|
|
22
|
+
throw new Error('Cannot invoke lambda, client not initialized');
|
|
23
|
+
}
|
|
24
|
+
if (_.isEmpty(arn) || _.isEmpty(version)) {
|
|
25
|
+
throw new Error('Cannot invoke lambda, invalid ARN specified');
|
|
26
|
+
}
|
|
27
|
+
const dataString = JSON.stringify(data);
|
|
28
|
+
const invokeData = {
|
|
29
|
+
FunctionName: arn,
|
|
30
|
+
ClientContext: Buffer.from('{}').toString('base64'),
|
|
31
|
+
InvocationType: 'Event',
|
|
32
|
+
LogType: 'Tail',
|
|
33
|
+
Payload: Buffer.from(dataString),
|
|
34
|
+
Qualifier: version
|
|
35
|
+
};
|
|
36
|
+
logger.debug('Invoke lambda data:', invokeData);
|
|
37
|
+
return lambdaClient.invoke(invokeData);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function init () {
|
|
41
|
+
if (
|
|
42
|
+
!process.csecRuntimeSupportsFunctionGenerators ||
|
|
43
|
+
!process.csecRuntimeSupportsAsyncFunctionGenerators
|
|
44
|
+
) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const { Lambda } = require('@aws-sdk/client-lambda');
|
|
49
|
+
lambdaClient = new Lambda();
|
|
50
|
+
}
|
|
51
|
+
init();
|
|
52
|
+
|
|
53
|
+
module.exports = {
|
|
54
|
+
invokeLambda
|
|
55
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 New Relic Corporation. All rights reserved.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const axios = require('axios').default;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* fire http request based on provided config
|
|
10
|
+
* @param {*} config
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
function fireRequest (config) {
|
|
14
|
+
axios.defaults.timeout = 5000;
|
|
15
|
+
return axios(config);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
module.exports = {
|
|
19
|
+
fireRequest
|
|
20
|
+
};
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright 2023 New Relic Corporation. All rights reserved.
|
|
3
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
const {
|
|
7
|
+
SecWebSocket,
|
|
8
|
+
WSCallbacks,
|
|
9
|
+
aquireWebSocket,
|
|
10
|
+
isWebSocketBusy,
|
|
11
|
+
releaseWebSocket,
|
|
12
|
+
setWebSocketConn
|
|
13
|
+
} = require('./websocket');
|
|
14
|
+
|
|
15
|
+
const ResponseHandler = require('./response');
|
|
16
|
+
|
|
17
|
+
module.exports = {
|
|
18
|
+
SecWebSocket,
|
|
19
|
+
WSCallbacks,
|
|
20
|
+
aquireWebSocket,
|
|
21
|
+
isWebSocketBusy,
|
|
22
|
+
releaseWebSocket,
|
|
23
|
+
setWebSocketConn,
|
|
24
|
+
ResponseHandler
|
|
25
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
|
|
2
|
+
/*
|
|
3
|
+
* Copyright 2023 New Relic Corporation. All rights reserved.
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
let logger;
|
|
8
|
+
|
|
9
|
+
module.exports.handler = function handler (json) {
|
|
10
|
+
try {
|
|
11
|
+
const error = json.data;
|
|
12
|
+
logger.error("Policy updation faild due to violation:", error);
|
|
13
|
+
} catch (error) {
|
|
14
|
+
logger.error('paring exception while parsing configuration', error);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
module.exports.setLogger = l => { logger = l };
|
package/lib/nr-security-agent/lib/core/connections/websocket/response/collector-policy-handler.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
|
|
2
|
+
/*
|
|
3
|
+
* Copyright 2023 New Relic Corporation. All rights reserved.
|
|
4
|
+
* SPDX-License-Identifier: Apache-2.0
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
let logger;
|
|
8
|
+
|
|
9
|
+
module.exports.handler = function handler(json) {
|
|
10
|
+
try {
|
|
11
|
+
const policyData = json.data;
|
|
12
|
+
const PolicyManager = require('../../../Policy');
|
|
13
|
+
PolicyManager.setPolicyData(policyData);
|
|
14
|
+
|
|
15
|
+
} catch (error) {
|
|
16
|
+
logger.error('paring exception while parsing configuration', error);
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
module.exports.setLogger = l => { logger = l };
|