feat(firehose): deliver Redshift COPY loads to a Serverless workgroup (#7227)

* feat(firehose): deliver Redshift COPY loads to a Serverless workgroup

A ClusterJDBCURL with a redshift-serverless host now resolves the workgroup in
the stream's account and region and loads through its runtime, with its
database and admin user. A URL naming another database fails the delivery
through the existing retry path. Provisioned delivery is unchanged.

* fix(firehose): route Redshift Serverless delivery through the workgroup proxy

Connect Firehose Serverless delivery to the workgroup's proxy endpoint (127.0.0.1:<proxyPort>)
instead of the internal PostgreSQL container, matching provisioned cluster delivery and
enabling S3CopySimulator to intercept COPY FROM 's3://...'.

* fix(firehose): drop managed admin secret override on proxy delivery
This commit is contained in:
Vũ Thế Huy authored and GitHub committed 2026-11-01 20:46:59 -04:00
1 parent 3f675c00fb
commit 1fe8eae011
5 files changed
+184 -32

No files matched your search

@@ -6,6 +6,8 @@ import io.github.hectorvent.floci.services.firehose.model.RedshiftDestination;
import io.github.hectorvent.floci.services.redshift.RedshiftService;
import io.github.hectorvent.floci.services.redshift.model.Cluster;
import io.github.hectorvent.floci.services.redshift.model.Endpoint;
import io.github.hectorvent.floci.services.redshiftserverless.RedshiftServerlessService;
import io.github.hectorvent.floci.services.redshiftserverless.RedshiftServerlessService.WorkgroupTarget;
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
import org.junit.jupiter.api.BeforeEach;
@@ -26,12 +28,14 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
class FirehoseRedshiftDeliveryTest {
private final ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
private RedshiftService redshift;
private RedshiftServerlessService serverless;
private SecretsManagerService secrets;
private final List<String[]> executed = new ArrayList<>();
private FirehoseRedshiftDelivery delivery;
@@ -44,10 +48,111 @@ class FirehoseRedshiftDeliveryTest {
when(cluster.getEndpoint()).thenReturn(new Endpoint("host.docker.internal", 41234));
when(cluster.getMasterUsername()).thenReturn("admin");
when(redshift.describeClustersForAccount("000000000000", "c1")).thenReturn(List.of(cluster));
delivery = new FirehoseRedshiftDelivery(redshift, secrets, mapper,
serverless = mock(RedshiftServerlessService.class);
delivery = new FirehoseRedshiftDelivery(redshift, serverless, secrets, mapper,
(url, user, password, sql) -> executed.add(new String[] {url, user, password, sql}));
}
private RedshiftDestination serverlessDestination() throws Exception {
RedshiftDestination destination = destination();
destination.setClusterJdbcUrl(
"jdbc:redshift://wg1.000000000000.us-east-1.redshift-serverless.amazonaws.com:5439/analytics");
destination.setUsername(null);
return destination;
}
private void workgroup(String database) {
when(serverless.getWorkgroupTarget("wg1", "us-east-1")).thenReturn(new WorkgroupTarget(
"arn:aws:redshift-serverless:us-east-1:000000000000:workgroup/id1", "wg1", "host.docker.internal",
5432, database, "workgroupadmin", "ignored", "workgroupadmin", List.of(), null,
new Endpoint("localhost", 41300)));
}
@Test
void serverlessUrlLoadsIntoTheWorkgroupItsDatabaseAndItsAdminUser() throws Exception {
workgroup("analytics");
delivery.copy("000000000000", "us-east-1", serverlessDestination(), "stage", "k1");
assertEquals(1, executed.size());
assertEquals("jdbc:postgresql://127.0.0.1:41300/analytics?preferQueryMode=simple",
executed.get(0)[0]);
assertEquals("workgroupadmin", executed.get(0)[1]);
assertEquals("Secret123", executed.get(0)[2]);
verifyNoInteractions(redshift);
}
@Test
void serverlessDestinationKeepsAnExplicitUsername() throws Exception {
workgroup("analytics");
RedshiftDestination destination = serverlessDestination();
destination.setUsername("loader");
delivery.copy("000000000000", "us-east-1", destination, "stage", "k1");
assertEquals("loader", executed.get(0)[1]);
}
@Test
void serverlessUrlNamingAnotherDatabaseFailsTheDelivery() throws Exception {
workgroup("dev");
IllegalStateException e = assertThrows(IllegalStateException.class,
() -> delivery.copy("000000000000", "us-east-1", serverlessDestination(), "stage", "k1"));
assertTrue(e.getMessage().contains("analytics") && e.getMessage().contains("dev"), e.getMessage());
assertTrue(executed.isEmpty());
}
@Test
void unknownWorkgroupSurfacesAsAwsException() throws Exception {
when(serverless.getWorkgroupTarget("wg1", "us-east-1"))
.thenThrow(new AwsException("ResourceNotFoundException", "Workgroup wg1 not found", 400));
assertThrows(AwsException.class,
() -> delivery.copy("000000000000", "us-east-1", serverlessDestination(), "stage", "k"));
assertTrue(executed.isEmpty());
}
@Test
void workgroupWithoutARuntimeSurfacesAsAwsException() throws Exception {
when(serverless.getWorkgroupTarget("wg1", "us-east-1")).thenThrow(
new AwsException("ValidationException", "Workgroup runtime is not available", 400));
assertThrows(AwsException.class,
() -> delivery.copy("000000000000", "us-east-1", serverlessDestination(), "stage", "k"));
}
@Test
void serverlessSecretSignsInWithItsStoredCredentials() throws Exception {
workgroup("analytics");
RedshiftDestination destination = serverlessDestination();
RedshiftDestination.SecretsManagerConfiguration cfg = new RedshiftDestination.SecretsManagerConfiguration();
cfg.setEnabled(true);
cfg.setSecretArn("arn:aws:secretsmanager:us-east-1:000000000000:secret:admin-AbCdEf");
destination.setSecretsManagerConfiguration(cfg);
SecretVersion version = mock(SecretVersion.class);
when(version.getSecretString()).thenReturn("{\"username\":\"admin\",\"password\":\"pw-managed\"}");
when(secrets.getSecretValue(eq("arn:aws:secretsmanager:us-east-1:000000000000:secret:admin-AbCdEf"), any(), any(), eq("us-east-1"))).thenReturn(version);
delivery.copy("000000000000", "us-east-1", destination, "stage", "k1");
assertEquals("admin", executed.get(0)[1]);
assertEquals("pw-managed", executed.get(0)[2]);
}
@Test
void serverlessWithoutAnyCredentialsFailsTheDelivery() throws Exception {
workgroup("analytics");
RedshiftDestination destination = serverlessDestination();
destination.setPassword(null);
IllegalStateException e = assertThrows(IllegalStateException.class,
() -> delivery.copy("000000000000", "us-east-1", destination, "stage", "k1"));
assertTrue(e.getMessage().contains("workgroup wg1"), e.getMessage());
}
private RedshiftDestination destination() throws Exception {
return mapper.readValue("""
{"RoleARN": "arn:aws:iam::000000000000:role/firehose",
@@ -108,7 +213,7 @@ class FirehoseRedshiftDeliveryTest {
@Test
void jdbcFailureIsWrappedWithTableClusterAndSqlState() {
FirehoseRedshiftDelivery failing = new FirehoseRedshiftDelivery(redshift, secrets, mapper,
FirehoseRedshiftDelivery failing = new FirehoseRedshiftDelivery(redshift, serverless, secrets, mapper,
(url, user, password, sql) -> {
throw new SQLException("relation \"events\" does not exist", "42P01");
});
@@ -955,6 +955,7 @@ class RedshiftServerlessServiceTest {
assertEquals("analytics", target.database());
assertEquals("root", target.masterUsername());
assertEquals("Secret123", target.masterPassword());
assertEquals(created.getEndpoint().getPort(), target.endpoint().getPort());
}
String absentArn = "arn:aws:redshift-serverless:us-east-1:" + ACCOUNT_ID
+ ":workgroup/00000000-0000-0000-0000-000000000000";