mirror of
https://github.com/floci-io/floci.git
synced 2026-11-24 06:20:24 -05:00
fix(mwaa): apply AirflowConfigurationOptions to the Airflow container (#3457)
CreateEnvironment stored and GetEnvironment echoed back
AirflowConfigurationOptions, but nothing translated it into the
container's actual configuration, so a DAG reading a custom
conf.get("section", "key") value at import time or task-run time got
nothing back, even though GetEnvironment reported the option as
configured.
Added MwaaEnvironmentManager.airflowConfigurationOptionsEnv, which
converts each AWS "section.key" entry into the AIRFLOW__SECTION__KEY
environment variable real Amazon MWAA sets for the same option, and
wired it into startAirflowContainer's env list. A malformed key (no
"section.key" dot on both sides) or one that would collide with a
variable Floci itself manages (PROTECTED_ENV_VARS: the executor,
database connection, security keys) is skipped and logged rather than
silently breaking the environment's required wiring.
Since this map now genuinely does something instead of being inert,
also added the validation CreateEnvironment's botocore model declares
for it (a 1-64 character key matching [a-z]+([a-z0-9._]*[a-z0-9_]+)?,
a 1-65536 character printable-ASCII value) as a server-side
ValidationException: the AWS SDKs enforce this client-side, but Floci
is also reachable via raw HTTP/CLI --cli-input-json, which bypasses
that, and an oversized or malformed entry should be rejected before
Docker container creation rather than passed through.
Fixes #3436
This commit is contained in:
1 parent
828b53c5af
commit
d429d107ff
5 files changed
+191
No files matched your search
@@ -32,6 +32,7 @@ import java.security.SecureRandom;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
@@ -159,6 +160,7 @@ public class MwaaEnvironmentManager {
|
||||
"AIRFLOW__CORE__LOAD_EXAMPLES=false",
|
||||
"_AIRFLOW_WWW_USER_USERNAME=" + adminUser,
|
||||
"_AIRFLOW_WWW_USER_PASSWORD=" + adminPassword));
|
||||
env.addAll(airflowConfigurationOptionsEnv(environment.getAirflowConfigurationOptions()));
|
||||
|
||||
ContainerBuilder.Builder specBuilder = containerBuilder.newContainer(image)
|
||||
.withName(airflowContainerName)
|
||||
@@ -417,6 +419,41 @@ public class MwaaEnvironmentManager {
|
||||
return (major > 2 || (major == 2 && minor >= 11)) ? "python3.12" : "python3.11";
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates {@code AirflowConfigurationOptions} ({@code "section.key"} to value, e.g.
|
||||
* {@code core.dags_are_paused_at_creation}) into the {@code AIRFLOW__SECTION__KEY} environment
|
||||
* variables real Amazon MWAA sets for the same option, matching Airflow's own {@code conf.get}
|
||||
* section/key model. An entry without a non-empty section and key on both sides of the dot is
|
||||
* malformed per AWS's own format and is skipped rather than guessed at. An entry that would
|
||||
* collide with one of {@link #PROTECTED_ENV_VARS} is skipped too and logged: those are Floci's
|
||||
* own required LocalExecutor/DB/security wiring, not something a configuration option request
|
||||
* gets to override.
|
||||
*/
|
||||
static List<String> airflowConfigurationOptionsEnv(Map<String, String> options) {
|
||||
if (options == null || options.isEmpty()) {
|
||||
return List.of();
|
||||
}
|
||||
List<String> env = new ArrayList<>();
|
||||
for (Map.Entry<String, String> entry : options.entrySet()) {
|
||||
String dotted = entry.getKey();
|
||||
int dot = dotted.indexOf('.');
|
||||
if (dot <= 0 || dot == dotted.length() - 1) {
|
||||
LOG.warnv("Ignoring malformed AirflowConfigurationOptions key ''{0}'', expected "
|
||||
+ "the AWS \"section.key\" format", dotted);
|
||||
continue;
|
||||
}
|
||||
String key = "AIRFLOW__" + dotted.substring(0, dot).toUpperCase(Locale.ROOT)
|
||||
+ "__" + dotted.substring(dot + 1).toUpperCase(Locale.ROOT);
|
||||
if (PROTECTED_ENV_VARS.contains(key)) {
|
||||
LOG.warnv("Ignoring AirflowConfigurationOptions entry ''{0}'': it maps to {1}, "
|
||||
+ "which Floci itself manages for this environment", dotted, key);
|
||||
continue;
|
||||
}
|
||||
env.add(key + "=" + entry.getValue());
|
||||
}
|
||||
return env;
|
||||
}
|
||||
|
||||
/**
|
||||
* Environment variables a startup script must not be able to permanently override — mirrors
|
||||
* real MWAA's documented "reserved environment variables" (which AWS silently restores after a
|
||||
|
||||
@@ -124,6 +124,7 @@ public class MwaaService implements TagHandler {
|
||||
}
|
||||
|
||||
String version = resolveAirflowVersion(request.getAirflowVersion());
|
||||
validateAirflowConfigurationOptions(request.getAirflowConfigurationOptions());
|
||||
|
||||
String arn = AwsArnUtils.Arn.of("airflow", region, accountId, "environment/" + name).toString();
|
||||
|
||||
@@ -357,6 +358,47 @@ public class MwaaService implements TagHandler {
|
||||
return version;
|
||||
}
|
||||
|
||||
// CreateEnvironment's botocore model caps AirflowConfigurationOptions to these key/value
|
||||
// shapes, matched below.
|
||||
private static final Pattern AIRFLOW_CONFIGURATION_OPTION_KEY_PATTERN =
|
||||
Pattern.compile("[a-z]+([a-z0-9._]*[a-z0-9_]+)?");
|
||||
private static final Pattern AIRFLOW_CONFIGURATION_OPTION_VALUE_PATTERN = Pattern.compile("[ -~]+");
|
||||
private static final int MAX_AIRFLOW_CONFIGURATION_OPTION_KEY_LENGTH = 64;
|
||||
private static final int MAX_AIRFLOW_CONFIGURATION_OPTION_VALUE_LENGTH = 65536;
|
||||
|
||||
/**
|
||||
* The AWS SDKs enforce {@code AirflowConfigurationOptions}' key/value shape client-side, but
|
||||
* Floci is also reachable via raw HTTP or CLI {@code --cli-input-json}, which bypasses that,
|
||||
* and this map now genuinely flows into the Airflow container's environment
|
||||
* ({@link MwaaEnvironmentManager#airflowConfigurationOptionsEnv}) rather than being inert, so
|
||||
* an oversized or malformed entry is worth rejecting here rather than passing through to
|
||||
* Docker container creation.
|
||||
*/
|
||||
private void validateAirflowConfigurationOptions(Map<String, String> options) {
|
||||
if (options == null) {
|
||||
return;
|
||||
}
|
||||
for (Map.Entry<String, String> entry : options.entrySet()) {
|
||||
String key = entry.getKey();
|
||||
if (key == null || key.length() > MAX_AIRFLOW_CONFIGURATION_OPTION_KEY_LENGTH
|
||||
|| !AIRFLOW_CONFIGURATION_OPTION_KEY_PATTERN.matcher(key).matches()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid AirflowConfigurationOptions key '" + key + "': must be 1-"
|
||||
+ MAX_AIRFLOW_CONFIGURATION_OPTION_KEY_LENGTH
|
||||
+ " characters matching " + AIRFLOW_CONFIGURATION_OPTION_KEY_PATTERN.pattern(),
|
||||
400);
|
||||
}
|
||||
String value = entry.getValue();
|
||||
if (value == null || value.length() > MAX_AIRFLOW_CONFIGURATION_OPTION_VALUE_LENGTH
|
||||
|| !AIRFLOW_CONFIGURATION_OPTION_VALUE_PATTERN.matcher(value).matches()) {
|
||||
throw new AwsException("ValidationException",
|
||||
"Invalid AirflowConfigurationOptions value for key '" + key + "': must be 1-"
|
||||
+ MAX_AIRFLOW_CONFIGURATION_OPTION_VALUE_LENGTH + " printable ASCII characters",
|
||||
400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String buildWebserverUrl(int proxyPort) {
|
||||
String scheme = config.tls().enabled() ? "https" : "http";
|
||||
String host = config.hostname().orElse("localhost");
|
||||
|
||||
Reference in new issue
Block a user