backups: repository: Simplify handling of remote repo properties

- Simplify save() method such that a simple load(uuid).save() does not destroy
data. Currently, the verified parameter is lost.

- Drop unused store_credentials argument to save().

- Make verified a property of the SSH repository and instantiate with
it properly.

Tests performed:

- Adding a new remote repository works with SSH verification from unknown and
known hosts.

- Mounting and unmounting works.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2021-01-15 15:57:33 -08:00 committed by James Valleroy
parent 1170e438a3
commit 8c161431ba
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 24 additions and 20 deletions

View File

@ -80,11 +80,7 @@ class BaseBorgRepository(abc.ABC):
known_credentials = []
def __init__(self, path, credentials=None, uuid=None, **kwargs):
"""Instantiate a new repository.
If only a uuid is given, load the values from kvstore.
"""
"""Instantiate a new repository."""
self._path = path
self.credentials = credentials or {}
self.uuid = uuid or str(uuid1())
@ -295,28 +291,22 @@ class BaseBorgRepository(abc.ABC):
create_subvolume=False, backup_file=archive_path,
encryption_passphrase=passphrase)
def _get_storage_format(self, store_credentials, verified):
def _get_storage_format(self):
"""Return a dict representing the repository."""
storage = {
'path': self._path,
'storage_type': self.storage_type,
'added_by_module': 'backups',
'verified': verified
'credentials': self.credentials,
}
if self.uuid:
storage['uuid'] = self.uuid
if store_credentials:
storage['credentials'] = self.credentials
return storage
def save(self, store_credentials=True, verified=False):
"""Save the repository in store (kvstore).
- store_credentials: Boolean whether credentials should be stored.
"""
storage = self._get_storage_format(store_credentials, verified)
def save(self):
"""Save the repository in store (kvstore)."""
storage = self._get_storage_format()
self.uuid = store.update_or_add(storage)
@ -364,9 +354,21 @@ class SshBorgRepository(BaseBorgRepository):
sort_order = 30
flags = {'removable': True, 'mountable': True}
def __init__(self, path, credentials=None, uuid=None, schedule=None,
verified=None, **kwargs):
"""Instantiate a new repository."""
super().__init__(path, credentials, uuid, schedule, **kwargs)
self.verified = verified or False
def _get_storage_format(self):
"""Return a dict representing the repository."""
storage = super()._get_storage_format()
storage['verified'] = self.verified
return storage
def is_usable(self):
"""Return whether repository is usable."""
return self.kwargs.get('verified')
return self.verified
@property
def borg_path(self):

View File

@ -289,7 +289,8 @@ class AddRemoteRepositoryView(SuccessMessageMixin, FormView):
'encryption_passphrase': encryption_passphrase
}
repository = SshBorgRepository(path, credentials)
repository.save(verified=False)
repository.verfied = False
repository.save()
messages.success(self.request, _('Added new remote SSH repository.'))
url = reverse('backups:verify-ssh-hostkey', args=[repository.uuid])
@ -359,7 +360,8 @@ def _save_repository(request, repository):
"""Initialize and save a repository. Convert errors to messages."""
try:
repository.initialize()
repository.save(verified=True)
repository.verified = True
repository.save()
return True
except paramiko.BadHostKeyException:
message = _('SSH host public key could not be verified.')