fix(mwaa): restrict CLI proxy to supported commands (#4797)

* fix(mwaa): restrict CLI proxy to supported commands

* test(mwaa): cover every supported CLI command
This commit is contained in:
Aniket Shukla authored and GitHub committed 2026-10-01 09:11:06 -04:00
1 parent 6a98f69a5d
commit 8a2b998378
3 files changed
+64 -4

No files matched your search

+1 -1
View File
@@ -42,7 +42,7 @@ Once Airflow's unauthenticated `/health` endpoint reports both `metadatabase` an
### Web/CLI proxy
Floci runs its own HTTP proxy per environment (published on a host port from the configured range) that fronts the real Airflow webserver: every request is forwarded through to Airflow as-is, **except** `POST /aws_mwaa/cli`, which the proxy intercepts itself — it validates the `Authorization: Bearer <CliToken>` from `CreateCliToken`, then runs the requested `airflow` CLI command inside the container via `docker exec` and returns the AWS-documented `{stdout, stderr}` (base64) shape. `Environment.WebserverUrl` and the URL returned by `CreateWebLoginToken` both point at this proxy, so Airflow-UI browsing and the CLI-token flow go through the same place.
Floci runs its own HTTP proxy per environment (published on a host port from the configured range) that fronts the real Airflow webserver: every request is forwarded through to Airflow as-is, **except** `POST /aws_mwaa/cli`, which the proxy intercepts itself. It validates the `Authorization: Bearer <CliToken>` from `CreateCliToken`, then runs supported Amazon MWAA Airflow CLI commands inside the container via `docker exec` and returns the AWS-documented `{stdout, stderr}` (base64) shape. Unsupported commands return an error in `stderr` without reaching the container. The supported commands match the [Amazon MWAA Airflow v2 CLI list](https://docs.aws.amazon.com/mwaa/latest/userguide/airflow-cli-command-reference.html); every Airflow version Floci currently offers is at least 2.8.4. `Environment.WebserverUrl` and the URL returned by `CreateWebLoginToken` both point at this proxy, so Airflow-UI browsing and the CLI-token flow go through the same place.
### Startup scripts (`StartupScriptS3Path`)
@@ -33,6 +33,7 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@@ -60,6 +61,19 @@ public class MwaaEnvironmentManager {
/** The OS user the stock apache/airflow image runs its process as (uid 50000) — distinct from
* the Airflow web-UI admin login username configured via {@code _AIRFLOW_WWW_USER_USERNAME}. */
private static final String CONTAINER_OS_USER = "airflow";
private static final Set<String> MWAA_CLI_COMMANDS = Set.of("cheat-sheet", "version");
private static final Map<String, Set<String>> MWAA_CLI_SUBCOMMANDS = Map.of(
"connections", Set.of("add", "delete"),
"dags", Set.of("backfill", "delete", "list", "list-import-errors", "list-jobs",
"list-runs", "next-execution", "pause", "report", "reserialize", "show",
"state", "test", "trigger", "unpause"),
"db", Set.of("clean"),
"providers", Set.of("behaviours", "get", "hooks", "links", "list", "notifications",
"secrets", "triggerer", "widgets"),
"roles", Set.of("add-perms", "create", "del-perms", "list"),
"tasks", Set.of("clear", "failed-deps", "list", "render", "run", "state",
"states-for-dag-run", "test"),
"variables", Set.of("delete", "get", "list", "set"));
private final ContainerBuilder containerBuilder;
private final ContainerLifecycleManager lifecycleManager;
@@ -373,12 +387,27 @@ public class MwaaEnvironmentManager {
* {@code ;}, {@code |} or {@code $(...)} reaches Airflow as plain arguments.
*/
public ContainerExec.Result runAirflowCli(String airflowContainerId, String cliCommand) throws Exception {
List<String> arguments = splitCliArguments(cliCommand);
if (!isSupportedCliCommand(arguments)) {
return new ContainerExec.Result(1, "", "Command is not supported by Amazon MWAA\n", false);
}
List<String> argv = new ArrayList<>();
argv.add("airflow");
argv.addAll(splitCliArguments(cliCommand));
argv.addAll(arguments);
return execInContainer(airflowContainerId, argv.toArray(new String[0]));
}
static boolean isSupportedCliCommand(List<String> arguments) {
if (arguments.isEmpty()) {
return false;
}
if (MWAA_CLI_COMMANDS.contains(arguments.getFirst())) {
return true;
}
Set<String> subcommands = MWAA_CLI_SUBCOMMANDS.get(arguments.getFirst());
return arguments.size() >= 2 && subcommands != null && subcommands.contains(arguments.get(1));
}
/**
* Splits a CLI command line into arguments using POSIX shell quoting without any expansion:
* spaces, tabs and newlines separate arguments, single quotes are literal, double quotes honour
@@ -558,12 +558,43 @@ class MwaaEnvironmentManagerTest {
void shellSyntaxInACliCommandReachesAirflowAsPlainArguments() throws Exception {
List<List<String>> commands = ContainerExecStubs.completeEveryExec(dockerClient, AIRFLOW_ID, 0, "", "");
manager.runAirflowCli(AIRFLOW_ID, "dags list; touch /tmp/pwned $(id) `id` | cat > /tmp/out");
manager.runAirflowCli(AIRFLOW_ID, "dags list ; touch /tmp/pwned $(id) `id` | cat > /tmp/out");
assertEquals(List.of(List.of("airflow", "dags", "list;", "touch", "/tmp/pwned", "$(id)", "`id`",
assertEquals(List.of(List.of("airflow", "dags", "list", ";", "touch", "/tmp/pwned", "$(id)", "`id`",
"|", "cat", ">", "/tmp/out")), commands);
}
@Test
void unsupportedCommandsNeverReachTheAirflowContainer() throws Exception {
List<List<String>> commands = ContainerExecStubs.completeEveryExec(dockerClient, AIRFLOW_ID, 0, "", "");
for (String command : List.of("users list", "users create", "db shell", "config get-value",
"webserver", "scheduler", "celery worker", "dags unknown", "dags", "")) {
ContainerExec.Result result = manager.runAirflowCli(AIRFLOW_ID, command);
assertEquals(1, result.exitCode(), command);
assertTrue(result.stderr().contains("not supported"), command);
}
assertEquals(List.of(), commands);
}
@Test
void allowsEverySupportedMwaaCliCommand() {
for (String command : List.of(
"cheat-sheet", "version", "connections add", "connections delete",
"dags backfill", "dags delete", "dags list", "dags list-import-errors",
"dags list-jobs", "dags list-runs", "dags next-execution", "dags pause",
"dags report", "dags reserialize", "dags show", "dags state", "dags test",
"dags trigger", "dags unpause", "db clean", "providers behaviours",
"providers get", "providers hooks", "providers links", "providers list",
"providers notifications", "providers secrets", "providers triggerer",
"providers widgets", "roles add-perms", "roles create", "roles del-perms",
"roles list", "tasks clear", "tasks failed-deps", "tasks list", "tasks render",
"tasks run", "tasks state", "tasks states-for-dag-run", "tasks test",
"variables delete", "variables get", "variables list", "variables set")) {
assertTrue(MwaaEnvironmentManager.isSupportedCliCommand(List.of(command.split(" "))), command);
}
}
@Test
void quotedCliArgumentsReachAirflowWhole() throws Exception {
List<List<String>> commands = ContainerExecStubs.completeEveryExec(dockerClient, AIRFLOW_ID, 0, "", "");