mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
refactor(glue): share Redshift JDBC target resolution and record the outcome of a crawl (#9195)
This commit is contained in:
1 parent
351973d3f8
commit
2250972578
13 files changed
+440
-88
No files matched your search
@@ -7,6 +7,7 @@ import io.floci.aws.services.firehose.model.RedshiftDestination;
|
||||
import io.floci.aws.services.iam.IamService;
|
||||
import io.floci.aws.services.iam.ServiceRoleAuthorizer;
|
||||
import io.floci.aws.services.iam.model.IamRole;
|
||||
import io.floci.aws.services.redshift.RedshiftJdbcTargets;
|
||||
import io.floci.aws.services.redshift.RedshiftService;
|
||||
import io.floci.aws.services.redshift.model.Cluster;
|
||||
import io.floci.aws.services.redshift.model.Endpoint;
|
||||
@@ -53,6 +54,7 @@ class FirehoseRedshiftDeliveryTest {
|
||||
private IamService iamService;
|
||||
private final List<String[]> executed = new ArrayList<>();
|
||||
private FirehoseRedshiftDelivery delivery;
|
||||
private RedshiftJdbcTargets jdbcTargets;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
@@ -65,7 +67,8 @@ class FirehoseRedshiftDeliveryTest {
|
||||
serverless = mock(RedshiftServerlessService.class);
|
||||
roleAuthorizer = mock(ServiceRoleAuthorizer.class);
|
||||
iamService = mock(IamService.class);
|
||||
delivery = new FirehoseRedshiftDelivery(redshift, serverless, secrets, roleAuthorizer, iamService, mapper,
|
||||
jdbcTargets = new RedshiftJdbcTargets(redshift, serverless, secrets, mapper);
|
||||
delivery = new FirehoseRedshiftDelivery(jdbcTargets, roleAuthorizer, iamService,
|
||||
(url, user, password, sql) -> executed.add(new String[] {url, user, password, sql}));
|
||||
}
|
||||
|
||||
@@ -282,8 +285,8 @@ class FirehoseRedshiftDeliveryTest {
|
||||
void aFailedCopyNeitherLeaksCredentialsIntoTheErrorNorKeepsTheSession() throws Exception {
|
||||
knownLoadRole();
|
||||
List<String> sent = new ArrayList<>();
|
||||
FirehoseRedshiftDelivery failing = new FirehoseRedshiftDelivery(redshift, serverless, secrets,
|
||||
roleAuthorizer, iamService, mapper, (url, user, password, sql) -> {
|
||||
FirehoseRedshiftDelivery failing = new FirehoseRedshiftDelivery(jdbcTargets,
|
||||
roleAuthorizer, iamService, (url, user, password, sql) -> {
|
||||
sent.add(sql);
|
||||
throw new SQLException("rejected: " + sql, "XX000");
|
||||
});
|
||||
@@ -348,8 +351,8 @@ class FirehoseRedshiftDeliveryTest {
|
||||
|
||||
@Test
|
||||
void jdbcFailureIsWrappedWithTableClusterAndSqlState() {
|
||||
FirehoseRedshiftDelivery failing = new FirehoseRedshiftDelivery(redshift, serverless, secrets,
|
||||
roleAuthorizer, iamService, mapper,
|
||||
FirehoseRedshiftDelivery failing = new FirehoseRedshiftDelivery(jdbcTargets,
|
||||
roleAuthorizer, iamService,
|
||||
(url, user, password, sql) -> {
|
||||
throw new SQLException("relation \"events\" does not exist", "42P01");
|
||||
});
|
||||
|
||||
@@ -11,6 +11,7 @@ import io.floci.aws.services.iam.ScpProvider;
|
||||
import io.floci.aws.services.iam.ServiceRoleAuthorizer;
|
||||
import io.floci.aws.services.iam.model.CallerContext;
|
||||
import io.floci.aws.services.iam.model.IamRole;
|
||||
import io.floci.aws.services.redshift.RedshiftJdbcTargets;
|
||||
import io.floci.aws.services.redshift.RedshiftService;
|
||||
import io.floci.aws.services.redshiftserverless.RedshiftServerlessService;
|
||||
import io.floci.aws.services.secretsmanager.SecretsManagerService;
|
||||
@@ -68,9 +69,9 @@ class FirehoseRedshiftRoleAccessTest {
|
||||
when(scpProvider.isResolvable()).thenReturn(false);
|
||||
ServiceRoleAuthorizer authorizer = new ServiceRoleAuthorizer(config, iamService, evaluator,
|
||||
new AssumeRolePolicyEvaluator(new ObjectMapper(), evaluator), scpProvider);
|
||||
delivery = new FirehoseRedshiftDelivery(mock(RedshiftService.class), mock(RedshiftServerlessService.class),
|
||||
mock(SecretsManagerService.class), authorizer, iamService, new ObjectMapper(),
|
||||
(url, user, password, sql) -> { });
|
||||
delivery = new FirehoseRedshiftDelivery(new RedshiftJdbcTargets(mock(RedshiftService.class),
|
||||
mock(RedshiftServerlessService.class), mock(SecretsManagerService.class), new ObjectMapper()),
|
||||
authorizer, iamService, (url, user, password, sql) -> { });
|
||||
}
|
||||
|
||||
private void role(String name, String arn, String trust, String policy) {
|
||||
|
||||
@@ -7,6 +7,7 @@ import io.floci.aws.core.storage.StorageFactory;
|
||||
import io.floci.aws.services.glue.model.Crawler;
|
||||
import io.floci.aws.services.glue.model.CrawlerRunRecord;
|
||||
import io.floci.aws.services.glue.model.CrawlerTargets;
|
||||
import io.floci.aws.services.glue.model.LastCrawlInfo;
|
||||
import io.floci.aws.services.glue.model.S3Target;
|
||||
import io.floci.aws.services.glue.model.Schedule;
|
||||
import io.floci.aws.services.glue.schemaregistry.GlueSchemaRegistryService;
|
||||
@@ -29,6 +30,7 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
@@ -68,6 +70,79 @@ class GlueCrawlerRunServiceTest {
|
||||
return new GlueCrawlerRunService(recordStore, new InMemoryStorage<>(), glueService, runDurationSeconds, clock);
|
||||
}
|
||||
|
||||
private GlueCrawlerRunService service(CrawlRunner runner) {
|
||||
return new GlueCrawlerRunService(recordStore, new InMemoryStorage<>(), glueService, 0, clock, runner);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aCrawlThatFailsReportsFailedWithItsErrorMessageAndNoTableChanges() {
|
||||
createCrawler("raw", null);
|
||||
GlueCrawlerRunService service = service(name -> CrawlOutcome.failed("Connection missing not found."));
|
||||
|
||||
service.startCrawler("raw");
|
||||
|
||||
Crawler crawler = service.withRunState(glueService.getCrawler("raw"));
|
||||
assertEquals("READY", crawler.getState());
|
||||
assertEquals("FAILED", crawler.getLastCrawl().getStatus());
|
||||
assertEquals("Connection missing not found.", crawler.getLastCrawl().getErrorMessage());
|
||||
Map<String, Object> metrics = metrics(service, "raw");
|
||||
assertEquals(0, metrics.get("TablesCreated"));
|
||||
assertEquals(0, metrics.get("TablesUpdated"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aRunnerThatThrowsEndsTheCrawlAsFailedInsteadOfLeavingItActive() {
|
||||
createCrawler("raw", null);
|
||||
AtomicInteger calls = new AtomicInteger();
|
||||
GlueCrawlerRunService service = service(name -> {
|
||||
calls.incrementAndGet();
|
||||
throw new IllegalStateException("backend exploded");
|
||||
});
|
||||
|
||||
service.startCrawler("raw", null, "wfr_1");
|
||||
|
||||
assertFalse(service.isRunning("raw"));
|
||||
assertEquals(1, calls.get());
|
||||
LastCrawlInfo lastCrawl = service.withRunState(glueService.getCrawler("raw")).getLastCrawl();
|
||||
assertEquals("FAILED", lastCrawl.getStatus());
|
||||
assertEquals("backend exploded", lastCrawl.getErrorMessage());
|
||||
assertEquals("FAILED", service.crawlsInWorkflowRun("wfr_1").get(0).state());
|
||||
service.startCrawler("raw");
|
||||
assertEquals(2, calls.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void metricsReportTheTableChangesOfTheLastCrawl() {
|
||||
createCrawler("raw", null);
|
||||
List<CrawlOutcome> outcomes = List.of(CrawlOutcome.succeeded(3, 0, 0), CrawlOutcome.succeeded(1, 2, 0));
|
||||
int[] next = {0};
|
||||
GlueCrawlerRunService service = service(name -> outcomes.get(next[0]++));
|
||||
|
||||
service.startCrawler("raw");
|
||||
service.startCrawler("raw");
|
||||
|
||||
Map<String, Object> metrics = metrics(service, "raw");
|
||||
assertEquals(1, metrics.get("TablesCreated"));
|
||||
assertEquals(2, metrics.get("TablesUpdated"));
|
||||
assertEquals(0, metrics.get("TablesDeleted"));
|
||||
assertEquals("SUCCEEDED", service.withRunState(glueService.getCrawler("raw")).getLastCrawl().getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aLaterSuccessfulCrawlClearsTheErrorMessageOfAFailedOne() {
|
||||
createCrawler("raw", null);
|
||||
List<CrawlOutcome> outcomes = List.of(CrawlOutcome.failed("boom"), CrawlOutcome.succeeded());
|
||||
int[] next = {0};
|
||||
GlueCrawlerRunService service = service(name -> outcomes.get(next[0]++));
|
||||
|
||||
service.startCrawler("raw");
|
||||
service.startCrawler("raw");
|
||||
|
||||
LastCrawlInfo lastCrawl = service.withRunState(glueService.getCrawler("raw")).getLastCrawl();
|
||||
assertEquals("SUCCEEDED", lastCrawl.getStatus());
|
||||
assertNull(lastCrawl.getErrorMessage());
|
||||
}
|
||||
|
||||
private void createCrawler(String name, String scheduleExpression) {
|
||||
S3Target target = new S3Target();
|
||||
target.setPath("s3://raw/" + name);
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
package io.floci.aws.services.redshift;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.DatabaseSecretCredentials;
|
||||
import io.floci.aws.services.redshift.RedshiftJdbcTargets.Backend;
|
||||
import io.floci.aws.services.redshift.model.Cluster;
|
||||
import io.floci.aws.services.redshift.model.Endpoint;
|
||||
import io.floci.aws.services.redshiftserverless.RedshiftServerlessService;
|
||||
import io.floci.aws.services.redshiftserverless.RedshiftServerlessService.WorkgroupTarget;
|
||||
import io.floci.aws.services.secretsmanager.SecretsManagerService;
|
||||
import io.floci.aws.services.secretsmanager.model.SecretVersion;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
class RedshiftJdbcTargetsTest {
|
||||
|
||||
private static final String ACCOUNT_ID = "000000000000";
|
||||
private static final String REGION = "us-east-1";
|
||||
private static final String SECRET_ARN = "arn:aws:secretsmanager:us-east-1:000000000000:secret:admin-AbCdEf";
|
||||
|
||||
private RedshiftService redshift;
|
||||
private RedshiftServerlessService serverless;
|
||||
private SecretsManagerService secrets;
|
||||
private RedshiftJdbcTargets targets;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
redshift = mock(RedshiftService.class);
|
||||
serverless = mock(RedshiftServerlessService.class);
|
||||
secrets = mock(SecretsManagerService.class);
|
||||
targets = new RedshiftJdbcTargets(redshift, serverless, secrets, new ObjectMapper());
|
||||
}
|
||||
|
||||
private static RedshiftJdbcUrl clusterUrl() {
|
||||
return RedshiftJdbcUrl.parse("jdbc:redshift://c1.abc.us-east-1.redshift.amazonaws.com:5439/analytics");
|
||||
}
|
||||
|
||||
private static RedshiftJdbcUrl workgroupUrl(String database) {
|
||||
return RedshiftJdbcUrl.parse("jdbc:redshift://wg1.000000000000.us-east-1.redshift-serverless.amazonaws.com:5439/"
|
||||
+ database);
|
||||
}
|
||||
|
||||
private void workgroup(String database, Endpoint endpoint, int runtimePort) {
|
||||
when(serverless.getWorkgroupTarget("wg1", REGION)).thenReturn(new WorkgroupTarget(
|
||||
"arn:aws:redshift-serverless:us-east-1:000000000000:workgroup/id1", "wg1", "host.docker.internal",
|
||||
runtimePort, database, "workgroupadmin", "ignored", "workgroupadmin", List.of(), null, endpoint));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aClusterUrlResolvesToTheClusterPortAndTheEmulatedDevDatabase() {
|
||||
Cluster cluster = mock(Cluster.class);
|
||||
when(cluster.getEndpoint()).thenReturn(new Endpoint("host.docker.internal", 41234));
|
||||
when(cluster.getMasterUsername()).thenReturn("admin");
|
||||
when(redshift.describeClustersForAccount(ACCOUNT_ID, "c1")).thenReturn(List.of(cluster));
|
||||
|
||||
Backend backend = targets.resolve(ACCOUNT_ID, REGION, clusterUrl());
|
||||
|
||||
assertEquals(new Backend("cluster c1", "127.0.0.1", 41234, "dev", "admin"), backend);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aClusterThatDoesNotExistIsRefusedByTheRedshiftService() {
|
||||
when(redshift.describeClustersForAccount(ACCOUNT_ID, "c1"))
|
||||
.thenThrow(new AwsException("ClusterNotFound", "Cluster c1 not found.", 404));
|
||||
|
||||
assertThrows(AwsException.class, () -> targets.resolve(ACCOUNT_ID, REGION, clusterUrl()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void aServerlessUrlResolvesToTheWorkgroupEndpointItsDatabaseAndItsAdminUser() {
|
||||
workgroup("analytics", new Endpoint("localhost", 41300), 5432);
|
||||
|
||||
Backend backend = targets.resolve(ACCOUNT_ID, REGION, workgroupUrl("analytics"));
|
||||
|
||||
assertEquals(new Backend("workgroup wg1", "127.0.0.1", 41300, "analytics", "workgroupadmin"), backend);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aWorkgroupWithoutAnEndpointFallsBackToItsRuntimePort() {
|
||||
workgroup("analytics", null, 5432);
|
||||
|
||||
assertEquals(5432, targets.resolve(ACCOUNT_ID, REGION, workgroupUrl("analytics")).port());
|
||||
}
|
||||
|
||||
@Test
|
||||
void aServerlessUrlNamingAnotherDatabaseIsRefused() {
|
||||
workgroup("analytics", new Endpoint("localhost", 41300), 5432);
|
||||
|
||||
IllegalStateException refused = assertThrows(IllegalStateException.class,
|
||||
() -> targets.resolve(ACCOUNT_ID, REGION, workgroupUrl("other")));
|
||||
|
||||
assertTrue(refused.getMessage().contains("Database other does not exist in workgroup wg1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void credentialsComeFromTheUsernameAndPasswordInTheSecret() {
|
||||
SecretVersion version = mock(SecretVersion.class);
|
||||
when(version.getSecretString()).thenReturn("{\"username\":\"admin\",\"password\":\"pw-managed\"}");
|
||||
when(secrets.getSecretValue(eq(SECRET_ARN), any(), any(), eq(REGION))).thenReturn(version);
|
||||
|
||||
DatabaseSecretCredentials credentials = targets.credentialsFromSecret(SECRET_ARN, REGION);
|
||||
|
||||
assertEquals(new DatabaseSecretCredentials("admin", "pw-managed"), credentials);
|
||||
}
|
||||
|
||||
@Test
|
||||
void aSecretWithoutUsernameAndPasswordIsRefused() {
|
||||
SecretVersion version = mock(SecretVersion.class);
|
||||
when(version.getSecretString()).thenReturn("{\"token\":\"x\"}");
|
||||
when(secrets.getSecretValue(eq(SECRET_ARN), any(), any(), eq(REGION))).thenReturn(version);
|
||||
|
||||
IllegalStateException refused = assertThrows(IllegalStateException.class,
|
||||
() -> targets.credentialsFromSecret(SECRET_ARN, REGION));
|
||||
|
||||
assertEquals("Secret " + SECRET_ARN + " does not contain username and password fields.",
|
||||
refused.getMessage());
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package io.floci.aws.services.firehose;
|
||||
package io.floci.aws.services.redshift;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
Reference in new issue
Block a user