Skip to content

Commit

Permalink
Encrypt backup metadata (#170)
Browse files Browse the repository at this point in the history
* Encrypt backup metadata

* Log crypto error
  • Loading branch information
kirillgarbar authored Jul 17, 2024
1 parent 32b9373 commit de0178c
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions ch_backup/backup/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
from typing import Callable, List, Optional, Sequence
from urllib.parse import quote

from nacl.exceptions import CryptoError

from ch_backup import logging
from ch_backup.backup.metadata import BackupMetadata, PartMetadata
from ch_backup.calculators import calc_encrypted_size, calc_tarball_size
Expand Down Expand Up @@ -50,7 +52,7 @@ def upload_backup_metadata(self, backup: BackupMetadata) -> None:
try:
logging.debug("Saving backup metadata in {}", remote_path)
self._storage_loader.upload_data(
backup.dump_json(light=False), remote_path=remote_path
backup.dump_json(light=False), remote_path=remote_path, encryption=True
)
logging.debug("Saving backup light metadata in {}", remote_light_path)
self._storage_loader.upload_data(
Expand Down Expand Up @@ -283,6 +285,15 @@ def get_backup_names(self) -> Sequence[str]:
self._config["path_root"], recursive=False, absolute=False
)

def _load_metadata(self, path: str, encryption: bool) -> BackupMetadata:
try:
data = self._storage_loader.download_data(path, encryption=encryption)
return BackupMetadata.load_json(data)
except CryptoError:
raise
except Exception as e:
raise StorageError("Failed to download backup metadata") from e

def get_backup(
self, backup_name: str, use_light_meta: bool = False
) -> Optional[BackupMetadata]:
Expand All @@ -298,11 +309,16 @@ def get_backup(
if not self._storage_loader.path_exists(path):
return None

# New backup metadata is encrypted
# Retry in case it is old and not encrypted
try:
data = self._storage_loader.download_data(path)
return BackupMetadata.load_json(data)
except Exception as e:
raise StorageError("Failed to download backup metadata") from e
return self._load_metadata(path, not use_light_meta)
except CryptoError:
logging.exception(
"Attempt to download encrypted metadata from {} has failed. Will try to download it as not encrypted",
path,
)
return self._load_metadata(path, False)

def get_backups(self, use_light_meta: bool = False) -> List[BackupMetadata]:
"""
Expand Down Expand Up @@ -333,11 +349,12 @@ def reload_backup(
else self._backup_metadata_path(backup.name)
)

# New backup metadata is encrypted
# Retry in case it is old and not encrypted
try:
data = self._storage_loader.download_data(path)
return BackupMetadata.load_json(data)
except Exception as e:
raise StorageError("Failed to download backup metadata") from e
return self._load_metadata(path, not use_light_meta)
except CryptoError:
return self._load_metadata(path, False)

def get_database_create_statement(
self, backup_meta: BackupMetadata, db_name: str
Expand Down

0 comments on commit de0178c

Please sign in to comment.