-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added accountID to throttled error log
- Loading branch information
1 parent
c6e9f30
commit 15a70e8
Showing
5 changed files
with
97 additions
and
8 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,39 @@ | ||
package token | ||
|
||
import ( | ||
"encoding/base32" | ||
"encoding/hex" | ||
"fmt" | ||
) | ||
|
||
// accountForAKID is a best-effort method to extract the account ID from an AKID for | ||
// logging on throttled requests. This should not be called on untrusted input (i.e. | ||
// AKID from the request before validating the request from STS). | ||
// | ||
// This is not foolproof, but avoids an `sts:GetAccessKeyInfo` call per AKID. | ||
// adapted from https://hackingthe.cloud/aws/enumeration/get-account-id-from-keys/ | ||
func accountForAKID(akid string) string { | ||
if len(akid) < 20 { | ||
// too short | ||
return "" | ||
} | ||
decoded, err := base32.StdEncoding.DecodeString(akid[4:]) | ||
if err != nil { | ||
// decoding error | ||
return "" | ||
} | ||
y := decoded[:6] | ||
z := uint64(0) | ||
for i := 0; i < len(y); i++ { | ||
z = (z << 8) | uint64(y[i]) | ||
} | ||
// this mask bytestring is always valid | ||
maskBytes, _ := hex.DecodeString("7fffffffff80") | ||
mask := uint64(0) | ||
for i := 0; i < len(maskBytes); i++ { | ||
mask = (mask << 8) | uint64(maskBytes[i]) | ||
} | ||
// Apply mask and shift right by 7 bits | ||
e := (z & mask) >> 7 | ||
return fmt.Sprintf("%012d", e) | ||
} |
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,44 @@ | ||
package token | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestAccountForAKID(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
akid string | ||
expected string | ||
wantErr error | ||
}{ | ||
{ | ||
name: "empty akid", | ||
akid: "", | ||
expected: "", | ||
}, | ||
{ | ||
name: "akid with account", | ||
akid: "ASIAR2TG44V5PDTTBZRR", | ||
expected: "125843596666", | ||
}, | ||
{ | ||
name: "account starting with a 0", | ||
akid: "ASIAQNZGKIQY56JQ7WML", | ||
expected: "029608264753", | ||
}, | ||
{ | ||
name: "non base32 encoded akid", | ||
akid: "ASIAc29tZXRoaW5nCg==", | ||
expected: "", | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
actual := accountForAKID(tc.akid) | ||
if actual != tc.expected { | ||
t.Errorf("expected %s, got %s", tc.expected, actual) | ||
} | ||
}) | ||
} | ||
} |
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