Skip to content

Commit 016d1e3

Browse files
committed
Update based on feedbacks
1 parent 97beabe commit 016d1e3

File tree

3 files changed

+3
-74
lines changed

3 files changed

+3
-74
lines changed

gems/aws-sdk-core/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
Unreleased Changes
22
------------------
33

4+
* Issue - Skip `Aws::InstanceProfileCredentials` instantiation when `ENV['AWS_EC2_METADATA_DISABLED']` is set to `true` in the credential resolution chain.
5+
46
* Issue - Refactor `InstanceProfileCredentials` to improve code clarity and documentation.
57

68
3.226.2 (2025-07-01)

gems/aws-sdk-core/lib/aws-sdk-core/instance_profile_credentials.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,6 @@ def refresh
200200
end
201201

202202
def retrieve_credentials
203-
return '{}' if ec2_metadata_disabled?
204-
205203
# Retry loading credentials a configurable number of times if
206204
# the instance metadata service is not responding.
207205
begin
@@ -258,10 +256,6 @@ def token_set?
258256
@token && !@token.expired?
259257
end
260258

261-
def ec2_metadata_disabled?
262-
ENV.fetch('AWS_EC2_METADATA_DISABLED', 'false').downcase == 'true'
263-
end
264-
265259
def open_connection
266260
uri = URI.parse(@endpoint)
267261
http = Net::HTTP.new(uri.hostname || @endpoint, uri.port || @port)

gems/aws-sdk-core/spec/aws/instance_profile_credentials_spec.rb

Lines changed: 1 addition & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -245,80 +245,13 @@ module Aws
245245
end
246246
end
247247

248-
describe 'disable IMDS flag' do
249-
it 'does not attempt to get credentials when disable flag set' do
250-
ENV['AWS_EC2_METADATA_DISABLED'] = 'true'
251-
expect(InstanceProfileCredentials.new.set?).to be(false)
252-
end
253-
254-
it 'has a disable flag which is not case sensitive' do
255-
ENV['AWS_EC2_METADATA_DISABLED'] = 'TrUe'
256-
expect(InstanceProfileCredentials.new.set?).to be(false)
257-
end
258-
259-
it 'ignores values other than true for the disable flag (secure)' do
260-
ENV['AWS_EC2_METADATA_DISABLED'] = '1'
261-
expiration = Time.now.utc + 3600
262-
resp = <<-JSON.strip
263-
{
264-
"Code" : "Success",
265-
"LastUpdated" : "2013-11-22T20:03:48Z",
266-
"Type" : "AWS-HMAC",
267-
"AccessKeyId" : "akid",
268-
"SecretAccessKey" : "secret",
269-
"Token" : "session-token",
270-
"Expiration" : "#{expiration.strftime('%Y-%m-%dT%H:%M:%SZ')}"
271-
}
272-
JSON
273-
stub_request(:put, ipv4_endpoint_token_path)
274-
.to_return(
275-
status: 200,
276-
body: "my-token\n",
277-
headers: { 'x-aws-ec2-metadata-token-ttl-seconds' => '21600' }
278-
)
279-
stub_request(:get, ipv4_endpoint + path)
280-
.with(headers: { 'x-aws-ec2-metadata-token' => 'my-token' })
281-
.to_return(status: 200, body: "profile-name\n")
282-
stub_request(:get, "#{ipv4_endpoint_creds_path}profile-name")
283-
.with(headers: { 'x-aws-ec2-metadata-token' => 'my-token' })
284-
.to_return(status: 200, body: resp)
285-
c = InstanceProfileCredentials.new(backoff: 0)
286-
expect(c.credentials.access_key_id).to eq('akid')
287-
expect(c.credentials.secret_access_key).to eq('secret')
288-
expect(c.credentials.session_token).to eq('session-token')
289-
end
290-
291-
it 'ignores values other than true for the disable flag (insecure)' do
292-
ENV['AWS_EC2_METADATA_DISABLED'] = '1'
293-
expiration = Time.now.utc + 3600
294-
resp = <<-JSON.strip
295-
{
296-
"Code" : "Success",
297-
"LastUpdated" : "2013-11-22T20:03:48Z",
298-
"Type" : "AWS-HMAC",
299-
"AccessKeyId" : "akid",
300-
"SecretAccessKey" : "secret",
301-
"Token" : "session-token",
302-
"Expiration" : "#{expiration.strftime('%Y-%m-%dT%H:%M:%SZ')}"
303-
}
304-
JSON
305-
stub_request(:put, ipv4_endpoint_token_path).to_return(status: 404)
306-
stub_request(:get, ipv4_endpoint + path).to_return(status: 200, body: "profile-name\n")
307-
stub_request(:get, "#{ipv4_endpoint_creds_path}profile-name").to_return(status: 200, body: resp)
308-
c = InstanceProfileCredentials.new(backoff: 0)
309-
expect(c.credentials.access_key_id).to eq('akid')
310-
expect(c.credentials.secret_access_key).to eq('secret')
311-
expect(c.credentials.session_token).to eq('session-token')
312-
end
313-
end
314-
315248
describe 'disable IMDS v1 flag' do
316249
before do
317250
ENV['AWS_EC2_METADATA_V1_DISABLED'] = 'true'
318251
end
319252

320253
it 'has a disable flag which is not case sensitive' do
321-
ENV['AWS_EC2_METADATA_DISABLED'] = 'TrUe'
254+
ENV['AWS_EC2_METADATA_V1_DISABLED'] = 'TrUe'
322255
c = InstanceProfileCredentials.new(backoff: 0)
323256
expect(c.disable_imds_v1).to be(true)
324257
end

0 commit comments

Comments
 (0)