mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(mwaa): isolate environments by account and region (#3400)
* fix(mwaa): isolate environments by account and region * fix(mwaa): migrate legacy environment records safely
This commit is contained in:
1 parent
5bf8075d52
commit
c6e64a0977
5 files changed
+363
-67
No files matched your search
+52
-17
@@ -1,7 +1,6 @@
|
||||
package io.github.hectorvent.floci.services.mwaa;
|
||||
|
||||
import io.github.hectorvent.floci.config.EmulatorConfig;
|
||||
import io.github.hectorvent.floci.core.common.RegionResolver;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerBuilder;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerDetector;
|
||||
import io.github.hectorvent.floci.core.common.docker.ContainerLifecycleManager;
|
||||
@@ -27,6 +26,7 @@ import java.util.Optional;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -41,7 +41,6 @@ class MwaaEnvironmentManagerTest {
|
||||
|
||||
private ContainerLifecycleManager lifecycleManager;
|
||||
private ContainerDetector containerDetector;
|
||||
private RegionResolver regionResolver;
|
||||
private MwaaEnvironmentManager manager;
|
||||
|
||||
@BeforeEach
|
||||
@@ -78,13 +77,14 @@ class MwaaEnvironmentManagerTest {
|
||||
"AWS_ACCESS_KEY_ID=test",
|
||||
"AWS_SECRET_ACCESS_KEY=test",
|
||||
"AWS_ENDPOINT_URL=http://localhost:4566"));
|
||||
|
||||
regionResolver = Mockito.mock(RegionResolver.class);
|
||||
when(regionResolver.getAccountId()).thenReturn("000000000000");
|
||||
when(regionResolver.getDefaultRegion()).thenReturn("us-east-1");
|
||||
when(awsEnv.sdkBaselineEnv(eq("eu-west-1"), any())).thenReturn(List.of(
|
||||
"AWS_DEFAULT_REGION=eu-west-1",
|
||||
"AWS_ACCESS_KEY_ID=test",
|
||||
"AWS_SECRET_ACCESS_KEY=test",
|
||||
"AWS_ENDPOINT_URL=http://localhost:4566"));
|
||||
|
||||
manager = new MwaaEnvironmentManager(containerBuilder, lifecycleManager, containerDetector, config,
|
||||
awsEnv, regionResolver);
|
||||
awsEnv);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -157,6 +157,24 @@ class MwaaEnvironmentManagerTest {
|
||||
assertTrue(spec.env().contains("AWS_DEFAULT_REGION=us-east-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void airflowContainerUsesTheEnvironmentRegionForItsOwnAwsSdkCalls() {
|
||||
when(containerDetector.isRunningInContainer()).thenReturn(false);
|
||||
when(lifecycleManager.create(any())).thenReturn("airflow-container-id");
|
||||
when(lifecycleManager.startCreated(eq("airflow-container-id"), any())).thenReturn(
|
||||
new ContainerInfo("airflow-container-id", Map.of(8080, new EndpointInfo("172.18.0.5", 8080))));
|
||||
|
||||
Environment environment = new Environment();
|
||||
environment.setName("my-env");
|
||||
environment.setArn("arn:aws:airflow:eu-west-1:111111111111:environment/my-env");
|
||||
|
||||
manager.startAirflowContainer(environment, "2.10.5", "172.18.0.9", "db-secret-pw", null);
|
||||
|
||||
ArgumentCaptor<ContainerSpec> captor = ArgumentCaptor.forClass(ContainerSpec.class);
|
||||
verify(lifecycleManager).create(captor.capture());
|
||||
assertTrue(captor.getValue().env().contains("AWS_DEFAULT_REGION=eu-west-1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void entrypointAndCmdOverrideTheStockImageDefaults() {
|
||||
ContainerSpec spec = startAirflowAndCaptureSpec(false);
|
||||
@@ -215,12 +233,6 @@ class MwaaEnvironmentManagerTest {
|
||||
assertFalse(spec.portBindings().containsKey(8080));
|
||||
}
|
||||
|
||||
@Test
|
||||
void containerNamingMatchesThePlan() {
|
||||
assertEquals("floci-mwaa-my-env-db", MwaaEnvironmentManager.dbContainerName(null, "my-env"));
|
||||
assertEquals("floci-mwaa-my-env-airflow", MwaaEnvironmentManager.airflowContainerName(null, "my-env"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void containerNamingAppliesTheConfiguredResourceNamespace() {
|
||||
EmulatorConfig.DockerConfig dockerConfig = Mockito.mock(EmulatorConfig.DockerConfig.class);
|
||||
@@ -228,10 +240,33 @@ class MwaaEnvironmentManagerTest {
|
||||
EmulatorConfig namespacedConfig = Mockito.mock(EmulatorConfig.class);
|
||||
when(namespacedConfig.docker()).thenReturn(dockerConfig);
|
||||
|
||||
assertEquals("floci-ns1-mwaa-my-env-db",
|
||||
MwaaEnvironmentManager.dbContainerName(namespacedConfig, "my-env"));
|
||||
assertEquals("floci-ns1-mwaa-my-env-airflow",
|
||||
MwaaEnvironmentManager.airflowContainerName(namespacedConfig, "my-env"));
|
||||
Environment environment = new Environment();
|
||||
environment.setName("my-env");
|
||||
environment.setAccountId("000000000000");
|
||||
environment.setArn("arn:aws:airflow:us-east-1:000000000000:environment/my-env");
|
||||
|
||||
assertEquals("floci-ns1-mwaa-000000000000.us-east-1.my-env-db",
|
||||
MwaaEnvironmentManager.dbContainerName(namespacedConfig, environment));
|
||||
assertEquals("floci-ns1-mwaa-000000000000.us-east-1.my-env-airflow",
|
||||
MwaaEnvironmentManager.airflowContainerName(namespacedConfig, environment));
|
||||
}
|
||||
|
||||
@Test
|
||||
void scopedContainerNamesDifferForSameNameEnvironments() {
|
||||
Environment eastEnvironment = new Environment();
|
||||
eastEnvironment.setName("shared-name");
|
||||
eastEnvironment.setAccountId("111111111111");
|
||||
eastEnvironment.setArn("arn:aws:airflow:us-east-1:111111111111:environment/shared-name");
|
||||
|
||||
Environment westEnvironment = new Environment();
|
||||
westEnvironment.setName("shared-name");
|
||||
westEnvironment.setAccountId("222222222222");
|
||||
westEnvironment.setArn("arn:aws:airflow:eu-west-1:222222222222:environment/shared-name");
|
||||
|
||||
assertNotEquals(MwaaEnvironmentManager.dbContainerName(null, eastEnvironment),
|
||||
MwaaEnvironmentManager.dbContainerName(null, westEnvironment));
|
||||
assertNotEquals(MwaaEnvironmentManager.airflowContainerName(null, eastEnvironment),
|
||||
MwaaEnvironmentManager.airflowContainerName(null, westEnvironment));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -31,6 +31,7 @@ import java.util.Optional;
|
||||
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.assertNotEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
@@ -41,6 +42,9 @@ import static org.mockito.Mockito.when;
|
||||
class MwaaServiceTest {
|
||||
|
||||
private MwaaService mwaaService;
|
||||
private AccountAwareStorageBackend<Environment> environmentStorage;
|
||||
private String currentAccount;
|
||||
private String currentRegion;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
@@ -48,16 +52,133 @@ class MwaaServiceTest {
|
||||
@Override
|
||||
public <V> AccountAwareStorageBackend<V> create(String serviceName, String fileName,
|
||||
TypeReference<Map<String, V>> typeReference) {
|
||||
return AccountAwareStorageBackend.inMemory("000000000000");
|
||||
environmentStorage = AccountAwareStorageBackend.<Environment>inMemory("000000000000");
|
||||
return (AccountAwareStorageBackend<V>) (AccountAwareStorageBackend<?>) environmentStorage;
|
||||
}
|
||||
};
|
||||
|
||||
EmulatorConfig config = testConfig();
|
||||
RegionResolver regionResolver = new RegionResolver("us-east-1", "000000000000");
|
||||
currentAccount = "000000000000";
|
||||
currentRegion = "us-east-1";
|
||||
RegionResolver regionResolver = Mockito.mock(RegionResolver.class);
|
||||
when(regionResolver.getAccountId()).thenAnswer(invocation -> currentAccount);
|
||||
when(regionResolver.getRegion()).thenAnswer(invocation -> currentRegion);
|
||||
S3Service s3Service = Mockito.mock(S3Service.class);
|
||||
mwaaService = new MwaaService(storageFactory, config, regionResolver, null, null, null, s3Service);
|
||||
}
|
||||
|
||||
@Test
|
||||
void sameNameEnvironmentsAreIsolatedByAccountAndRegion() {
|
||||
currentAccount = "111111111111";
|
||||
currentRegion = "us-east-1";
|
||||
Environment eastEnvironment = mwaaService.createEnvironment("shared-name",
|
||||
createRequest("arn:aws:s3:::east-bucket", "dags"));
|
||||
|
||||
currentAccount = "222222222222";
|
||||
currentRegion = "eu-west-1";
|
||||
Environment westEnvironment = mwaaService.createEnvironment("shared-name",
|
||||
createRequest("arn:aws:s3:::west-bucket", "dags"));
|
||||
|
||||
assertNotEquals(eastEnvironment.getArn(), westEnvironment.getArn());
|
||||
assertEquals(westEnvironment.getArn(), mwaaService.getEnvironment("shared-name").getArn());
|
||||
assertEquals(List.of("shared-name"), mwaaService.listEnvironments());
|
||||
|
||||
currentAccount = "111111111111";
|
||||
currentRegion = "us-east-1";
|
||||
assertEquals(eastEnvironment.getArn(), mwaaService.getEnvironment("shared-name").getArn());
|
||||
assertEquals(List.of("shared-name"), mwaaService.listEnvironments());
|
||||
}
|
||||
|
||||
@Test
|
||||
void deletingOneScopedEnvironmentDoesNotDeleteAnotherWithTheSameName() {
|
||||
currentAccount = "111111111111";
|
||||
currentRegion = "us-east-1";
|
||||
Environment eastEnvironment = mwaaService.createEnvironment("shared-name",
|
||||
createRequest("arn:aws:s3:::east-bucket", "dags"));
|
||||
|
||||
currentAccount = "222222222222";
|
||||
currentRegion = "eu-west-1";
|
||||
mwaaService.createEnvironment("shared-name", createRequest("arn:aws:s3:::west-bucket", "dags"));
|
||||
mwaaService.deleteEnvironment("shared-name");
|
||||
|
||||
currentAccount = "111111111111";
|
||||
currentRegion = "us-east-1";
|
||||
assertEquals(eastEnvironment.getArn(), mwaaService.getEnvironment("shared-name").getArn());
|
||||
}
|
||||
|
||||
@Test
|
||||
void cliTokensAreIsolatedForSameNameEnvironments() {
|
||||
currentAccount = "111111111111";
|
||||
currentRegion = "us-east-1";
|
||||
Environment eastEnvironment = mwaaService.createEnvironment("shared-name",
|
||||
createRequest("arn:aws:s3:::east-bucket", "dags"));
|
||||
String eastToken = (String) mwaaService.createCliToken("shared-name").get("CliToken");
|
||||
|
||||
currentAccount = "222222222222";
|
||||
currentRegion = "eu-west-1";
|
||||
Environment westEnvironment = mwaaService.createEnvironment("shared-name",
|
||||
createRequest("arn:aws:s3:::west-bucket", "dags"));
|
||||
String westToken = (String) mwaaService.createCliToken("shared-name").get("CliToken");
|
||||
|
||||
assertTrue(mwaaService.isValidCliToken(MwaaService.environmentIdentity(westEnvironment), westToken));
|
||||
assertFalse(mwaaService.isValidCliToken(MwaaService.environmentIdentity(westEnvironment), eastToken));
|
||||
assertTrue(mwaaService.isValidCliToken(MwaaService.environmentIdentity(eastEnvironment), eastToken));
|
||||
assertFalse(mwaaService.isValidCliToken(MwaaService.environmentIdentity(eastEnvironment), westToken));
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyEnvironmentIsMigratedOnlyWhenItsAccountAndRegionMatch() {
|
||||
Environment legacy = new Environment();
|
||||
legacy.setName("legacy-env");
|
||||
legacy.setArn("arn:aws:airflow:us-east-1:111111111111:environment/legacy-env");
|
||||
environmentStorage.putForAccount("111111111111", "legacy-env", legacy);
|
||||
|
||||
currentAccount = "111111111111";
|
||||
currentRegion = "us-east-1";
|
||||
assertEquals(legacy, mwaaService.getEnvironment("legacy-env"));
|
||||
|
||||
Environment wrongRegion = new Environment();
|
||||
wrongRegion.setName("foreign-env");
|
||||
wrongRegion.setArn("arn:aws:airflow:eu-west-1:222222222222:environment/foreign-env");
|
||||
environmentStorage.putForAccount("222222222222", "foreign-env", wrongRegion);
|
||||
|
||||
currentAccount = "222222222222";
|
||||
currentRegion = "us-east-1";
|
||||
assertThrows(AwsException.class, () -> mwaaService.getEnvironment("foreign-env"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void listMigratesMatchingLegacyEnvironmentAndRemovesTheOldKey() {
|
||||
Environment legacy = new Environment();
|
||||
legacy.setName("legacy-list-env");
|
||||
legacy.setArn("arn:aws:airflow:us-east-1:111111111111:environment/legacy-list-env");
|
||||
environmentStorage.putForAccount("111111111111", "legacy-list-env", legacy);
|
||||
|
||||
currentAccount = "111111111111";
|
||||
currentRegion = "us-east-1";
|
||||
|
||||
assertEquals(List.of("legacy-list-env"), mwaaService.listEnvironments());
|
||||
assertTrue(environmentStorage.getForAccount("111111111111", "legacy-list-env").isEmpty());
|
||||
assertTrue(environmentStorage.getForAccount("111111111111", "us-east-1/legacy-list-env").isPresent());
|
||||
}
|
||||
|
||||
@Test
|
||||
void promotionDoesNotLeaveASecondLegacyEnvironmentEntry() {
|
||||
Environment legacy = new Environment();
|
||||
legacy.setName("promoted-env");
|
||||
legacy.setArn("arn:aws:airflow:us-east-1:111111111111:environment/promoted-env");
|
||||
legacy.setStatus(EnvironmentStatus.CREATING);
|
||||
environmentStorage.putForAccount("111111111111", "promoted-env", legacy);
|
||||
|
||||
currentAccount = "111111111111";
|
||||
currentRegion = "us-east-1";
|
||||
legacy.setStatus(EnvironmentStatus.AVAILABLE);
|
||||
mwaaService.putEnvironment(legacy);
|
||||
|
||||
assertEquals(List.of("promoted-env"), mwaaService.listEnvironments());
|
||||
assertTrue(environmentStorage.getForAccount("111111111111", "promoted-env").isEmpty());
|
||||
}
|
||||
|
||||
private EmulatorConfig testConfig() {
|
||||
EmulatorConfig.MwaaServiceConfig mwaaConfig = proxy(EmulatorConfig.MwaaServiceConfig.class,
|
||||
(proxy, method, args) -> switch (method.getName()) {
|
||||
@@ -289,6 +410,22 @@ class MwaaServiceTest {
|
||||
assertEquals("data", tags.get("team"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void taggingRejectsAResourceArnOutsideTheRequestScope() {
|
||||
currentAccount = "111111111111";
|
||||
currentRegion = "us-east-1";
|
||||
Environment first = mwaaService.createEnvironment("shared-name",
|
||||
createRequest("arn:aws:s3:::first-bucket", "dags"));
|
||||
|
||||
currentAccount = "222222222222";
|
||||
currentRegion = "eu-west-1";
|
||||
mwaaService.createEnvironment("shared-name", createRequest("arn:aws:s3:::second-bucket", "dags"));
|
||||
|
||||
AwsException exception = assertThrows(AwsException.class,
|
||||
() -> mwaaService.tagResource(null, first.getArn(), Map.of("owner", "first")));
|
||||
assertEquals("ResourceNotFoundException", exception.getErrorCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void tagHandlerServiceKeyIsAirflow() {
|
||||
assertEquals("airflow", mwaaService.serviceKey());
|
||||
@@ -311,8 +448,10 @@ class MwaaServiceTest {
|
||||
Map<String, Object> response = mwaaService.createCliToken("cli-token-env");
|
||||
String token = (String) response.get("CliToken");
|
||||
assertNotNull(token);
|
||||
assertTrue(mwaaService.isValidCliToken("cli-token-env", token));
|
||||
assertFalse(mwaaService.isValidCliToken("cli-token-env", "not-a-real-token"));
|
||||
assertTrue(mwaaService.isValidCliToken(MwaaService.environmentIdentity(
|
||||
mwaaService.getEnvironment("cli-token-env")), token));
|
||||
assertFalse(mwaaService.isValidCliToken(MwaaService.environmentIdentity(
|
||||
mwaaService.getEnvironment("cli-token-env")), "not-a-real-token"));
|
||||
assertFalse(mwaaService.isValidCliToken("other-env", token));
|
||||
}
|
||||
|
||||
@@ -399,7 +538,7 @@ class MwaaServiceTest {
|
||||
assertEquals(EnvironmentStatus.CREATE_FAILED, environment.getStatus());
|
||||
verify(portAllocator).release(8701);
|
||||
verify(environmentManager).stopEnvironment(environment);
|
||||
verify(proxyManager).stopProxy("failed-proxy-env");
|
||||
verify(proxyManager).stopProxy(MwaaService.environmentIdentity(environment));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user