MovableInkAWS 2.2.1 → 2.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb083b13e12b669f0843ea32ea2b8b4280ccce9b5ac6055964d9ca1214d36f60
4
- data.tar.gz: 967f30f88ebef0135620f12930075da3bdeb05d4b449c2e5612a267f3197ff73
3
+ metadata.gz: add902b34bc1b872e499b1dd0d62ae1750fd7a2f136f954371e74b1c13a3f762
4
+ data.tar.gz: e691e43c549acd10af89ece802b06ebead70ebbd6e96b314f832947ea82738b3
5
5
  SHA512:
6
- metadata.gz: 687824a79299c74f15b6c9d1f3fded4663020670ae533619045e2097b99a38782348c3572d51bcb0d130d6b0e4e84b0ee78b778795792ba375b45118a94aaba9
7
- data.tar.gz: 85feb71050191506f20429f851fca622597090365fe6817b54091daf539f7c84918466b20ba8566d2c948c1326188f4a33bdc8b3989d2f80e9b82cb6ffeebedf
6
+ metadata.gz: 4666488ed8debc85bf3ca0e18d3f65ce23ccb0ac41061899cfe0bebadcfbc614a7f4fcdd4ac98342330b4a3b7e6f73d9199d3be3072b44effab4732b1aa5c759
7
+ data.tar.gz: 694dbc4e5b8d6eb4bf9c4966b7f149a225a3199b2d0f4093a9fe9ee949049f3d653ec92b4c85cba02bc56ff875ba028a652f34aa6050b007ed7b730c4d288f4b
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- MovableInkAWS (2.2.1)
4
+ MovableInkAWS (2.3.3)
5
5
  aws-sdk-athena (~> 1)
6
6
  aws-sdk-autoscaling (~> 1)
7
7
  aws-sdk-cloudwatch (~> 1)
@@ -24,47 +24,47 @@ GEM
24
24
  addressable (2.7.0)
25
25
  public_suffix (>= 2.0.2, < 5.0)
26
26
  aws-eventstream (1.1.1)
27
- aws-partitions (1.433.0)
27
+ aws-partitions (1.445.0)
28
28
  aws-sdk-athena (1.37.0)
29
29
  aws-sdk-core (~> 3, >= 3.112.0)
30
30
  aws-sigv4 (~> 1.1)
31
- aws-sdk-autoscaling (1.58.0)
31
+ aws-sdk-autoscaling (1.60.0)
32
32
  aws-sdk-core (~> 3, >= 3.112.0)
33
33
  aws-sigv4 (~> 1.1)
34
- aws-sdk-cloudwatch (1.50.0)
34
+ aws-sdk-cloudwatch (1.51.0)
35
35
  aws-sdk-core (~> 3, >= 3.112.0)
36
36
  aws-sigv4 (~> 1.1)
37
- aws-sdk-core (3.113.0)
37
+ aws-sdk-core (3.114.0)
38
38
  aws-eventstream (~> 1, >= 1.0.2)
39
39
  aws-partitions (~> 1, >= 1.239.0)
40
40
  aws-sigv4 (~> 1.1)
41
41
  jmespath (~> 1.0)
42
- aws-sdk-ec2 (1.227.0)
42
+ aws-sdk-ec2 (1.234.0)
43
43
  aws-sdk-core (~> 3, >= 3.112.0)
44
44
  aws-sigv4 (~> 1.1)
45
45
  aws-sdk-eks (1.51.0)
46
46
  aws-sdk-core (~> 3, >= 3.112.0)
47
47
  aws-sigv4 (~> 1.1)
48
- aws-sdk-elasticache (1.54.0)
48
+ aws-sdk-elasticache (1.55.0)
49
49
  aws-sdk-core (~> 3, >= 3.112.0)
50
50
  aws-sigv4 (~> 1.1)
51
51
  aws-sdk-kms (1.43.0)
52
52
  aws-sdk-core (~> 3, >= 3.112.0)
