@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,36 @@
1
+ /*
2
+ * Copyright 2023 New Relic Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ let podProperties;
7
+ function PodProperties () {
8
+
9
+ }
10
+ PodProperties.prototype.constructor = PodProperties;
11
+
12
+ /**
13
+ * Returns the current instanceof PodProperties,
14
+ * creates one if not already created.
15
+ *
16
+ * @returns {PodProperties} instance
17
+ */
18
+ function getInstance () {
19
+ if (!podProperties) {
20
+ podProperties = new PodProperties();
21
+ podProperties.name = '';
22
+ podProperties.namespace = '';
23
+ podProperties.clusterName = '';
24
+ podProperties.clusterId = '';
25
+ podProperties.entrypoint = '';
26
+ podProperties.ipAddress = '';
27
+ podProperties.hostIpAddress = '';
28
+ podProperties.creationTimestamp = '';
29
+ podProperties.containerProperties = [];
30
+ }
31
+ return podProperties;
32
+ }
33
+
34
+ module.exports = {
35
+ getInstance
36
+ };
@@ -0,0 +1,105 @@
1
+
2
+ /*
3
+ * Copyright 2023 New Relic Corporation. All rights reserved.
4
+ * SPDX-License-Identifier: Apache-2.0
5
+ */
6
+
7
+ const API = require('../../../nr-security-api');
8
+ const NRAgent = API.getNRAgent();
9
+ const { Agent } = require('./agent');
10
+
11
+ let policy;
12
+ const path = require('path');
13
+ const fs = require('fs');
14
+ const yaml = require('js-yaml');
15
+ const logs = require('./logging');
16
+ const logger = logs.getLogger();
17
+ const initLogger = logs.getInitLogger();
18
+ const jsonValidator = require('./jsonValidator');
19
+ const LOG_MESSAGES = require('./sec-agent-constants').LOG_MESSAGES;
20
+ const statusUtils = require('./statusUtils');
21
+
22
+ function Policy() {
23
+ this.data = {};
24
+ }
25
+ Policy.prototype.constructor = Policy;
26
+
27
+ /**
28
+ * Returns the current instanceof Policy,
29
+ * creates one if not already created.
30
+ *
31
+ * @returns {Policy} instance
32
+ */
33
+ function getInstance() {
34
+ if (!policy) {
35
+ policy = new Policy();
36
+ }
37
+ return policy;
38
+ }
39
+
40
+
41
+ // sets up policy data to use globally.
42
+ function setPolicyData(data) {
43
+ policy = getInstance();
44
+ const jsonValidationResult = jsonValidator.validate(data);
45
+ if (jsonValidationResult && jsonValidationResult.errors && jsonValidationResult.errors.length > 0) {
46
+ logger.error(LOG_MESSAGES.EXP_RAISE_POL, JSON.stringify(jsonValidationResult.errors));
47
+ logger.info(LOG_MESSAGES.FALLBACK_TO_CURR_POLICY, JSON.stringify(policy.data));
48
+ return false;
49
+ }
50
+ if (policy.data.version !== data.version) {
51
+ policy.data = data;
52
+ appInfoVersionUpdate(policy, data);
53
+ initLogger.info(LOG_MESSAGES.POLICY_APPLIED, JSON.stringify(policy.data));
54
+ logger.info('Applied policy is:', JSON.stringify(policy.data));
55
+ if (NRAgent && NRAgent.config.security.detection) {
56
+ logger.info('Security detection flags:', JSON.stringify(NRAgent.config.security.detection));
57
+ }
58
+ return true;
59
+ }
60
+ }
61
+
62
+ // function to setup default policy provided in resources directory
63
+ function setDefaultPolicy() {
64
+ const default_policy_file = path.resolve(__dirname, '../../resources/default-policy.yaml');
65
+ // Get document, or throw exception on error
66
+ try {
67
+ const default_policy = yaml.load(fs.readFileSync(default_policy_file, 'utf8'));
68
+ policy = getInstance();
69
+ policy.data = default_policy;
70
+ logger.info(LOG_MESSAGES.DEFAULT_POL_SET, JSON.stringify(policy.data));
71
+ } catch (e) {
72
+ logger.error(LOG_MESSAGES.UNABLE_SET_DEFAULT_POL, e);
73
+ statusUtils.addErrortoBuffer(e);
74
+ };
75
+ }
76
+
77
+ /**
78
+ * Function to set applicationInfo with updated policy version
79
+ * @param {*} policy
80
+ * @param {*} data
81
+ */
82
+ function appInfoVersionUpdate(policy, data) {
83
+ const appInfo = require('./applicationinfo').getInstance();
84
+ const { Agent } = require('./agent');
85
+ appInfo.policyVersion = data.version || policy.data.version;
86
+ if (NRAgent && appInfo.entityGuid) {
87
+ Agent.getAgent().setApplicationInfo(appInfo);
88
+ }
89
+ }
90
+
91
+ /**
92
+ * Function to send updated policy to validator
93
+ * @param {*} data
94
+ */
95
+ function sendUpdatedPolicy(data) {
96
+ const wsClient = Agent.getAgent().client;
97
+ let updatedPolicy = Object.assign({}, data);
98
+ updatedPolicy.jsonName = 'lc-policy';
99
+ wsClient.dispatcher(updatedPolicy);
100
+ }
101
+ module.exports = {
102
+ getInstance,
103
+ setPolicyData,
104
+ setDefaultPolicy
105
+ };
@@ -0,0 +1,56 @@
1
+ /*
2
+ * Copyright 2023 New Relic Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ const SecAgentStatus = function () {
6
+ if (Object.getPrototypeOf(this).constructor !== SecAgentStatus) {
7
+ return new SecAgentStatus();
8
+ }
9
+ if (!SecAgentStatus.instance) {
10
+ SecAgentStatus.instance = this;
11
+ let status = SecAgentStatus.codes.CONNECTING;
12
+ const codeList = [];
13
+ for (let code in SecAgentStatus.codes) {
14
+ codeList.push(SecAgentStatus.codes[code])
15
+ }
16
+ this.getStatus = function () {
17
+ return status;
18
+ }
19
+ this.setStatus = function (STATUS) {
20
+ if (STATUS && codeList.includes(STATUS)) {
21
+ status = STATUS;
22
+ } else {
23
+ throw new Error("Unknown Status");
24
+ }
25
+ }
26
+ } else {
27
+ return SecAgentStatus.instance;
28
+ }
29
+ }
30
+ SecAgentStatus.prototype.constructor = SecAgentStatus;
31
+
32
+ /**
33
+ * Valid values to set agent status.
34
+ */
35
+ SecAgentStatus.codes = {
36
+ CONNECTING: 'connecting',
37
+ ACTIVE: "active",
38
+ DISABLED: "disabled",
39
+ DETACHED: "detached"
40
+ }
41
+
42
+ /**
43
+ * Get instance of CSECAgentStatus.
44
+ */
45
+ const getInstance = function () {
46
+ if (!SecAgentStatus.instance) {
47
+ return new SecAgentStatus();
48
+ } else {
49
+ return SecAgentStatus.instance;
50
+ }
51
+ }
52
+
53
+ module.exports = {
54
+ CSECAgentStatus: SecAgentStatus,
55
+ getInstance
56
+ };
@@ -0,0 +1,146 @@
1
+ /*
2
+ * Copyright 2023 New Relic Corporation. All rights reserved.
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+
6
+ const { EventEmitter } = require('events');
7
+
8
+ const APPLICATION_INFO_UPDATE_EVENT = 'application-info-updated';
9
+ const WS_CONNECTION_STATUS_UPDATE_EVENT = 'ws-connection-updated';
10
+ const AGENT_STATUS_UPDATED_EVENT = 'agent-status-updated';
11
+ const WS_UPDATE_EVENT = 'websocket-updated';
12
+ const RECONNECT_WS_EVENT = 'reconnect-websocket';
13
+ const LOG_MESSAGES = require('./sec-agent-constants').LOG_MESSAGES;
14
+ const LinkingMetaData = require('./LinkingMetadata');
15
+ const API = require('../../../nr-security-api');
16
+ let NRAgent = API.getNRAgent();
17
+ const agentConfig = require('../../resources/config')
18
+
19
+ const EVENTS = {
20
+ APPLICATION_INFO_UPDATE_EVENT,
21
+ WS_CONNECTION_STATUS_UPDATE_EVENT,
22
+ AGENT_STATUS_UPDATED_EVENT,
23
+ WS_UPDATE_EVENT,
24
+ RECONNECT_WS_EVENT
25
+ };
26
+
27
+ let instance = null;
28
+ /**
29
+ * This defines the CSEC agent instance which would hold
30
+ * necessary info for agent.
31
+ *
32
+ * @param {ApplicationInfo} applicationInfo
33
+ * @param {Logger} logger
34
+ * @param {WebSocket} client
35
+ * @param {AgentStatus} status
36
+ */
37
+ function Agent(applicationInfo, logger, client, status) {
38
+ const version = require(agentConfig.AGENT_DIR + '/package.json').version;
39
+ this.applicationInfo = applicationInfo;
40
+ this.client = client;
41
+ this.version = version;
42
+ this.logger = logger;
43
+ this.status = status;
44
+ this.wsStatus = 0;
45
+ }
46
+
47
+ Agent.prototype = {};
48
+ Agent.prototype.constructor = Agent;
49
+
50
+ /**
51
+ * Sets the application info in agent
52
+ * and triggers the update event.
53
+ * @param {ApplicationInfo} applicationInfoObject
54
+ */
55
+ Agent.prototype.setApplicationInfo = function setApplicationInfo(applicationInfoObject) {
56
+ this.applicationInfo = applicationInfoObject;
57
+ if (NRAgent && NRAgent.config && NRAgent.config.entity_guid) {
58
+ this.applicationInfo.entityGuid = NRAgent.config.entity_guid;
59
+ this.applicationInfo.identifier.nodeId = NRAgent.config.entity_guid;
60
+ this.applicationInfo.identifier.id = NRAgent.config.entity_guid;
61
+
62
+ Agent.eventEmitter.emit(EVENTS.APPLICATION_INFO_UPDATE_EVENT, applicationInfoObject);
63
+ const logs = require('./logging');
64
+ const initLogger = logs.getInitLogger();
65
+ initLogger.info(LOG_MESSAGES.INIT_APPINFO_SEND, JSON.stringify(applicationInfoObject));
66
+
67
+ }
68
+
69
+ };
70
+
71
+ /**
72
+ * Sets the websocket client in agent
73
+ * and triggers the update event.
74
+ * @param {SecWebSocket} client
75
+ */
76
+ Agent.prototype.setClient = function setClient(client) {
77
+ this.client = client;
78
+ Agent.eventEmitter.emit(EVENTS.WS_UPDATE_EVENT, client);
79
+ };
80
+
81
+ /**
82
+ * Sets the websocket status in agent
83
+ * and triggers the update event.
84
+ * @param {ApplicationInfo} applicationInfoObject
85
+ */
86
+ Agent.prototype.setWebsocketStatus = function setWebsocketStatus(status) {
87
+ this.wsStatus = status;
88
+ Agent.eventEmitter.emit(EVENTS.WS_CONNECTION_STATUS_UPDATE_EVENT, status);
89
+ };
90
+
91
+ /**
92
+ * Sets the agent status in agent
93
+ * and triggers the update event.
94
+ * @param {ApplicationInfo} applicationInfoObject
95
+ */
96
+ Agent.prototype.setAgentStatus = function setAgentStatus(status) {
97
+ this.status = status;
98
+ Agent.eventEmitter.emit(EVENTS.AGENT_STATUS_UPDATED_EVENT, status);
99
+ };
100
+
101
+ Agent.EVENTS = EVENTS;
102
+
103
+ Agent.eventEmitter = new EventEmitter();
104
+
105
+ /**
106
+ * This initializes a singleton agent instance.
107
+ *
108
+ * @param {ApplicationInfo} applicationInfo
109
+ * @param {Logger} logger
110
+ * @param {WebSocket} client
111
+ * @param {AgentStatus} status
112
+ */
113
+ Agent.init = function init(applicationInfo, logger, client, status) {
114
+ if (!instance) {
115
+ instance = new Agent(applicationInfo, logger, client, status);
116
+ }
117
+ };
118
+
119
+ /**
120
+ * Returns the singleton agent instance.
121
+ * Returns null if init is not called.
122
+ *
123
+ * @returns {Agent} agent
124
+ */
125
+ Agent.getAgent = function getAgent() {
126
+ return instance;
127
+ };
128
+
129
+ Agent.setNRAgent = function setNRAgent(agent) {
130
+ if (agent) {
131
+ NRAgent = agent;
132
+ process.env.account_id = NRAgent.config.account_id;
133
+ process.env.agent_run_id = NRAgent.config.run_id;
134
+ }
135
+ const CSECAgent = Agent.getAgent();
136
+ if (CSECAgent) {
137
+ CSECAgent.applicationInfo.entityGuid = NRAgent.config.entity_guid;
138
+ CSECAgent.applicationInfo.linkingMetadata = LinkingMetaData.getLinkingMetadata();
139
+ CSECAgent.applicationInfo.linkingMetadata.agentRunId = NRAgent.config.run_id;
140
+ CSECAgent.setApplicationInfo(CSECAgent.applicationInfo);
141
+ }
142
+ };
143
+
144
+ module.exports = {
145
+ Agent
146
+ };