mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-05-20 10:34:30 +00:00
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:
parent
0b43caf81d
commit
de7275d4a5
@ -31,12 +31,12 @@
|
|||||||
|
|
||||||
<p>
|
<p>
|
||||||
The authenticity of host {{ hostname }} cannot be established.<br/>
|
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>
|
</p>
|
||||||
|
|
||||||
<div class="accordion">
|
<section>
|
||||||
<p>
|
<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>
|
</p>
|
||||||
<div class="collapse" id="help">
|
<div class="collapse" id="help">
|
||||||
{% blocktrans trimmed %}
|
{% blocktrans trimmed %}
|
||||||
@ -46,7 +46,7 @@
|
|||||||
<code>sudo ssh-keygen -lf /etc/ssh/ssh_host_rsa_key</code>
|
<code>sudo ssh-keygen -lf /etc/ssh/ssh_host_rsa_key</code>
|
||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</section>
|
||||||
|
|
||||||
{{ form|bootstrap }}
|
{{ form|bootstrap }}
|
||||||
|
|
||||||
|
|||||||
@ -26,16 +26,15 @@ from .views import (AddRepositoryView, CreateArchiveView, DeleteArchiveView,
|
|||||||
UploadArchiveView, VerifySshHostkeyView, mount_repository,
|
UploadArchiveView, VerifySshHostkeyView, mount_repository,
|
||||||
umount_repository)
|
umount_repository)
|
||||||
|
|
||||||
# TODO Refactor path params to be more semantic
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
url(r'^sys/backups/$', IndexView.as_view(), name='index'),
|
url(r'^sys/backups/$', IndexView.as_view(), name='index'),
|
||||||
url(r'^sys/backups/create/$', CreateArchiveView.as_view(), name='create'),
|
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'),
|
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'),
|
DeleteArchiveView.as_view(), name='delete'),
|
||||||
url(r'^sys/backups/upload/$', UploadArchiveView.as_view(), name='upload'),
|
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'),
|
RestoreArchiveView.as_view(), name='restore-archive'),
|
||||||
url(r'^sys/backups/restore-from-upload/$', RestoreFromUploadView.as_view(),
|
url(r'^sys/backups/restore-from-upload/$', RestoreFromUploadView.as_view(),
|
||||||
name='restore-from-upload'),
|
name='restore-from-upload'),
|
||||||
|
|||||||
@ -282,8 +282,11 @@ class AddRepositoryView(SuccessMessageMixin, FormView):
|
|||||||
_, hostname, _ = re.split('[@:]', path)
|
_, hostname, _ = re.split('[@:]', path)
|
||||||
credentials = _get_credentials(form.cleaned_data)
|
credentials = _get_credentials(form.cleaned_data)
|
||||||
if not self._is_ssh_hostkey_verified(hostname):
|
if not self._is_ssh_hostkey_verified(hostname):
|
||||||
repository = SshBorgRepository(path=path,
|
# Cannot mount at this point because we cannot connect
|
||||||
credentials=credentials)
|
# and validate the directory.
|
||||||
|
repository = SshBorgRepository(
|
||||||
|
path=path, credentials=credentials, automount=False)
|
||||||
|
# Save for now, verify in the next view
|
||||||
repository.save(verified=False)
|
repository.save(verified=False)
|
||||||
uuid = repository.uuid
|
uuid = repository.uuid
|
||||||
url = reverse('backups:verify-ssh-hostkey', args=[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 = self.get_context_data()
|
||||||
context_data['form'] = form
|
context_data['form'] = form
|
||||||
return render(request, self.template_name, context_data)
|
return render(request, self.template_name, context_data)
|
||||||
try:
|
|
||||||
repository.get_info()
|
_create_borg_repository(repository,
|
||||||
except BorgRepositoryDoesNotExistError:
|
form.cleaned_data['encryption'])
|
||||||
repository.create_repository(
|
|
||||||
form.cleaned_data['encryption'])
|
|
||||||
repository.save()
|
|
||||||
return redirect(self.success_url)
|
return redirect(self.success_url)
|
||||||
else:
|
else:
|
||||||
context_data = self.get_context_data()
|
context_data = self.get_context_data()
|
||||||
@ -382,16 +382,23 @@ class VerifySshHostkeyView(SuccessMessageMixin, FormView):
|
|||||||
uuid=uuid)
|
uuid=uuid)
|
||||||
except ValidationError as err:
|
except ValidationError as err:
|
||||||
messages.error(self.request, err.message)
|
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)
|
network_storage.delete(uuid)
|
||||||
return redirect(reverse_lazy('backups:repository-add'))
|
return redirect(reverse_lazy('backups:repository-add'))
|
||||||
try:
|
_create_borg_repository(repository, repo_data.get(
|
||||||
repository.get_info()
|
'encryption', 'none'))
|
||||||
except BorgRepositoryDoesNotExistError:
|
|
||||||
repository.create_repository(repo_data.get('encryption', 'none'))
|
|
||||||
repository.save()
|
|
||||||
return super().form_valid(form)
|
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):
|
def _get_credentials(data):
|
||||||
credentials = {}
|
credentials = {}
|
||||||
for field_name in ["ssh_password", "encryption_passphrase"]:
|
for field_name in ["ssh_password", "encryption_passphrase"]:
|
||||||
@ -421,7 +428,7 @@ def _validate_remote_repository(path, credentials, uuid=None):
|
|||||||
try:
|
try:
|
||||||
ssh_client.connect(hostname, username=username, password=password)
|
ssh_client.connect(hostname, username=username, password=password)
|
||||||
except Exception as err:
|
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)})
|
raise ValidationError(msg, params={'err': str(err)})
|
||||||
else:
|
else:
|
||||||
sftp_client = ssh_client.open_sftp()
|
sftp_client = ssh_client.open_sftp()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user