53
53
  aws-sigv4 (~> 1.1)
54
- aws-sdk-rds (1.117.0)
54
+ aws-sdk-rds (1.118.0)
55
55
  aws-sdk-core (~> 3, >= 3.112.0)
56
56
  aws-sigv4 (~> 1.1)
57
- aws-sdk-route53 (1.47.0)
57
+ aws-sdk-route53 (1.49.0)
58
58
  aws-sdk-core (~> 3, >= 3.112.0)
59
59
  aws-sigv4 (~> 1.1)
60
- aws-sdk-s3 (1.91.0)
60
+ aws-sdk-s3 (1.93.1)
61
61
  aws-sdk-core (~> 3, >= 3.112.0)
62
62
  aws-sdk-kms (~> 1)
63
63
  aws-sigv4 (~> 1.1)
64
64
  aws-sdk-sns (1.39.0)
65
65
  aws-sdk-core (~> 3, >= 3.112.0)
66
66
  aws-sigv4 (~> 1.1)
67
- aws-sdk-ssm (1.106.0)
67
+ aws-sdk-ssm (1.108.0)
68
68
  aws-sdk-core (~> 3, >= 3.112.0)
69
69
  aws-sigv4 (~> 1.1)
70
70
  aws-sigv4 (1.2.3)
@@ -156,24 +156,24 @@ module MovableInk
156
156
 
157
157
  def map_ec2_consul_endpoint(endpoint)
158
158
  OpenStruct.new ({
159
- private_ip_address: endpoint.Node['Address'],
160
- instance_id: endpoint.Node['Meta']['instance_id'],
159
+ private_ip_address: endpoint.Node.dig('Address'),
160
+ instance_id: endpoint.Node.dig('Meta', 'instance_id'),
161
161
  tags: [
162
162
  {
163
163
  key: 'Name',
164
- value: endpoint.Node['Node']
164
+ value: endpoint.Node.dig('Node')
165
165
  },
166
166
  {
167
167
  key: 'mi:roles',
168
- value: endpoint.Node['Meta']['mi_roles']
168
+ value: endpoint.Node.dig('Meta', 'mi_roles')
169
169
  },
170
170
  {
171
171
  key: 'mi:monitoring_roles',
172
- value: endpoint.Node['Meta']['mi_monitoring_roles']
172
+ value: endpoint.Node.dig('Meta', 'mi_monitoring_roles')
173
173
  }
174
174
  ],
175
175
  placement: {
176
- availability_zone: endpoint.Node['Meta']['availability_zone']
176
+ availability_zone: endpoint.Node.dig('Meta', 'availability_zone')
177
177
  }
178
178
  })
179
179
  end
@@ -3,7 +3,7 @@ module MovableInk
3
3
  module Errors
4
4
  class ServiceError < StandardError; end
5
5
  class FailedWithBackoff < StandardError; end
6
- class EC2Required < StandardError; end
6
+ class MetadataTimeout < StandardError; end
7
7
  class NoEnvironmentTagError < StandardError; end
8
8
  class InvalidDiscoveryTypeError < StandardError; end
9
9
  class RoleNameRequiredError < StandardError; end
@@ -3,59 +3,55 @@ require 'net/http'
3
3
  module MovableInk
4
4
  class AWS
5
5
  module Metadata
6
- def http
7
- @http ||= begin
6
+ def http(timeout_seconds: 1)
7
+ @http = begin
8
8
  http = Net::HTTP.new("169.254.169.254", 80)
9
- http.open_timeout = 1
10
- http.read_timeout = 1
9
+ http.open_timeout = timeout_seconds
10
+ http.read_timeout = timeout_seconds
11
11
  http
12
12
  end
13
13
  end
14
14
 
