Files
floci/tools/aws/test_fetch_cloudtrail_s3_data_events.py
T
Priamo Germosen 215e37eb48 fix(cloudtrail): name S3 denials as CloudTrail records the operation's data event (#9460)
IamEnforcementFilter named the CloudTrail data event of a denied S3 request
from a hand-written table keyed by the IAM action, so a request checked
under another operation's action took that operation's name: HeadBucket
was recorded as ListObjects, CopyObject, UploadPart and UploadPartCopy as
PutObject, GetObjectTorrent as GetObject, ListBuckets as a data event,
and ListObjectVersions, ListParts, DeleteObjects, AbortMultipartUpload and
the legal hold and retention calls were not recorded.

A denial is now recorded under the data event of the operation the request
matched, from src/main/resources/aws/cloudtrail-s3-data-events.json, which
tools/aws/regen_cloudtrail_s3_data_events.py generates from botocore's S3
model, the S3 user guide's data events table and its sentence naming the
event a call is recorded with (vendored by
tools/aws/fetch_cloudtrail_s3_data_events.py), and AWS's S3 service
reference. An operation the table for general purpose buckets lists is
recorded under its name or the one the guide gives it (PutObjectRetention
as PutObjectLockRetention), a versioned one under the version-less name
the table lists (ListObjectsV2 as ListObjects), and an operation it does
not list records no data event. readOnly is the service reference's, by
the reading iam-action-resources.json's readOnlyActions are vendored by,
in place of CloudTrailService's hand-written list.

The IAM data drift workflow runs on a pull request touching any of the six
new files, so the online check covers a change to the table alone.
2026-11-25 15:10:24 -04:00

59 lines
3.4 KiB
Python

#!/usr/bin/env python3
"""Tests for fetch_cloudtrail_s3_data_events.py, on a page in the S3 user guide's markup."""
from __future__ import annotations
import pytest
import fetch_cloudtrail_s3_data_events as f
def row(console: str, kind: str, apis: list[str]) -> str:
"""A row of the data events table, its APIs an itemized list as the guide lays them out."""
items = "".join(f'<li class="listitem"><p><a href="https://docs.aws.amazon.com/AmazonS3/latest/API/API_{api.split()[0]}'
f'.html">{api.split(" (")[0]}</a>{api[len(api.split(" (")[0]):]}</p></li>' for api in apis)
return (f'<tr><td tabindex="-1"><b>{console}</b></td><td tabindex="-1"><code class="code">{kind}</code></td>'
f'<td tabindex="-1"><div class="itemizedlist"><ul class="itemizedlist">{items}</ul></div></td></tr>')
RECORDED_AS = ('<p>When you call <code class="code">PutObjectRetention</code>, the CloudTrail data event is\n'
' recorded with the <code class="code">eventName</code>\n <code class="code">PutObjectLockRetention'
'</code>, and includes\n <code class="code">objectRetentionInfo</code> in\n'
' <code class="code">additionalEventData</code>.</p>')
PAGE = ('<html><body><p>Amazon S3 logs all control plane operations as management events.</p>'
'<table id="w1"><thead><tr><th>Data event type (console)</th><th>resources.type value</th>'
'<th>Data APIs logged to CloudTrail</th></tr></thead>'
+ row("S3", "AWS::S3::Object", ["GetObject", "ListObjects", "PutObjectRetention"])
+ row("S3 Access Point", "AWS::S3::Access Point",
["CopyObject (same-region copies only)", "ListObjects", "ListObjectsV2"])
+ "</table>" + RECORDED_AS + "</body></html>")
def test_each_resource_type_keeps_its_api_cells_as_they_stand():
assert f.read_data_events(PAGE) == {
"AWS::S3::Object": ["GetObject", "ListObjects", "PutObjectRetention"],
"AWS::S3::Access Point": ["CopyObject (same-region copies only)", "ListObjects", "ListObjectsV2"]}
def test_a_sentence_naming_the_event_a_call_is_recorded_with_is_kept_as_it_stands():
assert f.read_recorded_as(PAGE) == [{
"call": "PutObjectRetention", "eventName": "PutObjectLockRetention",
"sentence": "When you call PutObjectRetention, the CloudTrail data event is recorded with the eventName "
"PutObjectLockRetention, and includes objectRetentionInfo in additionalEventData."}]
assert f.read_recorded_as("<p>Amazon S3 logs all control plane operations as management events.</p>") == []
def test_a_page_whose_layout_changed_is_refused():
with pytest.raises(ValueError, match="no table headed"):
f.read_data_events(PAGE.replace("resources.type value", "Resource type"))
with pytest.raises(ValueError, match="lists no resource type"):
f.read_data_events(PAGE.split("<table")[0] + '<table id="w1"><tr><th>Data event type (console)</th>'
"<th>resources.type value</th><th>Data APIs logged to CloudTrail</th></tr></table>")
def test_the_file_is_stale_when_the_page_says_something_else():
vendored = f.build(PAGE)
assert vendored["_source"]["page"] == f.PAGE
assert not f.stale(vendored, PAGE)
assert f.stale(vendored, PAGE.replace(">ListObjectsV2<", ">ListObjectVersions<"))
assert f.stale(vendored, PAGE.replace("PutObjectLockRetention", "PutObjectRetention"))