fix(iam): check Redshift Data API requests on the cluster or workgroup they name (#7234)

Under IAM enforcement, Redshift Data API requests were checked on `*`,
so a grant scoped to a cluster or a serverless workgroup was refused
and a deny scoped to one never applied.

The Service Reference gives ExecuteStatement, BatchExecuteStatement,
DescribeTable, ListDatabases, ListSchemas and ListTables two resource
types: Redshift's `cluster:${ClusterName}`, which ClusterIdentifier
fills, and Redshift Serverless's `workgroup/${WorkgroupId}`. A
workgroup's ARN ends in the id Redshift Serverless gives it (its
workgroupId, "The unique identifier of the workgroup"; botocore's
WorkgroupNameString takes an ARN ending in workgroup/<uuid>), while a
request names it by WorkgroupName, "The serverless workgroup name or
Amazon Resource Name (ARN)". So the workgroup is a FOUND_BY type,
found by WorkgroupName.

FOUND_BY named a type by its members only where the rule had derived
a fill for the type to replace, as ResourceId fills a scalable target's
${ResourceId}. No member fills ${WorkgroupId}, so FOUND_BY now names
every FOUND_BY type the action acts on whose members the request has,
as its docstring already said. The Application Auto Scaling entries
are unchanged.

RedshiftDataResourceResolver, which resolves the target a request
runs on, now implements StoredResourceArns for redshift-data: it
gives the ARN of the workgroup a name or an ARN finds, through
RedshiftServerlessService.findWorkgroupByNameOrArn, which the
service's own name-or-ARN lookup now uses as well.

Redshift Data joins the table (12 operations, 1048 over 26
namespaces). The six operations above are checked on the cluster's
ARN or on the stored workgroup's, and on `workgroup/*` for a workgroup
there is none of; their SecretArn names neither. A request naming only
its SessionId stays on `*`, and so do CancelStatement,
DescribeStatement, GetStatementResult, GetStatementResultV2,
ListSessions and ListStatements, which the Service Reference gives no
resource type.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-11-01 20:57:48 -06:00
1 parent 8c32e9f6ba
commit 9bd90d6580
11 files changed
+219 -35

No files matched your search

