bp-check/services/ecs.py

228 lines
8.8 KiB
Python
Raw Normal View History

2024-08-05 02:30:34 +00:00
from models import RuleCheckResult
import boto3
2024-08-10 05:49:34 +00:00
client = boto3.client("ecs")
2024-08-05 02:30:34 +00:00
def ecs_awsvpc_networking_enabled():
2024-08-10 05:49:34 +00:00
compliant_resources = []
non_compliant_resources = []
task_definitions = client.list_task_definitions(status="ACTIVE")["taskDefinitionArns"]
latest_task_definitions = {}
for task_definition in task_definitions:
2024-08-10 08:50:51 +00:00
family, revision = task_definition.rsplit(":", 1)
latest_task_definitions[family] = max(latest_task_definitions.get(family, 0), int(revision))
2024-08-10 05:49:34 +00:00
2024-08-10 08:50:51 +00:00
for family, revision in latest_task_definitions.items():
task_definition_arn = f"{family}:{revision}"
2024-08-10 05:49:34 +00:00
task_definition = client.describe_task_definition(taskDefinition=task_definition_arn)["taskDefinition"]
if task_definition.get("networkMode") == "awsvpc":
compliant_resources.append(task_definition["taskDefinitionArn"])
else:
non_compliant_resources.append(task_definition["taskDefinitionArn"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-10 05:49:34 +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 ecs_containers_nonprivileged():
2024-08-10 05:49:34 +00:00
compliant_resources = []
non_compliant_resources = []
task_definitions = client.list_task_definitions(status="ACTIVE")["taskDefinitionArns"]
latest_task_definitions = {}
for task_definition in task_definitions:
2024-08-10 08:50:51 +00:00
family, revision = task_definition.rsplit(":", 1)
latest_task_definitions[family] = max(latest_task_definitions.get(family, 0), int(revision))
2024-08-10 05:49:34 +00:00
2024-08-10 08:50:51 +00:00
for family, revision in latest_task_definitions.items():
task_definition_arn = f"{family}:{revision}"
2024-08-10 05:49:34 +00:00
task_definition = client.describe_task_definition(taskDefinition=task_definition_arn)["taskDefinition"]
containers = task_definition["containerDefinitions"]
for container in containers:
2024-08-10 08:50:51 +00:00
if container.get("privileged"):
2024-08-10 05:49:34 +00:00
non_compliant_resources.append(task_definition["taskDefinitionArn"])
break
else:
compliant_resources.append(task_definition["taskDefinitionArn"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-10 05:49:34 +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 ecs_containers_readonly_access():
2024-08-10 05:49:34 +00:00
compliant_resources = []
non_compliant_resources = []
task_definitions = client.list_task_definitions(status="ACTIVE")["taskDefinitionArns"]
latest_task_definitions = {}
for task_definition in task_definitions:
2024-08-10 08:50:51 +00:00
family, revision = task_definition.rsplit(":", 1)
latest_task_definitions[family] = max(latest_task_definitions.get(family, 0), int(revision))
2024-08-10 05:49:34 +00:00
2024-08-10 08:50:51 +00:00
for family, revision in latest_task_definitions.items():
task_definition_arn = f"{family}:{revision}"
2024-08-10 05:49:34 +00:00
task_definition = client.describe_task_definition(taskDefinition=task_definition_arn)["taskDefinition"]
containers = task_definition["containerDefinitions"]
for container in containers:
if container.get("readonlyRootFilesystem") == False:
non_compliant_resources.append(task_definition["taskDefinitionArn"])
break
else:
compliant_resources.append(task_definition["taskDefinitionArn"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-10 05:49:34 +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 ecs_container_insights_enabled():
2024-08-10 05:49:34 +00:00
compliant_resources = []
non_compliant_resources = []
cluster_arns = client.list_clusters()["clusterArns"]
for cluster_arn in cluster_arns:
clusters = client.describe_clusters(clusters=[cluster_arn], include=["SETTINGS"])["clusters"]
for cluster in clusters:
settings = cluster["settings"]
for setting in settings:
if setting["name"] == "containerInsights" and setting["value"] == "enabled":
compliant_resources.append(cluster_arn)
break
else:
non_compliant_resources.append(cluster_arn)
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-10 05:49:34 +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 ecs_fargate_latest_platform_version():
2024-08-10 05:49:34 +00:00
compliant_resources = []
non_compliant_resources = []
cluster_arns = client.list_clusters()["clusterArns"]
for cluster_arn in cluster_arns:
services = client.list_services(cluster=cluster_arn, launchType="FARGATE")["serviceArns"]
for service in services:
service = client.describe_services(cluster=cluster_arn, services=[service])["services"][0]
if service["launchType"] == "FARGATE":
if service["platformVersion"] == "LATEST":
compliant_resources.append(service["serviceArn"])
else:
non_compliant_resources.append(service["serviceArn"])
else:
compliant_resources.append(service["serviceArn"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-10 05:49:34 +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 ecs_task_definition_log_configuration():
2024-08-10 05:49:34 +00:00
compliant_resources = []
non_compliant_resources = []
task_definitions = client.list_task_definitions(status="ACTIVE")["taskDefinitionArns"]
latest_task_definitions = {}
for task_definition in task_definitions:
2024-08-10 08:50:51 +00:00
family, revision = task_definition.rsplit(":", 1)
latest_task_definitions[family] = max(latest_task_definitions.get(family, 0), int(revision))
2024-08-10 05:49:34 +00:00
2024-08-10 08:50:51 +00:00
for family, revision in latest_task_definitions.items():
task_definition_arn = f"{family}:{revision}"
2024-08-10 05:49:34 +00:00
task_definition = client.describe_task_definition(taskDefinition=task_definition_arn)["taskDefinition"]
containers = task_definition["containerDefinitions"]
for container in containers:
2024-08-10 08:50:51 +00:00
if "logConfiguration" not in container:
2024-08-10 05:49:34 +00:00
non_compliant_resources.append(task_definition["taskDefinitionArn"])
break
else:
compliant_resources.append(task_definition["taskDefinitionArn"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-10 05:49:34 +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 ecs_task_definition_memory_hard_limit():
2024-08-10 05:49:34 +00:00
compliant_resources = []
non_compliant_resources = []
task_definitions = client.list_task_definitions(status="ACTIVE")["taskDefinitionArns"]
latest_task_definitions = {}
for task_definition in task_definitions:
2024-08-10 08:50:51 +00:00
family, revision = task_definition.rsplit(":", 1)
latest_task_definitions[family] = max(latest_task_definitions.get(family, 0), int(revision))
2024-08-10 05:49:34 +00:00
2024-08-10 08:50:51 +00:00
for family, revision in latest_task_definitions.items():
task_definition_arn = f"{family}:{revision}"
2024-08-10 05:49:34 +00:00
task_definition = client.describe_task_definition(taskDefinition=task_definition_arn)["taskDefinition"]
containers = task_definition["containerDefinitions"]
for container in containers:
2024-08-10 08:50:51 +00:00
if "memory" not in container:
2024-08-10 05:49:34 +00:00
non_compliant_resources.append(task_definition["taskDefinitionArn"])
break
else:
compliant_resources.append(task_definition["taskDefinitionArn"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-10 05:49:34 +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 ecs_task_definition_nonroot_user():
2024-08-10 05:49:34 +00:00
compliant_resources = []
non_compliant_resources = []
task_definitions = client.list_task_definitions(status="ACTIVE")["taskDefinitionArns"]
latest_task_definitions = {}
for task_definition in task_definitions:
2024-08-10 08:50:51 +00:00
family, revision = task_definition.rsplit(":", 1)
latest_task_definitions[family] = max(latest_task_definitions.get(family, 0), int(revision))
2024-08-10 05:49:34 +00:00
2024-08-10 08:50:51 +00:00
for family, revision in latest_task_definitions.items():
task_definition_arn = f"{family}:{revision}"
2024-08-10 05:49:34 +00:00
task_definition = client.describe_task_definition(taskDefinition=task_definition_arn)["taskDefinition"]
containers = task_definition["containerDefinitions"]
for container in containers:
if container.get("user") in [None, "root"]:
non_compliant_resources.append(task_definition["taskDefinitionArn"])
break
else:
compliant_resources.append(task_definition["taskDefinitionArn"])
2024-08-05 02:30:34 +00:00
return RuleCheckResult(
2024-08-10 05:49:34 +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
)