@@ -5,6 +5,7 @@ package logging
55
66import (
77 "regexp"
8+ "unsafe"
89)
910
1011// IAM Unique ID prefixes from
@@ -24,48 +25,45 @@ var UniqueIDRegex = regexp.MustCompile(`(A3T[A-Z0-9]` +
2425 `|ASIA` + // STS temporary access key
2526 `)[A-Z0-9]{16,}` )
2627
27- var SensitiveKeyRegex = regexp .MustCompile (`[A-Za-z0-9/+=]{16,}` )
28-
2928const (
3029 unmaskedFirst = 4
3130 unmaskedLast = 4
3231)
3332
34- func MaskAWSAccessKey (field string ) string {
35- field = UniqueIDRegex .ReplaceAllStringFunc (field , func (s string ) string {
33+ func MaskAWSAccessKey (field [] byte ) [] byte {
34+ field = UniqueIDRegex .ReplaceAllFunc (field , func (s [] byte ) [] byte {
3635 return partialMaskString (s , unmaskedFirst , unmaskedLast )
3736 })
3837 return field
3938}
4039
4140func MaskAWSSensitiveValues (field string ) string {
42- field = MaskAWSAccessKey (field )
43- field = MaskAWSSecretKeys (field )
44- return field
41+ b := unsafe .Slice (unsafe .StringData (field ), len (field ))
42+ b = MaskAWSAccessKey (b )
43+ MaskAWSSecretKeys (b )
44+ return unsafe .String (unsafe .SliceData (b ), len (b ))
4545}
4646
4747// MaskAWSSecretKeys masks likely AWS secret access keys in the input.
4848// See https://aws.amazon.com/blogs/security/a-safer-way-to-distribute-aws-credentials-to-ec2/:
4949// "Find me 40-character, base-64 strings that don’t have any base 64 characters immediately before or after".
50- func MaskAWSSecretKeys (in string ) string {
50+ func MaskAWSSecretKeys (in [] byte ) {
5151 const (
5252 secretKeyLen = 40
5353 )
5454 len := len (in )
55- out := make ([]byte , len )
5655 base64Characters := 0
5756
5857 for i := 0 ; i < len ; i ++ {
5958 b := in [i ]
60- out [i ] = b
6159
6260 if (b >= 'A' && b <= 'Z' ) || (b >= 'a' && b <= 'z' ) || (b >= '0' && b <= '9' ) || b == '/' || b == '+' || b == '=' {
6361 // base64 character.
6462 base64Characters ++
6563 } else {
6664 if base64Characters == secretKeyLen {
6765 for j := (i - secretKeyLen ) + unmaskedFirst ; j < i - unmaskedLast ; j ++ {
68- out [j ] = '*'
66+ in [j ] = '*'
6967 }
7068 }
7169
@@ -75,9 +73,7 @@ func MaskAWSSecretKeys(in string) string {
7573
7674 if base64Characters == secretKeyLen {
7775 for j := (len - secretKeyLen ) + unmaskedFirst ; j < len - unmaskedLast ; j ++ {
78- out [j ] = '*'
76+ in [j ] = '*'
7977 }
8078 }
81-
82- return string (out )
8379}
0 commit comments