From c8d4244ef4ab54923076d753cc484963505b89de Mon Sep 17 00:00:00 2001 From: Chris Redekop Date: Thu, 26 Oct 2017 15:56:32 -0400 Subject: [PATCH] Add has_roles to aws_ec2_instance (#90) * Rename EC2-instance resources Signed-off-by: Chris Redekop * Add interim updates Signed-off-by: Chris Redekop * testing for issue 82 Signed-off-by: Simon Varlow * completed integration for EC2 roles Signed-off-by: Simon Varlow * adding in the beginning of the unit test for issue 82 Signed-off-by: Simon Varlow * Fix unit tests Signed-off-by: Chris Redekop * Add has_roles? examples Signed-off-by: Chris Redekop * Remove redundant gsub Signed-off-by: Chris Redekop * corrected OpenStruct format Signed-off-by: Simon Varlow * setting up variable for InstanceProfile Signed-off-by: Simon Varlow * Updated the unit test so all variables are at the top Signed-off-by: Simon Varlow * Fixed Rubocop issues that were detected Signed-off-by: Simon Varlow * Updating README.md to include changes to aws_ec2 Signed-off-by: Simon Varlow * Add failing IT for has_roles? Signed-off-by: Chris Redekop * Add negative IT and fix uncovered issue Signed-off-by: Chris Redekop * Fix Rubocop issue Signed-off-by: Chris Redekop * Fix integration test Signed-off-by: Chris Redekop * Fix Rubocop issues and unit tests Signed-off-by: Chris Redekop * Pin AWS dependency to '~> 2' Signed-off-by: Chris Redekop --- Gemfile | 2 +- README.md | 4 +- libraries/aws_ec2_instance.rb | 18 ++++- test/integration/build/aws.tf | 42 +++++++++++- .../verify/controls/aws_ec2_instance.rb | 21 +++--- test/unit/resources/aws_ec2_instance_test.rb | 68 +++++++++++++++++-- 6 files changed, 135 insertions(+), 20 deletions(-) diff --git a/Gemfile b/Gemfile index a1aea8afe..48bdf8619 100644 --- a/Gemfile +++ b/Gemfile @@ -4,7 +4,7 @@ gem 'rake' gem 'inspec', '~> 1' gem 'rubocop', '~> 0.44.0' gem 'highline', '~> 1.6.0' -gem 'aws-sdk' +gem 'aws-sdk', '~> 2' gem 'nokogiri' gem 'minitest', '5.10.1' diff --git a/README.md b/README.md index b2b4d73a5..cacfaf4e1 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ control "aws-1" do impact 0.7 title 'Checks the machine is running' - describe aws_ec2('i-my-ec2-instance-id') do + describe aws_ec2_instance('my-ec2-machine') do it { should be_running } end end @@ -61,7 +61,7 @@ end ### Available Resources - * `aws_ec2` - This resource reads information about an ec2 instance + * `aws_ec2_instance` - This resource reads information about an ec2 instance * `aws_iam_access_key` - Verifies settings for AWS IAM access keys * `aws_iam_password_policy` - Verifies iam password policy * `aws_iam_root_user` - Verifies settings for AWS root account diff --git a/libraries/aws_ec2_instance.rb b/libraries/aws_ec2_instance.rb index b1eba6588..f2a048308 100644 --- a/libraries/aws_ec2_instance.rb +++ b/libraries/aws_ec2_instance.rb @@ -1,5 +1,4 @@ # author: Christoph Hartmann - class AwsEc2Instance < Inspec.resource(1) name 'aws_ec2_instance' desc 'Verifies settings for an EC2 instance' @@ -7,10 +6,12 @@ class AwsEc2Instance < Inspec.resource(1) example " describe aws_ec2_instance('i-123456') do it { should be_running } + it { should have_roles } end describe aws_ec2_instance(name: 'my-instance') do it { should be_running } + it { should have_roles } end " @@ -19,6 +20,7 @@ class AwsEc2Instance < Inspec.resource(1) @opts.is_a?(Hash) ? @display_name = @opts[:name] : @display_name = opts @ec2_client = conn.ec2_client @ec2_resource = conn.ec2_resource + @iam_resource = conn.iam_resource end def id @@ -86,6 +88,20 @@ class AwsEc2Instance < Inspec.resource(1) "EC2 Instance #{@display_name}" end + 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 + private def instance diff --git a/test/integration/build/aws.tf b/test/integration/build/aws.tf index b2be37a59..e0d462942 100644 --- a/test/integration/build/aws.tf +++ b/test/integration/build/aws.tf @@ -4,12 +4,48 @@ terraform { provider "aws" {} +resource "aws_iam_role" "example" { + name = "${terraform.env}.example" + + assume_role_policy = <