-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from gateway-fm/feat/log-redaction
ps-414 feat: add logger redact funcs (ip addr only currently)
- Loading branch information
Showing
3 changed files
with
122 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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,22 @@ | ||
package logger | ||
|
||
import "regexp" | ||
|
||
const ipAddrRegex = `\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b` | ||
|
||
type redactor struct { | ||
ipAddrRegex *regexp.Regexp | ||
} | ||
|
||
func NewRedactor() *redactor { | ||
return &redactor{regexp.MustCompile(ipAddrRegex)} | ||
} | ||
|
||
func (r *redactor) Redact(message string) string { | ||
|
||
if r.ipAddrRegex.MatchString(message) { | ||
return r.ipAddrRegex.ReplaceAllString(message, "xxx.xxx.xxx.xxx") | ||
} | ||
|
||
return message | ||
} |
This file contains 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,22 @@ | ||
package logger | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func Test_Redactor_RemovesIPAddresses(t *testing.T) { | ||
r := NewRedactor() | ||
|
||
m := map[string]string{ | ||
"this contains a valid ip: 172.217.22.14": "this contains a valid ip: xxx.xxx.xxx.xxx", | ||
"192.168.1.1 this started with a valid ip": "xxx.xxx.xxx.xxx this started with a valid ip", | ||
"this contains a non-ip 1234.12.15.2": "this contains a non-ip 1234.12.15.2", | ||
"this has ip with port, 192.168.1.1:80": "this has ip with port, xxx.xxx.xxx.xxx:80", | ||
} | ||
|
||
for msg, res := range m { | ||
if r.Redact(msg) != res { | ||
t.Errorf("Expected %s, got %s", res, r.Redact(msg)) | ||
} | ||
} | ||
} |