Files
floci/tools/aws/test_fetch_ec2_request_samples.py
T
Priamo Germosen e41bbd9351 fix(ec2): fill ec2:AccepterVpc and ec2:RequesterVpc for VPC peering requests (#10253)
Under IAM enforcement a VPC peering request carried neither key, so a
policy scoped by either side of the peering refused what it should allow.
Each key is now the ARN of the VPC on that side, in its own Region and
account: from the connection EC2 holds for Accept, Reject, Delete and
ModifyVpcPeeringConnectionOptions, and from the request for the
connection CreateVpcPeeringConnection makes.

The generator reads an ARN key named as another type with a word before
it from the structure member that begins with the key's name, opted in
for EC2. Create's parameters are joined to the record's sides by AWS's
own sample request and response on the API reference page, vendored by
tools/aws/fetch_ec2_request_samples.py.

Closes #8366
2026-12-05 14:42:10 -04:00

81 lines
3.5 KiB
Python

import html
import json
import pytest
import fetch_ec2_request_samples as f
def block(text):
return '<pre class="programlisting"><code>' + html.escape(text) + '</code></pre>'
REQUEST = '''https://ec2.amazonaws.com/?Action=CreateVpcPeeringConnection
&VpcId=vpc-1a2b3c4d
&PeerVpcId=vpc-a1b2c3d4
&PeerOwnerId=123456789012
&AUTHPARAMS'''
RESPONSE = '''<CreateVpcPeeringConnectionResponse xmlns="https://example.test/ec2">
<requestId>7a62c49f-347e-4fc4-9331-6e8eEXAMPLE</requestId>
<vpcPeeringConnection><vpcPeeringConnectionId>pcx-73a5401a</vpcPeeringConnectionId>
<requesterVpcInfo><ownerId>777788889999</ownerId><vpcId>vpc-1a2b3c4d</vpcId></requesterVpcInfo>
<accepterVpcInfo><ownerId>123456789012</ownerId><vpcId>vpc-a1b2c3d4</vpcId></accepterVpcInfo>
</vpcPeeringConnection>
</CreateVpcPeeringConnectionResponse>'''
def page(request=REQUEST, response=RESPONSE):
return block(request) + block(response)
def test_pairs_a_sample_request_with_the_response_that_follows_it():
assert f.read_samples("CreateVpcPeeringConnection", page()) == [{
"request": {"VpcId": "vpc-1a2b3c4d", "PeerVpcId": "vpc-a1b2c3d4", "PeerOwnerId": "123456789012"},
"response": [
{"path": ["requestId"], "value": "7a62c49f-347e-4fc4-9331-6e8eEXAMPLE"},
{"path": ["vpcPeeringConnection", "vpcPeeringConnectionId"], "value": "pcx-73a5401a"},
{"path": ["vpcPeeringConnection", "requesterVpcInfo", "ownerId"], "value": "777788889999"},
{"path": ["vpcPeeringConnection", "requesterVpcInfo", "vpcId"], "value": "vpc-1a2b3c4d"},
{"path": ["vpcPeeringConnection", "accepterVpcInfo", "ownerId"], "value": "123456789012"},
{"path": ["vpcPeeringConnection", "accepterVpcInfo", "vpcId"], "value": "vpc-a1b2c3d4"}]}]
def test_a_request_without_its_response_and_other_operations_are_left_out():
other = REQUEST.replace("Action=CreateVpcPeeringConnection", "Action=OtherAction")
lone = REQUEST.replace("PeerOwnerId=123456789012", "PeerRegion=us-west-2")
source = block(other) + block(RESPONSE) + block(lone) + page()
assert f.read_samples("CreateVpcPeeringConnection", source) == \
f.read_samples("CreateVpcPeeringConnection", page())
@pytest.mark.parametrize("source", ["", block(REQUEST), block(RESPONSE)])
def test_a_page_with_no_paired_sample_fails_closed(source):
with pytest.raises(ValueError, match="no sample request"):
f.read_samples("CreateVpcPeeringConnection", source)
def test_malformed_response_is_not_silently_omitted():
with pytest.raises(ValueError, match="malformed XML"):
f.read_samples("CreateVpcPeeringConnection", page(response=RESPONSE.replace("</accepterVpcInfo>", "")))
def test_verification_detects_changed_samples_and_ignores_retrieval_date(tmp_path, monkeypatch):
output = tmp_path / "samples.json"
monkeypatch.setattr(f, "OUTPUT", output)
fetch = lambda url: page()
assert f.main([], fetch) == 0
source = json.loads(output.read_text())
source["_source"]["retrieved"] = "2000-01-01"
output.write_text(json.dumps(source))
assert f.main(["--verify"], fetch) == 0
assert f.main(["--verify"], lambda url: page(response=RESPONSE.replace("vpc-a1b2c3d4", "vpc-ffffffff"))) == 1
def test_source_declares_namespace_and_pages_for_shared_verification():
source = f.build({url: page() for url in f.PAGES.values()})
assert source["namespace"] == "ec2"
assert source["_source"]["pages"] == list(f.PAGES.values())
source["namespace"] = "other"
assert f.stale(source, {url: page() for url in f.PAGES.values()})