import boto3 from pprint import pprint from utility import get_all_available_regions, have_terraform_tags WAF_LOGGING_DESTINATION = "arn:aws:s3:::aws-waf-logs-shared-orcd" def get_waf_info(arn, region, scope): waf_client = boto3.client("wafv2", region_name=region) logging_config = waf_client.get_logging_configuration(ResourceArn=arn) logging_destinations = logging_config.get("LoggingConfiguration", {}).get( "LogDestinationConfigs", [] ) tags = waf_client.list_tags_for_resource(ResourceARN=arn)["TagInfoForResource"][ "TagList" ] waf_info = { "ARN": arn, "Scope": scope, "HaveTerraformTags": have_terraform_tags(tags), "LoggingDestinations": logging_destinations, } return waf_info def list_waf(region, scope): waf_client = boto3.client("wafv2", region_name=region) next_marker = None waf_list = [] while True: if next_marker: response = waf_client.list_web_acls(Scope=scope, NextMarker=next_marker) else: response = waf_client.list_web_acls(Scope=scope) for acl in response.get("WebACLs", []): waf_list.append(get_waf_info(acl["ARN"], region, scope)) next_marker = response.get("NextMarker") if not next_marker: break return waf_list def verify_waf_logging(regions): """Checks all waf acl logging destinations to find ones which don't include WAF_LOGGING_DESTINATION""" complete_waf_list = [] complete_waf_list.extend(list_waf("us-east-1", "CLOUDFRONT")) for region in regions: complete_waf_list.extend(list_waf(region, "REGIONAL")) return [ waf for waf in complete_waf_list if WAF_LOGGING_DESTINATION not in waf["LoggingDestinations"] ] if __name__ == "__main__": regions = get_all_available_regions() pprint(verify_waf_logging(regions))