Skip to content

Commit

Permalink
Address review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
emontnemery committed Nov 11, 2024
1 parent 4014ceb commit 5f5d964
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
6 changes: 2 additions & 4 deletions homeassistant/backup_restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,8 @@ class RestoreBackupFileContent:
password: str | None = None


def password_to_key(password: str | None) -> bytes | None:
def password_to_key(password: str) -> bytes:
"""Generate a AES Key from password."""
if password is None:
return None
key: bytes = password.encode()
for _ in range(100):
key = hashlib.sha256(key).digest()
Expand Down Expand Up @@ -105,7 +103,7 @@ def _extract_backup(
f"homeassistant.tar{'.gz' if backup_meta["compressed"] else ''}",
),
gzip=backup_meta["compressed"],
key=password_to_key(password),
key=password_to_key(password) if password is not None else None,
mode="r",
) as istf:
istf.extractall(
Expand Down
6 changes: 5 additions & 1 deletion homeassistant/components/backup/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ async def load_platforms(self) -> None:
async def async_restore_backup(
self,
slug: str,
*,
password: str | None = None,
**kwargs: Any,
) -> None:
Expand All @@ -137,6 +138,7 @@ async def async_restore_backup(
@abc.abstractmethod
async def async_create_backup(
self,
*,
password: str | None = None,
**kwargs: Any,
) -> Backup:
Expand Down Expand Up @@ -235,6 +237,7 @@ async def async_remove_backup(self, *, slug: str, **kwargs: Any) -> None:

async def async_create_backup(
self,
*,
password: str | None = None,
**kwargs: Any,
) -> Backup:
Expand Down Expand Up @@ -308,7 +311,7 @@ def _mkdir_and_generate_backup_contents(
with outer_secure_tarfile.create_inner_tar(
"./homeassistant.tar.gz",
gzip=True,
key=password_to_key(password=password),
key=password_to_key(password) if password is not None else None,
) as core_tar:
atomic_contents_add(
tar_file=core_tar,
Expand All @@ -321,6 +324,7 @@ def _mkdir_and_generate_backup_contents(
async def async_restore_backup(
self,
slug: str,
*,
password: str | None = None,
**kwargs: Any,
) -> None:
Expand Down
15 changes: 14 additions & 1 deletion tests/test_backup_restore.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,24 @@ def _patched_path_read_text(path: Path, **kwargs):
@pytest.mark.parametrize(
("password", "expected"),
[
(None, None),
("test", b"\xf0\x9b\xb9\x1f\xdc,\xff\xd5x\xd6\xd6\x8fz\x19.\x0f"),
("lorem ipsum...", b"#\xe0\xfc\xe0\xdb?_\x1f,$\rQ\xf4\xf5\xd8\xfb"),
],
)
def test_pw_to_key(password: str | None, expected: bytes | None) -> None:
"""Test password to key conversion."""
assert backup_restore.password_to_key(password) == expected


@pytest.mark.parametrize(
("password", "expected"),
[
(None, None),
("test", b"\xf0\x9b\xb9\x1f\xdc,\xff\xd5x\xd6\xd6\x8fz\x19.\x0f"),
("lorem ipsum...", b"#\xe0\xfc\xe0\xdb?_\x1f,$\rQ\xf4\xf5\xd8\xfb"),
],
)
def test_pw_to_key_none(password: str | None, expected: bytes | None) -> None:
"""Test password to key conversion."""
with pytest.raises(AttributeError):
backup_restore.password_to_key(None)

0 comments on commit 5f5d964

Please sign in to comment.