fix(stepfunctions): check EMR Serverless tasks as the same request made directly (#8322)

The EMR Serverless task arm wrote its action ("emr-serverless:" plus the API
name) and its ARNs (/applications/<id>, /jobruns/<id>) by hand, and skipped
the checks a direct request gets beside its own action: iam:PassRole on
StartJobRun's execution role, and emr-serverless:TagResource on the tags
CreateApplication and StartJobRun send.

A new RequestChecks reads a JSON request as the enforcement filter reads the
same request made directly: the action from IamAuthorizedActions, its
resources from RequestResources.ofBody, the request's tags on the action where
the tag-on-create table says it takes them, iam:PassRole on each role the
passed-roles table reads with iam:PassedToService, and the tagging action on
what a create makes. PassedRoles, TagOnCreate and RequestResources.created
gain a body form for it. The arm now hands RequestChecks the request the
integration sends EMR Serverless, so a regeneration of any of those tables
moves the task and the direct call together.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-14 12:42:27 -04:00
1 parent e2712cf344
commit 97fcbf21d0
10 files changed
+338 -37

No files matched your search

@@ -0,0 +1,63 @@
package io.floci.aws.core.common;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
/** A JSON request is checked as the enforcement filter checks the same request made directly. */
class RequestChecksTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final String APPLICATION = "arn:aws:emr-serverless:us-east-1:000000000000:/applications/a";
private static List<RequestChecks.Check> checks(String operation, String body) throws Exception {
return RequestChecks.of(operation, MAPPER.readTree(body), "aws", "us-east-1", "000000000000", null);
}
@Test
void anOperationIsCheckedOnTheResourcesItsRequestNames() throws Exception {
assertEquals(List.of(new RequestChecks.Check("emr-serverless:StartApplication", APPLICATION, Map.of())),
checks("emr-serverless:StartApplication", "{\"applicationId\":\"a\"}"));
assertEquals(List.of(new RequestChecks.Check("emr-serverless:CreateApplication", "*", Map.of())),
checks("emr-serverless:CreateApplication", "{}"));
}
/** A request naming none of the resources its operation acts on is checked on {@code *}, as the filter does. */
@Test
void aRequestNamingNoneOfItsResourcesIsCheckedOnAnyResource() throws Exception {
assertEquals(List.of(new RequestChecks.Check("emr-serverless:StartApplication", "*", Map.of())),
checks("emr-serverless:StartApplication", "{}"));
}
@Test
void aJobRunsRoleAndTagsAreCheckedBesideItsAction() throws Exception {
String role = "arn:aws:iam::000000000000:role/job";
Map<String, List<String>> tagged = Map.of("aws:RequestTag/team", List.of("data"), "aws:TagKeys", List.of("team"));
assertEquals(List.of(new RequestChecks.Check("emr-serverless:StartJobRun", APPLICATION, tagged),
new RequestChecks.Check("iam:PassRole", role,
Map.of("iam:PassedToService", List.of("emr-serverless.amazonaws.com"))),
new RequestChecks.Check("emr-serverless:TagResource", APPLICATION, tagged)),
checks("emr-serverless:StartJobRun",
"{\"applicationId\":\"a\",\"executionRoleArn\":\"" + role + "\",\"tags\":{\"team\":\"data\"}}"));
}
/** An operation whose passed roles are read by hand, from a request of its own, is refused rather than misread. */
@Test
void anOperationPassingRolesReadByHandIsNotReadFromABody() {
assertThrows(IllegalArgumentException.class,
() -> checks("states:CreateStateMachine", "{\"name\":\"m\",\"roleArn\":\"arn:aws:iam::000000000000:role/r\"}"));
}
/** An operation the request-resource table does not read needs nothing here. */
@Test
void anOperationTheTableDoesNotReadNeedsNothing() throws Exception {
JsonNode body = MAPPER.readTree("{}");
assertEquals(List.of(), RequestChecks.of("nothing:DoesIt", body, "aws", "us-east-1", "000000000000", null));
}
}
@@ -215,6 +215,51 @@ class StepFunctionsJobTaskIamIntegrationTest {
target.notReached().check();
}
/**
* A job run passes its execution role to EMR Serverless as a direct {@code StartJobRun} does, so the role the task
* runs as needs {@code iam:PassRole} on it beside the action on the application.
*/
@Test
void aJobRunNeedsPassRoleOnItsExecutionRole() throws Exception {
Target target = startJobRunTarget();
String role = role(STATES_TRUST, allow(target.resourceArn(), target.actions()));
Response refused = terminal(start(machine(jsonPathTask(target, null), role), "{}"));
assertFailed(refused, target.error());
String cause = refused.jsonPath().getString("cause");
assertTrue(cause.contains("iam:PassRole") && cause.contains(target.arguments().get("ExecutionRoleArn").toString()),
cause);
target.notReached().check();
}
/**
* Tags an application is created with need {@code emr-serverless:TagResource} on the application it makes, as a
* direct {@code CreateApplication} does, and the create itself is checked with them.
*/
@Test
void tagsAnApplicationIsCreatedWithNeedTagResource() throws Exception {
Target untagged = createApplicationTarget();
Map<String, Object> arguments = new LinkedHashMap<>(untagged.arguments());
arguments.put("Tags", Map.of("team", "data"));
Target target = new Target(untagged.integration(), untagged.error(), untagged.resourceArn(), arguments,
untagged.reached(), untagged.notReached(), untagged.actions());
Response refused = terminal(start(machine(jsonPathTask(target, null), role(STATES_TRUST, target.grant())), "{}"));
assertFailed(refused, target.error());
assertTrue(refused.jsonPath().getString("cause").contains("emr-serverless:TagResource"), refused.asString());
target.notReached().check();
Response created = terminal(start(machine(jsonPathTask(target, null), role(STATES_TRUST, policy(List.of(
Map.of("Effect", "Allow", "Action", List.of("emr-serverless:CreateApplication"), "Resource", List.of("*"),
"Condition", Map.of("StringEquals", Map.of("aws:RequestTag/team", "data"))),
Map.of("Effect", "Allow", "Action", List.of("emr-serverless:TagResource"),
"Resource", List.of("arn:aws:emr-serverless:" + REGION + ":" + ACCOUNT + ":/applications/*")))))),
"{}"));
assertEquals("SUCCEEDED", created.jsonPath().getString("status"), created.asString());
target.reached().check();
}
@ParameterizedTest
@MethodSource("eachIntegration")
void aCredentialsRoleNotAllowedTheActionIsRefusedEvenIfTheExecutionRoleIs(String integration) throws Exception {
@@ -530,8 +575,13 @@ class StepFunctionsJobTaskIamIntegrationTest {
return actions[0];
}
/** The actions on the resource, and {@code iam:PassRole} on the execution role a request passes, if any. */
String grant() {
return allow(resourceArn, actions);
Object role = arguments.get("ExecutionRoleArn");
Map<String, Object> onResource = Map.of("Effect", "Allow", "Action", List.of(actions),
"Resource", List.of(resourceArn));
return policy(role == null ? List.of(onResource) : List.of(onResource, Map.of("Effect", "Allow",
"Action", List.of("iam:PassRole"), "Resource", List.of(role.toString()))));
}
}
@@ -587,6 +587,37 @@ class TaskPermissionsTest {
.orElseThrow().permissions());
}
/** A job run passes its execution role to EMR Serverless, as a direct {@code StartJobRun} does. */
@Test
void aJobRunNeedsPassRoleOnItsExecutionRole() throws Exception {
String role = "arn:aws:iam::111122223333:role/job";
assertEquals(List.of(new Permission("emr-serverless:StartJobRun", APPLICATION),
new Permission("iam:PassRole", role,
Map.of("iam:PassedToService", List.of("emr-serverless.amazonaws.com")))),
TaskPermissions.of(integration("emr-serverless:startJobRun"),
json("{\"ApplicationId\":\"a\",\"ExecutionRoleArn\":\"" + role + "\"}"), machine("aws"))
.orElseThrow().permissions());
}
/**
* Tags a create or a job run sends need {@code emr-serverless:TagResource}, on the application a create makes or
* the one a job run is checked on, as a direct request's do, and the call itself is checked with them.
*/
@Test
void tagsACreateOrAJobRunSendsNeedTagResource() throws Exception {
Map<String, List<String>> tagged = Map.of("aws:RequestTag/team", List.of("data"), "aws:TagKeys", List.of("team"));
assertEquals(List.of(new Permission("emr-serverless:CreateApplication", "*", tagged),
new Permission("emr-serverless:TagResource",
"arn:aws:emr-serverless:eu-west-1:111122223333:/applications/*", tagged)),
TaskPermissions.of(integration("emr-serverless:createApplication"),
json("{\"Tags\":{\"team\":\"data\"}}"), machine("aws")).orElseThrow().permissions());
assertEquals(List.of(new Permission("emr-serverless:StartJobRun", APPLICATION, tagged),
new Permission("emr-serverless:TagResource", APPLICATION, tagged)),
TaskPermissions.of(integration("emr-serverless:startJobRun"),
json("{\"ApplicationId\":\"a\",\"Tags\":{\"team\":\"data\"}}"), machine("aws"))
.orElseThrow().permissions());
}
private static TaskPermissions ecsPermissions(String tail, String request, EcsService ecs) throws Exception {
return TaskPermissions.of(integration(tail), json(request), machine("aws"), null, ecs, false).orElseThrow();
}