Repository navigation
Expand file tree
/
Copy pathcloudformation_lambda_resource_policy_probe.py
More file actions
executable file
·308 lines (286 loc) · 17.5 KB
/
Copy pathcloudformation_lambda_resource_policy_probe.py
File metadata and controls
executable file
·308 lines (286 loc) · 17.5 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
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
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python3
"""Capture exact-owned native CFN function policies and stale-Sid deletion.
Evidence is never overwritten; --cleanup-only resumes its durable inventory.
Only own-account grants are used. Functions are never invoked.
"""
import argparse
import hashlib
import json
import os
from pathlib import Path
import signal
import urllib.request
import uuid
import boto3
import botocore
from botocore.config import Config
from cloudformation_lambda_layer_probe import LayerProbe, archive_bytes
from cloudformation_lambda_version_probe import VersionProbe
from cloudtrail_service_probe import REGION, document, now
TYPE = "AWS::Lambda::ResourcePolicy"
SOURCES = [
"https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-lambda-resourcepolicy.md",
"https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-lambda-permission.md",
"https://docs.aws.amazon.com/AWSCloudFormation/latest/TemplateReference/aws-resource-lambda-layerversionpermission.md",
"https://docs.aws.amazon.com/lambda/latest/api/API_PutResourcePolicy.md",
"https://docs.aws.amazon.com/lambda/latest/api/API_DeleteResourcePolicy.md",
]
class ResourcePolicyProbe(LayerProbe):
def __init__(self, args):
self.args = args
self.account = args.account
self.root = "arn:aws:iam::" + self.account + ":root"
self.session = boto3.Session(region_name=REGION)
config = Config(ignore_configured_endpoint_urls=True, parameter_validation=False,
retries={"total_max_attempts": 1}, connect_timeout=5, read_timeout=35)
self.clients = {name: self.session.client(name, config=config)
for name in ("sts", "iam", "lambda", "cloudformation", "logs")}
self.credentials = self.session.get_credentials().get_frozen_credentials()
self.cleanup_phase = False
if args.cleanup_only:
self.data = json.loads(args.output.read_text())
if (self.data["account"], self.data["region"]) != (self.account, REGION):
raise RuntimeError("Inventory outside approved account/region")
if not self.data["prefix"].startswith("stackd-cfn-fpolicy-"):
raise RuntimeError("Not a function-policy inventory")
else:
if args.output.exists():
raise RuntimeError("Refusing to overwrite native evidence")
self.data = {
"source": "Native AWS public endpoints; signed boto3; endpoint overrides disabled",
"captured_at": now(), "account": self.account, "region": REGION,
"prefix": "stackd-cfn-fpolicy-" + uuid.uuid4().hex[:12],
"sdk": {"boto3": boto3.__version__, "botocore": botocore.__version__},
"environment": {"credential_method": self.session.get_credentials().method,
"aws_variable_names": sorted(k for k in os.environ if k.startswith("AWS_"))},
"scope": "Exact-owned non-invoked function, permissionless role, layer, stacks; own-account/root grants only",
"bounds": {"stack_wait_seconds": 420, "poll_seconds": 3},
"owned": {"stacks": {}, "layers": {}, "statements": [], "aliases": []},
"calls": [], "findings": {}, "templates": {}, "schemas": {},
"sources": SOURCES, "documentation": [],
"calibration_gaps": [
"Cross-account authorization and invocation are unmeasured; own-account grants only.",
"Public, wildcard, organization and function-URL grants are unmeasured and never created.",
"Private CloudFormation ownership tokens, restart recovery and local SQLite persistence are not native measurements.",
],
"cleanup": {"complete": False}, "workflow_complete": False,
}
self.save()
identity = self.call("identity", "sts", "get_caller_identity")
if identity["Account"] != self.account:
raise RuntimeError("Unauthorized native account")
self.data["identity"] = identity
self.finding("authorized_identity", identity)
def documents(self):
for name in (TYPE, "AWS::Lambda::Permission"):
registry = self.call("schema-" + name.split("::")[-1], "cloudformation", "describe_type",
{"Type": "RESOURCE", "TypeName": name}, required=False)
if not registry:
self.data["calibration_gaps"].append(name + " DescribeType unavailable: " + self.code())
continue
schema = json.loads(registry["Schema"])
self.data["schemas"][name] = schema
self.finding("schema_" + name.split("::")[-1], {key: schema.get(key) for key in
("properties", "primaryIdentifier", "readOnlyProperties", "createOnlyProperties", "required")})
if self.args.schema_output:
path = self.args.schema_output
capture = json.loads(path.read_text())
if (capture["account"], capture["region"]) != (self.account, REGION):
raise RuntimeError("Unauthorized schema inventory")
row = self.data["calls"][-1]
capture["types"][name] = {
"input": row["input"], "schema": schema,
"metadata": {key: value for key, value in registry.items() if key != "Schema"},
"arn": registry.get("Arn", ""), "default_version_id": registry.get("DefaultVersionId", ""),
"request_id": row["metadata"].get("RequestId"), "http_status": row["metadata"].get("HTTPStatusCode"),
"captured_at": row["finished_at"], "capture": str(self.args.output),
}
path.write_text(json.dumps(document(capture), indent=2) + "\n")
for source in SOURCES:
try:
with urllib.request.urlopen(source, timeout=30) as response:
raw = response.read()
self.data["documentation"].append({"source": source, "captured_at": now(),
"sha256": hashlib.sha256(raw).hexdigest(), "content": raw.decode()})
except Exception as error:
self.data["calibration_gaps"].append(source + ": " + str(error))
self.save()
schema = self.data["schemas"].get(TYPE, {})
self.finding("documentation_schema_contradiction", {
"documentation_property": "FunctionResourceArn",
"registry_properties": sorted(schema.get("properties", {})),
"registry_primary_identifier": schema.get("primaryIdentifier"),
"boundary": "Template syntax/property documentation says FunctionResourceArn; native registry and documentation example use ResourceArn."})
def stack(self, key, label, template, update=False):
result = VersionProbe.stack(self, key, label, template, update)
owned = self.data["owned"]["stacks"][key]
if owned.get("id") and result.get("stack_status") not in ("CREATE_COMPLETE", "UPDATE_COMPLETE"):
self.call(label + "-validation-details", "cloudformation", "describe_events",
{"StackName": owned["id"]}, required=False)
return result
def resource_template(self, kind, properties):
outputs = {"ResourceRef": {"Value": {"Ref": "Resource"}}}
for pointer in self.data["schemas"].get(kind, {}).get("readOnlyProperties", []):
if pointer.count("/") == 2:
name = pointer.split("/")[-1]
outputs["Attribute" + name] = {"Value": {"Fn::GetAtt": ["Resource", name]}}
return {"AWSTemplateFormatVersion": "2010-09-09", "Resources": {
"Resource": {"Type": kind, "Properties": properties}}, "Outputs": outputs}
def policy(self, label, arn, layer=False):
if layer:
name, version = arn.rsplit(":", 1)
request = {"LayerName": name, "VersionNumber": int(version)}
operation = "get_layer_version_policy"
else:
self.assert_function_arn(arn)
request, operation = {"ResourceArn": arn}, "get_resource_policy"
result = self.call(label, "lambda", operation, request, required=False)
return {"code": self.code(), **result,
**({"document": json.loads(result["Policy"])} if result.get("Policy") else {})}
def assert_function_arn(self, arn):
base = self.data["owned"]["function"]["arn"]
allowed = [base] + [base + ":" + v for v in self.data["owned"]["function"]["versions"]]
allowed += [base + ":" + alias for alias in self.data["owned"]["aliases"]]
if arn not in allowed:
raise RuntimeError("Function ARN lacks exact-created inventory")
def stale_permission(self, changed=False, layer=False):
label = ("layer" if layer else "function") + ("-changed" if changed else "-identical")
if layer:
name = self.reserve_layer(label)
result = self.call(label + "-publish", "lambda", "publish_layer_version", {
"LayerName": name, "Description": self.data["prefix"],
"Content": {"ZipFile": archive_bytes("python/probe.py", "VALUE = 1\n")}})
arn = result["LayerVersionArn"]
self.remember_layer(arn)
request = {"LayerName": name, "VersionNumber": result["Version"]}
kind, action, field = "AWS::Lambda::LayerVersionPermission", "lambda:GetLayerVersion", "LayerVersionArn"
remove, add = "remove_layer_version_permission", "add_layer_version_permission"
else:
arn = self.data["owned"]["function"]["arn"]
request = {"FunctionName": arn}
kind, action, field = "AWS::Lambda::Permission", "lambda:InvokeFunction", "FunctionName"
remove, add = "remove_permission", "add_permission"
template = self.resource_template(kind, {field: arn, "Action": action, "Principal": self.account})
created = self.stack(label, label + "-create", template)
if created.get("stack_status") != "CREATE_COMPLETE":
raise RuntimeError(label + " permission creation failed")
before = self.policy(label + "-before", arn, layer)
statements = before["document"]["Statement"]
if len(statements) != 1:
raise RuntimeError("Expected exactly one exact-created statement")
sid = statements[0]["Sid"]
self.data["owned"]["statements"].append({"arn": arn, "sid": sid, "stack": label})
self.save()
self.call(label + "-native-remove", "lambda", remove, {**request, "StatementId": sid})
grant = {**request, "StatementId": sid, "Action": action, "Principal": self.account}
if changed:
if layer:
# Root is normalized by Lambda; layer statement's only other mutable
# grant field is OrganizationId, intentionally outside this probe.
raise RuntimeError("Changed layer statement is outside authorized probe")
grant["SourceAccount"] = self.account
self.call(label + "-native-readd", "lambda", add, grant)
replacement = self.policy(label + "-replacement", arn, layer)
status = self.delete_stack(label, label + "-delete")
after = self.policy(label + "-after-delete", arn, layer)
self.finding(label + "-stale-delete", {"before": before, "replacement": replacement,
"replacement_statement_equal": before["document"]["Statement"] == replacement["document"]["Statement"],
"delete_status": status, "after": after,
"replacement_survives": any(item.get("Sid") == sid for item in after.get("document", {}).get("Statement", []))})
def policy_document(self, arn, *sids):
self.assert_function_arn(arn)
return {"Version": "2012-10-17", "Statement": [{"Sid": sid, "Effect": "Allow",
"Principal": {"AWS": self.root}, "Action": "lambda:InvokeFunction", "Resource": arn} for sid in sids]}
def policy_stack(self, label, arn, sids, update=False, *, key="policy", allow_failure=False):
schema = self.data["schemas"][TYPE]
properties = schema["properties"]
arn_fields = [name for name in properties if name.endswith("Arn") and name not in
[path.split("/")[-1] for path in schema.get("readOnlyProperties", [])]]
if len(arn_fields) != 1:
raise RuntimeError("Cannot derive canonical ARN property from native schema")
self.finding("canonical_arn_property", arn_fields[0])
template = self.resource_template(TYPE, {arn_fields[0]: arn, "PolicyDocument": self.policy_document(arn, *sids)})
result = self.stack(key, label, template, update)
self.finding(label + "-policy", self.policy(label + "-read", arn))
if not allow_failure and result.get("stack_status") not in ("CREATE_COMPLETE", "UPDATE_COMPLETE"):
raise RuntimeError(label + " failed")
return result
def workflow(self):
self.documents()
VersionProbe.setup(self)
function = self.data["owned"]["function"]
log_name = "/aws/lambda/" + function["name"]
logs = self.call("log-name-absent", "logs", "describe_log_groups", {"logGroupNamePrefix": log_name})
if any(item["logGroupName"] == log_name for item in logs.get("logGroups", [])):
raise RuntimeError("Unexpected existing function log group")
self.data["owned"]["log_group"] = {"name": log_name, "absence_verified": True}
self.save()
if not self.args.policy_only:
self.stale_permission()
self.stale_permission(changed=True)
self.stale_permission(layer=True)
if TYPE not in self.data["schemas"]:
raise RuntimeError("Native ResourcePolicy schema unavailable")
arn = function["arn"]
if not self.args.policy_only:
self.call("preexisting-owned-policy", "lambda", "put_resource_policy", {
"ResourceArn": arn, "Policy": json.dumps(self.policy_document(arn, "PreexistingOwned"))})
self.policy_stack("policy-existing-create", arn, ["First"], key="conflict", allow_failure=True)
self.delete_stack("conflict", "policy-existing-delete-stack")
self.call("policy-existing-delete-native", "lambda", "delete_resource_policy", {"ResourceArn": arn}, required=False)
self.policy_stack("policy-create", arn, ["First"])
self.call("policy-native-extra", "lambda", "add_permission", {
"FunctionName": arn, "StatementId": "NativeExtra", "Action": "lambda:InvokeFunction", "Principal": self.account})
self.finding("policy-before-update", self.policy("policy-before-update-read", arn))
self.policy_stack("policy-full-update", arn, ["Second", "Third"], update=True)
published = self.call("publish-owned-version", "lambda", "publish_version", {"FunctionName": arn})
function["versions"].append(published["Version"])
self.save()
version_arn = published["FunctionArn"]
self.policy_stack("policy-version-replacement", version_arn, ["VersionGrant"], update=True)
self.finding("policy-old-base-after-replacement", self.policy("policy-old-base-read", arn))
alias = "owned"
self.data["owned"]["aliases"].append(alias)
self.save()
alias_result = self.call("create-owned-alias", "lambda", "create_alias", {
"FunctionName": arn, "Name": alias, "FunctionVersion": published["Version"]})
alias_arn = alias_result["AliasArn"]
self.policy_stack("policy-alias-replacement", alias_arn, ["AliasGrant"], update=True)
self.finding("policy-old-version-after-replacement", self.policy("policy-old-version-read", version_arn))
overwritten = self.policy_document(alias_arn, "NativeOverwrite")
self.call("policy-native-overwrite", "lambda", "put_resource_policy", {
"ResourceArn": alias_arn, "Policy": json.dumps(overwritten)})
self.finding("policy-overwrite-before-delete", self.policy("policy-overwrite-read", alias_arn))
status = self.delete_stack("policy", "policy-delete-overwrite")
self.finding("policy-overwrite-delete", {"delete_status": status,
"after": self.policy("policy-after-delete", alias_arn)})
self.policy_stack("policy-normal-create", arn, ["Normal"])
status = self.delete_stack("policy", "policy-normal-delete")
self.finding("policy-normal-delete", {"delete_status": status,
"after": self.policy("policy-normal-after-delete", arn)})
self.data["workflow_complete"] = True
self.save()
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--account", required=True, help="AWS account ID that STS must match before native writes")
parser.add_argument("--output", type=Path, default=Path(".stackd/probes/cloudformation/function_resource_policy.json"))
parser.add_argument("--schema-output", type=Path)
parser.add_argument("--cleanup-only", action="store_true")
parser.add_argument("--policy-only", action="store_true", help="Skip previously captured stale-Sid and existing-policy scenarios")
args = parser.parse_args()
probe = ResourcePolicyProbe(args)
def interrupted(signum, frame):
raise RuntimeError("Interrupted by signal " + str(signum))
for signum in (signal.SIGINT, signal.SIGTERM):
signal.signal(signum, interrupted)
try:
if not args.cleanup_only:
probe.workflow()
except Exception as error:
probe.data["failure"] = {"at": now(), "error": str(error)}
probe.save()
raise
finally:
probe.cleanup()
if __name__ == "__main__":
main()