diff --git a/src/main/java/io/github/hectorvent/floci/services/mwaa/MwaaService.java b/src/main/java/io/github/hectorvent/floci/services/mwaa/MwaaService.java index 7c593ba4f6..e74a107c95 100644 --- a/src/main/java/io/github/hectorvent/floci/services/mwaa/MwaaService.java +++ b/src/main/java/io/github/hectorvent/floci/services/mwaa/MwaaService.java @@ -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 storage; private final EmulatorConfig config; private final RegionResolver regionResolver; @@ -339,7 +350,7 @@ public class MwaaService implements TagHandler { private String resolveAirflowVersion(String requested) { List 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); } diff --git a/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaServiceTest.java index 932a785d69..6a9aa2dac9 100644 --- a/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/mwaa/MwaaServiceTest.java @@ -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 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 AccountAwareStorageBackend create(String serviceName, String fileName, + TypeReference> 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");