15
- def retrieve_metadata(key)
16
- request = Net::HTTP::Get.new("/latest/meta-data/#{key}")
17
- request['X-aws-ec2-metadata-token'] = imds_token
18
- response = http.request(request)
19
- response.body
20
- rescue
21
- ""
15
+ def retrieve_metadata(key, tries: 3)
16
+ tries.times do |num|
17
+ num += 1
18
+ request = Net::HTTP::Get.new("/latest/meta-data/#{key}")
19
+ request['X-aws-ec2-metadata-token'] = imds_token
20
+ response = http(timeout_seconds: num * 3).request(request)
21
+ return response.body
22
+ rescue Net::OpenTimeout, Net::ReadTimeout, Errno::EHOSTDOWN
23
+ sleep(num * 2)
24
+ end
25
+
26
+ raise MovableInk::AWS::Errors::MetadataTimeout
22
27
  end
23
28
 
24
29
  def availability_zone
25
- @availability_zone ||= begin
26
- az = retrieve_metadata('placement/availability-zone')
27
- raise(MovableInk::AWS::Errors::EC2Required) if az.empty?
28
- az
29
- end
30
+ @availability_zone ||= retrieve_metadata('placement/availability-zone')
30
31
  end
31
32
 
32
33
  def instance_id
33
- @instance_id ||= begin
34
- id = retrieve_metadata('instance-id')
35
- raise(MovableInk::AWS::Errors::EC2Required) if id.empty?
36
- id
37
- end
34
+ @instance_id ||= retrieve_metadata('instance-id')
38
35
  end
39
36
 
40
37
  def private_ipv4
41
- @ipv4 ||= begin
42
- ipv4 = retrieve_metadata('local-ipv4')
43
- raise(MovableInk::AWS::Errors::EC2Required) if ipv4.empty?
44
- ipv4
45
- end
38
+ @ipv4 ||= retrieve_metadata('local-ipv4')
46
39
  end
47
40
 
48
41
  private
49
42
 
50
- def imds_token
51
- begin
43
+ def imds_token(tries: 3)
44
+ tries.times do |num|
45
+ num += 1
52
46
  request = Net::HTTP::Put.new('/latest/api/token')
53
47
  request['X-aws-ec2-metadata-token-ttl-seconds'] = 120
54
- response = http.request(request)
55
- response.body
56
- rescue
57
- nil
48
+ response = http(timeout_seconds: num * 3).request(request)
49
+ return response.body
50
+ rescue Net::OpenTimeout, Net::ReadTimeout, Errno::EHOSTDOWN
51
+ sleep(num * 2)
58
52
  end
53
+
54
+ raise MovableInk::AWS::Errors::MetadataTimeout
59
55
  end
60
56
  end
61
57
  end
@@ -65,6 +65,8 @@ module MovableInk
65
65
  rescue Aws::EC2::Errors::RequestLimitExceeded,
66
66
  Aws::EC2::Errors::ResourceAlreadyAssociated,
67
67
  Aws::EC2::Errors::Unavailable,
68
+ Aws::EC2::Errors::InternalError,
69
+ Aws::EC2::Errors::Http503Error,
68
70
  Aws::EKS::Errors::TooManyRequestsException,
69
71
  Aws::SNS::Errors::ThrottledException,
70
72
  Aws::SNS::Errors::Throttling,
@@ -77,6 +79,9 @@ module MovableInk
77
79
  Aws::Route53::Errors::ServiceUnavailable,
78
80
  Aws::SSM::Errors::TooManyUpdates,
79
81
  Aws::SSM::Errors::ThrottlingException,
82
+ Aws::SSM::Errors::InternalServerError,
83
+ Aws::SSM::Errors::Http503Error,
84
+ Aws::SSM::Errors::Http502Error,
80
85
  Aws::Athena::Errors::ThrottlingException,
81
86
  MovableInk::AWS::Errors::NoEnvironmentTagError
82
87
  sleep_time = (num+1)**2 + rand(10)
@@ -103,7 +108,11 @@ module MovableInk
103
108
  end
104
109
 
105
110
  def my_region
