MovableInkAWS 1.0.18 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c67a1debb22324f3f567c390c5a5baf61c714b56567712dd6269dd044b2ed9dd
4
- data.tar.gz: 9b02840d52d5ee4a4f99060ec274f9938617247e1df9468efbb352fb2ff2285f
3
+ metadata.gz: 88f1e4859005fa8150bd22e1c712a404ae26e79d39af2812ba5771b17c24fc70
4
+ data.tar.gz: b9611d91105694d9da25bc8551928f18e9561ef139ec7a291c0f88df5af93c87
5
5
  SHA512:
6
- metadata.gz: f547de79011e7fc83bb33db7d905da20c55d8a5308ef6e539193bfabacbcf1a5f0cde3a417ec454b6c24f0361ec1bef5b0d05ebbba5730f1cc5427643be28550
7
- data.tar.gz: 87ad9c72f123ef2e70d3ff35d89eeef5dac3948144668e88554de402eff5b71e733144d2d7dc9fd51473662a3e36fc479525c1ff01a63ac0d34c1046d1d2d2c7
6
+ metadata.gz: 9ff752b86badeb543f248e9b81781404ed35336b9a28bf0b9e59903945a26ba7c4a7323f77ec99a81c74421058c100fe5d7c82b172f65322433bc20bc6b3931a
7
+ data.tar.gz: 201f767f782b5d675834a1539e9e336b25b06773c7fa5e9162dc37d4c21fda0efbadec142bfd52f0ff7ef3082b5f0a2c2a7bc7833a110fe1c99f7f2ac26df079
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- MovableInkAWS (1.0.18)
4
+ MovableInkAWS (1.1.0)
5
5
  aws-sdk-athena (~> 1)
6
6
  aws-sdk-autoscaling (~> 1)
7
7
  aws-sdk-cloudwatch (~> 1)
@@ -20,8 +20,8 @@ GEM
20
20
  remote: https://rubygems.org/
21
21
  specs:
22
22
  aws-eventstream (1.1.0)
23
- aws-partitions (1.322.0)
24
- aws-sdk-athena (1.26.0)
23
+ aws-partitions (1.325.0)
24
+ aws-sdk-athena (1.27.0)
25
25
  aws-sdk-core (~> 3, >= 3.71.0)
26
26
  aws-sigv4 (~> 1.1)
27
27
  aws-sdk-autoscaling (1.37.0)
@@ -30,18 +30,18 @@ GEM
30
30
  aws-sdk-cloudwatch (1.38.0)
31
31
  aws-sdk-core (~> 3, >= 3.71.0)
32
32
  aws-sigv4 (~> 1.1)
33
- aws-sdk-core (3.97.0)
33
+ aws-sdk-core (3.97.1)
34
34
  aws-eventstream (~> 1, >= 1.0.2)
35
35
  aws-partitions (~> 1, >= 1.239.0)
36
36
  aws-sigv4 (~> 1.1)
37
37
  jmespath (~> 1.0)
38
- aws-sdk-ec2 (1.164.0)
38
+ aws-sdk-ec2 (1.165.0)
39
39
  aws-sdk-core (~> 3, >= 3.71.0)
40
40
  aws-sigv4 (~> 1.1)
41
- aws-sdk-elasticache (1.35.0)
41
+ aws-sdk-elasticache (1.36.0)
42
42
  aws-sdk-core (~> 3, >= 3.71.0)
43
43
  aws-sigv4 (~> 1.1)
44
- aws-sdk-kms (1.32.0)
44
+ aws-sdk-kms (1.33.0)
45
45
  aws-sdk-core (~> 3, >= 3.71.0)
46
46
  aws-sigv4 (~> 1.1)
47
47
  aws-sdk-rds (1.85.0)
@@ -50,14 +50,14 @@ GEM
50
50
  aws-sdk-route53 (1.35.0)
51
51
  aws-sdk-core (~> 3, >= 3.71.0)
52
52
  aws-sigv4 (~> 1.1)
53
- aws-sdk-s3 (1.67.0)
53
+ aws-sdk-s3 (1.67.1)
54
54
  aws-sdk-core (~> 3, >= 3.96.1)
55
55
  aws-sdk-kms (~> 1)
56
56
  aws-sigv4 (~> 1.1)
57
57
  aws-sdk-sns (1.24.0)
58
58
  aws-sdk-core (~> 3, >= 3.71.0)
59
59
  aws-sigv4 (~> 1.1)
60
- aws-sdk-ssm (1.79.0)
60
+ aws-sdk-ssm (1.80.0)
61
61
  aws-sdk-core (~> 3, >= 3.71.0)
62
62
  aws-sigv4 (~> 1.1)
63
63
  aws-sigv4 (1.1.4)
