Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't store received backups in a TempDir #132272

Merged
merged 5 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 14 additions & 20 deletions homeassistant/components/backup/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from pathlib import Path
import shutil
import tarfile
from tempfile import TemporaryDirectory
import time
from typing import TYPE_CHECKING, Any, Protocol

Expand Down Expand Up @@ -382,25 +381,11 @@ async def async_receive_backup(
contents: aiohttp.BodyPartReader,
) -> None:
"""Receive and store a backup file from upload."""
temp_dir_handler = await self.hass.async_add_executor_job(TemporaryDirectory)
target_temp_file = Path(
temp_dir_handler.name, contents.filename or "backup.tar"
)
target_temp_file = Path(self.temp_backup_dir, contents.filename or "backup.tar")
await self.hass.async_add_executor_job(make_backup_dir, self.temp_backup_dir)

await receive_file(self.hass, contents, target_temp_file)

def _copy_and_cleanup(
local_file_paths: list[Path], backup: AgentBackup
) -> Path:
if local_file_paths:
tar_file_path = local_file_paths[0]
else:
tar_file_path = self.temp_backup_dir / f"{backup.backup_id}.tar"
for local_path in local_file_paths:
shutil.copy(target_temp_file, local_path)
temp_dir_handler.cleanup()
return tar_file_path

try:
backup = await self.hass.async_add_executor_job(
read_backup, target_temp_file
Expand All @@ -414,9 +399,18 @@ def _copy_and_cleanup(
for agent_id in agent_ids
if agent_id in self.local_backup_agents
]
tar_file_path = await self.hass.async_add_executor_job(
_copy_and_cleanup, local_file_paths, backup
)
if local_file_paths:

def _copy_to_local_agents(local_file_paths: list[Path]) -> Path:
for local_path in local_file_paths:
shutil.copy(target_temp_file, local_path)
return local_file_paths[0]

tar_file_path = await self.hass.async_add_executor_job(
_copy_to_local_agents, local_file_paths
)
else:
tar_file_path = target_temp_file
await self._async_upload_backup(
backup=backup, agent_ids=agent_ids, path=tar_file_path
)
Expand Down
1 change: 1 addition & 0 deletions tests/components/cloud/test_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -525,6 +525,7 @@ async def test_agents_upload_not_protected(
size=0.0,
)
with (
patch("pathlib.Path.open"),
patch(
"homeassistant.components.backup.manager.read_backup",
return_value=test_backup,
Expand Down
1 change: 1 addition & 0 deletions tests/components/hassio/test_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@

supervisor_client.backups.reload.assert_not_called()
with (
patch("pathlib.Path.open"),
patch(
"homeassistant.components.backup.manager.BackupManager.async_get_backup",
) as fetch_backup,
Expand All @@ -199,7 +200,7 @@
data={"file": StringIO("test")},
)

assert resp.status == 201

Check failure on line 203 in tests/components/hassio/test_backup.py

View workflow job for this annotation

GitHub Actions / Run tests Python 3.12 (hassio)

test_agent_upload AssertionError: assert 500 == 201 + where 500 = <ClientResponse(http://127.0.0.1:35851/api/backup/upload?agent_id=hassio.local) [500 Internal Server Error]>\n<CIMultiD...iff', 'Server': '', 'X-Frame-Options': 'SAMEORIGIN', 'Content-Length': '70', 'Date': 'Wed, 04 Dec 2024 16:20:36 GMT')>\n.status
supervisor_client.backups.reload.assert_called_once()


Expand Down
1 change: 1 addition & 0 deletions tests/components/kitchen_sink/test_backup.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ async def test_agents_upload(
)

with (
patch("pathlib.Path.open"),
patch(
"homeassistant.components.backup.manager.BackupManager.async_get_backup",
) as fetch_backup,
Expand Down
Loading