Skip to content

feat(cli): add HTTP debug logging#485

Open
sidsri14 wants to merge 1 commit into
elastic:mainfrom
sidsri14:feat/debug-http-logging
Open

feat(cli): add HTTP debug logging#485
sidsri14 wants to merge 1 commit into
elastic:mainfrom
sidsri14:feat/debug-http-logging

Conversation

@sidsri14

Copy link
Copy Markdown

Summary

  • add a global --debug flag and ELASTIC_DEBUG=1 activation
  • log HTTP request and response details to stderr for Elasticsearch, Kibana, and Cloud clients
  • redact credential-bearing headers case-insensitively and clone responses before reading their bodies
  • preserve existing client behavior for network and response-body errors

Closes #316

Testing

  • npm test (1,502 passed, 0 failed, 1 skipped; 100% line, branch, and function coverage)
  • npm run test:lint
  • ESLint over all changed test files
  • SPDX header validation over all code files
  • MegaLinter patch-relevant stages passed: ESLint, jscpd, git diff, gitleaks, and secretlint. The full Windows Docker run exited after Trivy reached its context deadline; shell/YAML findings were CRLF checkout artifacts.

AI assistance

This contribution was developed with Codex under my direction and reviewed and tested locally before submission.

@cla-checker-service

Copy link
Copy Markdown

❌ Author of the following commits did not sign a Contributor Agreement:
f81ca80

Please, read and sign the above mentioned agreement if you want to contribute to this project

Comment thread src/lib/http-debug.ts
Comment on lines +6 to +12
const REDACTED_HEADERS = new Set([
'authorization',
'proxy-authorization',
'x-api-key',
'cookie',
'set-cookie',
])

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/cli-schema.ts
name: 'ELASTIC_DEBUG',
required: false,
description: 'Set to 1 to print HTTP request and response details to stderr',
},

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to also add a --verbose/-v flag so an env var isn't the only way to do this.

Comment thread src/lib/http-debug.ts
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`)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread src/lib/http-debug.ts
}

return response
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(cli): add --debug flag and ELASTIC_DEBUG env var for HTTP request logging

2 participants