@contrast/agentify 1.2.0 → 1.3.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.
- package/lib/index.js +42 -15
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -15,7 +15,10 @@
|
|
|
15
15
|
|
|
16
16
|
'use strict';
|
|
17
17
|
|
|
18
|
+
const fs = require('fs');
|
|
19
|
+
const path = require('path');
|
|
18
20
|
const Module = require('module');
|
|
21
|
+
const { env } = require('process');
|
|
19
22
|
|
|
20
23
|
const defaultOpts = {
|
|
21
24
|
install: true,
|
|
@@ -25,6 +28,7 @@ const defaultOpts = {
|
|
|
25
28
|
'deadzones',
|
|
26
29
|
'scopes',
|
|
27
30
|
'sources',
|
|
31
|
+
'architectureComponents',
|
|
28
32
|
'assess',
|
|
29
33
|
'protect',
|
|
30
34
|
'depHooks',
|
|
@@ -33,12 +37,12 @@ const defaultOpts = {
|
|
|
33
37
|
]
|
|
34
38
|
};
|
|
35
39
|
|
|
36
|
-
module.exports = function(
|
|
40
|
+
module.exports = function(core) {
|
|
37
41
|
// compose add'l local services
|
|
38
|
-
require('./sources')(
|
|
39
|
-
require('./contrast-methods')(
|
|
40
|
-
require('./function-hooks')(
|
|
41
|
-
require('./rewrite-hooks')(
|
|
42
|
+
require('./sources')(core);
|
|
43
|
+
require('./contrast-methods')(core);
|
|
44
|
+
require('./function-hooks')(core);
|
|
45
|
+
require('./rewrite-hooks')(core);
|
|
42
46
|
|
|
43
47
|
/**
|
|
44
48
|
* The interface is a function, which when called, will hook runMain
|
|
@@ -56,10 +60,10 @@ module.exports = function(deps) {
|
|
|
56
60
|
opts = { ...defaultOpts, ...opts };
|
|
57
61
|
}
|
|
58
62
|
|
|
59
|
-
return new Agent(
|
|
63
|
+
return new Agent(core, opts);
|
|
60
64
|
}
|
|
61
65
|
|
|
62
|
-
|
|
66
|
+
core.agentify = agentify;
|
|
63
67
|
|
|
64
68
|
return agentify;
|
|
65
69
|
};
|
|
@@ -67,16 +71,16 @@ module.exports = function(deps) {
|
|
|
67
71
|
class Agent {
|
|
68
72
|
/**
|
|
69
73
|
*
|
|
70
|
-
* @param {object}
|
|
74
|
+
* @param {object} core dependencies
|
|
71
75
|
* @param {object} opts
|
|
72
76
|
* @param {function} opts.preRunMain custom function for registering services
|
|
73
77
|
* @param {boolean} opts.install whether to automatically install
|
|
74
78
|
*/
|
|
75
|
-
constructor(
|
|
79
|
+
constructor(core, opts) {
|
|
76
80
|
const self = this;
|
|
77
|
-
const { config, logger } =
|
|
81
|
+
const { config, logger } = core;
|
|
78
82
|
|
|
79
|
-
this.
|
|
83
|
+
this.core = core;
|
|
80
84
|
this.opts = opts;
|
|
81
85
|
this.runMain = Module.runMain;
|
|
82
86
|
|
|
@@ -89,7 +93,7 @@ class Agent {
|
|
|
89
93
|
logger.info('Starting the Contrast agent');
|
|
90
94
|
logger.debug({ config }, 'Agent configuration');
|
|
91
95
|
|
|
92
|
-
const plugin = await opts.preRunMain(
|
|
96
|
+
const plugin = await opts.preRunMain(core);
|
|
93
97
|
|
|
94
98
|
if (opts.install) {
|
|
95
99
|
await self.install();
|
|
@@ -101,6 +105,8 @@ class Agent {
|
|
|
101
105
|
} catch (err) {
|
|
102
106
|
await self.handleInstallFailure(err);
|
|
103
107
|
}
|
|
108
|
+
|
|
109
|
+
self.logEffectiveConfig();
|
|
104
110
|
return self.runMain.apply(this, args);
|
|
105
111
|
};
|
|
106
112
|
}
|
|
@@ -113,7 +119,7 @@ class Agent {
|
|
|
113
119
|
* @param {error} err Error thrown during install
|
|
114
120
|
*/
|
|
115
121
|
async handleInstallFailure(err) {
|
|
116
|
-
this.
|
|
122
|
+
this.core.logger.error(
|
|
117
123
|
{ err },
|
|
118
124
|
'A fatal agent installation error has occurred. The application will be run without instrumentation.'
|
|
119
125
|
);
|
|
@@ -121,13 +127,34 @@ class Agent {
|
|
|
121
127
|
|
|
122
128
|
async install() {
|
|
123
129
|
for (const svcName of this.opts.svcList) {
|
|
124
|
-
|
|
125
|
-
const svc = this.deps[svcName];
|
|
130
|
+
const svc = this.core[svcName];
|
|
126
131
|
if (svc && svc.install) {
|
|
132
|
+
this.core.logger.trace('installing service: %s', svcName);
|
|
127
133
|
await svc.install();
|
|
128
134
|
}
|
|
129
135
|
}
|
|
130
136
|
}
|
|
137
|
+
|
|
138
|
+
logEffectiveConfig() {
|
|
139
|
+
const { config, getEffectiveConfig } = this.core;
|
|
140
|
+
|
|
141
|
+
if (env.CONTRAST__AGENT__DIAGNOSTICS__ENABLE !== 'false') {
|
|
142
|
+
const content = JSON.stringify(getEffectiveConfig(), null, 2).concat('\n\n');
|
|
143
|
+
|
|
144
|
+
if (env.CONTRAST__AGENT__DIAGNOSTICS__QUIET !== 'true') {
|
|
145
|
+
fs.writeFileSync(1, content, 'utf8');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
let outputDir = env.CONTRAST__AGENT__DIAGNOSTICS__REPORT_PATH;
|
|
149
|
+
if (!outputDir && config.agent.logger.path) {
|
|
150
|
+
outputDir = path.join(config.agent.logger.path, '../contrast_effective_config.json');
|
|
151
|
+
} else {
|
|
152
|
+
outputDir = path.join(__dirname, 'contrast_effective_config.json');
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
fs.writeFileSync(outputDir, content, 'utf-8');
|
|
156
|
+
}
|
|
157
|
+
}
|
|
131
158
|
}
|
|
132
159
|
|
|
133
160
|
module.exports.Agent = Agent;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@contrast/agentify",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "Configures Contrast agent services and instrumentation within an application",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE",
|
|
6
6
|
"author": "Contrast Security <nodejs@contrastsecurity.com> (https://www.contrastsecurity.com)",
|