Skip to content
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

Pass logging context to Detector.FromData #2228

Closed
wants to merge 1 commit into from

Conversation

rgmz
Copy link
Contributor

@rgmz rgmz commented Dec 15, 2023

Description:

This PR touches a lot of files, however, the sole change is replacing import ... "context" with import ... "github.com/trufflesecurity/trufflehog/v3/pkg/context".

Why?
While the logging context is not used in any detectors (as far as I'm aware), it is incredibly useful for local debugging, similar to #2191. Right now it is difficult to figure out what specific chunk triggered a detector, and in many instances logging is just done with fmt.Printf which is devoid of any context. Perhaps there is an easier or better way to accomplish this — let me know.

An alternative is to change Detector.FromData to Detector.FromChunk (#1517). However, that's a larger change that could be accomplished via less invasive means (#1741).

Checklist:

  • Tests passing (make test-community)?
  • Lint passing (make lint this requires golangci-lint)?

@rgmz rgmz requested review from a team as code owners December 15, 2023 15:17
@rgmz rgmz force-pushed the feat/detector-context branch from 69ce6f9 to baae1f6 Compare December 15, 2023 15:23
@mcastorina
Copy link
Collaborator

First off, thanks for taking the time and effort for this change.

Perhaps there is an easier or better way to accomplish this — let me know.

Would using logContext.AddLogger accomplish what you're doing?

// AddLogger converts a context.Context into a Context. If the underlying type
// is already a Context, that will be returned, otherwise a default logger will
// be added.
func AddLogger(parent context.Context) Context {
if loggerCtx, ok := parent.(Context); ok {
return loggerCtx
}
return WithLogger(parent, defaultLogger)
}

In the detector you could do something like the below when needed for debugging. Since we already pass a log context into the detectors, it should preserve the key/value pairs.

logger := logContext.AddLogger(ctx).Logger()

@rgmz
Copy link
Contributor Author

rgmz commented Dec 15, 2023

That's bizarre: I swear that didn't work before but it does now. Perhaps I accidentally mixed in a call to context instead of the github.com/trufflesecurity/.../context which doesn't preserve values (Slack thread). 😵

@rgmz rgmz closed this Dec 15, 2023
@rgmz rgmz deleted the feat/detector-context branch December 15, 2023 19:46
@mcastorina
Copy link
Collaborator

Gah, sorry I missed that thread! Yeah, I think the log info would get lost if it's wrapped in a stdlib context. I wonder if there's a way to traverse the ancestry of contexts instead of only looking at the child..

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

Successfully merging this pull request may close these issues.

2 participants