Handle botocore.exceptions.ClientError

This commit is contained in:
skyuecx0630 2024-08-07 16:06:40 +09:00
parent aab42f61b0
commit f16a8fe911

View File

@ -1,5 +1,6 @@
from models import RuleCheckResult from models import RuleCheckResult
import boto3 import boto3
import botocore.exceptions
client = boto3.client("s3") client = boto3.client("s3")
@ -36,8 +37,8 @@ def s3_bucket_default_lock_enabled():
try: try:
response = client.get_object_lock_configuration(Bucket=bucket["Name"]) response = client.get_object_lock_configuration(Bucket=bucket["Name"])
compliant_resources.append(f"arn:aws:s3:::{bucket['Name']}") compliant_resources.append(f"arn:aws:s3:::{bucket['Name']}")
except Exception as e: except botocore.exceptions.ClientError as e:
if e.__class__.__name__ == "ObjectLockConfigurationNotFoundError": if e.response['Error']['Code'] == "ObjectLockConfigurationNotFoundError":
non_compliant_resources.append(f"arn:aws:s3:::{bucket['Name']}") non_compliant_resources.append(f"arn:aws:s3:::{bucket['Name']}")
else: else:
raise e raise e
@ -197,8 +198,8 @@ def s3_lifecycle_policy_check():
try: try:
configuration = client.get_bucket_lifecycle_configuration(Bucket=bucket["Name"]) configuration = client.get_bucket_lifecycle_configuration(Bucket=bucket["Name"])
compliant_resources.append(f"arn:aws:s3:::{bucket['Name']}") compliant_resources.append(f"arn:aws:s3:::{bucket['Name']}")
except Exception as e: except botocore.exceptions.ClientError as e:
if e.__class__.__name__ == "NoSuchLifecycleConfiguration": if e.response['Error']['Code'] == "NoSuchLifecycleConfiguration":
non_compliant_resources.append(f"arn:aws:s3:::{bucket['Name']}") non_compliant_resources.append(f"arn:aws:s3:::{bucket['Name']}")
else: else:
raise e raise e