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-rds"
|
2019-05-25 08:33:26 +00:00
|
|
|
|
2018-03-28 15:23:44 +00:00
|
|
|
class AwsRdsInstance < Inspec.resource(1)
|
2019-06-11 22:24:35 +00:00
|
|
|
name "aws_rds_instance"
|
|
|
|
desc "Verifies settings for an rds instance"
|
2019-03-19 14:17:32 +00:00
|
|
|
example <<~EXAMPLE
|
2018-03-28 15:23:44 +00:00
|
|
|
describe aws_rds_instance(db_instance_identifier: 'test-instance-id') 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-03-28 15:23:44 +00:00
|
|
|
|
|
|
|
include AwsSingularResourceMixin
|
|
|
|
attr_reader :db_instance_identifier
|
|
|
|
|
|
|
|
def to_s
|
|
|
|
"RDS Instance #{@db_instance_identifier}"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def validate_params(raw_params)
|
|
|
|
validated_params = check_resource_param_names(
|
|
|
|
raw_params: raw_params,
|
|
|
|
allowed_params: [:db_instance_identifier],
|
|
|
|
allowed_scalar_name: :db_instance_identifier,
|
2019-06-11 22:24:35 +00:00
|
|
|
allowed_scalar_type: String
|
2018-03-28 15:23:44 +00:00
|
|
|
)
|
2019-06-11 22:24:35 +00:00
|
|
|
if validated_params.empty? || !validated_params.key?(:db_instance_identifier)
|
|
|
|
raise ArgumentError, "You must provide an id for the aws_rds_instance."
|
2018-03-28 15:23:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
if validated_params.key?(:db_instance_identifier) && validated_params[:db_instance_identifier] !~ /^[a-z]{1}[0-9a-z\-]{0,62}$/
|
2019-06-11 22:24:35 +00:00
|
|
|
raise ArgumentError, "aws_rds_instance Database Instance ID must be in the format: start with a letter followed by up to 62 letters/numbers/hyphens."
|
2018-03-28 15:23:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
validated_params
|
|
|
|
end
|
|
|
|
|
|
|
|
def fetch_from_api
|
|
|
|
backend = BackendFactory.create(inspec_runner)
|
|
|
|
dsg_response = nil
|
|
|
|
catch_aws_errors do
|
|
|
|
begin
|
|
|
|
dsg_response = backend.describe_db_instances(db_instance_identifier: db_instance_identifier)
|
|
|
|
@exists = true
|
|
|
|
rescue Aws::RDS::Errors::DBInstanceNotFound
|
|
|
|
@exists = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if dsg_response.db_instances.empty?
|
|
|
|
@exists = false
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
@db_instance_identifier = dsg_response.db_instances[0].db_instance_identifier
|
|
|
|
end
|
|
|
|
|
|
|
|
# Uses the SDK API to really talk to AWS
|
|
|
|
class Backend
|
|
|
|
class AwsClientApi < AwsBackendBase
|
|
|
|
BackendFactory.set_default_backend(self)
|
|
|
|
self.aws_client_class = Aws::RDS::Client
|
|
|
|
|
|
|
|
def describe_db_instances(query)
|
|
|
|
aws_service_client.describe_db_instances(query)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|