# ################################### ## Rule Specification ## ##################################### # # Rule Identifier: # api_gw_v1_execution_logging_enabled_check # # Description: # This control checks whether all methods in Amazon API Gateway stage have execution logging configured. # # Reports on: # AWS::ApiGateway::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 API Gateway stage resources # Then: SKIP # Scenario: 2 # Given: The input document is an AWS CloudFormation or CloudFormation hook document # And: The input document contains an API Gateway stage resource # And: In the stage resource, 'MethodSettings' is not present or is provided and is an empty list. # Then: FAIL # Scenario: 3 # Given: The input document is an AWS CloudFormation or CloudFormation hook document # And: The input document contains an API Gateway stage resource # And: In the stage resource, Execution Logging is not configured for all HTTP Methods and API resources (In # 'MethodSettings', 'LoggingLevel' is omitted, or not set to 'ERROR' or 'INFO', for 'HttpMethod' of '*' and # 'ResourcePath' of '/*' ) # Then: FAIL # Scenario: 4 # Given: The input document is an AWS CloudFormation or CloudFormation hook document # And: The input document contains an API Gateway stage resource # And: In the stage resource, Execution Logging is configured for all HTTP Methods and API resources (In # 'MethodSettings', 'LoggingLevel' is set to 'ERROR' or 'INFO', for 'HttpMethod' of '*' and # 'ResourcePath' of '/*' ) # And: 'LoggingLevel' has been set to 'OFF' for any other Method Setting # Then: FAIL # Scenario: 5 # Given: The input document is an AWS CloudFormation or CloudFormation hook document # And: The input document contains an API Gateway stage resource # And: In the stage resource, Execution Logging is configured for all HTTP Methods and API resources (In # 'MethodSettings', 'LoggingLevel' is set to 'ERROR' or 'INFO', for 'HttpMethod' of '*' and # 'ResourcePath' of '/*' ) # And: 'LoggingLevel' has not been provided or set to 'ERROR' or 'INFO' for all other Method Settings # Then: PASS # # Constants # let API_GW_STAGE_TYPE = "AWS::ApiGateway::Stage" let INPUT_DOCUMENT = this let VALID_LOG_LEVELS = [ "ERROR", "INFO" ] # # Assignments # let api_gateway_stages = Resources.*[ Type == %API_GW_STAGE_TYPE ] # # Primary Rules # rule api_gw_v1_execution_logging_enabled_check when is_cfn_template(%INPUT_DOCUMENT) %api_gateway_stages not empty { check(%api_gateway_stages.Properties) << [CT.APIGATEWAY.PR.1]: Require an Amazon API Gateway REST and WebSocket API to have logging activated [FIX]: Configure execution logging on Amazon API Gateway stages with a 'MethodSetting' that sets 'LoggingLevel' to 'ERROR' or 'INFO' for all methods ('HttpMethod' of '*' and 'ResourcePath' of '/*'). Ensure that you do not set 'LoggingLevel' to 'OFF' for any method setting. >> } rule api_gw_v1_execution_logging_enabled_check when is_cfn_hook(%INPUT_DOCUMENT, %API_GW_STAGE_TYPE) { check(%INPUT_DOCUMENT.%API_GW_STAGE_TYPE.resourceProperties) << [CT.APIGATEWAY.PR.1]: Require an Amazon API Gateway REST and WebSocket API to have logging activated [FIX]: Configure execution logging on Amazon API Gateway stages with a 'MethodSetting' that sets 'LoggingLevel' to 'ERROR' or 'INFO' for all methods ('HttpMethod' of '*' and 'ResourcePath' of '/*'). Ensure that you do not set 'LoggingLevel' to 'OFF' for any method setting. >> } # # Parameterized Rules # rule check(api_gateway_stage) { %api_gateway_stage { # Scenario 2 MethodSettings exists MethodSettings is_list MethodSettings not empty # Scenario 3 # At least one wildcard entry exists with valid logging enabled some MethodSettings[*] { HttpMethod exists ResourcePath exists LoggingLevel exists HttpMethod == "*" ResourcePath == "/*" LoggingLevel in %VALID_LOG_LEVELS } # Scenario 4, 5 # When other methods explictly set/override logging settings, ensure that logging is not disabled MethodSettings[*] { when LoggingLevel exists { LoggingLevel in %VALID_LOG_LEVELS } } } } # # 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 }