Skip to content

Commit 78b532e

Browse files
committed
Harden SSH remote cleanup validation for custom bases (apache#69813)
1 parent 17f3f48 commit 78b532e

4 files changed

Lines changed: 65 additions & 6 deletions

File tree

‎providers/ssh/src/airflow/providers/ssh/operators/ssh_remote_job.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,10 +452,16 @@ def _cleanup_remote_job(self, job_dir: str, remote_os: str) -> None:
452452
"""
453453
self.log.info("Cleaning up remote job directory: %s", job_dir)
454454
expected_base = self._paths.base_dir if self._paths else self.remote_base_dir
455-
if remote_os == "posix":
456-
cleanup_cmd = build_posix_cleanup_command(job_dir, expected_base=expected_base)
457-
else:
458-
cleanup_cmd = build_windows_cleanup_command(job_dir, expected_base=expected_base)
455+
try:
456+
if remote_os == "posix":
457+
cleanup_cmd = build_posix_cleanup_command(job_dir, expected_base=expected_base)
458+
else:
459+
cleanup_cmd = build_windows_cleanup_command(job_dir, expected_base=expected_base)
460+
except ValueError as e:
461+
self.log.warning(
462+
"Cleanup validation failed; leaving remote job directory %s in place: %s", job_dir, e
463+
)
464+
return
459465

460466
last_error: Exception | None = None
461467
for attempt in range(1, self.cleanup_retries + 1):

‎providers/ssh/src/airflow/providers/ssh/utils/remote_job.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ def _validate_job_dir(
4444
:raises ValueError: If job_dir doesn't start with the expected base path
4545
"""
4646
if remote_os == "posix":
47-
expected_base = expected_base or POSIX_DEFAULT_BASE_DIR
47+
expected_base = POSIX_DEFAULT_BASE_DIR if expected_base is None else expected_base
4848
expected_prefix = expected_base + "/"
4949
else:
50-
expected_base = expected_base or WINDOWS_DEFAULT_BASE_DIR
50+
expected_base = WINDOWS_DEFAULT_BASE_DIR if expected_base is None else expected_base
5151
expected_prefix = expected_base + "\\"
5252

5353
if not job_dir.startswith(expected_prefix):

‎providers/ssh/tests/unit/ssh/operators/test_ssh_remote_job.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,35 @@ def test_execute_complete_with_cleanup(self):
333333
call_args = self.mock_hook.exec_ssh_client_command.call_args
334334
assert "rm -rf" in call_args[0][1]
335335

336+
def test_execute_complete_with_cleanup_custom_remote_base_dir(self):
337+
"""Test execute_complete performs cleanup under a custom remote base directory."""
338+
op = SSHRemoteJobOperator(
339+
task_id="test_task",
340+
ssh_conn_id="test_conn",
341+
command="/path/to/script.sh",
342+
cleanup="on_success",
343+
remote_base_dir="/tmp-data/airflow-ssh-jobs",
344+
)
345+
346+
event = {
347+
"done": True,
348+
"status": "success",
349+
"exit_code": 0,
350+
"job_id": "test_job_123",
351+
"job_dir": "/tmp-data/airflow-ssh-jobs/test_job_123",
352+
"log_file": "/tmp-data/airflow-ssh-jobs/test_job_123/stdout.log",
353+
"exit_code_file": "/tmp-data/airflow-ssh-jobs/test_job_123/exit_code",
354+
"log_chunk": "",
355+
"log_offset": 0,
356+
"remote_os": "posix",
357+
}
358+
359+
op.execute_complete({}, event)
360+
361+
self.mock_hook.exec_ssh_client_command.assert_called_once()
362+
call_args = self.mock_hook.exec_ssh_client_command.call_args
363+
assert "rm -rf '/tmp-data/airflow-ssh-jobs/test_job_123'" == call_args[0][1]
364+
336365
def test_on_kill(self):
337366
"""Test on_kill attempts to kill remote process."""
338367
op = SSHRemoteJobOperator(
@@ -460,3 +489,17 @@ def test_cleanup_gives_up_after_retries_without_raising(self):
460489
op._cleanup_remote_job("/tmp/airflow-ssh-jobs/test_job_123", "posix")
461490

462491
assert self.mock_hook.exec_ssh_client_command.call_count == 3
492+
493+
def test_cleanup_validation_failure_leaves_directory_without_raising(self, caplog):
494+
"""A cleanup validation failure leaves the remote directory in place."""
495+
op = SSHRemoteJobOperator(
496+
task_id="test_task",
497+
ssh_conn_id="test_conn",
498+
command="/path/to/script.sh",
499+
remote_base_dir="/tmp-data/airflow-ssh-jobs",
500+
)
501+
502+
op._cleanup_remote_job("/tmp/airflow-ssh-jobs/test_job_123", "posix")
503+
504+
self.mock_hook.exec_ssh_client_command.assert_not_called()
505+
assert "Cleanup validation failed" in caplog.text

‎providers/ssh/tests/unit/ssh/utils/test_remote_job.py‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,11 @@ def test_posix_cleanup_rejects_path_outside_custom_base(self):
433433
"/tmp/airflow-ssh-jobs/job_123", expected_base="/tmp-data/airflow-ssh-jobs"
434434
)
435435

436+
def test_posix_cleanup_does_not_replace_empty_expected_base_with_default(self):
437+
"""Test an empty expected base does not fall back to the default base directory."""
438+
with pytest.raises(ValueError, match="Invalid job directory"):
439+
build_posix_cleanup_command("/tmp/airflow-ssh-jobs/job_123", expected_base="")
440+
436441
def test_windows_cleanup_rejects_invalid_path(self):
437442
"""Test Windows cleanup rejects paths outside expected base directory."""
438443
with pytest.raises(ValueError, match="Invalid job directory"):
@@ -445,3 +450,8 @@ def test_windows_cleanup_rejects_path_outside_custom_base(self):
445450
"$env:TEMP\\airflow-ssh-jobs\\job_123",
446451
expected_base="$env:TEMP\\custom-airflow-ssh-jobs",
447452
)
453+
454+
def test_windows_cleanup_does_not_replace_empty_expected_base_with_default(self):
455+
"""Test an empty expected base does not fall back to the default base directory."""
456+
with pytest.raises(ValueError, match="Invalid job directory"):
457+
build_windows_cleanup_command("$env:TEMP\\airflow-ssh-jobs\\job_123", expected_base="")

0 commit comments

Comments
 (0)