Example #1
0
    def get(self, group_id=None):
        if not group_id:
            root = context.application.data_manager.root
            return application.make_ok_response(data=root)

        # 返回数据组中数据列表
        _group = context.application.data_manager.id_map.get(group_id)
        if not _group:
            return context.make_fail_response('Not found group')

        return application.make_ok_response(data=_group)
Example #2
0
def template():
    if request.method == 'GET':
        '''
        Get all template list, all draft list, last selected template and last selected draft
        '''
        draft_version = cache.check_draft_version()
        if draft_version < cache.DRAFT_VERSION_V_1_8_0:
            logger.info('Updating draft into v1.8.0 from v1.7.0')
            cache.update_selected_template()
            cache.update_all_draft_file()

        templates = template_loader.template_list()
        last_selected_template = cache.get_selected_template()
        selected_template_index = None
        for index, template in enumerate(templates):
            if template['path'] == last_selected_template:
                selected_template_index = index
                break
        if not selected_template_index:
            return application.make_ok_response(templates=templates)

        template_key = cache.get_filename(last_selected_template)
        drafts = cache.get_draft_list(template_key)
        last_selected_cache = cache.get_selected_cache()

        return application.make_ok_response(
            selected_template_index=selected_template_index,
            templates=templates,
            selected_cache=last_selected_cache,
            drafts=drafts)

    elif request.method == 'POST':
        '''
        Get template detail by path
        And save lastest_selected_template
        '''
        if not request.json or not request.json.get('path'):
            return application.make_fail_response(
                'Missing required argument: path')

        template_path = request.json['path']
        draft_name = request.json.get('cache_name', '')
        draft_filename = cache.get_filename(template_path, draft_name)
        template_detail = cache.get(draft_filename)

        template = template_loader.get_template(template_path)
        if inspect.getargspec(template.form).args:
            template_detail = template.form({'cache': template_detail})
        else:
            template_detail = template.form()

        cache.selected_template(template_path, draft_name)
        return jsonify(template_detail)
Example #3
0
    def put(self):
        selected_filter_name = request.json.get('name')
        if not selected_filter_name:
            context.application.selected_filter = None
            return application.make_ok_response()

        for f in context.application.filters:
            if f['name'] == selected_filter_name:
                context.application.selected_filter = f
                return application.make_ok_response()

        return application.make_fail_response(f'Flow filter {selected_filter_name} not found!')
Example #4
0
def ui_cache(key):
    if request.method == 'GET':
        data = cache.get(key)
        if data:
            return application.make_ok_response(data=data)
        else:
            return application.make_fail_response(f'Not found cache by key {key}')
    elif request.method == 'POST':
        data = request.json
        for field in data:
            if 'extraMsg' in field:
                del field['extraMsg']
        cache.put(key, data)
        return application.make_ok_response()
Example #5
0
    def get(self, checker_id=None):
        if not checker_id:
            checkers = application.checkers
            script_info_list = [checkers[checker_name].json() for checker_name in checkers]
            return application.make_ok_response(data=script_info_list)

        _checker = application.checkers.get(checker_id)
        if not _checker:
            return context.make_fail_response(f'Checker {checker_id} not found')

        checker = application.checkers[checker_id]
        with codecs.open(checker.path, 'r', 'utf-8') as f:
            content = f.read()
            return application.make_ok_response(data=content)
Example #6
0
    def get(self, checker_id=None):
        if not checker_id:
            checkers = application.checkers
            script_info_list = [
                checkers[checker_name].json() for checker_name in checkers
            ]
            script_info_list.sort(key=lambda x: x['name'])
            return application.make_ok_response(data=script_info_list)

        _checker = application.checkers.get(checker_id)
        if not _checker:
            return application.make_fail_response(
                f'Checker {checker_id} not found')

        content = _checker.read()
        return application.make_ok_response(data=content)
Example #7
0
 def get(self, id):
     for item in context.application.cache.items():
         if item['id'] == id:
             display_item = {}
             encoders_decoders.decoder_handler(item, res=display_item)
             return application.make_ok_response(data=display_item)
     return abort(400, 'Request not found')
Example #8
0
def mock():
    uri = request.json["uri"]
    ip =  config.get('ip')
    port = config.get('mock.port')

    requests.get(url=f"http://{ip}:{port}/mock/{uri}")
    return application.make_ok_response()
