mirror of
https://github.com/floci-io/floci.git
synced 2026-11-22 21:50:46 -05:00
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:
1 parent
3f675c00fb
commit
1fe8eae011
5 files changed
+184
-32
No files matched your search
@@ -182,12 +182,14 @@ Floci does not write the `redshift_failed` manifest objects that AWS creates aft
|
||||
|
||||
- `ClusterJDBCURL` must match the AWS pattern, so it needs a host such as
|
||||
`mycluster.abc123.us-east-1.redshift.amazonaws.com`. Floci treats the URL as a logical address: the first
|
||||
host label is the cluster identifier, and the host suffix and port are ignored. The database name is ignored
|
||||
too, because the emulated database is always `dev`. The cluster is looked up at delivery time, so a stream can
|
||||
be created before its cluster.
|
||||
host label is the cluster identifier or workgroup name, and the host suffix and port are ignored. For a
|
||||
provisioned cluster the database name is ignored too, because the emulated database is always `dev`. The cluster
|
||||
or workgroup is looked up at delivery time, so a stream can be created before it exists.
|
||||
- A `ClusterJDBCURL` with a `redshift-serverless` host delivers to the Serverless workgroup of that name in the
|
||||
stream's account and region, through the workgroup proxy. A workgroup has one database, so a URL that names
|
||||
another database fails the delivery, which then follows the usual retry path. Without a `Username` the workgroup's
|
||||
admin user signs in.
|
||||
- `ProcessingConfiguration` on a Redshift destination is accepted and returned but not executed.
|
||||
- A `ClusterJDBCURL` with a `redshift-serverless` host is accepted, but delivery looks the name up as a provisioned
|
||||
cluster, so a serverless workgroup is not a delivery target yet.
|
||||
- The `COPY` carries no credentials, so the proxy reads the staged object as an anonymous client. With S3
|
||||
authorization enforced and a private intermediate bucket the load fails and the staged object stays in S3; use a
|
||||
bucket the anonymous principal can read, or leave enforcement off.
|
||||
|
||||
+57
-21
@@ -2,9 +2,11 @@ package io.github.hectorvent.floci.services.firehose;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.github.hectorvent.floci.core.common.DatabaseSecretCredentials;
|
||||
import io.github.hectorvent.floci.core.common.RequestScopes;
|
||||
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.redshiftserverless.RedshiftServerlessService;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.SecretsManagerService;
|
||||
import io.github.hectorvent.floci.services.secretsmanager.model.SecretVersion;
|
||||
import jakarta.enterprise.context.ApplicationScoped;
|
||||
@@ -18,9 +20,9 @@ import java.sql.Statement;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
* Loads a delivered S3 object into a Floci Redshift cluster. The {@code COPY} runs over a JDBC
|
||||
* connection to the cluster's wire proxy in simple query mode, so the proxy's S3 COPY emulation
|
||||
* does the work exactly as it does for any other client.
|
||||
* Loads a delivered S3 object into a Floci Redshift cluster or Redshift Serverless workgroup. The
|
||||
* {@code COPY} runs over a JDBC connection to the cluster's wire proxy, or to the workgroup's runtime,
|
||||
* in simple query mode, so the S3 COPY emulation does the work exactly as it does for any other client.
|
||||
*/
|
||||
@ApplicationScoped
|
||||
public class FirehoseRedshiftDelivery {
|
||||
@@ -34,20 +36,28 @@ public class FirehoseRedshiftDelivery {
|
||||
void execute(String jdbcUrl, String user, String password, String sql) throws SQLException;
|
||||
}
|
||||
|
||||
/**
|
||||
* Where a delivery connects. {@code defaultUser} signs in when the destination names none.
|
||||
*/
|
||||
private record Backend(String description, String host, int port, String database, String defaultUser) {
|
||||
}
|
||||
|
||||
private final RedshiftService redshiftService;
|
||||
private final RedshiftServerlessService serverlessService;
|
||||
private final SecretsManagerService secretsManagerService;
|
||||
private final ObjectMapper mapper;
|
||||
private final SqlRunner runner;
|
||||
|
||||
@Inject
|
||||
public FirehoseRedshiftDelivery(RedshiftService redshiftService, SecretsManagerService secretsManagerService,
|
||||
ObjectMapper mapper) {
|
||||
this(redshiftService, secretsManagerService, mapper, FirehoseRedshiftDelivery::runJdbc);
|
||||
public FirehoseRedshiftDelivery(RedshiftService redshiftService, RedshiftServerlessService serverlessService,
|
||||
SecretsManagerService secretsManagerService, ObjectMapper mapper) {
|
||||
this(redshiftService, serverlessService, secretsManagerService, mapper, FirehoseRedshiftDelivery::runJdbc);
|
||||
}
|
||||
|
||||
FirehoseRedshiftDelivery(RedshiftService redshiftService, SecretsManagerService secretsManagerService,
|
||||
ObjectMapper mapper, SqlRunner runner) {
|
||||
FirehoseRedshiftDelivery(RedshiftService redshiftService, RedshiftServerlessService serverlessService,
|
||||
SecretsManagerService secretsManagerService, ObjectMapper mapper, SqlRunner runner) {
|
||||
this.redshiftService = redshiftService;
|
||||
this.serverlessService = serverlessService;
|
||||
this.secretsManagerService = secretsManagerService;
|
||||
this.mapper = mapper;
|
||||
this.runner = runner;
|
||||
@@ -56,31 +66,57 @@ public class FirehoseRedshiftDelivery {
|
||||
/**
|
||||
* Runs the destination's COPY for the object at {@code s3://bucket/key}.
|
||||
*
|
||||
* Throws {@code AwsException} when the cluster or the secret cannot be resolved.
|
||||
* Throws {@code AwsException} when the cluster, the workgroup or the secret cannot be resolved.
|
||||
*
|
||||
* @throws IllegalStateException when the statement itself fails; the message carries the table and SQL state
|
||||
*/
|
||||
public void copy(String accountId, String region, RedshiftDestination destination, String bucket, String key) {
|
||||
RedshiftJdbcUrl target = RedshiftJdbcUrl.parse(destination.getClusterJdbcUrl());
|
||||
Cluster cluster = redshiftService.describeClustersForAccount(accountId, target.clusterIdentifier()).get(0);
|
||||
String[] credentials = credentials(destination, cluster, region);
|
||||
Backend backend = target.serverless()
|
||||
? workgroupBackend(accountId, region, target)
|
||||
: clusterBackend(accountId, target);
|
||||
String[] credentials = credentials(destination, backend, region);
|
||||
RedshiftDestination.CopyCommand command = destination.getCopyCommand();
|
||||
String sql = RedshiftCopySql.build(command.getDataTableName(), command.getDataTableColumns(), bucket, key,
|
||||
command.getCopyOptions());
|
||||
// The database name in the URL is ignored: the emulated container database is always "dev".
|
||||
String jdbcUrl = "jdbc:postgresql://127.0.0.1:" + cluster.getEndpoint().getPort()
|
||||
+ "/dev?preferQueryMode=simple";
|
||||
String jdbcUrl = "jdbc:postgresql://" + backend.host() + ":" + backend.port() + "/" + backend.database()
|
||||
+ "?preferQueryMode=simple";
|
||||
try {
|
||||
runner.execute(jdbcUrl, credentials[0], credentials[1], sql);
|
||||
} catch (SQLException e) {
|
||||
throw new IllegalStateException("COPY into " + command.getDataTableName() + " on cluster "
|
||||
+ target.clusterIdentifier() + " failed (SQLSTATE " + e.getSQLState() + "): " + e.getMessage(), e);
|
||||
throw new IllegalStateException("COPY into " + command.getDataTableName() + " on "
|
||||
+ backend.description() + " failed (SQLSTATE " + e.getSQLState() + "): " + e.getMessage(), e);
|
||||
}
|
||||
LOG.debugv("Firehose copied s3://{0}/{1} into {2} on cluster {3}", bucket, key,
|
||||
command.getDataTableName(), target.clusterIdentifier());
|
||||
LOG.debugv("Firehose copied s3://{0}/{1} into {2} on {3}", bucket, key,
|
||||
command.getDataTableName(), backend.description());
|
||||
}
|
||||
|
||||
private String[] credentials(RedshiftDestination destination, Cluster cluster, String region) {
|
||||
private Backend clusterBackend(String accountId, RedshiftJdbcUrl target) {
|
||||
Cluster cluster = redshiftService.describeClustersForAccount(accountId, target.clusterIdentifier()).get(0);
|
||||
// The database name in the URL is ignored: the emulated container database is always "dev".
|
||||
return new Backend("cluster " + target.clusterIdentifier(), "127.0.0.1", cluster.getEndpoint().getPort(),
|
||||
"dev", cluster.getMasterUsername());
|
||||
}
|
||||
|
||||
/**
|
||||
* A workgroup has one database, so a URL that names another one cannot load anything: the same
|
||||
* refusal the Data API gives, here a failed delivery that goes through the retry path.
|
||||
*/
|
||||
private Backend workgroupBackend(String accountId, String region, RedshiftJdbcUrl target) {
|
||||
RedshiftServerlessService.WorkgroupTarget workgroup = RequestScopes.callAs(accountId, region,
|
||||
() -> serverlessService.getWorkgroupTarget(target.clusterIdentifier(), region));
|
||||
if (!target.database().equals(workgroup.database())) {
|
||||
throw new IllegalStateException("Database " + target.database() + " does not exist in workgroup "
|
||||
+ workgroup.workgroupName() + "; the only database is " + workgroup.database() + ".");
|
||||
}
|
||||
int port = workgroup.endpoint() != null && workgroup.endpoint().getPort() > 0
|
||||
? workgroup.endpoint().getPort()
|
||||
: workgroup.port();
|
||||
return new Backend("workgroup " + workgroup.workgroupName(), "127.0.0.1", port,
|
||||
workgroup.database(), workgroup.adminUsername());
|
||||
}
|
||||
|
||||
private String[] credentials(RedshiftDestination destination, Backend backend, String region) {
|
||||
RedshiftDestination.SecretsManagerConfiguration secrets = destination.getSecretsManagerConfiguration();
|
||||
if (secrets != null && Boolean.TRUE.equals(secrets.getEnabled())) {
|
||||
SecretVersion secret = secretsManagerService.getSecretValue(secrets.getSecretArn(), null, null, region);
|
||||
@@ -90,10 +126,10 @@ public class FirehoseRedshiftDelivery {
|
||||
return new String[] {credentials.username(), credentials.password()};
|
||||
}
|
||||
if (destination.getPassword() == null) {
|
||||
throw new IllegalStateException("The Redshift destination for cluster " + cluster.getClusterIdentifier()
|
||||
throw new IllegalStateException("The Redshift destination for " + backend.description()
|
||||
+ " has no Password and no enabled SecretsManagerConfiguration, so Firehose cannot sign in.");
|
||||
}
|
||||
String user = destination.getUsername() != null ? destination.getUsername() : cluster.getMasterUsername();
|
||||
String user = destination.getUsername() != null ? destination.getUsername() : backend.defaultUser();
|
||||
return new String[] {user, destination.getPassword()};
|
||||
}
|
||||
|
||||
|
||||
+12
-4
@@ -12,6 +12,7 @@ import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend;
|
||||
import io.github.hectorvent.floci.core.storage.StorageFactory;
|
||||
import io.github.hectorvent.floci.services.redshift.SnapshotDumpCopier;
|
||||
import io.github.hectorvent.floci.services.redshift.TempCredential;
|
||||
import io.github.hectorvent.floci.services.redshift.model.Endpoint;
|
||||
import io.github.hectorvent.floci.services.redshiftserverless.RedshiftServerlessSnapshotCopyService.CopyRule;
|
||||
import io.github.hectorvent.floci.services.redshiftserverless.model.ConfigParameter;
|
||||
import io.github.hectorvent.floci.services.redshiftserverless.model.Namespace;
|
||||
@@ -1201,24 +1202,31 @@ public class RedshiftServerlessService implements Resettable {
|
||||
return new WorkgroupTarget(workgroup.getWorkgroupArn(), workgroup.getWorkgroupName(),
|
||||
workgroup.getRuntimeHost(), workgroup.getRuntimePort(), namespace.getDbName(),
|
||||
workgroup.getMasterUsername(), namespace.getAdminUserPassword(), adminUserOf(namespace),
|
||||
namespace.getIamRoles(), namespace.getAdminPasswordSecretArn());
|
||||
namespace.getIamRoles(), namespace.getAdminPasswordSecretArn(), workgroup.getEndpoint());
|
||||
}
|
||||
|
||||
/** Connection details of a workgroup's backend; {@code adminUsername} is the name clients log in with. */
|
||||
public record WorkgroupTarget(String arn, String workgroupName, String host, int port, String database,
|
||||
String masterUsername, String masterPassword, String adminUsername,
|
||||
List<String> iamRoleArns, String adminPasswordSecretArn) {
|
||||
List<String> iamRoleArns, String adminPasswordSecretArn, Endpoint endpoint) {
|
||||
public WorkgroupTarget(String arn, String workgroupName, String host, int port, String database,
|
||||
String masterUsername, String masterPassword, String adminUsername,
|
||||
List<String> iamRoleArns, String adminPasswordSecretArn) {
|
||||
this(arn, workgroupName, host, port, database, masterUsername, masterPassword, adminUsername,
|
||||
iamRoleArns, adminPasswordSecretArn, null);
|
||||
}
|
||||
|
||||
public WorkgroupTarget(String arn, String workgroupName, String host, int port, String database,
|
||||
String masterUsername, String masterPassword, String adminUsername,
|
||||
List<String> iamRoleArns) {
|
||||
this(arn, workgroupName, host, port, database, masterUsername, masterPassword, adminUsername,
|
||||
iamRoleArns, null);
|
||||
iamRoleArns, null, null);
|
||||
}
|
||||
|
||||
public WorkgroupTarget(String arn, String workgroupName, String host, int port, String database,
|
||||
String masterUsername, String masterPassword, String adminUsername) {
|
||||
this(arn, workgroupName, host, port, database, masterUsername, masterPassword, adminUsername,
|
||||
List.of(), null);
|
||||
List.of(), null, null);
|
||||
}
|
||||
|
||||
public WorkgroupTarget {
|
||||
|
||||
+107
-2
@@ -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");
|
||||
});
|
||||
|
||||
+1
@@ -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";
|
||||
|
||||
Reference in new issue
Block a user