@contrast/agentify 1.3.0 → 1.3.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/lib/index.js CHANGED
@@ -18,7 +18,6 @@
18
18
  const fs = require('fs');
19
19
  const path = require('path');
20
20
  const Module = require('module');
21
- const { env } = require('process');
22
21
 
23
22
  const defaultOpts = {
24
23
  install: true,
@@ -32,8 +31,9 @@ const defaultOpts = {
32
31
  'assess',
33
32
  'protect',
34
33
  'depHooks',
34
+ 'enhancedLibraryUsage',
35
35
  'rewriteHooks',
36
- 'functionHooks',
36
+ 'functionHooks'
37
37
  ]
38
38
  };
39
39
 
@@ -138,21 +138,28 @@ class Agent {
138
138
  logEffectiveConfig() {
139
139
  const { config, getEffectiveConfig } = this.core;
140
140
 
141
- if (env.CONTRAST__AGENT__DIAGNOSTICS__ENABLE !== 'false') {
141
+ if (config._flat['agent.diagnostics.enable'] !== false) {
142
142
  const content = JSON.stringify(getEffectiveConfig(), null, 2).concat('\n\n');
143
143
 
144
- if (env.CONTRAST__AGENT__DIAGNOSTICS__QUIET !== 'true') {
144
+ if (config._flat['agent.diagnostics.quiet'] !== true) {
145
145
  fs.writeFileSync(1, content, 'utf8');
146
146
  }
147
147
 
148
- let outputDir = env.CONTRAST__AGENT__DIAGNOSTICS__REPORT_PATH;
148
+ let outputDir = config._flat['agent.diagnostics.report_path'];
149
149
  if (!outputDir && config.agent.logger.path) {
150
150
  outputDir = path.join(config.agent.logger.path, '../contrast_effective_config.json');
151
- } else {
152
- outputDir = path.join(__dirname, 'contrast_effective_config.json');
153
151
  }
154
152
 
155
- fs.writeFileSync(outputDir, content, 'utf-8');
153
+ try {
154
+ fs.writeFileSync(outputDir, content, 'utf-8');
155
+ } catch (err) {
156
+ outputDir = path.join(process.cwd(), 'contrast_effective_config.json');
157
+ try {
158
+ fs.writeFileSync(outputDir, content, 'utf-8');
159
+ } catch (err) {
160
+ fs.writeFileSync(1, `Couldn't create effective config file: ${err}`, 'utf-8');
161
+ }
162
+ }
156
163
  }
157
164
  }
158
165
  }
package/lib/sources.js CHANGED
@@ -14,73 +14,66 @@
14
14
  */
15
15
 
16
16
  'use strict';
17
+ const patchType = 'http-sources';
17
18
 
18
19
  module.exports = function(core) {
19
- const { depHooks, patcher, logger, scopes: { sources: sourcesStorage } } = core;
20
- const sources = core.sources = {};
20
+ const {
21
+ instrumentation: { instrument },
22
+ scopes: { sources: sourcesStorage }
23
+ } = core;
21
24
 
22
- sources.install = function install() {
23
- depHooks.resolve({ name: 'http' }, http => patchServerEmit(http, 'httpServer'));
24
- depHooks.resolve({ name: 'https' }, https => patchServerEmit(https, 'httpsServer'));
25
- depHooks.resolve({ name: 'http2' }, http2 => {
26
- patchCreateServer(http2, 'http2Server');
27
- patchCreateServer(http2, 'http2SecureServer', 'createSecureServer');
28
- });
29
- depHooks.resolve({ name: 'spdy' }, spdy => patchServerEmit(spdy, 'spdyServer'));
30
- };
31
-
32
- function patchServerEmit(serverSource, sourceName) {
33
- serverSource.Server.prototype = patcher.patch(serverSource.Server.prototype, 'emit', {
34
- name: `${sourceName}.Server.prototype.emit`,
35
- patchType: 'http-sources',
36
- around: emitAroundHook(sourceName)
37
- });
38
- }
39
-
40
- function patchCreateServer(serverSource, sourceName, constructorName = 'createServer') {
41
- patcher.patch(serverSource, constructorName, {
42
- name: sourceName,
43
- patchType: 'http-sources',
44
- post: createServerPostHook(sourceName)
45
- });
46
- }
47
-
48
- function createServerPostHook(serverType) {
49
- return function(data) {
50
- const { result: server } = data;
51
- const serverPrototype = server ? Object.getPrototypeOf(server) : null;
52
-
53
- if (!serverPrototype) {
54
- logger.error('Unable to patch server prototype, continue without instrumentation');
55
- return;
56
- }
57
-
58
- patcher.patch(serverPrototype, 'emit', {
59
- name: 'Server.prototype.emit',
60
- patchType: 'req-async-storage',
61
- around: emitAroundHook(serverType)
62
- });
63
- };
64
- }
65
-
66
- function emitAroundHook(serverType) {
67
- return function emitAroundHook(next, data) {
25
+ function aroundHook(serverType) {
26
+ return function around(next, data) {
68
27
  const { args: [event] } = data;
69
-
70
28
  if (event !== 'request') return next();
71
-
72
29
  const store = { serverType };
73
-
74
30
  return sourcesStorage.run(store, next);
75
31
  };
76
32
  }
77
33
 
78
- return {
79
- install: sources.install,
80
- functions: {
81
- patchCreateServer,
82
- createServerPostHook,
83
- emitAroundHook
84
- }
34
+ function install() {
35
+ [{
36
+ moduleName: 'http'
37
+ },
38
+ {
39
+ moduleName: 'https'
40
+ },
41
+ {
42
+ moduleName: 'spdy'
43
+ },
44
+ {
45
+ moduleName: 'http2',
46
+ patchObjectsProps: [
47
+ {
48
+ methods: ['createServer', 'createSecureServer'],
49
+ patchType,
50
+ patchObjects: [
51
+ {
52
+ name: 'Server.prototype',
53
+ methods: ['emit'],
54
+ patchType,
55
+ around: aroundHook('http2')
56
+ }
57
+ ]
58
+ }
59
+ ]
60
+ }].forEach(({ moduleName, patchObjectsProps }) => {
61
+ const patchObjects = patchObjectsProps || [
62
+ {
63
+ name: 'Server.prototype',
64
+ methods: ['emit'],
65
+ patchType,
66
+ around: aroundHook(moduleName)
67
+ }
68
+ ];
69
+ instrument({
70
+ moduleName,
71
+ patchObjects
72
+ });
73
+ });
74
+ }
75
+ return core.sources = {
76
+ install,
77
+ aroundHook
85
78
  };
86
79
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrast/agentify",
3
- "version": "1.3.0",
3
+ "version": "1.3.1",
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)",
@@ -16,4 +16,4 @@
16
16
  "scripts": {
17
17
  "test": "../scripts/test.sh"
18
18
  }
19
- }
19
+ }