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:
Niels Kootstra authored and GitHub committed 2026-09-11 10:25:21 -04:00
1 parent 5bf8075d52
commit c6e64a0977
5 files changed
+363 -67

No files matched your search

@@ -1,7 +1,7 @@
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.AwsArnUtils;
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;
@@ -66,21 +66,18 @@ public class MwaaEnvironmentManager {
private final ContainerDetector containerDetector;
private final EmulatorConfig config;
private final LaunchedContainerAwsEnv awsEnv;
private final RegionResolver regionResolver;
@Inject
public MwaaEnvironmentManager(ContainerBuilder containerBuilder,
ContainerLifecycleManager lifecycleManager,
ContainerDetector containerDetector,
EmulatorConfig config,
LaunchedContainerAwsEnv awsEnv,
RegionResolver regionResolver) {
LaunchedContainerAwsEnv awsEnv) {
this.containerBuilder = containerBuilder;
this.lifecycleManager = lifecycleManager;
this.containerDetector = containerDetector;
this.config = config;
this.awsEnv = awsEnv;
this.regionResolver = regionResolver;
}
/**
@@ -98,7 +95,7 @@ public class MwaaEnvironmentManager {
String dbPassword = generateSecret(24);
environment.setDbPassword(dbPassword);
String dbContainerName = dbContainerName(config, name);
String dbContainerName = dbContainerName(config, environment);
String dbVolume = dbContainerName;
lifecycleManager.removeIfExists(dbContainerName);
lifecycleManager.ensureVolume(dbVolume);
@@ -114,7 +111,7 @@ public class MwaaEnvironmentManager {
.withExposedPort(POSTGRES_PORT)
.withLogRotation()
.withLabels(ContainerStorageHelper.resourceIdentityLabels(
"mwaa", name, regionResolver.getAccountId(), regionResolver.getDefaultRegion()))
"mwaa", name, environmentAccount(environment), environmentRegion(environment)))
.build();
String dbContainerId = lifecycleManager.create(dbSpec);
@@ -135,7 +132,7 @@ public class MwaaEnvironmentManager {
void startAirflowContainer(Environment environment, String airflowVersion, String dbIp, String dbPassword,
byte[] startupScriptContent) {
String name = environment.getName();
String airflowContainerName = airflowContainerName(config, name);
String airflowContainerName = airflowContainerName(config, environment);
String dagsVolume = airflowContainerName + "-dags";
String logsVolume = airflowContainerName + "-logs";
@@ -151,7 +148,7 @@ public class MwaaEnvironmentManager {
// Points DAG code's own AWS SDK calls (boto3, botocore) at Floci itself, the same way
// Lambda/ECS containers already do via LaunchedContainerAwsEnv — otherwise a real DAG's
// boto3.client("s3") etc. would target real AWS instead of this emulator.
List<String> env = new ArrayList<>(awsEnv.sdkBaselineEnv(config.defaultRegion(), Optional.empty()));
List<String> env = new ArrayList<>(awsEnv.sdkBaselineEnv(environmentRegion(environment), Optional.empty()));
env.addAll(List.of(
"AIRFLOW__CORE__EXECUTOR=LocalExecutor",
"AIRFLOW__DATABASE__SQL_ALCHEMY_CONN=" + sqlAlchemyConn,
@@ -177,7 +174,7 @@ public class MwaaEnvironmentManager {
.withDockerNetwork(config.services().mwaa().dockerNetwork())
.withLogRotation()
.withLabels(ContainerStorageHelper.resourceIdentityLabels(
"mwaa", name, regionResolver.getAccountId(), regionResolver.getDefaultRegion()));
"mwaa", name, environmentAccount(environment), environmentRegion(environment)));
if (!containerDetector.isRunningInContainer()) {
specBuilder.withDynamicPort(AIRFLOW_WEBSERVER_PORT);
@@ -293,8 +290,8 @@ public class MwaaEnvironmentManager {
if (environment.getDbContainerId() != null) {
lifecycleManager.stopAndRemove(environment.getDbContainerId(), null);
}
String airflowContainerName = airflowContainerName(config, name);
lifecycleManager.removeVolume(dbContainerName(config, name));
String airflowContainerName = airflowContainerName(config, environment);
lifecycleManager.removeVolume(dbContainerName(config, environment));
lifecycleManager.removeVolume(airflowContainerName + "-dags");
lifecycleManager.removeVolume(airflowContainerName + "-logs");
LOG.infov("Stopped MWAA containers for environment {0}", name);
@@ -345,12 +342,27 @@ public class MwaaEnvironmentManager {
* daemon (via {@code FLOCI_DOCKER_RESOURCE_NAMESPACE}) don't collide, same as every other
* Docker-backed service (EKS, RDS, ...). {@code config} may be {@code null} — the helper treats
* that as "no namespace configured" and returns the base name unchanged. */
static String dbContainerName(EmulatorConfig config, String environmentName) {
return ContainerStorageHelper.dockerName(config, "floci-mwaa-" + environmentName + "-db");
static String dbContainerName(EmulatorConfig config, Environment environment) {
return ContainerStorageHelper.dockerName(config, "floci-mwaa-" + environmentIdentity(environment) + "-db");
}
static String airflowContainerName(EmulatorConfig config, String environmentName) {
return ContainerStorageHelper.dockerName(config, "floci-mwaa-" + environmentName + "-airflow");
static String airflowContainerName(EmulatorConfig config, Environment environment) {
return ContainerStorageHelper.dockerName(config,
"floci-mwaa-" + environmentIdentity(environment) + "-airflow");
}
private static String environmentIdentity(Environment environment) {
return environmentAccount(environment) + "." + environmentRegion(environment) + "." + environment.getName();
}
static String environmentAccount(Environment environment) {
return environment.getAccountId() != null
? environment.getAccountId()
: AwsArnUtils.accountOrDefault(environment.getArn(), "000000000000");
}
static String environmentRegion(Environment environment) {
return AwsArnUtils.regionOrDefault(environment.getArn(), "us-east-1");
}
/**
@@ -30,6 +30,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
@@ -91,7 +92,7 @@ public class MwaaService implements TagHandler {
dagSyncPoller.shutdownNow();
if (!config.services().mwaa().mock() && !config.services().mwaa().keepRunningOnShutdown()) {
for (Environment environment : allEnvironments()) {
proxyManager.stopProxy(environment.getName());
proxyManager.stopProxy(environmentIdentity(environment));
environmentManager.stopEnvironment(environment);
}
}
@@ -101,7 +102,9 @@ public class MwaaService implements TagHandler {
if (name == null || name.isBlank()) {
throw new AwsException("ValidationException", "Environment name is required", 400);
}
if (storage.get(name).isPresent()) {
String accountId = regionResolver.getAccountId();
String region = regionResolver.getRegion();
if (getStoredEnvironment(accountId, region, name).isPresent()) {
// CreateEnvironment's botocore model declares only ServiceUnavailableException,
// ValidationException, and InternalServerException — no "already exists" shape — so an
// SDK client can't map a ResourceAlreadyExistsException here.
@@ -111,8 +114,6 @@ public class MwaaService implements TagHandler {
String version = resolveAirflowVersion(request.getAirflowVersion());
String region = config.defaultRegion();
String accountId = regionResolver.getAccountId();
String arn = AwsArnUtils.Arn.of("airflow", region, accountId, "environment/" + name).toString();
Environment environment = new Environment();
@@ -158,7 +159,7 @@ public class MwaaService implements TagHandler {
environment.setProxyPort(proxyPort);
environment.setWebserverUrl(buildWebserverUrl(proxyPort));
String airflowContainerId = environment.getAirflowContainerId();
proxyManager.startProxy(name, proxyPort,
proxyManager.startProxy(environmentIdentity(environment), proxyPort,
environment.getAirflowInternalHost(), environment.getAirflowInternalPort(),
this::isValidCliToken,
cliCommand -> environmentManager.runAirflowCli(airflowContainerId, cliCommand));
@@ -173,7 +174,7 @@ public class MwaaService implements TagHandler {
}
}
storage.put(name, environment);
putEnvironment(environment);
return environment;
}
@@ -187,7 +188,7 @@ public class MwaaService implements TagHandler {
*/
private void rollbackFailedCreate(Environment environment, int proxyPort) {
try {
proxyManager.stopProxy(environment.getName());
proxyManager.stopProxy(environmentIdentity(environment));
} catch (Exception e) {
LOG.warnv("Error stopping proxy while rolling back failed MWAA environment {0}: {1}",
environment.getName(), e.getMessage());
@@ -205,13 +206,22 @@ public class MwaaService implements TagHandler {
}
public Environment getEnvironment(String name) {
return storage.get(name)
return getStoredEnvironment(regionResolver.getAccountId(), regionResolver.getRegion(), name)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"No environment found for name: " + name, 404));
}
public List<String> listEnvironments() {
return storage.scan(k -> true).stream()
String accountId = regionResolver.getAccountId();
String region = regionResolver.getRegion();
if (storage instanceof AccountAwareStorageBackend<Environment> aware) {
migrateLegacyEnvironments(aware, accountId, region);
}
List<Environment> environments = storage instanceof AccountAwareStorageBackend<Environment> aware
? aware.scanForAccount(accountId, k -> true)
: storage.scan(k -> true);
return environments.stream()
.filter(environment -> region.equals(environmentRegion(environment)))
.map(Environment::getName)
.collect(Collectors.toList());
}
@@ -287,17 +297,18 @@ public class MwaaService implements TagHandler {
Environment environment = getEnvironment(name);
environment.setStatus(EnvironmentStatus.DELETING);
if (!config.services().mwaa().mock()) {
proxyManager.stopProxy(name);
proxyManager.stopProxy(environmentIdentity(environment));
environmentManager.stopEnvironment(environment);
// Mirrors how Lambda/MSK/EC2/ECR release their allocated ports on cleanup — otherwise
// repeated create/delete cycles exhaust the configured proxy port range even though no
// MWAA proxy is actually running anymore.
portAllocator.release(environment.getProxyPort());
}
cliTokensByEnvironment.remove(name);
dagSyncState.remove(name);
requirementsEtagByEnvironment.remove(name);
storage.delete(name);
String environmentIdentity = environmentIdentity(environment);
cliTokensByEnvironment.remove(environmentIdentity);
dagSyncState.remove(environmentIdentity);
requirementsEtagByEnvironment.remove(environmentIdentity);
deleteEnvironment(environment);
return environment;
}
@@ -314,14 +325,14 @@ public class MwaaService implements TagHandler {
public Map<String, Object> createCliToken(String name) {
Environment environment = getEnvironment(name);
String token = generateToken();
cliTokensByEnvironment.computeIfAbsent(name, k -> ConcurrentHashMap.newKeySet()).add(token);
cliTokensByEnvironment.computeIfAbsent(environmentIdentity(environment), k -> ConcurrentHashMap.newKeySet()).add(token);
return Map.of(
"CliToken", token,
"WebServerHostname", hostnameFromUrl(environment.getWebserverUrl()));
}
boolean isValidCliToken(String environmentName, String token) {
Set<String> tokens = cliTokensByEnvironment.get(environmentName);
boolean isValidCliToken(String environmentIdentity, String token) {
Set<String> tokens = cliTokensByEnvironment.get(environmentIdentity);
return tokens != null && tokens.contains(token);
}
@@ -392,11 +403,29 @@ public class MwaaService implements TagHandler {
}
private Environment findByArn(String resourceArn) {
int idx = resourceArn.lastIndexOf('/');
if (idx < 0 || idx == resourceArn.length() - 1) {
if (resourceArn == null) {
throw new AwsException("ValidationException", "Invalid resource ARN: " + resourceArn, 400);
}
try {
AwsArnUtils.Arn arn = AwsArnUtils.parse(resourceArn);
String name = arn.resource().startsWith("environment/")
? arn.resource().substring("environment/".length())
: "";
if (name.isBlank() || arn.region().isBlank() || arn.accountId().isBlank()
|| !"airflow".equals(arn.service())) {
throw new IllegalArgumentException("not an MWAA environment ARN");
}
if (!arn.accountId().equals(regionResolver.getAccountId())
|| !arn.region().equals(regionResolver.getRegion())) {
throw new AwsException("ResourceNotFoundException",
"No environment found for name: " + name, 404);
}
return getStoredEnvironment(arn.accountId(), arn.region(), name)
.orElseThrow(() -> new AwsException("ResourceNotFoundException",
"No environment found for name: " + name, 404));
} catch (IllegalArgumentException e) {
throw new AwsException("ValidationException", "Invalid resource ARN: " + resourceArn, 400);
}
return getEnvironment(resourceArn.substring(idx + 1));
}
private void startReadinessPoller() {
@@ -457,7 +486,8 @@ public class MwaaService implements TagHandler {
currentByKey.put(relative, obj.getETag());
}
Map<String, String> previous = dagSyncState.computeIfAbsent(environment.getName(), k -> new HashMap<>());
String environmentIdentity = environmentIdentity(environment);
Map<String, String> previous = dagSyncState.computeIfAbsent(environmentIdentity, k -> new HashMap<>());
for (Map.Entry<String, String> entry : currentByKey.entrySet()) {
String relative = entry.getKey();
String etag = entry.getValue();
@@ -492,10 +522,11 @@ public class MwaaService implements TagHandler {
}
try {
S3Object requirements = s3Service.getObject(bucket, environment.getRequirementsS3Path());
String lastEtag = requirementsEtagByEnvironment.get(environment.getName());
String environmentIdentity = environmentIdentity(environment);
String lastEtag = requirementsEtagByEnvironment.get(environmentIdentity);
if (!requirements.getETag().equals(lastEtag)) {
environmentManager.installRequirements(environment, requirements.getData());
requirementsEtagByEnvironment.put(environment.getName(), requirements.getETag());
requirementsEtagByEnvironment.put(environmentIdentity, requirements.getETag());
}
} catch (Exception e) {
LOG.warnv("Could not sync requirements.txt for environment {0}: {1}",
@@ -532,11 +563,87 @@ public class MwaaService implements TagHandler {
return storage.scan(k -> true);
}
private void putEnvironment(Environment environment) {
if (environment.getAccountId() != null && storage instanceof AccountAwareStorageBackend<Environment> aware) {
aware.putForAccount(environment.getAccountId(), environment.getName(), environment);
void putEnvironment(Environment environment) {
String key = environmentKey(environmentRegion(environment), environment.getName());
String accountId = environmentAccount(environment);
if (accountId != null && storage instanceof AccountAwareStorageBackend<Environment> aware) {
aware.getForAccountMigratingLegacyKeys(accountId, key, List.of(environment.getName()),
candidate -> accountId.equals(environmentAccount(candidate))
&& environmentRegion(environment).equals(environmentRegion(candidate)),
isDefaultScope(accountId, environmentRegion(environment)));
aware.putForAccount(accountId, key, environment);
} else {
storage.put(environment.getName(), environment);
storage.put(key, environment);
}
}
private void deleteEnvironment(Environment environment) {
String key = environmentKey(environmentRegion(environment), environment.getName());
String accountId = environmentAccount(environment);
if (accountId != null && storage instanceof AccountAwareStorageBackend<Environment> aware) {
aware.deleteForAccount(accountId, key);
} else {
storage.delete(key);
}
}
private Optional<Environment> getStoredEnvironment(String accountId, String region, String name) {
String key = environmentKey(region, name);
if (storage instanceof AccountAwareStorageBackend<Environment> aware) {
return aware.getForAccountMigratingLegacyKeys(accountId, key, List.of(name),
environment -> accountId.equals(environmentAccount(environment))
&& region.equals(environmentRegion(environment)),
isDefaultScope(accountId, region));
}
return storage.get(key);
}
private void migrateLegacyEnvironments(AccountAwareStorageBackend<Environment> aware,
String accountId, String region) {
for (Environment legacy : aware.scanUnscopedLegacy(environment ->
accountId.equals(environmentAccount(environment))
&& region.equals(environmentRegion(environment)))) {
migrateLegacyEnvironment(aware, accountId, region, legacy);
}
for (String legacyKey : aware.keysForAccount(accountId)) {
if (legacyKey.contains("/")) {
continue;
}
aware.getForAccount(accountId, legacyKey)
.filter(environment -> accountId.equals(environmentAccount(environment))
&& region.equals(environmentRegion(environment)))
.ifPresent(environment -> migrateLegacyEnvironment(aware, accountId, region, environment));
}
}
private void migrateLegacyEnvironment(AccountAwareStorageBackend<Environment> aware,
String accountId, String region, Environment environment) {
String key = environmentKey(region, environment.getName());
aware.getForAccountMigratingLegacyKeys(accountId, key, List.of(environment.getName()),
candidate -> accountId.equals(environmentAccount(candidate))
&& region.equals(environmentRegion(candidate)),
isDefaultScope(accountId, region)).ifPresent(value -> aware.putForAccount(accountId, key, value));
}
private boolean isDefaultScope(String accountId, String region) {
return accountId.equals(regionResolver.getDefaultAccountId())
&& region.equals(regionResolver.getDefaultRegion());
}
private static String environmentKey(String region, String name) {
return region + "/" + name;
}
static String environmentIdentity(Environment environment) {
return environmentAccount(environment) + "/" + environmentRegion(environment) + "/" + environment.getName();
}
private static String environmentAccount(Environment environment) {
return MwaaEnvironmentManager.environmentAccount(environment);
}
private static String environmentRegion(Environment environment) {
return MwaaEnvironmentManager.environmentRegion(environment);
}
}