@contrast/agent 4.27.1 → 4.28.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.
@@ -20,11 +20,11 @@ const path = require('path');
20
20
 
21
21
  const HELP_MESSAGE = `
22
22
  Description:
23
- The read-contrast-config utility returns the current effective node agent configuration.
23
+ The config-diagnostics utility returns the current effective node agent configuration.
24
24
 
25
25
  Usage:
26
- npx -p @contrast/agent read-contrast-config <path/to/entry/script> [options]
27
- npx -p @contrast/agent read-contrast-config --help
26
+ npx -p @contrast/agent config-diagnostics <path/to/entry/script> [options]
27
+ npx -p @contrast/agent config-diagnostics --help
28
28
 
29
29
  Options:
30
30
  --quiet -q Prevents output of the report to the console.
package/lib/contrast.js CHANGED
@@ -26,6 +26,7 @@ const sourceMapUtility = require('./util/source-map');
26
26
  const loggerFactory = require('./core/logger');
27
27
  const logger = loggerFactory('contrast:contrast-init');
28
28
  const { outputAgentConfigFile } = require('./util/config-diagnostics-utils');
29
+ const { fetchSystemInfo, outputSystemInfo } = require('../system-diagnostics');
29
30
 
30
31
  function getAgentSnippet() {
31
32
  // getting reference to name every time for testing purposes
@@ -335,6 +336,9 @@ contrastAgent.bootstrap = function(args) {
335
336
  } catch (err) {
336
337
  outputAgentConfigFile(agent, options, args, err);
337
338
  }
339
+
340
+ const info = fetchSystemInfo();
341
+ outputSystemInfo({ skip: false, quiet: true, output: 'contrast_system_info.json' }, info);
338
342
  });
339
343
  };
340
344
 
@@ -93,9 +93,11 @@ Object.defineProperty(Function.prototype, '__isContrastHooked', {
93
93
  }
94
94
  });
95
95
 
96
- Function.prototype._contrast_toString = function() {
97
- return Reflect.apply(functionToString, this, arguments);
98
- };
96
+ Object.defineProperty(Function.prototype, '_contrast_toString', {
97
+ value: function _contrast_toString() {
98
+ return Reflect.apply(functionToString, this, arguments);
99
+ }
100
+ });
99
101
 
100
102
  function runHooks(type, data, thisTarget, fnHooks) {
101
103
  fnHooks.forEach((hook, key) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/agent",
3
- "version": "4.27.1",
3
+ "version": "4.28.0",
4
4
  "description": "Node.js security instrumentation by Contrast Security",
5
5
  "keywords": [
6
6
  "security",
@@ -55,7 +55,8 @@
55
55
  "node-contrast": "cli.js",
56
56
  "perf-logs": "perf-logs.js",
57
57
  "contrast-transpile": "cli-rewriter.js",
58
- "read-contrast-config": "config-diagnostics.js"
58
+ "config-diagnostics": "config-diagnostics.js",
59
+ "system-diagnostics": "system-diagnostics.js"
59
60
  },
60
61
  "files": [
61
62
  "bin/**",
@@ -66,7 +67,8 @@
66
67
  "esm.mjs",
67
68
  "perf-logs.js",
68
69
  "cli-rewriter.js",
69
- "config-diagnostics.js"
70
+ "config-diagnostics.js",
71
+ "system-diagnostics.js"
70
72
  ],
71
73
  "repository": {
72
74
  "type": "git"
@@ -0,0 +1,171 @@
1
+ #!/usr/bin/env node
2
+ /**
3
+ Copyright: 2022 Contrast Security, Inc
4
+ Contact: support@contrastsecurity.com
5
+ License: Commercial
6
+
7
+ NOTICE: This Software and the patented inventions embodied within may only be
8
+ used as part of Contrast Security’s commercial offerings. Even though it is
9
+ made available through public repositories, use of this Software is subject to
10
+ the applicable End User Licensing Agreement found at
11
+ https://www.contrastsecurity.com/enduser-terms-0317a or as otherwise agreed
12
+ between Contrast Security and the End User. The Software may not be reverse
13
+ engineered, modified, repackaged, sold, redistributed or otherwise used in a
14
+ way not consistent with the End User License Agreement.
15
+ */
16
+ 'use strict';
17
+
18
+ const path = require('path');
19
+ const fs = require('fs');
20
+ const os = require('os');
21
+ const pkg = require('./package');
22
+ const { setup } = require('./lib/core/config/util');
23
+ const logger = require('./lib/core/logger')('contrast:system-diagnostics');
24
+
25
+ const HELP_MESSAGE = `
26
+ Description:
27
+ The system-diagnostics utility returns the system info for the server/container the agent is running on.
28
+
29
+ Usage:
30
+ npx -p @contrast/agent system-diagnostics --help
31
+ npx -p @contrast/agent system-diagnostics [options]
32
+
33
+ Options:
34
+ --quiet -q Prevents output of the report to the console.
35
+ --output -o The directory to write the report in. Defaults to the current directory.
36
+ `;
37
+
38
+ function isContainer() {
39
+ try {
40
+ fs.statSync('/.dockerenv');
41
+ return true;
42
+ } catch (err) {
43
+ // if no docker env, check /proc/self/cgroup
44
+ }
45
+
46
+ try {
47
+ return fs.readFileSync('/proc/self/cgroup', 'utf8').includes('docker');
48
+ } catch (err) {
49
+ return false;
50
+ }
51
+ }
52
+
53
+ function isUsingPM2() {
54
+ const externalPkgPath =
55
+ process.env['npm_package_json'] || path.join(process.env['PWD'], 'package.json');
56
+ const externalPkg = require(externalPkgPath);
57
+ return { used: (process.env.pmx || false), version: externalPkg.dependencies.pm2 };
58
+ }
59
+
60
+ const diagnostics = {
61
+ parseArgv(argv) {
62
+ const args = {
63
+ help: false,
64
+ output: 'contrast_system_info.json',
65
+ quiet: false,
66
+ };
67
+
68
+ for (let i = 0; i < argv.length; i++) {
69
+ const arg = argv[i];
70
+
71
+ switch (arg) {
72
+ case '--help':
73
+ args.help = true;
74
+ return args;
75
+ case '--output':
76
+ case '-o': {
77
+ // grab the next value and skip in the next loop
78
+ const output = argv[++i];
79
+ if (!output) {
80
+ throw new Error('Expected a value to be provided after --output');
81
+ }
82
+ args.output = output;
83
+ break;
84
+ }
85
+ case '--quiet':
86
+ case '-q':
87
+ args.quiet = true;
88
+ break;
89
+ default:
90
+ if (!arg.startsWith('-')) {
91
+ args.entry = arg;
92
+ }
93
+ break;
94
+ }
95
+ }
96
+
97
+ return args;
98
+ },
99
+
100
+ outputSystemInfo(args, info) {
101
+ try {
102
+ fs.accessSync(path.join(args.output, '..'), fs.constants.RDWD_OK);
103
+ fs.writeFileSync(args.output, JSON.stringify(info, null, 2), 'utf-8');
104
+ } catch (err) {
105
+ console.log(`Couldn't create system info file: ${err}`);
106
+ }
107
+
108
+ if (!args.quiet) {
109
+ console.log(JSON.stringify(info, null, 2));
110
+ }
111
+ },
112
+
113
+ fetchSystemInfo() {
114
+ const yaml = setup({}, logger);
115
+
116
+ const info = {
117
+ ReportDate: new Date(),
118
+ MachineName: os.hostname(),
119
+ Contrast: {
120
+ Url: yaml.api.url,
121
+ Proxy: yaml.api.proxy,
122
+ Server: {
123
+ Name: yaml._flat['server.name'],
124
+ },
125
+ Agent: {
126
+ Name: '@contrast/agent',
127
+ Version: pkg.version,
128
+ },
129
+ },
130
+ Node: {
131
+ Version: process.version
132
+ },
133
+ PM2: isUsingPM2(),
134
+ OperatingSystem: {
135
+ Architecture: os.arch(),
136
+ Name: os.type(),
137
+ Version: os.release(),
138
+ KernelVersion: os.version(),
139
+ CPU: {
140
+ type: os.cpus()[0].model,
141
+ count: os.cpus().length,
142
+ }
143
+ },
144
+ Host: {
145
+ isDocker: isContainer(),
146
+ Memory: {
147
+ Total: (os.totalmem() / 1e6).toFixed(0).concat(' MB'),
148
+ Free: (os.freemem() / 1e6).toFixed(0).concat(' MB'),
149
+ Used: ((os.totalmem() - os.freemem()) / 1e6).toFixed(0).concat(' MB'),
150
+ }
151
+ },
152
+ };
153
+
154
+ return info;
155
+ }
156
+ };
157
+ module.exports = diagnostics;
158
+
159
+ // only run if this file is being executed as an entry script
160
+ // istanbul ignore next
161
+
162
+ if (require.main === module) {
163
+ const args = diagnostics.parseArgv(process.argv.slice(2));
164
+
165
+ if (args.help) {
166
+ console.log(HELP_MESSAGE);
167
+ } else {
168
+ const info = diagnostics.fetchSystemInfo();
169
+ diagnostics.outputSystemInfo(args, info);
170
+ }
171
+ }