backups: Show error when password is provided for unencrypted repo

- Without this error, users will believe that a repository is encrypted (as
  password is provided) but it is not (repository was created earlier without a
  password).

- Remove the need to send credentials, an instance property, while getting
  encryption data, an instance method.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2019-08-29 14:06:06 -07:00 committed by James Valleroy
parent 00da744a94
commit 9db1f186cd
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
2 changed files with 17 additions and 7 deletions

View File

@ -32,3 +32,7 @@ class SshfsError(PlinthError):
class BorgRepositoryExists(BorgError):
"""A repository at target location already exists during initialization."""
class BorgUnencryptedRepository(BorgError):
"""Attempt to provide password on an unencrypted repository."""

View File

@ -146,7 +146,14 @@ class BaseBorgRepository(abc.ABC):
def get_info(self):
"""Return Borg information about a repository."""
output = self.run(['info', '--path', self.borg_path])
return json.loads(output)
output = json.loads(output)
print(output, self._get_encryption_data())
if output['encryption']['mode'] == 'none' and \
self._get_encryption_data():
raise errors.BorgUnencryptedRepository(
_('Existing repository is not encrypted.'))
return output
def get_view_content(self):
"""Get archives with additional information as needed by the view"""
@ -154,7 +161,7 @@ class BaseBorgRepository(abc.ABC):
'uuid': self.uuid,
'name': self.name,
'storage_type': self.storage_type,
'is_encrypted': bool(self._get_encryption_data(self.credentials)),
'is_encrypted': bool(self._get_encryption_data()),
'flags': self.flags,
'error': None,
}
@ -204,10 +211,9 @@ class BaseBorgRepository(abc.ABC):
self.get_info() # If password is incorrect raise an error early.
@staticmethod
def _get_encryption_data(credentials):
def _get_encryption_data(self):
"""Return additional dictionary data to send to backups call."""
passphrase = credentials.get('encryption_passphrase', None)
passphrase = self.credentials.get('encryption_passphrase', None)
if passphrase:
return {'encryption_passphrase': passphrase}
@ -229,7 +235,7 @@ class BaseBorgRepository(abc.ABC):
if key not in self.known_credentials:
raise ValueError('Unknown credentials entry: %s' % key)
input_data = json.dumps(self._get_encryption_data(self.credentials))
input_data = json.dumps(self._get_encryption_data())
return self._run('backups', arguments, superuser=superuser,
input=input_data.encode())
@ -259,7 +265,7 @@ class BaseBorgRepository(abc.ABC):
return chunk
args = ['export-tar', '--path', self._get_archive_path(archive_name)]
input_data = json.dumps(self._get_encryption_data(self.credentials))
input_data = json.dumps(self._get_encryption_data())
proc = self._run('backups', args, run_in_background=True)
proc.stdin.write(input_data.encode())
proc.stdin.close()