generated from app-sre/python-project-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
validate_plan.py
152 lines (127 loc) · 4.92 KB
/
validate_plan.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import logging
import os
import sys
from collections.abc import Mapping, Sequence
from typing import TYPE_CHECKING, Any
from boto3 import Session
from botocore.config import Config
from external_resources_io.input import parse_model, read_input_from_file
from external_resources_io.terraform import (
Action,
ResourceChange,
TerraformJsonPlanParser,
)
if TYPE_CHECKING:
from mypy_boto3_ec2.client import EC2Client
from mypy_boto3_ec2.type_defs import SecurityGroupTypeDef, SubnetTypeDef
else:
EC2Client = SubnetTypeDef = SecurityGroupTypeDef = object
from er_aws_msk.app_interface_input import AppInterfaceInput
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logging.getLogger("botocore").setLevel(logging.ERROR)
MIN_SUBNETS = 3
class AWSApi:
"""AWS Api Class"""
def __init__(self, config_options: Mapping[str, Any]) -> None:
self.session = Session()
self.config = Config(**config_options)
@property
def ec2_client(self) -> EC2Client:
"""Gets a boto EC2 client"""
return self.session.client("ec2", config=self.config)
def get_subnets(self, subnets: Sequence[str]) -> list[SubnetTypeDef]:
"""Get the subnet"""
data = self.ec2_client.describe_subnets(
SubnetIds=subnets,
)
return data["Subnets"]
def get_security_groups(
self, security_groups: Sequence[str]
) -> list[SecurityGroupTypeDef]:
"""Get the subnet"""
data = self.ec2_client.describe_security_groups(
GroupIds=security_groups,
)
return data["SecurityGroups"]
class MskPlanValidator:
"""The plan validator class"""
def __init__(
self, plan: TerraformJsonPlanParser, app_interface_input: AppInterfaceInput
) -> None:
self.plan = plan
self.input = app_interface_input
self.aws_api = AWSApi(config_options={"region_name": self.input.data.region})
self.errors: list[str] = []
@property
def msk_instance_updates(self) -> list[ResourceChange]:
"""Get the msk instance updates"""
return [
c
for c in self.plan.plan.resource_changes
if c.type == "aws_msk_cluster"
and c.change
and Action.ActionCreate in c.change.actions
]
def _validate_subnets(self, subnets: Sequence[str]) -> str | None:
logger.info(f"Validating subnets {subnets}")
vpc_ids: set[str] = set()
if len(subnets) < MIN_SUBNETS:
self.errors.append("At least 3 subnets are required")
return None
data = self.aws_api.get_subnets(subnets)
if missing := set(subnets).difference({s.get("SubnetId") for s in data}):
self.errors.append(f"Subnet(s) {missing} not found")
return None
for subnet in data:
if "VpcId" not in subnet:
self.errors.append(
f"VpcId not found for subnet {subnet.get('SubnetId')}"
)
continue
vpc_ids.add(subnet["VpcId"])
if len(vpc_ids) > 1:
self.errors.append("All subnets must belong to the same VPC")
return vpc_ids.pop()
def _validate_security_groups(
self, security_groups: Sequence[str], vpc_id: str
) -> None:
logger.info(f"Validating security group {security_groups}")
data = self.aws_api.get_security_groups(security_groups)
if missing := set(security_groups).difference({s.get("GroupId") for s in data}):
self.errors.append(f"Security group(s) {missing} not found")
return
for sg in data:
if sg.get("VpcId") != vpc_id:
self.errors.append(
f"Security group {sg.get('GroupId')} does not belong to the same VPC as the subnets"
)
def validate(self) -> bool:
"""Validate method"""
for u in self.msk_instance_updates:
if not u.change or not u.change.after:
continue
if vpc_id := self._validate_subnets(
subnets=u.change.after["broker_node_group_info"][0]["client_subnets"]
):
self._validate_security_groups(
security_groups=u.change.after["broker_node_group_info"][0][
"security_groups"
],
vpc_id=vpc_id,
)
return not self.errors
if __name__ == "__main__":
app_interface_input = parse_model(
AppInterfaceInput,
read_input_from_file(
file_path=os.environ.get("ER_INPUT_FILE", "/inputs/input.json"),
),
)
logger.info("Running MSK terraform plan validation")
plan = TerraformJsonPlanParser(plan_path=sys.argv[1])
validator = MskPlanValidator(plan, app_interface_input)
if not validator.validate():
logger.error(validator.errors)
sys.exit(1)
logger.info("Validation ended succesfully")