generated from timoguin/repo-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
config.py
297 lines (220 loc) · 9.44 KB
/
config.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
"""
Classes and utilities for working with notification from AWS Config. This is a work
in progress.
See the documentation for the structure of various notifications:
https://docs.aws.amazon.com/config/latest/developerguide/notifications-for-AWS-Config.html # noqa
"""
from dataclasses import dataclass, field
import logging
from typing import Any, Optional, Union
from .base import ModelBase
logging.getLogger(__name__).addHandler(logging.NullHandler())
@dataclass
class SnapshotDeliveryStartedNotification(ModelBase):
"""Notification sent when a config snapshot delivery is started"""
message_type: str # Should be "ConfigurationSnapshotDeliveryStarted"
notification_creation_time: str
record_version: str
config_snapshot_id: Optional[str]
@dataclass
class SnapshotDeliveryCompletedNotification(ModelBase):
"""Notification sent when a config snapshot delivery is completed"""
message_type: str # Should be "ConfigurationSnapshotDeliveryCompleted"
notification_creation_time: str
record_version: str
s3_bucket: str
s3_object_key: str
config_snapshot_id: Optional[str]
@dataclass
class HistoryDeliveryStartedNotification(ModelBase):
"""Notification sent when config history delivery is started"""
message_type: str # Should be "ConfigurationHistoryDeliveryStarted"
notification_creation_time: str
record_version: str
config_snapshot_id: Optional[str]
@dataclass
class HistoryDeliveryCompletedNotification(ModelBase):
"""Notification sent when config history delivery is completed"""
message_type: str # Should be "ConfigurationHistoryDeliveryCompleted"
notification_creation_time: str
record_version: str
s3_bucket: str
s3_object_key: str
config_snapshot_id: Optional[str]
@dataclass
class ConfigurationItemRelationshipItem(ModelBase):
"""Represents a resource that can be related to a configuration item"""
name: str
resource_id: str
resource_type: str
resource_name: str
@dataclass
class ConfigurationItem(ModelBase):
"""Configuration state and relationships for a resource"""
arn: str
availability_zone: str
aws_account_id: str
configuration: dict[str, Any]
configuration_item_capture_time: str
configuration_item_status: str
configuration_item_version: str
configuration_state_id: str
related_events: list[Any]
relationships: list[ConfigurationItemRelationshipItem]
resource_creation_time: str
resource_id: str
resource_type: str
tags: dict[str, str]
# TODO: Some examples don't show this field
configuration_state_md5_hash: str = field(default=None)
# TODO: Some examples don't show this field. Unsure if type is consistent.
# Real-life item change events have a dict of dicts.
supplementary_configuration: dict[str, Any] = field(default_factory=dict)
@dataclass
class ConfigurationItemDiffChangedProperty(ModelBase):
"""Represents a changed property of a configuration item"""
change_type: str
previous_value: Optional[Union[int, str, dict[str, Any]]] = field(default=None)
updated_value: Optional[Union[int, str, dict[str, Any]]] = field(default=None)
@dataclass
class ConfigurationItemDiff(ModelBase):
"""Configuration state and relationships for a resource"""
change_type: str
changed_properties: dict[str, ConfigurationItemDiffChangedProperty]
@dataclass
class ItemChangeNotification(ModelBase):
"""Notification sent when configuration has changed for a resource"""
configuration_item: dict[str, Any]
message_type: str # Should be "ConfigurationItemChangeNotification"
configuration_item_diff: Optional[ConfigurationItemDiff]
notification_creation_time: Optional[str]
record_version: Optional[str]
# # TODO: Some examples in the docs say "ConfigurationItem" and others say
# # "ConfigurationItems". For now we'll add both options and mark them as optional.
# configuration_items: list[dict[str, Any]] = field(default_factory=list)
# configuration_item: dict[str, Any] = field(default_factory=dict)
#
# Real-life item change events show "configuration_item"
@dataclass
class ComplianceEvaluationResultQualifier(ModelBase):
"""Unique qualifiers for an evaluated resource and the name of the rule"""
config_rule_name: str
resource_type: str
resource_id: str
@dataclass
class ComplianceEvaluationResultIdentifier(ModelBase):
"""
Details about a compliance rule evaluatation against a resource, including an
ordering timestamp
"""
evaluation_result_qualifier: ComplianceEvaluationResultQualifier
ordering_timestamp: str
@dataclass
class ComplianceEvaluationResult(ModelBase):
"""The result of a rule compliance evaluation"""
evaluation_result_identifier: ComplianceEvaluationResultIdentifier
compliance_type: str
result_recorded_time: str
config_rule_invoked_time: str
# Don't know the expected type of either of these. The examples only show "null"
# for the values. Going to mark annotation as Any type and result_token as string.
# TODO: Verify the types when not null.
annotation: Optional[Any] = field(default=None)
result_token: Optional[str] = field(default=None)
@dataclass
class ComplianceChangeNotification(ModelBase):
"""Notification sent when the compliance status for a resource has changed"""
aws_account_id: str
config_rule_name: str
config_rule_arn: str
resource_type: str
resource_id: str
aws_region: str
new_evaluation_result: ComplianceEvaluationResult
old_evaluation_result: ComplianceEvaluationResult
notification_creation_time: str
message_type: str # ComplianceChangeNotification
record_version: str
# m.new_evaluation_result.compliance_type will equal COMPLIANT or NON_CONPLIANT
# TODO: It seems there is a "ScheduledEvaluation" event that is created when Config
# triggers a scheduled rule evaluation (periodic). It is sent to the Lambda during
# invocation.
#
# https://docs.aws.amazon.com/config/latest/developerguide/evaluate-config_develop-rules_example-events.html#periodic-example-event
@dataclass
class ConfigRulesEvaluationStartedNotification(ModelBase):
"""Notification sent when a rule evaluation has started"""
# TODO: Are there evaluation finished notifications?
aws_account_id: str
aws_region: str
config_rule_names: list[str]
notification_creation_time: str
message_type: str # ConfigRulesEvaluationStarted
record_version: str
@dataclass
class OversizedConfigurationItemSummary(ModelBase):
"""
The subset of configuration details provided by an oversized change notification
for a resource
"""
arn: str
aws_account_id: str
aws_region: str
change_type: str
configuration_item_capture_time: str
configuration_item_status: str
configuration_item_version: str
configuration_state_id: int
configuration_state_md5_hash: str
resource_creation_time: str
resource_id: str
resource_type: str
availability_zone: str = field(default=None)
resource_name: str = field(default=None)
@dataclass
class OversizedConfigurationItemChangeS3DeliverySummary(ModelBase):
"""Details about where an oversized configuration change were delivered in S3"""
# The bucket location is null if there was a delivery error
s3_bucket_location: str = field(default=None)
error_code: str = field(default=None)
error_message: str = field(default=None)
@dataclass
class OversizedConfigurationItemChangeNotification(ModelBase):
"""Notification sent when the a configuration change is too large for SNS"""
change_summary: OversizedConfigurationItemSummary
s3_delivery_summary: OversizedConfigurationItemChangeS3DeliverySummary
@dataclass
class DeliveryFailedNotification(ModelBase):
"""
Notification sent when a config snapshot or oversized config item change can't be
delivered to S3
https://docs.aws.amazon.com/config/latest/developerguide/notification-delivery-failed.html # noqa
"""
# TODO: There is only one documented example that shows a failed delivery for an
# oversized configuration item change. It is the same as the oversized changed
# notfication, except the S3 delivery summary has the error code and message fields
# populated, with a null value for the S3 bucket location.
#
# I'm unsure what the notification would look like for a snapshot delivery failure.
pass
# Maps the value for the "messageType" field to the corresponding model
MESSAGE_TYPE_MAP = {
"ConfigurationSnapshotDeliveryStarted": SnapshotDeliveryStartedNotification,
"ConfigurationSnapshotDeliveryCompleted": SnapshotDeliveryCompletedNotification,
"ConfigurationHistoryDeliveryStarted": HistoryDeliveryStartedNotification,
"ConfigurationHistoryDeliveryCompleted": HistoryDeliveryCompletedNotification,
"ConfigurationItemChangeNotification": ItemChangeNotification,
"ComplianceChangeNotification": ComplianceChangeNotification,
"ConfigRulesEvaluationStarted": ConfigRulesEvaluationStartedNotification,
"OversizedConfigurationItemChangeNotification": OversizedConfigurationItemChangeNotification, # noqa
"DeliveryFailedNotification": DeliveryFailedNotification,
}
def get_model(message_type: str) -> Any:
"""
Takes a message type string and returns the model class based on the above
mapping
"""
model = MESSAGE_TYPE_MAP.get(message_type)
if model is None:
raise Exception(f"Model not found for message type {message_type}")
return model