@@ -1,4 +1,5 @@
1
1
  require_relative 'aws/errors'
2
+ require_relative 'aws/metadata'
2
3
  require_relative 'aws/ec2'
3
4
  require_relative 'aws/sns'
4
5
  require_relative 'aws/autoscaling'
@@ -13,6 +14,7 @@ require 'aws-sdk-cloudwatch'
13
14
 
14
15
  module MovableInk
15
16
  class AWS
17
+ include Metadata
16
18
  include EC2
17
19
  include SNS
18
20
  include Autoscaling
@@ -93,14 +95,6 @@ module MovableInk
93
95
  self.class.regions
94
96
  end
95
97
 
96
- def availability_zone
97
- @availability_zone ||= begin
98
- az = `ec2metadata --availability-zone 2>/dev/null`.chomp
99
- raise(MovableInk::AWS::Errors::EC2Required) if az.empty?
100
- az
101
- end
102
- end
103
-
104
98
  def my_region
105
99
  @my_region ||= availability_zone.chop
106
100
  end
@@ -63,14 +63,6 @@ module MovableInk
63
63
  end
64
64
  end
65
65
 
66
- def instance_id
67
- @instance_id ||= begin
68
- id = `ec2metadata --instance-id 2>/dev/null`.chomp
69
- raise(MovableInk::AWS::Errors::EC2Required) if id.empty?
70
- id
71
- end
72
- end
73
-
74
66
  def instance_tags
75
67
  @instance_tags ||= run_with_backoff(quiet: true) do
