"""Tests for the AWS Session Manager documentation extract.""" from __future__ import annotations import pytest import fetch_ssm_session_permissions as f @pytest.fixture def pages() -> dict[str, str]: default = """
The system creates a document called SSM-SessionManagerRunShell.
This is the default session document.
By default, Session Manager uses the default session document when a user starts a session by using an AWS CLI command like the following example:
To start a default shell session, you must specify the default session document in the IAM policy.
aws ssm start-session --target i-02573cafcfEXAMPLE
{"Statement": [{"Effect": "Allow", "Action": ["ssm:StartSession"],
"Resource": ["arn:aws:ec2:us-east-1:111122223333:instance/instance-id",
"arn:aws:ssm:us-east-1:111122223333:document/SSM-SessionManagerRunShell"]}]}
"""
return {
f.DEFAULT_PAGE: default,
f.HYBRID_PAGE: 'Hybrid managed nodes are distinguished from Amazon EC2 instances with the prefix "mi-". ' 'Amazon EC2 instance IDs use the prefix "i-".
', f.SESSIONS_PAGE: "aws ssm start-session --target ecs:<cluster>_<container>_<id>"
"",
**{url: f'POST / HTTP/1.1\nX-Amz-Target: AmazonSSM.{operation}\n'
'\n{"Target": "i-02573cafcfEXAMPLE"}'
for operation, url in f.API_PAGES.items()},
}
def test_extract_copies_the_guide_facts_and_api_requests(pages: dict[str, str]) -> None:
source = f.build(pages)
assert source["_source"]["pages"] == list(pages)
assert source["defaultDescription"] == (
"The system creates a document called SSM-SessionManagerRunShell. This is the default session document."
)
assert source["defaultPolicy"]["Statement"][0]["Resource"] == [
"arn:aws:ec2:us-east-1:111122223333:instance/instance-id",
"arn:aws:ssm:us-east-1:111122223333:document/SSM-SessionManagerRunShell",
]
assert source["defaultCommand"] == "aws ssm start-session --target i-02573cafcfEXAMPLE"
assert source["sessionRequests"] == {name: {"Target": "i-02573cafcfEXAMPLE"} for name in f.API_PAGES}
assert source["managedNodePrefixes"].endswith('Amazon EC2 instance IDs use the prefix "i-".')
assert source["ecsCommand"] == "aws ssm start-session --target ecs:{}"
with pytest.raises(ValueError, match="sample request"):
f.build(pages)