2019-06-11 22:24:35 +00:00
|
|
|
require "resource_support/aws/aws_singular_resource_mixin"
|
|
|
|
require "resource_support/aws/aws_backend_base"
|
|
|
|
require "aws-sdk-iam"
|
2019-05-25 08:33:26 +00:00
|
|
|
|
2017-12-14 03:34:52 +00:00
|
|
|
class AwsIamRole < Inspec.resource(1)
|
2019-06-11 22:24:35 +00:00
|
|
|
name "aws_iam_role"
|
|
|
|
desc "Verifies settings for an IAM Role"
|
2019-03-19 14:17:32 +00:00
|
|
|
example <<~EXAMPLE
|
2017-12-14 03:34:52 +00:00
|
|
|
describe aws_iam_role('my-role') do
|
|
|
|
it { should exist }
|
|
|
|
end
|
2019-03-19 14:17:32 +00:00
|
|
|
EXAMPLE
|
2019-06-11 22:24:35 +00:00
|
|
|
supports platform: "aws"
|
2017-12-14 03:34:52 +00:00
|
|
|
|
2018-02-08 04:26:37 +00:00
|
|
|
include AwsSingularResourceMixin
|
|
|
|
attr_reader :description, :role_name
|
2017-12-14 03:34:52 +00:00
|
|
|
|
2018-04-13 21:25:53 +00:00
|
|
|
def to_s
|
|
|
|
"IAM Role #{role_name}"
|
|
|
|
end
|
|
|
|
|
2017-12-14 03:34:52 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def validate_params(raw_params)
|
|
|
|
validated_params = check_resource_param_names(
|
|
|
|
raw_params: raw_params,
|
|
|
|
allowed_params: [:role_name],
|
|
|
|
allowed_scalar_name: :role_name,
|
2019-06-11 22:24:35 +00:00
|
|
|
allowed_scalar_type: String
|
2017-12-14 03:34:52 +00:00
|
|
|
)
|
|
|
|
if validated_params.empty?
|
2019-06-11 22:24:35 +00:00
|
|
|
raise ArgumentError, "You must provide a role_name to aws_iam_role."
|
2017-12-14 03:34:52 +00:00
|
|
|
end
|
2019-07-09 00:20:30 +00:00
|
|
|
|
2017-12-14 03:34:52 +00:00
|
|
|
validated_params
|
|
|
|
end
|
|
|
|
|
2018-02-08 04:26:37 +00:00
|
|
|
def fetch_from_api
|
2017-12-14 03:34:52 +00:00
|
|
|
role_info = nil
|
|
|
|
begin
|
2018-02-08 04:26:37 +00:00
|
|
|
role_info = BackendFactory.create(inspec_runner).get_role(role_name: role_name)
|
2017-12-14 03:34:52 +00:00
|
|
|
rescue Aws::IAM::Errors::NoSuchEntity
|
|
|
|
@exists = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
@exists = true
|
|
|
|
@description = role_info.role.description
|
|
|
|
end
|
|
|
|
|
|
|
|
# Uses the SDK API to really talk to AWS
|
|
|
|
class Backend
|
2018-02-08 04:26:37 +00:00
|
|
|
class AwsClientApi < AwsBackendBase
|
2017-12-14 03:34:52 +00:00
|
|
|
BackendFactory.set_default_backend(self)
|
2018-02-08 04:26:37 +00:00
|
|
|
self.aws_client_class = Aws::IAM::Client
|
2017-12-14 03:34:52 +00:00
|
|
|
def get_role(query)
|
2018-02-08 04:26:37 +00:00
|
|
|
aws_service_client.get_role(query)
|
2017-12-14 03:34:52 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|