Skip to content

Commit

Permalink
Merge pull request #41 from fullstack-build/development
Browse files Browse the repository at this point in the history
Fix #40, Rename maskStrings into maskAny
  • Loading branch information
terehov committed Aug 10, 2020
2 parents 2bc44ea + 20b4b14 commit 23ee248
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 33 deletions.
10 changes: 0 additions & 10 deletions .githooks/pre-commit/generate_docs

This file was deleted.

5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -583,10 +583,11 @@ secretiveLogger.info(secretiveObject);

```

##### `maskStrings`
##### `maskAny`
```default: [] ```

When `maskValuesOfKeys` is just not enough, and you really want to make sure no secrets get populated, you can also use `maskStrings` to mask every occurrence of a string.
When `maskValuesOfKeys` is just not enough, and you really want to make sure no secrets get populated, you can also use `maskAny` to mask every occurrence of a string/number.
> **Hint:** It will also mask keys if it encounters this strings/numbers.
**`maskValuesOfKeys` is case sensitive!**

Expand Down
2 changes: 1 addition & 1 deletion example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ verySecretiveObject.nested["circular"] = verySecretiveObject;

const secretiveLogger = new Logger({
name: "SecretiveLogger",
maskStrings: ["swordfish", "pass1234"],
maskAny: ["swordfish", "pass1234"],
maskValuesOfKeys: ["test", "authorization", "password"],
});

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"api-extractor": "tsc && mkdir -p ./docs/api_extractor && api-extractor run --local --verbose",
"typedoc": "typedoc",
"generate-all-docs": "npm run api-extractor && typedoc",
"release": "np"
"release": "npm run generate-all-docs && git add && np"
},
"jest": {
"verbose": true,
Expand Down
23 changes: 10 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export class Logger {
private _mySettings: ISettingsParam = {};
private _childLogger: Logger[] = [];
private _maskValuesOfKeysRegExp: RegExp | undefined;
private _maskStringsRegExp: RegExp | undefined;
private _maskAnyRegExp: RegExp | undefined;

/**
* @param settings - Configuration of the logger instance (all settings are optional with sane defaults)
Expand Down Expand Up @@ -130,7 +130,7 @@ export class Logger {

prefix: [],
maskValuesOfKeys: ["password"],
maskStrings: [],
maskAny: [],
maskPlaceholder: "[***]",

printLogMessageInNewLine: false,
Expand Down Expand Up @@ -208,9 +208,9 @@ export class Logger {
)
: undefined;

this._maskStringsRegExp =
this.settings.maskStrings?.length > 0
? new RegExp(Object.values(this.settings.maskStrings).join("|"), "g")
this._maskAnyRegExp =
this.settings.maskAny?.length > 0
? new RegExp(Object.values(this.settings.maskAny).join("|"), "g")
: undefined;

LoggerHelper.setUtilsInspectStyles(
Expand Down Expand Up @@ -797,7 +797,7 @@ export class Logger {
if (this._maskValuesOfKeysRegExp != null) {
inspectedString = inspectedString.replace(
this._maskValuesOfKeysRegExp,
"$1$2: " +
"$1$2 " +
LoggerHelper.styleString(
[this.settings.prettyInspectHighlightStyles.string],
`'${this.settings.maskPlaceholder}'`
Expand All @@ -806,9 +806,9 @@ export class Logger {
);
}

return this._maskStringsRegExp != null
return this._maskAnyRegExp != null
? inspectedString.replace(
this._maskStringsRegExp,
this._maskAnyRegExp,
this.settings.maskPlaceholder
)
: inspectedString;
Expand All @@ -819,11 +819,8 @@ export class Logger {
...param: unknown[]
): string {
const formattedStr: string = format(formatParam, ...param);
return this._maskStringsRegExp != null
? formattedStr.replace(
this._maskStringsRegExp,
this.settings.maskPlaceholder
)
return this._maskAnyRegExp != null
? formattedStr.replace(this._maskAnyRegExp, this.settings.maskPlaceholder)
: formattedStr;
}
}
8 changes: 4 additions & 4 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ export interface ISettingsParam {
/** Exclude case-insensitive keys for object passed to `tslog` that could potentially contain sensitive information (e.g. `password` or `Authorization`), default: ["password"] */
maskValuesOfKeys?: (number | string)[];

/** Mask all of this case-sensitive strings from logs (e.g. all secrets from ENVs etc.). Will be replaced with [***] */
maskStrings?: string[];
/** Mask all occurrences (case-sensitive) from logs (e.g. all secrets from ENVs etc.). Will be replaced with [***] */
maskAny?: (string | number)[];

/** String to use a placeholder to mask sensitive values. */
maskPlaceholder?: string;
Expand Down Expand Up @@ -169,7 +169,7 @@ export interface ISettings extends ISettingsParam {
dateTimeTimezone: string;
prefix: unknown[];
maskValuesOfKeys: (number | string)[];
maskStrings: string[];
maskAny: (string | number)[];
maskPlaceholder: string;
printLogMessageInNewLine: boolean;
displayDateTime: boolean;
Expand All @@ -190,7 +190,7 @@ export interface ISettings extends ISettingsParam {
*/
export interface IStd {
/** stream.Writable */
write: Function;
write: (message: string) => void;
}

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/settings.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -737,15 +737,15 @@ describe("Logger: settings", () => {
expect(stdStringGroups2?.[3]).toBe("[xxx]");
});

test("init logger: maskStrings", (): void => {
test("init logger: maskAny", (): void => {
const stdArray: string[] = [];
const std: { write: (line: string) => void } = {
write: (line: string) => {
stdArray.push(line);
},
};
const logger: Logger = new Logger({
maskStrings: ["test", "swordfish", "pass1234"],
maskAny: ["test", "swordfish", "pass1234"],
maskPlaceholder: "[xxx]",
stdOut: std,
stdErr: std,
Expand Down

0 comments on commit 23ee248

Please sign in to comment.