# ################################### ## Rule Specification ## ##################################### # # Rule Identifier: # cloudwatch_log_group_encrypted_check # # Description: # This control checks whether an Amazon CloudWatch log group is encrypted at rest with an AWS KMS key # # Reports on: # AWS::Logs::LogGroup # # Evaluates: # AWS CloudFormation, AWS CloudFormation hook # # Rule Parameters: # None # # Scenarios: # Scenario: 1 # Given: The input document is an AWS CloudFormation or CloudFormation hook document # And: The input document does not contain any CloudWatch log group resources # Then: SKIP # Scenario: 2 # Given: The input document is an AWS CloudFormation or CloudFormation hook document # And: The input document contains a CloudWatch log group resource # And: 'KmsKeyId' has not been provided # Then: FAIL # Scenario: 3 # Given: The input document is an AWS CloudFormation or CloudFormation hook document # And: The input document contains a CloudWatch log group resource # And: 'KmsKeyId' has been provided as an empty string or invalid local reference to a KMS Key # Then: FAIL # Scenario: 4 # Given: The input document is an AWS CloudFormation or CloudFormation hook document # And: The input document contains a CloudWatch log group resource # And: 'KmsKeyId' has been provided as a non-empty string or valid local reference to a KMS Key # Then: PASS # # Constants # let CLOUDWATCH_LOGS_TYPE = "AWS::Logs::LogGroup" let INPUT_DOCUMENT = this # # Assignments # let cloudwatch_log_groups = Resources.*[ Type == %CLOUDWATCH_LOGS_TYPE ] # # Primary Rules # rule cloudwatch_log_group_encrypted_check when is_cfn_template(%INPUT_DOCUMENT) %cloudwatch_log_groups not empty { check(%cloudwatch_log_groups.Properties) << [CT.CLOUDWATCH.PR.3]: Require an Amazon CloudWatch log group to be encrypted at rest with an AWS KMS key [FIX]: Set 'KmsKeyId' to the ARN of an AWS KMS customer managed key configured with permissions that allow the CloudWatch service principal to use the key. >> } rule cloudwatch_log_group_encrypted_check when is_cfn_hook(%INPUT_DOCUMENT, %CLOUDWATCH_LOGS_TYPE) { check(%INPUT_DOCUMENT.%CLOUDWATCH_LOGS_TYPE.resourceProperties) << [CT.CLOUDWATCH.PR.3]: Require an Amazon CloudWatch log group to be encrypted at rest with an AWS KMS key [FIX]: Set 'KmsKeyId' to the ARN of an AWS KMS customer managed key configured with permissions that allow the CloudWatch service principal to use the key. >> } # # Parameterized Rules # rule check(cloudwatch_log_group){ %cloudwatch_log_group { # Scenario 2 KmsKeyId exists # Scenario 3 and 4 check_is_string_and_not_empty(KmsKeyId) or check_local_references(%INPUT_DOCUMENT, KmsKeyId, "AWS::KMS::Key") } } # # Utility Rules # rule is_cfn_template(doc) { %doc { AWSTemplateFormatVersion exists or Resources exists } } rule is_cfn_hook(doc, RESOURCE_TYPE) { %doc.%RESOURCE_TYPE.resourceProperties exists } rule check_is_string_and_not_empty(value) { %value { this is_string this != /\A\s*\z/ } } rule check_local_references(doc, reference_properties, referenced_resource_type) { %reference_properties { 'Fn::GetAtt' { query_for_resource(%doc, this[0], %referenced_resource_type) <> } or Ref { query_for_resource(%doc, this, %referenced_resource_type) <> } } } rule query_for_resource(doc, resource_key, resource_type) { let referenced_resource = %doc.Resources[ keys == %resource_key ] %referenced_resource not empty %referenced_resource { Type == %resource_type } }