ssh: Use privileged decorator for actions

Tests:

- Functional tests work
- Updating SSH keys in user edit page sets the SSH keys. File is updated
  properly. Page shows newly set SSH keys.
  - Entering invalid auth credentials throws error
- Enabling/disabling SSH password authentication works.
  - Configuration file is updated.
  - App page shows proper value

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa authored and James Valleroy committed 2022-10-08 18:53:15 -04:00
1 parent 8bdb73df9a
commit 12cf5065b0
5 files changed
+51 -121

No files matched your search

+5 -10
View File
@@ -15,6 +15,7 @@ from django.utils.translation import gettext as _
from django.utils.translation import gettext_lazy
import plinth.forms
import plinth.modules.ssh.privileged as ssh_privileged
from plinth import actions
from plinth.errors import ActionError
from plinth.modules import first_boot
@@ -294,16 +295,10 @@ class UserUpdateForm(ValidNewUsernameCheckMixin, PasswordConfirmForm,
_('Failed to add user to group.'))
try:
actions.superuser_run('ssh', [
'set-keys',
'--username',
user.get_username(),
'--keys',
self.cleaned_data['ssh_keys'].strip(),
'--auth-user',
auth_username,
], input=confirm_password.encode())
except ActionError:
ssh_privileged.set_keys(user.get_username(),
self.cleaned_data['ssh_keys'].strip(),
auth_username, confirm_password)
except Exception:
messages.error(self.request, _('Unable to set SSH keys.'))
is_active = self.cleaned_data['is_active']
+4 -2
View File
@@ -1,4 +1,5 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Django views for user app."""
import django.views.generic
from django.contrib import messages
@@ -13,6 +14,7 @@ from django.utils.translation import gettext_lazy
from django.views.generic.edit import (CreateView, DeleteView, FormView,
UpdateView)
import plinth.modules.ssh.privileged as ssh_privileged
from plinth import actions, translation
from plinth.errors import ActionError
from plinth.modules import first_boot
@@ -36,6 +38,7 @@ class ContextMixin:
class UserCreate(ContextMixin, SuccessMessageMixin, CreateView):
"""View to create a new user."""
form_class = CreateUserForm
template_name = 'users_create.html'
model = User
@@ -95,8 +98,7 @@ class UserUpdate(ContextMixin, SuccessMessageMixin, UpdateView):
"""Return the data for initial form load."""
initial = super().get_initial()
try:
ssh_keys = actions.superuser_run(
'ssh', ['get-keys', '--username', self.object.username])
ssh_keys = ssh_privileged.get_keys(self.object.username)
initial['ssh_keys'] = ssh_keys.strip()
initial['language'] = self.object.userprofile.language
except ActionError: