Skip to content

Commit 8325c9d

Browse files
committed
Merge pull request #104 from Igorshp/aim_roles
Add support for IAM role credentials
2 parents 36dbfb4 + c7485de commit 8325c9d

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

lib/kitchen/driver/ec2.rb

+18-8
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ module Driver
3030
# @author Fletcher Nichol <[email protected]>
3131
class Ec2 < Kitchen::Driver::SSHBase
3232

33+
extend Fog::AWS::CredentialFetcher::ServiceMethods
3334
default_config :region, 'us-east-1'
3435
default_config :availability_zone, 'us-east-1b'
3536
default_config :flavor_id, 'm1.small'
@@ -41,13 +42,13 @@ class Ec2 < Kitchen::Driver::SSHBase
4142
default_config :iam_profile_name, nil
4243
default_config :price, nil
4344
default_config :aws_access_key_id do |driver|
44-
ENV['AWS_ACCESS_KEY'] || ENV['AWS_ACCESS_KEY_ID']
45+
ENV['AWS_ACCESS_KEY'] || ENV['AWS_ACCESS_KEY_ID'] || iam_creds[:aws_access_key_id]
4546
end
4647
default_config :aws_secret_access_key do |driver|
47-
ENV['AWS_SECRET_KEY'] || ENV['AWS_SECRET_ACCESS_KEY']
48+
ENV['AWS_SECRET_KEY'] || ENV['AWS_SECRET_ACCESS_KEY'] || iam_creds[:aws_secret_access_key]
4849
end
4950
default_config :aws_session_token do |driver|
50-
ENV['AWS_SESSION_TOKEN'] || ENV['AWS_TOKEN']
51+
ENV['AWS_SESSION_TOKEN'] || ENV['AWS_TOKEN'] || iam_creds[:aws_session_token]
5152
end
5253
default_config :aws_ssh_key_id do |driver|
5354
ENV['AWS_SSH_KEY_ID']
@@ -80,7 +81,7 @@ class Ec2 < Kitchen::Driver::SSHBase
8081
validations[d] = lambda do |attr, val, driver|
8182
unless val.nil?
8283
driver.warn "WARN: The config key `#{attr}` is deprecated," +
83-
" please use `block_device_mappings`"
84+
' please use `block_device_mappings`'
8485
end
8586
end
8687
end
@@ -97,14 +98,23 @@ class Ec2 < Kitchen::Driver::SSHBase
9798
end
9899
end
99100

101+
def self.iam_creds
102+
@iam_creds ||= begin
103+
fetch_credentials(use_iam_profile:true)
104+
rescue RuntimeError => e
105+
debug("fetch_credentials failed with exception #{e.message}:#{e.backtrace.join("\n")}")
106+
{}
107+
end
108+
end
109+
100110
def create(state)
101111
return if state[:server_id]
102112

103113
info("Creating <#{state[:server_id]}>...")
104-
info("If you are not using an account that qualifies under the AWS")
105-
info("free-tier, you may be charged to run these suites. The charge")
106-
info("should be minimal, but neither Test Kitchen nor its maintainers")
107-
info("are responsible for your incurred costs.")
114+
info('If you are not using an account that qualifies under the AWS')
115+
info('free-tier, you may be charged to run these suites. The charge')
116+
info('should be minimal, but neither Test Kitchen nor its maintainers')
117+
info('are responsible for your incurred costs.')
108118

109119
if config[:price]
110120
# Spot instance when a price is set

0 commit comments

Comments
 (0)