-
Notifications
You must be signed in to change notification settings - Fork 1
/
role.go
70 lines (58 loc) · 1.5 KB
/
role.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
package directory
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
type ARN struct {
Partition string
Service string
Region string
Account string
Resource string
}
type IamInfo struct {
Code string `json:"Code"`
LastUpdatee string `json:"LastUpdated"`
InstanceProfileArn string `json:"InstanceProfileArn"`
InstanceProfileId string `json:"InstanceProfileId"`
}
func parseArn(arn string) ARN {
// "arn:aws:iam::756413706286:instance-profile/bastion",
parts := strings.Split(arn, ":")
return ARN{
Partition: parts[1],
Service: parts[2],
Region: parts[3],
Account: parts[4],
Resource: parts[5],
}
}
// curl http://169.254.169.254/latest/meta-data/iam/info
// {
// "Code" : "Success",
// "LastUpdated" : "2016-11-16T18:52:29Z",
// "InstanceProfileArn" : "arn:aws:iam::756413706286:instance-profile/bastion",
// "InstanceProfileId" : "AIPAIHHSYB75V3MLIVTG6"
//}
func GetRole() (string, error) {
resp, err := http.Get("http://169.254.169.254/latest/meta-data/iam/info")
if err != nil {
return "", err
}
// If there is no IAM role then info will return 404
if resp.StatusCode == http.StatusNotFound {
return "", nil
}
var info IamInfo
if err := json.NewDecoder(resp.Body).Decode(&info); err != nil {
return "", err
}
arn := parseArn(info.InstanceProfileArn)
parts := strings.SplitN(arn.Resource, "/", 2)
if len(parts) != 2 {
return "", fmt.Errorf("arn resource in unknown format; Resource=%s", arn.Resource)
}
return parts[1], nil
}