/
ecs_credentials_spec.rb
190 lines (159 loc) · 6.42 KB
/
ecs_credentials_spec.rb
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
188
189
190
require 'spec_helper'
module Aws
describe ECSCredentials do
let(:path) { '/latest/credentials?id=foobarbaz' }
describe 'without instance metadata service present' do
[
Errno::EHOSTUNREACH,
Errno::ECONNREFUSED,
SocketError,
Timeout::Error,
].each do |error_class|
it "returns no credentials for #{error_class}" do
stub_request(:get, "http://169.254.170.2#{path}").to_raise(error_class)
expect(ECSCredentials.new(credential_path: path, backoff:0).set?).to be(false)
end
end
end
describe 'with ECS credential service present' do
before(:each) do
stub_const('ENV', {
"AWS_CONTAINER_CREDENTIALS_RELATIVE_URI" => path
})
end
let(:expiration) { Time.now.utc + 3600 }
let(:expiration2) { expiration + 3600 }
let(:resp) { <<-JSON.strip }
{
"RoleArn" : "arn:aws:iam::123456789012:role/BarFooRole",
"AccessKeyId" : "akid",
"SecretAccessKey" : "secret",
"Token" : "session-token",
"Expiration" : "#{expiration.strftime('%Y-%m-%dT%H:%M:%SZ')}"
}
JSON
let(:resp2) { <<-JSON.strip }
{
"RoleArn" : "arn:aws:iam::123456789012:role/BarFooRole",
"AccessKeyId" : "akid-2",
"SecretAccessKey" : "secret-2",
"Token" : "session-token-2",
"Expiration" : "#{(expiration2).strftime('%Y-%m-%dT%H:%M:%SZ')}"
}
JSON
before(:each) do
stub_request(:get, "http://169.254.170.2#{path}").
to_return(:status => 200, :body => resp).
to_return(:status => 200, :body => resp2)
end
it 'populates credentials from the instance profile' do
c = ECSCredentials.new(backoff:0)
expect(c.credentials.access_key_id).to eq('akid')
expect(c.credentials.secret_access_key).to eq('secret')
expect(c.credentials.session_token).to eq('session-token')
expect(c.expiration.to_s).to eq(expiration.to_s)
end
it 're-queries the metadata service when #refresh! is called' do
c = ECSCredentials.new
c.refresh!
expect(c.credentials.access_key_id).to eq('akid-2')
expect(c.credentials.secret_access_key).to eq('secret-2')
expect(c.credentials.session_token).to eq('session-token-2')
expect(c.expiration.to_s).to eq(expiration2.to_s)
end
it 'retries if the first load fails' do
stub_request(:get, "http://169.254.170.2#{path}").
to_return(:status => 200, :body => resp2)
c = ECSCredentials.new(backoff:0)
expect(c.credentials.access_key_id).to eq('akid-2')
expect(c.credentials.secret_access_key).to eq('secret-2')
expect(c.credentials.session_token).to eq('session-token-2')
expect(c.expiration.to_s).to eq(expiration2.to_s)
end
it 'retries if get profile response is invalid JSON' do
stub_request(:get, "http://169.254.170.2#{path}").
to_return(:status => 200, :body => ' ').
to_return(:status => 200, :body => '').
to_return(:status => 200, :body => '{').
to_return(:status => 200, :body => resp2)
c = ECSCredentials.new(backoff:0)
expect(c.credentials.access_key_id).to eq('akid-2')
expect(c.credentials.secret_access_key).to eq('secret-2')
expect(c.credentials.session_token).to eq('session-token-2')
expect(c.expiration.to_s).to eq(expiration2.to_s)
end
it 'retries invalid JSON exactly 3 times' do
stub_request(:get, "http://169.254.170.2#{path}").
to_return(:status => 200, :body => '').
to_return(:status => 200, :body => ' ').
to_return(:status => 200, :body => '{').
to_return(:status => 200, :body => ' ')
expect {
ECSCredentials.new(backoff:0)
}.to raise_error(
Aws::Errors::MetadataParserError,
'Failed to parse metadata service response.'
)
end
it 'retries errors parsing expiration time 3 times' do
stub_request(:get, "http://169.254.170.2#{path}").
to_return(:status => 200, :body => '{ "Expiration": "Expiration" }').
to_return(:status => 200, :body => '{ "Expiration": "Expiration" }').
to_return(:status => 200, :body => '{ "Expiration": "Expiration" }').
to_return(:status => 200, :body => '{ "Expiration": "Expiration" }')
expect {
ECSCredentials.new(backoff:0)
}.to raise_error(ArgumentError)
end
describe 'auto refreshing' do
# expire in 4 minutes
let(:expiration) { Time.now.utc + 299 }
it 'auto-refreshes within 5 minutes from expiration' do
c = ECSCredentials.new
expect(c.credentials.access_key_id).to eq('akid-2')
expect(c.credentials.secret_access_key).to eq('secret-2')
expect(c.credentials.session_token).to eq('session-token-2')
expect(c.expiration.to_s).to eq(expiration2.to_s)
end
end
describe 'failure cases' do
let(:resp) { '{}' }
it 'given an empty response, entry credentials are returned' do
# This handles the case when the service response but returns
# a JSON document without credentials (error cases)
stub_request(:get, "http://169.254.170.2#{path}").
to_return(:status => 200, :body => resp)
c = ECSCredentials.new
expect(c.set?).to be(false)
expect(c.credentials.access_key_id).to be(nil)
expect(c.credentials.secret_access_key).to be(nil)
expect(c.credentials.session_token).to be(nil)
expect(c.expiration).to be(nil)
end
end
end
describe '#retries' do
before(:each) do
stub_const('ENV', {
"AWS_CONTAINER_CREDENTIALS_RELATIVE_URI" => path
})
end
it 'defaults to 0' do
stub_request(:get, "http://169.254.170.2#{path}").to_raise(SocketError)
expect(ECSCredentials.new(backoff:0).retries).to be(5)
end
it 'keeps trying "retries" times, with exponential backoff' do
expected_request = stub_request(:get, "http://169.254.170.2#{path}").
to_raise(Errno::ECONNREFUSED)
expect(Kernel).to receive(:sleep).with(1)
expect(Kernel).to receive(:sleep).with(2)
expect(Kernel).to receive(:sleep).with(4)
ECSCredentials.new(
backoff: lambda{|n| Kernel.sleep(2 ** n ) },
retries:3
)
assert_requested(expected_request, times:4)
end
end
end
end