Repository navigation
feat(health): report redacted datasource/SSH integration errors #162
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
4 commits
Select commit
Hold shift + click to select a range
76d52bc
feat(health): report redacted datasource/SSH integration errors
PrashantBtkl 2cd19cf
fix(health): address review feedback on integration error reporting
PrashantBtkl 00bf9e7
Merge branch 'main' into feat/proxy-agent-integration-errors
PrashantBtkl c3671ea
fix(deps): bump Go to 1.26.9 and x/net, grpc to clear govulncheck fin…
PrashantBtkl 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package proxy | ||
|
|
||
| import ( | ||
| "errors" | ||
| "io" | ||
| "net" | ||
| "regexp" | ||
| "strings" | ||
| "unicode/utf8" | ||
| ) | ||
|
|
||
| // Error classes surfaced on the Proxy Agent tab of the Agent Health page. | ||
| const ( | ||
| ErrorClassDatasource = "datasource_integration_failure" | ||
| ErrorClassSSH = "ssh_integration_failure" | ||
|
|
||
| maxReportedErrorLen = 512 | ||
| ) | ||
|
|
||
| // ErrorClass maps a proxy type to the error class reported upstream. | ||
| func ErrorClass(proxyType string) string { | ||
| if proxyType == "ssh-proxy" { | ||
| return ErrorClassSSH | ||
| } | ||
| return ErrorClassDatasource | ||
| } | ||
|
|
||
| // integrationErrorMarkers are substrings (lowercase) of errors that mean the | ||
| // datasource itself is unreachable or rejecting our credentials, as opposed to | ||
| // the caller sending a bad query or hitting a policy rule. | ||
| var integrationErrorMarkers = []string{ | ||
| "connection refused", "connection reset", "broken pipe", "no such host", | ||
| "i/o timeout", "network is unreachable", "no route to host", | ||
| "handshake failed", "unable to authenticate", "authentication failed", | ||
| "password authentication", "access denied for user", "permission denied (publickey", | ||
| "host key", "x509:", "tls:", "sasl", "noauth", "wrongpass", | ||
| } | ||
|
|
||
| // IsIntegrationError reports whether err looks like a connectivity or | ||
| // authentication failure of the integration, rather than a caller mistake | ||
| // (invalid input, disallowed command, SQL error) which must not mark a | ||
| // healthy datasource as broken. | ||
| func IsIntegrationError(err error) bool { | ||
| if err == nil { | ||
| return false | ||
| } | ||
| var netErr net.Error | ||
| if errors.As(err, &netErr) || errors.Is(err, io.EOF) || errors.Is(err, io.ErrUnexpectedEOF) { | ||
| return true | ||
| } | ||
| msg := strings.ToLower(err.Error()) | ||
| for _, m := range integrationErrorMarkers { | ||
| if strings.Contains(msg, m) { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| const secretKeys = `password|passwd|pwd|passphrase|secret|token|api[_-]?key|access[_-]?key|private[_-]?key|authorization|credentials?` | ||
|
|
||
| var ( | ||
| // scheme://user:pass@host → scheme://[redacted]@host | ||
| urlUserinfoRe = regexp.MustCompile(`([a-zA-Z][a-zA-Z0-9+.-]*://)[^\s/@]+@`) | ||
| // password=..., "password":"...", token: ... (key may be followed by a closing quote) | ||
| secretKVRe = regexp.MustCompile(`(?i)\b(` + secretKeys + `)("?\s*[=:]\s*)("[^"]*"|'[^']*'|\S+)`) | ||
| // Authorization header schemes with an inline credential. | ||
| bearerRe = regexp.MustCompile(`(?i)\b(bearer|basic)\s+[A-Za-z0-9._~+/=-]{6,}`) | ||
| pemBlockRe = regexp.MustCompile(`(?s)-----BEGIN [A-Z ]*PRIVATE KEY-----.*?(-----END [A-Z ]*PRIVATE KEY-----|$)`) | ||
|
|
||
| // [fd00::5]:5432 and bare IPv6 with at least five groups. | ||
| ipv6BracketRe = regexp.MustCompile(`\[[0-9a-fA-F:.]*:[0-9a-fA-F:.]*\]`) | ||
| ipv6BareRe = regexp.MustCompile(`\b(?:[0-9a-fA-F]{1,4}:){4,7}[0-9a-fA-F]{1,4}\b`) | ||
| ipv4Re = regexp.MustCompile(`\b\d{1,3}(?:\.\d{1,3}){3}\b`) | ||
| // host.domain:port pairs such as db.internal.example.com:5432 | ||
| hostPortRe = regexp.MustCompile(`\b[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)+:\d{1,5}\b`) | ||
| // Go's resolver and dialer name the host right after these words, with or | ||
| // without dots: "lookup db.internal: no such host", "dial tcp postgres:5432". | ||
| lookupHostRe = regexp.MustCompile(`(?i)(\blookup\s+)[^\s:]+`) | ||
| dialHostRe = regexp.MustCompile(`(?i)(\bdial\s+(?:tcp|udp)\d?\s+)[^\s:]+`) | ||
| ) | ||
|
|
||
| // RedactError strips credentials and hostnames/IPs from an error message | ||
| // before it leaves the host, and bounds its length. | ||
| // | ||
| // It is best-effort: a bare hostname or a username in free-form error text | ||
| // that follows none of the patterns above is not detected. | ||
| func RedactError(msg string) string { | ||
| msg = strings.ToValidUTF8(msg, "") | ||
| msg = pemBlockRe.ReplaceAllString(msg, "[redacted-key]") | ||
| msg = urlUserinfoRe.ReplaceAllString(msg, "${1}[redacted]@") | ||
| msg = bearerRe.ReplaceAllString(msg, "${1} [redacted]") | ||
| msg = secretKVRe.ReplaceAllString(msg, "${1}${2}[redacted]") | ||
| msg = ipv6BracketRe.ReplaceAllString(msg, "[redacted-host]") | ||
| msg = ipv6BareRe.ReplaceAllString(msg, "[redacted-host]") | ||
| msg = ipv4Re.ReplaceAllString(msg, "[redacted-host]") | ||
| msg = hostPortRe.ReplaceAllString(msg, "[redacted-host]") | ||
| msg = lookupHostRe.ReplaceAllString(msg, "${1}[redacted-host]") | ||
| msg = dialHostRe.ReplaceAllString(msg, "${1}[redacted-host]") | ||
| msg = strings.Join(strings.Fields(msg), " ") | ||
| if len(msg) > maxReportedErrorLen { | ||
| i := maxReportedErrorLen | ||
| for i > 0 && !utf8.RuneStart(msg[i]) { | ||
| i-- | ||
| } | ||
| msg = msg[:i] + "…" | ||
| } | ||
| return msg | ||
| } | ||
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.