generated from UK-Export-Finance/nestjs-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(APIM-344): changed redaction in crashed bootstrap and changed re…
…daction in recovered bootstrap
- Loading branch information
Showing
7 changed files
with
142 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import { REDACT_STRING_PATHS } from '@ukef/constants'; | ||
import { RandomValueGenerator } from '@ukef-test/support/generator/random-value-generator'; | ||
|
||
import { redactStringsInLogArgs } from './redact-strings-in-log-args.helper'; | ||
|
||
describe('Redact errors helper', () => { | ||
const valueGenerator = new RandomValueGenerator(); | ||
|
||
describe('redactStringsInLogArgs', () => { | ||
const domain = valueGenerator.httpsUrl(); | ||
const otherSensitivefield = valueGenerator.word(); | ||
const message = `ConnectionError: Failed to connect to ${domain}, ${otherSensitivefield}`; | ||
const redactedMessage = `ConnectionError: Failed to connect to [RedactedDomain], [Redacted]`; | ||
const redactStrings = [ | ||
{ searchValue: domain, replaceValue: '[RedactedDomain]' }, | ||
{ searchValue: otherSensitivefield, replaceValue: '[Redacted]' }, | ||
]; | ||
const args = [ | ||
{ | ||
message: message, | ||
stack: message, | ||
originalError: { | ||
message: message, | ||
stack: message, | ||
safe: 'Nothing sensitive', | ||
}, | ||
driverError: { | ||
message: message, | ||
stack: message, | ||
originalError: { | ||
message: message, | ||
stack: message, | ||
safe: 'Nothing sensitive', | ||
}, | ||
}, | ||
}, | ||
]; | ||
const expectedResult = [ | ||
{ | ||
message: redactedMessage, | ||
stack: redactedMessage, | ||
originalError: { | ||
message: redactedMessage, | ||
stack: redactedMessage, | ||
safe: 'Nothing sensitive', | ||
}, | ||
driverError: { | ||
message: redactedMessage, | ||
stack: redactedMessage, | ||
originalError: { | ||
message: redactedMessage, | ||
stack: redactedMessage, | ||
safe: 'Nothing sensitive', | ||
}, | ||
}, | ||
}, | ||
]; | ||
|
||
it('replaces sensitive data in input object', () => { | ||
const redacted = redactStringsInLogArgs(true, REDACT_STRING_PATHS, redactStrings, args); | ||
|
||
expect(redacted).toStrictEqual(expectedResult); | ||
}); | ||
|
||
it('returns original input if redactLogs is set to false', () => { | ||
const redacted = redactStringsInLogArgs(false, REDACT_STRING_PATHS, redactStrings, args); | ||
|
||
expect(redacted).toStrictEqual(args); | ||
}); | ||
|
||
it('replaces sensitive data in different input object', () => { | ||
const args = [ | ||
{ | ||
field1: message, | ||
field2: { | ||
field3: message, | ||
safe: 'Nothing sensitive', | ||
}, | ||
}, | ||
]; | ||
const expectedResult = [ | ||
{ | ||
field1: redactedMessage, | ||
field2: { | ||
field3: redactedMessage, | ||
safe: 'Nothing sensitive', | ||
}, | ||
}, | ||
]; | ||
const redactPaths = ['field1', 'field2.field3']; | ||
const redacted = redactStringsInLogArgs(true, redactPaths, redactStrings, args); | ||
|
||
expect(redacted).toStrictEqual(expectedResult); | ||
}); | ||
}); | ||
}); |
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,31 @@ | ||
import { get, set } from 'lodash'; | ||
|
||
// This helper function is used to redact sensitive data in Error object strings. | ||
export const redactStringsInLogArgs = ( | ||
redactLogs: boolean, | ||
redactPaths: string[], | ||
redactStrings: { searchValue: string | RegExp; replaceValue: string }[], | ||
args: any, | ||
): any => { | ||
if (!redactLogs) { | ||
return args; | ||
} | ||
args.forEach((arg, index) => { | ||
redactPaths.forEach((path) => { | ||
const value: string = get(arg, path); | ||
if (value) { | ||
const safeValue = redactString(redactStrings, value); | ||
set(args[index], path, safeValue); | ||
} | ||
}); | ||
}); | ||
return args; | ||
}; | ||
|
||
const redactString = (redactStrings: { searchValue: string | RegExp; replaceValue: string }[], string: string): string => { | ||
let safeString: string = string; | ||
redactStrings.forEach((redact) => { | ||
safeString = safeString.replaceAll(redact.searchValue, redact.replaceValue); | ||
}); | ||
return safeString; | ||
}; |
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