Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PROD-54189 Fix regex configuration via environment variables #113

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/adapters/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ function requestProxy(options = {}) {
reqId = requestOptions.headers[traceHeaderName];

reqObj = mapOutReq(requestOptions, reqId, opts);
const blacklistedPathRegex = opts.blacklistedPathRegex;
const blacklistedPathRegex =
typeof opts.blacklistedPathRegex === 'string'
? new RegExp(opts.blacklistedPathRegex, 'gi')
: opts.blacklistedPathRegex;
if (!reqObj.protocol || (blacklistedPathRegex && blacklistedPathRegex.test(reqObj.path))) {
// do not log custom requests (for example requests started by the newrelic agent)
return target.apply(thisArg, argumentsList);
Expand Down
2 changes: 1 addition & 1 deletion lib/transformers/transformers.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ const mapOutReq = (requestOptions, reqId, opts = {}) => {
if (opts.bodyKeysCallback && typeof opts.bodyKeysCallback === 'function') {
picked = opts.bodyKeysCallback(picked);
} else if (opts.bodyKeysRegex) {
const REGEX = opts.bodyKeysRegex;
const REGEX = typeof opts.bodyKeysRegex === 'string' ? new RegExp(opts.bodyKeysRegex, 'i') : opts.bodyKeysRegex;
picked = pickBy(picked, (_, key) => REGEX.test(key));
} else {
picked = pick(picked, opts.bodyKeys);
Expand Down
134 changes: 92 additions & 42 deletions test/lib/adapters/default/requestProxyTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,50 +164,100 @@ describe('#defaultAdapter', () => {
logger.info.callCount.should.eql(0);
});

it('should pass but not log when path is in blacklisted paths', () => {
const logger = {
info: sandbox.spy()
};
const serialize = a => a;
const traceHeaderName = 'test';
const requestProxy = defaultAdapter.requestProxy({
logger,
serialize,
traceHeaderName,
level: 'info',
opts: {
blacklistedPathRegex: new RegExp('^/v4.0/traces$'),
request: {
enabled: true
context('when path is in blacklisted paths', () => {
it('should pass but not log when path is in blacklisted paths', () => {
const logger = {
info: sandbox.spy()
};
const serialize = a => a;
const traceHeaderName = 'test';
const requestProxy = defaultAdapter.requestProxy({
logger,
serialize,
traceHeaderName,
level: 'info',
opts: {
blacklistedPathRegex: new RegExp('^/v4.0/traces$'),
request: {
enabled: true
},
hostFieldName: 'myHost'
}
});
const options = {
method: 'PUT',
port: '8080',
headers: {
test: 'ok'
},
hostFieldName: 'myHost'
}
protocol: 'https:',
path: '/v4.0/traces',
pathname: '/v4.0/traces',
hostname: 'test-host'
};
const http = {
request: () => {
return {
on: (event, fn) => {
fn({
statusCode: 200
});
}
};
}
};
http.request = new Proxy(http.request, requestProxy);
http.request(options);
logger.info.callCount.should.eql(0);
});

it('should convert a string blacklistedPathRegex to RegExp', () => {
const logger = {
info: sandbox.spy(),
error: sandbox.spy()
};
const serialize = a => a;
const traceHeaderName = 'test';
const requestProxy = defaultAdapter.requestProxy({
logger,
serialize,
traceHeaderName,
level: 'info',
opts: {
blacklistedPathRegex: '^/v4.0/trac.*',
request: {
enabled: true
},
hostFieldName: 'myHost'
}
});
const options = {
method: 'PUT',
port: '8080',
headers: {
test: 'ok'
},
protocol: 'https:',
path: '/v4.0/traces',
pathname: '/v4.0/traces',
hostname: 'test-host'
};
const http = {
request: () => {
return {
on: (event, fn) => {
fn({
statusCode: 200
});
}
};
}
};
http.request = new Proxy(http.request, requestProxy);
http.request(options);
logger.info.callCount.should.eql(0);
logger.error.callCount.should.eql(0);
});
const options = {
method: 'PUT',
port: '8080',
headers: {
test: 'ok'
},
protocol: 'https:',
path: '/v4.0/traces',
pathname: '/v4.0/traces',
hostname: 'test-host'
};
const http = {
request: () => {
return {
on: (event, fn) => {
fn({
statusCode: 200
});
}
};
}
};
http.request = new Proxy(http.request, requestProxy);
http.request(options);
logger.info.callCount.should.eql(0);
});

it('should pass when incomingMessage.url does not exist', () => {
Expand Down
41 changes: 41 additions & 0 deletions test/lib/transformers/transformersTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,45 @@ describe('mapOutReq', () => {
log_tag: 'outbound_request'
});
});

it('should convert a string bodyKeysRegex to RegExp', () => {
const inMsg = {
method: 'POST',
body: '{ "bananas": "bar", "banana1": "apple1", "banana2": "apple2" }',
headers: {
'Content-type': 'application/json; charset=UTF-8'
},
uri: {
protocol: 'http:',
hostname: 'hostname',
pathname: 'path',
query: 'query'
}
};

const options = {
bodyKeysRegex: '^banana\\d+'
};

const result = mapOutReq(inMsg, undefined, options);
result.should.eql({
method: 'POST',
protocol: 'http',
host: 'hostname',
port: undefined,
path: 'path',
query: 'query',
href: 'http://hostname/path',
requestId: undefined,
contentLength: 0,
log_tag: 'outbound_request',
metaHeaders: {},
metaBody: {
body: {
banana1: 'apple1',
banana2: 'apple2'
}
}
});
});
});