Example #9
0
    def get(self, channel=None, page=0, event_id=None):
        db = application.server['db']
        channel_rules = []
        if channel:
            channel_rules = channel.split('+')
        if event_id:
            page = db.get_page_index_by_event_id(event_id,
                                                 channel_rules,
                                                 limit=PAGE_SIZE)
        events = db.get_event(channel_rules,
                              offset=page * PAGE_SIZE,
                              limit=PAGE_SIZE)
        page_count = db.get_page_count(channel_rules, page_size=PAGE_SIZE)
        result = []
        for event in events:
            event_str = event.json()

            # Import decoder for decoding the requested content
            if event_str.get('channel') == 'flow':
                content = json.loads(event_str['content'])
                application.encoders_decoders.decoder_handler(content['flow'])
                event_str['content'] = json.dumps(content, ensure_ascii=False)

            result.append(event_str)
        return application.make_ok_response(events=result,
                                            page=page,
                                            page_count=page_count,
                                            page_size=PAGE_SIZE)
Example #10
0
 def get(self, _id):
     data = context.application.data_manager.get(_id)
     display_item = {}
     # Import decoder for decoding the requested content
     application.encoders_decoders.decoder_handler(data,
                                                   output=display_item)
     return application.make_ok_response(data=display_item)
Example #11
0
 def put(self, mode=None):
     if context.Mode.contains(mode):
         context.application.work_mode = mode
         return application.make_ok_response()
     else:
         return application.make_fail_response(
             f'Unknown record mode: {mode}')
Example #12
0
 def post(self):
     parent_node = request.json.get("parentNode")
     snapshot_name = request.json.get('snapshotName', '')
     if "id" not in parent_node:
         return application.make_fail_response(msg="object has no attribute : parentNode.id")
     context.application.data_manager.import_snapshot(parent_node["id"], snapshot_name)
     return application.make_ok_response()
Example #13
0
    def post(self):
        if request.json:
            parent_id = request.json.get('parentId')
            name = request.json.get('snapshotName', '')
            if not parent_id:
                return application.make_fail_response('parent_id is required!')

            context.application.data_manager.import_snapshot(parent_id, name)

        elif request.files:
            parent_id = request.form.get('parent_id') if request.form else ''
            if not parent_id:
                return application.make_fail_response('parent_id is required!')

            stream = request.files['file']
            if not stream:
                return application.make_fail_response('file is required!')

            filename = stream.filename or str(uuid.uuid4())
            path = application._cm.ROOT / 'snapshot' / filename
            if path.suffix != '.lb':
                return application.make_fail_response(
                    f'Unknown file type `.{path.suffix}`, `.lb` is required!')

            stream.save(str(path))
            application.snapshot_import_uri = f'file://{str(path)}'
            name = path.stem

            context.application.data_manager.import_snapshot(parent_id,
                                                             name,
                                                             path=path)

        return application.make_ok_response()
Example #14
0
def template():
    if request.method == 'GET':
        templates = template_loader.template_list()
        selected_index = None
        last_selected_template = cache.get_selected_template()
        for index, template in enumerate(templates):
            if template['path'] == last_selected_template:
                selected_index = index
        return application.make_ok_response(selected_index=selected_index,
                                            templates=templates)
    elif request.method == 'POST':
        '''
        Get template detail by path
        And save lastest_selected_template
        '''
        if request.json and 'path' in request.json:
            template_path = request.json['path']
            template = template_loader.get_template(template_path)
            # load from cache
            template_key = md5(template_path.encode()).hexdigest()
            template_detail = cache.get(template_key)
            # load from template
            if inspect.getargspec(template.form).args:
                template_detail = template.form({'cache': template_detail})
            else:
                template_detail = template.form()
            cache.selected_template(template_path)
            return jsonify(template_detail)
        else:
            return application.make_fail_response(
                'Need path argument for getting template detail')
Example #15
0
    def get(self, search_str=None):
        _matched_group = []

        def _add_group(_group):
            _matched_group.append({
                'id': _group['id'],
                'name': _group['name'],
                'type': _group['type'],
                'parent_id': _group['parent_id'],
                'abs_parent_path': _get_abs_parent_path(_group),
                'abs_parent_obj': _get_abs_parent_obj(_group)
            })

        def _get_abs_parent_path(_group):
            _abs_parent_path = context.application.data_manager._get_abs_parent_path(_group)
            return _abs_parent_path

        def _get_abs_parent_obj(_group):
            _abs_parent_obj = context.application.data_manager._get_abs_parent_obj(_group)
            return _abs_parent_obj

        if search_str:
            if context.application.data_manager.id_map.get(search_str):
                # search_str is group id
                group = context.application.data_manager.id_map.get(search_str)
                _add_group(group)
            else:
                for _id, group in context.application.data_manager.id_map.items():
                    if group.get('type') == 'group' and search_str.lower() in group.get('name', '').lower():
                        _add_group(group)
        else:
            for _id, group in context.application.data_manager.id_map.items():
                if group.get('type') == 'group' and group.get('name'):
                    _add_group(group)
        return application.make_ok_response(data=_matched_group)
