#!/usr/bin/env python3 """Tests for fetch_sts_source_identity.py, on a page in the IAM user guide's markup.""" from __future__ import annotations import pytest import fetch_sts_source_identity as f PERMISSIONS = ("

Permissions required to set source identity

") THINGS = ("

Things to know about source identity

Trust policies for all roles connected to an identity " "provider (IdP) must have the sts:SetSourceIdentity permission.

The value that is set " "persists when a role is used to " "assume another role through the AWS CLI or AWS API, known as role chaining. The value that is set " "cannot be changed during the role session.

When an identity sets a source identity, the " "sts:SourceIdentity key is present in the request. For subsequent actions taken during the " "role session, the aws:SourceIdentity key is present in the request.

") ASSUME_ROLE = ("

Using source identity with AssumeRole

To pass source identity while assuming a role, use " "the -–source-identity AWS CLI option or the SourceIdentity AWS API " "parameter.

") SAML = ("

Using source identity with AssumeRoleWithSAML

To set a SAML attribute for source identity, include " "the Attribute element with the Name attribute set to " "https://aws.amazon.com/SAML/Attributes/SourceIdentity. Use the AttributeValue " "element.

") WEB_IDENTITY = ("

Using source identity with AssumeRoleWithWebIdentity

Include source identity in the " 'https://aws.amazon.com/' "source_identity namespace in the token when you submit the request.

") OTHER = "

Using source identity with AssumeRoot

Nothing to read here.

" CHAINED = ("

Control access using source identity information

Therefore, both the " "aws:SourceIdentity and sts:SourceIdentity keys are present in the request " "context.

") def page(*parts: str) -> str: sections = parts or (THINGS, PERMISSIONS, ASSUME_ROLE, SAML, WEB_IDENTITY, OTHER, CHAINED) return f'

Monitor

{"".join(sections)}
' def test_each_operation_naming_its_parameter_is_read_with_the_key_it_carries(): built = f.build(lambda url: page()) # A section naming no API parameter, as the SAML one, gives no request member. assert built["keys"] == [{"operation": "AssumeRole", "member": "SourceIdentity", "key": "sts:SourceIdentity", "row": "To pass source identity while assuming a role, use the -–source-identity AWS " "CLI option or the SourceIdentity AWS API parameter."}] assert built["permission"]["action"] == "sts:SetSourceIdentity" assert built["permission"]["chaining"].startswith("When you assume a role with another role, called role chaining") assert built["sessionKey"]["key"] == "aws:SourceIdentity" assert built["persists"].endswith("The value that is set cannot be changed during the role session.") assert built["permission"]["identityProvider"].startswith("Trust policies for all roles connected to an identity") def test_a_federated_operation_is_read_with_the_attribute_or_claim_carrying_the_value(): federated = {entry["operation"]: entry for entry in f.build(lambda url: page())["federated"]} assert set(federated) == {"AssumeRoleWithSAML", "AssumeRoleWithWebIdentity"} assert federated["AssumeRoleWithSAML"]["attribute"] == "https://aws.amazon.com/SAML/Attributes/SourceIdentity" # The page links the claim's host inside its code element; the claim is the element's whole text. assert federated["AssumeRoleWithWebIdentity"]["claim"] == "https://aws.amazon.com/source_identity" assert federated["AssumeRoleWithWebIdentity"]["row"].startswith( "Include source identity in the https://aws.amazon.com/source_identity namespace") assert all(entry["key"] == "sts:SourceIdentity" for entry in federated.values()) def test_a_page_whose_layout_or_wording_changed_is_refused(): with pytest.raises(ValueError, match="names no API parameter"): f.build(lambda url: page(THINGS, PERMISSIONS, SAML, CHAINED)) with pytest.raises(ValueError, match="which action specifying a source identity needs"): f.build(lambda url: page(THINGS, ASSUME_ROLE, CHAINED)) with pytest.raises(ValueError, match="names sts:SetSourceIdentity for setting"): f.build(lambda url: page(THINGS, PERMISSIONS.replace("permissions for sts:SetSourceIdentity", "permissions for sts:SetOther"), ASSUME_ROLE, CHAINED)) with pytest.raises(ValueError, match="a role an identity provider is connected to"): f.build(lambda url: page(THINGS.replace("must have the sts:SetSourceIdentity permission", "must have the sts:Other permission"), PERMISSIONS, ASSUME_ROLE, CHAINED)) with pytest.raises(ValueError, match="other keys for a chained request"): f.build(lambda url: page(THINGS, PERMISSIONS, ASSUME_ROLE, CHAINED.replace("aws:SourceIdentity", "aws:Other"))) def test_the_file_is_stale_when_the_page_says_something_else(): vendored = f.build(lambda url: page()) assert not f.stale(vendored, page()) assert f.stale(vendored, page(THINGS, PERMISSIONS, ASSUME_ROLE.replace("SourceIdentity", "Identity"), CHAINED))