forked from aws/amazon-ecs-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ec2_metadata_client.go
187 lines (159 loc) · 5.56 KB
/
ec2_metadata_client.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
178
179
180
181
182
183
184
185
186
187
// Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
package ec2
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"strings"
"time"
"github.com/aws/amazon-ecs-agent/agent/utils"
"github.com/cihub/seelog"
)
const (
EC2_METADATA_SERVICE_URL = "http://169.254.169.254"
SECURITY_CREDENTIALS_RESOURCE = "/2014-02-25/meta-data/iam/security-credentials/"
INSTANCE_IDENTITY_DOCUMENT_RESOURCE = "/2014-02-25/dynamic/instance-identity/document"
INSTANCE_IDENTITY_DOCUMENT_SIGNATURE_RESOURCE = "/2014-02-25/dynamic/instance-identity/signature"
SIGNED_INSTANCE_IDENTITY_DOCUMENT_RESOURCE = "/2014-02-25/dynamic/instance-identity/pkcs7"
EC2_METADATA_REQUEST_TIMEOUT = time.Duration(1 * time.Second)
)
const (
metadataRetries = 5
metadataRetryMaxDelay = 2 * time.Second
metadataRetryStartDelay = 250 * time.Millisecond
metadataRetryDelayMultiple = 2
)
type RoleCredentials struct {
Code string `json:"Code"`
LastUpdated time.Time `json:"LastUpdated"`
Type string `json:"Type"`
AccessKeyId string `json:"AccessKeyId"`
SecretAccessKey string `json:"SecretAccessKey"`
Token string `json:"Token"`
Expiration time.Time `json:"Expiration"`
}
type InstanceIdentityDocument struct {
InstanceId string `json:"instanceId"`
InstanceType string `json:"instanceType"`
Region string `json:"region"`
PrivateIp *string `json:"privateIp"`
AvailabilityZone string `json:"availabilityZone"`
}
type HttpClient interface {
Get(string) (*http.Response, error)
}
type EC2MetadataClient interface {
DefaultCredentials() (*RoleCredentials, error)
ReadResource(string) ([]byte, error)
InstanceIdentityDocument() (*InstanceIdentityDocument, error)
}
type ec2MetadataClientImpl struct {
client HttpClient
}
func NewEC2MetadataClient(httpClient HttpClient) EC2MetadataClient {
if httpClient == nil {
var lowTimeoutDial http.RoundTripper = &http.Transport{
Dial: (&net.Dialer{
Timeout: EC2_METADATA_REQUEST_TIMEOUT,
}).Dial,
}
httpClient = &http.Client{Transport: lowTimeoutDial}
}
return &ec2MetadataClientImpl{client: httpClient}
}
func (c *ec2MetadataClientImpl) DefaultCredentials() (*RoleCredentials, error) {
securityCredentialResp, err := c.ReadResource(SECURITY_CREDENTIALS_RESOURCE)
if err != nil {
return nil, err
}
securityCredentialList := strings.Split(strings.TrimSpace(string(securityCredentialResp)), "\n")
if len(securityCredentialList) == 0 {
return nil, errors.New("No security credentials in response")
}
defaultCredentialName := securityCredentialList[0]
rawResp, err := c.ReadResource(SECURITY_CREDENTIALS_RESOURCE + defaultCredentialName)
if err != nil {
return nil, err
}
var credential RoleCredentials
err = json.Unmarshal(rawResp, &credential)
if err != nil {
return nil, err
}
return &credential, nil
}
func (c *ec2MetadataClientImpl) InstanceIdentityDocument() (*InstanceIdentityDocument, error) {
rawIidResp, err := c.ReadResource(INSTANCE_IDENTITY_DOCUMENT_RESOURCE)
if err != nil {
return nil, err
}
var iid InstanceIdentityDocument
err = json.Unmarshal(rawIidResp, &iid)
if err != nil {
return nil, err
}
return &iid, nil
}
func (c *ec2MetadataClientImpl) ResourceServiceUrl(path string) string {
// TODO, override EC2_METADATA_SERVICE_URL based on the environment
return EC2_METADATA_SERVICE_URL + path
}
func (c *ec2MetadataClientImpl) ReadResource(path string) ([]byte, error) {
endpoint := c.ResourceServiceUrl(path)
var err error
var resp *http.Response
utils.RetryNWithBackoff(utils.NewSimpleBackoff(metadataRetryStartDelay, metadataRetryMaxDelay, metadataRetryDelayMultiple, 0.2), metadataRetries, func() error {
resp, err = c.client.Get(endpoint)
if err == nil && resp.StatusCode == 200 {
return nil
}
if resp != nil && resp.Body != nil {
resp.Body.Close()
}
if err == nil {
seelog.Warnf("Error accessing the EC2 Metadata Service; non-200 response: %v", resp.StatusCode)
return fmt.Errorf("Error contacting EC2 Metadata service; non-200 response: %v", resp.StatusCode)
} else {
seelog.Warnf("Error accessing the EC2 Metadata Service; retrying: %v", err)
return err
}
})
if resp != nil && resp.Body != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
return ioutil.ReadAll(resp.Body)
}
// DefaultClient is the client used for package level methods.
var DefaultClient = NewEC2MetadataClient(nil)
// ReadResource reads a given path from the EC2 metadata service using the
// default client
func ReadResource(path string) ([]byte, error) {
return DefaultClient.ReadResource(path)
}
// GetInstanceIdentityDocument returns an InstanceIdentityDocument read using
// the default client
func GetInstanceIdentityDocument() (*InstanceIdentityDocument, error) {
return DefaultClient.InstanceIdentityDocument()
}
// DefaultCredentials returns the instance's default role read using the default
// client
func DefaultCredentials() (*RoleCredentials, error) {
return DefaultClient.DefaultCredentials()
}