-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from pedrobranco/feature/add-request-logging
Add logging to all requests
- Loading branch information
Showing
7 changed files
with
454 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
import { obfuscate } from './request-obfuscator'; | ||
import request from 'request'; | ||
import requestLogger from '@uphold/request-logger'; | ||
|
||
/** | ||
* Exports. | ||
*/ | ||
|
||
export default logger => requestLogger(request, (request, instance) => { | ||
obfuscate(request, instance); | ||
|
||
if (request.type === 'response') { | ||
return logger.debug({ request }, `Received response for request ${request.id}`); | ||
} | ||
|
||
return logger.debug({ request }, `Making request ${request.id} to ${request.method} ${request.uri}`); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
import methods from '../methods'; | ||
import { defaults, get, has, isArray, isEmpty, isString, map, mapKeys } from 'lodash'; | ||
|
||
/** | ||
* Map all methods to lowercase. | ||
*/ | ||
|
||
const lowercaseMethods = mapKeys(methods, (value, key) => key.toLowerCase()); | ||
|
||
/** | ||
* Obfuscate the response body. | ||
*/ | ||
|
||
function obfuscateResponseBody(body, method) { | ||
const fn = get(lowercaseMethods[method], 'obfuscate.response'); | ||
|
||
if (!fn || isEmpty(body.result)) { | ||
return body; | ||
} | ||
|
||
return defaults({ result: fn(body.result) }, body); | ||
} | ||
|
||
/** | ||
* Obfuscate the response. | ||
*/ | ||
|
||
function obfuscateResponse(request, instance) { | ||
if (!get(request, 'response.body')) { | ||
return; | ||
} | ||
|
||
if (get(request, `response.headers['content-type']`) === 'application/octet-stream') { | ||
request.response.body = '******'; | ||
|
||
return; | ||
} | ||
|
||
if (!instance.body) { | ||
return; | ||
} | ||
|
||
const requestBody = JSON.parse(instance.body); | ||
|
||
if (isArray(request.response.body)) { | ||
const methodsById = mapKeys(requestBody, method => method.id); | ||
|
||
request.response.body = map(request.response.body, request => obfuscateResponseBody(request, methodsById[request.id].method)); | ||
|
||
return; | ||
} | ||
|
||
request.response.body = obfuscateResponseBody(request.response.body, requestBody.method); | ||
} | ||
|
||
/** | ||
* Obfuscate the request body. | ||
*/ | ||
|
||
function obfuscateRequestBody(body) { | ||
const method = get(lowercaseMethods[body.method], 'obfuscate.request'); | ||
|
||
if (!method) { | ||
return body; | ||
} | ||
|
||
body.params = method(body.params); | ||
|
||
return body; | ||
} | ||
|
||
/** | ||
* Obfuscate the request. | ||
*/ | ||
|
||
function obfuscateRequest(request) { | ||
if (!isString(request.body)) { | ||
return; | ||
} | ||
|
||
request.body = JSON.parse(request.body); | ||
|
||
if (isArray(request.body)) { | ||
request.body = map(request.body, obfuscateRequestBody); | ||
} else { | ||
request.body = obfuscateRequestBody(request.body); | ||
} | ||
|
||
request.body = JSON.stringify(request.body); | ||
} | ||
|
||
/** | ||
* Obfuscate headers. | ||
*/ | ||
|
||
function obfuscateHeaders(request) { | ||
if (!has(request, 'headers.authorization')) { | ||
return; | ||
} | ||
|
||
request.headers.authorization = request.headers.authorization.replace(/(Basic )(.*)/, `$1******`); | ||
} | ||
|
||
/** | ||
* Export `RequestObfuscator`. | ||
*/ | ||
|
||
export function obfuscate(request, instance) { | ||
obfuscateHeaders(request); | ||
obfuscateRequest(request); | ||
obfuscateResponse(request, instance); | ||
} |
Oops, something went wrong.