Skip to content

Commit e0b2190

Browse files
Alena KastsiukavetsHelen1987
Alena Kastsiukavets
authored andcommitted
# Extract region from instance metadata instead of parsing availability zone
Extract region from instance metadata instead of parsing availability zone and updated tests accordingly # Why is this change needed? Information about region is available from instance metadata. Use it instead of unreliable availability zone name parsing # How does it address the issue? Extract region from instance metadata # How was this tested ? ``` bb release ``` cr https://code.amazon.com/reviews/CR-17976878
1 parent a6cf994 commit e0b2190

File tree

2 files changed

+11
-48
lines changed

2 files changed

+11
-48
lines changed

lib/instance_metadata.rb

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
require 'json'
33
require 'instance_agent'
44

5+
# https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-identity-documents.html
56
class InstanceMetadata
67

78
IP_ADDRESS = '169.254.169.254'
89
PORT = 80
910
HTTP_TIMEOUT = 30
10-
11+
1112
def self.host_identifier
12-
doc = JSON.parse(http_get('/latest/dynamic/instance-identity/document').strip)
1313
"arn:#{partition}:ec2:#{doc['region']}:#{doc['accountId']}:instance/#{doc['instanceId']}"
1414
end
1515

@@ -18,15 +18,7 @@ def self.partition
1818
end
1919

2020
def self.region
21-
begin
22-
az = http_get('/latest/meta-data/placement/availability-zone').strip
23-
rescue Net::ReadTimeout, Net::OpenTimeout
24-
raise InstanceMetadata::InstanceMetadataError.new('Not an EC2 instance and region not provided in the environment variable AWS_REGION. Please specify your region using environment variable AWS_REGION.')
25-
end
26-
27-
raise "Invalid availability zone name: #{az}" unless
28-
az =~ /[a-z]{2}-[a-z]+-\d+[a-z]/
29-
az.chop
21+
doc['region']
3022
end
3123

3224
def self.instance_id
@@ -57,4 +49,9 @@ def self.http_get(path)
5749
return response.body
5850
end
5951
end
52+
53+
private
54+
def self.doc
55+
JSON.parse(http_get('/latest/dynamic/instance-identity/document').strip)
56+
end
6057
end

test/instance_metadata_test.rb

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ class InstanceMetadataTest < InstanceAgentTestCase
55

66
def self.should_check_status_code(&blk)
77
should 'raise unless status code is 200' do
8-
@response.stubs(:code).returns(503)
98
@instance_doc_response.stubs(:code).returns(503)
109
assert_raise(&blk)
1110
end
@@ -20,23 +19,19 @@ def self.should_check_status_code(&blk)
2019
@host_identifier = "arn:#{@partition}:ec2:#{region}:#{account_id}:instance/#{instance_id}"
2120
@instance_document = JSON.dump({"accountId" => account_id, "region" => region, "instanceId" => instance_id})
2221
@http = mock()
23-
@response = mock()
2422
@instance_doc_response = mock()
2523
@partition_response = mock()
2624
@partition_response.stubs(:code).returns("200")
27-
@response.stubs(:code).returns("200")
2825
@instance_doc_response.stubs(:code).returns("200")
2926

3027
@http.stubs(:get).with('/latest/meta-data/services/partition').returns(@partition_response)
31-
@http.stubs(:get).with('/latest/meta-data/placement/availability-zone').returns(@response)
3228
@http.stubs(:get).with('/latest/dynamic/instance-identity/document').returns(@instance_doc_response)
3329
Net::HTTP.stubs(:start).yields(@http)
3430
end
3531

3632
context 'getting the host identifier' do
3733

3834
setup do
39-
@response.stubs(:body).returns("us-east-1a")
4035
@partition_response.stubs(:body).returns(@partition)
4136
@instance_doc_response.stubs(:body).returns(@instance_document)
4237
end
@@ -69,7 +64,7 @@ def self.should_check_status_code(&blk)
6964
context 'getting the region' do
7065

7166
setup do
72-
@response.stubs(:body).returns("us-east-1a")
67+
@instance_doc_response.stubs(:body).returns(@instance_document)
7368
end
7469

7570
should 'connect to the right host' do
@@ -79,41 +74,12 @@ def self.should_check_status_code(&blk)
7974

8075
should 'call the correct URL' do
8176
@http.expects(:get).
82-
with("/latest/meta-data/placement/availability-zone").
83-
returns(@response)
77+
with("/latest/dynamic/instance-identity/document").
78+
returns(@instance_doc_response)
8479
InstanceMetadata.region
8580
end
8681

8782
should 'return the region part of the AZ' do
88-
@response.stubs(:body).returns("us-east-1a")
89-
assert_equal("us-east-1", InstanceMetadata.region)
90-
end
91-
92-
should 'raise InstanceMetadataError if http read times out' do
93-
@http.expects(:get).
94-
with("/latest/meta-data/placement/availability-zone").
95-
raises(Net::ReadTimeout)
96-
assert_raised_with_message('Not an EC2 instance and region not provided in the environment variable AWS_REGION. Please specify your region using environment variable AWS_REGION.', InstanceMetadata::InstanceMetadataError) do
97-
InstanceMetadata.region
98-
end
99-
end
100-
101-
should 'raise InstanceMetadataError if http open times out' do
102-
@http.expects(:get).
103-
with("/latest/meta-data/placement/availability-zone").
104-
raises(Net::OpenTimeout)
105-
assert_raised_with_message('Not an EC2 instance and region not provided in the environment variable AWS_REGION. Please specify your region using environment variable AWS_REGION.', InstanceMetadata::InstanceMetadataError) do
106-
InstanceMetadata.region
107-
end
108-
end
109-
110-
should 'raise an error if the response is not an AZ' do
111-
@response.stubs(:body).returns("foobar")
112-
assert_raise { InstanceMetadata.region }
113-
end
114-
115-
should 'ignore whitespace in the body' do
116-
@response.stubs(:body).returns(" \tus-east-1a ")
11783
assert_equal("us-east-1", InstanceMetadata.region)
11884
end
11985

0 commit comments

Comments
 (0)