inspec/libraries/aws_ec2_instance.rb

128 lines
2.8 KiB
Ruby
Raw Normal View History

require '_aws'
2016-12-15 08:53:01 +00:00
# author: Christoph Hartmann
class AwsEc2Instance < Inspec.resource(1)
name 'aws_ec2_instance'
2016-12-15 10:55:21 +00:00
desc 'Verifies settings for an EC2 instance'
2016-12-15 08:53:01 +00:00
example "
describe aws_ec2_instance('i-123456') do
2016-12-15 08:53:01 +00:00
it { should be_running }
Add has_roles to aws_ec2_instance (#90) * Rename EC2-instance resources Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Add interim updates Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * testing for issue 82 Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * completed integration for EC2 roles Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * adding in the beginning of the unit test for issue 82 Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Fix unit tests Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Add has_roles? examples Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Remove redundant gsub Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * corrected OpenStruct format Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * setting up variable for InstanceProfile Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Updated the unit test so all variables are at the top Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Fixed Rubocop issues that were detected Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Updating README.md to include changes to aws_ec2 Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Add failing IT for has_roles? Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Add negative IT and fix uncovered issue Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Fix Rubocop issue Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Fix integration test Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Fix Rubocop issues and unit tests Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Pin AWS dependency to '~> 2' Signed-off-by: Chris Redekop <chris.redekop@d2l.com>
2017-10-26 19:56:32 +00:00
it { should have_roles }
2016-12-15 08:53:01 +00:00
end
describe aws_ec2_instance(name: 'my-instance') do
2016-12-15 08:53:01 +00:00
it { should be_running }
Add has_roles to aws_ec2_instance (#90) * Rename EC2-instance resources Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Add interim updates Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * testing for issue 82 Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * completed integration for EC2 roles Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * adding in the beginning of the unit test for issue 82 Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Fix unit tests Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Add has_roles? examples Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Remove redundant gsub Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * corrected OpenStruct format Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * setting up variable for InstanceProfile Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Updated the unit test so all variables are at the top Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Fixed Rubocop issues that were detected Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Updating README.md to include changes to aws_ec2 Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Add failing IT for has_roles? Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Add negative IT and fix uncovered issue Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Fix Rubocop issue Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Fix integration test Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Fix Rubocop issues and unit tests Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Pin AWS dependency to '~> 2' Signed-off-by: Chris Redekop <chris.redekop@d2l.com>
2017-10-26 19:56:32 +00:00
it { should have_roles }
2016-12-15 08:53:01 +00:00
end
"
def initialize(opts, conn = AWSConnection.new)
2016-12-15 08:53:01 +00:00
@opts = opts
@opts.is_a?(Hash) ? @display_name = @opts[:name] : @display_name = opts
@ec2_client = conn.ec2_client
@ec2_resource = conn.ec2_resource
Add has_roles to aws_ec2_instance (#90) * Rename EC2-instance resources Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Add interim updates Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * testing for issue 82 Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * completed integration for EC2 roles Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * adding in the beginning of the unit test for issue 82 Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Fix unit tests Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Add has_roles? examples Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Remove redundant gsub Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * corrected OpenStruct format Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * setting up variable for InstanceProfile Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Updated the unit test so all variables are at the top Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Fixed Rubocop issues that were detected Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Updating README.md to include changes to aws_ec2 Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Add failing IT for has_roles? Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Add negative IT and fix uncovered issue Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Fix Rubocop issue Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Fix integration test Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Fix Rubocop issues and unit tests Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Pin AWS dependency to '~> 2' Signed-off-by: Chris Redekop <chris.redekop@d2l.com>
2017-10-26 19:56:32 +00:00
@iam_resource = conn.iam_resource
2016-12-15 08:53:01 +00:00
end
def id
return @instance_id if defined?(@instance_id)
if @opts.is_a?(Hash)
first = @ec2_resource.instances(
{
filters: [{
name: 'tag:Name',
values: [@opts[:name]],
}],
},
).first
# catch case where the instance is not known
@instance_id = first.id unless first.nil?
else
@instance_id = @opts
end
end
alias instance_id id
def exists?
return false if instance.nil?
instance.exists?
2016-12-15 08:53:01 +00:00
end
# returns the instance state
def state
instance&.state&.name
2016-12-15 08:53:01 +00:00
end
# helper methods for each state
%w{
pending running shutting-down
terminated stopping stopped unknown
}.each do |state_name|
define_method state_name.tr('-', '_') + '?' do
state == state_name
end
end
# attributes that we want to expose
%w{
public_ip_address private_ip_address key_name private_dns_name
public_dns_name subnet_id architecture root_device_type
root_device_name virtualization_type client_token launch_time
instance_type image_id vpc_id
}.each do |attribute|
define_method attribute do
instance.send(attribute) if instance
2016-12-15 08:53:01 +00:00
end
end
def security_groups
@security_groups ||= instance.security_groups.map { |sg|
{ id: sg.group_id, name: sg.group_name }
}
2016-12-15 08:53:01 +00:00
end
def tags
@tags ||= instance.tags.map { |tag| { key: tag.key, value: tag.value } }
end
def to_s
"EC2 Instance #{@display_name}"
end
Add has_roles to aws_ec2_instance (#90) * Rename EC2-instance resources Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Add interim updates Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * testing for issue 82 Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * completed integration for EC2 roles Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * adding in the beginning of the unit test for issue 82 Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Fix unit tests Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Add has_roles? examples Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Remove redundant gsub Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * corrected OpenStruct format Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * setting up variable for InstanceProfile Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Updated the unit test so all variables are at the top Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Fixed Rubocop issues that were detected Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Updating README.md to include changes to aws_ec2 Signed-off-by: Simon Varlow <simon.varlow@d2l.com> * Add failing IT for has_roles? Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Add negative IT and fix uncovered issue Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Fix Rubocop issue Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Fix integration test Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Fix Rubocop issues and unit tests Signed-off-by: Chris Redekop <chris.redekop@d2l.com> * Pin AWS dependency to '~> 2' Signed-off-by: Chris Redekop <chris.redekop@d2l.com>
2017-10-26 19:56:32 +00:00
def has_roles?
instance_profile = instance.iam_instance_profile
if instance_profile
roles = @iam_resource.instance_profile(
instance_profile.arn.gsub(%r{^.*\/}, ''),
).roles
else
roles = nil
end
roles && !roles.empty?
end
2016-12-15 08:53:01 +00:00
private
def instance
@instance ||= @ec2_resource.instance(id)
end
end
# Deprecated
class AwsEc2 < AwsEc2Instance
name 'aws_ec2'
def initialize(opts, conn = AWSConnection.new)
deprecated
super(opts, conn)
end
def deprecated
warn '[DEPRECATION] `aws_ec2(parameter)` is deprecated. ' \
'Please use `aws_ec2_instance(parameter)` instead.'
end
end