##// END OF EJS Templates
tests: fixed some tests because of wrong compare strings
tests: fixed some tests because of wrong compare strings

File last commit:

r5093:525812a8 default
r5097:ff28a588 default
Show More
my_account_ssh_keys.py
144 lines | 5.5 KiB | text/x-python | PythonLexer
copyrights: updated for 2023
r5088 # Copyright (C) 2016-2023 RhodeCode GmbH
ssh: added ssh key management into my account.
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
from rhodecode.apps.ssh_support import SshKeyFileChangeEvent
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()
SSH: disable visually support ssh keys if we have them disabled in the .ini
r2045 c.ssh_enabled = self.request.registry.settings.get(
'ssh.generate_authorized_keyfile')
ssh: added ssh key management into my account.
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'
Issue #5536 - ability to disable server-side SSH key generation...
r3478 if c.ssh_key_generator_enabled:
ssh-keys: allow generation of legacy keys for older systems and windows.
r4238 private_format = self.request.GET.get('private_format') \
or SshKeyModel.DEFAULT_PRIVATE_KEY_FORMAT
Issue #5536 - ability to disable server-side SSH key generation...
r3478 comment = 'RhodeCode-SSH {}'.format(c.user.email or '')
ssh-keys: allow generation of legacy keys for older systems and windows.
r4238 c.private, c.public = SshKeyModel().generate_keypair(
comment=comment, private_format=private_format)
Issue #5536 - ability to disable server-side SSH key generation...
r3478 c.target_form_url = h.route_path(
'my_account_ssh_keys', _query=dict(default_key=c.public))
ssh: added ssh key management into my account.
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')
ssh: show fingerprint when adding already existing key....
r2594 fingerprint = 'unknown'
ssh: added ssh key management into my account.
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(
ssh-keys: further sanitize data of inputted SSH keys.
r2751 c.user.user_id, fingerprint, key.keydata, description)
ssh: added ssh key management into my account.
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")
ssh: show fingerprint when adding already existing key....
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),
ssh: added ssh key management into my account.
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'))