106
- @my_region ||= availability_zone.chop
111
+ @my_region ||= if ENV['AWS_REGION'].nil?
112
+ availability_zone.chop
113
+ else
114
+ ENV['AWS_REGION']
115
+ end
107
116
  end
108
117
 
109
118
  def datacenter(region: my_region)
@@ -1,5 +1,5 @@
1
1
  module MovableInk
2
2
  class AWS
3
- VERSION = '2.2.1'
3
+ VERSION = '2.3.3'
4
4
  end
5
5
  end
data/spec/aws_spec.rb CHANGED
@@ -1,17 +1,37 @@
1
1
  require_relative '../lib/movable_ink/aws'
2
+ require 'webmock/rspec'
2
3
 
3
4
  describe MovableInk::AWS do
4
5
  context "outside EC2" do
6
+ before(:each) do
7
+ allow_any_instance_of(MovableInk::AWS).to receive(:sleep).and_return(true)
8
+ end
9
+
10
+ after(:each) { ENV['AWS_REGION'] = nil }
11
+
5
12
  it 'doesnt raise an error if instance_id is set' do
6
13
  aws = MovableInk::AWS.new(instance_id: 'i-987654321')
7
14
  expect(aws.instance_id).to eq('i-987654321')
8
15
  end
16
+
17
+ it 'doesnt raise if AWS_REGION is set' do
18
+ ENV['AWS_REGION'] = 'us-east-1'
19
+ miaws = MovableInk::AWS.new
20
+ expect(miaws.my_region).to eq('us-east-1')
21
+ end
22
+
23
+ it 'raises when AWS_REGION is not set and the metadata service is not available' do
24
+ miaws = MovableInk::AWS.new
25
+ # stub an error making a request to the metadata api
26
+ stub_request(:put, 'http://169.254.169.254/latest/api/token').to_raise(Net::OpenTimeout)
27
+ expect { miaws.my_region }.to raise_error(MovableInk::AWS::Errors::MetadataTimeout)
28
+ end
9
29
  end
10
30
 
11
31
  context "inside EC2" do
12
32
  it "should find the datacenter by region" do
13
33
  aws = MovableInk::AWS.new
14
- expect(aws).to receive(:retrieve_metadata).with('placement/availability-zone').and_return("us-east-1a")
34
+ expect(aws).to receive(:availability_zone).and_return("us-east-1a")
15
35
  expect(aws.datacenter).to eq('iad')
16
36
  end
17
37
 
data/spec/ec2_spec.rb CHANGED
@@ -2,12 +2,16 @@ require_relative '../lib/movable_ink/aws'
2
2
  require 'webmock/rspec'
3
3
 
4
4
  describe MovableInk::AWS::EC2 do
5
+ before(:each) do
6
+ allow_any_instance_of(MovableInk::AWS).to receive(:sleep).and_return(true)
7
+ end
8
+
5
9
  context "outside EC2" do
6
10
  it "should raise an error if trying to load mi_env outside of EC2" do
7
11
  aws = MovableInk::AWS.new
8
- allow(aws).to receive(:retrieve_metadata).with('instance-id').and_return("")
9
- allow(aws).to receive(:retrieve_metadata).with('placement/availability-zone').and_return("")
10
- expect{ aws.mi_env }.to raise_error(MovableInk::AWS::Errors::EC2Required)
12
+ # stub an error making a request to the metadata api
13
+ stub_request(:put, 'http://169.254.169.254/latest/api/token').to_raise(Net::OpenTimeout)
14
+ expect{ aws.mi_env }.to raise_error(MovableInk::AWS::Errors::MetadataTimeout)
11
15
  end
12
16
 
13
17
  it "should use the provided environment" do
@@ -17,12 +21,16 @@ describe MovableInk::AWS::EC2 do
17
21
 
18
22
  it "should not find a 'me'" do
19
23
  aws = MovableInk::AWS.new