Example #16
0
def ui_cache(template_key):
    if request.method == 'GET':
        data = cache.get_draft_list(template_key)
        return application.make_ok_response(data=data)

    elif request.method == 'POST':
        template_path = request.json.get('template_path')
        if not template_path:
            return application.make_fail_response(
                'Missing required argument: template_path')

        cache_name = request.json.get('cache_name')
        if not cache_name:
            return application.make_fail_response(
                'Missing required argument: cache_name')

        template_detail = request.json.get('data')
        if not template_detail:
            return application.make_fail_response(
                'Missing required argument: data')

        for field in template_detail:
            if 'extraMsg' in field:
                del field['extraMsg']

        draft_name = cache.get_filename(template_path, cache_name)
        cache.put(draft_name, template_detail)
        cache.put(f'{draft_name}{cache.DRAFT_INFO_FILE_SUFFIX}',
                  {'cache_name': cache_name})
        cache.selected_template(template_path, cache_name)
        return application.make_ok_response()

    elif request.method == 'DELETE':
        template_path = request.json.get('template_path')
        if not template_path:
            return application.make_fail_response(
                'Missing required argument: template_path')

        cache_name = request.json.get('cache_name')
        if not cache_name:
            return application.make_fail_response(
                'Missing required argument: cache_name')

        draft_name = cache.get_filename(template_path, cache_name)
        cache.delete(draft_name)
        cache.delete(f'{draft_name}{cache.DRAFT_INFO_FILE_SUFFIX}')
        return application.make_ok_response()
Example #17
0
 def delete(self):
     _ids = request.json.get('ids')
     if _ids:
         context.application.cache.delete_by_ids(*_ids)
     else:
         context.application.cache.clear()
     context.application.socket_io.emit('action', 'delete flow log')
     return application.make_ok_response()
Example #18
0
 def get(self, id):
     for item in context.application.cache.items():
         if item['id'] == id:
             # Import decoder for decoding the requested content
             display_item = {}
             application.encoders_decoders.decoder_handler(item, output=display_item)
             return application.make_ok_response(data=display_item)
     return application.make_fail_response(f'Request {id} not found')
Example #19
0
    def patch(self):

        update_conf = request.get_json()
        if update_conf is None or not isinstance(update_conf, dict):
            return application.make_fail_response(
                'Request body must be a JSONObject!')

        if not update_conf:
            logger.warning(
                'This request cannot modify config, request body is empty.')
            return application.make_ok_response()

        try:
            application._cm.override_config_field(update_conf)
            return application.make_ok_response()
        except ConfigException as e:
            return application.make_fail_response(str(e))
Example #20
0
    def post(self):
        if request.files:
            stream = request.files['file']
            if not stream:
                return application.make_fail_response('Missing required file')

            parent_id = request.form.get('parent_id') if request.form else None
            if not parent_id:
                return application.make_fail_response(
                    'Missing required argument: parent_id')

            filename = stream.filename or str(uuid.uuid4())
            path = context.application.data_manager.snapshot_workspace / filename
            stream.save(str(path))

            group_id = context.application.data_manager.import_from_file(
                parent_id, path)
            return application.make_ok_response(group_id=group_id)

        elif request.json:
            parent_id = request.json.get('parentId')
            name = request.json.get('snapshotName', '')
            if not parent_id:
                return application.make_fail_response(
                    'Missing required argument: parent_id')

            snapshot_id = request.json.get('snapshotId')
            snapshot_info = context.application.data_manager.snapshot_import_cache.get(
                snapshot_id)
            if not snapshot_info:
                return application.make_fail_response(
                    f'Snapshot cache {snapshot_id} not found!')

            path = snapshot_info.get('path')
            if not Path(path).exists():
                link = snapshot_info.get('link')
                path = context.application.data_manager.read_snapshot_from_link(
                    link)
                context.application.data_manager.snapshot_import_cache[
                    snapshot_id]['path'] = path

            group_id = context.application.data_manager.import_from_file(
                parent_id, path, name=name)
            return application.make_ok_response(group_id=group_id)

        return application.make_fail_response('No import snapshot info found!')
Example #21
0
    def get(self, mode=None):
        if not mode:
            db = application.server['db']
            channel_list = db.get_channel_list()
            return jsonify([item[0] for item in channel_list])

        elif mode == 'default':
            channel = application.config.get('event.default_channel', [])
            return application.make_ok_response(data=channel)
Example #22
0
def take_screenshot(platform, device_id):
    """
    channel: {platform}.cmd
    """
    channel = platform + '.cmd'
    cmd = {}
    cmd['cmd'] = 'screenshot'
    cmd['device_id'] = [device_id]
    lyrebird.publish(channel.lower(), cmd)
    return application.make_ok_response(message=f'Take screenshot success!')
