/
pref_sshkeys.py
130 lines (109 loc) · 4.53 KB
/
pref_sshkeys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
# rdiffweb, A web interface to rdiff-backup repositories
# Copyright (C) 2012-2021 rdiffweb contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Plugins to allows users to configure the SSH keys using the web
interface. Basically it's a UI for `~/.ssh/authorized_keys`. For this
plugin to work properly, the users home directory need to match a real
user home.
"""
import logging
from wtforms import validators
from wtforms.fields.core import StringField
from wtforms.fields.simple import TextField
from wtforms.validators import ValidationError
from wtforms.widgets.core import TextArea
from rdiffweb.controller import Controller, flash
from rdiffweb.controller.cherrypy_wtf import CherryForm
from rdiffweb.controller.filter_authorization import is_maintainer
from rdiffweb.core import authorizedkeys
from rdiffweb.core.i18n import ugettext as _
from rdiffweb.core.store import DuplicateSSHKeyError
_logger = logging.getLogger(__name__)
def validate_key(unused_form, field):
"""Custom validator to check the SSH Key."""
key = authorizedkeys.check_publickey(field.data)
if not key:
raise ValidationError(_("Invalid SSH key."))
class SshForm(CherryForm):
title = StringField(
_('Title'),
description=_('The title is an optional description to identify the key. e.g.: bob@thinkpad-t530'),
validators=[validators.required()])
key = TextField(
_('Key'),
widget=TextArea(),
description=_("Enter a SSH public key. It should start with 'ssh-dss', 'ssh-ed25519', 'ssh-rsa', 'ecdsa-sha2-nistp256', 'ecdsa-sha2-nistp384' or 'ecdsa-sha2-nistp521'."),
validators=[validators.required(), validate_key])
fingerprint = StringField('Fingerprint')
class DeleteSshForm(CherryForm):
fingerprint = StringField('Fingerprint')
class SSHKeysPlugin(Controller):
"""
Plugin to configure SSH keys.
"""
panel_id = 'sshkeys'
panel_name = _('SSH Keys')
def _add_key(self, action, form):
assert action == 'add'
assert form
if not form.validate():
for unused, messages in form.errors.items():
for message in messages:
flash(message, level='warning')
return
try:
self.app.currentuser.add_authorizedkey(key=form.key.data, comment=form.title.data)
except DuplicateSSHKeyError as e:
flash(str(e), level='error')
except:
flash(_("Unknown error while adding the SSH Key"), level='error')
_logger.warning("error adding ssh key", exc_info=1)
def _delete_key(self, action, form):
assert action == 'delete'
assert form
if not form.validate():
for unused, messages in form.errors.items():
for message in messages:
flash(message, level='warning')
return
is_maintainer()
try:
self.app.currentuser.delete_authorizedkey(form.fingerprint.data)
except:
flash(_("Unknown error while removing the SSH Key"), level='error')
_logger.warning("error removing ssh key", exc_info=1)
def render_prefs_panel(self, panelid, action=None, **kwargs): # @UnusedVariable
# Handle action
form = SshForm()
if action == "add":
self._add_key(action, form)
elif action == 'delete':
self._delete_key(action, DeleteSshForm())
# Get SSH keys if file exists.
params = {
'form': form
}
try:
params["sshkeys"] = [
{'title': key.comment or (key.keytype + ' ' + key.key[:18]),
'fingerprint': key.fingerprint}
for key in self.app.currentuser.authorizedkeys]
except IOError:
params["sshkeys"] = []
flash(_("Failed to get SSH keys"), level='error')
_logger.warning("error reading SSH keys", exc_info=1)
return "prefs_sshkeys.html", params