bp-check/services/cloudfront.py

139 lines
4.7 KiB
Python
Raw Normal View History

2024-08-05 02:30:34 +00:00
from models import RuleCheckResult
import boto3
2024-08-07 11:40:35 +00:00
client = boto3.client("cloudfront")
2024-08-05 02:30:34 +00:00
def cloudfront_accesslogs_enabled():
2024-08-07 11:40:35 +00:00
compliant_resources = []
non_compliant_resources = []
distributions = client.list_distributions()["DistributionList"]["Items"]
for distribution in distributions:
distribution = client.get_distribution(Id=distribution["Id"])["Distribution"]
if (
"Logging" in distribution["DistributionConfig"]
and distribution["DistributionConfig"]["Logging"]["Enabled"] == True
):
compliant_resources.append(distribution["ARN"])
else:
non_compliant_resources.append(distribution["ARN"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-07 11:40:35 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resources,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)
def cloudfront_associated_with_waf():
2024-08-07 11:40:35 +00:00
compliant_resources = []
non_compliant_resources = []
distributions = client.list_distributions()["DistributionList"]["Items"]
for distribution in distributions:
if "WebACLId" in distribution and distribution["WebACLId"] != "":
2024-08-07 11:40:35 +00:00
compliant_resources.append(distribution["ARN"])
else:
non_compliant_resources.append(distribution["ARN"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-07 11:40:35 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resources,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)
def cloudfront_default_root_object_configured():
2024-08-07 11:40:35 +00:00
compliant_resources = []
non_compliant_resources = []
distributions = client.list_distributions()["DistributionList"]["Items"]
for distribution in distributions:
distribution = client.get_distribution(Id=distribution["Id"])["Distribution"]
if distribution["DistributionConfig"]["DefaultRootObject"] != "":
compliant_resources.append(distribution["ARN"])
else:
non_compliant_resources.append(distribution["ARN"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-07 11:40:35 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resources,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)
def cloudfront_no_deprecated_ssl_protocols():
2024-08-07 11:40:35 +00:00
compliant_resources = []
non_compliant_resources = []
distributions = client.list_distributions()["DistributionList"]["Items"]
for distribution in distributions:
for origin in distribution["Origins"]["Items"]:
2024-08-07 11:40:35 +00:00
if (
"CustomOriginConfig" in origin
and origin["CustomOriginConfig"]["OriginProtocolPolicy"] in ["https-only", "match-viewer"]
2024-08-07 11:40:35 +00:00
and "SSLv3" in origin["CustomOriginConfig"]["OriginSslProtocols"]["Items"]
):
non_compliant_resources.append(distribution["ARN"])
break
else:
compliant_resources.append(distribution["ARN"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-07 11:40:35 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resources,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)
def cloudfront_s3_origin_access_control_enabled():
2024-08-07 11:40:35 +00:00
compliant_resources = []
non_compliant_resources = []
distributions = client.list_distributions()["DistributionList"]
for distribution in distributions["Items"]:
for origin in distribution["Origins"]["Items"]:
if "S3OriginConfig" in origin and origin["OriginAccessControlId"] == "":
non_compliant_resources.append(distribution["ARN"])
2024-08-07 11:40:35 +00:00
break
else:
compliant_resources.append(distribution["ARN"])
2024-08-07 11:40:35 +00:00
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-07 11:40:35 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resources,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)
def cloudfront_viewer_policy_https():
2024-08-07 11:40:35 +00:00
compliant_resources = []
non_compliant_resources = []
distributions = client.list_distributions()["DistributionList"]["Items"]
for distribution in distributions:
if distribution["DefaultCacheBehavior"]["ViewerProtocolPolicy"] == "allow-all":
non_compliant_resources.append(distribution["ARN"])
continue
allow_alls = [
behavior
for behavior in distribution["CacheBehaviors"]["Items"]
if behavior["ViewerProtocolPolicy"] == "allow-all"
]
if allow_alls:
2024-08-07 11:40:35 +00:00
non_compliant_resources.append(distribution["ARN"])
continue
compliant_resources.append(distribution["ARN"])
2024-08-07 11:40:35 +00:00
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-07 11:40:35 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resources,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)