Skip to content
This repository has been archived by the owner on Aug 16, 2022. It is now read-only.

Commit

Permalink
fix: Redact separate error messages separately (#1071)
Browse files Browse the repository at this point in the history
This splits single lookupRegex into two:

* one matches lookup failures
* second matches read errors
  • Loading branch information
irmatov authored Jun 20, 2022
1 parent e04e493 commit e46371b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
20 changes: 14 additions & 6 deletions client/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,18 @@ import (
const ssoInvalidOrExpired = "failed to refresh cached credentials, the SSO session has expired or is invalid"

var (
requestIdRegex = regexp.MustCompile(`\s([Rr]equest[ _]{0,1}(ID|Id|id):)\s[A-Za-z0-9-]+`)
hostIdRegex = regexp.MustCompile(`\sHostID: [A-Za-z0-9+/_=-]+`)
arnIdRegex = regexp.MustCompile(`(\s)(arn:aws[A-Za-z0-9-]*:)[^ \.\(\)\[\]\{\}\;\,]+(\s?)`)
urlRegex = regexp.MustCompile(`([\s"])http(s?):\/\/[a-z0-9_\-\./]+([":\s]?)`)
lookupRegex = regexp.MustCompile(`(\slookup\s)[-A-Za-z0-9\.]+\son\s([0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}:[0-9]{1,5})(:.+?)([0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}:[0-9]{1,5})->([0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}:[0-9]{1,5})(:.*)`)
requestIdRegex = regexp.MustCompile(`\s([Rr]equest[ _]{0,1}(ID|Id|id):)\s[A-Za-z0-9-]+`)
hostIdRegex = regexp.MustCompile(`\sHostID: [A-Za-z0-9+/_=-]+`)
arnIdRegex = regexp.MustCompile(`(\s)(arn:aws[A-Za-z0-9-]*:)[^ \.\(\)\[\]\{\}\;\,]+(\s?)`)
urlRegex = regexp.MustCompile(`([\s"])http(s?):\/\/[a-z0-9_\-\./]+([":\s]?)`)
lookupRegex = regexp.MustCompile(
`\blookup\s[-A-Za-z0-9\.]+\s` + // " lookup host.name "
`on\s\S+:\d+`, // "on 123.123.123.123:53"
)
readXonYRegex = regexp.MustCompile(
`\bread\s(udp|tcp)\s` + // "read udp "
`\S+:\d+->\S+:\d+`, // "192.168.1.2:5353->192.168.1.1:53"
)
dialRegex = regexp.MustCompile(`(\sdial\s)(tcp|udp)(\s)([0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}\.[0-9]{0,3}:[0-9]{1,5})(:.+?)`)
encAuthRegex = regexp.MustCompile(`(\s)(Encoded authorization failure message:)\s[A-Za-z0-9_-]+`)
userRegex = regexp.MustCompile(`(\s)(is not authorized to perform: .+ on resource:\s)(user)\s.+`)
Expand Down Expand Up @@ -235,7 +242,8 @@ func removePII(aa []string, msg string) string {
msg = hostIdRegex.ReplaceAllString(msg, " HostID: xxxx")
msg = arnIdRegex.ReplaceAllString(msg, "${1}${2}xxxx${3}")
msg = urlRegex.ReplaceAllString(msg, "${1}http${2}://xxxx${3}")
msg = lookupRegex.ReplaceAllString(msg, "${1}xxxx${3}xxxx->xxxx${6}")
msg = lookupRegex.ReplaceAllString(msg, "lookup xxxx on xxxx:xx")
msg = readXonYRegex.ReplaceAllString(msg, "read $1 xxxx:xx->xxxx:xx")
msg = dialRegex.ReplaceAllString(msg, "${1}${2}${3}xxxx${5}")
msg = encAuthRegex.ReplaceAllString(msg, "${1}${2} xxxx")
msg = userRegex.ReplaceAllString(msg, "${1}${2}${3} xxxx")
Expand Down
6 changes: 5 additions & 1 deletion client/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestRemovePII(t *testing.T) {
},
{
"operation error CloudWatch Logs: DescribeMetricFilters, exceeded maximum number of attempts, 10, https response error StatusCode: 0, RequestID: , request send failed, Post \"https://logs.eu-central-1.amazonaws.com/\": dial tcp: lookup logs.eu-central-1.amazonaws.com on 192.168.1.1:53: read udp 192.168.1.2:5353->192.168.1.1:53: i/o timeout",
"operation error CloudWatch Logs: DescribeMetricFilters, exceeded maximum number of attempts, 10, https response error StatusCode: 0, RequestID: , request send failed, Post \"https://xxxx\": dial tcp: lookup xxxx: read udp xxxx->xxxx: i/o timeout",
"operation error CloudWatch Logs: DescribeMetricFilters, exceeded maximum number of attempts, 10, https response error StatusCode: 0, RequestID: , request send failed, Post \"https://xxxx\": dial tcp: lookup xxxx on xxxx:xx: read udp xxxx:xx->xxxx:xx: i/o timeout",
},
{
"EC2: DescribeImageAttribute - You are not authorized to perform this operation. Encoded authorization failure message: SOMEENCODEDMESSAGEWITHNUMBERS1234567ANDDASHANDUNDERSCORES-ABCDE_123123123_EXAMPLEMESSAGE",
Expand All @@ -55,6 +55,10 @@ func TestRemovePII(t *testing.T) {
"ResourceType name not found - Could not find example request type named 'resource-dev-1111'",
"ResourceType name not found - Could not find example request type named 'xxxx'",
},
{
`qldb.ledgers: failed to resolve table "aws_qldb_ledgers": error at github.com/cloudquery/cq-provider-aws/resources/services/qldb.fetchQldbLedgers[ledgers.go:264] operation error QLDB: ListLedgers, exceeded maximum number of attempts, 10, https response error StatusCode: 0, RequestID: , request send failed, Get "https://qldb.ap-southeast-1.amazonaws.com/ledgers": dial tcp: lookup qldb.ap-southeast-1.amazonaws.com on 172.20.0.10:53: no such host`,
`qldb.ledgers: failed to resolve table "aws_qldb_ledgers": error at github.com/cloudquery/cq-provider-aws/resources/services/qldb.fetchQldbLedgers[ledgers.go:264] operation error QLDB: ListLedgers, exceeded maximum number of attempts, 10, https response error StatusCode: 0, RequestID: , request send failed, Get "https://xxxx": dial tcp: lookup xxxx on xxxx:xx: no such host`,
},
}
for i, tc := range cases {
res := removePII([]string{"123456789"}, tc.Input)
Expand Down

0 comments on commit e46371b

Please sign in to comment.