dynamic-express-logging
Advanced tools
Comparing version 1.0.4 to 1.0.5
23
index.js
@@ -20,3 +20,3 @@ /** | ||
var maskObject = getMaskObject(req); | ||
if (maskObject) maskBody(incomingRequest.body); | ||
if (maskObject) maskBody(incomingRequest.body, maskObject.requestNodesToMask); | ||
customLogger.info(util.format("Start handling request %s:%s", incomingRequest.method, incomingRequest.originalUrl), {headers: JSON.stringify(maskingHeaders(maskObject, incomingRequest.headers))}, {body: JSON.stringify(incomingRequest.body)}); | ||
@@ -32,3 +32,3 @@ | ||
var maskObject = getMaskObject(req); | ||
if (maskObject && res.statusCode == 200) maskBody(responseBody); | ||
if (maskObject && res.statusCode == 200) maskBody(responseBody, maskObject.responseNodesToMask); | ||
customLogger.info(util.format("Finish handle %s:%s request", req.method, req.originalUrl), util.format("statusCode: %s", res.statusCode), util.format("headers: %j", maskingHeaders(maskObject, req.headers)), util.format("responseBody: %j", responseBody)); | ||
@@ -71,4 +71,8 @@ }; | ||
var maskBody = function (body) { | ||
if (body) { | ||
var maskBody = function (body, nodesToMask) { | ||
if (body && nodesToMask) { | ||
nodesToMask.forEach(function (nodeToMask) { | ||
body[nodeToMask] = maskString(body[nodeToMask]); | ||
}); | ||
} else if (body) { | ||
var keys = Object.keys(body); | ||
@@ -78,2 +82,4 @@ keys.forEach(function (key) { | ||
}); | ||
}else{ | ||
} | ||
@@ -83,6 +89,9 @@ }; | ||
function maskString(string) { | ||
for (var i = 1; i < string.length - 1; i++) { | ||
string = string.substr(0, i) + '*' + string.substr(i + 1); | ||
if(string){ | ||
for (var i = 1; i < string.length - 1; i++) { | ||
string = string.substr(0, i) + '*' + string.substr(i + 1); | ||
} | ||
return string | ||
} | ||
return string; | ||
return undefined; | ||
} |
{ | ||
"name": "dynamic-express-logging", | ||
"version": "1.0.4", | ||
"version": "1.0.5", | ||
"description": "express logging for incoming request and response", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -10,5 +10,7 @@ # express-logging | ||
* Logging every response | ||
* Option to mask all request/response body | ||
* Option to mask specific headers | ||
* You can use any logger you want | ||
* Option to mask all request/response body ( default masking all the request and response) | ||
* Option to mask specific request nodes (optional) | ||
* Option to mask specific response nodes (optional) | ||
* Option to mask specific headers (required) | ||
* You can use any logger you want ( required) | ||
@@ -38,3 +40,3 @@ `Please notice, in case that you want to mask a header from all your request, you have to add object in the JSON for every endpoint` | ||
``` | ||
#### JSON EXAMPLE | ||
#### JSON EXAMPLE 1 | ||
```json | ||
@@ -52,3 +54,45 @@ [ | ||
#### JSON EXAMPLE 2 | ||
```json | ||
[ | ||
{ | ||
"endPoint": "/v1/health", | ||
"method": "GET", | ||
"headers":[ | ||
"user-agent" | ||
], | ||
requestNodesToMask :["keyname1","keyname2] | ||
} | ||
] | ||
``` | ||
#### JSON EXAMPLE 3 | ||
```json | ||
[ | ||
{ | ||
"endPoint": "/v1/health", | ||
"method": "GET", | ||
"headers":[ | ||
"user-agent" | ||
], | ||
responseNodesToMask :["keyname1","keyname2] | ||
} | ||
] | ||
``` | ||
#### JSON EXAMPLE 4 | ||
```json | ||
[ | ||
{ | ||
"endPoint": "/v1/health", | ||
"method": "GET", | ||
"headers":[ | ||
"user-agent" | ||
], | ||
requestNodesToMask :["keyname1","keyname2], | ||
responseNodesToMask :["keyname1","keyname2] | ||
} | ||
] | ||
``` | ||
### Full example: | ||
@@ -55,0 +99,0 @@ ```node |
@@ -147,2 +147,281 @@ /** | ||
describe("When call it with object to be mask with specific request node to mask", function () { | ||
var sandbox; | ||
var requestObject; | ||
var responseObject; | ||
var expectedResponseBody; | ||
var stubLogger; | ||
var stubNext; | ||
var maskingObject; | ||
before(function () { | ||
sandbox = sinon.sandbox.create(); | ||
stubLogger = {info: sandbox.stub()}; | ||
requestObject = { | ||
method: "GET", | ||
originalUrl: "/v1/health", | ||
headers: {"first": "value", "second": "value"}, | ||
body: {"key1": "value","key2":"value","key3":"value"} | ||
}; | ||
responseObject = { | ||
statusCode: 200, | ||
end: sandbox.stub() | ||
}; | ||
expectedResponseBody = JSON.stringify({ | ||
"firstKey": "firstValue", | ||
"secondKey": "secondValue" | ||
}); | ||
maskingObject = [ | ||
{ | ||
"endPoint": "/v1/health", | ||
"method": "GET", | ||
"headers": [ | ||
"first" | ||
], | ||
requestNodesToMask:["key1","key3"] | ||
} | ||
]; | ||
index.init(stubLogger, maskingObject); | ||
stubNext = sandbox.stub(); | ||
index.requestMiddleware(requestObject, responseObject, stubNext); | ||
responseObject.end(expectedResponseBody, "UTF-8"); | ||
}); | ||
it("Should mask all the body and relevant headers", function () { | ||
stubLogger.info.withArgs(util.format("Start handling request %s:%s", requestObject.method, requestObject.originalUrl), | ||
{headers: JSON.stringify({"first": "v***e", "second": "value"})}, | ||
{body: JSON.stringify({"key1": "v***e","key2":"value","key3":"v***e"})}).calledOnce.should.equal(true); | ||
}); | ||
it("Should mask response body and relevant headers", function () { | ||
stubLogger.info.withArgs(util.format("Finish handle %s:%s request", requestObject.method, requestObject.originalUrl), | ||
util.format("statusCode: %s", responseObject.statusCode), | ||
util.format("headers: %j", {"first": "v***e", "second": "value"}), | ||
util.format("responseBody: %s", JSON.stringify({ | ||
"firstKey": "f********e", | ||
"secondKey": "s*********e" | ||
}))).calledOnce.should.equal(true); | ||
}); | ||
it("Should call next", function () { | ||
stubNext.withArgs().calledOnce.should.equal(true); | ||
}) | ||
}); | ||
describe("When call it with object to be mask with specific response node to mask", function () { | ||
var sandbox; | ||
var requestObject; | ||
var responseObject; | ||
var expectedResponseBody; | ||
var stubLogger; | ||
var stubNext; | ||
var maskingObject; | ||
before(function () { | ||
sandbox = sinon.sandbox.create(); | ||
stubLogger = {info: sandbox.stub()}; | ||
requestObject = { | ||
method: "GET", | ||
originalUrl: "/v1/health", | ||
headers: {"first": "value", "second": "value"}, | ||
body: {"key1": "value","key2":"value","key3":"value"} | ||
}; | ||
responseObject = { | ||
statusCode: 200, | ||
end: sandbox.stub() | ||
}; | ||
expectedResponseBody = JSON.stringify({ | ||
"firstKey": "firstValue", | ||
"secondKey": "secondValue" | ||
}); | ||
maskingObject = [ | ||
{ | ||
"endPoint": "/v1/health", | ||
"method": "GET", | ||
"headers": [ | ||
"first" | ||
], | ||
responseNodesToMask:["firstKey"] | ||
} | ||
]; | ||
index.init(stubLogger, maskingObject); | ||
stubNext = sandbox.stub(); | ||
index.requestMiddleware(requestObject, responseObject, stubNext); | ||
responseObject.end(expectedResponseBody, "UTF-8"); | ||
}); | ||
it("Should mask all the body and relevant headers", function () { | ||
stubLogger.info.withArgs(util.format("Start handling request %s:%s", requestObject.method, requestObject.originalUrl), | ||
{headers: JSON.stringify({"first": "v***e", "second": "value"})}, | ||
{body: JSON.stringify({"key1": "v***e","key2":"v***e","key3":"v***e"})}).calledOnce.should.equal(true); | ||
}); | ||
it("Should mask response body and relevant headers", function () { | ||
stubLogger.info.withArgs(util.format("Finish handle %s:%s request", requestObject.method, requestObject.originalUrl), | ||
util.format("statusCode: %s", responseObject.statusCode), | ||
util.format("headers: %j", {"first": "v***e", "second": "value"}), | ||
util.format("responseBody: %s", JSON.stringify({ | ||
"firstKey": "f********e", | ||
"secondKey": "secondValue" | ||
}))).calledOnce.should.equal(true); | ||
}); | ||
it("Should call next", function () { | ||
stubNext.withArgs().calledOnce.should.equal(true); | ||
}) | ||
}); | ||
describe("When call it with object to be mask with specific response & request nodes to mask", function () { | ||
var sandbox; | ||
var requestObject; | ||
var responseObject; | ||
var expectedResponseBody; | ||
var stubLogger; | ||
var stubNext; | ||
var maskingObject; | ||
before(function () { | ||
sandbox = sinon.sandbox.create(); | ||
stubLogger = {info: sandbox.stub()}; | ||
requestObject = { | ||
method: "GET", | ||
originalUrl: "/v1/health", | ||
headers: {"first": "value", "second": "value"}, | ||
body: {"key1": "value","key2":"value","key3":"value"} | ||
}; | ||
responseObject = { | ||
statusCode: 200, | ||
end: sandbox.stub() | ||
}; | ||
expectedResponseBody = JSON.stringify({ | ||
"firstKey": "firstValue", | ||
"secondKey": "secondValue" | ||
}); | ||
maskingObject = [ | ||
{ | ||
"endPoint": "/v1/health", | ||
"method": "GET", | ||
"headers": [ | ||
"first" | ||
], | ||
responseNodesToMask:["firstKey"], | ||
requestNodesToMask:["key1"] | ||
} | ||
]; | ||
index.init(stubLogger, maskingObject); | ||
stubNext = sandbox.stub(); | ||
index.requestMiddleware(requestObject, responseObject, stubNext); | ||
responseObject.end(expectedResponseBody, "UTF-8"); | ||
}); | ||
it("Should mask all the body and relevant headers", function () { | ||
stubLogger.info.withArgs(util.format("Start handling request %s:%s", requestObject.method, requestObject.originalUrl), | ||
{headers: JSON.stringify({"first": "v***e", "second": "value"})}, | ||
{body: JSON.stringify({"key1": "v***e","key2":"value","key3":"value"})}).calledOnce.should.equal(true); | ||
}); | ||
it("Should mask response body and relevant headers", function () { | ||
stubLogger.info.withArgs(util.format("Finish handle %s:%s request", requestObject.method, requestObject.originalUrl), | ||
util.format("statusCode: %s", responseObject.statusCode), | ||
util.format("headers: %j", {"first": "v***e", "second": "value"}), | ||
util.format("responseBody: %s", JSON.stringify({ | ||
"firstKey": "f********e", | ||
"secondKey": "secondValue" | ||
}))).calledOnce.should.equal(true); | ||
}); | ||
it("Should call next", function () { | ||
stubNext.withArgs().calledOnce.should.equal(true); | ||
}) | ||
}); | ||
describe("When call it with object to be mask without requestBody", function () { | ||
var sandbox; | ||
var requestObject; | ||
var responseObject; | ||
var expectedResponseBody; | ||
var stubLogger; | ||
var stubNext; | ||
var maskingObject; | ||
before(function () { | ||
sandbox = sinon.sandbox.create(); | ||
stubLogger = {info: sandbox.stub()}; | ||
requestObject = { | ||
method: "GET", | ||
originalUrl: "/v1/health", | ||
headers: {"first": "value", "second": "value"} | ||
}; | ||
responseObject = { | ||
statusCode: 200, | ||
end: sandbox.stub() | ||
}; | ||
expectedResponseBody = JSON.stringify({ | ||
"firstKey": "firstValue", | ||
"secondKey": "secondValue" | ||
}); | ||
maskingObject = [ | ||
{ | ||
"endPoint": "/v1/health", | ||
"method": "GET", | ||
"headers": [ | ||
"first" | ||
], | ||
responseNodesToMask:["firstKey"], | ||
requestNodesToMask:["key1"] | ||
} | ||
]; | ||
index.init(stubLogger, maskingObject); | ||
stubNext = sandbox.stub(); | ||
index.requestMiddleware(requestObject, responseObject, stubNext); | ||
responseObject.end(expectedResponseBody, "UTF-8"); | ||
}); | ||
it("Should mask all the body and relevant headers", function () { | ||
stubLogger.info.withArgs(util.format("Start handling request %s:%s", requestObject.method, requestObject.originalUrl), | ||
{headers: JSON.stringify({"first": "v***e", "second": "value"})}, | ||
{body: undefined}).calledOnce.should.equal(true); | ||
}); | ||
it("Should mask response body and relevant headers", function () { | ||
stubLogger.info.withArgs(util.format("Finish handle %s:%s request", requestObject.method, requestObject.originalUrl), | ||
util.format("statusCode: %s", responseObject.statusCode), | ||
util.format("headers: %j", {"first": "v***e", "second": "value"}), | ||
util.format("responseBody: %s", JSON.stringify({ | ||
"firstKey": "f********e", | ||
"secondKey": "secondValue" | ||
}))).calledOnce.should.equal(true); | ||
}); | ||
it("Should call next", function () { | ||
stubNext.withArgs().calledOnce.should.equal(true); | ||
}) | ||
}); | ||
describe("When call it with object to be mask , but call it with different api", function () { | ||
@@ -210,3 +489,3 @@ var sandbox; | ||
}) | ||
}) | ||
}); | ||
@@ -213,0 +492,0 @@ describe("When call it with object to be mask , but call it with different method", function () { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29680
550
112