mirror of
https://github.com/floci-io/floci.git
synced 2026-11-29 08:31:53 -05:00
Under enforcement a role whose trust policy conditions AssumeRoleWithSAML or AssumeRoleWithWebIdentity on sts:SourceIdentity refused every assume, and one that withheld sts:SetSourceIdentity was never consulted: neither operation read the source identity its identity provider sets, so the session carried none, the response returned none and its requests carried no aws:SourceIdentity. The Service Reference gives both actions sts:SourceIdentity but lists no authorized actions for either operation, and botocore models the value only in the response. The IAM user guide's page on source identity, which tools/aws/fetch_sts_source_identity.py already vendors, says where each carries it, "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" and "include source identity in the https://aws.amazon.com/source_identity namespace in the token", and what it needs, "trust policies for all roles connected to an identity provider (IdP) must have the sts:SetSourceIdentity permission". The fetcher now copies those sentences too, and regen_iam_authorized_actions.py gives each operation a companion naming the attribute or the claim and the key it is read as, where the reference's action of the operation's name takes sts:SourceIdentity. STS reads the value the table names, holds it to botocore's sourceIdentityType (the shape botocore gives the response's SourceIdentity, as it does AssumeRole's member), and refuses several values or one outside it with InvalidIdentityToken. The trust policy reads it as sts:SourceIdentity, through the keys the federated trust evaluators already read in full, and is checked for sts:SetSourceIdentity before the assume, as for AssumeRole. The session records it under aws:SourceIdentity, which a session assumed from it carries on, and the response returns it. Part of #6640.
90 lines
6.4 KiB
Python
90 lines
6.4 KiB
Python
#!/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 = ("<h2>Permissions required to set source identity</h2><ul><li><p>To specify a source identity, "
|
||
"principals (IAM users and roles) must have permissions to <code>sts:SetSourceIdentity</code>. As the "
|
||
"administrator, you can configure this.</p></li><li><p>When you assume a role with another role, called "
|
||
"<a>role chaining</a>, permissions for <code>sts:SetSourceIdentity</code> are required in both the "
|
||
"permissions policy of the principal who is assuming the role and in the role trust policy of the "
|
||
"target role. Otherwise, the assume role operation will fail.</p></li></ul>")
|
||
THINGS = ("<h2>Things to know about source identity</h2><p>Trust policies for all roles connected to an identity "
|
||
"provider (IdP) must have the <code>sts:SetSourceIdentity</code> permission.</p><p>The value that is set "
|
||
"persists when a role is used to "
|
||
"assume another role through the AWS CLI or AWS API, known as <a>role chaining</a>. The value that is set "
|
||
"cannot be changed during the role session.</p><p>When an identity sets a source identity, the "
|
||
"<code>sts:SourceIdentity</code> key is present in the request. For subsequent actions taken during the "
|
||
"role session, the <code>aws:SourceIdentity</code> key is present in the request.</p>")
|
||
ASSUME_ROLE = ("<h2>Using source identity with AssumeRole</h2><p>To pass source identity while assuming a role, use "
|
||
"the <code>-–source-identity</code> AWS CLI option or the <code>SourceIdentity</code> AWS API "
|
||
"parameter.</p>")
|
||
SAML = ("<h2>Using source identity with AssumeRoleWithSAML</h2><p>To set a SAML attribute for source identity, include "
|
||
"the <code>Attribute</code> element with the <code>Name</code> attribute set to "
|
||
"<code>https://aws.amazon.com/SAML/Attributes/SourceIdentity</code>. Use the <code>AttributeValue</code> "
|
||
"element.</p>")
|
||
WEB_IDENTITY = ("<h2>Using source identity with AssumeRoleWithWebIdentity</h2><p>Include source identity in the "
|
||
'<code class="code"><a href="https://aws.amazon.com/"><span>https://aws.amazon.com/</span></a>'
|
||
"source_identity</code> namespace in the token when you submit the request.</p>")
|
||
OTHER = "<h2>Using source identity with AssumeRoot</h2><p>Nothing to read here.</p>"
|
||
CHAINED = ("<h2>Control access using source identity information</h2><p>Therefore, both the "
|
||
"<code>aws:SourceIdentity</code> and <code>sts:SourceIdentity</code> keys are present in the request "
|
||
"context.</p>")
|
||
|
||
|
||
def page(*parts: str) -> str:
|
||
sections = parts or (THINGS, PERMISSIONS, ASSUME_ROLE, SAML, WEB_IDENTITY, OTHER, CHAINED)
|
||
return f'<div id="main-col-body"><h1>Monitor</h1>{"".join(sections)}</div><div class="prev-next"></div>'
|
||
|
||
|
||
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 <code>sts:SetSourceIdentity",
|
||
"permissions for <code>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 <code>sts:SetSourceIdentity</code> permission",
|
||
"must have the <code>sts:Other</code> 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("<code>aws:SourceIdentity", "<code>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("<code>SourceIdentity", "<code>Identity"),
|
||
CHAINED))
|