-
Notifications
You must be signed in to change notification settings - Fork 0
/
awskey.go
58 lines (46 loc) · 1.05 KB
/
awskey.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package awskey
import (
"encoding/base32"
"errors"
"fmt"
)
type KeyType string
const (
STSBearerToken KeyType = "ABIA"
ContextSpecific KeyType = "ACCA"
Group KeyType = "AGPA"
IAMUser KeyType = "AIDA"
EC2Profile KeyType = "AIPA"
AccessKey KeyType = "AKIA"
ManagedPolicy KeyType = "ANPA"
VersionManagedPolicy KeyType = "ANVA"
PublicKey KeyType = "APKA"
Role KeyType = "AROA"
Certificate KeyType = "ASCA"
STSKey KeyType = "ASIA"
)
type Key struct {
Type KeyType
AccountID string
}
func Decode(key string) (*Key, error) {
var k Key
if len(key) < 4 {
return nil, errors.New("Invalid key too short")
}
k.Type = KeyType(key[:4])
key = key[4:]
b, err := base32.StdEncoding.DecodeString(key)
if err != nil {
return nil, fmt.Errorf("Invalid key %w", err)
}
var x uint64
for i := 0; i < 6; i++ {
x = x << 8
x |= uint64(b[i])
}
x &= 0x7fffffffff80
x >>= 7
k.AccountID = fmt.Sprintf("%012d", x)
return &k, nil
}