improve host edit

pull/342/head
vapao 2021-06-28 00:26:07 +08:00
parent bf45935b17
commit f8bfc127f1
1 changed files with 14 additions and 18 deletions

View File

@ -4,7 +4,7 @@
from django.views.generic import View from django.views.generic import View
from django.db.models import F from django.db.models import F
from django.http.response import HttpResponseBadRequest from django.http.response import HttpResponseBadRequest
from libs import json_response, JsonParser, Argument from libs import json_response, JsonParser, Argument, AttrDict
from apps.setting.utils import AppSetting from apps.setting.utils import AppSetting
from apps.account.utils import get_host_perms from apps.account.utils import get_host_perms
from apps.host.models import Host, Group from apps.host.models import Host, Group
@ -13,7 +13,6 @@ from apps.schedule.models import Task
from apps.monitor.models import Detection from apps.monitor.models import Detection
from libs.ssh import SSH, AuthenticationException from libs.ssh import SSH, AuthenticationException
from paramiko.ssh_exception import BadAuthenticationType from paramiko.ssh_exception import BadAuthenticationType
from libs import AttrDict
from openpyxl import load_workbook from openpyxl import load_workbook
@ -40,8 +39,7 @@ class HostView(View):
Argument('password', required=False), Argument('password', required=False),
).parse(request.body) ).parse(request.body)
if error is None: if error is None:
if valid_ssh(form.hostname, form.port, form.username, password=form.pop('password'), if not valid_ssh(form.hostname, form.port, form.username, password=form.pop('password'), pkey=form.pkey):
pkey=form.pkey) is False:
return json_response('auth fail') return json_response('auth fail')
group_ids = form.pop('group_ids') group_ids = form.pop('group_ids')
@ -125,25 +123,23 @@ def post_import(request):
return json_response(summary) return json_response(summary)
def valid_ssh(hostname, port, username, password=None, pkey=None, with_expect=True): def valid_ssh(hostname, port, username, password=None, pkey=None):
private_key, public_key = AppSetting.get_ssh_key() private_key, public_key = AppSetting.get_ssh_key()
if password: try:
_cli = SSH(hostname, port, username, password=str(password))
_cli.add_public_key(public_key)
if pkey: if pkey:
private_key = pkey private_key = pkey
try: elif password:
cli = SSH(hostname, port, username, private_key) ssh = SSH(hostname, port, username, password=str(password))
cli.ping() ssh.add_public_key(public_key)
except BadAuthenticationType:
if with_expect: ssh = SSH(hostname, port, username, private_key)
raise TypeError('该主机不支持密钥认证请参考官方文档错误代码E01') ssh.ping()
return False
except AuthenticationException:
if password and with_expect:
raise ValueError('密钥认证失败请参考官方文档错误代码E02')
return False
return True return True
except BadAuthenticationType:
raise TypeError('该主机不支持密钥认证请参考官方文档错误代码E01')
except AuthenticationException:
if password:
raise ValueError('密钥认证失败请参考官方文档错误代码E02')
def post_parse(request): def post_parse(request):