fix(mwaa): reject a malformed configured AirflowVersion as ValidationException (#3453)

MwaaEnvironmentManager.pythonTagFor parses AirflowVersion with a bare
Integer.parseInt. resolveAirflowVersion only checked that the requested
version was present in the operator-configured supported-versions list,
so a misconfigured entry (e.g. a stray "latest") would pass that check
and reach pythonTagFor, surfacing as an internal NumberFormatException
out of container startup, well after Postgres had already been created
for the environment, rather than a clean 400 at CreateEnvironment.

resolveAirflowVersion now also validates the version's shape (a numeric
major.minor, with an optional patch) before checking list membership, so
a malformed value is rejected immediately as ValidationException.

Follow-up from hectorvent's review on #3445.
This commit is contained in:
Priamo Germosen authored and GitHub committed 2026-09-11 23:20:41 -05:00
1 parent 6d43e16d0e
commit a7902b97f3
2 files changed
+46 -2

No files matched your search

@@ -36,6 +36,7 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ApplicationScoped
@@ -43,6 +44,16 @@ public class MwaaService implements TagHandler {
private static final Logger LOG = Logger.getLogger(MwaaService.class);
/**
* Shape {@code MwaaEnvironmentManager.pythonTagFor} requires: at least a numeric
* {@code major.minor}, an optional {@code .patch}. {@code supported-versions} is
* operator-configurable, so a malformed entry (e.g. a stray {@code latest}) must be rejected
* here as a clean {@code ValidationException} rather than reaching {@code pythonTagFor} and
* surfacing as an internal {@code NumberFormatException} well after Postgres has already been
* created for the environment.
*/
private static final Pattern AIRFLOW_VERSION_PATTERN = Pattern.compile("\\d+\\.\\d+(\\.\\d+)?");
private final StorageBackend<String, Environment> storage;
private final EmulatorConfig config;
private final RegionResolver regionResolver;
@@ -339,7 +350,7 @@ public class MwaaService implements TagHandler {
private String resolveAirflowVersion(String requested) {
List<String> supported = config.services().mwaa().supportedVersions();
String version = requested != null && !requested.isBlank() ? requested : config.services().mwaa().defaultVersion();
if (!supported.contains(version)) {
if (!AIRFLOW_VERSION_PATTERN.matcher(version).matches() || !supported.contains(version)) {
throw new AwsException("ValidationException",
"Unsupported AirflowVersion '" + version + "'. Supported versions: " + supported, 400);
}
@@ -180,10 +180,14 @@ class MwaaServiceTest {
}
private EmulatorConfig testConfig() {
return testConfig(List.of("2.10.5", "2.9.3", "2.8.4"));
}
private EmulatorConfig testConfig(List<String> supportedVersions) {
EmulatorConfig.MwaaServiceConfig mwaaConfig = proxy(EmulatorConfig.MwaaServiceConfig.class,
(proxy, method, args) -> switch (method.getName()) {
case "enabled", "mock" -> true;
case "supportedVersions" -> List.of("2.10.5", "2.9.3", "2.8.4");
case "supportedVersions" -> supportedVersions;
case "defaultVersion" -> "2.10.5";
case "proxyBasePort" -> 8700;
case "proxyMaxPort" -> 8799;
@@ -294,6 +298,35 @@ class MwaaServiceTest {
assertEquals("ValidationException", ex.getErrorCode());
}
@Test
void createEnvironmentRejectsAMalformedConfiguredVersionInsteadOfCrashing() {
// supported-versions is operator-configurable, so a stray non-numeric entry like "latest"
// must be rejected as a clean ValidationException here, not reach
// MwaaEnvironmentManager.pythonTagFor and surface as an internal NumberFormatException
// well after Postgres has already been created for the environment.
StorageFactory storageFactory = new StorageFactory(null, null) {
@Override
public synchronized <V> AccountAwareStorageBackend<V> create(String serviceName, String fileName,
TypeReference<Map<String, V>> typeReference) {
return AccountAwareStorageBackend.inMemory("000000000000");
}
};
RegionResolver regionResolver = Mockito.mock(RegionResolver.class);
when(regionResolver.getAccountId()).thenReturn("000000000000");
when(regionResolver.getRegion()).thenReturn("us-east-1");
S3Service s3Service = Mockito.mock(S3Service.class);
MwaaService misconfiguredService = new MwaaService(storageFactory, testConfig(List.of("2.10.5", "latest")),
regionResolver, null, null, null, s3Service);
CreateEnvironmentRequest request = createRequest("arn:aws:s3:::my-bucket", "dags");
request.setAirflowVersion("latest");
AwsException ex = assertThrows(AwsException.class,
() -> misconfiguredService.createEnvironment("latest-version-env", request));
assertEquals(400, ex.getHttpStatus());
assertEquals("ValidationException", ex.getErrorCode());
}
@Test
void createEnvironmentWithSupportedVersionRoundTrips() {
CreateEnvironmentRequest request = createRequest("arn:aws:s3:::my-bucket", "dags");