feat(stepfunctions): evaluate the execution role on SNS and EventBridge tasks (#7622)

This commit is contained in:
Hector Ventura authored and GitHub committed 2026-11-06 08:34:39 -04:00
1 parent cdcb79a832
commit b1ba11c61e
16 files changed
+1255 -74

No files matched your search

@@ -60,6 +60,7 @@ class EventBridgePermissionIntegrationTest {
.statusCode(200)
.body("Policy", notNullValue())
.body("Policy", containsString("test-stmt"))
.body("Policy", containsString("\"Principal\":\"*\""))
.body("Policy", containsString("events:PutEvents"));
}
@@ -90,7 +91,7 @@ class EventBridgePermissionIntegrationTest {
.post("/")
.then()
.statusCode(200)
.body("Policy", containsString("123456789012"))
.body("Policy", containsString("\"Principal\":{\"AWS\":\"arn:aws:iam::123456789012:root\"}"))
.body("Policy", not(containsString("\"*\"")));
}
@@ -0,0 +1,53 @@
package io.github.hectorvent.floci.services.eventbridge;
import io.github.hectorvent.floci.services.iam.ResourcePolicyProvider.ResourcePolicy;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.List;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
class EventBridgeResourcePolicyProviderTest {
private static final String OWNER = "111122223333";
private static final String BUS = "arn:aws:events:us-east-1:" + OWNER + ":event-bus/orders";
private static final String POLICY = "{\"Version\":\"2012-10-17\",\"Statement\":[]}";
private EventBridgeService eventBridgeService;
private EventBridgeResourcePolicyProvider provider;
@BeforeEach
void setUp() {
eventBridgeService = mock(EventBridgeService.class);
when(eventBridgeService.findBusPolicy(anyString())).thenReturn(Optional.empty());
provider = new EventBridgeResourcePolicyProvider(eventBridgeService);
}
@Test
void aBusComesWithItsPolicyAndTheAccountInItsArn() {
when(eventBridgeService.findBusPolicy(BUS)).thenReturn(Optional.of(POLICY));
assertEquals(List.of(new ResourcePolicy(POLICY, OWNER)), provider.getResourcePolicies("events", BUS));
}
@Test
void aBusWithNoPolicyStillHasItsOwner() {
assertEquals(List.of(new ResourcePolicy(null, OWNER)), provider.getResourcePolicies("events", BUS));
}
@Test
void anythingThatIsNotOneBusIsLeftToOtherProviders() {
String events = "arn:aws:events:us-east-1:" + OWNER + ":";
assertTrue(provider.getResourcePolicies("events", "*").isEmpty());
assertTrue(provider.getResourcePolicies("events", events + "event-bus/*").isEmpty());
assertTrue(provider.getResourcePolicies("events", events + "rule/orders/r").isEmpty());
assertTrue(provider.getResourcePolicies("events", "arn:aws:events:::event-bus/orders").isEmpty());
assertTrue(provider.getResourcePolicies("sqs", "arn:aws:sqs:us-east-1:" + OWNER + ":orders").isEmpty());
}
}
@@ -418,7 +418,7 @@ class LambdaTaskAuthorizerTest {
roleWithPolicies(STATES_DESCRIBE_ANYTHING);
executionRole(STATES_TRUST);
authorizer.authorize(machine(), null, "Ec2.", List.of(new Permission("states:DescribeRegions", "*")));
authorizer.authorize(machine(), null, "Ec2.AccessDeniedException", List.of(new Permission("states:DescribeRegions", "*")));
}
@Test
@@ -429,7 +429,7 @@ class LambdaTaskAuthorizerTest {
executionRole(STATES_TRUST);
AslExecutor.FailStateException refused = assertThrows(AslExecutor.FailStateException.class,
() -> authorizer.authorize(machine(), null, "Ec2.",
() -> authorizer.authorize(machine(), null, "Ec2.AccessDeniedException",
List.of(new Permission("states:DescribeRegions", "*"))));
assertEquals("Ec2.AccessDeniedException", refused.error);
@@ -487,10 +487,10 @@ class LambdaTaskAuthorizerTest {
"Resource":"*","Condition":{"StringEquals":{"states:HTTPEndpoint":"https://example.test"}}}]}""");
executionRole(STATES_TRUST);
authorizer.authorize(machine(), null, "States.", List.of(new Permission("states:InvokeHTTPEndpoint", "*",
authorizer.authorize(machine(), null, "States.AccessDeniedException", List.of(new Permission("states:InvokeHTTPEndpoint", "*",
Map.of("states:HTTPEndpoint", List.of("https://example.test")))));
AslExecutor.FailStateException refused = assertThrows(AslExecutor.FailStateException.class,
() -> authorizer.authorize(machine(), null, "States.", List.of(new Permission(
() -> authorizer.authorize(machine(), null, "States.AccessDeniedException", List.of(new Permission(
"states:InvokeHTTPEndpoint", "*", Map.of("states:HTTPEndpoint", List.of("https://other.test"))))));
assertEquals("States.AccessDeniedException", refused.error);
}
@@ -503,13 +503,13 @@ class LambdaTaskAuthorizerTest {
"Condition":{"StringEquals":{"aws:RequestedRegion":"us-east-1"}}}]}""");
executionRole(STATES_TRUST);
authorizer.authorize(machine(), null, "S3.", List.of(new Permission("s3:PutObject", BUCKET)));
authorizer.authorize(machine(), null, "S3.AccessDeniedException", List.of(new Permission("s3:PutObject", BUCKET)));
}
@Test
void anExecutionRoleThatDoesNotExistRefusesTheTask() {
AslExecutor.FailStateException refused = assertThrows(AslExecutor.FailStateException.class,
() -> authorizer.authorize(machine(), null, "SQS.", List.of()));
() -> authorizer.authorize(machine(), null, "SQS.AccessDeniedException", List.of()));
assertEquals("States.Permissions", refused.error);
assertTrue(refused.cause.contains(ROLE), refused.cause);
@@ -521,7 +521,7 @@ class LambdaTaskAuthorizerTest {
executionRole(trust("{\"Service\":\"lambda.amazonaws.com\"}"));
AslExecutor.FailStateException refused = assertThrows(AslExecutor.FailStateException.class,
() -> authorizer.authorize(machine(), null, "S3.", List.of(new Permission("s3:PutObject", BUCKET))));
() -> authorizer.authorize(machine(), null, "S3.AccessDeniedException", List.of(new Permission("s3:PutObject", BUCKET))));
assertEquals("States.Permissions", refused.error);
assertTrue(refused.cause.contains(ROLE), refused.cause);
@@ -535,9 +535,9 @@ class LambdaTaskAuthorizerTest {
when(iamService.resolvePrincipalContext(ROLE)).thenReturn(CallerContext.of(List.of(ASSUME_ANY_ROLE)));
when(iamService.resolvePrincipalContext(OWN_TASK_ROLE)).thenReturn(CallerContext.of(List.of(PUT_OBJECTS)));
authorizer.authorize(machine(), OWN_TASK_ROLE, "S3.", List.of(new Permission("s3:PutObject", BUCKET)));
authorizer.authorize(machine(), OWN_TASK_ROLE, "S3.AccessDeniedException", List.of(new Permission("s3:PutObject", BUCKET)));
AslExecutor.FailStateException refused = assertThrows(AslExecutor.FailStateException.class,
() -> authorizer.authorize(machine(), OWN_TASK_ROLE, "S3.", List.of(
() -> authorizer.authorize(machine(), OWN_TASK_ROLE, "S3.AccessDeniedException", List.of(
new Permission("s3:PutObject", BUCKET), new Permission("s3:GetObject", BUCKET))));
assertEquals("S3.AccessDeniedException", refused.error);
@@ -552,7 +552,7 @@ class LambdaTaskAuthorizerTest {
taskRole(OWN_TASK_ROLE, trust("{\"Service\":\"lambda.amazonaws.com\"}"));
AslExecutor.FailStateException refused = assertThrows(AslExecutor.FailStateException.class,
() -> authorizer.authorize(machine(), OWN_TASK_ROLE, "S3.", List.of()));
() -> authorizer.authorize(machine(), OWN_TASK_ROLE, "S3.AccessDeniedException", List.of()));
assertEquals("States.TaskFailed", refused.error);
assertEquals("The role " + ROLE + " is not authorized to assume the task state's role, " + OWN_TASK_ROLE + ".",
@@ -0,0 +1,777 @@
package io.github.hectorvent.floci.services.stepfunctions;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.services.sqs.SqsService;
import io.github.hectorvent.floci.testing.EnforcementFixtures;
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
import io.github.hectorvent.floci.testing.PartitionCleanup;
import io.github.hectorvent.floci.testing.RestAssuredJsonUtils;
import io.github.hectorvent.floci.testing.SqsTestQueue;
import io.github.hectorvent.floci.testing.StepFunctionsTestClient;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.response.Response;
import jakarta.inject.Inject;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.ValueSource;
import java.net.URI;
import java.time.Duration;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
import static io.github.hectorvent.floci.testing.StepFunctionsTestClient.LAMBDA_TRUST;
import static io.github.hectorvent.floci.testing.StepFunctionsTestClient.STATES_TRUST;
import static io.github.hectorvent.floci.testing.StepFunctionsTestClient.trusting;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** SNS and EventBridge tasks under IAM enforcement; each destination forwards to a queue the test reads. */
@QuarkusTest
@TestProfile(IamEnforcementProfile.class)
class StepFunctionsMessagingTaskIamIntegrationTest {
private static final String ACCOUNT = "000000000451";
private static final String OTHER_ACCOUNT = "000000000452";
private static final String REGION = "us-east-1";
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final String READ_ONLY = allow("*", "sns:ListTopics", "events:ListRules");
private static final String ASSUME = allow("*", "sts:AssumeRole");
private static final String PUBLISH_ANY = allow("*", "sns:Publish", "events:PutEvents");
@TestHTTPResource
URI baseUri;
@Inject
SqsService sqsService;
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@ParameterizedTest
@ValueSource(strings = {"sns:publish", "aws-sdk:sns:publish", "events:putEvents"})
void anExecutionRoleAllowedTheActionOnTheResourceDelivers(String integration) throws Exception {
Target target = target(integration);
String role = role(STATES_TRUST, allow(target.resourceArn(), target.action()));
Response completed = terminal(start(machine(jsonPathTask(target, null), role), "{}"));
assertEquals("SUCCEEDED", completed.jsonPath().getString("status"), completed.asString());
assertDelivered(target);
}
@ParameterizedTest
@CsvSource({"sns:publish,jsonpath", "aws-sdk:sns:publish,jsonpath", "events:putEvents,jsonpath",
"sns:publish,jsonata", "aws-sdk:sns:publish,jsonata", "events:putEvents,jsonata"})
void anExecutionRoleNotAllowedTheActionIsRefused(String integration, String form) throws Exception {
Target target = target(integration);
Map<String, Object> task = "jsonata".equals(form) ? jsonataTask(target, null)
: jsonPathTask(target, null);
String machine = machine(task, role(STATES_TRUST, READ_ONLY));
Response completed = terminal(start(machine, "{}"));
assertFailed(completed, target.error());
String cause = completed.jsonPath().getString("cause");
assertTrue(cause.contains(target.action()), cause);
assertTrue(cause.contains(target.resourceArn()), cause);
assertNothingDelivered(target);
}
@ParameterizedTest
@ValueSource(strings = {"sns:publish", "aws-sdk:sns:publish", "events:putEvents"})
void theActionOnAnotherResourceDoesNotCount(String integration) throws Exception {
Target target = target(integration);
String elsewhere = target.resourceArn() + "-elsewhere";
String machine = machine(jsonPathTask(target, null), role(STATES_TRUST, allow(elsewhere, target.action())));
assertFailed(terminal(start(machine, "{}")), target.error());
assertNothingDelivered(target);
}
@ParameterizedTest
@ValueSource(strings = {"sns:publish", "aws-sdk:sns:publish", "events:putEvents"})
void anExecutionRoleThatDoesNotTrustStepFunctionsIsRefusedWhateverItsPolicies(String integration) throws Exception {
Target target = target(integration);
String role = role(LAMBDA_TRUST, PUBLISH_ANY);
Response completed = terminal(start(machine(jsonPathTask(target, null), role), "{}"));
assertFailed(completed, "States.Permissions");
assertTrue(completed.jsonPath().getString("cause").contains(role), completed.asString());
assertNothingDelivered(target);
}
@ParameterizedTest
@ValueSource(strings = {"sns:publish", "aws-sdk:sns:publish", "events:putEvents"})
void anExecutionRoleThatDoesNotExistIsRefused(String integration) throws Exception {
Target target = target(integration);
String role = "arn:aws:iam::" + ACCOUNT + ":role/no-such-role-" + UUID.randomUUID();
Response completed = terminal(start(machine(jsonPathTask(target, null), role), "{}"));
assertFailed(completed, "States.Permissions");
assertTrue(completed.jsonPath().getString("cause").contains(role), completed.asString());
assertNothingDelivered(target);
}
@ParameterizedTest
@CsvSource({"sns:publish,jsonpath", "aws-sdk:sns:publish,jsonpath", "events:putEvents,jsonpath",
"sns:publish,jsonata", "aws-sdk:sns:publish,jsonata", "events:putEvents,jsonata"})
void credentialsDeliverAsTheirRoleWithoutTheExecutionRolesPermission(String integration, String form)
throws Exception {
Target target = target(integration);
String executionRole = role(STATES_TRUST, ASSUME);
String taskRole = role(trusting(executionRole), allow(target.resourceArn(), target.action()));
Map<String, Object> task = "jsonata".equals(form) ? jsonataTask(target, "{% $states.input.role %}")
: jsonPathTask(target, taskRole);
String machine = machine(task, executionRole);
String execution = start(machine, MAPPER.writeValueAsString(Map.of("role", taskRole)));
Response completed = terminal(execution);
assertEquals("SUCCEEDED", completed.jsonPath().getString("status"), completed.asString());
assertDelivered(target);
JsonNode scheduled = eventsOfType(execution, "TaskScheduled").getFirst();
assertEquals(taskRole, scheduled.path("taskScheduledEventDetails").path("taskCredentials").path("roleArn").asText());
}
@ParameterizedTest
@ValueSource(strings = {"sns:publish", "aws-sdk:sns:publish", "events:putEvents"})
void aRoleTheExecutionRoleCannotAssumeFailsInTheNestedForm(String integration) throws Exception {
Target target = target(integration);
String executionRole = role(STATES_TRUST, PUBLISH_ANY);
String taskRole = role(LAMBDA_TRUST, PUBLISH_ANY);
Response completed = terminal(start(machine(jsonPathTask(target, taskRole), executionRole), "{}"));
assertFailed(completed, "States.TaskFailed");
assertEquals("The role " + executionRole + " is not authorized to assume the task state's role, " + taskRole + ".",
completed.jsonPath().getString("cause"));
assertNothingDelivered(target);
}
@ParameterizedTest
@ValueSource(strings = {"sns:publish", "events:putEvents"})
void aCredentialsRoleThatDoesNotExistFailsAsARefusedAssumption(String integration) throws Exception {
Target target = target(integration);
String taskRole = "arn:aws:iam::" + ACCOUNT + ":role/no-such-role-" + UUID.randomUUID();
String executionRole = role(STATES_TRUST, PUBLISH_ANY);
assertFailed(terminal(start(machine(jsonPathTask(target, taskRole), executionRole), "{}")), "States.TaskFailed");
assertNothingDelivered(target);
}
@ParameterizedTest
@ValueSource(strings = {"sns:publish", "aws-sdk:sns:publish", "events:putEvents"})
void aCredentialsRoleNotAllowedTheActionIsRefusedEvenIfTheExecutionRoleIs(String integration) throws Exception {
Target target = target(integration);
String executionRole = role(STATES_TRUST, allow("*", "sts:AssumeRole", "sns:Publish", "events:PutEvents"));
String taskRole = role(trusting(executionRole), READ_ONLY);
Response completed = terminal(start(machine(jsonPathTask(target, taskRole), executionRole), "{}"));
assertFailed(completed, target.error());
String cause = completed.jsonPath().getString("cause");
assertTrue(cause.contains("assumed-role/" + taskRole.substring(taskRole.lastIndexOf('/') + 1)), cause);
assertNothingDelivered(target);
}
@ParameterizedTest
@CsvSource({"sns:publish,jsonpath", "aws-sdk:sns:publish,jsonpath", "events:putEvents,jsonpath",
"sns:publish,jsonata", "events:putEvents,jsonata"})
void waitForTaskTokenRefusedDoesNotWaitAndDeliversNothing(String integration, String form) throws Exception {
Target target = target(integration);
Map<String, Object> task = new LinkedHashMap<>("jsonata".equals(form)
? jsonataTask(target, null) : jsonPathTask(target, null));
task.put("Resource", "arn:aws:states:::" + integration + ".waitForTaskToken");
task.put("TimeoutSeconds", 60);
Response completed = terminal(start(machine(task, role(STATES_TRUST, READ_ONLY)), "{}"));
assertFailed(completed, target.error());
assertNothingDelivered(target);
}
@ParameterizedTest
@ValueSource(strings = {"sns:publish", "aws-sdk:sns:publish", "events:putEvents"})
void waitForTaskTokenAllowedDeliversAndWaits(String integration) throws Exception {
Target target = target(integration);
String executionRole = role(STATES_TRUST, ASSUME);
String taskRole = role(trusting(executionRole), allow(target.resourceArn(), target.action()));
Map<String, Object> task = new LinkedHashMap<>(Map.of("Type", "Task",
"Resource", "arn:aws:states:::" + integration + ".waitForTaskToken",
"Credentials", Map.of("RoleArn", taskRole),
"Parameters", target.tokenArguments(), "TimeoutSeconds", 60, "End", true));
String execution = start(machine(task, executionRole), "{}");
JsonNode delivered = target.sink().awaitJson(Duration.ofSeconds(5));
String token = integration.contains("sns")
? MAPPER.readTree(delivered.path("Message").asText()).path("token").asText()
: delivered.path("detail").path("token").asText();
request("SendTaskSuccess", Map.of("taskToken", token, "output", "{\"done\":true}")).then().statusCode(200);
Response completed = terminal(execution);
assertEquals("SUCCEEDED", completed.jsonPath().getString("status"), completed.asString());
}
@ParameterizedTest
@ValueSource(strings = {"sns:publish", "aws-sdk:sns:publish", "events:putEvents"})
void aRefusalCanBeRetriedAndCaught(String integration) throws Exception {
Target target = target(integration);
Map<String, Object> task = new LinkedHashMap<>(jsonPathTask(target, null));
task.put("Retry", List.of(Map.of("ErrorEquals", List.of(target.error()), "IntervalSeconds", 1,
"BackoffRate", 1.0, "MaxAttempts", 1)));
task.put("Catch", List.of(Map.of("ErrorEquals", List.of(target.error()), "Next", "Recovered")));
String definition = MAPPER.writeValueAsString(Map.of("StartAt", "Send", "States", Map.of("Send", task,
"Recovered", Map.of("Type", "Pass", "End", true))));
String execution = start(machineWithDefinition(definition, role(STATES_TRUST, READ_ONLY)), "{}");
Response completed = terminal(execution);
assertEquals("SUCCEEDED", completed.jsonPath().getString("status"), completed.asString());
assertEquals(2, eventsOfType(execution, "TaskFailed").size());
assertEquals(target.error(), eventsOfType(execution, "TaskFailed").getLast()
.path("taskFailedEventDetails").path("error").asText());
}
/** The retry decides again, so a role granted the action between attempts then delivers. */
@ParameterizedTest
@ValueSource(strings = {"sns:publish", "events:putEvents"})
void aRetryDecidesAgain(String integration) throws Exception {
Target target = target(integration);
String roleName = "sfn-msg-" + UUID.randomUUID();
String role = EnforcementFixtures.createRole(cleanup, baseUri.getPort(), ACCOUNT, "/", roleName,
STATES_TRUST, READ_ONLY);
Map<String, Object> task = new LinkedHashMap<>(jsonPathTask(target, null));
task.put("Retry", List.of(Map.of("ErrorEquals", List.of(target.error()), "IntervalSeconds", 2,
"BackoffRate", 1.0, "MaxAttempts", 3)));
String execution = start(machine(task, role), "{}");
await(execution, "TaskFailed");
given().port(baseUri.getPort()).header("Authorization", auth(ACCOUNT, "iam"))
.formParam("Action", "PutRolePolicy").formParam("Version", "2010-05-08")
.formParam("RoleName", roleName).formParam("PolicyName", "p").formParam("PolicyDocument", PUBLISH_ANY)
.post("/").then().statusCode(200);
Response completed = terminal(execution, 20);
assertEquals("SUCCEEDED", completed.jsonPath().getString("status"), completed.asString());
assertDelivered(target);
}
@ParameterizedTest
@ValueSource(strings = {"sns:publish", "aws-sdk:sns:publish"})
void aSameAccountTopicPolicyGrantsARoleWithNoIdentityPermission(String integration) throws Exception {
Target target = target(integration);
String role = role(STATES_TRUST, READ_ONLY);
setTopicPolicy(target.resourceArn(), topicStatement("Allow", role, target.resourceArn()));
Response completed = terminal(start(machine(jsonPathTask(target, null), role), "{}"));
assertEquals("SUCCEEDED", completed.jsonPath().getString("status"), completed.asString());
assertDelivered(target);
}
@ParameterizedTest
@CsvSource({"sns:publish,SNS.AuthorizationErrorException", "aws-sdk:sns:publish,Sns.AuthorizationErrorException"})
void aTopicPolicyDenyRefusesARoleItsIdentityPolicyAllows(String integration, String error) throws Exception {
Target target = target(integration);
String role = role(STATES_TRUST, PUBLISH_ANY);
setTopicPolicy(target.resourceArn(), topicStatement("Deny", role, target.resourceArn()));
Response completed = terminal(start(machine(jsonPathTask(target, null), role), "{}"));
assertFailed(completed, error);
assertNothingDelivered(target);
}
@Test
void aForeignCredentialsRoleNeedsTheTopicPolicyToo() throws Exception {
Target target = target("sns:publish");
String executionRole = role(STATES_TRUST, ASSUME);
String foreignRole = EnforcementFixtures.createRole(cleanup, baseUri.getPort(), OTHER_ACCOUNT, "/",
"sfn-msg-" + UUID.randomUUID(), trusting(executionRole), PUBLISH_ANY);
String machine = machine(jsonPathTask(target, foreignRole), executionRole);
Response refused = terminal(start(machine, "{}"));
assertFailed(refused, "SNS.AuthorizationErrorException");
String cause = refused.jsonPath().getString("cause");
assertTrue(cause.contains("arn:aws:sts::" + OTHER_ACCOUNT + ":assumed-role/"), cause);
assertNothingDelivered(target);
setTopicPolicy(target.resourceArn(), topicStatement("Allow", foreignRole, target.resourceArn()));
Response allowed = terminal(start(machine, "{}"));
assertEquals("SUCCEEDED", allowed.jsonPath().getString("status"), allowed.asString());
assertDelivered(target);
}
/** Foreign without its identity half: the topic policy alone does not admit another account's role. */
@Test
void aForeignCredentialsRoleNeedsItsIdentityPolicyToo() throws Exception {
Target target = target("sns:publish");
String executionRole = role(STATES_TRUST, ASSUME);
String foreignRole = EnforcementFixtures.createRole(cleanup, baseUri.getPort(), OTHER_ACCOUNT, "/",
"sfn-msg-" + UUID.randomUUID(), trusting(executionRole), READ_ONLY);
setTopicPolicy(target.resourceArn(), topicStatement("Allow", foreignRole, target.resourceArn()));
Response completed = terminal(start(machine(jsonPathTask(target, foreignRole), executionRole), "{}"));
assertFailed(completed, "SNS.AuthorizationErrorException");
assertNothingDelivered(target);
}
@Test
void aTargetArnNeedsThePermissionOnThatResource() throws Exception {
Target target = target("sns:publish");
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::sns:publish",
"Parameters", Map.of("TargetArn", target.resourceArn(), "Message", "hello"), "End", true);
assertFailed(terminal(start(machine(task, role(STATES_TRUST, READ_ONLY)), "{}")), "SNS.AuthorizationErrorException");
assertNothingDelivered(target);
Response allowed = terminal(start(machine(task,
role(STATES_TRUST, allow(target.resourceArn(), "sns:Publish"))), "{}"));
assertEquals("SUCCEEDED", allowed.jsonPath().getString("status"), allowed.asString());
assertDelivered(target);
}
@Test
void anSmsNeedsThePermissionOnEveryResource() throws Exception {
Target target = target("sns:publish");
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::sns:publish",
"Parameters", Map.of("PhoneNumber", "+15555550100", "Message", "hello"), "End", true);
assertFailed(terminal(start(machine(task,
role(STATES_TRUST, allow(target.resourceArn(), "sns:Publish"))), "{}")), "SNS.AuthorizationErrorException");
Response allowed = terminal(start(machine(task, role(STATES_TRUST, PUBLISH_ANY)), "{}"));
assertEquals("SUCCEEDED", allowed.jsonPath().getString("status"), allowed.asString());
}
/** No permission is checked, so a role with none reaches SNS, which answers with its own errors. */
@ParameterizedTest
@CsvSource({"orders,SNS.NotFoundException", "arn:aws:sqs:us-east-1:000000000451:orders,SNS.NotFoundException",
"'',SNS.NotFoundException"})
void aTopicArnThatIsNotAnSnsArnIsAnsweredBySns(String topicArn, String error) throws Exception {
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::sns:publish",
"Parameters", Map.of("TopicArn", topicArn, "Message", "hello"), "End", true);
assertFailed(terminal(start(machine(task, role(STATES_TRUST, READ_ONLY)), "{}")), error);
}
@Test
void aPublishNamingNothingIsAnsweredBySns() throws Exception {
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::sns:publish",
"Parameters", Map.of("Message", "hello"), "End", true);
assertFailed(terminal(start(machine(task, role(STATES_TRUST, READ_ONLY)), "{}")),
"SNS.InvalidParameterException");
}
/** A statement naming the account leaves the decision to the account's IAM policies, as AWS does. */
@Test
void anEventBusPolicyNamingTheAccountDoesNotGrantARoleWithNoIdentityPermission() throws Exception {
Target target = target("events:putEvents");
String role = role(STATES_TRUST, READ_ONLY);
events("PutPermission", Map.of("EventBusName", busName(target.resourceArn()), "StatementId", "s",
"Action", "events:PutEvents", "Principal", ACCOUNT));
assertFailed(terminal(start(machine(jsonPathTask(target, null), role), "{}")), "EventBridge.AccessDeniedException");
assertNothingDelivered(target);
}
@Test
void aSameAccountBusPolicyNamingTheRoleGrantsItWithNoIdentityPermission() throws Exception {
Target target = target("events:putEvents");
String role = role(STATES_TRUST, READ_ONLY);
putBusPolicy(target.resourceArn(), busStatement("Allow", role, target.resourceArn()));
Response completed = terminal(start(machine(jsonPathTask(target, null), role), "{}"));
assertEquals("SUCCEEDED", completed.jsonPath().getString("status"), completed.asString());
assertDelivered(target);
}
@Test
void aBusPolicyDenyRefusesARoleItsIdentityPolicyAllows() throws Exception {
Target target = target("events:putEvents");
String role = role(STATES_TRUST, PUBLISH_ANY);
putBusPolicy(target.resourceArn(), busStatement("Deny", role, target.resourceArn()));
assertFailed(terminal(start(machine(jsonPathTask(target, null), role), "{}")), "EventBridge.AccessDeniedException");
assertNothingDelivered(target);
}
/** Another account's role needs the bus's grant as well as its own, which {@code PutPermission} gives. */
@Test
void aForeignCredentialsRoleNeedsTheBusPolicyToo() throws Exception {
Target target = target("events:putEvents");
String executionRole = role(STATES_TRUST, ASSUME);
String foreignRole = EnforcementFixtures.createRole(cleanup, baseUri.getPort(), OTHER_ACCOUNT, "/",
"sfn-msg-" + UUID.randomUUID(), trusting(executionRole), PUBLISH_ANY);
String machine = machine(jsonPathTask(target, foreignRole), executionRole);
Response refused = terminal(start(machine, "{}"));
assertFailed(refused, "EventBridge.AccessDeniedException");
String cause = refused.jsonPath().getString("cause");
assertTrue(cause.contains("arn:aws:sts::" + OTHER_ACCOUNT + ":assumed-role/"), cause);
assertNothingDelivered(target);
events("PutPermission", Map.of("EventBusName", busName(target.resourceArn()), "StatementId", "s",
"Action", "events:PutEvents", "Principal", OTHER_ACCOUNT));
Response allowed = terminal(start(machine, "{}"));
assertEquals("SUCCEEDED", allowed.jsonPath().getString("status"), allowed.asString());
assertDelivered(target);
}
@Test
void aForeignCredentialsRoleNeedsItsIdentityPolicyOnABusToo() throws Exception {
Target target = target("events:putEvents");
String executionRole = role(STATES_TRUST, ASSUME);
String foreignRole = EnforcementFixtures.createRole(cleanup, baseUri.getPort(), OTHER_ACCOUNT, "/",
"sfn-msg-" + UUID.randomUUID(), trusting(executionRole), READ_ONLY);
events("PutPermission", Map.of("EventBusName", busName(target.resourceArn()), "StatementId", "s",
"Action", "events:PutEvents", "Principal", OTHER_ACCOUNT));
Response completed = terminal(start(machine(jsonPathTask(target, foreignRole), executionRole), "{}"));
assertFailed(completed, "EventBridge.AccessDeniedException");
assertNothingDelivered(target);
}
@Test
void anEntryNamingNoBusNeedsThePermissionOnTheDefaultBus() throws Exception {
String name = "sfn-iam-default-" + UUID.randomUUID();
SqsTestQueue sink = eventSink("default", name);
String defaultBus = "arn:aws:events:" + REGION + ":" + ACCOUNT + ":event-bus/default";
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::events:putEvents",
"Parameters", Map.of("Entries", List.of(entry(null, name))), "End", true);
assertFailed(terminal(start(machine(task, role(STATES_TRUST, allow(defaultBus + "-other", "events:PutEvents"))),
"{}")), "EventBridge.AccessDeniedException");
assertEquals(0, sink.receive().size());
Response allowed = terminal(start(machine(task, role(STATES_TRUST, allow(defaultBus, "events:PutEvents"))), "{}"));
assertEquals("SUCCEEDED", allowed.jsonPath().getString("status"), allowed.asString());
assertEquals(1, sink.receive().size());
}
/** The name and each ARN reach the same bus, whichever account the ARN names, so that bus decides. */
@ParameterizedTest
@CsvSource({"name", "arn", "foreign-arn"})
void aBusNamedByItsArnIsTheBusItsLookupReaches(String form) throws Exception {
Target target = target("events:putEvents");
String name = busName(target.resourceArn());
String given = switch (form) {
case "name" -> name;
case "arn" -> target.resourceArn();
default -> "arn:aws:events:" + REGION + ":" + OTHER_ACCOUNT + ":event-bus/" + name;
};
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::events:putEvents",
"Parameters", Map.of("Entries", List.of(entry(given, target.source()))), "End", true);
assertFailed(terminal(start(machine(task, role(STATES_TRUST, allow(given + "-other", "events:PutEvents"))), "{}")),
"EventBridge.AccessDeniedException");
assertEquals(0, target.sink().receive().size());
Response allowed = terminal(start(machine(task,
role(STATES_TRUST, allow(target.resourceArn(), "events:PutEvents"))), "{}"));
assertEquals("SUCCEEDED", allowed.jsonPath().getString("status"), allowed.asString());
assertDelivered(target);
}
/** Refused ahead of the call, so the entries for the bus the role may use are not sent either. */
@ParameterizedTest
@ValueSource(booleans = {true, false})
void entriesForSeveralBusesAreRefusedWhenOneIsDenied(boolean deniedFirst) throws Exception {
Target allowedBus = target("events:putEvents");
Target deniedBus = target("events:putEvents");
List<Object> entries = deniedFirst
? List.of(entry(busName(deniedBus.resourceArn()), deniedBus.source()),
entry(busName(allowedBus.resourceArn()), allowedBus.source()))
: List.of(entry(busName(allowedBus.resourceArn()), allowedBus.source()),
entry(busName(deniedBus.resourceArn()), deniedBus.source()));
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::events:putEvents",
"Parameters", Map.of("Entries", entries), "End", true);
String role = role(STATES_TRUST, allow(allowedBus.resourceArn(), "events:PutEvents"));
Response completed = terminal(start(machine(task, role), "{}"));
assertFailed(completed, "EventBridge.AccessDeniedException");
assertTrue(completed.jsonPath().getString("cause").contains(deniedBus.resourceArn()), completed.asString());
assertEquals(0, allowedBus.sink().receive().size());
assertEquals(0, deniedBus.sink().receive().size());
}
@Test
void anEntryEventBridgeWouldRejectStillNeedsItsBus() throws Exception {
Target allowedBus = target("events:putEvents");
Target deniedBus = target("events:putEvents");
Map<String, Object> rejected = entry(busName(deniedBus.resourceArn()), deniedBus.source());
rejected.remove("Source");
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::events:putEvents",
"Parameters", Map.of("Entries", List.of(entry(busName(allowedBus.resourceArn()), allowedBus.source()),
rejected)), "End", true);
String role = role(STATES_TRUST, allow(allowedBus.resourceArn(), "events:PutEvents"));
Response completed = terminal(start(machine(task, role), "{}"));
assertFailed(completed, "EventBridge.AccessDeniedException");
assertTrue(completed.jsonPath().getString("cause").contains(deniedBus.resourceArn()), completed.asString());
assertEquals(0, allowedBus.sink().receive().size());
}
@Test
void entriesForSeveralBusesEachNeedAndGetTheirPermission() throws Exception {
Target first = target("events:putEvents");
Target second = target("events:putEvents");
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::events:putEvents",
"Parameters", Map.of("Entries", List.of(entry(first.resourceArn(), first.source()),
entry(busName(second.resourceArn()), second.source()))), "End", true);
String role = role(STATES_TRUST, MAPPER.writeValueAsString(Map.of("Version", "2012-10-17", "Statement", List.of(
Map.of("Effect", "Allow", "Action", "events:PutEvents",
"Resource", List.of(first.resourceArn(), second.resourceArn()))))));
Response completed = terminal(start(machine(task, role), "{}"));
assertEquals("SUCCEEDED", completed.jsonPath().getString("status"), completed.asString());
assertDelivered(first);
assertDelivered(second);
}
/** A malformed member names no bus, so a role with no permission gets EventBridge's answer. */
@Test
void aMalformedBusArnIsAnsweredByEventBridge() throws Exception {
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::events:putEvents",
"Parameters", Map.of("Entries", List.of(entry("arn:aws:events:", "sfn-iam"))), "End", true);
Response completed = terminal(start(machine(task, role(STATES_TRUST, READ_ONLY)), "{}"));
assertEquals("FAILED", completed.jsonPath().getString("status"), completed.asString());
assertNotEquals("EventBridge.AccessDeniedException", completed.jsonPath().getString("error"), completed.asString());
}
@Test
void noEntriesNeedNoPermission() throws Exception {
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::events:putEvents",
"Parameters", Map.of("Entries", List.of()), "End", true);
Response completed = terminal(start(machine(task, role(STATES_TRUST, READ_ONLY)), "{}"));
assertEquals("SUCCEEDED", completed.jsonPath().getString("status"), completed.asString());
}
private static String allow(String resource, String... actions) {
try {
return MAPPER.writeValueAsString(Map.of("Version", "2012-10-17", "Statement", List.of(
Map.of("Effect", "Allow", "Action", List.of(actions), "Resource", resource))));
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
private static String topicStatement(String effect, String principal, String topicArn) {
return """
{"Version":"2012-10-17","Statement":[{"Effect":"%s","Principal":{"AWS":"%s"},
"Action":"sns:Publish","Resource":"%s"}]}
""".formatted(effect, principal, topicArn);
}
private static String busStatement(String effect, String principal, String busArn) {
return """
{"Version":"2012-10-17","Statement":[{"Sid":"s","Effect":"%s","Principal":{"AWS":"%s"},
"Action":"events:PutEvents","Resource":"%s"}]}
""".formatted(effect, principal, busArn);
}
private void putBusPolicy(String busArn, String policy) {
events("PutPermission", Map.of("EventBusName", busName(busArn), "Policy", policy));
}
private static String busName(String busArn) {
return busArn.substring(busArn.lastIndexOf('/') + 1);
}
private static Map<String, Object> entry(String bus, String source) {
Map<String, Object> entry = new LinkedHashMap<>(Map.of("Source", source, "DetailType", "t",
"Detail", Map.of("m", "hello")));
if (bus != null) {
entry.put("EventBusName", bus);
}
return entry;
}
private static void assertFailed(Response completed, String error) {
assertEquals("FAILED", completed.jsonPath().getString("status"), completed.asString());
assertEquals(error, completed.jsonPath().getString("error"), completed.asString());
}
private static void assertDelivered(Target target) throws Exception {
JsonNode delivered = target.sink().awaitJson(Duration.ofSeconds(5));
assertTrue(delivered.toString().contains("hello"), delivered.toString());
}
private static void assertNothingDelivered(Target target) {
assertEquals(0, target.sink().receive().size());
}
/**
* A topic or an event bus a task sends to, and the queue that receives what reaches it.
*
* @param source the event source its rule matches, for a bus
*/
private record Target(String error, String action, String resourceArn, Map<String, Object> arguments,
Map<String, Object> tokenArguments, SqsTestQueue sink, String source) {
}
private Target target(String integration) {
return integration.contains("sns") ? topicTarget(integration.startsWith("aws-sdk") ? "Sns." : "SNS.") : busTarget();
}
private Target topicTarget(String errorPrefix) {
String topicArn = EnforcementFixtures.createTopic(cleanup, baseUri.getPort(), auth(ACCOUNT, "sns"),
"sfn-iam-" + UUID.randomUUID());
SqsTestQueue sink = queue();
given().port(baseUri.getPort()).header("Authorization", auth(ACCOUNT, "sns"))
.formParam("Action", "Subscribe").formParam("TopicArn", topicArn)
.formParam("Protocol", "sqs").formParam("Endpoint", sink.arn())
.post("/").then().statusCode(200);
return new Target(errorPrefix + "AuthorizationErrorException", "sns:Publish", topicArn,
Map.of("TopicArn", topicArn, "Message", "hello"),
Map.of("TopicArn", topicArn, "Message", Map.of("token.$", "$$.Task.Token")), sink, null);
}
private Target busTarget() {
String name = "sfn-iam-" + UUID.randomUUID();
Response created = events("CreateEventBus", Map.of("Name", name));
cleanup.register(() -> events("DeleteEventBus", Map.of("Name", name)));
String source = "sfn-iam-" + UUID.randomUUID();
SqsTestQueue sink = eventSink(name, source);
Map<String, Object> tokenEntry = new LinkedHashMap<>(entry(name, source));
tokenEntry.put("Detail", Map.of("token.$", "$$.Task.Token"));
return new Target("EventBridge.AccessDeniedException", "events:PutEvents",
created.jsonPath().getString("EventBusArn"), Map.of("Entries", List.of(entry(name, source))),
Map.of("Entries", List.of(tokenEntry)), sink, source);
}
/** A queue receiving the events of {@code source} on {@code bus}, through a rule removed with it. */
private SqsTestQueue eventSink(String bus, String source) {
SqsTestQueue sink = queue();
String rule = "sfn-iam-" + UUID.randomUUID();
events("PutRule", Map.of("Name", rule, "EventBusName", bus,
"EventPattern", "{\"source\":[\"" + source + "\"]}"));
cleanup.register(() -> events("DeleteRule", Map.of("Name", rule, "EventBusName", bus)));
events("PutTargets", Map.of("Rule", rule, "EventBusName", bus,
"Targets", List.of(Map.of("Id", "1", "Arn", sink.arn()))));
cleanup.register(() -> events("RemoveTargets", Map.of("Rule", rule, "EventBusName", bus, "Ids", List.of("1"))));
return sink;
}
private Response events(String action, Map<String, ?> input) {
Response response = given().port(baseUri.getPort()).header("Authorization", auth(ACCOUNT, "events"))
.header("X-Amz-Target", "AWSEvents." + action)
.contentType("application/x-amz-json-1.1").body(MAPPER.valueToTree(input).toString()).post("/");
response.then().statusCode(200);
return response;
}
private void setTopicPolicy(String topicArn, String policy) {
EnforcementFixtures.setTopicPolicy(baseUri.getPort(), auth(ACCOUNT, "sns"), topicArn, policy);
}
private Map<String, Object> jsonPathTask(Target target, String taskRole) {
Map<String, Object> task = new LinkedHashMap<>(Map.of("Type", "Task",
"Resource", "arn:aws:states:::" + integrationOf(target),
"Parameters", target.arguments(), "End", true));
if (taskRole != null) {
task.put("Credentials", Map.of("RoleArn", taskRole));
}
return task;
}
private Map<String, Object> jsonataTask(Target target, String roleExpression) {
Map<String, Object> task = new LinkedHashMap<>(Map.of("Type", "Task", "QueryLanguage", "JSONata",
"Resource", "arn:aws:states:::" + integrationOf(target),
"Arguments", target.arguments(), "End", true));
if (roleExpression != null) {
task.put("Credentials", Map.of("RoleArn", roleExpression));
}
return task;
}
private static String integrationOf(Target target) {
return switch (target.error()) {
case "SNS.AuthorizationErrorException" -> "sns:publish";
case "Sns.AuthorizationErrorException" -> "aws-sdk:sns:publish";
default -> "events:putEvents";
};
}
private SqsTestQueue queue() {
SqsTestQueue queue = SqsTestQueue.create(sqsService, ACCOUNT, REGION, "sfn-msg-iam-");
cleanup.register(queue::delete);
return queue;
}
private String role(String trust, String policy) {
return EnforcementFixtures.createRole(cleanup, baseUri.getPort(), ACCOUNT, "/",
"sfn-msg-" + UUID.randomUUID(), trust, policy);
}
private String machine(Map<String, Object> task, String role) throws Exception {
return machineWithDefinition(MAPPER.writeValueAsString(
Map.of("StartAt", "Send", "States", Map.of("Send", task))), role);
}
private String machineWithDefinition(String definition, String role) {
return sfn().createMachine(ACCOUNT, "sfn-msg-iam-", definition, "STANDARD", role);
}
private String start(String machine, String input) {
return sfn().start(ACCOUNT, machine, input);
}
private Response terminal(String execution) throws Exception {
return terminal(execution, 5);
}
private Response terminal(String execution, int seconds) throws Exception {
return sfn().terminal(ACCOUNT, execution, seconds);
}
private void await(String execution, String eventType) throws Exception {
sfn().await(ACCOUNT, execution, eventType, 5);
}
private List<JsonNode> eventsOfType(String execution, String type) throws Exception {
return sfn().eventsOfType(ACCOUNT, execution, type);
}
private Response request(String action, Map<String, ?> input) {
return sfn().request(ACCOUNT, action, input);
}
private StepFunctionsTestClient sfn() {
return new StepFunctionsTestClient(baseUri.getPort(), cleanup);
}
}
@@ -27,13 +27,14 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
import static io.github.hectorvent.floci.testing.StepFunctionsTestClient.LAMBDA_TRUST;
import static io.github.hectorvent.floci.testing.StepFunctionsTestClient.STATES_TRUST;
import static io.github.hectorvent.floci.testing.StepFunctionsTestClient.trusting;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
@QuarkusTest
@TestProfile(IamEnforcementProfile.class)
@@ -42,14 +43,6 @@ class StepFunctionsSqsTaskIamIntegrationTest {
private static final String OTHER_ACCOUNT = "000000000442";
private static final String REGION = "us-east-1";
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final String STATES_TRUST = """
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"sts:AssumeRole",
"Principal":{"Service":"states.amazonaws.com"}}]}
""";
private static final String LAMBDA_TRUST = """
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"sts:AssumeRole",
"Principal":{"Service":"lambda.amazonaws.com"}}]}
""";
private static final String SEND = allow("sqs:SendMessage");
private static final String READ_ONLY = allow("sqs:GetQueueUrl");
private static final String ASSUME = allow("sts:AssumeRole");
@@ -360,13 +353,6 @@ class StepFunctionsSqsTaskIamIntegrationTest {
}
}
private static String trusting(String role) {
return """
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"sts:AssumeRole",
"Principal":{"AWS":"%s"}}]}
""".formatted(role);
}
private static String queuePolicy(String effect, String principal, String queueArn) {
return """
{"Version":"2012-10-17","Statement":[{"Effect":"%s","Principal":{"AWS":"%s"},
@@ -437,14 +423,7 @@ class StepFunctionsSqsTaskIamIntegrationTest {
}
private void await(String execution, String eventType) throws Exception {
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
do {
if (!eventsOfType(execution, eventType).isEmpty()) {
return;
}
Thread.sleep(25);
} while (System.nanoTime() < deadline);
fail("No " + eventType + " event in " + execution);
sfn().await(ACCOUNT, execution, eventType, 5);
}
private List<JsonNode> eventsOfType(String execution, String type) throws Exception {
@@ -26,6 +26,7 @@ import org.junit.jupiter.params.provider.ValueSource;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@@ -235,6 +236,26 @@ class StepFunctionsTaskCredentialsIntegrationTest {
assertEquals(taskRole, taskScheduled(execution).path("taskCredentials").path("roleArn").asText());
}
/** Nothing evaluates a role that does not exist, nor a publisher no policy allows. */
@ParameterizedTest
@ValueSource(strings = {"sns:publish", "aws-sdk:sns:publish", "events:putEvents"})
void messagingCredentialsRetainDefaultIamDisabledBehavior(String integration) throws Exception {
Map<String, Object> arguments = integration.contains("sns")
? Map.of("TopicArn", EnforcementFixtures.createTopic(cleanup, baseUri.getPort(), auth(SOURCE, "sns"),
"credentials-" + UUID.randomUUID()), "Message", "hello")
: Map.of("Entries", List.of(Map.of("Source", "credentials", "DetailType", "t", "Detail", "{}")));
String taskRole = "arn:aws:iam::" + TARGET + ":role/no-such-role";
String definition = MAPPER.writeValueAsString(Map.of("StartAt", "Send", "States", Map.of("Send", Map.of(
"Type", "Task", "Resource", "arn:aws:states:::" + integration,
"Credentials", Map.of("RoleArn", taskRole), "Parameters", arguments, "End", true))));
String execution = start(machine(SOURCE, definition, "STANDARD"), "{}");
Response done = terminal(SOURCE, execution);
assertEquals("SUCCEEDED", done.jsonPath().getString("status"), done.asString());
assertEquals(taskRole, taskScheduled(execution).path("taskCredentials").path("roleArn").asText());
}
@Test
void credentialsThatDoNotResolveStillPassWithEnforcementOff() throws Exception {
SqsTestQueue queue = SqsTestQueue.create(sqsService, SOURCE, "us-east-1", "credentials-");
@@ -19,6 +19,8 @@ class TaskPermissionsTest {
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final String ACCOUNT = "111122223333";
private static final String TOPIC = "arn:aws:sns:eu-west-1:111122223333:orders";
private static final String ENDPOINT = "arn:aws:sns:eu-west-1:111122223333:endpoint/GCM/app/id";
/** Each form resolves in the state machine's Region, whichever host it names, as the queue lookup does. */
@ParameterizedTest
@@ -30,7 +32,7 @@ class TaskPermissionsTest {
"444455556666/orders, 444455556666",
"http://localhost:4566/not-an-account/orders, 111122223333"})
void aQueueUrlNamesTheQueueItsLookupReaches(String queueUrl, String account) {
assertEquals(new TaskPermissions("SQS.", List.of(new Permission("sqs:SendMessage",
assertEquals(new TaskPermissions("SQS.AccessDeniedException", List.of(new Permission("sqs:SendMessage",
"arn:aws:sqs:eu-west-1:" + account + ":orders"))),
TaskPermissions.of(integration("sqs:sendMessage"), input(queueUrl), machine("aws")).orElseThrow());
}
@@ -44,25 +46,144 @@ class TaskPermissionsTest {
@Test
void theSdkFormNamesItsOwnErrorPrefix() {
assertEquals("Sqs.", TaskPermissions.of(integration("aws-sdk:sqs:sendMessage"), input("orders"), machine("aws"))
.orElseThrow().errorPrefix());
assertEquals("Sqs.AccessDeniedException", TaskPermissions.of(integration("aws-sdk:sqs:sendMessage"),
input("orders"), machine("aws")).orElseThrow().deniedError());
}
/** SQS answers these with its own validation error once the role has passed. */
@ParameterizedTest
@ValueSource(strings = {"{}", "{\"QueueUrl\":\" \"}", "{\"QueueUrl\":7}"})
void aRequestNamingNoQueueNeedsNoPermission(String request) throws Exception {
assertEquals(new TaskPermissions("SQS.", List.of()),
assertEquals(new TaskPermissions("SQS.AccessDeniedException", List.of()),
TaskPermissions.of(integration("sqs:sendMessage"), MAPPER.readTree(request), machine("aws")).orElseThrow());
}
@ParameterizedTest
@ValueSource(strings = {"lambda:invoke", "apigateway:invoke", "sns:publish", "aws-sdk:sqs:receiveMessage",
"states:startExecution", "dynamodb:putItem", "http:invoke"})
@ValueSource(strings = {"lambda:invoke", "apigateway:invoke", "aws-sdk:sqs:receiveMessage", "aws-sdk:sns:listTopics",
"sns:publishBatch", "aws-sdk:eventbridge:putEvents", "events:listRules", "states:startExecution",
"dynamodb:putItem", "http:invoke"})
void integrationsNotMappedStayUnchecked(String integration) {
assertEquals(Optional.empty(), TaskPermissions.of(integration(integration), input("orders"), machine("aws")));
}
@ParameterizedTest
@CsvSource({"sns:publish,SNS.AuthorizationErrorException", "aws-sdk:sns:publish,Sns.AuthorizationErrorException"})
void aPublishNeedsThePermissionOnItsTopicAndIsRefusedAsSnsRefuses(String integration, String deniedError) throws Exception {
assertEquals(new TaskPermissions(deniedError, List.of(new Permission("sns:Publish", TOPIC))),
TaskPermissions.of(integration(integration), json("{\"TopicArn\":\"" + TOPIC + "\"}"), machine("aws"))
.orElseThrow());
}
@Test
void aPublishToATargetNeedsThePermissionOnTheTarget() throws Exception {
assertEquals(List.of(new Permission("sns:Publish", ENDPOINT)), snsPermissions("{\"TargetArn\":\"" + ENDPOINT + "\"}"));
}
/** {@code SnsService.publish} takes the topic before the target, so that is what it sends to. */
@Test
void aPublishNamingBothNeedsThePermissionOnTheTopic() throws Exception {
assertEquals(List.of(new Permission("sns:Publish", TOPIC)),
snsPermissions("{\"TargetArn\":\"" + ENDPOINT + "\",\"TopicArn\":\"" + TOPIC + "\"}"));
assertEquals(List.of(new Permission("sns:Publish", ENDPOINT)),
snsPermissions("{\"TopicArn\":null,\"TargetArn\":\"" + ENDPOINT + "\"}"));
}
/** {@code SnsService.publish} takes a phone number before any ARN, so a topic in the request is not reached. */
@ParameterizedTest
@ValueSource(strings = {"{\"PhoneNumber\":\"+15555550100\"}",
"{\"PhoneNumber\":\"+15555550100\",\"TopicArn\":\"" + TOPIC + "\"}"})
void anSmsNeedsThePermissionOnAnyResource(String request) throws Exception {
assertEquals(List.of(new Permission("sns:Publish", "*")), snsPermissions(request));
}
/** SNS answers these with its own error once the role has passed: none of them reaches a topic. */
@ParameterizedTest
@ValueSource(strings = {"{}", "{\"TopicArn\":\"orders\"}", "{\"TopicArn\":\"\"}", "{\"TopicArn\":7}",
"{\"TopicArn\":\"arn:aws:sns\"}", "{\"TopicArn\":\"arn:aws:sqs:eu-west-1:111122223333:orders\"}",
"{\"TargetArn\":\"orders\"}", "{\"PhoneNumber\":null}",
"{\"TopicArn\":\"orders\",\"TargetArn\":\"" + ENDPOINT + "\"}"})
void aPublishNamingNoSnsResourceNeedsNoPermission(String request) throws Exception {
assertEquals(List.of(), snsPermissions(request));
}
@Test
void anEntryNamingNoBusNeedsThePermissionOnTheDefaultBus() throws Exception {
assertEquals(new TaskPermissions("EventBridge.AccessDeniedException", List.of(new Permission("events:PutEvents", bus("default")))),
TaskPermissions.of(integration("events:putEvents"), json("{\"Entries\":[{\"Source\":\"a\"}]}"),
machine("aws")).orElseThrow());
}
/** Each form reaches the bus of that name where the call runs, whichever account and Region an ARN names. */
@ParameterizedTest
@CsvSource({
"orders, orders",
"arn:aws:events:eu-west-1:111122223333:event-bus/orders, orders",
"arn:aws-cn:events:cn-north-1:444455556666:event-bus/orders, orders",
"arn:aws:events:eu-west-1:111122223333:event-bus/default, default",
"'', default",
"' ', default",
"arn:aws:s3:::orders, arn:aws:s3:::orders"})
void aBusNameOrArnNamesTheBusItsLookupReaches(String busName, String reached) throws Exception {
assertEquals(List.of(new Permission("events:PutEvents", bus(reached))),
eventsPermissions(MAPPER.writeValueAsString(Map.of("Entries", List.of(Map.of("EventBusName", busName))))));
}
@Test
void aNullOrNonTextBusIsReadAsTheHandlerReadsIt() throws Exception {
assertEquals(List.of(new Permission("events:PutEvents", bus("default"))),
eventsPermissions("{\"Entries\":[{\"EventBusName\":null},{\"EventBusName\":{\"a\":1}}]}"));
assertEquals(List.of(new Permission("events:PutEvents", bus("7"))),
eventsPermissions("{\"Entries\":[{\"EventBusName\":7}]}"));
}
@Test
void entriesNeedOnePermissionForEachDistinctBusInOrder() throws Exception {
assertEquals(List.of(new Permission("events:PutEvents", bus("b")), new Permission("events:PutEvents", bus("default")),
new Permission("events:PutEvents", bus("a"))),
eventsPermissions("{\"Entries\":[{\"EventBusName\":\"b\"},{},{\"EventBusName\":\"a\"},"
+ "{\"EventBusName\":\"" + bus("b") + "\"},{\"EventBusName\":\"default\"}]}"));
}
@Test
void theBusIsInTheStateMachinesPartition() throws Exception {
assertEquals(List.of(new Permission("events:PutEvents",
"arn:aws-cn:events:eu-west-1:" + ACCOUNT + ":event-bus/default")),
TaskPermissions.of(integration("events:putEvents"), json("{\"Entries\":[{}]}"), machine("aws-cn"))
.orElseThrow().permissions());
}
/** A malformed member names no bus, so EventBridge answers it, and the others are still checked. */
@ParameterizedTest
@ValueSource(strings = {"{}", "{\"Entries\":[]}", "{\"Entries\":{\"EventBusName\":\"a\"}}", "{\"Entries\":{\"x\":{\"EventBusName\":\"a\"}}}", "{\"Entries\":\"a\"}",
"{\"Entries\":[7]}", "{\"Entries\":[[{\"EventBusName\":\"a\"}]]}",
"{\"Entries\":[{\"EventBusName\":\"arn:aws:events:\"}]}",
"{\"Entries\":[{\"EventBusName\":\"arn:aws:events:eu-west-1:111122223333:rule/orders\"}]}"})
void entriesNamingNoBusNeedNoPermission(String request) throws Exception {
assertEquals(List.of(), eventsPermissions(request));
}
@Test
void aMalformedEntryLeavesTheOthersChecked() throws Exception {
assertEquals(List.of(new Permission("events:PutEvents", bus("a"))),
eventsPermissions("{\"Entries\":[7,{\"EventBusName\":\"arn:aws:events:\"},{\"EventBusName\":\"a\"}]}"));
}
private static List<Permission> snsPermissions(String request) throws Exception {
return TaskPermissions.of(integration("sns:publish"), json(request), machine("aws")).orElseThrow().permissions();
}
private static List<Permission> eventsPermissions(String request) throws Exception {
return TaskPermissions.of(integration("events:putEvents"), json(request), machine("aws")).orElseThrow().permissions();
}
private static String bus(String name) {
return "arn:aws:events:eu-west-1:" + ACCOUNT + ":event-bus/" + name;
}
private static JsonNode json(String request) throws Exception {
return MAPPER.readTree(request);
}
private static StatesIntegration integration(String tail) {
return StatesIntegration.parse("arn:aws:states:::" + tail).orElseThrow();
}
@@ -17,6 +17,17 @@ import static org.junit.jupiter.api.Assertions.fail;
/** Step Functions calls a test makes as one account, and the state machines it leaves to {@code cleanup}. */
public final class StepFunctionsTestClient {
/** A trust policy letting Step Functions assume the role: what an execution role needs. */
public static final String STATES_TRUST = """
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"sts:AssumeRole",
"Principal":{"Service":"states.amazonaws.com"}}]}
""";
/** A trust policy naming another service, so Step Functions may not assume the role. */
public static final String LAMBDA_TRUST = """
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"sts:AssumeRole",
"Principal":{"Service":"lambda.amazonaws.com"}}]}
""";
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final long POLL_MILLIS = 25;
@@ -66,6 +77,18 @@ public final class StepFunctionsTestClient {
return null;
}
/** Polls until the execution's history holds an event of {@code eventType}. */
public void await(String account, String execution, String eventType, int seconds) throws Exception {
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(seconds);
do {
if (!eventsOfType(account, execution, eventType).isEmpty()) {
return;
}
Thread.sleep(POLL_MILLIS);
} while (System.nanoTime() < deadline);
fail("No " + eventType + " event in " + execution);
}
public List<JsonNode> eventsOfType(String account, String execution, String type) throws Exception {
Response history = request(account, "GetExecutionHistory", Map.of("executionArn", execution));
history.then().statusCode(200);
@@ -78,6 +101,14 @@ public final class StepFunctionsTestClient {
return events;
}
/** A trust policy letting {@code roleArn} assume the role, as a task's {@code Credentials} role needs. */
public static String trusting(String roleArn) {
return """
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":"sts:AssumeRole",
"Principal":{"AWS":"%s"}}]}
""".formatted(roleArn);
}
private void removeMachine(String account, String arn) {
Response executions = request(account, "ListExecutions", Map.of("stateMachineArn", arn));
if (executions.statusCode() == 200) {