Handle exceptions gracefully

This commit is contained in:
EC2 Default User 2024-08-14 04:21:46 +00:00
parent fb94b40c23
commit f7923b4890
3 changed files with 19 additions and 2 deletions

View File

@ -15,4 +15,15 @@ class RuleChecker:
def check_rule(self, rule_name) -> RuleCheckResult: def check_rule(self, rule_name) -> RuleCheckResult:
check_func = getattr(self, convert_snake_case(rule_name)) 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

View File

@ -9,7 +9,7 @@ class CloudFrontRuleChecker(RuleChecker):
@cached_property @cached_property
def distributions(self): def distributions(self):
return self.client.list_distributions()["DistributionList"]["Items"] return self.client.list_distributions()["DistributionList"].get("Items", [])
@cached_property @cached_property
def distribution_details(self): def distribution_details(self):

View File

@ -11,6 +11,8 @@ class CodeSeriesChecker(RuleChecker):
@cached_property @cached_property
def projects(self): def projects(self):
project_names = self.build_client.list_projects()["projects"] project_names = self.build_client.list_projects()["projects"]
if not project_names:
return []
return self.build_client.batch_get_projects(names=project_names)["projects"] return self.build_client.batch_get_projects(names=project_names)["projects"]
def codebuild_project_environment_privileged_check(self): def codebuild_project_environment_privileged_check(self):
@ -59,6 +61,10 @@ class CodeSeriesChecker(RuleChecker):
deployment_group_names = self.deploy_client.list_deployment_groups( deployment_group_names = self.deploy_client.list_deployment_groups(
applicationName=application applicationName=application
)["deploymentGroups"] )["deploymentGroups"]
if not deployment_group_names:
continue
deployment_groups = self.deploy_client.batch_get_deployment_groups( deployment_groups = self.deploy_client.batch_get_deployment_groups(
applicationName=application, deploymentGroupNames=deployment_group_names applicationName=application, deploymentGroupNames=deployment_group_names
)["deploymentGroupsInfo"] )["deploymentGroupsInfo"]