From de7275d4a54d0074169b1c81c67960a82078c011 Mon Sep 17 00:00:00 2001 From: Joseph Nuthalapati Date: Mon, 10 Jun 2019 18:30:29 +0530 Subject: [PATCH] backups: ssh remotes: Refactoring - Make url scheme consistent - Add an FA icon to the drop-down button in VerifySshHostkeyView - Refactoring to reduce code duplication Signed-off-by: Joseph Nuthalapati --- .../backups/templates/verify_ssh_hostkey.html | 8 ++--- plinth/modules/backups/urls.py | 7 ++-- plinth/modules/backups/views.py | 35 +++++++++++-------- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/plinth/modules/backups/templates/verify_ssh_hostkey.html b/plinth/modules/backups/templates/verify_ssh_hostkey.html index c07bd5686..990a8aabb 100644 --- a/plinth/modules/backups/templates/verify_ssh_hostkey.html +++ b/plinth/modules/backups/templates/verify_ssh_hostkey.html @@ -31,12 +31,12 @@

The authenticity of host {{ hostname }} cannot be established.
- The SSH server advertises the following public keys. Please verify any one of them. + The SSH host advertises the following public keys. Please verify any one of them.

-
+

- +

{% blocktrans trimmed %} @@ -46,7 +46,7 @@ sudo ssh-keygen -lf /etc/ssh/ssh_host_rsa_key

-
+ {{ form|bootstrap }} diff --git a/plinth/modules/backups/urls.py b/plinth/modules/backups/urls.py index 6ff3f61d9..13f6351e7 100644 --- a/plinth/modules/backups/urls.py +++ b/plinth/modules/backups/urls.py @@ -26,16 +26,15 @@ from .views import (AddRepositoryView, CreateArchiveView, DeleteArchiveView, UploadArchiveView, VerifySshHostkeyView, mount_repository, umount_repository) -# TODO Refactor path params to be more semantic urlpatterns = [ url(r'^sys/backups/$', IndexView.as_view(), name='index'), url(r'^sys/backups/create/$', CreateArchiveView.as_view(), name='create'), - url(r'^sys/backups/download/(?P[^/]+)/(?P[^/]+)/$', + url(r'^sys/backups/(?P[^/]+)/download/(?P[^/]+)/$', DownloadArchiveView.as_view(), name='download'), - url(r'^sys/backups/delete/(?P[^/]+)/(?P[^/]+)/$', + url(r'^sys/backups/(?P[^/]+)/delete/(?P[^/]+)/$', DeleteArchiveView.as_view(), name='delete'), url(r'^sys/backups/upload/$', UploadArchiveView.as_view(), name='upload'), - url(r'^sys/backups/restore-archive/(?P[^/]+)/(?P[^/]+)/$', + url(r'^sys/backups/(?P[^/]+)/restore-archive/(?P[^/]+)/$', RestoreArchiveView.as_view(), name='restore-archive'), url(r'^sys/backups/restore-from-upload/$', RestoreFromUploadView.as_view(), name='restore-from-upload'), diff --git a/plinth/modules/backups/views.py b/plinth/modules/backups/views.py index c9337d5d4..036ee6178 100644 --- a/plinth/modules/backups/views.py +++ b/plinth/modules/backups/views.py @@ -282,8 +282,11 @@ class AddRepositoryView(SuccessMessageMixin, FormView): _, hostname, _ = re.split('[@:]', path) credentials = _get_credentials(form.cleaned_data) if not self._is_ssh_hostkey_verified(hostname): - repository = SshBorgRepository(path=path, - credentials=credentials) + # Cannot mount at this point because we cannot connect + # and validate the directory. + repository = SshBorgRepository( + path=path, credentials=credentials, automount=False) + # Save for now, verify in the next view repository.save(verified=False) uuid = repository.uuid url = reverse('backups:verify-ssh-hostkey', args=[uuid]) @@ -296,12 +299,9 @@ class AddRepositoryView(SuccessMessageMixin, FormView): context_data = self.get_context_data() context_data['form'] = form return render(request, self.template_name, context_data) - try: - repository.get_info() - except BorgRepositoryDoesNotExistError: - repository.create_repository( - form.cleaned_data['encryption']) - repository.save() + + _create_borg_repository(repository, + form.cleaned_data['encryption']) return redirect(self.success_url) else: context_data = self.get_context_data() @@ -382,16 +382,23 @@ class VerifySshHostkeyView(SuccessMessageMixin, FormView): uuid=uuid) except ValidationError as err: messages.error(self.request, err.message) + # If a ValidationError is thrown, delete the repository + # so that the user can have another go at creating it. network_storage.delete(uuid) return redirect(reverse_lazy('backups:repository-add')) - try: - repository.get_info() - except BorgRepositoryDoesNotExistError: - repository.create_repository(repo_data.get('encryption', 'none')) - repository.save() + _create_borg_repository(repository, repo_data.get( + 'encryption', 'none')) return super().form_valid(form) +def _create_borg_repository(repository, encryption='none'): + try: + repository.get_info() + except BorgRepositoryDoesNotExistError: + repository.create_repository(encryption) + repository.save() + + def _get_credentials(data): credentials = {} for field_name in ["ssh_password", "encryption_passphrase"]: @@ -421,7 +428,7 @@ def _validate_remote_repository(path, credentials, uuid=None): try: ssh_client.connect(hostname, username=username, password=password) except Exception as err: - msg = _('Accessing the remote repository failed. Details: %(err)s') + msg = _(f'Accessing the remote repository failed. Details: {err}') raise ValidationError(msg, params={'err': str(err)}) else: sftp_client = ssh_client.open_sftp()