ec2-host 1.0.1 → 1.0.2

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
  SHA1:
3
- metadata.gz: 8f749471944700121c781943703aa72e8b8a07d9
4
- data.tar.gz: 9db09c8155b61efb38851d579740fb809ce30aab
3
+ metadata.gz: ad6de8f55b767b6131c58d34025197f7d50b5bf9
4
+ data.tar.gz: '0586edf0954b7480db15634a168ae2eced6a0acf'
5
5
  SHA512:
6
- metadata.gz: 7b1098d2a578af8b30dab95e87ebe1f4b89e23bfab7f8bf51370549051413c75756ce38f210e98fcabb28f325a01f5be399e0229922f11d5683b2a8079c3d016
7
- data.tar.gz: e5a6fe2fae9935252dc4af230c3726fec9a43bb1824e7e11cae1615f6d03be85499b0e397f5dc19b4bb9e603ff2d219c0b78e90e065b1f4a09bfb469b3fbdb12
6
+ metadata.gz: 5353b0fe64005d1f69f3197ac767d87058aafe0a03f9d101859491d6486c4d828761b1969571bffd69c9d4714b7e1267dad0971cfebccbca409defaac2d66fc6
7
+ data.tar.gz: b6576721559e850cf1db0abe94aaf1bbb83272a7be7e562957df7cfab0e5d09fa7c19190db2e5428571de28943c3c6a1bff3ac66abfa2d59cfd57fea77ae1ae3
@@ -1,3 +1,11 @@
1
+ # 1.0.2 (2019/02/05)
2
+
3
+ Enhancements:
4
+
5
+ * Support AWS\_SHARED\_CREDENTIALS\_FILE environment variable.
6
+ * Support ~/.aws/config with instance profile.
7
+ * Remove dependency on aws\_config gem.
8
+
1
9
  # 1.0.1 (2019/02/05)
2
10
 
3
11
  Enhancements:
@@ -134,7 +134,7 @@
134
134
  <li class="public ">
135
135
  <span class="summary_signature">
136
136
 
137
- <a href="#aws_credential_file-class_method" title="aws_credential_file (class method)">.<strong>aws_credential_file</strong> &#x21d2; Object </a>
137
+ <a href="#aws_credentials_file-class_method" title="aws_credentials_file (class method)">.<strong>aws_credentials_file</strong> &#x21d2; Object </a>
138
138
 
139
139
 
140
140
 
@@ -658,11 +658,11 @@
658
658
  </div>
659
659
 
660
660
 
661
- <span id="aws_credential_file=-class_method"></span>
661
+ <span id="aws_credentials_file=-class_method"></span>
662
662
  <div class="method_details ">
663
- <h3 class="signature " id="aws_credential_file-class_method">
663
+ <h3 class="signature " id="aws_credentials_file-class_method">
664
664
 
665
- .<strong>aws_credential_file</strong> &#x21d2; <tt>Object</tt>
665
+ .<strong>aws_credentials_file</strong> &#x21d2; <tt>Object</tt>
666
666
 
667
667
 
668
668
 
@@ -684,8 +684,8 @@
684
684
  <td>
685
685
  <pre class="code"><span class="info file"># File 'lib/ec2/host/config.rb', line 55</span>
686
686
 
687
- <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_aws_credential_file'>aws_credential_file</span>
688
- <span class='ivar'>@aws_credential_file</span> <span class='op'>||=</span>
687
+ <span class='kw'>def</span> <span class='kw'>self</span><span class='period'>.</span><span class='id identifier rubyid_aws_credentials_file'>aws_credentials_file</span>
688
+ <span class='ivar'>@aws_credentials_file</span> <span class='op'>||=</span>
689
689
  <span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>AWS_CREDENTIALS_FILE</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='op'>||</span> <span class='id identifier rubyid_config'>config</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>AWS_CREDENTIALS_FILE</span><span class='tstring_end'>&#39;</span></span><span class='comma'>,</span> <span class='kw'>nil</span><span class='rparen'>)</span> <span class='op'>||</span>
