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-cloudtrail"
|
2019-05-25 08:33:26 +00:00
|
|
|
|
2018-01-23 16:26:27 +00:00
|
|
|
class AwsCloudTrailTrail < Inspec.resource(1)
|
2019-06-11 22:24:35 +00:00
|
|
|
name "aws_cloudtrail_trail"
|
|
|
|
desc "Verifies settings for an individual AWS CloudTrail Trail"
|
2019-03-19 14:17:32 +00:00
|
|
|
example <<~EXAMPLE
|
2018-01-23 16:26:27 +00:00
|
|
|
describe aws_cloudtrail_trail('trail-name') do
|
|
|
|
it { should exist }
|
|
|
|
end
|
2019-03-19 14:17:32 +00:00
|
|
|
EXAMPLE
|
2018-01-23 16:26:27 +00:00
|
|
|
|
2019-06-11 22:24:35 +00:00
|
|
|
supports platform: "aws"
|
2018-02-08 04:26:37 +00:00
|
|
|
|
|
|
|
include AwsSingularResourceMixin
|
|
|
|
attr_reader :cloud_watch_logs_log_group_arn, :cloud_watch_logs_role_arn, :home_region,
|
2019-11-07 23:17:22 +00:00
|
|
|
:kms_key_id, :s3_bucket_name, :trail_arn
|
2018-01-23 16:26:27 +00:00
|
|
|
|
|
|
|
def to_s
|
|
|
|
"CloudTrail #{@trail_name}"
|
|
|
|
end
|
|
|
|
|
|
|
|
def multi_region_trail?
|
|
|
|
@is_multi_region_trail
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_file_validation_enabled?
|
|
|
|
@log_file_validation_enabled
|
|
|
|
end
|
|
|
|
|
|
|
|
def encrypted?
|
|
|
|
!kms_key_id.nil?
|
|
|
|
end
|
|
|
|
|
2018-04-06 18:04:57 +00:00
|
|
|
def delivered_logs_days_ago
|
|
|
|
query = { name: @trail_name }
|
|
|
|
catch_aws_errors do
|
|
|
|
begin
|
|
|
|
resp = BackendFactory.create(inspec_runner).get_trail_status(query).to_h
|
2019-06-11 22:24:35 +00:00
|
|
|
((Time.now - resp[:latest_cloud_watch_logs_delivery_time]) / (24 * 60 * 60)).to_i unless resp[:latest_cloud_watch_logs_delivery_time].nil?
|
2018-04-06 18:04:57 +00:00
|
|
|
rescue Aws::CloudTrail::Errors::TrailNotFoundException
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-01-23 16:26:27 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def validate_params(raw_params)
|
|
|
|
validated_params = check_resource_param_names(
|
|
|
|
raw_params: raw_params,
|
|
|
|
allowed_params: [:trail_name],
|
|
|
|
allowed_scalar_name: :trail_name,
|
2019-06-11 22:24:35 +00:00
|
|
|
allowed_scalar_type: String
|
2018-01-23 16:26:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if validated_params.empty?
|
|
|
|
raise ArgumentError, "You must provide the parameter 'trail_name' to aws_cloudtrail_trail."
|
|
|
|
end
|
|
|
|
|
|
|
|
validated_params
|
|
|
|
end
|
|
|
|
|
2018-02-08 04:26:37 +00:00
|
|
|
def fetch_from_api
|
|
|
|
backend = BackendFactory.create(inspec_runner)
|
2018-01-23 16:26:27 +00:00
|
|
|
|
|
|
|
query = { trail_name_list: [@trail_name] }
|
|
|
|
resp = backend.describe_trails(query)
|
|
|
|
|
|
|
|
@trail = resp.trail_list[0].to_h
|
|
|
|
@exists = !@trail.empty?
|
|
|
|
@s3_bucket_name = @trail[:s3_bucket_name]
|
|
|
|
@is_multi_region_trail = @trail[:is_multi_region_trail]
|
|
|
|
@trail_arn = @trail[:trail_arn]
|
|
|
|
@log_file_validation_enabled = @trail[:log_file_validation_enabled]
|
|
|
|
@cloud_watch_logs_role_arn = @trail[:cloud_watch_logs_role_arn]
|
|
|
|
@cloud_watch_logs_log_group_arn = @trail[:cloud_watch_logs_log_group_arn]
|
|
|
|
@kms_key_id = @trail[:kms_key_id]
|
|
|
|
@home_region = @trail[:home_region]
|
|
|
|
end
|
|
|
|
|
|
|
|
class Backend
|
2018-02-08 04:26:37 +00:00
|
|
|
class AwsClientApi < AwsBackendBase
|
|
|
|
AwsCloudTrailTrail::BackendFactory.set_default_backend(self)
|
|
|
|
self.aws_client_class = Aws::CloudTrail::Client
|
2018-01-23 16:26:27 +00:00
|
|
|
|
|
|
|
def describe_trails(query)
|
2018-02-08 04:26:37 +00:00
|
|
|
aws_service_client.describe_trails(query)
|
2018-01-23 16:26:27 +00:00
|
|
|
end
|
2018-04-06 18:04:57 +00:00
|
|
|
|
|
|
|
def get_trail_status(query)
|
|
|
|
aws_service_client.get_trail_status(query)
|
|
|
|
end
|
2018-01-23 16:26:27 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|