|
30 | 30 | WINDOWS_DEFAULT_BASE_DIR = "$env:TEMP\\airflow-ssh-jobs" |
31 | 31 |
|
32 | 32 |
|
33 | | -def _validate_job_dir(job_dir: str, remote_os: Literal["posix", "windows"]) -> None: |
| 33 | +def _validate_job_dir( |
| 34 | + job_dir: str, |
| 35 | + remote_os: Literal["posix", "windows"], |
| 36 | + expected_base: str | None = None, |
| 37 | +) -> None: |
34 | 38 | """ |
35 | 39 | Validate that job_dir is under the expected base directory. |
36 | 40 |
|
37 | 41 | :param job_dir: The job directory path to validate |
38 | 42 | :param remote_os: Operating system type |
| 43 | + :param expected_base: Expected base directory for the job |
39 | 44 | :raises ValueError: If job_dir doesn't start with the expected base path |
40 | 45 | """ |
41 | 46 | if remote_os == "posix": |
42 | | - expected_prefix = POSIX_DEFAULT_BASE_DIR + "/" |
| 47 | + expected_base = expected_base or POSIX_DEFAULT_BASE_DIR |
| 48 | + expected_prefix = expected_base + "/" |
43 | 49 | else: |
44 | | - expected_prefix = WINDOWS_DEFAULT_BASE_DIR + "\\" |
| 50 | + expected_base = expected_base or WINDOWS_DEFAULT_BASE_DIR |
| 51 | + expected_prefix = expected_base + "\\" |
45 | 52 |
|
46 | 53 | if not job_dir.startswith(expected_prefix): |
47 | 54 | raise ValueError( |
@@ -452,27 +459,29 @@ def build_windows_kill_command(pid_file: str) -> str: |
452 | 459 | return f"powershell.exe -NoProfile -NonInteractive -EncodedCommand {encoded_script}" |
453 | 460 |
|
454 | 461 |
|
455 | | -def build_posix_cleanup_command(job_dir: str) -> str: |
| 462 | +def build_posix_cleanup_command(job_dir: str, expected_base: str | None = None) -> str: |
456 | 463 | """ |
457 | 464 | Build a POSIX command to clean up the job directory. |
458 | 465 |
|
459 | 466 | :param job_dir: Path to the job directory |
| 467 | + :param expected_base: Expected base directory for the job |
460 | 468 | :return: Shell command to remove the directory |
461 | 469 | :raises ValueError: If job_dir is not under the expected base directory |
462 | 470 | """ |
463 | | - _validate_job_dir(job_dir, "posix") |
| 471 | + _validate_job_dir(job_dir, "posix", expected_base) |
464 | 472 | return f"rm -rf '{job_dir}'" |
465 | 473 |
|
466 | 474 |
|
467 | | -def build_windows_cleanup_command(job_dir: str) -> str: |
| 475 | +def build_windows_cleanup_command(job_dir: str, expected_base: str | None = None) -> str: |
468 | 476 | """ |
469 | 477 | Build a PowerShell command to clean up the job directory. |
470 | 478 |
|
471 | 479 | :param job_dir: Path to the job directory |
| 480 | + :param expected_base: Expected base directory for the job |
472 | 481 | :return: PowerShell command to remove the directory |
473 | 482 | :raises ValueError: If job_dir is not under the expected base directory |
474 | 483 | """ |
475 | | - _validate_job_dir(job_dir, "windows") |
| 484 | + _validate_job_dir(job_dir, "windows", expected_base) |
476 | 485 | escaped_path = job_dir.replace("'", "''") |
477 | 486 | script = f"Remove-Item -Recurse -Force -Path '{escaped_path}' -ErrorAction SilentlyContinue" |
478 | 487 | script_bytes = script.encode("utf-16-le") |
|
0 commit comments