690
690
  <span class='const'>ENV</span><span class='lbracket'>[</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>AWS_CREDENTIAL_FILE</span><span class='tstring_end'>&#39;</span></span><span class='rbracket'>]</span> <span class='op'>||</span> <span class='id identifier rubyid_config'>config</span><span class='period'>.</span><span class='id identifier rubyid_fetch'>fetch</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>AWS_CREDENTIAL_FILE</span><span class='tstring_end'>&#39;</span></span><span class='comma'>,</span> <span class='kw'>nil</span><span class='rparen'>)</span> <span class='op'>||</span> <span class='comment'># ref. aws cli (supported lately)
691
691
  </span> <span class='const'>File</span><span class='period'>.</span><span class='id identifier rubyid_expand_path'>expand_path</span><span class='lparen'>(</span><span class='tstring'><span class='tstring_beg'>&#39;</span><span class='tstring_content'>~/.aws/credentials</span><span class='tstring_end'>&#39;</span></span><span class='rparen'>)</span>
@@ -1373,4 +1373,4 @@
1373
1373
 
1374
1374
  </div>
1375
1375
  </body>
1376
- </html>
1376
+ </html>
@@ -322,4 +322,4 @@ $ env $(cat .env) terraform apply</code></pre>
322
322
 
323
323
  </div>
324
324
  </body>
325
- </html>
325
+ </html>
@@ -322,4 +322,4 @@ $ env $(cat .env) terraform apply</code></pre>
322
322
 
323
323
  </div>
324
324
  </body>
325
- </html>
325
+ </html>
@@ -94,7 +94,7 @@
94
94
 
95
95
  <li class="odd ">
96
96
  <div class="item">
97
- <span class='object_link'><a href="EC2/Host/Config.html#aws_credential_file-class_method" title="EC2::Host::Config.aws_credential_file (method)">aws_credential_file</a></span>
97
+ <span class='object_link'><a href="EC2/Host/Config.html#aws_credentials_file-class_method" title="EC2::Host::Config.aws_credentials_file (method)">aws_credentials_file</a></span>
98
98
  <small>EC2::Host::Config</small>
99
99
  </div>
100
100
  </li>
@@ -18,7 +18,7 @@ Gem::Specification.new do |gem|
18
18
 
19
19
  gem.add_runtime_dependency 'aws-sdk-ec2'
20
20
  gem.add_runtime_dependency 'dotenv'
21
- gem.add_runtime_dependency 'aws_config'
21
+ gem.add_runtime_dependency 'inifile'
22
22
 
23
23
  gem.add_development_dependency 'yard'
24
24
  gem.add_development_dependency 'rdoc'
@@ -1,5 +1,5 @@
1
1
  require 'dotenv'
2
- require 'aws_config'
2
+ require 'inifile'
3
3
  Dotenv.load
4
4
 
5
5
  class EC2
@@ -11,7 +11,7 @@ class EC2
11
11
  :aws_profile,
12
12
  :aws_access_key_id,
13
13
  :aws_secret_access_key,
14
- :aws_credential_file,
14
+ :aws_credentials_file,
15
15
  :log_level,
16
16
  :hostname_tag,
17
17
  :roles_tag,
@@ -33,7 +33,7 @@ class EC2
33
33
  @aws_region ||=
34
34
  ENV['AWS_REGION'] || config.fetch('AWS_REGION', nil) || # ref. old aws cli
35
35
  ENV['AWS_DEFAULT_REGION'] || config.fetch('AWS_DEFAULT_REGION', nil) || # ref. aws cli and terraform
36
- aws_config['region'] || raise('AWS_REGION nor AWS_DEFAULT_REGION is not set')
36
+ aws_config['region'] || raise('AWS_REGION nor AWS_DEFAULT_REGION nor reagion in ~/.aws/config is not set')
37
37
  end
38
38
 
39
39
  def self.aws_profile
@@ -52,12 +52,16 @@ class EC2
52
52
  @aws_secret_access_key ||= ENV['AWS_SECRET_ACCESS_KEY'] || config.fetch('AWS_SECRET_ACCESS_KEY', nil)
53
53
  end
54
54
 
55
- def self.aws_credential_file
56
- @aws_credential_file ||=
57
- ENV['AWS_CREDENTIALS_FILE'] || config.fetch('AWS_CREDENTIALS_FILE', nil) ||
58
- ENV['AWS_CREDENTIAL_FILE'] || config.fetch('AWS_CREDENTIAL_FILE', nil) || # ref. aws cli (supported lately)
55
+ def self.aws_credentials_file
56
+ @aws_credentials_file ||=
57
+ ENV['AWS_CREDENTIALS_FILE'] || config.fetch('AWS_CREDENTIALS_FILE', nil) || # old
58
+ ENV['AWS_CREDENTIAL_FILE'] || config.fetch('AWS_CREDENTIAL_FILE', nil) || # old
59
+ ENV['AWS_SHARED_CREDENTIALS_FILE'] || config.fetch('AWS_SHARED_CREDENTIALS_FILE', nil) || # ref. https://docs.aws.amazon.com/ja_jp/cli/latest/userguide/cli-configure-envvars.html
59
60
  File.expand_path('~/.aws/credentials')
60
61
  end
62
+ class << self
63
+ alias_method :aws_credential_file, :aws_credentials_file # for backward compatibility
64
+ end
61
65
 
62
66
  def self.aws_config_file
63
67
  @aws_config_file ||= ENV['AWS_CONFIG_FILE'] || config.fetch('AWS_CONFIG_FILE', nil) || File.expand_path('~/.aws/config')
@@ -65,10 +69,13 @@ class EC2
65
69
 
66
70
  def self.aws_config
67
71
  return @aws_config if @aws_config
68
- if File.readable?(aws_config_file) && File.readable?(aws_credential_file)
69
- AWSConfig.config_file = aws_config_file
70
- AWSConfig.credentials_file = aws_credential_file
71
- @aws_config = AWSConfig[aws_profile]
72
+ if File.readable?(aws_config_file)
73
+ ini = IniFile.load(aws_config_file).to_h
74
+ if aws_profile == 'default'
75
+ @aws_config = ini['default']
76
+ else
77
+ @aws_config = ini["profile #{aws_profile}"]
78
+ end
72
79
  end
73
80
  @aws_config ||= {}
74
81
  end
@@ -35,25 +35,32 @@ class EC2
35
35
  @ec2 ||= Aws::EC2::Client.new(region: Config.aws_region, credentials: credentials)
36
36
  end
37
37
 
38
- def credentials
39
- if Config.aws_access_key_id and Config.aws_secret_access_key
38
+ def ec2_instance?
39
+ (not instance_id.nil?) rescue false
40
+ end
41
+
42
+ def raw_credentials
43
+ if Config.aws_config['credential_source'] == 'Ec2InstanceMetadata'
44
+ Aws::InstanceProfileCredentials.new
45
+ elsif Config.aws_access_key_id and Config.aws_secret_access_key
40
46
  Aws::Credentials.new(Config.aws_access_key_id, Config.aws_secret_access_key)
47
+ elsif File.readable?(Config.aws_credentials_file)
48
+ Aws::SharedCredentials.new(profile_name: Config.aws_profile, path: Config.aws_credentials_file)
49
+ elsif ec2_instance? # fallback to instance profile
50
+ Aws::InstanceProfileCredentials.new
51
+ end
52
+ end
53
+
54
+ def credentials
55
+ if Config.aws_config['role_arn']
56
+ # wrapped by assume role if necessary
57
+ Aws::AssumeRoleCredentials.new(
58
+ client: Aws::STS::Client.new(raw_credentials),
59
+ role_arn: Config.aws_config['role_arn'],
60
+ role_session_name: "ec2-host-session-#{Time.now.to_i}"
61
+ )
41
62
  else
42
- aws_config = Config.aws_config
43
- if aws_config[:role_arn]
44
- Aws::AssumeRoleCredentials.new(
45
- client: Aws::STS::Client.new(aws_config.config_hash),
46
- role_arn: aws_config[:role_arn],
47
- role_session_name: "ec2-host-session-#{Time.now.to_i}"
48
- )
49
- elsif aws_config[:credential_source] == "Ec2InstanceMetadata"
50
- Aws::InstanceProfileCredentials.new
51
- else
52
- Aws::SharedCredentials.new(
53
- profile_name: Config.aws_profile,
54
- path: Config.aws_credential_file
55
- )
56
- end
63
+ raw_credentials
57
64
  end
58
65
  end
59
66
 
@@ -1,5 +1,5 @@
1
1
  class EC2
2
2
  class Host
3
- VERSION = '1.0.1'
3
+ VERSION = '1.0.2'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ec2-host
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Naotoshi Seo
@@ -39,7 +39,7 @@ dependencies:
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
- name: aws_config
42
+ name: inifile
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - ">="