mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(redshift): scope clusters to the Region they were created in (#8421)
A Redshift cluster is Regional, but Floci found clusters by identifier alone, so DescribeClusters in one Region listed a cluster created in another, and that Region could modify, pause, reboot, snapshot or delete it. Every call naming a cluster now finds it only in the Region it was created in, through regionalCluster, and answers ClusterNotFound elsewhere; DescribeClusters and DescribeTags list only the request Region's clusters. A managed password rotation refuses changes only in its cluster's Region, so it is not seen from another. Work done outside a request, such as snapshot copies, zero-ETL, Firehose delivery and metrics, still finds a cluster in any Region, and a cluster identifier stays unique across the account, as the CreateCluster reference documents. Part of #8218
This commit is contained in:
1 parent
0baf1a1b79
commit
35a9ca0f4b
3 files changed
+159
-65
No files matched your search
@@ -42,6 +42,7 @@ import jakarta.ws.rs.core.MultivaluedHashMap;
|
||||
import jakarta.ws.rs.core.MultivaluedMap;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
@@ -384,6 +385,107 @@ class RedshiftServiceTest {
|
||||
return cluster;
|
||||
}
|
||||
|
||||
/**
|
||||
* A cluster is Regional: it is listed and found only in the Region it was created in, and every call
|
||||
* naming it from another Region of the account answers ClusterNotFound.
|
||||
*/
|
||||
@Test
|
||||
void aClusterIsListedAndFoundOnlyInTheRegionItWasCreatedIn() {
|
||||
Cluster cluster = storedCluster("analytics", "us-east-1");
|
||||
cluster.setTags(Map.of("team", "data"));
|
||||
when(clusterBackend.scan(any())).thenReturn(List.of(cluster));
|
||||
RedshiftService west = serviceIn("eu-west-1");
|
||||
|
||||
assertEquals(List.of(cluster), service.describeClusters(null));
|
||||
assertEquals(List.of(cluster), service.describeClusters("analytics"));
|
||||
assertEquals(List.of(new RedshiftService.TaggedResource(
|
||||
"arn:aws:redshift:us-east-1:111111111111:cluster:analytics", "cluster", "team", "data")),
|
||||
service.describeTags(null, "cluster", null));
|
||||
assertEquals(List.of(), west.describeClusters(null));
|
||||
assertEquals(List.of(), west.describeTags(null, "cluster", null));
|
||||
assertEquals(Optional.empty(), west.findClusterByArn("arn:aws:redshift:eu-west-1:111111111111:cluster:analytics"));
|
||||
for (Executable call : List.<Executable>of(
|
||||
() -> west.describeClusters("analytics"),
|
||||
() -> west.modifyCluster("analytics", null, null, null, null, List.of()),
|
||||
() -> west.modifyClusterIamRoles("analytics", List.of(), List.of()),
|
||||
() -> west.describeLoggingStatus("analytics"),
|
||||
() -> west.enableLogging("analytics", null, null, "cloudwatch", List.of("connectionlog")),
|
||||
() -> west.disableLogging("analytics"),
|
||||
() -> west.pauseCluster("analytics"),
|
||||
() -> west.resumeCluster("analytics"),
|
||||
() -> west.rebootCluster("analytics"),
|
||||
() -> west.createSnapshot("nightly", "analytics"),
|
||||
() -> west.enableSnapshotCopy("analytics", "us-west-2", null, null, null),
|
||||
() -> west.modifyClusterSnapshotSchedule("analytics", null, true),
|
||||
() -> west.createTags("arn:aws:redshift:eu-west-1:111111111111:cluster:analytics", Map.of("k", "v")),
|
||||
() -> west.deleteCluster("analytics"))) {
|
||||
assertEquals("ClusterNotFound", assertThrows(AwsException.class, call).getErrorCode());
|
||||
}
|
||||
verify(clusterBackend, never()).put(any(), any());
|
||||
verify(clusterBackend, never()).delete(any());
|
||||
}
|
||||
|
||||
/**
|
||||
* Work done outside a request on a cluster's behalf learns its Region: the one it was created in, and the
|
||||
* default Region for a cluster stored before clusters kept one.
|
||||
*/
|
||||
@Test
|
||||
void aClustersRegionIsTheOneItWasCreatedIn() {
|
||||
storedCluster("analytics", "eu-west-1");
|
||||
storedCluster("legacy", null);
|
||||
RedshiftService west = serviceIn("eu-west-1");
|
||||
|
||||
assertEquals(Optional.of("eu-west-1"), service.clusterRegion("111111111111", "analytics"));
|
||||
assertEquals(Optional.of("us-east-1"), west.clusterRegion("111111111111", "legacy"));
|
||||
assertEquals(Optional.empty(), service.clusterRegion("111111111111", "missing"));
|
||||
}
|
||||
|
||||
/**
|
||||
* A managed master password rotation refuses changes to its cluster in the cluster's Region only: from another
|
||||
* Region the cluster is not found, as for any change there, so its rotation is not seen.
|
||||
*/
|
||||
@Test
|
||||
void aRotatingClusterIsNotFoundFromAnotherRegion() {
|
||||
Cluster cluster = storedCluster("managed-cluster", "us-east-1");
|
||||
cluster.setMasterPasswordSecretArn("arn:aws:secretsmanager:us-east-1:111111111111:secret:redshift-managed");
|
||||
when(clusterBackend.scanAllAccountEntries(any())).thenReturn(List.of(
|
||||
new AccountAwareStorageBackend.AccountEntry<>("111111111111", "managed-cluster", cluster)));
|
||||
RedshiftService west = serviceIn("eu-west-1");
|
||||
|
||||
for (RedshiftService rotating : List.of(service, west)) {
|
||||
rotating.reserve(cluster.getMasterPasswordSecretArn(), "us-east-1");
|
||||
String expected = rotating == service ? "InvalidRequestException" : "ClusterNotFound";
|
||||
try {
|
||||
assertEquals(expected, assertThrows(AwsException.class,
|
||||
() -> rotating.deleteCluster("managed-cluster")).getErrorCode());
|
||||
assertEquals(expected, assertThrows(AwsException.class,
|
||||
() -> rotating.rebootCluster("managed-cluster")).getErrorCode());
|
||||
} finally {
|
||||
rotating.release(cluster.getMasterPasswordSecretArn(), "us-east-1");
|
||||
}
|
||||
}
|
||||
verify(cm, never()).stop("111111111111", "managed-cluster");
|
||||
}
|
||||
|
||||
/** An available cluster stored in account 111111111111, created in {@code region}; null for none recorded. */
|
||||
private Cluster storedCluster(String identifier, String region) {
|
||||
Cluster cluster = new Cluster();
|
||||
cluster.setClusterIdentifier(identifier);
|
||||
cluster.setClusterStatus("available");
|
||||
cluster.setSnapshotRegion(region);
|
||||
when(clusterBackend.get(identifier)).thenReturn(Optional.of(cluster));
|
||||
when(clusterBackend.getForAccount("111111111111", identifier)).thenReturn(Optional.of(cluster));
|
||||
return cluster;
|
||||
}
|
||||
|
||||
/** A service whose calls run in {@code region}, the default Region staying us-east-1. */
|
||||
private RedshiftService serviceIn(String region) {
|
||||
RegionResolver resolver = spy(new RegionResolver("us-east-1", "111111111111"));
|
||||
doReturn(region).when(resolver).getRegion();
|
||||
return new RedshiftService(sf, cm, config, resolver, proxyManager, dockerHostResolver,
|
||||
credentialBroker, secretsManagerService, new ObjectMapper(), null, null);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(booleans = {false, true})
|
||||
void loggingDestinationRegionSurvivesStorageAndRestart(boolean refresh) throws Exception {
|
||||
@@ -980,25 +1082,6 @@ class RedshiftServiceTest {
|
||||
autoCopyConsumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Work done outside a request on a cluster's behalf, Redshift Spectrum over JDBC, learns the cluster's Region: the
|
||||
* one it was created in, and the default Region for a cluster stored before clusters kept one.
|
||||
*/
|
||||
@Test
|
||||
void aClustersRegionIsTheOneItWasCreatedIn() {
|
||||
Cluster analytics = new Cluster();
|
||||
analytics.setClusterIdentifier("analytics");
|
||||
analytics.setSnapshotRegion("eu-west-1");
|
||||
Cluster legacy = new Cluster();
|
||||
legacy.setClusterIdentifier("legacy");
|
||||
when(clusterBackend.getForAccount("111111111111", "analytics")).thenReturn(Optional.of(analytics));
|
||||
when(clusterBackend.getForAccount("111111111111", "legacy")).thenReturn(Optional.of(legacy));
|
||||
|
||||
assertEquals(Optional.of("eu-west-1"), service.clusterRegion("111111111111", "analytics"));
|
||||
assertEquals(Optional.of("us-east-1"), service.clusterRegion("111111111111", "legacy"));
|
||||
assertEquals(Optional.empty(), service.clusterRegion("111111111111", "missing"));
|
||||
}
|
||||
|
||||
private static final String ORDERS_TABLE_ARN = "arn:aws:dynamodb:us-east-1:111111111111:table/orders";
|
||||
private static final String NAMESPACE_ARN = "arn:aws:redshift-serverless:us-east-1:111111111111:namespace/"
|
||||
+ "8f3e0b52-6c1a-4f0e-9d7b-2a4c5e6f7a8b";
|
||||
|
||||
@@ -211,9 +211,10 @@ class RedshiftSnapshotCopyServiceTest {
|
||||
fixture.service(SOURCE).createSnapshot("nightly", "warehouse");
|
||||
fixture.service(SOURCE).runSnapshotCopies(Instant.now());
|
||||
fixture.service(SOURCE).deleteSnapshot("nightly");
|
||||
seedCluster(fixture, "replica", DESTINATION, null, null);
|
||||
|
||||
AwsException refused = assertThrows(AwsException.class,
|
||||
() -> fixture.service(DESTINATION).createSnapshot("nightly", "warehouse"));
|
||||
() -> fixture.service(DESTINATION).createSnapshot("nightly", "replica"));
|
||||
assertEquals("ClusterSnapshotAlreadyExists", refused.getErrorCode());
|
||||
}
|
||||
|
||||
@@ -347,14 +348,20 @@ class RedshiftSnapshotCopyServiceTest {
|
||||
}
|
||||
|
||||
private static Cluster seedCluster(Fixture fixture, String id, Instant copyEnabledAt) {
|
||||
return seedCluster(fixture, id, SOURCE, DESTINATION, copyEnabledAt);
|
||||
}
|
||||
|
||||
/** An available cluster of {@code region}, copying its snapshots to {@code copyDestination} when not null. */
|
||||
private static Cluster seedCluster(Fixture fixture, String id, String region, String copyDestination,
|
||||
Instant copyEnabledAt) {
|
||||
Cluster cluster = new Cluster();
|
||||
cluster.setClusterIdentifier(id);
|
||||
cluster.setNodeType("dc2.large");
|
||||
cluster.setMasterUsername("admin");
|
||||
cluster.setMasterPassword("Secret123!");
|
||||
cluster.setClusterStatus("available");
|
||||
cluster.setSnapshotRegion(SOURCE);
|
||||
cluster.setSnapshotCopyDestinationRegion(DESTINATION);
|
||||
cluster.setSnapshotRegion(region);
|
||||
cluster.setSnapshotCopyDestinationRegion(copyDestination);
|
||||
cluster.setSnapshotCopyRetentionPeriod(7);
|
||||
cluster.setSnapshotCopyManualRetentionPeriod(-1);
|
||||
cluster.setSnapshotCopyEnabledAt(copyEnabledAt);
|
||||
|
||||
Reference in new issue
Block a user