-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetadata-models.go
98 lines (80 loc) · 1.9 KB
/
metadata-models.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"crypto/rand"
"crypto/sha1"
"encoding/hex"
"errors"
"fmt"
"regexp"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go/aws/credentials"
)
var (
REGION_AZ_REGEXP = regexp.MustCompile("((?:us|ca|eu|ap|sa)-(?:north|south)?(?:east|west)-\\d)([a-f])")
)
type IAMCredentials struct {
Code string
LastUpdated time.Time
Type string
AccessKeyId string
SecretAccessKey string
Token string
Expiration time.Time
rawCredentials *credentials.Credentials
}
func generatePlausibleId(prefix string) *string {
b := make([]byte, 10)
rand.Read(b)
h := sha1.New().Sum(b)
o := fmt.Sprintf("%s-%s", prefix, hex.EncodeToString(h)[0:17])
return &o
}
func generateInstanceId(hostname *string) *string {
h := sha1.New().Sum([]byte(*hostname))
o := fmt.Sprintf("i-%s", hex.EncodeToString(h)[0:17])
return &o
}
func generatePlausibleProfileId() *string {
b := make([]byte, 16)
rand.Read(b)
for i, bb := range b {
if bb%3 == 0 {
b[i] = bb%10 + 48
} else {
b[i] = bb%26 + 65
}
}
res := fmt.Sprintf("AIPAI%s", string(b))
return &res
}
func parseAccountFromARN(arn string) (int64, error) {
parts := strings.Split(arn, ":")
if len(parts) != 6 {
return 0, errors.New("Invalid ARN format")
}
id, err := strconv.ParseInt(parts[4], 10, 64)
if err != nil {
return 0, err
}
return id, nil
}
func parseRoleName(arn string) (string, error) {
parts := strings.Split(arn, ":")
if len(parts) != 6 {
return "", errors.New("Invalid ARN format")
}
role := strings.Split(parts[5], "/")
if len(role) != 2 {
return "", errors.New("Invalid role name format")
}
return role[1], nil
}
func parseRegionAZ(in string) (string, string, error) {
match := REGION_AZ_REGEXP.FindAllStringSubmatch("us-west-2a", -1)
if match == nil || len(match) == 0 {
return "", "", errors.New("Unable to parse region/AZ")
}
return match[0][1], match[0][2], nil
}