fix(redshift): check the extra IAM actions GetClusterCredentials needs (#6067)

This commit is contained in:
Aymen JAMAAOUI authored and GitHub committed 2026-10-20 07:29:25 -04:00
1 parent fa4bacd0d1
commit d0dac9814f
3 files changed
+174 -4

No files matched your search

+7
View File
@@ -1504,6 +1504,13 @@ it does on AWS, and a `Deny` on the full ARN applies. `CreateSecret` is evaluate
policy scoped to a name prefix such as `secret:app/*` allows it. A name that matches no secret in the
request's account and region is evaluated as `secret:<name>`, and an ARN that matches none as given.
**Redshift GetClusterCredentials options.** Besides `redshift:GetClusterCredentials`, a request with
`DbGroups` needs `redshift:JoinGroup` on each group's `dbgroup:<cluster>/<group>` ARN, and a request with
`AutoCreate` set to `true` needs `redshift:CreateClusterUser` on the user's `dbuser:<cluster>/<user>` ARN,
as the GetClusterCredentials API reference lists them. They are checked before the cluster is looked up,
so a denied request gets `AccessDenied` even for a cluster that does not exist. The `dbname` resource
that `DbName` needs is not checked.
**IAM's own resources are named.** When enforcement evaluates a request, the target resource comes
from `ResourceArnBuilder`, which builds an ARN for S3, Lambda, SQS, SNS, DynamoDB, Kinesis, Secrets
Manager, SSM, KMS and IAM. Within IAM the resource type each action is evaluated against is the one
@@ -3,6 +3,7 @@ package io.github.hectorvent.floci.services.redshift;
import io.github.hectorvent.floci.config.EmulatorConfig;
import io.github.hectorvent.floci.core.common.AwsArnUtils;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.common.IamEnforcementFilter;
import io.github.hectorvent.floci.core.common.PaginatedResult;
import io.github.hectorvent.floci.core.common.RegionResolver;
import io.github.hectorvent.floci.core.common.XmlBuilder;
@@ -48,24 +49,27 @@ public class RedshiftQueryHandler {
private final RedshiftIamDbUserResolver iamDbUserResolver;
private final RegionResolver regionResolver;
private final RedshiftDynamoDbZeroEtlConsumer zeroEtlConsumer;
private final IamEnforcementFilter iamEnforcementFilter;
@Inject
public RedshiftQueryHandler(RedshiftService service, RedshiftCredentialBroker credentialBroker,
EmulatorConfig config, RedshiftIamDbUserResolver iamDbUserResolver,
RegionResolver regionResolver,
RedshiftDynamoDbZeroEtlConsumer zeroEtlConsumer) {
RedshiftDynamoDbZeroEtlConsumer zeroEtlConsumer,
IamEnforcementFilter iamEnforcementFilter) {
this.service = service;
this.credentialBroker = credentialBroker;
this.config = config;
this.iamDbUserResolver = iamDbUserResolver;
this.regionResolver = regionResolver;
this.zeroEtlConsumer = zeroEtlConsumer;
this.iamEnforcementFilter = iamEnforcementFilter;
}
RedshiftQueryHandler(RedshiftService service, RedshiftCredentialBroker credentialBroker,
EmulatorConfig config, RedshiftIamDbUserResolver iamDbUserResolver,
RegionResolver regionResolver) {
this(service, credentialBroker, config, iamDbUserResolver, regionResolver, null);
this(service, credentialBroker, config, iamDbUserResolver, regionResolver, null, null);
}
public Response handle(String action, MultivaluedMap<String, String> params) {
@@ -764,13 +768,14 @@ public class RedshiftQueryHandler {
case "GetClusterCredentials" -> {
String clusterId = requireParam(params, "ClusterIdentifier");
String dbUser = requireParam(params, "DbUser");
boolean autoCreate = Boolean.parseBoolean(params.getFirst("AutoCreate"));
List<String> dbGroups = memberList(params, "DbGroups");
authorizeCredentialOptions(authorizationHeader, clusterId, dbUser, autoCreate, dbGroups);
// describeClusters throws ClusterNotFound (404) for an unknown id.
service.describeClusters(clusterId);
// AWS prefixes the returned name IAMA: when AutoCreate is true, IAM: when it is false.
boolean autoCreate = Boolean.parseBoolean(params.getFirst("AutoCreate"));
String effectiveDbUser = (autoCreate ? "IAMA:" : "IAM:") + dbUser;
int duration = resolveDurationSeconds(params);
List<String> dbGroups = memberList(params, "DbGroups");
TempCredential credential = credentialBroker.issue(
regionResolver.getAccountId(), clusterId, effectiveDbUser, dbGroups, duration);
@@ -793,6 +798,28 @@ public class RedshiftQueryHandler {
}
}
/**
* Authorizes the actions GetClusterCredentials needs besides redshift:GetClusterCredentials, which
* the API reference lists for its options: redshift:JoinGroup on each group in {@code DbGroups},
* and redshift:CreateClusterUser on the database user when {@code AutoCreate} is true. It runs
* before the cluster lookup, so a denied caller learns nothing about the cluster.
*/
private void authorizeCredentialOptions(String authorizationHeader, String clusterId, String dbUser,
boolean autoCreate, List<String> dbGroups) {
if (iamEnforcementFilter == null) {
return;
}
String region = regionResolver.resolveRegionFromAuth(authorizationHeader);
for (String dbGroup : dbGroups) {
iamEnforcementFilter.authorizeAdditionalResource(authorizationHeader, "redshift:JoinGroup",
regionResolver.buildArn("redshift", region, "dbgroup:" + clusterId + "/" + dbGroup));
}
if (autoCreate) {
iamEnforcementFilter.authorizeAdditionalResource(authorizationHeader, "redshift:CreateClusterUser",
regionResolver.buildArn("redshift", region, "dbuser:" + clusterId + "/" + dbUser));
}
}
private static String requireParam(MultivaluedMap<String, String> params, String name) {
String value = params.getFirst(name);
if (value == null || value.isBlank()) {
@@ -0,0 +1,136 @@
package io.github.hectorvent.floci.services.redshift;
import io.github.hectorvent.floci.testing.EnforcementFixtures;
import io.github.hectorvent.floci.testing.IamEnforcementProfile;
import io.github.hectorvent.floci.testing.PartitionCleanup;
import io.quarkus.test.common.http.TestHTTPResource;
import io.quarkus.test.junit.QuarkusTest;
import io.quarkus.test.junit.TestProfile;
import io.restassured.specification.RequestSpecification;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import java.net.URI;
import java.util.List;
import java.util.UUID;
import static io.github.hectorvent.floci.testing.EnforcementFixtures.auth;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.containsString;
/**
* GetClusterCredentials needs more than redshift:GetClusterCredentials when it uses its options, as the
* API reference lists them: redshift:JoinGroup on each {@code dbgroup} named in {@code DbGroups}, and
* redshift:CreateClusterUser on the {@code dbuser} when {@code AutoCreate} is true. The checks run before
* the cluster lookup, so a caller they allow gets {@code ClusterNotFound} for a cluster that does not
* exist, and the tests need no Docker.
*/
@QuarkusTest
@TestProfile(IamEnforcementProfile.class)
class RedshiftGetClusterCredentialsIamEnforcementIntegrationTest {
private static final String ADMIN = "test";
private static final String ARN_PREFIX = "arn:aws:redshift:us-east-1:000000000000:";
@TestHTTPResource("/")
static URI baseUri;
@RegisterExtension
final PartitionCleanup cleanup = new PartitionCleanup();
@Test
void getClusterCredentials_dbGroupsWithoutJoinGroup_isDenied() {
String cluster = uniqueName("gcc-groups");
String user = uniqueName("gcc-groups");
String accessKeyId = createUser(user);
userPolicy(user, allow("redshift:GetClusterCredentials", "*"));
assertDenied(credentials(accessKeyId, cluster).formParam("DbGroups.DbGroup.1", "analysts"));
userPolicy(user, allow("redshift:GetClusterCredentials", "*") + ","
+ allow("redshift:JoinGroup", ARN_PREFIX + "dbgroup:" + cluster + "/analysts"));
assertDenied(credentials(accessKeyId, cluster)
.formParam("DbGroups.DbGroup.1", "analysts")
.formParam("DbGroups.DbGroup.2", "admins"));
}
@Test
void getClusterCredentials_dbGroupsWithJoinGroupOnEachGroup_passesTheIamCheck() {
String cluster = uniqueName("gcc-groups-ok");
String user = uniqueName("gcc-groups-ok");
String accessKeyId = createUser(user);
userPolicy(user, allow("redshift:GetClusterCredentials", "*") + ","
+ allow("redshift:JoinGroup", ARN_PREFIX + "dbgroup:" + cluster + "/analysts") + ","
+ allow("redshift:JoinGroup", ARN_PREFIX + "dbgroup:" + cluster + "/admins"));
assertClusterNotFound(credentials(accessKeyId, cluster)
.formParam("DbGroups.DbGroup.1", "analysts")
.formParam("DbGroups.DbGroup.2", "admins"));
}
@Test
void getClusterCredentials_autoCreateWithoutCreateClusterUser_isDenied() {
String cluster = uniqueName("gcc-auto");
String user = uniqueName("gcc-auto");
String accessKeyId = createUser(user);
userPolicy(user, allow("redshift:GetClusterCredentials", "*"));
assertDenied(credentials(accessKeyId, cluster).formParam("AutoCreate", "true"));
}
@Test
void getClusterCredentials_autoCreateWithCreateClusterUserOnTheDbUser_passesTheIamCheck() {
String cluster = uniqueName("gcc-auto-ok");
String user = uniqueName("gcc-auto-ok");
String accessKeyId = createUser(user);
userPolicy(user, allow("redshift:GetClusterCredentials", "*") + ","
+ allow("redshift:CreateClusterUser", ARN_PREFIX + "dbuser:" + cluster + "/analyst"));
assertClusterNotFound(credentials(accessKeyId, cluster).formParam("AutoCreate", "true"));
}
@Test
void getClusterCredentials_withoutOptions_needsOnlyGetClusterCredentials() {
String cluster = uniqueName("gcc-plain");
String user = uniqueName("gcc-plain");
String accessKeyId = createUser(user);
userPolicy(user, allow("redshift:GetClusterCredentials", "*"));
assertClusterNotFound(credentials(accessKeyId, cluster).formParam("AutoCreate", "false"));
}
private static RequestSpecification credentials(String accessKeyId, String cluster) {
return given()
.header("Authorization", auth(accessKeyId, "redshift"))
.contentType("application/x-www-form-urlencoded")
.formParam("Action", "GetClusterCredentials")
.formParam("Version", "2012-12-01")
.formParam("ClusterIdentifier", cluster)
.formParam("DbUser", "analyst");
}
private static void assertDenied(RequestSpecification request) {
request.when().post("/").then().statusCode(403).body(containsString("AccessDenied"));
}
private static void assertClusterNotFound(RequestSpecification request) {
request.when().post("/").then().statusCode(404).body(containsString("ClusterNotFound"));
}
private static String uniqueName(String prefix) {
return prefix + "-" + UUID.randomUUID().toString().substring(0, 8);
}
private String createUser(String user) {
return EnforcementFixtures.createUser(cleanup, baseUri.getPort(), auth(ADMIN, "iam"), user).accessKeyId();
}
private static String allow(String action, String resource) {
return "{\"Effect\":\"Allow\",\"Action\":\"" + action + "\",\"Resource\":\"" + resource + "\"}";
}
private void userPolicy(String user, String statements) {
EnforcementFixtures.putUserPolicy(baseUri.getPort(), auth(ADMIN, "iam"), user, "p",
"{\"Version\":\"2012-10-17\",\"Statement\":[" + statements + "]}");
}
}