Example #23
0
 def put(self):
     template = request.json["templateName"]
     # check template valid
     if template in list(config.indexes_by_name.keys()):
         # reset bandwidth
         config.bandwidth = config.indexes_by_name[template]
         logger.debug(f'Modified bandwidth: {config.bandwidth}')
         return application.make_ok_response(bandwidth=config.bandwidth)
     else:
         return application.make_fail_response(msg=f'invalid template ! request data is {template}')
Example #24
0
    def get(self):
        info = context.application.data_manager.decompress_snapshot()
        tmp_snapshot_file_list = [
            info['snapshot_storage_path'],
            f'{info["snapshot_storage_path"]}.lb'
        ]
        context.application.data_manager.remove_tmp_snapshot_file(tmp_snapshot_file_list)

        detail = info['snapshot_detail']
        detail.pop('children')
        return application.make_ok_response(data=detail)
Example #25
0
 def _get_all_status_item(self):
     all_status_item = []
     plugin_server = application.server['plugin']
     for plugin_name in plugin_server.plugins:
         plugin = plugin_server.plugins[plugin_name]
         for status_item in plugin.status:
             all_status_item.append({
                 'id': status_item.id,
                 'text': status_item.get_text()
             })
     return application.make_ok_response(data=all_status_item)
Example #26
0
    def _get_menu_by_status_item_name(self, name):
        plugin_server = application.server['plugin']
        for plugin in plugin_server.plugins.values():
            for status_item in plugin.status:
                if status_item.name != name:
                    continue

                menu_list = status_item.get_detail()
                return application.make_ok_response(data=menu_list)

        return application.make_fail_response(
            f'Status item not found, id={name}')
Example #27
0
 def _get_menu_by_status_item_name(self, name):
     plugin_server = application.server['plugin']
     for plugin_name in plugin_server.plugins:
         plugin = plugin_server.plugins[plugin_name]
         for status_item in plugin.status:
             if status_item.name == name:
                 menu_list = [
                     menu_item.json()
                     for menu_item in status_item.get_menu()
                 ]
                 return application.make_ok_response(data=menu_list)
     return application.make_fail_response(
         f'Status item not found, id={name}')
Example #28
0
def issue():
    issue_req = request.json
    template_info = issue_req['template']
    template = template_loader.get_template(template_info['path'])

    issue_data = issue_req['issue']
    attachments = issue_req['attachments']
    snapshots = issue_req['snapshots']

    all_bugit_message = ''

    # Export Snapshot
    for snapshot in snapshots:
        attachments.append(attachment.export_snapshot(snapshot))
    # Set bugit script context
    context = {'issue': issue_data, 'attachments': attachments}
    # Set submit actions
    submit_action_functions = template.submit()
    # Do submit
    for action_function in submit_action_functions:
        try:
            bug_url = action_function(context)
            if bug_url:
                all_bugit_message += bug_url
        except Exception as e:
            if e.__class__.__name__ == 'BugitFormInputError':
                return application.make_fail_response(str(e))
            error_message = traceback.format_exc()
            trace = "<br/>".join(error_message.split("\n"))
            lyrebird.report({
                'action': 'bugit.submit',
                'bugit': {
                    'status': 'failure',
                    'data': issue_data,
                    'error': error_message
                }
            })
            return application.make_fail_response(
                f'Script function "{action_function.__name__}" crash.<br/>{trace}'
            )
    lyrebird.report({
        'action': 'bugit.submit',
        'bugit': {
            'status': 'success',
            'data': issue_data
        }
    })
    #Delete Snapshot
    attachment.remove_attach()
    return application.make_ok_response(
        message=f'Create issue success! {all_bugit_message}')
Example #29
0
    def put(self, checker_id):
        _checker = application.checkers.get(checker_id)
        if not _checker:
            return application.make_fail_response(f'Checker {checker_id} not found')

        if 'status' not in request.json:
            return application.make_fail_response(f'Checker {checker_id} status not found')

        _checker_status = request.json.get('status')
        if _checker_status:
            application.checkers[checker_id].activate()
        else:
            application.checkers[checker_id].deactivate()
        return application.make_ok_response()
Example #30
0
 def get(self, channel=None, page=0, event_id=None):
     db = application.server['db']
     channel_rules = []
     if channel:
         channel_rules = channel.split('+')
     if event_id:
         page = db.get_page_index_by_event_id(event_id, channel_rules, limit=PAGE_SIZE)
     events = db.get_event(channel_rules, offset=page*PAGE_SIZE, limit=PAGE_SIZE)
     page_count = db.get_page_count(channel_rules, page_size=PAGE_SIZE)
     result = []
     for event in events:
         event_str = event.json()
         result.append(event_str)
     return application.make_ok_response(events=result, page=page, page_count=page_count, page_size=PAGE_SIZE)