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 <njoseph@thoughtworks.com>
This commit is contained in:
Joseph Nuthalapati 2019-06-10 18:30:29 +05:30
parent 0b43caf81d
commit de7275d4a5
No known key found for this signature in database
GPG Key ID: 5398F00A2FA43C35
3 changed files with 28 additions and 22 deletions

View File

@ -31,12 +31,12 @@
<p>
The authenticity of host {{ hostname }} cannot be established.<br/>
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.
</p>
<div class="accordion">
<section>
<p>
<a class="btn btn-default" data-toggle="collapse" href="#help" aria-expanded="false" aria-controls="footwear">{% trans "How to verify?" %}</a>
<a class="btn btn-default" data-toggle="collapse" href="#help" aria-expanded="false">{% trans "How to verify?" %} <i class="fa fa-chevron-down fa-fw" aria-hidden="true"></i></a>
</p>
<div class="collapse" id="help">
{% blocktrans trimmed %}
@ -46,7 +46,7 @@
<code>sudo ssh-keygen -lf /etc/ssh/ssh_host_rsa_key</code>
</p>
</div>
</div>
</section>
{{ form|bootstrap }}

View File

@ -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<uuid>[^/]+)/(?P<name>[^/]+)/$',
url(r'^sys/backups/(?P<uuid>[^/]+)/download/(?P<name>[^/]+)/$',
DownloadArchiveView.as_view(), name='download'),
url(r'^sys/backups/delete/(?P<uuid>[^/]+)/(?P<name>[^/]+)/$',
url(r'^sys/backups/(?P<uuid>[^/]+)/delete/(?P<name>[^/]+)/$',
DeleteArchiveView.as_view(), name='delete'),
url(r'^sys/backups/upload/$', UploadArchiveView.as_view(), name='upload'),
url(r'^sys/backups/restore-archive/(?P<uuid>[^/]+)/(?P<name>[^/]+)/$',
url(r'^sys/backups/(?P<uuid>[^/]+)/restore-archive/(?P<name>[^/]+)/$',
RestoreArchiveView.as_view(), name='restore-archive'),
url(r'^sys/backups/restore-from-upload/$', RestoreFromUploadView.as_view(),
name='restore-from-upload'),

View File

@ -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()