From f7923b48906e85dc8b34a31ae1298cb8223ed580 Mon Sep 17 00:00:00 2001 From: EC2 Default User Date: Wed, 14 Aug 2024 04:21:46 +0000 Subject: [PATCH] Handle exceptions gracefully --- models.py | 13 ++++++++++++- services/cloudfront.py | 2 +- services/codeseries.py | 6 ++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/models.py b/models.py index 26ca79e..e7e768a 100644 --- a/models.py +++ b/models.py @@ -15,4 +15,15 @@ class RuleChecker: def check_rule(self, rule_name) -> RuleCheckResult: check_func = getattr(self, convert_snake_case(rule_name)) - return check_func() + try: + result = check_func() + except Exception as e: + result = RuleCheckResult( + passed=False, + compliant_resources=[], + non_compliant_resources=[ + "Rule check failed due to folling errors: ", + str(e), + ], + ) + return result diff --git a/services/cloudfront.py b/services/cloudfront.py index 7e78237..d8037c8 100644 --- a/services/cloudfront.py +++ b/services/cloudfront.py @@ -9,7 +9,7 @@ class CloudFrontRuleChecker(RuleChecker): @cached_property def distributions(self): - return self.client.list_distributions()["DistributionList"]["Items"] + return self.client.list_distributions()["DistributionList"].get("Items", []) @cached_property def distribution_details(self): diff --git a/services/codeseries.py b/services/codeseries.py index ca3d013..9f691b2 100644 --- a/services/codeseries.py +++ b/services/codeseries.py @@ -11,6 +11,8 @@ class CodeSeriesChecker(RuleChecker): @cached_property def projects(self): project_names = self.build_client.list_projects()["projects"] + if not project_names: + return [] return self.build_client.batch_get_projects(names=project_names)["projects"] def codebuild_project_environment_privileged_check(self): @@ -59,6 +61,10 @@ class CodeSeriesChecker(RuleChecker): deployment_group_names = self.deploy_client.list_deployment_groups( applicationName=application )["deploymentGroups"] + + if not deployment_group_names: + continue + deployment_groups = self.deploy_client.batch_get_deployment_groups( applicationName=application, deploymentGroupNames=deployment_group_names )["deploymentGroupsInfo"]