24
+ # stub an error making a request to the metadata api
25
+ stub_request(:put, 'http://169.254.169.254/latest/api/token').to_raise(Net::OpenTimeout)
20
26
  expect(aws.me).to eq(nil)
21
27
  end
22
28
  end
23
29
 
24
30
  context "inside EC2" do
25
- WebMock.allow_net_connect!
31
+ before(:each) { WebMock.allow_net_connect! }
32
+ after(:each) { WebMock.disable_net_connect! }
33
+
26
34
  let(:aws) { MovableInk::AWS.new }
27
35
  let(:ec2) { Aws::EC2::Client.new(stub_responses: true) }
28
36
  let(:tag_data) { ec2.stub_data(:describe_tags, tags: [
@@ -474,6 +482,7 @@ describe MovableInk::AWS::EC2 do
474
482
 
475
483
  ojos_instances = aws.instances(role: 'ojos', availability_zone: other_availability_zone, discovery_type: 'consul')
476
484
  expect(ojos_instances.map{|i| i.tags.first[:value]}).to eq(['ojos_instance2', 'ojos_instance3'])
485
+ expect(ojos_instances.map{|i| i.tags[1][:value]}).to eq(['ojos', 'ojos'])
477
486
  end
478
487
 
479
488
  it "returns backends that are synced from consul-k8s" do
@@ -1,20 +1,25 @@
1
1
  require_relative '../lib/movable_ink/aws'
2
+ require 'webmock/rspec'
2
3
 
3
4
  describe MovableInk::AWS::Metadata do
5
+ before(:each) do
6
+ allow_any_instance_of(MovableInk::AWS).to receive(:sleep).and_return(true)
7
+ end
8
+
4
9
  context 'outside ec2' do
5
- it 'should raise an error if EC2 is required' do
10
+ it 'should raise an error if the metadata service times out' do
6
11
  aws = MovableInk::AWS.new
7
- allow(aws).to receive(:retrieve_metadata).with('instance-id').and_return("")
8
- allow(aws).to receive(:retrieve_metadata).with('placement/availability-zone').and_return("")
9
-
10
- expect{ aws.instance_id }.to raise_error(MovableInk::AWS::Errors::EC2Required)
11
- expect{ aws.availability_zone }.to raise_error(MovableInk::AWS::Errors::EC2Required)
12
+ # stub an error making a request to the metadata api
13
+ stub_request(:put, 'http://169.254.169.254/latest/api/token').to_raise(Net::OpenTimeout)
14
+ expect{ aws.instance_id }.to raise_error(MovableInk::AWS::Errors::MetadataTimeout)
15
+ expect{ aws.availability_zone }.to raise_error(MovableInk::AWS::Errors::MetadataTimeout)
12
16
  end
13
17
 
14
18
  it 'should raise an error if trying to load private_ipv4 outside of EC2' do
15
19
  aws = MovableInk::AWS.new
16
- allow(aws).to receive(:retrieve_metadata).with('local-ipv4').and_return('')
17
- expect{ aws.private_ipv4 }.to raise_error(MovableInk::AWS::Errors::EC2Required)
20
+ # stub an error making a request to the metadata api
21
+ stub_request(:put, 'http://169.254.169.254/latest/api/token').to_raise(Net::OpenTimeout)
22
+ expect{ aws.private_ipv4 }.to raise_error(MovableInk::AWS::Errors::MetadataTimeout)
18
23
  end
19
24
  end
20
25
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: MovableInkAWS
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 2.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Chesler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-17 00:00:00.000000000 Z
11
+ date: 2021-12-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-core
@@ -276,7 +276,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
276
276
  - !ruby/object:Gem::Version
277
277
  version: '0'
278
278
  requirements: []
279
- rubygems_version: 3.0.3
279
+ rubygems_version: 3.0.3.1
280
280
  signing_key:
281
281
  specification_version: 4
282
282
  summary: AWS Utility methods for MovableInk