mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
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:
1 parent
6a98f69a5d
commit
8a2b998378
3 files changed
+64
-4
No files matched your search
@@ -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
|
||||
|
||||
Reference in new issue
Block a user