# ################################### ## Rule Specification ## ##################################### # # Rule Identifier: # api_gw_v2_access_logs_enabled_check # # Description: # This control checks whether Amazon API Gateway V2 stages have access logging enabled. Access logging is supported for HTTP and WebSocket APIs. # # Reports on: # AWS::ApiGatewayV2::Stage # # 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 APIGatewayV2 stage resources # Then: SKIP # Scenario: 2 # Given: The input document is an AWS CloudFormation or CloudFormation hook document # And: The input document contains an APIGatewayV2 stage resource # And: 'AccessLogSettings' 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 an APIGatewayV2 stage resource # And: 'AccessLogSettings' has been provided # And: 'AccessLogSettings.DestinationArn' has not been provided, or has been provided as an empty string or # invalid local reference # And: 'AccessLogSettings.Format' is provided as a non-empty string # Then: FAIL # Scenario: 4 # Given: The input document is an AWS CloudFormation or CloudFormation hook document # And: The input document contains an APIGatewayV2 stage resource # And: 'AccessLogSettings' has been provided # And: 'AccessLogSettings.DestinationArn' is provided as a non-empty string or valid local reference # And: 'AccessLogSettings.Format' has not been provided, or is an empty string # Then: FAIL # Scenario: 5 # Given: The input document is an AWS CloudFormation or CloudFormation hook document # And: The input document contains an APIGatewayV2 stage resource # And: 'AccessLogSettings' has been provided # And: 'AccessLogSettings.DestinationArn' is provided as a non-empty string or valid local reference # And: 'AccessLogSettings.Format' is provided as a non-empty string # Then: PASS # # Constants # let API_GW_V2_STAGE_TYPE = "AWS::ApiGatewayV2::Stage" let INPUT_DOCUMENT = this # # Assignments # let api_gateway_v2_stages = Resources.*[ Type == %API_GW_V2_STAGE_TYPE ] # # Primary Rules # rule api_gw_v2_access_logs_enabled_check when is_cfn_template(%INPUT_DOCUMENT) %api_gateway_v2_stages not empty { check(%api_gateway_v2_stages.Properties) << [CT.APIGATEWAY.PR.4]: Require an Amazon API Gateway V2 stage to have access logging activated [FIX]: Provide an 'AccessLogSettings' configuration, setting 'DestinationArn' to the ARN of an Amazon CloudWatch log group and 'Format' to a single line log format configuration. >> } rule api_gw_v2_access_logs_enabled_check when is_cfn_hook(%INPUT_DOCUMENT, %API_GW_V2_STAGE_TYPE) { check(%INPUT_DOCUMENT.%API_GW_V2_STAGE_TYPE.resourceProperties) << [CT.APIGATEWAY.PR.4]: Require an Amazon API Gateway V2 stage to have access logging activated [FIX]: Provide an 'AccessLogSettings' configuration, setting 'DestinationArn' to the ARN of an Amazon CloudWatch log group and 'Format' to a single line log format configuration. >> } # # Parameterized Rules # rule check(api_gateway_v2_stage) { %api_gateway_v2_stage { # Scenario 2 AccessLogSettings exists AccessLogSettings is_struct AccessLogSettings { # Scenario 3 DestinationArn exists check_is_string_and_not_empty(DestinationArn) or check_local_references(%INPUT_DOCUMENT, DestinationArn, "AWS::Logs::LogGroup") # Scenario 4, 5 Format exists check_is_string_and_not_empty(Format) } } } # # 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, referenced_resource_type) { let referenced_resource = %doc.Resources[ keys == %resource_key ] %referenced_resource not empty %referenced_resource { Type == %referenced_resource_type } }