@@ -461,6 +461,45 @@ class RequestResourcesTest {
stored));
}
/**
* A Data API request runs on the cluster its {@code ClusterIdentifier} names, or on the serverless workgroup
* stored under the name or ARN its {@code WorkgroupName} gives, whose ARN ends in the id Redshift Serverless
* mints; its {@code SecretArn} names neither, and a request naming its session alone names none.
*/
@Test
void aDataApiRequestIsOnTheClusterOrTheStoredWorkgroupItNames() {
String workgroups = "arn:aws:redshift-serverless:us-east-1:000000000000:workgroup/";
String id = "0f1e2d3c-4b5a-4978-8a9b-0c1d2e3f4a5b";
StoredResourceArns stored = new StoredResourceArns() {
@Override
public String namespace() {
return "redshift-data";
}
@Override
public Optional<String> find(String type, List<String> names, String region) {
return "workgroup".equals(type) && "us-east-1".equals(region)
&& (List.of("analytics").equals(names) || List.of(workgroups + id).equals(names))
? Optional.of(workgroups + id) : Optional.empty();
}
};
String secret = ",\"SecretArn\":\"arn:aws:secretsmanager:us-east-1:000000000000:secret:db-AbCdEf\"";
assertEquals(List.of("arn:aws:redshift:us-east-1:000000000000:cluster:warehouse"), RequestResources.of(
"redshift-data:ExecuteStatement", json("{\"Sql\":\"select 1\",\"ClusterIdentifier\":\"warehouse\""
+ secret + "}"), "aws", "us-east-1", "000000000000", stored));
for (String named : List.of("analytics", workgroups + id)) {
assertEquals(List.of(workgroups + id), RequestResources.of("redshift-data:ListDatabases",
json("{\"Database\":\"dev\",\"WorkgroupName\":\"" + named + "\"" + secret + "}"), "aws",
"us-east-1", "000000000000", stored), named);
}
assertEquals(List.of(workgroups + "*"), RequestResources.of("redshift-data:ListDatabases",
json("{\"Database\":\"dev\",\"WorkgroupName\":\"other\"}"), "aws", "us-east-1", "000000000000",
stored));
assertEquals(List.of(), RequestResources.of("redshift-data:ExecuteStatement",
json("{\"Sql\":\"select 1\",\"SessionId\":\"" + id + "\"}"), "aws", "us-east-1", "000000000000",
stored));
}
/** A request value is a name, so a {@code ${...}} in it is kept, not read as a variable of the format. */
@Test
void aValueIsTakenLiterally() {
@@ -3,10 +3,13 @@ package io.github.hectorvent.floci.services.iam;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import io.github.hectorvent.floci.services.redshift.model.Endpoint;
import io.github.hectorvent.floci.services.redshiftserverless.RedshiftServerlessRuntime;
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.quarkus.test.InjectMock;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
@@ -33,6 +36,9 @@ import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.Mockito.when;
/**
* A request of a service whose ARNs the vendored table of request resources gives is checked on the resource it
@@ -49,6 +55,8 @@ class IamRequestResourceEnforcementIntegrationTest {
private static final String ELB = "arn:aws:elasticloadbalancing:" + REGION + ":" + ACCOUNT + ":";
private static final String EVENTS = "arn:aws:events:" + REGION + ":" + ACCOUNT + ":";
private static final String TARGETS = "arn:aws:application-autoscaling:" + REGION + ":" + ACCOUNT + ":scalable-target/";
private static final String REDSHIFT = "arn:aws:redshift:" + REGION + ":" + ACCOUNT + ":";
private static final String WORKGROUPS = "arn:aws:redshift-serverless:" + REGION + ":" + ACCOUNT + ":workgroup/";
/** The AWS Load Balancer Controller's create, tag-on-create and listener statements (its policy v2.14.0). */
private static final String CONTROLLER_CREATE = "{\"Effect\":\"Allow\",\"Action\":[\"elasticloadbalancing:CreateLoadBalancer\","
+ "\"elasticloadbalancing:CreateTargetGroup\"],\"Resource\":\"*\","
@@ -74,6 +82,10 @@ class IamRequestResourceEnforcementIntegrationTest {
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
/** A serverless workgroup's PostgreSQL container is not needed to check a request on it, so none starts. */
@InjectMock
RedshiftServerlessRuntime runtime;
@BeforeAll
static void configureRestAssured() {
RestAssuredJsonUtils.configureAwsContentTypes();
@@ -1270,6 +1282,63 @@ class IamRequestResourceEnforcementIntegrationTest {
.then();
}
/**
* A Data API request is checked on the cluster it names, or on the serverless workgroup its name or ARN finds,
* whose ARN ends in the id Redshift Serverless gave it; one naming a workgroup there is none of is checked with
* that id open. Before, these were checked on {@code *}.
*/
@Test
void aDataApiRequestIsCheckedOnTheClusterOrWorkgroupItNames() {
when(runtime.start(any(String.class), any(String.class), any(String.class), any(String.class),
any(String.class), any(String.class), any(Endpoint.class), anyBoolean(), any()))
.thenReturn(new RedshiftServerlessRuntime.Backend("127.0.0.1", 55432));
String name = "res-wg-" + suffix();
String arn = createWorkgroup(name);
assertTrue(arn.startsWith(WORKGROUPS), arn);
User user = user("res-data");
user.policy(allowOn("redshift-data:ListDatabases", arn) + ","
+ allowOn("redshift-data:ListDatabases", REDSHIFT + "cluster:res-cluster"));
// Authorized, a database the workgroup does not have, or a cluster Floci does not have, is refused by the
// Data API, not by IAM.
for (String named : List.of(name, arn)) {
listDatabases(user, "\"WorkgroupName\":\"" + named + "\"").statusCode(400)
.body(containsString("does not exist in workgroup"));
}
listDatabases(user, "\"ClusterIdentifier\":\"res-cluster\",\"DbUser\":\"admin\"").statusCode(400)
.body(containsString("was not found"));
refusedJson(listDatabases(user, "\"ClusterIdentifier\":\"res-other\",\"DbUser\":\"admin\""),
"redshift-data:ListDatabases");
refusedJson(listDatabases(user, "\"WorkgroupName\":\"res-wg-missing\""), "redshift-data:ListDatabases");
// A deny on the workgroup holds, whether a request names it by its name or by its ARN.
user.policy(allowOn("redshift-data:*", "*") + ",{\"Effect\":\"Deny\","
+ "\"Action\":\"redshift-data:ListDatabases\",\"Resource\":\"" + arn + "\"}");
for (String named : List.of(name, arn)) {
refusedJson(listDatabases(user, "\"WorkgroupName\":\"" + named + "\""), "redshift-data:ListDatabases");
}
}
/** Creates a serverless workgroup and its namespace as the admin and gives the workgroup's ARN. */
private String createWorkgroup(String name) {
adminJson("redshift-serverless", "RedshiftServerless.CreateNamespace")
.body("{\"namespaceName\":\"" + name + "-ns\",\"adminUsername\":\"admin\","
+ "\"adminUserPassword\":\"Secret123!\"}").post("/").then().statusCode(200);
cleanup.register(() -> adminJson("redshift-serverless", "RedshiftServerless.DeleteNamespace")
.body("{\"namespaceName\":\"" + name + "-ns\"}").post("/"));
String arn = adminJson("redshift-serverless", "RedshiftServerless.CreateWorkgroup")
.body("{\"workgroupName\":\"" + name + "\",\"namespaceName\":\"" + name + "-ns\"}").post("/").then()
.statusCode(200).extract().path("workgroup.workgroupArn");
cleanup.register(() -> adminJson("redshift-serverless", "RedshiftServerless.DeleteWorkgroup")
.body("{\"workgroupName\":\"" + name + "\"}").post("/"));
return arn;
}
/** A ListDatabases of a database no workgroup or cluster has, on the target the members name. */
private static ValidatableResponse listDatabases(User user, String members) {
return user.json("redshift-data", "RedshiftData.ListDatabases", "1.1")
.body("{\"Database\":\"res-none\"," + members + "}").post("/").then();
}
/** A monthly cost budget of the name, with a billing view ARN when one is given. */
private static String budget(String name, String billingViewArn) {
return "{\"BudgetName\":\"" + name + "\",\"BudgetType\":\"COST\",\"TimeUnit\":\"MONTHLY\",\"BudgetLimit\":"
@@ -435,6 +435,22 @@ class RedshiftServerlessServiceTest {
() -> service.untagResource(absent, List.of("a"), REGION)).getErrorCode());
}
@Test
void aWorkgroupIsFoundByItsNameOrArnInItsRegion() {
create("find-ns");
String arn = createWorkgroup("find-wg", "find-ns").getWorkgroupArn();
assertTrue(arn.matches("arn:aws:redshift-serverless:" + REGION + ":" + ACCOUNT_ID
+ ":workgroup/[0-9a-f-]{36}"), arn);
for (String named : List.of("find-wg", arn)) {
assertEquals(Optional.of(arn),
service.findWorkgroupByNameOrArn(named, REGION).map(Workgroup::getWorkgroupArn), named);
}
assertEquals(Optional.empty(), service.findWorkgroupByNameOrArn("other-wg", REGION));
assertEquals(Optional.empty(), service.findWorkgroupByNameOrArn("find-wg", "eu-west-1"));
assertEquals(Optional.empty(), service.findWorkgroupByNameOrArn(arn.substring(0, arn.lastIndexOf('/') + 1)
+ "00000000-0000-0000-0000-000000000000", REGION));
}
@Test
void tagsSurviveAnUnrelatedNamespaceUpdate() {
Namespace created = service.createNamespace("keep-tags-ns", "admin", null, null, null, null, null,