feat(stepfunctions): evaluate the execution role on DynamoDB, S3, RDS Data and Redshift Data tasks (#7722)

Under IAM enforcement, these tasks are now authorized as the execution role, or as the Credentials role a state names. DynamoDB is checked as a direct request is: batches on each table, transactions by each item's own action, ListTables on its regional collection and statements by what they do. S3 putObject is checked on the object in the bucket's own partition, and a bucket policy can grant or deny. RDS Data needs the action on the cluster and GetSecretValue on the secret. Redshift Data needs what an EventBridge target needs, and its statement operations are checked on any resource.

A request naming nothing it can reach is left to its service. With enforcement off nothing changes.

Signed-off-by: Hector Ventura <hectorvent@gmail.com>
This commit is contained in:
Hector Ventura authored and GitHub committed 2026-11-07 09:16:40 -04:00
1 parent 08be536e15
commit 87eaaab92d
14 files changed
+1563 -23

No files matched your search

@@ -80,8 +80,10 @@ class LambdaTaskAuthorizerTest {
when(functionPolicies.getResourcePolicies("lambda", OWN_FUNCTION))
.thenReturn(List.of(new ResourcePolicy(null, SFN_ACCOUNT)));
when(functionPolicies.getResourcePolicies(anyString(), anyString(), anyString())).thenCallRealMethod();
when(functionPolicies.resolveResourceArn(anyString(), anyString())).thenCallRealMethod();
bucketPolicies = mock(ResourcePolicyProvider.class);
when(bucketPolicies.getResourcePolicies(anyString(), anyString(), anyString())).thenReturn(List.of());
when(bucketPolicies.resolveResourceArn(anyString(), anyString())).thenCallRealMethod();
Instance<ResourcePolicyProvider> providers = mock(Instance.class);
when(providers.iterator()).thenAnswer(call -> List.of(functionPolicies, bucketPolicies).iterator());
ObjectMapper mapper = new ObjectMapper();
@@ -437,6 +439,23 @@ class LambdaTaskAuthorizerTest {
+ "because no identity-based policy allows the states:DescribeRegions action", refused.cause);
}
/** The ARN the policies see is the one the resource's provider resolves, as a direct request's is. */
@Test
void aPermissionIsEvaluatedOnTheArnItsProviderResolves() {
String moved = "arn:aws-cn:s3:::orders";
when(bucketPolicies.resolveResourceArn("s3", BUCKET)).thenReturn(moved);
roleWithPolicies("""
{"Version":"2012-10-17","Statement":[
{"Effect":"Allow","Action":"s3:PutObject","Resource":"%s"}]}""".formatted(BUCKET));
executionRole(STATES_TRUST);
AslExecutor.FailStateException refused = assertThrows(AslExecutor.FailStateException.class,
() -> authorizer.authorize(machine(), null, "S3.AccessDeniedException", List.of(new Permission("s3:PutObject", BUCKET))));
assertEquals("S3.AccessDeniedException", refused.error);
assertTrue(refused.cause.contains(moved), refused.cause);
}
/** An S3 ARN has no account segment: with no owner known, it is not another account's resource. */
@Test
void anS3ArnWithNoAccountIsNotCrossAccount() {
@@ -0,0 +1,894 @@
package io.floci.aws.services.stepfunctions;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.services.rdsdata.RdsDataService;
import io.floci.aws.services.redshiftdata.RedshiftDataService;
import io.floci.aws.testing.EnforcementFixtures;
import io.floci.aws.testing.IamEnforcementProfile;
import io.floci.aws.testing.PartitionCleanup;
import io.floci.aws.testing.RestAssuredJsonUtils;
import io.floci.aws.testing.StepFunctionsTestClient;
import io.quarkus.test.InjectMock;
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 org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
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.Arguments;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import java.net.URI;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
import static io.floci.aws.testing.EnforcementFixtures.auth;
import static io.floci.aws.testing.StepFunctionsTestClient.LAMBDA_TRUST;
import static io.floci.aws.testing.StepFunctionsTestClient.STATES_TRUST;
import static io.floci.aws.testing.StepFunctionsTestClient.trusting;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* DynamoDB, S3, RDS Data and Redshift Data tasks under IAM enforcement. DynamoDB and S3 run for real, so the tests
* read what a task wrote. The Data APIs need database containers, so their services are mocked, as
* {@code StepFunctionsRdsDataIntegrationTest} and {@code StepFunctionsRedshiftDataIntegrationTest} mock them, and
* reaching the service is what is observed.
*/
@QuarkusTest
@TestProfile(IamEnforcementProfile.class)
class StepFunctionsDataTaskIamIntegrationTest {
private static final String ACCOUNT = "000000000461";
private static final String OTHER_ACCOUNT = "000000000462";
private static final String REGION = "us-east-1";
private static final ObjectMapper MAPPER = new ObjectMapper();
private static final String READ_ONLY = allow("*", "sns:ListTopics");
private static final String ASSUME = allow("*", "sts:AssumeRole");
private static final String SECRET = "arn:aws:secretsmanager:" + REGION + ":" + ACCOUNT + ":secret:db-AbCdEf";
private static final String CLUSTER = "arn:aws:rds:" + REGION + ":" + ACCOUNT + ":cluster:orders";
private static final String REDSHIFT_CLUSTER = "arn:aws:redshift:" + REGION + ":" + ACCOUNT + ":cluster:orders";
@TestHTTPResource
URI baseUri;
@InjectMock
RdsDataService rdsDataService;
@InjectMock
RedshiftDataService redshiftDataService;
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
}
@BeforeEach
void stubDataApis() {
when(rdsDataService.executeStatement(any(JsonNode.class), anyString()))
.thenAnswer(call -> MAPPER.createObjectNode());
when(redshiftDataService.executeStatement(any(JsonNode.class), anyString()))
.thenAnswer(call -> MAPPER.createObjectNode().put("Id", "statement"));
when(redshiftDataService.describeStatement(any(JsonNode.class)))
.thenAnswer(call -> MAPPER.createObjectNode().put("Id", "statement"));
}
static Stream<String> integrations() {
return Stream.of("dynamodb:putItem", "aws-sdk:dynamodb:putItem", "s3:putObject", "aws-sdk:s3:putObject",
"aws-sdk:rdsdata:executeStatement", "aws-sdk:redshiftdata:executeStatement");
}
static Stream<Arguments> integrationsInBothLanguages() {
return integrations().flatMap(integration -> Stream.of(Arguments.of(integration, "jsonpath"),
Arguments.of(integration, "jsonata")));
}
@ParameterizedTest
@MethodSource("integrations")
void anExecutionRoleAllowedTheActionOnTheResourceReachesTheService(String integration) throws Exception {
Target target = target(integration);
Response completed = terminal(start(machine(jsonPathTask(target, null), role(STATES_TRUST, target.grant())), "{}"));
assertEquals("SUCCEEDED", completed.jsonPath().getString("status"), completed.asString());
target.reached().check();
}
@ParameterizedTest
@MethodSource("integrationsInBothLanguages")
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);
Response completed = terminal(start(machine(task, role(STATES_TRUST, READ_ONLY)), "{}"));
assertFailed(completed, target.error());
String cause = completed.jsonPath().getString("cause");
assertTrue(cause.contains(target.action()), cause);
assertTrue(cause.contains(target.resourceArn()), cause);
target.notReached().check();
}
@ParameterizedTest
@MethodSource("integrations")
void theActionOnAnotherResourceDoesNotCount(String integration) throws Exception {
Target target = target(integration);
String role = role(STATES_TRUST, allow(target.resourceArn() + "-elsewhere", target.action()));
assertFailed(terminal(start(machine(jsonPathTask(target, null), role), "{}")), target.error());
target.notReached().check();
}
@ParameterizedTest
@MethodSource("integrations")
void anExecutionRoleThatDoesNotTrustStepFunctionsIsRefusedWhateverItsPolicies(String integration) throws Exception {
Target target = target(integration);
String role = role(LAMBDA_TRUST, target.grant());
Response completed = terminal(start(machine(jsonPathTask(target, null), role), "{}"));
assertFailed(completed, "States.Permissions");
assertTrue(completed.jsonPath().getString("cause").contains(role), completed.asString());
target.notReached().check();
}
@ParameterizedTest
@MethodSource("integrations")
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());
target.notReached().check();
}
@ParameterizedTest
@MethodSource("integrationsInBothLanguages")
void credentialsReachTheServiceAsTheirRoleWithoutTheExecutionRolesPermission(String integration, String form)
throws Exception {
Target target = target(integration);
String executionRole = role(STATES_TRUST, ASSUME);
String taskRole = role(trusting(executionRole), target.grant());
Map<String, Object> task = "jsonata".equals(form) ? jsonataTask(target, "{% $states.input.role %}")
: jsonPathTask(target, taskRole);
String execution = start(machine(task, executionRole), MAPPER.writeValueAsString(Map.of("role", taskRole)));
Response completed = terminal(execution);
assertEquals("SUCCEEDED", completed.jsonPath().getString("status"), completed.asString());
target.reached().check();
assertEquals(taskRole, eventsOfType(execution, "TaskScheduled").getFirst()
.path("taskScheduledEventDetails").path("taskCredentials").path("roleArn").asText());
}
@ParameterizedTest
@MethodSource("integrations")
void aRoleTheExecutionRoleCannotAssumeFailsInTheNestedForm(String integration) throws Exception {
Target target = target(integration);
String executionRole = role(STATES_TRUST, target.grant());
String taskRole = role(STATES_TRUST, target.grant());
Response completed = terminal(start(machine(jsonPathTask(target, taskRole), executionRole), "{}"));
assertFailed(completed, "States.TaskFailed");
assertTrue(completed.jsonPath().getString("cause").contains("is not authorized to assume"), completed.asString());
target.notReached().check();
}
@ParameterizedTest
@MethodSource("integrations")
void aCredentialsRoleNotAllowedTheActionIsRefusedEvenIfTheExecutionRoleIs(String integration) throws Exception {
Target target = target(integration);
String executionRole = role(STATES_TRUST, allowAll());
String taskRole = role(trusting(executionRole), READ_ONLY);
assertFailed(terminal(start(machine(jsonPathTask(target, taskRole), executionRole), "{}")), target.error());
target.notReached().check();
}
@ParameterizedTest
@MethodSource("integrations")
void waitForTaskTokenRefusedDoesNotWaitAndReachesNothing(String integration) throws Exception {
Target target = target(integration);
Map<String, Object> task = new LinkedHashMap<>(jsonPathTask(target, null));
task.put("Resource", "arn:aws:states:::" + integration + ".waitForTaskToken");
task.put("TimeoutSeconds", 60);
assertFailed(terminal(start(machine(task, role(STATES_TRUST, READ_ONLY)), "{}")), target.error());
target.notReached().check();
}
@ParameterizedTest
@MethodSource("integrations")
void waitForTaskTokenAllowedReachesTheServiceAndWaits(String integration) throws Exception {
Target target = target(integration);
Map<String, Object> task = new LinkedHashMap<>(Map.of("Type", "Task",
"Resource", "arn:aws:states:::" + integration + ".waitForTaskToken",
"Parameters", target.tokenArguments(), "TimeoutSeconds", 60, "End", true));
String execution = start(machine(task, role(STATES_TRUST, target.grant())), "{}");
String token = target.token().read();
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
@MethodSource("integrations")
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());
}
/** Each optimized API needs its own action: the same role with every other one is refused. */
@ParameterizedTest
@CsvSource({"getItem,GetItem", "deleteItem,DeleteItem", "updateItem,UpdateItem", "scan,Scan"})
void anOptimizedOperationNeedsItsOwnAction(String api, String operation) throws Exception {
String table = table();
Map<String, Object> arguments = "scan".equals(api) ? Map.of("TableName", table)
: Map.of("TableName", table, "Key", Map.of("pk", Map.of("S", "a")));
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::dynamodb:" + api,
"Parameters", arguments, "End", true);
Response allowed = terminal(start(machine(task, role(STATES_TRUST, allow(tableArn(table), "dynamodb:" + operation))), "{}"));
Response refused = terminal(start(machine(task, role(STATES_TRUST, allow(tableArn(table), "dynamodb:PutItem"))), "{}"));
assertEquals("SUCCEEDED", allowed.jsonPath().getString("status"), allowed.asString());
assertFailed(refused, "DynamoDB.AccessDeniedException");
}
@Test
void anOptimizedApiFlociDoesNotHaveStaysAnUnsupportedResource() throws Exception {
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::dynamodb:query",
"Parameters", Map.of("TableName", "any"), "End", true);
assertFailed(terminal(start(machine(task, role(STATES_TRUST, READ_ONLY)), "{}")), "States.TaskFailed");
}
@ParameterizedTest
@CsvSource({"getItem,GetItem", "deleteItem,DeleteItem", "updateItem,UpdateItem", "scan,Scan", "query,Query"})
void anSdkOperationNeedsItsOwnAction(String api, String operation) throws Exception {
String table = table();
Map<String, Object> arguments = switch (api) {
case "scan" -> Map.of("TableName", table);
case "query" -> Map.of("TableName", table, "KeyConditionExpression", "pk = :k",
"ExpressionAttributeValues", Map.of(":k", Map.of("S", "a")));
default -> Map.of("TableName", table, "Key", Map.of("pk", Map.of("S", "a")));
};
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::aws-sdk:dynamodb:" + api,
"Parameters", arguments, "End", true);
Response allowed = terminal(start(machine(task, role(STATES_TRUST, allow(tableArn(table), "dynamodb:" + operation))), "{}"));
Response refused = terminal(start(machine(task, role(STATES_TRUST, allow(tableArn(table), "dynamodb:PutItem"))), "{}"));
assertEquals("SUCCEEDED", allowed.jsonPath().getString("status"), allowed.asString());
assertFailed(refused, "DynamoDb.AccessDeniedException");
}
@ParameterizedTest
@ValueSource(booleans = {true, false})
void aBatchWriteOnTwoTablesIsRefusedWhenOneIsDeniedAndWritesNothing(boolean firstDenied) throws Exception {
String first = table();
String second = table();
Map<String, Object> task = sdkTask("batchWriteItem", Map.of("RequestItems", Map.of(
first, List.of(Map.of("PutRequest", Map.of("Item", Map.of("pk", Map.of("S", "batch"))))),
second, List.of(Map.of("PutRequest", Map.of("Item", Map.of("pk", Map.of("S", "batch"))))))));
String allowedTable = firstDenied ? second : first;
Response refused = terminal(start(machine(task, role(STATES_TRUST,
allow(tableArn(allowedTable), "dynamodb:BatchWriteItem"))), "{}"));
assertFailed(refused, "DynamoDb.AccessDeniedException");
assertTrue(refused.jsonPath().getString("cause").contains(tableArn(firstDenied ? first : second)), refused.asString());
assertFalse(hasItem(first, "batch"));
assertFalse(hasItem(second, "batch"));
Response allowed = terminal(start(machine(task, role(STATES_TRUST,
allowEach(List.of(tableArn(first), tableArn(second)), "dynamodb:BatchWriteItem"))), "{}"));
assertEquals("SUCCEEDED", allowed.jsonPath().getString("status"), allowed.asString());
assertTrue(hasItem(first, "batch"));
assertTrue(hasItem(second, "batch"));
}
@Test
void aBatchGetNeedsItsActionOnEachTable() throws Exception {
String first = table();
String second = table();
Map<String, Object> keys = Map.of("Keys", List.of(Map.of("pk", Map.of("S", "a"))));
Map<String, Object> task = sdkTask("batchGetItem", Map.of("RequestItems", Map.of(first, keys, second, keys)));
assertFailed(terminal(start(machine(task, role(STATES_TRUST, allow(tableArn(first), "dynamodb:BatchGetItem"))), "{}")),
"DynamoDb.AccessDeniedException");
Response allowed = terminal(start(machine(task, role(STATES_TRUST,
allowEach(List.of(tableArn(first), tableArn(second)), "dynamodb:BatchGetItem"))), "{}"));
assertEquals("SUCCEEDED", allowed.jsonPath().getString("status"), allowed.asString());
}
/** Authorized as each item's own action, never as the transaction's. */
@Test
void aTransactWriteNeedsEachItemsActionOnItsTableAndWritesNothingWhenOneIsDenied() throws Exception {
String first = table();
String second = table();
putItem(second, "gone");
Map<String, Object> task = sdkTask("transactWriteItems", Map.of("TransactItems", List.of(
Map.of("Put", Map.of("TableName", first, "Item", Map.of("pk", Map.of("S", "written")))),
Map.of("Delete", Map.of("TableName", second, "Key", Map.of("pk", Map.of("S", "gone")))))));
Response refused = terminal(start(machine(task, role(STATES_TRUST, allowEachAction(Map.of(
"dynamodb:PutItem", tableArn(first), "dynamodb:DeleteItem", tableArn(first))))), "{}"));
assertFailed(refused, "DynamoDb.AccessDeniedException");
assertTrue(refused.jsonPath().getString("cause").contains("dynamodb:DeleteItem"), refused.asString());
assertTrue(refused.jsonPath().getString("cause").contains(tableArn(second)), refused.asString());
assertFalse(hasItem(first, "written"));
assertTrue(hasItem(second, "gone"));
Response transactVerbOnly = terminal(start(machine(task, role(STATES_TRUST,
allow("*", "dynamodb:TransactWriteItems"))), "{}"));
assertFailed(transactVerbOnly, "DynamoDb.AccessDeniedException");
Response allowed = terminal(start(machine(task, role(STATES_TRUST, allowEachAction(Map.of(
"dynamodb:PutItem", tableArn(first), "dynamodb:DeleteItem", tableArn(second))))), "{}"));
assertEquals("SUCCEEDED", allowed.jsonPath().getString("status"), allowed.asString());
assertTrue(hasItem(first, "written"));
assertFalse(hasItem(second, "gone"));
}
@Test
void aTransactGetNeedsGetItemOnEachTable() throws Exception {
String first = table();
String second = table();
Map<String, Object> key = Map.of("pk", Map.of("S", "a"));
Map<String, Object> task = sdkTask("transactGetItems", Map.of("TransactItems", List.of(
Map.of("Get", Map.of("TableName", first, "Key", key)),
Map.of("Get", Map.of("TableName", second, "Key", key)))));
Response refused = terminal(start(machine(task, role(STATES_TRUST, allow(tableArn(first), "dynamodb:GetItem"))), "{}"));
assertFailed(refused, "DynamoDb.AccessDeniedException");
assertTrue(refused.jsonPath().getString("cause").contains(tableArn(second)), refused.asString());
Response allowed = terminal(start(machine(task, role(STATES_TRUST,
allowEach(List.of(tableArn(first), tableArn(second)), "dynamodb:GetItem"))), "{}"));
assertEquals("SUCCEEDED", allowed.jsonPath().getString("status"), allowed.asString());
}
/** {@code dynamodb:EnclosingOperation} reaches the policy, as it does for a direct transaction. */
@Test
void aTransactionsEnclosingOperationReachesThePolicy() throws Exception {
String table = table();
Map<String, Object> task = sdkTask("transactWriteItems", Map.of("TransactItems", List.of(
Map.of("Put", Map.of("TableName", table, "Item", Map.of("pk", Map.of("S", "conditioned")))))));
String policy = MAPPER.writeValueAsString(Map.of("Version", "2012-10-17", "Statement", List.of(
Map.of("Effect", "Allow", "Action", "dynamodb:PutItem", "Resource", tableArn(table), "Condition",
Map.of("StringEquals", Map.of("dynamodb:EnclosingOperation", "TransactWriteItems"))))));
Response allowed = terminal(start(machine(task, role(STATES_TRUST, policy)), "{}"));
assertEquals("SUCCEEDED", allowed.jsonPath().getString("status"), allowed.asString());
}
@Test
void listTablesNeedsItsRegionalCollection() throws Exception {
String collection = "arn:aws:dynamodb:" + REGION + ":" + ACCOUNT + ":table/*";
Map<String, Object> task = sdkTask("listTables", Map.of());
Response allowed = terminal(start(machine(task, role(STATES_TRUST, allow(collection, "dynamodb:ListTables"))), "{}"));
Response refused = terminal(start(machine(task, role(STATES_TRUST,
allow("arn:aws:dynamodb:" + REGION + ":" + ACCOUNT + ":table/one", "dynamodb:ListTables"))), "{}"));
assertEquals("SUCCEEDED", allowed.jsonPath().getString("status"), allowed.asString());
assertFailed(refused, "DynamoDb.AccessDeniedException");
assertTrue(refused.jsonPath().getString("cause").contains(collection), refused.asString());
}
@Test
void aStatementIsAuthorizedByWhatItDoes() throws Exception {
String table = table();
Map<String, Object> task = sdkTask("executeStatement", Map.of("Statement", "SELECT * FROM \"" + table + "\""));
Response allowed = terminal(start(machine(task, role(STATES_TRUST, allow(tableArn(table), "dynamodb:PartiQLSelect"))), "{}"));
Response refused = terminal(start(machine(task, role(STATES_TRUST, allow(tableArn(table), "dynamodb:ExecuteStatement"))), "{}"));
assertEquals("SUCCEEDED", allowed.jsonPath().getString("status"), allowed.asString());
assertFailed(refused, "DynamoDb.AccessDeniedException");
}
@ParameterizedTest
@CsvSource({"dynamodb:putItem,DynamoDB.", "aws-sdk:dynamodb:putItem,DynamoDb."})
void aTableNamedByItsArnNeedsThePermissionOnThatArn(String integration, String prefix) throws Exception {
String table = table();
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::" + integration,
"Parameters", Map.of("TableName", tableArn(table), "Item", Map.of("pk", Map.of("S", "by-arn"))), "End", true);
assertFailed(terminal(start(machine(task, role(STATES_TRUST, allow(tableArn(table) + "x", "dynamodb:PutItem"))), "{}")),
prefix + "AccessDeniedException");
Response allowed = terminal(start(machine(task, role(STATES_TRUST, allow(tableArn(table), "dynamodb:PutItem"))), "{}"));
assertEquals("SUCCEEDED", allowed.jsonPath().getString("status"), allowed.asString());
}
/** No table policy can grant it, so the role's own policy on that ARN is not enough. */
@Test
void aTableArnOfAnotherAccountIsRefused() throws Exception {
String arn = "arn:aws:dynamodb:" + REGION + ":" + OTHER_ACCOUNT + ":table/" + table();
Map<String, Object> task = sdkTask("putItem", Map.of("TableName", arn, "Item", Map.of("pk", Map.of("S", "a"))));
assertFailed(terminal(start(machine(task, role(STATES_TRUST, allow(arn, "dynamodb:PutItem"))), "{}")),
"DynamoDb.AccessDeniedException");
}
@Test
void anOptimizedRequestNamingNoTableIsAnsweredByDynamoDb() throws Exception {
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::dynamodb:putItem",
"Parameters", Map.of("Item", Map.of("pk", Map.of("S", "a"))), "End", true);
Response completed = terminal(start(machine(task, role(STATES_TRUST, READ_ONLY)), "{}"));
assertEquals("FAILED", completed.jsonPath().getString("status"), completed.asString());
assertFalse(completed.jsonPath().getString("error").endsWith("AccessDeniedException"), completed.asString());
}
@Test
void aTransactionWithNoItemsIsAnsweredByDynamoDb() throws Exception {
Map<String, Object> task = sdkTask("transactWriteItems", Map.of("TransactItems", "not-a-list"));
Response completed = terminal(start(machine(task, role(STATES_TRUST, READ_ONLY)), "{}"));
assertEquals("FAILED", completed.jsonPath().getString("status"), completed.asString());
assertFalse(completed.jsonPath().getString("error").endsWith("AccessDeniedException"), completed.asString());
}
@ParameterizedTest
@ValueSource(strings = {"s3:putObject", "aws-sdk:s3:putObject"})
void aRequestNamingNoBucketIsAnsweredByTheIntegration(String integration) throws Exception {
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::" + integration,
"Parameters", Map.of("Key", "a"), "End", true);
assertFailed(terminal(start(machine(task, role(STATES_TRUST, READ_ONLY)), "{}")), "S3.InvalidRequest");
}
@ParameterizedTest
@ValueSource(strings = {"s3:putObject", "aws-sdk:s3:putObject"})
void aBucketPolicyGrantsARoleWithNoIdentityPermission(String integration) throws Exception {
Target target = target(integration);
String role = role(STATES_TRUST, READ_ONLY);
setBucketPolicy(target.bucket(), bucketStatement("Allow", role, target.bucket()));
Response completed = terminal(start(machine(jsonPathTask(target, null), role), "{}"));
assertEquals("SUCCEEDED", completed.jsonPath().getString("status"), completed.asString());
target.reached().check();
}
@ParameterizedTest
@ValueSource(strings = {"s3:putObject", "aws-sdk:s3:putObject"})
void aBucketPolicyDenyRefusesARoleItsIdentityPolicyAllows(String integration) throws Exception {
Target target = target(integration);
String role = role(STATES_TRUST, target.grant());
setBucketPolicy(target.bucket(), bucketStatement("Deny", role, target.bucket()));
Response completed = terminal(start(machine(jsonPathTask(target, null), role), "{}"));
assertFailed(completed, "S3.AccessDeniedException");
assertTrue(completed.jsonPath().getString("cause").contains("resource-based policy"), completed.asString());
target.notReached().check();
}
/**
* Buckets are per account unless the global namespace is on, so another account's is not one the task reaches.
* With it on, {@code LambdaTaskAuthorizerTest} pins that the bucket's owner makes the call a cross-account one.
*/
@Test
void aBucketOfAnotherAccountIsAnsweredByS3OnceTheRoleIsAllowed() throws Exception {
String bucket = bucket(OTHER_ACCOUNT, REGION);
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::s3:putObject",
"Parameters", Map.of("Bucket", bucket, "Key", "a", "Body", "hello"), "End", true);
assertFailed(terminal(start(machine(task, role(STATES_TRUST, READ_ONLY)), "{}")), "S3.AccessDeniedException");
assertFailed(terminal(start(machine(task, role(STATES_TRUST, allow("arn:aws:s3:::" + bucket + "/a", "s3:PutObject"))), "{}")),
"S3.NoSuchBucket");
}
/** S3's one namespace reaches a bucket of another partition, whose ARN is the one a direct request is checked on. */
@Test
void aBucketInAnotherPartitionIsEvaluatedInItsOwn() throws Exception {
String bucket = bucket(ACCOUNT, "cn-north-1");
String key = "partitioned";
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::s3:putObject",
"Parameters", Map.of("Bucket", bucket, "Key", key, "Body", "hello"), "End", true);
Response refused = terminal(start(machine(task, role(STATES_TRUST,
allow("arn:aws:s3:::" + bucket + "/" + key, "s3:PutObject"))), "{}"));
Response allowed = terminal(start(machine(task, role(STATES_TRUST,
allow("arn:aws-cn:s3:::" + bucket + "/" + key, "s3:PutObject"))), "{}"));
assertFailed(refused, "S3.AccessDeniedException");
assertTrue(refused.jsonPath().getString("cause").contains("arn:aws-cn:s3:::" + bucket), refused.asString());
assertEquals("SUCCEEDED", allowed.jsonPath().getString("status"), allowed.asString());
}
@Test
void aMissingBucketIsAnsweredByS3OnceTheRoleIsAllowed() throws Exception {
String bucket = "sfn-iam-missing-" + UUID.randomUUID();
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::s3:putObject",
"Parameters", Map.of("Bucket", bucket, "Key", "a"), "End", true);
assertFailed(terminal(start(machine(task, role(STATES_TRUST, allow("arn:aws:s3:::" + bucket + "/a", "s3:PutObject"))), "{}")),
"S3.NoSuchBucket");
}
/** The secret is the caller's to read, as AWS's own {@code AmazonRDSDataFullAccess} policy has it. */
@Test
void anRdsDataStatementNeedsTheSecretToo() throws Exception {
Target target = target("aws-sdk:rdsdata:executeStatement");
Response refused = terminal(start(machine(jsonPathTask(target, null), role(STATES_TRUST,
allow(CLUSTER, "rds-data:ExecuteStatement"))), "{}"));
assertFailed(refused, "RdsData.AccessDeniedException");
String cause = refused.jsonPath().getString("cause");
assertTrue(cause.contains("secretsmanager:GetSecretValue"), cause);
assertTrue(cause.contains(SECRET), cause);
target.notReached().check();
}
@ParameterizedTest
@CsvSource({"resourceArn,secretArn", "ResourceArn,SecretArn"})
void anRdsDataRequestIsReadInEitherCasing(String resourceMember, String secretMember) throws Exception {
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::aws-sdk:rdsdata:executeStatement",
"Parameters", Map.of(resourceMember, CLUSTER, secretMember, SECRET, "Sql", "select 1"), "End", true);
assertFailed(terminal(start(machine(task, role(STATES_TRUST, READ_ONLY)), "{}")), "RdsData.AccessDeniedException");
}
@Test
void anRdsDataRequestNamingNoClusterIsAnsweredByTheService() throws Exception {
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::aws-sdk:rdsdata:executeStatement",
"Parameters", Map.of("ResourceArn", "not-an-arn", "SecretArn", SECRET, "Sql", "select 1"), "End", true);
terminal(start(machine(task, role(STATES_TRUST, READ_ONLY)), "{}"));
verify(rdsDataService, timeout(5000)).executeStatement(any(JsonNode.class), anyString());
}
/** The Data API's statement operations act on no resource in the service reference. */
@Test
void aRedshiftDataStatementOperationIsCheckedOnAnyResource() throws Exception {
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::aws-sdk:redshiftdata:describeStatement",
"Parameters", Map.of("Id", "statement"), "End", true);
Response refused = terminal(start(machine(task, role(STATES_TRUST,
allow(REDSHIFT_CLUSTER, "redshift-data:DescribeStatement"))), "{}"));
Response allowed = terminal(start(machine(task, role(STATES_TRUST,
allow("*", "redshift-data:DescribeStatement"))), "{}"));
assertFailed(refused, "RedshiftData.AccessDeniedException");
assertEquals("SUCCEEDED", allowed.jsonPath().getString("status"), allowed.asString());
}
/** The credentials a cluster call authenticates with are the role's to get, as an EventBridge target's are. */
@Test
void aRedshiftDataCallAsADatabaseUserNeedsItsClusterCredentials() throws Exception {
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::aws-sdk:redshiftdata:executeStatement",
"Parameters", Map.of("ClusterIdentifier", "orders", "Database", "dev", "DbUser", "admin", "Sql", "select 1"),
"End", true);
Response refused = terminal(start(machine(task, role(STATES_TRUST,
allow(REDSHIFT_CLUSTER, "redshift-data:ExecuteStatement"))), "{}"));
assertFailed(refused, "RedshiftData.AccessDeniedException");
assertTrue(refused.jsonPath().getString("cause").contains("redshift:GetClusterCredentials"), refused.asString());
verify(redshiftDataService, never()).executeStatement(any(JsonNode.class), anyString());
Response allowed = terminal(start(machine(task, role(STATES_TRUST, allowEachAction(Map.of(
"redshift-data:ExecuteStatement", REDSHIFT_CLUSTER, "redshift:GetClusterCredentials", "*")))), "{}"));
assertEquals("SUCCEEDED", allowed.jsonPath().getString("status"), allowed.asString());
}
@Test
void aRedshiftDataRequestNamingNoTargetIsAnsweredByTheService() throws Exception {
Map<String, Object> task = Map.of("Type", "Task", "Resource", "arn:aws:states:::aws-sdk:redshiftdata:executeStatement",
"Parameters", Map.of("Sql", "select 1"), "End", true);
terminal(start(machine(task, role(STATES_TRUST, READ_ONLY)), "{}"));
verify(redshiftDataService, timeout(5000)).executeStatement(any(JsonNode.class), anyString());
}
private static String allow(String resource, String... actions) {
return allowEach(List.of(resource), actions);
}
private static String allowEach(List<String> resources, String... actions) {
return policy(List.of(Map.of("Effect", "Allow", "Action", List.of(actions), "Resource", resources)));
}
private static String allowEachAction(Map<String, String> resourceOfAction) {
List<Map<String, Object>> statements = new ArrayList<>();
resourceOfAction.forEach((action, resource) -> statements.add(
Map.of("Effect", "Allow", "Action", action, "Resource", resource)));
return policy(statements);
}
private static String allowAll() {
return allow("*", "dynamodb:*", "s3:*", "rds-data:*", "redshift-data:*", "secretsmanager:*", "sts:AssumeRole");
}
private static String policy(List<Map<String, Object>> statements) {
try {
return MAPPER.writeValueAsString(Map.of("Version", "2012-10-17", "Statement", statements));
} catch (Exception e) {
throw new IllegalStateException(e);
}
}
private static String bucketStatement(String effect, String principal, String bucket) {
return """
{"Version":"2012-10-17","Statement":[{"Effect":"%s","Principal":{"AWS":"%s"},
"Action":"s3:PutObject","Resource":"arn:aws:s3:::%s/*"}]}
""".formatted(effect, principal, bucket);
}
private static void assertFailed(Response completed, String error) {
assertEquals("FAILED", completed.jsonPath().getString("status"), completed.asString());
assertEquals(error, completed.jsonPath().getString("error"), completed.asString());
}
@FunctionalInterface
private interface Check {
void check() throws Exception;
}
@FunctionalInterface
private interface TokenRead {
String read() throws Exception;
}
/**
* What a task acts on and how to observe it.
*
* @param grant the policy that allows everything the call needs
*/
private record Target(String integration, String error, String action, String resourceArn, String grant,
Map<String, Object> arguments, Map<String, Object> tokenArguments, Check reached,
Check notReached, TokenRead token, String bucket) {
}
private Target target(String integration) {
return switch (integration) {
case "dynamodb:putItem" -> tableTarget(integration, "DynamoDB.");
case "aws-sdk:dynamodb:putItem" -> tableTarget(integration, "DynamoDb.");
case "s3:putObject", "aws-sdk:s3:putObject" -> objectTarget(integration);
case "aws-sdk:rdsdata:executeStatement" -> rdsDataTarget(integration);
case "aws-sdk:redshiftdata:executeStatement" -> redshiftDataTarget(integration);
default -> throw new IllegalArgumentException(integration);
};
}
private Target tableTarget(String integration, String prefix) {
String table = table();
return new Target(integration, prefix + "AccessDeniedException", "dynamodb:PutItem", tableArn(table),
allow(tableArn(table), "dynamodb:PutItem"),
Map.of("TableName", table, "Item", Map.of("pk", Map.of("S", "hello"))),
Map.of("TableName", table, "Item", Map.of("pk", Map.of("S.$", "$$.Task.Token"))),
() -> assertTrue(hasItem(table, "hello")),
() -> assertFalse(hasItem(table, "hello")),
() -> awaitScannedKey(table), null);
}
private Target objectTarget(String integration) {
String bucket = bucket(ACCOUNT, REGION);
String key = "orders/a+b:c.txt";
String objectArn = "arn:aws:s3:::" + bucket + "/" + key;
return new Target(integration, "S3.AccessDeniedException", "s3:PutObject", objectArn,
allow(objectArn, "s3:PutObject"),
Map.of("Bucket", bucket, "Key", key, "Body", "hello"),
Map.of("Bucket", bucket, "Key", key, "Body.$", "$$.Task.Token"),
() -> assertEquals("hello", object(bucket, key).asString()),
() -> assertEquals(404, object(bucket, key).statusCode()),
() -> awaitObject(bucket, key), bucket);
}
private Target rdsDataTarget(String integration) {
return new Target(integration, "RdsData.AccessDeniedException", "rds-data:ExecuteStatement", CLUSTER,
allowEachAction(Map.of("rds-data:ExecuteStatement", CLUSTER, "secretsmanager:GetSecretValue", SECRET)),
Map.of("ResourceArn", CLUSTER, "SecretArn", SECRET, "Sql", "select 1"),
Map.of("ResourceArn", CLUSTER, "SecretArn", SECRET, "Sql.$", "$$.Task.Token"),
() -> verify(rdsDataService, timeout(5000)).executeStatement(any(JsonNode.class), anyString()),
() -> verify(rdsDataService, never()).executeStatement(any(JsonNode.class), anyString()),
() -> {
ArgumentCaptor<JsonNode> sent = ArgumentCaptor.forClass(JsonNode.class);
verify(rdsDataService, timeout(5000)).executeStatement(sent.capture(), anyString());
return sent.getValue().path("sql").asText();
}, null);
}
private Target redshiftDataTarget(String integration) {
return new Target(integration, "RedshiftData.AccessDeniedException", "redshift-data:ExecuteStatement",
REDSHIFT_CLUSTER, allowEachAction(Map.of("redshift-data:ExecuteStatement", REDSHIFT_CLUSTER,
"secretsmanager:GetSecretValue", SECRET)),
Map.of("ClusterIdentifier", "orders", "SecretArn", SECRET, "Database", "dev", "Sql", "select 1"),
Map.of("ClusterIdentifier", "orders", "SecretArn", SECRET, "Database", "dev", "Sql.$", "$$.Task.Token"),
() -> verify(redshiftDataService, timeout(5000)).executeStatement(any(JsonNode.class), anyString()),
() -> verify(redshiftDataService, never()).executeStatement(any(JsonNode.class), anyString()),
() -> {
ArgumentCaptor<JsonNode> sent = ArgumentCaptor.forClass(JsonNode.class);
verify(redshiftDataService, timeout(5000)).executeStatement(sent.capture(), anyString());
return sent.getValue().path("Sql").asText();
}, null);
}
private String table() {
String name = "sfn-iam-" + UUID.randomUUID();
dynamoDb("CreateTable", Map.of("TableName", name, "BillingMode", "PAY_PER_REQUEST",
"AttributeDefinitions", List.of(Map.of("AttributeName", "pk", "AttributeType", "S")),
"KeySchema", List.of(Map.of("AttributeName", "pk", "KeyType", "HASH"))));
cleanup.register(() -> dynamoDb("DeleteTable", Map.of("TableName", name)));
return name;
}
private static String tableArn(String table) {
return "arn:aws:dynamodb:" + REGION + ":" + ACCOUNT + ":table/" + table;
}
private void putItem(String table, String key) {
dynamoDb("PutItem", Map.of("TableName", table, "Item", Map.of("pk", Map.of("S", key))));
}
private boolean hasItem(String table, String key) throws Exception {
Response found = dynamoDb("GetItem", Map.of("TableName", table, "Key", Map.of("pk", Map.of("S", key))));
return MAPPER.readTree(found.asString()).has("Item");
}
private String awaitScannedKey(String table) throws Exception {
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
do {
JsonNode items = MAPPER.readTree(dynamoDb("Scan", Map.of("TableName", table)).asString()).path("Items");
if (!items.isEmpty()) {
return items.get(0).path("pk").path("S").asText();
}
Thread.sleep(25);
} while (System.nanoTime() < deadline);
fail("Nothing reached " + table);
return null;
}
private Response dynamoDb(String action, Map<String, ?> input) {
Response response = sfn().json(ACCOUNT, "dynamodb", "DynamoDB_20120810." + action, "application/x-amz-json-1.0", input);
response.then().statusCode(200);
return response;
}
private String bucket(String account, String region) {
String name = "sfn-iam-" + UUID.randomUUID();
String authorization = auth(account, region, "s3");
given().port(baseUri.getPort()).header("Authorization", authorization)
.body(REGION.equals(region) ? "" : "<CreateBucketConfiguration><LocationConstraint>" + region
+ "</LocationConstraint></CreateBucketConfiguration>")
.put("/" + name).then().statusCode(200);
cleanup.register(() -> EnforcementFixtures.removeBucket(baseUri.getPort(), authorization, name));
return name;
}
private void setBucketPolicy(String bucket, String policy) {
given().port(baseUri.getPort()).header("Authorization", auth(ACCOUNT, "s3")).body(policy)
.queryParam("policy", "").put("/" + bucket).then().statusCode(200);
}
private Response object(String bucket, String key) {
return given().port(baseUri.getPort()).header("Authorization", auth(ACCOUNT, "s3"))
.urlEncodingEnabled(false).get("/" + bucket + "/" + key.replace("+", "%2B").replace(":", "%3A"));
}
private String awaitObject(String bucket, String key) throws Exception {
long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5);
do {
Response found = object(bucket, key);
if (found.statusCode() == 200) {
return found.asString();
}
Thread.sleep(25);
} while (System.nanoTime() < deadline);
fail("Nothing reached " + bucket + "/" + key);
return null;
}
private static Map<String, Object> sdkTask(String api, Map<String, ?> arguments) {
return Map.of("Type", "Task", "Resource", "arn:aws:states:::aws-sdk:dynamodb:" + api,
"Parameters", arguments, "End", true);
}
private Map<String, Object> jsonPathTask(Target target, String taskRole) {
Map<String, Object> task = new LinkedHashMap<>(Map.of("Type", "Task",
"Resource", "arn:aws:states:::" + target.integration(),
"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:::" + target.integration(),
"Arguments", target.arguments(), "End", true));
if (roleExpression != null) {
task.put("Credentials", Map.of("RoleArn", roleExpression));
}
return task;
}
private String role(String trust, String policy) {
return EnforcementFixtures.createRole(cleanup, baseUri.getPort(), ACCOUNT, "/",
"sfn-data-" + 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-data-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 sfn().terminal(ACCOUNT, execution, 10);
}
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);
}
}
@@ -33,6 +33,7 @@ import java.util.UUID;
import static io.floci.aws.testing.EnforcementFixtures.auth;
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.argThat;
@@ -256,6 +257,60 @@ class StepFunctionsTaskCredentialsIntegrationTest {
assertEquals(taskRole, taskScheduled(execution).path("taskCredentials").path("roleArn").asText());
}
/** Nothing evaluates a role that does not exist, nor a writer no policy allows. */
@ParameterizedTest
@ValueSource(strings = {"dynamodb:putItem", "aws-sdk:dynamodb:putItem", "s3:putObject", "aws-sdk:s3:putObject"})
void dataCredentialsRetainDefaultIamDisabledBehavior(String integration) throws Exception {
String name = "credentials-" + UUID.randomUUID();
Map<String, Object> arguments;
if (integration.contains("dynamodb")) {
dynamoDb("CreateTable", Map.of("TableName", name, "BillingMode", "PAY_PER_REQUEST",
"AttributeDefinitions", List.of(Map.of("AttributeName", "pk", "AttributeType", "S")),
"KeySchema", List.of(Map.of("AttributeName", "pk", "KeyType", "HASH"))));
cleanup.register(() -> dynamoDb("DeleteTable", Map.of("TableName", name)));
arguments = Map.of("TableName", name, "Item", Map.of("pk", Map.of("S", "a")));
} else {
given().port(baseUri.getPort()).header("Authorization", auth(SOURCE, "s3")).put("/" + name)
.then().statusCode(200);
cleanup.register(() -> EnforcementFixtures.removeBucket(baseUri.getPort(), auth(SOURCE, "s3"), name));
arguments = Map.of("Bucket", name, "Key", "a", "Body", "hello");
}
assertDataTaskIsNotEvaluated(integration, arguments, "SUCCEEDED");
}
/** Past authorization, the service answers: these name a database nothing has, so they fail with its own error. */
@ParameterizedTest
@ValueSource(strings = {"aws-sdk:rdsdata:executeStatement", "aws-sdk:redshiftdata:executeStatement"})
void dataApiCredentialsRetainDefaultIamDisabledBehavior(String integration) throws Exception {
Map<String, Object> arguments = integration.contains("rdsdata")
? Map.of("ResourceArn", "arn:aws:rds:us-east-1:" + SOURCE + ":cluster:no-such-cluster",
"SecretArn", "arn:aws:secretsmanager:us-east-1:" + SOURCE + ":secret:no-such-secret",
"Sql", "select 1")
: Map.of("ClusterIdentifier", "no-such-cluster", "Database", "dev", "Sql", "select 1");
assertDataTaskIsNotEvaluated(integration, arguments, "FAILED");
}
private void assertDataTaskIsNotEvaluated(String integration, Map<String, Object> arguments, String status)
throws Exception {
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(status, done.jsonPath().getString("status"), done.asString());
String error = String.valueOf(done.jsonPath().getString("error"));
assertFalse(error.endsWith("AccessDeniedException") || "States.Permissions".equals(error), done.asString());
assertEquals(taskRole, taskScheduled(execution).path("taskCredentials").path("roleArn").asText());
}
private void dynamoDb(String action, Map<String, ?> input) {
sfn().json(SOURCE, "dynamodb", "DynamoDB_20120810." + action, "application/x-amz-json-1.0", input)
.then().statusCode(200);
}
@Test
void credentialsThatDoNotResolveStillPassWithEnforcementOff() throws Exception {
SqsTestQueue queue = SqsTestQueue.create(sqsService, SOURCE, "us-east-1", "credentials-");
@@ -2,6 +2,7 @@ package io.floci.aws.services.stepfunctions;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.floci.aws.core.common.StoredResourceArns;
import io.floci.aws.services.stepfunctions.LambdaTaskAuthorizer.Permission;
import io.floci.aws.services.stepfunctions.model.StateMachine;
import org.junit.jupiter.api.Test;
@@ -14,12 +15,16 @@ import java.util.Map;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
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 REDSHIFT_CLUSTER = "arn:aws:redshift:eu-west-1:111122223333:cluster: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. */
@@ -61,7 +66,7 @@ class TaskPermissionsTest {
@ParameterizedTest
@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"})
"dynamodb:query", "http:invoke"})
void integrationsNotMappedStayUnchecked(String integration) {
assertEquals(Optional.empty(), TaskPermissions.of(integration(integration), input("orders"), machine("aws")));
}
@@ -168,6 +173,241 @@ class TaskPermissionsTest {
eventsPermissions("{\"Entries\":[7,{\"EventBusName\":\"arn:aws:events:\"},{\"EventBusName\":\"a\"}]}"));
}
@ParameterizedTest
@CsvSource({"putItem,PutItem", "getItem,GetItem", "deleteItem,DeleteItem", "updateItem,UpdateItem", "scan,Scan"})
void anOptimizedDynamoDbTaskNeedsItsActionOnTheTable(String api, String operation) throws Exception {
assertEquals(TaskPermissions.accessDenied("DynamoDB.", List.of(new Permission("dynamodb:" + operation, table("orders")))),
TaskPermissions.of(integration("dynamodb:" + api), json("{\"TableName\":\"orders\"}"), machine("aws"))
.orElseThrow());
}
@ParameterizedTest
@ValueSource(strings = {"{}", "{\"TableName\":\" \"}", "{\"TableName\":{}}"})
void anOptimizedDynamoDbTaskNamingNoTableNeedsNoPermission(String request) throws Exception {
assertEquals(TaskPermissions.accessDenied("DynamoDB.", List.of()),
TaskPermissions.of(integration("dynamodb:putItem"), json(request), machine("aws")).orElseThrow());
}
@Test
void anOptimizedDynamoDbApiNotServedStaysUnchecked() throws Exception {
assertEquals(Optional.empty(), TaskPermissions.of(integration("dynamodb:query"), json("{\"TableName\":\"t\"}"), machine("aws")));
}
@Test
void aTableNamedByItsArnIsEvaluatedAsGiven() throws Exception {
String arn = "arn:aws:dynamodb:eu-west-1:444455556666:table/orders";
assertEquals(List.of(new Permission("dynamodb:PutItem", arn)), sdkPermissions("putItem", "{\"TableName\":\"" + arn + "\"}"));
assertEquals(List.of(new Permission("dynamodb:PutItem", arn)),
TaskPermissions.of(integration("dynamodb:putItem"), json("{\"TableName\":\"" + arn + "\"}"), machine("aws"))
.orElseThrow().permissions());
}
@Test
void anSdkDynamoDbTaskNamesItsOwnPrefix() throws Exception {
assertEquals("DynamoDb.AccessDeniedException", TaskPermissions.of(integration("aws-sdk:dynamodb:getItem"),
json("{}"), machine("aws")).orElseThrow().deniedError());
}
@Test
void aBatchNeedsItsActionOnEachTable() throws Exception {
assertEquals(List.of(new Permission("dynamodb:BatchWriteItem", table("a")),
new Permission("dynamodb:BatchWriteItem", table("b"))),
sdkPermissions("batchWriteItem", "{\"RequestItems\":{\"a\":[],\"b\":[]}}"));
assertEquals(List.of(new Permission("dynamodb:BatchGetItem", table("a")),
new Permission("dynamodb:BatchGetItem", table("b"))),
sdkPermissions("batchGetItem", "{\"RequestItems\":{\"a\":{},\"b\":{}}}"));
}
@Test
void aTransactWriteNeedsEachItemsOwnActionOnItsTable() throws Exception {
Map<String, List<String>> enclosing = Map.of("dynamodb:EnclosingOperation", List.of("TransactWriteItems"));
assertEquals(List.of(new Permission("dynamodb:PutItem", table("aaa"), enclosing),
new Permission("dynamodb:DeleteItem", table("bbb"), enclosing),
new Permission("dynamodb:UpdateItem", table("aaa"), enclosing),
new Permission("dynamodb:ConditionCheckItem", table("ccc"), enclosing)),
sdkPermissions("transactWriteItems", """
{"TransactItems":[{"Put":{"TableName":"aaa"}},{"Delete":{"TableName":"bbb"}},
{"Update":{"TableName":"aaa"}},{"ConditionCheck":{"TableName":"ccc"}}]}"""));
}
@Test
void aTransactGetNeedsGetItemOnEachTable() throws Exception {
Map<String, List<String>> enclosing = Map.of("dynamodb:EnclosingOperation", List.of("TransactGetItems"));
assertEquals(List.of(new Permission("dynamodb:GetItem", table("aaa"), enclosing),
new Permission("dynamodb:GetItem", table("bbb"), enclosing)),
sdkPermissions("transactGetItems", "{\"TransactItems\":[{\"Get\":{\"TableName\":\"aaa\"}},{\"Get\":{\"TableName\":\"bbb\"}}]}"));
}
/** Execution resolves an ARN within the request's account, so that is the table the item is checked on. */
@Test
void aTransactItemNamedByAnArnIsTheTableItsExecutionReaches() throws Exception {
assertEquals(List.of(new Permission("dynamodb:PutItem", table("aaa"),
Map.of("dynamodb:EnclosingOperation", List.of("TransactWriteItems")))),
sdkPermissions("transactWriteItems", """
{"TransactItems":[{"Put":{"TableName":"arn:aws:dynamodb:eu-west-1:444455556666:table/aaa"}}]}"""));
}
@ParameterizedTest
@ValueSource(strings = {"{}", "{\"TransactItems\":\"x\"}", "{\"TransactItems\":[{\"Put\":{}}]}",
"{\"TransactItems\":[{\"Get\":{\"TableName\":\"aaa\"}}]}",
"{\"TransactItems\":[{\"Put\":{\"TableName\":\"arn:aws:dynamodb:ap-south-1:444455556666:table/aaa\"}}]}",
"{\"TransactItems\":[{\"Put\":{\"TableName\":\"a\"}}]}",
"{\"TransactItems\":[{\"Put\":{\"TableName\":\"arn:aws:s3:::aaa\"}}]}"})
void aTransactionNamingNoTableItCanReachNeedsNoPermission(String request) throws Exception {
assertEquals(List.of(), sdkPermissions("transactWriteItems", request));
}
@Test
void listTablesNeedsItsRegionalCollection() throws Exception {
assertEquals(List.of(new Permission("dynamodb:ListTables", table("*"))), sdkPermissions("listTables", "{}"));
}
@Test
void anOperationNamingNoTableIsCheckedOnAnyResource() throws Exception {
assertEquals(List.of(new Permission("dynamodb:DescribeLimits", "*")), sdkPermissions("describeLimits", "{}"));
}
@Test
void aStatementIsAuthorizedByWhatItDoes() throws Exception {
assertEquals(List.of(new Permission("dynamodb:PartiQLSelect", table("a"))),
sdkPermissions("executeStatement", "{\"Statement\":\"SELECT * FROM \\\"a\\\"\"}"));
assertEquals(List.of(new Permission("dynamodb:PartiQLInsert", table("a"))),
sdkPermissions("executeStatement", "{\"Statement\":\"INSERT INTO \\\"a\\\" VALUE {'pk':'x'}\"}"));
}
@Test
void aResourceThatIsNotAnArnNeedsNoPermission() throws Exception {
assertEquals(List.of(), sdkPermissions("tagResource", "{\"ResourceArn\":\"not-an-arn\"}"));
}
@ParameterizedTest
@ValueSource(strings = {"s3:putObject", "aws-sdk:s3:putObject"})
void anS3PutNeedsThePermissionOnTheObjectInTheMachinesPartition(String tail) throws Exception {
String request = "{\"Bucket\":\"orders\",\"Key\":\"a/b:c+d.txt\"}";
assertEquals(TaskPermissions.accessDenied("S3.", List.of(new Permission("s3:PutObject", "arn:aws:s3:::orders/a/b:c+d.txt"))),
TaskPermissions.of(integration(tail), json(request), machine("aws")).orElseThrow());
assertEquals(List.of(new Permission("s3:PutObject", "arn:aws-cn:s3:::orders/a/b:c+d.txt")),
TaskPermissions.of(integration(tail), json(request), machine("aws-cn")).orElseThrow().permissions());
}
@ParameterizedTest
@ValueSource(strings = {"{}", "{\"Bucket\":\"orders\"}", "{\"Key\":\"a\"}", "{\"Bucket\":\"\",\"Key\":\"a\"}",
"{\"Bucket\":null,\"Key\":\"a\"}", "{\"Bucket\":\"orders\",\"Key\":null}"})
void anS3PutNamingNoObjectNeedsNoPermission(String request) throws Exception {
assertEquals(TaskPermissions.accessDenied("S3.", List.of()),
TaskPermissions.of(integration("s3:putObject"), json(request), machine("aws")).orElseThrow());
}
@Test
void anS3ApiNotServedStaysUnchecked() throws Exception {
assertEquals(Optional.empty(), TaskPermissions.of(integration("s3:getObject"), json("{}"), machine("aws")));
}
@ParameterizedTest
@ValueSource(strings = {"{\"ResourceArn\":\"%s\",\"SecretArn\":\"%s\"}", "{\"resourceArn\":\"%s\",\"secretArn\":\"%s\"}"})
void anRdsDataStatementNeedsItsActionOnTheClusterAndTheSecret(String template) throws Exception {
String cluster = "arn:aws:rds:eu-west-1:" + ACCOUNT + ":cluster:orders";
String secret = "arn:aws:secretsmanager:eu-west-1:" + ACCOUNT + ":secret:db";
assertEquals(TaskPermissions.accessDenied("RdsData.", List.of(new Permission("rds-data:ExecuteStatement", cluster),
new Permission("secretsmanager:GetSecretValue", secret))),
TaskPermissions.of(integration("aws-sdk:rdsdata:executeStatement"), json(template.formatted(cluster, secret)),
machine("aws")).orElseThrow());
}
@Test
void anRdsDataStatementWithNoSecretArnNeedsOnlyItsAction() throws Exception {
String cluster = "arn:aws:rds:eu-west-1:" + ACCOUNT + ":cluster:orders";
assertEquals(List.of(new Permission("rds-data:ExecuteStatement", cluster)),
TaskPermissions.of(integration("aws-sdk:rdsdata:executeStatement"),
json("{\"ResourceArn\":\"" + cluster + "\",\"SecretArn\":\"db\"}"), machine("aws"))
.orElseThrow().permissions());
}
@ParameterizedTest
@ValueSource(strings = {"{}", "{\"ResourceArn\":\"orders\",\"SecretArn\":\"arn:aws:secretsmanager:eu-west-1:1:secret:db\"}"})
void anRdsDataStatementNamingNoClusterNeedsNoPermission(String request) throws Exception {
assertEquals(TaskPermissions.accessDenied("RdsData.", List.of()),
TaskPermissions.of(integration("aws-sdk:rdsdata:executeStatement"), json(request), machine("aws")).orElseThrow());
}
@Test
void anRdsDataApiNotServedStaysUnchecked() throws Exception {
assertEquals(Optional.empty(), TaskPermissions.of(integration("aws-sdk:rdsdata:beginTransaction"), json("{}"), machine("aws")));
}
@ParameterizedTest
@CsvSource({"describeStatement,DescribeStatement", "cancelStatement,CancelStatement",
"getStatementResult,GetStatementResult", "getStatementResultV2,GetStatementResult",
"listStatements,ListStatements", "listSessions,ListSessions"})
void aRedshiftDataOperationOnNoResourceIsCheckedOnAnyResource(String api, String action) throws Exception {
assertEquals(TaskPermissions.accessDenied("RedshiftData.", List.of(new Permission("redshift-data:" + action, "*"))),
TaskPermissions.of(integration("aws-sdk:redshiftdata:" + api), json("{\"Id\":\"s\"}"), machine("aws")).orElseThrow());
}
@ParameterizedTest
@CsvSource({"executeStatement,ExecuteStatement", "batchExecuteStatement,BatchExecuteStatement",
"listDatabases,ListDatabases", "listSchemas,ListSchemas", "listTables,ListTables", "describeTable,DescribeTable"})
void aRedshiftDataCallOnAClusterNeedsItsActionAndItsSecret(String api, String operation) throws Exception {
String secret = "arn:aws:secretsmanager:eu-west-1:" + ACCOUNT + ":secret:db";
assertEquals(List.of(new Permission("redshift-data:" + operation, REDSHIFT_CLUSTER),
new Permission("secretsmanager:GetSecretValue", secret)),
redshiftPermissions(api, "{\"ClusterIdentifier\":\"orders\",\"SecretArn\":\"" + secret + "\"}", null));
}
@Test
void aRedshiftDataCallAsADatabaseUserNeedsItsClusterCredentials() throws Exception {
String prefix = "arn:aws:redshift:eu-west-1:" + ACCOUNT;
assertEquals(List.of(new Permission("redshift-data:ExecuteStatement", REDSHIFT_CLUSTER),
new Permission("redshift:GetClusterCredentials", prefix + ":dbuser:orders/admin"),
new Permission("redshift:GetClusterCredentials", prefix + ":dbname:orders/dev")),
redshiftPermissions("executeStatement",
"{\"ClusterIdentifier\":\"orders\",\"DbUser\":\"admin\",\"Database\":\"dev\"}", null));
}
@Test
void aRedshiftDataCallOnAWorkgroupIsEvaluatedOnTheOneItsNameFinds() throws Exception {
String workgroup = "arn:aws:redshift-serverless:eu-west-1:" + ACCOUNT + ":workgroup/1234";
StoredResourceArns stored = mock(StoredResourceArns.class);
when(stored.findByMembers(eq("workgroup"), eq(List.of("analytics")), eq("eu-west-1"))).thenReturn(Optional.of(workgroup));
assertEquals(List.of(new Permission("redshift-data:ExecuteStatement", workgroup),
new Permission("redshift-serverless:GetCredentials", workgroup)),
redshiftPermissions("executeStatement", "{\"WorkgroupName\":\"analytics\"}", stored));
}
@Test
void aRedshiftDataCallOnASessionIsEvaluatedOnItsTarget() throws Exception {
StoredResourceArns stored = mock(StoredResourceArns.class);
when(stored.through("SessionId", "session", "eu-west-1")).thenReturn(List.of(REDSHIFT_CLUSTER));
assertEquals(List.of(new Permission("redshift-data:ExecuteStatement", REDSHIFT_CLUSTER)),
redshiftPermissions("executeStatement", "{\"SessionId\":\"session\"}", stored));
}
@ParameterizedTest
@CsvSource(delimiter = '|', value = {"executeStatement|{}", "executeStatement|[]", "executeSql|{\"ClusterIdentifier\":\"c\"}",
"noSuchOperation|{}"})
void aRedshiftDataCallNamingNothingItNeedsNeedsNoPermission(String api, String request) throws Exception {
assertEquals(TaskPermissions.accessDenied("RedshiftData.", List.of()),
TaskPermissions.of(integration("aws-sdk:redshiftdata:" + api), json(request), machine("aws")).orElseThrow());
}
@Test
void aRedshiftDataSuffixIsNotServedAndStaysUnchecked() throws Exception {
assertEquals(Optional.empty(), TaskPermissions.of(integration("aws-sdk:redshiftdata:executeStatement.sync"), json("{}"), machine("aws")));
}
private static List<Permission> sdkPermissions(String api, String request) throws Exception {
return TaskPermissions.of(integration("aws-sdk:dynamodb:" + api), json(request), machine("aws")).orElseThrow().permissions();
}
private static List<Permission> redshiftPermissions(String api, String request, StoredResourceArns stored) throws Exception {
return TaskPermissions.of(integration("aws-sdk:redshiftdata:" + api), json(request), machine("aws"), stored)
.orElseThrow().permissions();
}
private static String table(String name) {
return "arn:aws:dynamodb:eu-west-1:" + ACCOUNT + ":table/" + name;
}
private static List<Permission> snsPermissions(String request) throws Exception {
return TaskPermissions.of(integration("sns:publish"), json(request), machine("aws")).orElseThrow().permissions();
}
@@ -40,14 +40,26 @@ public final class StepFunctionsTestClient {
}
public Response request(String account, String action, Map<String, ?> input) {
return given().port(port).header("Authorization", auth(account, "states"))
.header("X-Amz-Target", "AWSStepFunctions." + action)
.contentType("application/x-amz-json-1.0").body(MAPPER.valueToTree(input).toString()).post("/");
return json(account, "states", "AWSStepFunctions." + action, "application/x-amz-json-1.0", input);
}
/**
* A JSON-protocol request to the service a task calls, signed as {@code account} for {@code signingName}, so a
* test reads and sets up the resources a task reaches in the account its execution runs in.
*/
public Response json(String account, String signingName, String target, String contentType, Map<String, ?> body) {
return given().port(port).header("Authorization", auth(account, signingName))
.header("X-Amz-Target", target)
.contentType(contentType).body(MAPPER.valueToTree(body).toString()).post("/");
}
/** Creates a state machine in us-east-1, which the end of the test stops and deletes. */
public String createMachine(String account, String namePrefix, String definition, String type, String role) {
String name = namePrefix + UUID.randomUUID();
return createMachineNamed(account, namePrefix + UUID.randomUUID(), definition, type, role);
}
/** {@link #createMachine} under the exact {@code name}, for a test that needs the machine's ARN up front. */
public String createMachineNamed(String account, String name, String definition, String type, String role) {
String arn = "arn:aws:states:us-east-1:" + account + ":stateMachine:" + name;
cleanup.register(() -> removeMachine(account, arn));
Response created = request(account, "CreateStateMachine", Map.of(