bp-check/services/ecr.py

86 lines
2.7 KiB
Python
Raw Normal View History

2024-08-05 02:30:34 +00:00
from models import RuleCheckResult
import boto3
2024-08-06 04:07:28 +00:00
import botocore
2024-08-05 02:30:34 +00:00
2024-08-06 04:07:28 +00:00
client = boto3.client("ecr")
2024-08-05 02:30:34 +00:00
def ecr_private_image_scanning_enabled():
2024-08-06 04:07:28 +00:00
repositories = client.describe_repositories()
compliant_resource = []
non_compliant_resources = []
for repository in repositories["repositories"]:
if repository["imageScanningConfiguration"]["scanOnPush"] == True:
compliant_resource.append(repository["repositoryArn"])
else:
non_compliant_resources.append(repository["repositoryArn"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-06 04:07:28 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resource,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)
def ecr_private_lifecycle_policy_configured():
2024-08-06 04:07:28 +00:00
repositories = client.describe_repositories()
compliant_resource = []
non_compliant_resources = []
for repository in repositories["repositories"]:
try:
response = client.get_lifecycle_policy(
registryId=repository["registryId"],
repositoryName=repository["repositoryName"],
)
compliant_resource.append(repository["repositoryArn"])
except Exception as e:
if e.__class__.__name__ == "LifecyclePolicyNotFoundException":
non_compliant_resources.append(repository["repositoryArn"])
else:
raise e
2024-08-06 04:07:28 +00:00
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-06 04:07:28 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resource,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)
def ecr_private_tag_immutability_enabled():
2024-08-06 04:07:28 +00:00
repositories = client.describe_repositories()
compliant_resource = []
non_compliant_resources = []
for repository in repositories["repositories"]:
if repository["imageTagMutability"] == "IMMUTABLE":
compliant_resource.append(repository["repositoryArn"])
else:
non_compliant_resources.append(repository["repositoryArn"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-06 04:07:28 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resource,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)
def ecr_kms_encryption_1():
2024-08-06 04:07:28 +00:00
repositories = client.describe_repositories()
compliant_resource = []
non_compliant_resources = []
for repository in repositories["repositories"]:
if repository["encryptionConfiguration"]["encryptionType"] == "KMS":
compliant_resource.append(repository["repositoryArn"])
else:
non_compliant_resources.append(repository["repositoryArn"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-06 04:07:28 +00:00
passed=not non_compliant_resources,
compliant_resources=compliant_resource,
non_compliant_resources=non_compliant_resources,
2024-08-05 02:30:34 +00:00
)