Files
floci/tools/aws/test_fetch_backup_arn_formats.py
T
Priamo Germosen 16bdd60072 fix(iam): check an AWS Backup recovery point on the ARN its request names (#9884)
The Service Reference gives AWS Backup's recovery point the format
arn:${Partition}:${Vendor}:${Region}:*:${ResourceType}:${RecoveryPointId}, a wildcard where the account goes, which
no ARN a request names matches as written, so every request naming a recovery point by its ARN
(DescribeRecoveryPoint, DeleteRecoveryPoint, StartRestoreJob and the rest) was checked on *.

The AWS Backup Developer Guide's table of resource ARNs gives each kind of recovery point's ARN with the account, or
nothing, in that place. tools/aws/fetch_backup_arn_formats.py vendors the table's recovery point rows as they stand
into tools/aws/sources/backup-arn-formats.json, refusing a row with anything else there, and writes the Service
Reference's format with the account variable in place of the wildcard. ARN_FORMAT_SOURCES grants it for backup, and
with_vendored_formats now takes a vendored format that has a variable for each of the reference's wildcards and
nothing else changed. The table's recovery point format becomes
arn:${Partition}:${Vendor}:${Region}:${Account}:${ResourceType}:${RecoveryPointId}.

Some rows give a recovery point no colon between its type and id, which that format does not fit (an EBS snapshot's
arn:aws:ec2:region::snapshot/*, a DynamoDB backup's table/*/backup/*). The fetcher also writes each row as a format
of its own, the last wildcard as ${RecoveryPointId} and one before it as ${Resource}, and the table gives the
recovery point those its format does not fit already (alternateFormats): EBS snapshots, EC2 images, FSx backups and
DynamoDB backups. RequestResourceMembers matches a type's ARN against its format or any of them.

The generic recovery point format also fits an IAM role whose path holds a colon, and the rule for an ARN member had
typed the roles StartRestoreJob, StartCopyJob and StartScanJob pass as recovery points. The generator now reads the
generated passed-roles table, as it reads the authorized-actions one, and reads no resource of an action from a
member it gives as a role the operation passes, whose ARN iam:PassRole is checked on: 98 rows in 83 operations go,
of which only those five could fit a role's ARN.

test_fetch_backup_arn_formats.py checks the fetcher's copy, its formats and refusals;
test_regen_iam_request_resources.py the account wildcard's replacement, the alternates kept and the passed-role rows
dropped; RequestResourceMembersTest the alternates' read and refusals; ResourceArnBuilderTest that DescribeRecoveryPoint
is checked on the ARN in its path, a Backup one sent encoded or not and an EBS one as an SDK encodes it, and
StartRestoreJob on its recovery point and not on the role it passes; IamRequestResourceEnforcementIntegrationTest that a
grant on one recovery point's ARN lets the caller describe that one and not another.
2026-12-01 07:44:42 -04:00

99 lines
5.6 KiB
Python

#!/usr/bin/env python3
"""Tests for fetch_backup_arn_formats.py, on pages in the AWS Backup Developer Guide's markup and Service Reference
documents."""
from __future__ import annotations
import json
import pytest
import fetch_backup_arn_formats as f
ROW = ('<tr>\n <td tabindex="-1">{kind}</td>\n <td tabindex="-1"><code class="code">{arn}</code></td>\n'
' <td tabindex="-1"><code class="code">example</code></td>\n</tr>')
EBS = ("Recovery point for Amazon EBS", 'arn:aws:ec2:<code class="replaceable">region</code>::snapshot/*')
EFS = ("Recovery point for Amazon EFS", 'arn:aws:backup:<code class="replaceable">region</code>:\n'
' <code class="replaceable">account-id</code>:recovery-point:*')
DSQL = ("Recovery point for Aurora DSQL", 'arn:<code class="replaceable">aws-partition</code>:backup:'
'<code class="replaceable">region</code>:<code class="replaceable">account-id</code>:recovery-point:'
'<code class="replaceable">recovery-point-id</code>')
PLAN = ("Backup plan", 'arn:aws:backup:<code class="replaceable">region</code>:<code class="replaceable">account-id'
'</code>:backup-plan:*')
def page(*rows: tuple[str, str]) -> str:
table = ('<table><thead><tr><th>Resource type</th><th>ARN format</th><th>Example unique ID</th></tr></thead>'
+ "".join(ROW.format(kind=kind, arn=arn) for kind, arn in rows) + "</table>")
return (f'<div id="main-col-body"><p>The following table lists resources.</p><div class="table-container" '
f'id="resource-arns-table"><div class="table-contents">{table}</div></div></div>')
def reference(*formats: tuple[str, str]) -> str:
return json.dumps({"Name": "backup", "Resources": [{"Name": name, "ARNFormats": [arn]} for name, arn in formats]})
RECOVERY_POINT = ("recoveryPoint", "arn:${Partition}:${Vendor}:${Region}:*:${ResourceType}:${RecoveryPointId}")
BACKUP_PLAN = ("backupPlan", "arn:${Partition}:backup:${Region}:${Account}:backup-plan:${BackupPlanId}")
def test_the_recovery_point_rows_are_copied_as_they_stand():
built = f.build(page(PLAN, EBS, EFS, DSQL), reference(RECOVERY_POINT))
assert built["recoveryPoints"] == [
{"resourceType": "Recovery point for Amazon EBS", "format": "arn:aws:ec2:<region>::snapshot/*"},
{"resourceType": "Recovery point for Amazon EFS",
"format": "arn:aws:backup:<region>:<account-id>:recovery-point:*"},
{"resourceType": "Recovery point for Aurora DSQL",
"format": "arn:<aws-partition>:backup:<region>:<account-id>:recovery-point:<recovery-point-id>"}]
def test_a_reference_format_with_a_wildcard_for_the_account_takes_the_account_variable_there():
built = f.build(page(EBS, EFS), reference(RECOVERY_POINT, BACKUP_PLAN))
assert built["arnFormats"] == {
"recoveryPoint": "arn:${Partition}:${Vendor}:${Region}:${Account}:${ResourceType}:${RecoveryPointId}"}
def test_each_recovery_point_row_is_also_written_as_a_format():
"""Each row's ARN is a format of its own: the partition, Region and account as the Service Reference's variables,
the last wildcard or placeholder past the account the recovery point's id, one before it the resource."""
dynamodb = ("Recovery point for DynamoDB without Advanced DynamoDB backup", 'arn:aws:dynamodb:<code class='
'"replaceable">region</code>:<code class="replaceable">account-id</code>:table/*/backup/*')
built = f.build(page(EBS, EFS, DSQL, dynamodb), reference(RECOVERY_POINT))
assert built["alternateFormats"] == {"recoveryPoint": [
"arn:${Partition}:backup:${Region}:${Account}:recovery-point:${RecoveryPointId}",
"arn:${Partition}:dynamodb:${Region}:${Account}:table/${Resource}/backup/${RecoveryPointId}",
"arn:${Partition}:ec2:${Region}::snapshot/${RecoveryPointId}"]}
three = ("Recovery point for Amazon EFS", 'arn:aws:backup:<code class="replaceable">region</code>:'
'<code class="replaceable">account-id</code>:a/*/b/*/c/*')
with pytest.raises(ValueError, match="3 wildcards past its account"):
f.build(page(EBS, three), reference(RECOVERY_POINT))
def test_a_recovery_point_with_anything_else_in_the_account_place_is_refused():
other = ("Recovery point for Amazon EFS", 'arn:aws:backup:<code class="replaceable">region</code>:*:'
'recovery-point:*')
with pytest.raises(ValueError, match="neither the account nor nothing"):
f.build(page(EBS, other), reference(RECOVERY_POINT))
def test_a_page_or_a_reference_that_no_longer_says_it_is_refused():
with pytest.raises(ValueError, match="no table of AWS Backup resource ARNs"):
f.build('<div id="main-col-body"><table></table></div>', reference(RECOVERY_POINT))
with pytest.raises(ValueError, match="lists no recovery point"):
f.build(page(PLAN), reference(RECOVERY_POINT))
with pytest.raises(ValueError, match="no AWS Backup format with a wildcard"):
f.build(page(EFS), reference(BACKUP_PLAN))
def test_stale_compares_everything_but_the_date():
built = f.build(page(EBS, EFS), reference(RECOVERY_POINT))
assert not f.stale(dict(built, _source={"retrieved": "2000-01-01"}), page(EBS, EFS), reference(RECOVERY_POINT))
assert f.stale(built, page(EBS, EFS, DSQL), reference(RECOVERY_POINT))
def test_the_vendored_file_is_the_one_the_pages_give():
vendored = json.loads(f.OUTPUT.read_text(encoding="utf-8"))
assert vendored["arnFormats"] == {
"recoveryPoint": "arn:${Partition}:${Vendor}:${Region}:${Account}:${ResourceType}:${RecoveryPointId}"}
assert all(f.ROW_RE.match(row["format"]) for row in vendored["recoveryPoints"])
assert vendored["alternateFormats"] == f.alternate_formats(vendored["recoveryPoints"])