aboutsummaryrefslogtreecommitdiff
path: root/metadata-models.go
diff options
context:
space:
mode:
Diffstat (limited to 'metadata-models.go')
-rw-r--r--metadata-models.go98
1 files changed, 98 insertions, 0 deletions
diff --git a/metadata-models.go b/metadata-models.go
new file mode 100644
index 0000000..eff9467
--- /dev/null
+++ b/metadata-models.go
@@ -0,0 +1,98 @@
1package main
2
3import (
4 "crypto/rand"
5 "crypto/sha1"
6 "encoding/hex"
7 "errors"
8 "fmt"
9 "regexp"
10 "strconv"
11 "strings"
12 "time"
13
14 "github.com/aws/aws-sdk-go/aws/credentials"
15)
16
17var (
18 REGION_AZ_REGEXP = regexp.MustCompile("((?:us|ca|eu|ap|sa)-(?:north|south)?(?:east|west)-\\d)([a-f])")
19)
20
21type IAMCredentials struct {
22 Code string
23 LastUpdated time.Time
24 Type string
25 AccessKeyId string
26 SecretAccessKey string
27 Token string
28 Expiration time.Time
29 rawCredentials *credentials.Credentials
30}
31
32func generatePlausibleId(prefix string) *string {
33 b := make([]byte, 10)
34 rand.Read(b)
35 h := sha1.New().Sum(b)
36 o := fmt.Sprintf("%s-%s", prefix, hex.EncodeToString(h)[0:17])
37 return &o
38}
39
40func generateInstanceId(hostname *string) *string {
41 h := sha1.New().Sum([]byte(*hostname))
42 o := fmt.Sprintf("i-%s", hex.EncodeToString(h)[0:17])
43 return &o
44}
45
46func generatePlausibleProfileId() *string {
47 b := make([]byte, 16)
48 rand.Read(b)
49
50 for i, bb := range b {
51 if bb%3 == 0 {
52 b[i] = bb%10 + 48
53 } else {
54 b[i] = bb%26 + 65
55 }
56 }
57
58 res := fmt.Sprintf("AIPAI%s", string(b))
59
60 return &res
61}
62
63func parseAccountFromARN(arn string) (int64, error) {
64 parts := strings.Split(arn, ":")
65 if len(parts) != 6 {
66 return 0, errors.New("Invalid ARN format")
67 }
68
69 id, err := strconv.ParseInt(parts[4], 10, 64)
70 if err != nil {
71 return 0, err
72 }
73
74 return id, nil
75}
76
77func parseRoleName(arn string) (string, error) {
78 parts := strings.Split(arn, ":")
79 if len(parts) != 6 {
80 return "", errors.New("Invalid ARN format")
81 }
82
83 role := strings.Split(parts[5], "/")
84 if len(role) != 2 {
85 return "", errors.New("Invalid role name format")
86 }
87
88 return role[1], nil
89}
90
91func parseRegionAZ(in string) (string, string, error) {
92 match := REGION_AZ_REGEXP.FindAllStringSubmatch("us-west-2a", -1)
93 if match == nil || len(match) == 0 {
94 return "", "", errors.New("Unable to parse region/AZ")
95 }
96
97 return match[0][1], match[0][2], nil
98}