#!/usr/bin/env python3
"""Tests for fetch_kms_api_permissions.py, on pages in the AWS KMS developer guide's and API reference's markup."""
from __future__ import annotations
import json
import pytest
import fetch_kms_api_permissions as f
API = "https://docs.aws.amazon.com/kms/latest/APIReference/"
HEAD = ('
| Actions and permissions | Policy type | Cross-account use | '
'Resources (for IAM policies) | AWS KMS condition keys |
')
DECRYPT = ('Decrypt '
'kms:Decrypt
| Key policy | '
'Yes | KMS key | '
' kms:EncryptionAlgorithm |
')
RE_ENCRYPT = ('ReEncrypt '
'kms:ReEncryptFrom
kms:ReEncryptTo '
'To use this operation, the caller needs permission on two KMS keys: | Key policy | '
'Yes | KMS key | None '
' |
')
# An operation whose first cell spans two rows, the second giving the key policy's type and resources.
CREATE_ALIAS = ('| CreateAlias'
' kms:CreateAlias
To use this operation, the caller needs '
'kms:CreateAlias permission on two resources: | '
'IAM policy (for the alias) | No | '
'Alias | None |
'
'Key policy (for the KMS key) | No | '
'KMS key | kms:CallerAccount |
')
KEY_ID = ('KeyId'
'Specifies the KMS key that AWS KMS uses to decrypt the ciphertext.
This parameter is required only '
'when the ciphertext was encrypted under an asymmetric KMS key. If you used a symmetric encryption KMS key, '
'AWS KMS can get the KMS key from metadata that it adds to the symmetric ciphertext blob. However, it is '
'always recommended as a best practice.
')
BLOB = ('CiphertextBlob'
'Ciphertext to be decrypted. The blob includes metadata.
')
CONCEPTS = ('- Alias ARN
The alias ARN is the '
'Amazon Resource Name (ARN) of an AWS KMS alias.
The format of an alias ARN is as follows:
'
'arn:<partition>:kms:'
'<region>:<account-id>:alias/'
'<alias-name>
'
'- Key ID
A key ID uniquely identifies '
'a KMS key.
'
'- Alias name
In the AWS KMS API, '
'alias names always begin with alias/.
The format of an alias name is as '
'follows:
alias/<alias-name>'
'
')
def permissions_page(*rows: str, times: int = 1) -> str:
return "" + HEAD + "".join(rows) * times + "
"
def test_each_operation_of_the_table_is_copied_with_its_actions_list_and_policies():
rows = f.read_permissions(permissions_page(DECRYPT, RE_ENCRYPT, CREATE_ALIAS))
assert rows == [
{"operation": "Decrypt", "apiReference": API + "API_Decrypt.html", "actions": ["kms:Decrypt"],
"description": [], "items": [], "policies": [{"policyType": "Key policy", "resources": "KMS key"}]},
{"operation": "ReEncrypt", "apiReference": API + "API_ReEncrypt.html",
"actions": ["kms:ReEncryptFrom", "kms:ReEncryptTo"],
"description": ["To use this operation, the caller needs permission on two KMS keys:"],
"items": ["kms:ReEncryptFrom on the KMS key used to decrypt",
"kms:ReEncryptTo on the KMS key used to encrypt"],
"policies": [{"policyType": "Key policy", "resources": "KMS key"}]},
{"operation": "CreateAlias", "apiReference": API + "API_CreateAlias.html", "actions": ["kms:CreateAlias"],
"description": ["To use this operation, the caller needs kms:CreateAlias permission on two resources:"],
"items": ["The alias (in an IAM policy)", "The KMS key (in a key policy)"],
"policies": [{"policyType": "IAM policy (for the alias)", "resources": "Alias"},
{"policyType": "Key policy (for the KMS key)", "resources": "KMS key"}]},
]
def test_a_page_whose_table_changed_its_layout_is_refused():
with pytest.raises(ValueError, match="tables"):
f.read_permissions("")
with pytest.raises(ValueError, match="headed"):
f.read_permissions("")
with pytest.raises(ValueError, match="naming no operation"):
f.read_permissions(permissions_page(DECRYPT.replace('Decrypt',
"Decrypt")))
def test_a_parameter_saying_kms_reads_its_resource_from_metadata_is_copied_with_that_sentence():
assert f.read_from_metadata("" + BLOB + KEY_ID + "
") == [
{"operation": "Decrypt", "parameter": "KeyId",
"sentence": "If you used a symmetric encryption KMS key, AWS KMS can get the KMS key from metadata that it "
"adds to the symmetric ciphertext blob."}]
def test_each_key_identifier_giving_a_format_is_copied_with_its_variables_marked():
assert f.read_identifiers(CONCEPTS) == [
{"identifier": "Alias ARN", "sentence": "The format of an alias ARN is as follows:",
"format": "arn:{partition}:kms:{region}:{account-id}:alias/{alias-name}"},
{"identifier": "Alias name", "sentence": "The format of an alias name is as follows:",
"format": "alias/{alias-name}"}]
with pytest.raises(ValueError, match="alias name and alias ARN"):
f.read_identifiers("
")
def test_the_file_is_stale_when_a_page_says_something_else(monkeypatch):
monkeypatch.setattr(f, "MINIMUM_ROWS", 1)
pages = (permissions_page(DECRYPT), {API + "API_Decrypt.html": "" + KEY_ID + "
"}, CONCEPTS)
vendored = f.build(*pages)
assert vendored["fromMetadata"][0]["parameter"] == "KeyId"
assert not f.stale(json.loads(json.dumps(vendored)), *pages)
assert f.stale(vendored, pages[0], {API + "API_Decrypt.html": "" + BLOB + "
"}, CONCEPTS)
with pytest.raises(ValueError, match="rows"):
monkeypatch.setattr(f, "MINIMUM_ROWS", 40)
f.build(*pages)