Пример #1
0
    def post(self, *args, **kwargs):
        args = self.get_argument('args', None)
        if args is not None:
            args = json.loads(args)
        else:
            return self.write_json(-1, '参数错误')

        if 'host_auth_id' not in args:
            return self.write_json(-1, '参数缺失')

        _host_auth_id = int(args['host_auth_id'])

        user = self.get_current_user()

        # host_auth_id 对应的是 ts_auth_info 表中的某个条目,含有具体的认证数据,因为管理员无需授权即可访问所有远程主机,因此
        # 直接给出 host_auth_id,且account直接指明是当前登录用户(其必然是管理员)

        tmp_auth_info = host.get_host_auth_info(_host_auth_id)
        if tmp_auth_info is None:
            return self.write_json(-2, '指定数据不存在')

        tmp_auth_info['account_lock'] = 0
        tmp_auth_info['account_name'] = user['name']

        with tmp_auth_id_lock:
            global tmp_auth_id_base
            tmp_auth_id_base -= 1
            auth_id = tmp_auth_id_base

        # 将这个临时认证信息放到session中备后续查找使用(10秒内有效)
        web_session().set('tmp-auth-info-{}'.format(auth_id), tmp_auth_info,
                          10)

        req = {'method': 'request_session', 'param': {'authid': auth_id}}
        _yr = async_post_http(req)
        return_data = yield _yr
        if return_data is None:
            return self.write_json(-3, '调用核心服务获取会话ID失败')

        if 'code' not in return_data:
            return self.write_json(-4, '核心服务获取会话ID时返回错误数据')

        _code = return_data['code']
        if _code != 0:
            return self.write_json(-5, '核心服务获取会话ID时返回错误 {}'.format(_code))

        try:
            session_id = return_data['data']['sid']
        except IndexError:
            return self.write_json(-5, '核心服务获取会话ID时返回错误数据')

        data = dict()
        data['session_id'] = session_id

        return self.write_json(0, data=data)
Пример #2
0
    def _get_auth_info(self, param):
        # 如果是页面上进行连接测试(增加或修改主机和用户时),信息并不写入数据库,而是在内存中存在,传递给core服务的
        # 应该是负数形式的authid。本接口支持区分这两种认证ID。

        if 'authid' not in param:
            return self.write_json(-1, message='invalid request.')

        authid = param['authid']
        if authid > 0:
            # 根据authid从数据库中查询对应的数据,然后返回给调用者
            x = host.get_auth_info(param['authid'])
            return self.write_json(0, data=x)
        elif authid < 0:
            x = web_session().taken('tmp-auth-info-{}'.format(authid), None)
            return self.write_json(0, data=x)
        else:
            return self.write_json(-1, message='invalid auth id.')
Пример #3
0
    def post(self, *args, **kwargs):
        args = self.get_argument('args', None)
        if args is not None:
            args = json.loads(args)
        else:
            return self.write_json(-1, '参数错误')

        user = self.get_current_user()

        tmp_auth_info = dict()

        try:
            _host_auth_id = int(args['host_auth_id'])
            _user_pswd = args['user_pswd']
            _cert_id = int(args['cert_id'])

            tmp_auth_info['host_ip'] = args['host_ip']
            tmp_auth_info['host_port'] = int(args['host_port'])
            tmp_auth_info['sys_type'] = int(args['sys_type'])
            tmp_auth_info['protocol'] = int(args['protocol'])
            tmp_auth_info['user_name'] = args['user_name']
            tmp_auth_info['auth_mode'] = int(args['auth_mode'])
            tmp_auth_info['user_param'] = args['user_param']
            tmp_auth_info['encrypt'] = 1
            tmp_auth_info['account_lock'] = 0
            tmp_auth_info['account_name'] = user['name']
        except IndexError:
            return self.write_json(-2, '参数缺失')

        if tmp_auth_info['auth_mode'] == 1:
            if len(_user_pswd) == 0:  # 修改登录用户信息时可能不会修改密码,因此页面上可能不会传来密码,需要从数据库中直接读取
                h = host.get_host_auth_info(_host_auth_id)
                tmp_auth_info['user_auth'] = h['user_auth']
            else:  # 如果页面上修改了密码或者新建账号时设定了密码,那么需要先交给core服务进行加密
                req = {'method': 'enc', 'param': {'p': _user_pswd}}
                _yr = async_post_http(req)
                return_data = yield _yr
                if return_data is None:
                    return self.write_json(-3, '调用核心服务加密失败')
                if 'code' not in return_data or return_data['code'] != 0:
                    return self.write_json(-3, '核心服务加密返回错误')

                tmp_auth_info['user_auth'] = return_data['data']['c']

        elif tmp_auth_info['auth_mode'] == 2:
            tmp_auth_info['user_auth'] = host.get_cert_info(_cert_id)
            if tmp_auth_info['user_auth'] is None:
                self.write_json(-100, '指定私钥不存在')
                return
        elif tmp_auth_info['auth_mode'] == 0:
            tmp_auth_info['user_auth'] = ''
        else:
            self.write_json(-101, '认证类型未知')
            return

        with tmp_auth_id_lock:
            global tmp_auth_id_base
            tmp_auth_id_base -= 1
            auth_id = tmp_auth_id_base

        web_session().set('tmp-auth-info-{}'.format(auth_id), tmp_auth_info, 10)

        req = {'method': 'request_session', 'param': {'authid': auth_id}}
        _yr = async_post_http(req)
        return_data = yield _yr
        if return_data is None:
            return self.write_json(-3, '调用核心服务获取会话ID失败')

        if 'code' not in return_data:
            return self.write_json(-4, '核心服务获取会话ID时返回错误数据')

        _code = return_data['code']
        if _code != 0:
            return self.write_json(-5, '核心服务获取会话ID时返回错误 {}'.format(_code))

        try:
            session_id = return_data['data']['sid']
        except IndexError:
            return self.write_json(-5, '核心服务获取会话ID时返回错误数据')

        data = dict()
        data['session_id'] = session_id

        return self.write_json(0, data=data)
Пример #4
0
 def get_session(self, name, _default=None):
     k = '{}-{}'.format(self._s_id, name)
     return web_session().get(k, _default)
Пример #5
0
 def set_session(self, name, value, expire=SESSION_EXPIRE):
     k = '{}-{}'.format(self._s_id, name)
     web_session().set(k, value, expire)
Пример #6
0
 def del_session(self, name):
     k = '{}-{}'.format(self._s_id, name)
     return web_session().set(k, '', -1)