mirror of
https://github.com/floci-io/floci.git
synced 2026-11-30 00:45:17 -05:00
The Service Reference lists the actions each Secrets Manager operation is authorized by without saying when each is needed, so a request was checked as its own action alone: a BatchGetSecretValue using Filters without secretsmanager:ListSecrets, a CreateSecret adding replica Regions without secretsmanager:ReplicateSecretToRegions and a ValidateResourcePolicy without secretsmanager:PutResourcePolicy were all let through. Each operation's page in the API reference says it, in its required permissions: "If you use filters, you must also have secretsmanager:ListSecrets", "To add replica Regions, you must also have secretsmanager:ReplicateSecretToRegions", "Required permissions: secretsmanager:ValidateResourcePolicy and secretsmanager:PutResourcePolicy". tools/aws/fetch_secretsmanager_required_permissions.py copies that paragraph for each operation the Service Reference lists as authorized by another of its actions, with each clause's action and what it is needed for or on, into tools/aws/sources/secretsmanager-required-permissions.json. Two generators read it: - regen_iam_authorized_actions.py: an action needed when the request does something is a companion of the request member the clause's words name as the generators' names go (filters is Filters, add replica Regions AddReplicaRegions), where the Service Reference lists it for the operation and does not mark it as tagging, which the tag-on-create table checks. A companion a request member sets may now carry no session key. - regen_iam_request_resources.py: actions an operation always needs beside its own are each checked on what the rules read for it (ValidateResourcePolicy and PutResourcePolicy on the secret its SecretId names). A request checked by parts that names nothing is now checked on * as each action the table checks on the same resources as its own, not as its own alone, so a ValidateResourcePolicy naming no secret still needs PutResourcePolicy. The fetcher refuses a sentence naming a secretsmanager: action in a wording it does not read, so a reworded page fails rather than dropping a check, and the enforcement test runs on the AWS SDK. Not read: GetSecretValue for each secret of a batch, which AWS reports per secret in Errors rather than refusing the call; PutResourcePolicy for a cross-account replica and DeleteResourcePolicy for one stopped replicating, whose words name no member of the request; and the KMS permissions of a secret on a customer managed key.
84 lines
5.0 KiB
Python
84 lines
5.0 KiB
Python
#!/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 = ("<p>Required permissions: <code>secretsmanager:BatchGetSecretValue</code>, and you must have\n"
|
|
"<code>secretsmanager:GetSecretValue</code> for each secret. If you use filters, you must also have\n"
|
|
"<code>secretsmanager:ListSecrets</code>. If the secrets are encrypted using customer-managed keys, then you also "
|
|
"need <code>kms:Decrypt</code> permissions for the keys.</p>")
|
|
CREATE = ("<p>Required permissions: <code>secretsmanager:CreateSecret</code>. If you include tags in the secret, you "
|
|
"also need <code>secretsmanager:TagResource</code>. To add replica Regions, you must also have "
|
|
"<code>secretsmanager:ReplicateSecretToRegions</code>.</p>")
|
|
|
|
|
|
def page(*paragraphs: str) -> str:
|
|
return (f'<div id="main-col-body"><h1>Operation</h1><p>Does a thing.</p>{"".join(paragraphs)}'
|
|
f'<h2>Request Syntax</h2></div><div class="prev-next"></div>')
|
|
|
|
|
|
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="<p>Permissions: secretsmanager:BatchGetSecretValue.</p>"))
|
|
with pytest.raises(ValueError, match="which names no action as they did"):
|
|
f.build(REFERENCE, pages(batch="<p>Required permissions: secretsmanager:BatchGetSecretValue, or a key.</p>"))
|
|
# 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())
|