Files
floci/tools/aws/test_fetch_emr_roles.py
T
Priamo Germosen 1a91dbda76 fix(iam): check iam:PassRole on the roles an EMR RunJobFlow request names (#9587)
AWS's Service Reference lists iam:PassRole for RunJobFlow, passing its
roles to application-autoscaling.amazonaws.com, ec2.amazonaws.com and
elasticmapreduce.amazonaws.com without saying which member's role goes
where, and JobFlowRole names an instance profile rather than a role.
The passed-roles table left the operation unread, so a caller allowed
elasticmapreduce:RunJobFlow could launch a cluster with any service role
and any instance profile.

A new vendored source, tools/aws/sources/emr-roles.json, written by its
own script from the EMR API reference, management guide and AWS CLI
reference, gives each member's role. The guide's page of each role names
its default role ("The default role name is EMR_EC2_DefaultRole"), and
the full access policy it gives users passes each default role to one
service ("iam:PassedToService": "ec2.amazonaws.com*"). The API reference
calls JobFlowRole an instance profile ("Also called instance profile and
Amazon EC2 role"), and the CLI reference's create-cluster examples give
the roles by name (--service-role EMR_DefaultRole). The steps' runtime
role, StepExecutionRoleArn, is not passed: "You can control access to
the runtime role with the condition key elasticmapreduce:ExecutionRoleArn."

The generator reads the source through ROLE_SOURCES, as it reads EKS's,
now taking a documented iam:PassedToService over a trust policy, a member
by name or as an instance profile where the source says so, and leaving
out a member whose role a condition key controls. RunJobFlow moves out of
notRead: ServiceRole to elasticmapreduce.amazonaws.com and AutoScalingRole
to application-autoscaling.amazonaws.com, each by name or ARN, and the
roles of the instance profile JobFlowRole names, by name or ARN, to
ec2.amazonaws.com, found with the instance-profile lookup RunInstances
already uses. A profile IAM does not hold passes no role, as Floci's EMR
already reads JobFlowRole. An instance-profile member is not read for a
call with no request of its own, which has no such lookup.

The instance profile is now found one way everywhere it is used
(IamService.instanceProfileNamed): by its name, or by its own ARN, path
and account included. IAM's passed-role check, EMR's step role and an
EC2 instance's credentials each read it their own way, and EMR kept only
the name after an ARN's last slash. A JobFlowRole ARN of another account
or path then found no role to check but ran the steps as the local
profile's role.

A member's JSON value is now read as the text a handler reads it as, a
number or a Boolean included, as the request resources and condition
keys already read a body's values. The passed-roles reader took strings
alone, while EMR takes "JobFlowRole":123456 as the profile 123456, so a
role named by a number went unchecked.
2026-11-27 08:11:04 -04:00

93 lines
5.5 KiB
Python

#!/usr/bin/env python3
"""Tests for fetch_emr_roles.py, on pages in the Amazon EMR API reference's, management guide's and AWS CLI
reference's markup."""
from __future__ import annotations
import pytest
import fetch_emr_roles as f
def entry(member: str, description: str) -> str:
return (f'<dt id="EMR-RunJobFlow-request-{member}"><a id="EMR-RunJobFlow-request-{member}"></a>'
f'<b><code class="code">{member}</code></b></dt><dd><p>{description}</p><p>Type: String</p></dd>')
def role_page(title: str, role: str) -> str:
return f"<title>{title}</title><p>The default role name is <code class=\"code\">{role}</code>.</p>"
POLICY = ('<pre class="programlisting"><div class="code-btn-container"><div class="btn-copy-code" title="Copy">'
'<awsui-icon name="copy"></awsui-icon></div></div><code class="json ">{"Version":"2012-10-17","Statement":['
'{"Sid":"PassRoleForElasticMapReduce","Effect":"Allow","Action":"iam:PassRole",'
'"Resource":["arn:aws:iam::*:role/EMR_DefaultRole_V2"],'
'"Condition":{"StringLike":{"iam:PassedToService":"elasticmapreduce.amazonaws.com*"}}},'
'{"Sid":"PassRoleForEC2","Effect":"Allow","Action":"iam:PassRole",'
'"Resource":["arn:aws:iam::*:role/EMR_EC2_DefaultRole"],'
'"Condition":{"StringLike":{"iam:PassedToService":"ec2.amazonaws.com*"}}},'
'{"Sid":"PassRoleForAutoScaling","Effect":"Allow","Action":"iam:PassRole",'
'"Resource":["arn:aws:iam::*:role/EMR_AutoScaling_DefaultRole"],'
'"Condition":{"StringLike":{"iam:PassedToService":"application-autoscaling.amazonaws.com*"}}}]}'
'</code></pre>')
CLI = ('<pre>aws emr create-cluster [--service-role &lt;value&gt;]</pre><p>InstanceProfile=string</p>'
'<div class="section" id="examples"><h2>Examples</h2>'
'<pre>aws emr create-cluster --service-role EMR_DefaultRole --ec2-attributes '
'InstanceProfile=EMR_EC2_DefaultRole --auto-scaling-role EMR_AutoScaling_DefaultRole</pre></div>')
PAGES = {
f.API_PAGE: (
entry("ServiceRole", "The IAM role that Amazon EMR assumes in order to access AWS resources on your behalf.")
+ entry("JobFlowRole", "Also called instance profile and Amazon EC2 role. The default role is "
"<code class=\"code\">EMR_EC2_DefaultRole</code>.")
+ entry("AutoScalingRole", "An IAM role for automatic scaling policies. The default role is "
"<code class=\"code\">EMR_AutoScaling_DefaultRole</code>.")
+ entry("StepExecutionRoleArn", "The runtime role can be a cross-account IAM role.")),
f.POLICY_PAGE: POLICY,
f.CLI_PAGE: CLI,
f.GUIDE + "emr-iam-role.html": role_page("Service role for Amazon EMR (EMR role)", "EMR_DefaultRole_V2"),
f.GUIDE + "emr-iam-role-for-ec2.html": role_page("Service role for cluster EC2 instances (EC2 instance profile)",
"EMR_EC2_DefaultRole"),
f.GUIDE + "emr-iam-role-automatic-scaling.html": role_page("Service role for automatic scaling in Amazon EMR",
"EMR_AutoScaling_DefaultRole"),
f.RUNTIME_ROLES_PAGE: "<p>You can control access to the runtime role with the condition key "
"<code class=\"code\">elasticmapreduce:ExecutionRoleArn</code>.</p>",
}
def by_member(built: dict) -> dict:
return {member["member"]: member for member in built["members"]}
def test_each_member_gives_its_default_roles_service_and_how_it_names_its_role():
members = by_member(f.build(lambda url: PAGES[url]))
assert members["ServiceRole"]["defaultRole"] == "EMR_DefaultRole_V2"
assert members["ServiceRole"]["passedToService"] == ["elasticmapreduce.amazonaws.com"]
assert members["ServiceRole"]["byName"] is True and members["ServiceRole"]["cliExample"] == "EMR_DefaultRole"
assert members["JobFlowRole"]["passedToService"] == ["ec2.amazonaws.com"]
assert members["JobFlowRole"]["instanceProfile"] is True and "byName" not in members["JobFlowRole"]
assert members["JobFlowRole"]["cliExample"] == "EMR_EC2_DefaultRole"
assert members["AutoScalingRole"]["passedToService"] == ["application-autoscaling.amazonaws.com"]
assert members["StepExecutionRoleArn"]["accessControlledBy"] == "elasticmapreduce:ExecutionRoleArn"
assert "passedToService" not in members["StepExecutionRoleArn"]
def test_a_description_naming_another_default_role_than_its_page_is_refused():
changed = dict(PAGES, **{f.GUIDE + "emr-iam-role-for-ec2.html": role_page("EC2", "EMR_EC2_OtherRole")})
with pytest.raises(ValueError, match="names EMR_EC2_DefaultRole"):
f.build(lambda url: changed[url])
def test_a_page_whose_layout_changed_is_refused():
for url, page, message in ((f.POLICY_PAGE, "<p>No policy.</p>", "passes no default role"),
(f.RUNTIME_ROLES_PAGE, "<p>Nothing.</p>", "names no condition key"),
(f.CLI_PAGE, "<pre>aws emr create-cluster</pre>", "has no examples"),
(f.API_PAGE, "<dl></dl>", "has no entry")):
changed = dict(PAGES, **{url: page})
with pytest.raises(ValueError, match=message):
f.build(lambda u: changed[u])
def test_the_file_is_stale_when_a_page_says_something_else():
vendored = f.build(lambda url: PAGES[url])
assert not f.stale(vendored, PAGES)
assert f.stale(vendored, dict(PAGES, **{f.POLICY_PAGE: POLICY.replace("ec2.amazonaws.com", "eks.amazonaws.com")}))