Repository navigation
Expand file tree
/
Copy pathmodels.py
More file actions
201 lines (157 loc) · 4.83 KB
/
Copy pathmodels.py
File metadata and controls
201 lines (157 loc) · 4.83 KB
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
"""Typed, immutable records for the deliberately bounded offline schema."""
from __future__ import annotations
from dataclasses import asdict, dataclass
from datetime import UTC, datetime
from typing import ClassVar
def timestamp(value: datetime) -> str:
return value.astimezone(UTC).isoformat().replace("+00:00", "Z")
@dataclass(frozen=True)
class Statement:
sid: str
effect: str
actions: tuple[str, ...]
resources: tuple[str, ...]
principals: tuple[tuple[str, str], ...]
conditions: tuple[tuple[str, str, tuple[str, ...]], ...]
@dataclass(frozen=True)
class PolicyDocument:
statements: tuple[Statement, ...]
@dataclass(frozen=True)
class PolicyResource:
id: str
document: PolicyDocument
resource_type: ClassVar[str] = "iam_policy"
@dataclass(frozen=True)
class RoleResource:
id: str
trust_policy: PolicyDocument
resource_type: ClassVar[str] = "iam_role"
@dataclass(frozen=True)
class PublicAccessBlock:
block_public_acls: bool
ignore_public_acls: bool
block_public_policy: bool
restrict_public_buckets: bool
@dataclass(frozen=True)
class Encryption:
algorithm: str
key_id: str | None
key_manager: str | None
@dataclass(frozen=True)
class BucketResource:
id: str
region: str
sensitive: bool
block_public_access: PublicAccessBlock
encryption: Encryption
versioning: bool
resource_type: ClassVar[str] = "s3_bucket"
@dataclass(frozen=True)
class IngressRule:
protocol: str
from_port: int | None
to_port: int | None
cidr: str
@dataclass(frozen=True)
class SecurityGroupResource:
id: str
region: str
ingress: tuple[IngressRule, ...]
resource_type: ClassVar[str] = "security_group"
@dataclass(frozen=True)
class TrailResource:
id: str
home_region: str
is_logging: bool
is_multi_region: bool
covered_regions: tuple[str, ...]
log_bucket: str
resource_type: ClassVar[str] = "cloudtrail"
Resource = PolicyResource | RoleResource | BucketResource | SecurityGroupResource | TrailResource
@dataclass(frozen=True)
class ResourceInventory:
policies: tuple[PolicyResource, ...]
roles: tuple[RoleResource, ...]
buckets: tuple[BucketResource, ...]
security_groups: tuple[SecurityGroupResource, ...]
trails: tuple[TrailResource, ...]
def all_resources(self) -> tuple[Resource, ...]:
return (*self.policies, *self.roles, *self.buckets, *self.security_groups, *self.trails)
@dataclass(frozen=True)
class Snapshot:
schema_version: int
synthetic: bool
snapshot_id: str
account_id: str
captured_at: datetime
required_regions: tuple[str, ...]
resources: ResourceInventory
source_sha256: str
@property
def resource_keys(self) -> tuple[tuple[str, str], ...]:
return tuple(sorted((r.resource_type, r.id) for r in self.resources.all_resources()))
@dataclass(frozen=True)
class Control:
id: str
title: str
severity: str
resource_type: str
remediation: str
references: tuple[str, ...]
limitations: tuple[str, ...]
def to_dict(self) -> dict[str, object]:
return asdict(self)
@dataclass(frozen=True)
class Finding:
finding_id: str
control_id: str
title: str
severity: str
resource_id: str
resource_type: str
status: str
reason: str
evidence: tuple[dict[str, object], ...]
remediation: str
references: tuple[str, ...]
limitations: tuple[str, ...]
def to_dict(self) -> dict[str, object]:
result = asdict(self)
result["evidence"] = list(self.evidence)
result["references"] = list(self.references)
result["limitations"] = list(self.limitations)
return result
@dataclass(frozen=True)
class Evaluation:
control_id: str
resource_id: str
resource_type: str
status: str
def to_dict(self) -> dict[str, object]:
return asdict(self)
@dataclass(frozen=True)
class Assessment:
snapshot_id: str
account_id: str
captured_at: datetime
source_sha256: str
controls_executed: tuple[str, ...]
findings: tuple[Finding, ...]
evaluations: tuple[Evaluation, ...]
inventory: tuple[tuple[str, str], ...]
def to_dict(self) -> dict[str, object]:
return {
"schema_version": 1,
"synthetic": True,
"snapshot_id": self.snapshot_id,
"account_id": self.account_id,
"captured_at": timestamp(self.captured_at),
"source_sha256": self.source_sha256,
"controls_executed": list(self.controls_executed),
"findings": [finding.to_dict() for finding in self.findings],
"evaluations": [evaluation.to_dict() for evaluation in self.evaluations],
"inventory": [
{"resource_type": kind, "resource_id": identifier}
for kind, identifier in self.inventory
],
}