Repository navigation
PER-16493: log only the PDP and API URL when the client starts #137
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
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
5c17048
PER-16493: log only the PDP and API URL when the client starts
EliMoshkovich bf111a5
PER-16493: review follow-ups: structured init fields, URL origins, pi…
EliMoshkovich 3223122
PER-16493: second review pass: log axios errors without their config,…
EliMoshkovich 8913b83
PER-16493: checkAllTenants sends its query as the body; API errors ke…
EliMoshkovich d17b3c5
PER-16493: redact the raw Authorization header line by line, not by r…
EliMoshkovich 7aacac9
PER-16493: checkAllTenants errors keep no Authorization header; API e…
EliMoshkovich d8ad051
PER-16493: test that the SDK logger uses its err serializer and redac…
EliMoshkovich 9801dc1
PER-16493: redactUrl drops the user info before the query; the init l…
EliMoshkovich e9d7882
PER-16493: checkAllTenants logs a failure once; error.request becomes…
EliMoshkovich 943d546
PER-16493: the err serializer redacts a URL in an error's input field…
EliMoshkovich 08494f3
PER-16493: redactUrl reads a valid URL as Node's parser does
EliMoshkovich 1b9cc5a
PER-16493: redactUrl rebuilds an http(s) URL from the parsed URL
EliMoshkovich 1e527da
PER-16493: the err serializer redacts input on a copy
EliMoshkovich f215370
PER-16493: HTTP debug lines log the URL a request goes to; pin the la…
EliMoshkovich f1f375c
PER-16493: build the logged URL without params, default it to ''; tes…
EliMoshkovich 7441f37
PER-16493: the logged URL is always a string, and the HTTP lines are …
EliMoshkovich 70591e2
PER-16493: the err serializer makes config.url a string too
EliMoshkovich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,20 +1,65 @@ | ||
| import util from 'util'; | ||
|
|
||
| import axios from 'axios'; | ||
| import pino from 'pino'; | ||
|
|
||
| import { IPermitConfig } from './config'; | ||
| import { redactUrl } from './utils/redact-url'; | ||
|
|
||
| export function prettyConsoleLog(label: string, data: any) { | ||
| console.log(label, util.inspect(data, false, 12, true)); | ||
| } | ||
|
|
||
| // pino's standard error serializer copies every field of an error, and an axios error's | ||
| // config, request and response hold the Authorization header (the request even as raw | ||
| // header text). An axios error is logged as what identifies the failure, and nothing else. | ||
| export function serializeError(error: unknown): unknown { | ||
| if (axios.isAxiosError(error)) { | ||
| return { | ||
| type: error.name, | ||
| message: error.message, | ||
| code: error.code, | ||
| status: error.response?.status, | ||
| method: error.config?.method?.toUpperCase(), | ||
| url: error.config?.url == null ? undefined : redactUrl(String(error.config.url)), | ||
| }; | ||
| } | ||
| // Any other error is copied field by field. Node's ERR_INVALID_URL, which axios rejects with | ||
| // for a URL it can't parse, carries that whole URL in `input`. | ||
| const serialized = pino.stdSerializers.err(error as Error); | ||
| // A copy: for a value that isn't error-like, pino returns the caller's own object. | ||
| if (typeof serialized?.input === 'string') { | ||
| return { ...serialized, input: redactUrl(serialized.input) }; | ||
| } | ||
| return serialized; | ||
| } | ||
|
|
||
| export class LoggerFactory { | ||
| static createLogger(config: IPermitConfig): pino.Logger { | ||
| return pino({ | ||
| // `destination` defaults to stdout, as before; tests pass a stream to read the output. | ||
| static createLogger(config: IPermitConfig, destination?: pino.DestinationStream): pino.Logger { | ||
| const options: pino.LoggerOptions = { | ||
| level: config.log.level, | ||
| prettyPrint: config.log.json ? { levelFirst: true } : false, | ||
| base: { label: config.log.label }, | ||
| timestamp: pino.stdTimeFunctions.isoTime, | ||
| }); | ||
| serializers: { err: serializeError }, | ||
| // A backstop for a credential that a log call passes in an object: a `token` field, or | ||
| // an `Authorization` / `authorization` header in a `headers` field, at the top level of | ||
| // that object or one level down. fast-redact has no recursive wildcard, so deeper fields | ||
| // and other spellings aren't covered; errors go through `serializeError` instead. | ||
| redact: { | ||
| paths: [ | ||
| 'token', | ||
| '*.token', | ||
| 'headers.Authorization', | ||
| 'headers.authorization', | ||
| '*.headers.Authorization', | ||
|
EliMoshkovich marked this conversation as resolved.
Resolved
|
||
| '*.headers.authorization', | ||
| ], | ||
| censor: '[REDACTED]', | ||
| }, | ||
| }; | ||
| // pino 8 has no prettyPrint option and throws on a truthy one, so `log.json` no longer | ||
| // reaches pino: the output is JSON whatever it says, as it already was by default. | ||
| return destination ? pino(options, destination) : pino(options); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.