forked from dynport/gocloud
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiam_credentials.go
97 lines (87 loc) · 2.05 KB
/
iam_credentials.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
package aws
import (
"encoding/json"
"fmt"
"io/ioutil"
"net"
"net/http"
"strings"
"time"
)
type awsCredentials struct {
Code string `json:"Code,omitempty"`
LastUpdated time.Time `json:"LastUpdated,omitempty"`
Type string `json:"Type,omitempty"`
AccessKeyId string `json:"AccessKeyId,omitempty"`
SecretAccessKey string `json:"SecretAccessKey,omitempty"`
Token string `json:"Token,omitempty"`
Expiration time.Time `json:"Expiration,omitempty"`
}
func CurrentAwsRegion() (string, error) {
s, e := CurrentAwsAvailabilityZone()
if e != nil {
return "", e
}
if len(s) > 4 {
return s[0 : len(s)-1], nil
}
return "", nil
}
func CurrentAwsAvailabilityZone() (string, error) {
rsp, e := http.Get("http://169.254.169.254/latest/meta-data/placement/availability-zone")
if e != nil {
return "", e
}
defer rsp.Body.Close()
b, e := ioutil.ReadAll(rsp.Body)
return string(b), e
}
func loadAwsCredentials(theUrl string) (*awsCredentials, error) {
r := &awsCredentials{}
rsp, e := http.Get(theUrl)
if e != nil {
return nil, e
}
defer rsp.Body.Close()
b, e := ioutil.ReadAll(rsp.Body)
if e != nil {
return nil, e
}
e = json.Unmarshal(b, r)
return r, e
}
func newFromIam() (*Client, error) {
con, e := net.DialTimeout("tcp", metadataIp+":80", 10*time.Millisecond)
if e != nil {
return nil, e
}
defer con.Close()
rsp, e := http.Get(securityCredentialsUrl)
if e != nil {
return nil, e
}
defer rsp.Body.Close()
if rsp.Status[0] != '2' {
return nil, fmt.Errorf("expected status 2xx, got %s", rsp.Status)
}
b, e := ioutil.ReadAll(rsp.Body)
if e != nil {
return nil, e
}
rolesString := string(b)
if rolesString == "" {
return nil, nil
}
roles := strings.Fields(string(b))
if len(roles) > 0 {
role := roles[0]
r, e := loadAwsCredentials(securityCredentialsUrl + role)
if e != nil {
return nil, e
}
client := &Client{Key: r.AccessKeyId, Secret: r.SecretAccessKey, SecurityToken: r.Token}
client.Region, _ = CurrentAwsRegion()
return client, nil
}
return nil, nil
}