Files
floci/tools/aws/test_fetch_eventbridge_conditions.py
T
Priamo Germosen b4cc436ad0 fix(iam): read EventBridge's rule and target condition keys from the vendored user guide (#9673)
Under enforcement a PutRule scoped by events:source was refused for every event pattern, and a PutTargets scoped by events:TargetArn for every target, as #8320 reports: the request condition key table never filled either key, so a Null or ForAllValues condition on them never saw a value. The table's reader is generic, but which member carries a key comes from SERVICES in regen_request_condition_keys.py, and EventBridge was not there. Neither botocore nor the Service Reference says where a request carries these keys: botocore models EventPattern as a string ("The event pattern").

EventBridge's user guide says it, in its table of condition keys: events:source is "the literal string for the source field of the event", with events:detail-type, events:detail.service, events:detail.eventTypeCode and events:detail.userIdentity.principalId the same for their fields, which a rule's event pattern gives ("EventPattern is a mandatory argument to PutRule"), and events:TargetArn is "the ARN of the target for the rule". A vendored source, tools/aws/sources/eventbridge-condition-keys.json, written by tools/aws/fetch_eventbridge_conditions.py from that page, gives each key its member, and the generator reads it for events (CONDITION_SOURCES), each key in the one operation the page names: PutEvents, CreateEventSource and UpdateEventSource take events:source too, which the page does not describe, so they are not read there. --verify checks the file against the page.

The table writes a field of a JSON document a string member holds after #: EventPattern#source.* is each literal string of the pattern's source, read by one generic step of the JSON reader. A pattern leaving a field out names nothing, so ForAllValues allows it and a Null check refuses it, as the page's examples show. The fields are read at the pattern's top level, where every example on the page gives them, so a $or branch's names nothing, and a matcher such as {"prefix": "aws."} is no literal string and names nothing. events:TargetArn reads each target's own Arn, not its dead-letter queue's.

Closes #8320.
2026-11-28 15:50:13 -04:00

58 lines
3.3 KiB
Python

#!/usr/bin/env python3
"""Tests for fetch_eventbridge_conditions.py, on a page in the EventBridge user guide's markup."""
from __future__ import annotations
import pytest
import fetch_eventbridge_conditions as f
def row(key: str, value: str) -> str:
return f"<tr><td><p><code>{key}</code></p></td><td><p>{value}</p></td><td><p>ArrayOfString, Null</p></td></tr>"
ROWS = (row("events:creatorAccount", "For creatorAccount, use the account ID for the account that created the rule.")
+ row("events:source", 'Use source for the literal string for the <b>source</b> field of the event such as '
'"aws.ec2".')
+ row("events: detail.service", "For service, use the literal string for the <b>detail.service</b> field of "
"the event, such as \"ABUSE\".")
+ row("events:TargetArn", 'For target-arn, use the ARN of the target for the rule, for example "arn:aws:lambda:'
'*:*:function:*".'))
SAYS = ("<p>the user must also have permissions for the <code>events:PutTargets</code> action because, if you want the "
"rule to do more than publish a CloudWatch metric, you must also add a target to a rule.</p>"
"<p>To grant access to a specific resource, use the <code>events:TargetArn</code> condition key.</p>"
"<p>Policy B allows <code>PutRule</code> only if the event pattern of the rule being created matches Amazon "
"EC2 events.</p><p><code>EventPattern</code> is a mandatory argument to <code>PutRule</code>.</p>")
def page(rows: str = ROWS, says: str = SAYS) -> str:
return (f'<div id="main-col-body">{says}<table><tr><th>Condition key</th><th>Key value pair</th></tr>{rows}'
f'</table></div><div class="prev-next"></div>')
def test_each_key_is_read_from_the_member_its_row_names():
keys = {entry["key"]: entry for entry in f.build(lambda url: page())["keys"]}
# A field of the event is read from the rule's event pattern, a target's ARN from each target; a key naming
# neither is not read.
assert (keys["events:source"]["operation"], keys["events:source"]["member"], keys["events:source"]["field"]) \
== ("PutRule", "EventPattern", "source")
assert keys["events:detail.service"]["field"] == "detail.service"
assert (keys["events:TargetArn"]["operation"], keys["events:TargetArn"]["member"]) == ("PutTargets",
"Targets.*.Arn")
assert "field" not in keys["events:TargetArn"]
assert "events:creatorAccount" not in keys
assert keys["events:source"]["says"][1] == "EventPattern is a mandatory argument to PutRule."
def test_a_page_whose_layout_changed_is_refused():
for changed, message in ((page(rows=""), "no table"), (page(says=SAYS.replace("mandatory", "required")),
"no longer says where PutRule")):
with pytest.raises(ValueError, match=message):
f.build(lambda url: changed)
def test_the_file_is_stale_when_the_page_says_something_else():
vendored = f.build(lambda url: page())
assert not f.stale(vendored, page())
assert f.stale(vendored, page(rows=ROWS.replace("<b>source</b> field", "<b>origin</b> field")))