feat(cli): add HTTP debug logging#485
Conversation
|
❌ Author of the following commits did not sign a Contributor Agreement: Please, read and sign the above mentioned agreement if you want to contribute to this project |
| const REDACTED_HEADERS = new Set([ | ||
| 'authorization', | ||
| 'proxy-authorization', | ||
| 'x-api-key', | ||
| 'cookie', | ||
| 'set-cookie', | ||
| ]) |
There was a problem hiding this comment.
We'll need to validate this list by scanning each API codebase to ensure these are the only values with the potential to leak secrets.
| name: 'ELASTIC_DEBUG', | ||
| required: false, | ||
| description: 'Set to 1 to print HTTP request and response details to stderr', | ||
| }, |
There was a problem hiding this comment.
It would be good to also add a --verbose/-v flag so an env var isn't the only way to do this.
| function writeHeaders (prefix: string, headers: RequestInit['headers']): void { | ||
| for (const [name, value] of new Headers(headers)) { | ||
| const printableValue = REDACTED_HEADERS.has(name.toLowerCase()) ? '(redacted)' : value | ||
| process.stderr.write(`${prefix} ${name}: ${printableValue}\n`) |
There was a problem hiding this comment.
This will violate the requirement that all stdout/stderr output be able to be parsed as valid JSON when --json is enabled on any command. This should be wrapped in a function that checks that flag and, if --json is enabled, it should push all debug statements to an array and include them in the JSON response somehow.
| } | ||
|
|
||
| return response | ||
| } |
There was a problem hiding this comment.
On the whole, providing a single fetch wrapped implementation is correct for this use case, and may have other benefits besides debug logs in the future. This makes fetchWithHttpDebug a bit too specific of a function name. Rename the module src/lib/http.ts and the function apiFetch or sendRequest; something that differentiates it from the built-in fetch function. That function should take an options object that looks like:
interface FetchOptions {
debug?: boolean
}This gives us a place to add new options as we need them.
Summary
--debugflag andELASTIC_DEBUG=1activationCloses #316
Testing
npm test(1,502 passed, 0 failed, 1 skipped; 100% line, branch, and function coverage)npm run test:lintAI assistance
This contribution was developed with Codex under my direction and reviewed and tested locally before submission.