From 9db1f186cdea9c7f1c501a674bc72bbaa1e947d1 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Thu, 29 Aug 2019 14:06:06 -0700 Subject: [PATCH] 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 Reviewed-by: James Valleroy --- plinth/modules/backups/errors.py | 4 ++++ plinth/modules/backups/repository.py | 20 +++++++++++++------- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/plinth/modules/backups/errors.py b/plinth/modules/backups/errors.py index a638f4bfa..019b548d1 100644 --- a/plinth/modules/backups/errors.py +++ b/plinth/modules/backups/errors.py @@ -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.""" diff --git a/plinth/modules/backups/repository.py b/plinth/modules/backups/repository.py index b240d0432..cb0fd8a31 100644 --- a/plinth/modules/backups/repository.py +++ b/plinth/modules/backups/repository.py @@ -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()