76
68
  ec2.describe_tags({
@@ -79,14 +71,6 @@ module MovableInk
79
71
  end
80
72
  end
81
73
 
82
- def private_ipv4
83
- @ipv4 ||= begin
84
- ipv4 = `ec2metadata --local-ipv4 2>/dev/null`.chomp
85
- raise(MovableInk::AWS::Errors::EC2Required) if ipv4.empty?
86
- ipv4
87
- end
88
- end
89
-
90
74
  def me
91
75
  @me ||= all_instances.select{|instance| instance.instance_id == instance_id}.first rescue nil
92
76
  end
@@ -0,0 +1,62 @@
1
+ require 'net/http'
2
+
3
+ module MovableInk
4
+ class AWS
5
+ module Metadata
6
+ def http
7
+ @http ||= begin
8
+ http = Net::HTTP.new("169.254.169.254", 80)
9
+ http.open_timeout = 1
10
+ http.read_timeout = 1
11
+ http
12
+ end
13
+ end
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
+ ""
22
+ end
23
+
24
+ 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
+ end
31
+
32
+ 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
38
+ end
39
+
40
+ 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
46
+ end
47
+
48
+ private
49
+
50
+ def imds_token
51
+ begin
52
+ request = Net::HTTP::Put.new('/latest/api/token')
53
+ request['X-aws-ec2-metadata-token-ttl-seconds'] = 120
54
+ response = http.request(request)
55
+ response.body
56
+ rescue
57
+ nil
58
+ end
59
+ end
60
+ end
61
+ end
62
+ end
@@ -1,5 +1,5 @@
1
1
  module MovableInk
2
2
  class AWS
3
- VERSION = '1.0.18'
3
+ VERSION = '1.1.0'
4
4
  end
5
5
  end
@@ -2,15 +2,6 @@ require_relative '../lib/movable_ink/aws'
2
2
 
3
3
  describe MovableInk::AWS do
4
4
  context "outside EC2" do
5
- it "should raise an error if EC2 is required" do
6
- aws = MovableInk::AWS.new
7
- allow(aws).to receive(:`).with('ec2metadata --instance-id 2>/dev/null').and_return("")
8
- allow(aws).to receive(:`).with('ec2metadata --availability-zone 2>/dev/null').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
- end
13
-
14
5
  it 'doesnt raise an error if instance_id is set' do
15
6
  aws = MovableInk::AWS.new(instance_id: 'i-987654321')
16
7
  expect(aws.instance_id).to eq('i-987654321')
@@ -18,21 +9,9 @@ describe MovableInk::AWS do
18
9
  end
19
10
 
20
11
  context "inside EC2" do
21
- it "should call ec2metadata to get the instance ID" do
22
- aws = MovableInk::AWS.new
23
- expect(aws).to receive(:`).with('ec2metadata --instance-id 2>/dev/null').and_return("i-12345\n")
24
- expect(aws.instance_id).to eq('i-12345')
25
- end
26
-
27
- it "should call ec2metadata to get the availability zone" do
28
- aws = MovableInk::AWS.new
29
- expect(aws).to receive(:`).with('ec2metadata --availability-zone 2>/dev/null').and_return("us-east-1a\n")
30
- expect(aws.availability_zone).to eq('us-east-1a')
31
- end
32
-
33
12
  it "should find the datacenter by region" do
34
13
  aws = MovableInk::AWS.new
35
- expect(aws).to receive(:`).with('ec2metadata --availability-zone 2>/dev/null').and_return("us-east-1a\n")
14
+ expect(aws).to receive(:retrieve_metadata).with('placement/availability-zone').and_return("us-east-1a")
36
15
  expect(aws.datacenter).to eq('iad')
37
16
  end
38
17
 
@@ -4,17 +4,11 @@ describe MovableInk::AWS::EC2 do
4
4
  context "outside EC2" do
5
5
  it "should raise an error if trying to load mi_env outside of EC2" do
6
6
  aws = MovableInk::AWS.new
7
- allow(aws).to receive(:`).with('ec2metadata --instance-id 2>/dev/null').and_return("")
8
- allow(aws).to receive(:`).with('ec2metadata --availability-zone 2>/dev/null').and_return("")
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
9
  expect{ aws.mi_env }.to raise_error(MovableInk::AWS::Errors::EC2Required)
10
10
  end
11
11
 
12
- it 'should raise an error if trying to load private_ipv4 outside of EC2' do
13
- aws = MovableInk::AWS.new
14
- allow(aws).to receive(:`).with('ec2metadata --local-ipv4 2>/dev/null').and_return('')
15
- expect{ aws.private_ipv4 }.to raise_error(MovableInk::AWS::Errors::EC2Required)
16
- end
17
-
18
12
  it "should use the provided environment" do
19
13
  aws = MovableInk::AWS.new(environment: 'test')
20
14
  expect(aws.mi_env).to eq('test')
@@ -67,12 +61,6 @@ describe MovableInk::AWS::EC2 do
67
61
  expect(aws.mi_env).to eq('test')
68
62
  end
69
63
 
70
- it 'calls ec2metadata to get the private ipv4 address of the instance' do
71
- aws = MovableInk::AWS.new
72
- allow(aws).to receive(:`).with('ec2metadata --local-ipv4 2>/dev/null').and_return('10.0.0.1')
73
- expect(aws.private_ipv4).to eq('10.0.0.1')
74
- end
75
-
76
64
  context 'instance_tags' do
77
65
  it 'returns the tags of the current instance' do
78
66
  ec2.stub_responses(:describe_tags, tag_data)
@@ -0,0 +1,40 @@
1
+ require_relative '../lib/movable_ink/aws'
2
+
3
+ describe MovableInk::AWS::Metadata do
4
+ context 'outside ec2' do
5
+ it 'should raise an error if EC2 is required' do
6
+ 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
+ end
13
+
14
+ it 'should raise an error if trying to load private_ipv4 outside of EC2' do
15
+ 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)
18
+ end
19
+ end
20
+
21
+ context 'inside ec2' do
22
+ it 'calls the EC2 metadata service to get the private ipv4 address of the instance' do
23
+ aws = MovableInk::AWS.new
24
+ allow(aws).to receive(:retrieve_metadata).with('local-ipv4').and_return('10.0.0.1')
25
+ expect(aws.private_ipv4).to eq('10.0.0.1')
26
+ end
27
+
28
+ it 'calls the EC2 metadata service to get the instance ID' do
29
+ aws = MovableInk::AWS.new
30
+ expect(aws).to receive(:retrieve_metadata).with('instance-id').and_return("i-12345")
31
+ expect(aws.instance_id).to eq('i-12345')
32
+ end
33
+
34
+ it 'calls the EC2 metadata service to get the availability zone' do
35
+ aws = MovableInk::AWS.new
36
+ expect(aws).to receive(:retrieve_metadata).with('placement/availability-zone').and_return("us-east-1a")
37
+ expect(aws.availability_zone).to eq('us-east-1a')
38
+ end
39
+ end
40
+ end
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: 1.0.18
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Chesler
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-05-30 00:00:00.000000000 Z
11
+ date: 2020-06-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-core
@@ -212,6 +212,7 @@ files:
212
212
  - lib/movable_ink/aws/ec2.rb
213
213
  - lib/movable_ink/aws/elasticache.rb
214
214
  - lib/movable_ink/aws/errors.rb
215
+ - lib/movable_ink/aws/metadata.rb
215
216
  - lib/movable_ink/aws/route53.rb
216
217
  - lib/movable_ink/aws/s3.rb
217
218
  - lib/movable_ink/aws/sns.rb
@@ -221,6 +222,7 @@ files:
221
222
  - spec/aws_spec.rb
222
223
  - spec/ec2_spec.rb
223
224
  - spec/elasticache_spec.rb
225
+ - spec/metadata_spec.rb
224
226
  - spec/route53_spec.rb
225
227
  - spec/s3_spec.rb
226
228
  - spec/sns_spec.rb