-
Notifications
You must be signed in to change notification settings - Fork 6
feat(cli): add verbose diagnostics mode #68
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| package diagnostic | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "io" | ||
| "os" | ||
| "regexp" | ||
| "strings" | ||
| ) | ||
|
|
||
| // EnabledFromEnv reports whether diagnostic output was requested by env. | ||
| func EnabledFromEnv() bool { | ||
| return os.Getenv("KONTEXT_DEBUG") == "1" | ||
| } | ||
|
|
||
| // Logger writes human diagnostics only when verbose output is enabled. | ||
| type Logger struct { | ||
| out io.Writer | ||
| enabled bool | ||
| } | ||
|
|
||
| func New(out io.Writer, enabled bool) Logger { | ||
| return Logger{out: out, enabled: enabled} | ||
| } | ||
|
|
||
| func (l Logger) Enabled() bool { | ||
| return l.enabled | ||
| } | ||
|
|
||
| func (l Logger) Printf(format string, args ...any) { | ||
| if !l.enabled || l.out == nil { | ||
| return | ||
| } | ||
| fmt.Fprint(l.out, Redact(fmt.Sprintf(format, args...))) | ||
| } | ||
|
|
||
| var secretPatterns = []*regexp.Regexp{ | ||
| regexp.MustCompile(`(?i)Bearer\s+[A-Za-z0-9._~+/=-]+`), | ||
| regexp.MustCompile(`(?i)(access_token|id_token|refresh_token|authorization|cookie)=([^&\s]+)`), | ||
| regexp.MustCompile(`(?i)(code|token)=([^&\s]+)`), | ||
|
michiosw marked this conversation as resolved.
|
||
| } | ||
|
|
||
| var jsonSecretPattern = regexp.MustCompile(`(?i)("(?:access_token|id_token|refresh_token|authorization|cookie|code|token)"\s*:\s*")([^"]+)(")`) | ||
|
|
||
| // Redact removes credential-shaped values before diagnostics reach stderr. | ||
| func Redact(input string) string { | ||
| output := jsonSecretPattern.ReplaceAllString(input, `${1}[REDACTED]${3}`) | ||
| for _, pattern := range secretPatterns { | ||
| output = pattern.ReplaceAllStringFunc(output, func(match string) string { | ||
| if len(match) >= 6 && strings.EqualFold(match[:6], "Bearer") { | ||
| return "Bearer [REDACTED]" | ||
| } | ||
| parts := pattern.FindStringSubmatch(match) | ||
| if len(parts) >= 2 { | ||
| return parts[1] + "=[REDACTED]" | ||
| } | ||
| return "[REDACTED]" | ||
| }) | ||
| } | ||
| return output | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package diagnostic | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestEnabledFromEnvRequiresOne(t *testing.T) { | ||
| t.Setenv("KONTEXT_DEBUG", "1") | ||
| if !EnabledFromEnv() { | ||
| t.Fatal("EnabledFromEnv() = false, want true") | ||
| } | ||
|
|
||
| t.Setenv("KONTEXT_DEBUG", "true") | ||
| if EnabledFromEnv() { | ||
| t.Fatal("EnabledFromEnv() = true, want false") | ||
| } | ||
| } | ||
|
|
||
| func TestLoggerWritesRedactedDiagnosticsOnlyWhenEnabled(t *testing.T) { | ||
| var output bytes.Buffer | ||
| logger := New(&output, false) | ||
| logger.Printf("Authorization: Bearer secret-token") | ||
| if output.String() != "" { | ||
| t.Fatalf("disabled logger output = %q, want empty", output.String()) | ||
| } | ||
|
|
||
| logger = New(&output, true) | ||
| logger.Printf("Authorization: Bearer secret-token code=secret-code") | ||
| got := output.String() | ||
| if strings.Contains(got, "secret-token") || strings.Contains(got, "secret-code") { | ||
| t.Fatalf("diagnostic output leaked secret: %q", got) | ||
| } | ||
| if !strings.Contains(got, "Bearer [REDACTED]") || !strings.Contains(got, "code=[REDACTED]") { | ||
| t.Fatalf("diagnostic output = %q, want redacted markers", got) | ||
| } | ||
| } | ||
|
|
||
| func TestLoggerRedactsJSONSecrets(t *testing.T) { | ||
| var output bytes.Buffer | ||
| logger := New(&output, true) | ||
|
|
||
| logger.Printf(`{"access_token":"secret-token","code":"secret-code","message":"keep"}`) | ||
| got := output.String() | ||
| if strings.Contains(got, "secret-token") || strings.Contains(got, "secret-code") { | ||
| t.Fatalf("diagnostic output leaked JSON secret: %q", got) | ||
| } | ||
| if !strings.Contains(got, `"access_token":"[REDACTED]"`) || !strings.Contains(got, `"code":"[REDACTED]"`) { | ||
| t.Fatalf("diagnostic output = %q, want JSON redaction markers", got) | ||
| } | ||
| if !strings.Contains(got, `"message":"keep"`) { | ||
| t.Fatalf("diagnostic output = %q, want non-secret fields preserved", got) | ||
| } | ||
| } |
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
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.