def add_ssh_public_key(username): """ Adds a new SSH public key for the given user account. Accepts mimetype:`text/plain` request bodies containing the SSH public key in the conventional OpenSSH format: <keytype> <key> <ident>. :param username: The user's username. """ user = _get_user(username) if not user.can_edit(identity.current.user): raise Forbidden403('Cannot edit user %s' % user) if request.mimetype != 'text/plain': raise UnsupportedMediaType415( 'Request content type must be text/plain') with convert_internal_errors(): keytext = request.data.strip() if '\n' in keytext: raise ValueError('SSH public keys may not contain newlines') elements = keytext.split(None, 2) if len(elements) != 3: raise ValueError('Invalid SSH public key') key = SSHPubKey(*elements) user.sshpubkeys.append(key) session.flush() # to populate id return jsonify(key.__json__())
def add_ssh_public_key(username): """ Adds a new SSH public key for the given user account. Accepts mimetype:`text/plain` request bodies containing the SSH public key in the conventional OpenSSH format: <keytype> <key> <ident>. :param username: The user's username. """ user = _get_user(username) if not user.can_edit(identity.current.user): raise Forbidden403('Cannot edit user %s' % user) if request.mimetype != 'text/plain': raise UnsupportedMediaType415('Request content type must be text/plain') with convert_internal_errors(): keytext = request.data.strip() if '\n' in keytext: raise ValueError('SSH public keys may not contain newlines') elements = keytext.split(None, 2) if len(elements) != 3: raise ValueError('Invalid SSH public key') key = SSHPubKey(*elements) user.sshpubkeys.append(key) session.flush() # to populate id return jsonify(key.__json__())
def test_duplicate_ssh_key_not_accepted(self): sshkey = (u'ssh-rsa', u'uniquekey', u'*****@*****.**') with session.begin(): self.user.sshpubkeys.append(SSHPubKey(*sshkey)) key = 'ssh-rsa %s [email protected]' % sshkey[1] pane = self.go_to_prefs_tab('SSH Public Keys') pane.find_element_by_name('key').send_keys(key) pane.find_element_by_tag_name('form').submit() self.assertIn('Duplicate SSH public key', pane.find_element_by_class_name('alert-error').text)
def test_delete_ssh_public_key(self): with session.begin(): user = data_setup.create_user() user.sshpubkeys.append(SSHPubKey(keytype=u'ssh-rsa', pubkey=u'abc', ident=u'*****@*****.**')) s = requests.Session() requests_login(s) response = s.delete(get_server_base() + 'users/%s/ssh-public-keys/%s' % (user.user_name, user.sshpubkeys[0].id)) self.assertEqual(response.status_code, 204) with session.begin(): session.expire_all() self.assertEqual(len(user.sshpubkeys), 0)
def ssh_key_remove(self, *args, **kw): user = identity.current.user keyid = kw.get('id', None) try: key = SSHPubKey.by_id(keyid) except InvalidRequestError: flash(_(u"SSH key not found")) redirect('.') if user != key.user: flash(_(u"May not remove another user's keys")) redirect('.') session.delete(key) flash(_(u"SSH public key removed")) redirect('.')
def test_provision_with_ssh_key(self): with session.begin(): user = data_setup.create_user(password=u'testing') user.sshpubkeys.append( SSHPubKey(u'ssh-rsa', u'AAAAvalidkeyyeah', u'user@host')) system = data_setup.create_system(status=SystemStatus.manual, lab_controller=self.lc) system.reserve_manually(service=u'testdata', user=user) b = self.browser login(b, user=user.user_name, password='******') provision = self.go_to_provision_tab(system) self.select_distro_tree(self.distro_tree) provision.find_element_by_xpath( './/button[text()="Provision"]').click() b.find_element_by_xpath( './/div[contains(@class, "modal")]//button[text()="OK"]').click() b.find_element_by_xpath('//div[contains(@class, "alert-success")]' '/h4[text()="Provisioning successful"]') with session.begin(): kickstart = RenderedKickstart.query.order_by( RenderedKickstart.id.desc()).first() self.assertIn('ssh-rsa AAAAvalidkeyyeah user@host', kickstart.kickstart)
def ssh_key_add(self, ssh_pub_key=None): user = identity.current.user k = SSHPubKey(*ssh_pub_key) user.sshpubkeys.append(k) flash(_(u"SSH public key added")) redirect('.')