mirror of
https://github.com/floci-io/floci.git
synced 2026-11-29 00:21:51 -05:00
Under enforcement a REST route no vendored model names runs unchecked, and AWS Batch was among them: a caller could create a compute environment with any service, instance or Spot Fleet role and register a job definition with any job or execution role, as #6255 reports. Batch's model routes are vendored, so each request is checked as its operation's action, and Batch joins the table of request resources and the passed-roles table. The passed-roles generator reads RegisterJobDefinition's roles, each passed to ecs-tasks.amazonaws.com, alone. The Service Reference passes CreateComputeEnvironment's and UpdateComputeEnvironment's roles to batch, ec2, ecs and spotfleet without saying which member's goes where, so a vendored source, tools/aws/sources/batch-roles.json, written by tools/aws/fetch_batch_roles.py from the Batch API reference and user guide, gives each: serviceRole is trusted by batch.amazonaws.com, a compute resource's instanceRole is an instance profile whose role is trusted by ec2.amazonaws.com, spotIamFleetRole by spotfleet.amazonaws.com, and a managed instances provider's infrastructureRoleArn needs "iam:PassedToService: ecs.amazonaws.com", the update's naming the same role in the same words. SubmitServiceJob, which Floci does not serve, names its role inside the SageMaker request it queues, a string the table does not read. The request-resources generator reads a type of two variables from a member whose documentation spells it, as it does ECS's "(family:revision)", now also where a variable shares only some of its words with the type's name, so DeregisterJobDefinition's "(name:revision)" names a job definition revision whole, not only its name, and where a member lists the form among those it takes, as SubmitJob's "definition-name, definition-name:revision, or the ARN" does, naming the revision rather than the job definition its name alone fills: "the request ARN includes the revision". BatchStoredResourceArns resolves it as Batch does, a name alone being the latest active revision a job runs, and answers the tags of what Batch stores. A member of a structure documented as "The Amazon Resource Name (ARN) of the" resource is read as one, so a job queue's computeEnvironmentOrder[].computeEnvironment is checked. No other namespace's reading changes. A role name a path prefixes, which Batch documents ("if a role with the name bar has a path of /foo/, specify /foo/bar as the role name"), is checked on the role IAM holds under that name and path. Batch's tag routes are served, as every namespace's the table reads, on the route its model gives, /v1/tags/{resourceArn} with the ARN encoded as an SDK sends it. Its tag-on-create reading waits: CreateComputeEnvironment is checked as batch:TagResource or batch:SetCapacityTags, and nothing says which of its tag members each is for. Part of #6255.
121 lines
7.9 KiB
Python
121 lines
7.9 KiB
Python
#!/usr/bin/env python3
|
|
"""Tests for fetch_batch_roles.py, on pages in the AWS Batch API reference's and user guide's markup."""
|
|
from __future__ import annotations
|
|
|
|
import pytest
|
|
|
|
import fetch_batch_roles as f
|
|
|
|
API = f.API_REFERENCE
|
|
GUIDE = f.USER_GUIDE
|
|
|
|
|
|
def entry(anchor: str, member: str, description: str) -> str:
|
|
return (f'<dl><dt id="{anchor}"><b><code class="code">{member}</code></b></dt><dd><p>{description}</p>'
|
|
f'<p>Type: String</p></dd></dl>')
|
|
|
|
|
|
def see(page: str, name: str) -> str:
|
|
return f'For more information, see <a href="{GUIDE}{page}">{name}</a> in the <i>AWS Batch User Guide</i>.'
|
|
|
|
|
|
def trust(service: str) -> str:
|
|
return ('<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": [ { "Effect": "Allow", "Principal": { "Service": "' + service + '" }, '
|
|
'"Action": "sts:AssumeRole" } ] }</code></pre>')
|
|
|
|
|
|
def guide(title: str, content: str) -> str:
|
|
return f'<title>{title}</title><div id="main-col-body">{content}</div><div class="prev-next"></div>'
|
|
|
|
|
|
SERVICE_ROLE = ("The full Amazon Resource Name (ARN) of the IAM role that allows AWS Batch to make calls to other AWS "
|
|
"services on your behalf. " + see("service_IAM_role.html", "AWS Batch service IAM role"))
|
|
INSTANCE_ROLE = ("The Amazon ECS instance profile applied to Amazon EC2 instances in a compute environment. You can "
|
|
"specify the short name or full Amazon Resource Name (ARN) of an instance profile. "
|
|
+ see("instance_IAM_role.html", "Amazon ECS instance role"))
|
|
PAGES = {
|
|
API + "API_CreateComputeEnvironment.html": entry("Batch-CreateComputeEnvironment-request-serviceRole",
|
|
"serviceRole", SERVICE_ROLE),
|
|
API + "API_UpdateComputeEnvironment.html": entry(
|
|
"Batch-UpdateComputeEnvironment-request-serviceRole", "serviceRole",
|
|
SERVICE_ROLE + " " + see("updating-compute-environments.html", "Updating compute environments")),
|
|
API + "API_ComputeResource.html": (
|
|
entry("Batch-Type-ComputeResource-instanceRole", "instanceRole", INSTANCE_ROLE)
|
|
+ entry("Batch-Type-ComputeResource-spotIamFleetRole", "spotIamFleetRole",
|
|
"The Amazon Resource Name (ARN) of the Amazon EC2 Spot Fleet IAM role applied to a SPOT compute "
|
|
"environment. " + see("spot_fleet_IAM_role.html", "Amazon EC2 spot fleet role"))),
|
|
API + "API_ComputeResourceUpdate.html": entry("Batch-Type-ComputeResourceUpdate-instanceRole", "instanceRole",
|
|
INSTANCE_ROLE),
|
|
API + "API_ManagedInstancesProvider.html": entry(
|
|
"Batch-Type-ManagedInstancesProvider-infrastructureRoleArn", "infrastructureRoleArn",
|
|
"The Amazon Resource Name (ARN) of the IAM role that Amazon ECS assumes. You must have the iam:PassRole "
|
|
"permission for this role with the condition iam:PassedToService: ecs.amazonaws.com ."),
|
|
API + "API_UpdateManagedInstancesProviderConfiguration.html": entry(
|
|
"Batch-Type-UpdateManagedInstancesProviderConfiguration-infrastructureRoleArn", "infrastructureRoleArn",
|
|
"The updated Amazon Resource Name (ARN) of the IAM role that Amazon ECS assumes."),
|
|
# The service role's page lists the service-linked roles' pages, each saying which services it trusts.
|
|
GUIDE + "service_IAM_role.html": guide("Using service-linked roles for AWS Batch",
|
|
'<a href="./using-service-linked-roles-batch-general.html">Using roles</a>'),
|
|
GUIDE + "using-service-linked-roles-batch-general.html": guide(
|
|
"Using roles for AWS Batch", "<p>The AWSServiceRoleForBatch service-linked role trusts the following services "
|
|
"to assume the role: batch.amazonaws.com</p>"),
|
|
# The instance role's page lists the page holding its trust policy.
|
|
GUIDE + "instance_IAM_role.html": guide("Amazon ECS instance role",
|
|
'<a href="./batch-check-ecsinstancerole.html">Check your role</a>'),
|
|
GUIDE + "batch-check-ecsinstancerole.html": guide("Check your account's Amazon ECS instance role",
|
|
trust("ec2.amazonaws.com")),
|
|
# The spot fleet role's trust policy is the argument of the CLI command that creates it.
|
|
GUIDE + "spot_fleet_IAM_role.html": guide("Amazon EC2 spot fleet role",
|
|
'<a href="./spot-fleet-roles-cli.html">With the AWS CLI</a>'),
|
|
GUIDE + "spot-fleet-roles-cli.html": guide(
|
|
"Create Amazon EC2 spot fleet roles with the AWS CLI",
|
|
"<pre>$ aws iam create-role --role-name AmazonEC2SpotFleetTaggingRole --assume-role-policy-document '{ "
|
|
'"Version":"2012-10-17", "Statement": [ { "Effect": "Allow", "Principal": { "Service": '
|
|
"\"spotfleet.amazonaws.com\" }, \"Action\": \"sts:AssumeRole\" } ] }'</pre>"),
|
|
}
|
|
|
|
|
|
def test_each_member_is_read_with_the_service_its_role_is_for():
|
|
built = f.build(lambda url: PAGES[url])
|
|
by_member = {(m.get("operation") or m["type"], m["member"]): m for m in built["members"]}
|
|
assert by_member[("CreateComputeEnvironment", "serviceRole")]["trustedBy"] == ["batch.amazonaws.com"]
|
|
description = by_member[("CreateComputeEnvironment", "serviceRole")]["description"]
|
|
assert description.startswith("The full Amazon Resource Name (ARN) of the IAM role that allows AWS Batch")
|
|
assert "see AWS Batch service IAM role in the AWS Batch User Guide" in description
|
|
# The later link names what the update depends on, not the role.
|
|
assert by_member[("UpdateComputeEnvironment", "serviceRole")]["rolePage"] == GUIDE + "service_IAM_role.html"
|
|
instance = by_member[("ComputeResource", "instanceRole")]
|
|
assert (instance["trustedBy"], instance["instanceProfile"]) == (["ec2.amazonaws.com"], True)
|
|
assert instance["trustPages"] == [GUIDE + "batch-check-ecsinstancerole.html"]
|
|
assert by_member[("ComputeResource", "spotIamFleetRole")]["trustedBy"] == ["spotfleet.amazonaws.com"]
|
|
assert "instanceProfile" not in by_member[("ComputeResource", "spotIamFleetRole")]
|
|
assert by_member[("ManagedInstancesProvider", "infrastructureRoleArn")]["passedToService"] == ["ecs.amazonaws.com"]
|
|
assert "rolePage" not in by_member[("ManagedInstancesProvider", "infrastructureRoleArn")]
|
|
# The update names the provider's role in the same words, so its role is that one.
|
|
update = by_member[("UpdateManagedInstancesProviderConfiguration", "infrastructureRoleArn")]
|
|
assert update["passedToService"] == ["ecs.amazonaws.com"]
|
|
assert update["sameRoleAs"] == {"type": "ManagedInstancesProvider", "member": "infrastructureRoleArn"}
|
|
|
|
|
|
def test_a_page_whose_layout_changed_is_refused():
|
|
for url, page, message in (
|
|
(API + "API_ComputeResource.html", "<dl></dl>", "has no entry"),
|
|
(GUIDE + "batch-check-ecsinstancerole.html", guide("Check", "<p>No policy.</p>"), "pages of a role"),
|
|
(API + "API_CreateComputeEnvironment.html",
|
|
entry("Batch-CreateComputeEnvironment-request-serviceRole", "serviceRole", "A role."), "pages of a role"),
|
|
(API + "API_UpdateManagedInstancesProviderConfiguration.html",
|
|
entry("Batch-Type-UpdateManagedInstancesProviderConfiguration-infrastructureRoleArn",
|
|
"infrastructureRoleArn", "The updated ARN of some other role."), "no longer names")):
|
|
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, **{GUIDE + "batch-check-ecsinstancerole.html": guide(
|
|
"Check your account's Amazon ECS instance role", trust("ecs-tasks.amazonaws.com"))}))
|