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
|
|
|
|
2018-02-01 20:55:54 +00:00
|
|
|
class AwsIamGroup < Inspec.resource(1)
|
2019-06-11 22:24:35 +00:00
|
|
|
name "aws_iam_group"
|
|
|
|
desc "Verifies settings for AWS IAM Group"
|
2019-03-19 14:17:32 +00:00
|
|
|
example <<~EXAMPLE
|
2018-02-01 20:55:54 +00:00
|
|
|
describe aws_iam_group('mygroup') 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"
|
2018-02-01 20:55:54 +00:00
|
|
|
|
2018-02-08 04:26:37 +00:00
|
|
|
include AwsSingularResourceMixin
|
2018-04-06 18:04:13 +00:00
|
|
|
attr_reader :group_name, :users
|
2018-02-01 20:55:54 +00:00
|
|
|
|
|
|
|
def to_s
|
|
|
|
"IAM Group #{group_name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def validate_params(raw_params)
|
|
|
|
validated_params = check_resource_param_names(
|
|
|
|
raw_params: raw_params,
|
|
|
|
allowed_params: [:group_name],
|
|
|
|
allowed_scalar_name: :group_name,
|
2019-06-11 22:24:35 +00:00
|
|
|
allowed_scalar_type: String
|
2018-02-01 20:55:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if validated_params.empty?
|
2019-06-11 22:24:35 +00:00
|
|
|
raise ArgumentError, "You must provide a group_name to aws_iam_group."
|
2018-02-01 20:55:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
validated_params
|
|
|
|
end
|
|
|
|
|
2018-02-08 04:26:37 +00:00
|
|
|
def fetch_from_api
|
|
|
|
backend = AwsIamGroup::BackendFactory.create(inspec_runner)
|
2018-02-01 20:55:54 +00:00
|
|
|
|
|
|
|
begin
|
2018-04-06 18:04:13 +00:00
|
|
|
resp = backend.get_group(group_name: group_name)
|
2018-02-01 20:55:54 +00:00
|
|
|
@exists = true
|
2018-04-06 18:04:13 +00:00
|
|
|
@aws_group_struct = resp[:group]
|
|
|
|
@users = resp[:users].map(&:user_name)
|
2018-02-01 20:55:54 +00:00
|
|
|
rescue Aws::IAM::Errors::NoSuchEntity
|
|
|
|
@exists = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class Backend
|
2018-02-08 04:26:37 +00:00
|
|
|
class AwsClientApi < AwsBackendBase
|
|
|
|
BackendFactory.set_default_backend(self)
|
|
|
|
self.aws_client_class = Aws::IAM::Client
|
2018-02-01 20:55:54 +00:00
|
|
|
|
2018-02-08 04:26:37 +00:00
|
|
|
def get_group(query)
|
|
|
|
aws_service_client.get_group(query)
|
|
|
|
end
|
2018-02-01 20:55:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|