mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(msk): isolate MSK resources by account and region (#3353)
* fix: isolate MSK clusters by region * fix(msk): scope broker resources by account and region * fix(msk): preserve legacy cluster data during identity migration * fix(msk): restrict legacy storage fallback to old clusters * ci: retrigger PR checks after unrelated test flake
This commit is contained in:
1 parent
5198cf29ec
commit
970cd301d1
5 files changed
+185
-23
No files matched your search
@@ -56,6 +56,7 @@ class MskServiceTest {
|
||||
private StorageFactory storageFactory;
|
||||
private EmulatorConfig config;
|
||||
private RedpandaManager redpandaManager;
|
||||
private RegionResolver regionResolver;
|
||||
// MskService's constructor creates the cluster backend first and the configuration backend
|
||||
// second; captured here so tests can seed raw entries directly into the configuration store
|
||||
// (e.g. to simulate a pre-revision-history persisted entry) without exposing it from MskService.
|
||||
@@ -87,7 +88,10 @@ class MskServiceTest {
|
||||
when(config.defaultRegion()).thenReturn("us-east-1");
|
||||
|
||||
redpandaManager = Mockito.mock(RedpandaManager.class);
|
||||
RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");
|
||||
regionResolver = Mockito.mock(RegionResolver.class);
|
||||
when(regionResolver.getRegion()).thenReturn("us-east-1");
|
||||
when(regionResolver.getAccountId()).thenReturn("000000000000");
|
||||
when(regionResolver.getDefaultRegion()).thenReturn("us-east-1");
|
||||
mskService = new MskService(storageFactory, config, regionResolver, redpandaManager);
|
||||
}
|
||||
|
||||
@@ -129,6 +133,53 @@ class MskServiceTest {
|
||||
assertEquals(2, clusters.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void sameClusterNameIsAllowedInDifferentRegionsAndListsOnlyCurrentRegion() {
|
||||
MskCluster east = mskService.createCluster("shared-name");
|
||||
|
||||
when(regionResolver.getRegion()).thenReturn("eu-west-1");
|
||||
MskCluster west = mskService.createCluster("shared-name");
|
||||
|
||||
assertNotEquals(east.getClusterArn(), west.getClusterArn());
|
||||
assertTrue(east.getClusterArn().contains(":us-east-1:"));
|
||||
assertTrue(west.getClusterArn().contains(":eu-west-1:"));
|
||||
assertEquals(List.of(west), mskService.listClusters());
|
||||
|
||||
when(regionResolver.getRegion()).thenReturn("us-east-1");
|
||||
assertEquals(List.of(east), mskService.listClusters());
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyClusterWithoutResourceRegionRemainsVisibleAfterRegionalIsolation() {
|
||||
MskCluster legacy = mskService.createCluster("legacy-cluster");
|
||||
legacy.setResourceRegion(null);
|
||||
|
||||
when(regionResolver.getRegion()).thenReturn("eu-west-1");
|
||||
|
||||
assertEquals(List.of(legacy), mskService.listClusters());
|
||||
assertEquals(legacy, mskService.describeCluster(legacy.getClusterArn()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sameClusterNameIsRejectedWithinOneRegion() {
|
||||
mskService.createCluster("shared-name");
|
||||
|
||||
AwsException error = assertThrows(AwsException.class, () -> mskService.createCluster("shared-name"));
|
||||
|
||||
assertEquals("ConflictException", error.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void clusterCannotBeDescribedOrDeletedFromAnotherRegion() {
|
||||
MskCluster east = mskService.createCluster("regional-cluster");
|
||||
|
||||
when(regionResolver.getRegion()).thenReturn("eu-west-1");
|
||||
|
||||
assertThrows(AwsException.class, () -> mskService.describeCluster(east.getClusterArn()));
|
||||
assertThrows(AwsException.class, () -> mskService.deleteCluster(east.getClusterArn()));
|
||||
assertEquals(0, mskService.listClusters().size());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deleteCluster() {
|
||||
MskCluster cluster = mskService.createCluster("test-cluster");
|
||||
@@ -355,6 +406,8 @@ class MskServiceTest {
|
||||
assertNotNull(reloaded.getVolumeId());
|
||||
assertEquals(cluster.getAccountId(), reloaded.getAccountId());
|
||||
assertNotNull(reloaded.getAccountId());
|
||||
assertEquals(cluster.getResourceRegion(), reloaded.getResourceRegion());
|
||||
assertNotNull(reloaded.getResourceRegion());
|
||||
|
||||
// and the client-facing metadata survives too
|
||||
assertEquals(3, reloaded.getNumberOfBrokerNodes());
|
||||
|
||||
@@ -38,6 +38,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
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.RETURNS_DEEP_STUBS;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.mock;
|
||||
@@ -102,6 +103,7 @@ class RedpandaManagerTest {
|
||||
lenient().when(logStreamer.generateLogStreamName(any())).thenReturn("log-stream");
|
||||
lenient().when(regionResolver.getDefaultRegion()).thenReturn("us-east-1");
|
||||
lenient().when(regionResolver.getAccountId()).thenReturn("000000000000");
|
||||
lenient().when(regionResolver.getDefaultAccountId()).thenReturn("000000000000");
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
@@ -230,6 +232,69 @@ class RedpandaManagerTest {
|
||||
specCaptor.getValue().labels());
|
||||
}
|
||||
|
||||
@Test
|
||||
void startContainerUsesAccountAwareLogsAndScopedHostPath() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(false);
|
||||
when(portAllocator.allocate(9300, 9399)).thenReturn(54321);
|
||||
|
||||
ContainerInfo info = new ContainerInfo("container-789",
|
||||
Map.of(KAFKA_PORT, new EndpointInfo("localhost", 54321)));
|
||||
when(lifecycleManager.createAndStart(any())).thenReturn(info);
|
||||
|
||||
ArgumentCaptor<ContainerSpec> specCaptor = ArgumentCaptor.forClass(ContainerSpec.class);
|
||||
manager.startContainer(newCluster());
|
||||
|
||||
verify(logStreamer).attachForAccount(eq("000000000000"), eq("container-789"),
|
||||
eq("/aws/msk/cluster/test-cluster"), eq("log-stream"), eq("us-east-1"),
|
||||
eq("msk:test-cluster"));
|
||||
verify(lifecycleManager).createAndStart(specCaptor.capture());
|
||||
String hostPath = specCaptor.getValue().binds().get(0).getPath().toString();
|
||||
assertTrue(hostPath.contains("000000000000"));
|
||||
assertTrue(hostPath.contains("us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void startContainerReusesLegacyHostPathWhenScopedPathIsMissing() throws Exception {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(false);
|
||||
when(portAllocator.allocate(9300, 9399)).thenReturn(54321);
|
||||
Files.createDirectories(tempDir.resolve("msk").resolve("test-cluster"));
|
||||
|
||||
MskCluster cluster = newCluster();
|
||||
cluster.setResourceRegion(null);
|
||||
ContainerInfo info = new ContainerInfo("container-legacy",
|
||||
Map.of(KAFKA_PORT, new EndpointInfo("localhost", 54321)));
|
||||
when(lifecycleManager.createAndStart(any())).thenReturn(info);
|
||||
|
||||
ArgumentCaptor<ContainerSpec> specCaptor = ArgumentCaptor.forClass(ContainerSpec.class);
|
||||
manager.startContainer(cluster);
|
||||
|
||||
verify(lifecycleManager).createAndStart(specCaptor.capture());
|
||||
assertEquals(tempDir.resolve("msk").resolve("test-cluster").toAbsolutePath(),
|
||||
Path.of(specCaptor.getValue().binds().get(0).getPath()).toAbsolutePath());
|
||||
}
|
||||
|
||||
@Test
|
||||
void startContainerDoesNotReuseLegacyPathForScopedCluster() throws Exception {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(false);
|
||||
when(portAllocator.allocate(9300, 9399)).thenReturn(54321);
|
||||
Files.createDirectories(tempDir.resolve("msk").resolve("test-cluster"));
|
||||
|
||||
MskCluster cluster = newCluster();
|
||||
cluster.setResourceRegion("us-east-1");
|
||||
ContainerInfo info = new ContainerInfo("container-scoped",
|
||||
Map.of(KAFKA_PORT, new EndpointInfo("localhost", 54321)));
|
||||
when(lifecycleManager.createAndStart(any())).thenReturn(info);
|
||||
|
||||
ArgumentCaptor<ContainerSpec> specCaptor = ArgumentCaptor.forClass(ContainerSpec.class);
|
||||
manager.startContainer(cluster);
|
||||
|
||||
verify(lifecycleManager).createAndStart(specCaptor.capture());
|
||||
assertTrue(Path.of(specCaptor.getValue().binds().get(0).getPath()).toAbsolutePath()
|
||||
.toString().contains("000000000000"));
|
||||
assertTrue(Path.of(specCaptor.getValue().binds().get(0).getPath()).toAbsolutePath()
|
||||
.toString().contains("us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void stopContainerReleasesAllocatedKafkaHostPort() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(false);
|
||||
|
||||
Reference in new issue
Block a user