mirror of
https://github.com/floci-io/floci.git
synced 2026-12-06 11:38:06 -05:00
A role's trust policy read a web identity token as two keys Floci built by hand, <issuer>:sub and <issuer>:aud, the latter always from the aud claim. The IAM User Guide's Default mapping, for every IdP it gives no tab of its own, also gives amr, email and oaud, and reads aud from azp where the token sets one; its page on creating an OIDC provider says AWS STS uses azp as the audience the provider's client IDs are checked against. A fetcher vendors the Default tab's rows and the provider page's azp sentence. The authorized-actions generator writes them as webIdentityClaims, and AssumeRoleWithWebIdentity reads the trust policy's keys and the token's audience from that section.
93 lines
5.4 KiB
Python
93 lines
5.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Tests for fetch_sts_oidc_claim_keys.py, on pages in the IAM User Guide's markup."""
|
|
from __future__ import annotations
|
|
|
|
import json
|
|
|
|
import pytest
|
|
|
|
import fetch_sts_oidc_claim_keys as f
|
|
|
|
APPLIES = "Use this mapping if your IdP is not listed in the tab options."
|
|
OTHERWISE = ('If no value is set for <code class="code">azp</code>, the <code class="code">aud</code>\n'
|
|
' condition key maps to the <code class="code">aud</code> claim.')
|
|
AUDIENCE = ('If your OIDC identity provider is setting both <code class="code">aud</code> and <code class="code">azp'
|
|
'</code>\n claims in the token, AWS STS will use the value in the <code class="code">azp</code> claim as '
|
|
'the\n <code class="code">aud</code> claim.')
|
|
HEADER = "<th>AWS STS condition key</th><th>IdP JWT claim</th><th>Available in session</th>"
|
|
ROWS = [("amr", "<p>amr</p>", "Yes"), ("aud", f"<p>azp</p><p>{OTHERWISE}</p>", "Yes"), ("sub", "<p>sub</p>", "Yes")]
|
|
|
|
|
|
def row(key: str, claim: str, session: str) -> str:
|
|
return (f'<tr><td tabindex="-1"><p>{key}</p></td><td tabindex="-1">{claim}</td>'
|
|
f'<td tabindex="-1"><p>{session}</p></td></tr>')
|
|
|
|
|
|
def keys_page(applies: str = APPLIES, header: str = HEADER, rows=ROWS, tab: str = "default",
|
|
section: str = "condition-keys-wif") -> str:
|
|
body = "".join(row(*each) for each in rows)
|
|
return (f'<h2 id="{section}">Available keys for AWS OIDC federation</h2><awsdocs-tabs><dl>'
|
|
f'<dt>Default</dt><dd tab-id="{tab}"><p>Default lists the standard OIDC claims. {applies} </p>'
|
|
f'<table id="d0e1"><thead><tr>{header}</tr></thead>{body}</table>'
|
|
# A code example's own tabs, nested in the Default tab, which do not end it.
|
|
f'<awsdocs-tabs><dl><dt>JSON</dt><dd tab-id="json"><pre>{{}}</pre></dd></dl></awsdocs-tabs>'
|
|
f'<p>More on the keys.</p></dd>'
|
|
f'<dt>AWS STS</dt><dd tab-id="aws-sts"><p>Use this mapping if your IdP is not listed in the tab options.'
|
|
f'</p><table><thead><tr>{HEADER}</tr></thead>{row("jti", "<p>jti</p>", "No")}</table></dd></dl>'
|
|
f'</awsdocs-tabs><h2 id="condition-keys-wif-more-info">More information</h2>')
|
|
|
|
|
|
def provider_page(sentence: str = AUDIENCE) -> str:
|
|
return (f'<div class="awsdocs-note-text"><p>If your IdP JWT token includes the <code class="code">azp</code> '
|
|
f'claim, enter this value as the Audience value.</p><p>{sentence}</p></div>')
|
|
|
|
|
|
def test_the_default_tab_s_rows_and_the_audience_sentence_are_copied_as_the_pages_give_them():
|
|
built = f.build(keys_page(), provider_page())
|
|
assert built["default"] == {"sentence": APPLIES, "keys": [
|
|
{"key": "amr", "claim": "amr", "inSession": True},
|
|
{"key": "aud", "claim": "azp", "inSession": True, "otherwise": "aud",
|
|
"otherwiseSentence": "If no value is set for azp, the aud condition key maps to the aud claim."},
|
|
{"key": "sub", "claim": "sub", "inSession": True}]}
|
|
assert built["audience"] == {"claim": "aud", "preferred": "azp", "sentence": "If your OIDC identity provider is "
|
|
"setting both aud and azp claims in the token, AWS STS will use the value in the azp "
|
|
"claim as the aud claim."}
|
|
|
|
|
|
@pytest.mark.parametrize("arguments, problem", [
|
|
({"section": "condition-keys-oidc"}, "has no section condition-keys-wif"),
|
|
({"tab": "standard"}, "has no default tab"),
|
|
({"applies": "Use this mapping for any IdP."}, "which IdPs its default mapping is for"),
|
|
({"header": "<th>Key</th><th>Claim</th>"}, "heads its table"),
|
|
({"rows": [("amr", "<p>amr</p>", "Sometimes")]}, "not a key, a claim and Yes or No"),
|
|
({"rows": [("amr", "<p>amr</p><p>a</p><p>b</p>", "Yes")]}, "is not a key, its claim"),
|
|
({"rows": [("aud", f"<p>azp</p><p>{OTHERWISE.replace('maps to', 'is')}</p>", "Yes")]}, "no longer names"),
|
|
({"rows": [("oaud", f"<p>azp</p><p>{OTHERWISE}</p>", "No")]}, "no longer names"),
|
|
({"rows": [("sub", "<p>sub</p>", "Yes"), ("sub", "<p>aud</p>", "No")]}, "gives no keys, or one twice"),
|
|
])
|
|
def test_a_reworded_keys_page_is_refused(arguments, problem):
|
|
with pytest.raises(ValueError, match=problem):
|
|
f.build(keys_page(**arguments), provider_page())
|
|
|
|
|
|
def test_a_reworded_provider_page_is_refused():
|
|
with pytest.raises(ValueError, match="which claim AWS STS reads a token's audience from"):
|
|
f.build(keys_page(), provider_page(AUDIENCE.replace("azp</code> claim as", "aud</code> claim as")))
|
|
with pytest.raises(ValueError, match="which claim AWS STS reads a token's audience from"):
|
|
f.build(keys_page(), provider_page(AUDIENCE + "</p><p>" + AUDIENCE))
|
|
|
|
|
|
def test_stale_compares_everything_but_the_date():
|
|
built = f.build(keys_page(), provider_page())
|
|
assert not f.stale(dict(built, _source={"retrieved": "2000-01-01"}), keys_page(), provider_page())
|
|
assert f.stale(built, keys_page(rows=ROWS[:2]), provider_page())
|
|
|
|
|
|
def test_the_vendored_file_is_what_its_fetcher_writes():
|
|
vendored = json.loads(f.OUTPUT.read_text(encoding="utf-8"))
|
|
assert [(entry["key"], entry["claim"], entry.get("otherwise")) for entry in vendored["default"]["keys"]] == [
|
|
("amr", "amr", None), ("aud", "azp", "aud"), ("email", "email", None), ("oaud", "aud", None),
|
|
("sub", "sub", None)]
|
|
assert (vendored["audience"]["claim"], vendored["audience"]["preferred"]) == ("aud", "azp")
|
|
assert vendored["_source"]["fetcher"] == "tools/aws/fetch_sts_oidc_claim_keys.py"
|