-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathaws_credentials.go
177 lines (145 loc) · 4.19 KB
/
aws_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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package s3
import (
"bufio"
"bytes"
"encoding/json"
"io"
"net"
"net/http"
"os"
"time"
)
type awsCredentials struct {
AccessKeyID string
SecretAccessKey string
SecurityToken string `json:"Token"`
Expiration time.Time
}
// expired checks to see if the temporary credentials from an IAM role are
// within 4 minutes of expiration (The IAM documentation says that new keys
// will be provisioned 5 minutes before the old keys expire). Credentials
// that do not have an Expiration cannot expire.
func (this *awsCredentials) expired() bool {
if this.Expiration.IsZero() {
// Credentials with no expiration can't expire
return false
}
expireTime := this.Expiration.Add(-4 * time.Minute)
// if t - 4 mins is before now, true
if expireTime.Before(time.Now()) {
return true
} else {
return false
}
}
const (
envAccessKey = "AWS_ACCESS_KEY"
envAccessKeyID = "AWS_ACCESS_KEY_ID"
envSecretKey = "AWS_SECRET_KEY"
envSecretAccessKey = "AWS_SECRET_ACCESS_KEY"
envSecurityToken = "AWS_SECURITY_TOKEN"
)
// ambientCredentials produces a set of credentials based on the environment
func ambientCredentials() awsCredentials {
// First use credentials from environment variables
newCredentials := loadCredentialsFromEnvironment()
// If there is no Access Key and you are on EC2, get the key from the role
if (newCredentials.AccessKeyID == "" || newCredentials.SecretAccessKey == "") && onEC2() {
newCredentials = getIAMRoleCredentials()
}
// If the key is expiring, get a new key
if newCredentials.expired() && onEC2() {
newCredentials = getIAMRoleCredentials()
}
return newCredentials
}
func loadCredentialsFromEnvironment() (credentials awsCredentials) {
credentials.AccessKeyID = os.Getenv(envAccessKeyID)
if credentials.AccessKeyID == "" {
credentials.AccessKeyID = os.Getenv(envAccessKey)
}
credentials.SecretAccessKey = os.Getenv(envSecretAccessKey)
if credentials.SecretAccessKey == "" {
credentials.SecretAccessKey = os.Getenv(envSecretKey)
}
credentials.SecurityToken = os.Getenv(envSecurityToken)
return credentials
}
// onEC2 checks to see if the program is running on an EC2 instance.
// It does this by looking for the EC2 metadata service.
// This caches that information in a struct so that it doesn't waste time.
func onEC2() bool {
if location == nil {
location = &awsLocation{}
}
if !(location.checked) {
c, err := net.DialTimeout("tcp", "169.254.169.254:80", time.Millisecond*100)
if err != nil {
location.ec2 = false
} else {
_ = c.Close()
location.ec2 = true
}
location.checked = true
}
return location.ec2
}
type awsLocation struct {
ec2 bool
checked bool
}
var location *awsLocation
// getIAMRoleList gets a list of the roles that are available to this instance
func getIAMRoleList() []string {
var roles []string
address := "http://169.254.169.254/latest/meta-data/iam/security-credentials/"
client := &http.Client{}
request, err := http.NewRequest("GET", address, nil)
if err != nil {
return roles
}
response, err := client.Do(request)
if err != nil {
return roles
}
defer closeHandle(response.Body)
scanner := bufio.NewScanner(response.Body)
for scanner.Scan() {
roles = append(roles, scanner.Text())
}
return roles
}
func getIAMRoleCredentials() awsCredentials {
roles := getIAMRoleList()
if len(roles) == 0 {
return awsCredentials{}
}
// Use the first role in the list
role := roles[0]
address := "http://169.254.169.254/latest/meta-data/iam/security-credentials/"
// Create the full URL of the role
var buffer bytes.Buffer
buffer.WriteString(address)
buffer.WriteString(role)
roleURL := buffer.String()
// Get the role
roleRequest, err := http.NewRequest("GET", roleURL, nil)
if err != nil {
return awsCredentials{}
}
client := &http.Client{Timeout: time.Second * 5}
roleResponse, err := client.Do(roleRequest)
if err != nil {
return awsCredentials{}
}
defer closeHandle(roleResponse.Body)
roleBuffer := new(bytes.Buffer)
_, _ = roleBuffer.ReadFrom(roleResponse.Body)
credentials := awsCredentials{}
err = json.Unmarshal(roleBuffer.Bytes(), &credentials)
if err != nil {
return awsCredentials{}
}
return credentials
}
func closeHandle(closer io.Closer) { _ = closer.Close() }