my_account_ssh_keys.py
144 lines
| 5.5 KiB
| text/x-python
|
PythonLexer
r5088 | # Copyright (C) 2016-2023 RhodeCode GmbH | |||
r2044 | # | |||
# This program is free software: you can redistribute it and/or modify | ||||
# it under the terms of the GNU Affero General Public License, version 3 | ||||
# (only), as published by the Free Software Foundation. | ||||
# | ||||
# 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 Affero General Public License | ||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
# | ||||
# This program is dual-licensed. If you wish to learn more about the | ||||
# RhodeCode Enterprise Edition, including its added features, Support services, | ||||
# and proprietary license terms, please see https://rhodecode.com/licenses/ | ||||
import logging | ||||
from pyramid.httpexceptions import HTTPFound | ||||
from rhodecode.apps._base import BaseAppView, DataGridAppView | ||||
r5318 | from rhodecode.apps.ssh_support.events import SshKeyFileChangeEvent | |||
r2044 | from rhodecode.events import trigger | |||
from rhodecode.lib import helpers as h | ||||
from rhodecode.lib import audit_logger | ||||
from rhodecode.lib.auth import LoginRequired, NotAnonymous, CSRFRequired | ||||
from rhodecode.model.db import IntegrityError, UserSshKeys | ||||
from rhodecode.model.meta import Session | ||||
from rhodecode.model.ssh_key import SshKeyModel | ||||
log = logging.getLogger(__name__) | ||||
class MyAccountSshKeysView(BaseAppView, DataGridAppView): | ||||
def load_default_context(self): | ||||
c = self._get_local_tmpl_context() | ||||
c.user = c.auth_user.get_instance() | ||||
r2045 | c.ssh_enabled = self.request.registry.settings.get( | |||
'ssh.generate_authorized_keyfile') | ||||
r2044 | return c | |||
@LoginRequired() | ||||
@NotAnonymous() | ||||
def my_account_ssh_keys(self): | ||||
_ = self.request.translate | ||||
c = self.load_default_context() | ||||
c.active = 'ssh_keys' | ||||
c.default_key = self.request.GET.get('default_key') | ||||
c.user_ssh_keys = SshKeyModel().get_ssh_keys(c.user.user_id) | ||||
return self._get_template_context(c) | ||||
@LoginRequired() | ||||
@NotAnonymous() | ||||
def ssh_keys_generate_keypair(self): | ||||
_ = self.request.translate | ||||
c = self.load_default_context() | ||||
c.active = 'ssh_keys_generate' | ||||
r3478 | if c.ssh_key_generator_enabled: | |||
r4238 | private_format = self.request.GET.get('private_format') \ | |||
or SshKeyModel.DEFAULT_PRIVATE_KEY_FORMAT | ||||
r3478 | comment = 'RhodeCode-SSH {}'.format(c.user.email or '') | |||
r4238 | c.private, c.public = SshKeyModel().generate_keypair( | |||
comment=comment, private_format=private_format) | ||||
r3478 | c.target_form_url = h.route_path( | |||
'my_account_ssh_keys', _query=dict(default_key=c.public)) | ||||
r2044 | return self._get_template_context(c) | |||
@LoginRequired() | ||||
@NotAnonymous() | ||||
@CSRFRequired() | ||||
def my_account_ssh_keys_add(self): | ||||
_ = self.request.translate | ||||
c = self.load_default_context() | ||||
user_data = c.user.get_api_data() | ||||
key_data = self.request.POST.get('key_data') | ||||
description = self.request.POST.get('description') | ||||
r2594 | fingerprint = 'unknown' | |||
r2044 | try: | |||
if not key_data: | ||||
raise ValueError('Please add a valid public key') | ||||
key = SshKeyModel().parse_key(key_data.strip()) | ||||
fingerprint = key.hash_md5() | ||||
ssh_key = SshKeyModel().create( | ||||
r2751 | c.user.user_id, fingerprint, key.keydata, description) | |||
r2044 | ssh_key_data = ssh_key.get_api_data() | |||
audit_logger.store_web( | ||||
'user.edit.ssh_key.add', action_data={ | ||||
'data': {'ssh_key': ssh_key_data, 'user': user_data}}, | ||||
user=self._rhodecode_user, ) | ||||
Session().commit() | ||||
# Trigger an event on change of keys. | ||||
trigger(SshKeyFileChangeEvent(), self.request.registry) | ||||
h.flash(_("Ssh Key successfully created"), category='success') | ||||
except IntegrityError: | ||||
log.exception("Exception during ssh key saving") | ||||
r2594 | err = 'Such key with fingerprint `{}` already exists, ' \ | |||
'please use a different one'.format(fingerprint) | ||||
h.flash(_('An error occurred during ssh key saving: {}').format(err), | ||||
r2044 | category='error') | |||
except Exception as e: | ||||
log.exception("Exception during ssh key saving") | ||||
h.flash(_('An error occurred during ssh key saving: {}').format(e), | ||||
category='error') | ||||
return HTTPFound(h.route_path('my_account_ssh_keys')) | ||||
@LoginRequired() | ||||
@NotAnonymous() | ||||
@CSRFRequired() | ||||
def my_account_ssh_keys_delete(self): | ||||
_ = self.request.translate | ||||
c = self.load_default_context() | ||||
user_data = c.user.get_api_data() | ||||
del_ssh_key = self.request.POST.get('del_ssh_key') | ||||
if del_ssh_key: | ||||
ssh_key = UserSshKeys.get_or_404(del_ssh_key) | ||||
ssh_key_data = ssh_key.get_api_data() | ||||
SshKeyModel().delete(del_ssh_key, c.user.user_id) | ||||
audit_logger.store_web( | ||||
'user.edit.ssh_key.delete', action_data={ | ||||
'data': {'ssh_key': ssh_key_data, 'user': user_data}}, | ||||
user=self._rhodecode_user,) | ||||
Session().commit() | ||||
# Trigger an event on change of keys. | ||||
trigger(SshKeyFileChangeEvent(), self.request.registry) | ||||
h.flash(_("Ssh key successfully deleted"), category='success') | ||||
return HTTPFound(h.route_path('my_account_ssh_keys')) | ||||