@contrast/telemetry 1.1.0

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 (3) hide show
  1. package/LICENSE +12 -0
  2. package/lib/index.js +152 -0
  3. package/package.json +23 -0
package/LICENSE ADDED
@@ -0,0 +1,12 @@
1
+ Copyright: 2023 Contrast Security, Inc
2
+ Contact: support@contrastsecurity.com
3
+ License: Commercial
4
+
5
+ NOTICE: This Software and the patented inventions embodied within may only be
6
+ used as part of Contrast Security’s commercial offerings. Even though it is
7
+ made available through public repositories, use of this Software is subject to
8
+ the applicable End User Licensing Agreement found at
9
+ https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
10
+ between Contrast Security and the End User. The Software may not be reverse
11
+ engineered, modified, repackaged, sold, redistributed or otherwise used in a
12
+ way not consistent with the End User License Agreement.
package/lib/index.js ADDED
@@ -0,0 +1,152 @@
1
+ /*
2
+ * Copyright: 2023 Contrast Security, Inc
3
+ * Contact: support@contrastsecurity.com
4
+ * License: Commercial
5
+
6
+ * NOTICE: This Software and the patented inventions embodied within may only be
7
+ * used as part of Contrast Security’s commercial offerings. Even though it is
8
+ * made available through public repositories, use of this Software is subject to
9
+ * the applicable End User Licensing Agreement found at
10
+ * https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
11
+ * between Contrast Security and the End User. The Software may not be reverse
12
+ * engineered, modified, repackaged, sold, redistributed or otherwise used in a
13
+ * way not consistent with the End User License Agreement.
14
+ */
15
+
16
+ 'use strict';
17
+
18
+ const { createHash, randomUUID } = require('crypto');
19
+ const os = require('os');
20
+ const process = require('process');
21
+ const { default: axios } = require('axios');
22
+ const { default: getMac } = require('getmac');
23
+ const { Event } = require('@contrast/common');
24
+
25
+ const DISCLAIMER = `The Contrast Node Agent collects usage data in order to help us improve compatibility and security coverage.
26
+ The data is anonymous and does not contain application data. It is collected by Contrast and is never shared.
27
+ You can opt-out of telemetry by setting the CONTRAST_AGENT_TELEMETRY_OPTOUT environment variable to '1' or 'true'.
28
+ `;
29
+ const TELEMETRY_URL = 'https://telemetry.nodejs.contrastsecurity.com/';
30
+ const TELEMETRY_VERSION = 'v0'; // TODO: set this to v1 when format is established.
31
+
32
+ /**
33
+ * @param {{
34
+ * agentName: string,
35
+ * agentVersion: string,
36
+ * config: import('@contrast/config').Config,
37
+ * logger: import('@contrast/logger').Logger,
38
+ * messages: import('@contrast/common').Messages,
39
+ * }} core
40
+ * @returns {import('@contrast/common').Installable}
41
+ */
42
+ module.exports = function(core) {
43
+ const {
44
+ agentName,
45
+ agentVersion,
46
+ appInfo,
47
+ config,
48
+ logger,
49
+ messages,
50
+ } = core;
51
+
52
+ const { instanceId, applicationId } = getIds(appInfo);
53
+
54
+ const telemetry = core.telemetry = {
55
+ client: null,
56
+ instanceId,
57
+ tags: {
58
+ applicationId,
59
+ isCustomerEnv: true,
60
+ osArch: appInfo.os.architecture,
61
+ osPlatform: appInfo.os.platform,
62
+ osRelease: appInfo.os.release,
63
+ isContainer: !!appInfo.containerId,
64
+ agent: agentName,
65
+ agentVersion,
66
+ isAssess: config.getEffectiveValue('assess.enable'),
67
+ isProtect: config.getEffectiveValue('protect.enable'),
68
+ nodeVersion: appInfo.node_version,
69
+ nodeVersionMajor: appInfo.nodeMajorVersion,
70
+ telemetryVersion: TELEMETRY_VERSION
71
+ },
72
+ isEnabled() {
73
+ return (
74
+ process.env.CONTRAST_AGENT_TELEMETRY_OPTOUT !== 'true' &&
75
+ process.env.CONTRAST_AGENT_TELEMETRY_OPTOUT !== '1'
76
+ );
77
+ },
78
+ install() {
79
+ if (!telemetry.isEnabled()) {
80
+ logger.info('Telemetry opt-out in effect. All usage data collection is suppressed.');
81
+ return;
82
+ }
83
+
84
+ logger.info(DISCLAIMER);
85
+
86
+ telemetry.client = axios.create({
87
+ baseURL: new URL('/api/v1/telemetry/metrics/node', TELEMETRY_URL).href,
88
+ headers: {
89
+ 'User-Agent': `${agentName}/${agentVersion}`,
90
+ }
91
+ });
92
+
93
+ telemetry.startup();
94
+ },
95
+
96
+ async startup() {
97
+ try {
98
+ const { external, heapTotal, heapUsed, rss } = process.memoryUsage();
99
+ await telemetry.client.post('/startup', [
100
+ {
101
+ timestamp: new Date().toISOString(),
102
+ instance: telemetry.instanceId,
103
+ tags: telemetry.tags,
104
+ fields: {
105
+ numCpus: os.cpus().length,
106
+ memTotal: os.totalmem(),
107
+ memHeapTotal: heapTotal,
108
+ memHeapUsed: heapUsed,
109
+ memExternal: external,
110
+ memRss: rss,
111
+ uptime: process.uptime()
112
+ }
113
+ }
114
+ ]);
115
+ } catch (err) {
116
+ logger.error({ err }, 'error occurred while reporting telemetry: /startup');
117
+ }
118
+ },
119
+ };
120
+
121
+ messages.on(Event.SERVER_SETTINGS_UPDATE, () => {
122
+ telemetry.tags.isAssess = config.getEffectiveValue('assess.enable');
123
+ telemetry.tags.isProtect = config.getEffectiveValue('protect.enable');
124
+ });
125
+
126
+ return core.telemetry;
127
+ };
128
+
129
+ module.exports.getIds = getIds;
130
+
131
+ function getIds (appInfo) {
132
+ let applicationId, instanceId;
133
+
134
+ try {
135
+ const hash = createHash('sha256').update(getMac());
136
+ if (appInfo.containerId) hash.update(appInfo.containerId);
137
+ instanceId = hash.copy().digest('hex');
138
+ applicationId = hash.update(appInfo.name).digest('hex');
139
+ } catch {
140
+ // If getMac fails we fall back to generating a UUID. "Unstable"
141
+ // identifiers such as these are prefixed with an underscore.
142
+ let id = randomUUID();
143
+ if (appInfo.containerId) {
144
+ id += `_${appInfo.containerId}`;
145
+ }
146
+ applicationId = instanceId = `_${id}`;
147
+ }
148
+
149
+ return { applicationId, instanceId };
150
+ }
151
+
152
+ module.exports.DISCLAIMER = DISCLAIMER;
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "@contrast/telemetry",
3
+ "version": "1.1.0",
4
+ "description": "Telemetry reporting for the Contrast Node.js agent.",
5
+ "license": "SEE LICENSE IN LICENSE",
6
+ "author": "Contrast Security <nodejs@contrastsecurity.com> (https://www.contrastsecurity.com)",
7
+ "main": "lib/index.js",
8
+ "engines": {
9
+ "npm": ">=6.13.7 <7 || >=8.3.1",
10
+ "node": ">=14.15.0"
11
+ },
12
+ "scripts": {
13
+ "test": "../scripts/test.sh"
14
+ },
15
+ "dependencies": {
16
+ "@contrast/common": "1.16.0",
17
+ "@contrast/config": "1.22.0",
18
+ "@contrast/core": "1.27.0",
19
+ "@contrast/logger": "1.7.0",
20
+ "axios": "^1.6.0",
21
+ "getmac": "^6.3.0"
22
+ }
23
+ }