#!/usr/bin/env python3 """Tests for fetch_secretsmanager_required_permissions.py, on the Service Reference and pages in the AWS Secrets Manager API reference's markup.""" from __future__ import annotations import pytest import fetch_secretsmanager_required_permissions as f def operation(name: str, *actions: str) -> dict: return {"Name": name, "AuthorizedActions": [{"Service": action.split(":")[0], "Name": action.split(":")[1]} for action in (f"secretsmanager:{name}", *actions)]} REFERENCE = {"Operations": [ operation("BatchGetSecretValue", "secretsmanager:GetSecretValue", "secretsmanager:ListSecrets"), operation("CreateSecret", "secretsmanager:ReplicateSecretToRegions"), # An operation authorized by its own action, or by another namespace's beside it, has no page read. operation("DescribeSecret"), operation("RotateSecret", "iam:PassRole"), ]} BATCH = ("

Required permissions: secretsmanager:BatchGetSecretValue, and you must have\n" "secretsmanager:GetSecretValue for each secret. If you use filters, you must also have\n" "secretsmanager:ListSecrets. If the secrets are encrypted using customer-managed keys, then you also " "need kms:Decrypt permissions for the keys.

") CREATE = ("

Required permissions: secretsmanager:CreateSecret. If you include tags in the secret, you " "also need secretsmanager:TagResource. To add replica Regions, you must also have " "secretsmanager:ReplicateSecretToRegions.

") def page(*paragraphs: str) -> str: return (f'

Operation

Does a thing.

{"".join(paragraphs)}' f'

Request Syntax

') def pages(batch: str = BATCH, create: str = CREATE) -> dict[str, str]: return {"BatchGetSecretValue": page(batch), "CreateSecret": page(create)} def test_each_clause_of_the_required_permissions_is_read_with_what_its_action_is_needed_for(): built = f.build(REFERENCE, pages()) assert [entry["operation"] for entry in built["required"]] == ["BatchGetSecretValue", "CreateSecret"] batch, create = built["required"] # The KMS sentence names no secretsmanager: action and is copied with the paragraph, not read. assert batch["permissions"] == [{"action": "secretsmanager:BatchGetSecretValue"}, {"action": "secretsmanager:GetSecretValue", "each": "secret"}, {"action": "secretsmanager:ListSecrets", "when": "filters"}] assert create["permissions"] == [{"action": "secretsmanager:CreateSecret"}, {"action": "secretsmanager:TagResource", "when": "tags"}, {"action": "secretsmanager:ReplicateSecretToRegions", "when": "add replica Regions"}] assert batch["paragraph"].startswith("Required permissions: secretsmanager:BatchGetSecretValue, and you must") def test_both_actions_of_a_required_pair_and_one_on_a_named_resource_are_read(): pair = f.clauses("Required permissions: secretsmanager:ValidateResourcePolicy and " "secretsmanager:PutResourcePolicy.") assert pair == [{"action": "secretsmanager:ValidateResourcePolicy"}, {"action": "secretsmanager:PutResourcePolicy"}] on = f.clauses("Required permissions: secretsmanager:AssociateCrossAccountReplica, secretsmanager:GetSecretValue " "on the primary secret, and secretsmanager:AcceptCrossAccountReplication on the replica secret.") assert on[1:] == [{"action": "secretsmanager:GetSecretValue", "on": "primary secret"}, {"action": "secretsmanager:AcceptCrossAccountReplication", "on": "replica secret"}] def test_a_page_whose_layout_or_wording_changed_is_refused(): with pytest.raises(ValueError, match="gives no required permissions"): f.build(REFERENCE, pages(batch="

Permissions: secretsmanager:BatchGetSecretValue.

")) with pytest.raises(ValueError, match="which names no action as they did"): f.build(REFERENCE, pages(batch="

Required permissions: secretsmanager:BatchGetSecretValue, or a key.

")) # A conditional sentence AWS rewords is refused rather than dropping its action. with pytest.raises(ValueError, match="gives a secretsmanager: action in no wording they used"): f.build(REFERENCE, pages(batch=BATCH.replace("If you use filters, you must also have", "With filters, you need"))) def test_the_file_is_stale_when_the_reference_or_a_page_says_something_else(): vendored = f.build(REFERENCE, pages()) assert not f.stale(vendored, REFERENCE, pages()) assert f.stale(vendored, REFERENCE, pages(create=CREATE.replace("To add replica Regions", "To add Regions"))) fewer = {"Operations": REFERENCE["Operations"][1:]} assert f.stale(vendored, fewer, pages())