Exemplo n.º 1
0
async def to_minimal_dashboard_description(json_object,
                                           request_context=None,
                                           async_splunk_client=None):
    """
    Parse a dashboard json object and return a DashboardDescription
    :param json_object: [dict] representing json object
    :param request_context:
    :param async_splunk_client:
    :return:
    """
    dashboard_id = helper.shorten_dashboard_id_from_url(
        get_string(json_object.get('id')))
    title = get_string(json_object.get('content').get('label'))
    app_name = get_string(json_object.get('acl').get('app'))

    # Populate display_app_name
    display_app_name = ""
    if async_splunk_client is not None:
        display_app_name = await fetch_display_app_name(
            request_context=request_context,
            app_name=app_name,
            async_splunk_client=async_splunk_client)

    dashboard_description = DashboardDescription(
        dashboard_id=dashboard_id,
        title=title,
        app_name=app_name,
        display_app_name=display_app_name)
    return dashboard_description
Exemplo n.º 2
0
def post_dashboard_endpoint(authtoken, uri, contents):
    """
    Make a POST request to the Splunk dashboards REST API, creating or updating
    the specified dashboard and returning the integrity hash of the xml
    """

    r, dashboard_res = rest.simpleRequest(uri,
                                          sessionKey=authtoken,
                                          method='POST',
                                          getargs={'output_mode': 'json'},
                                          postargs=contents,
                                          raiseAllErrors=True)
    dashboard_res = json.loads(dashboard_res)
    integrity_hash = dashboard_res['entry'][0]['content']['eai:digest']
    # Return short form of the dashboard_id after creating (i.e. admin/splunk_app_cloudgateway/dashboard_name)
    dashboard_id = shorten_dashboard_id_from_url(
        dashboard_res['entry'][0]['id'])
    return integrity_hash, dashboard_id
Exemplo n.º 3
0
    def get(self, request):
        """
        Handler which generates QR codes using a deep link generated from a dashboard ID, and
        returns it to the client as a binary body of the specified type. This function:
            1. Extracts request data
            2. Validates the requested file type
            3. Validates and transforms the dashboard ID into a deep link
            4. Generates and returns the QR code
        """
        # Extracts request data
        user = request['session']['user']
        dashboard_id = extract_parameter(request['query'], DASHBOARD_ID_LABEL,
                                         'query')
        file_type = request['path_info'].split('.')[-1]
        id_type = 'a' if 'isAr' in request['query'] else 's'

        # Validates the requested file type
        if file_type not in get_valid_file_types(with_names=False):
            raise Errors.SpacebridgeRestError(
                'QR code was requested with invalid file_type=%s' % file_type)

        # Validates and transforms the dashboard ID into a deep link
        deep_link = shorten_dashboard_id_from_url(dashboard_id)
        if deep_link == dashboard_id:  # Nothing was shortened so it must be an invalid dashboard_id
            raise Errors.SpacebridgeRestError(
                'QR code was requested for invalid dashboard_id=%s' %
                dashboard_id)
        deep_link = 'https://spl.mobi/%s%s/%s' % (
            id_type, QR_CODE_DASHBOARD_VERSION, deep_link)

        LOGGER.info('Generated QR code of deep_link=%s for user=%s' %
                    (deep_link, user))

        # Generates and returns the QR code
        return {
            'binary': generate_qr_code(deep_link, file_type),
            'status': 200,
        }
Exemplo n.º 4
0
async def to_dashboard_description(json_object,
                                   is_ar=False,
                                   request_context=None,
                                   async_splunk_client=None,
                                   show_refresh=True):
    """
    Parse a dashboard json object and return a DashboardDescription
    :param json_object: [dict] representing json object
    :param is_ar: if true, only return dashboard description if the dashboard is AR compatible
    :param request_context:
    :param async_splunk_client:
    :param show_refresh: show refresh params, default True
    :return:
    """

    if json_object is not None and isinstance(json_object, dict):
        dashboard_id = helper.shorten_dashboard_id_from_url(
            get_string(json_object.get('id')))
        name = get_string(json_object.get('name'))
        content = json_object.get('content')
        acl = json_object.get('acl')

        if acl is not None:
            app_name = get_string(acl.get('app'))

        if content is not None:
            title = get_string(content.get('label'))
            description = get_string(content.get('description'))
            dashboard_type = get_string(content.get('eai:type'))

            # TODO: Client not using currently, figure out how to fill out
            uses_custom_css = False
            uses_custom_javascript = False
            uses_custom_visualization = False
            uses_custom_html = dashboard_type == 'html'

            # Pull out dashboard xml data and parse to get DashboardDefinition
            dashboard_xml_data = content.get('eai:data')

            # If the dashboard version > 1, it's a UDF dashboard
            if 'version' in content and parse_version(str(content.get('version'))) > parse_version("1") \
                and dashboard_xml_data:

                root = get_root_element(dashboard_xml_data)
                jsn = json.loads(
                    parse_helpers.get_text(root.find('definition')))
                definition = UdfDashboardDescription.from_json(jsn)
                definition.dashboard_id = dashboard_id

            elif dashboard_xml_data and dashboard_type == 'views':

                root_element = get_root_element(dashboard_xml_data)
                definition = await to_dashboard_definition(
                    request_context=request_context,
                    app_name=app_name,
                    root=root_element,
                    dashboard_id=dashboard_id,
                    show_refresh=show_refresh,
                    async_splunk_client=async_splunk_client)
                if is_ar and (not definition.ar_compatible
                              or is_legacy_ar_dashboard(name, app_name)):
                    return None
            else:
                definition = DashboardDefinition(dashboard_id=dashboard_id,
                                                 title=title,
                                                 description=description)

            # Populate display_app_name
            display_app_name = ""
            if async_splunk_client is not None:
                display_app_name = await fetch_display_app_name(
                    request_context=request_context,
                    app_name=app_name,
                    async_splunk_client=async_splunk_client)

            input_tokens = definition.input_tokens if hasattr(
                definition, 'input_tokens') else {}
            meta = definition.meta if hasattr(definition, 'meta') else None

            submit_button = definition.submit_button if hasattr(
                definition, 'submit_button') else False
            auto_run = definition.auto_run if hasattr(definition,
                                                      'auto_run') else False

            dashboard_description = DashboardDescription(
                dashboard_id=dashboard_id,
                title=title,
                description=description,
                app_name=app_name,
                display_app_name=display_app_name,
                uses_custom_css=uses_custom_css,
                uses_custom_javascript=uses_custom_javascript,
                uses_custom_visualization=uses_custom_visualization,
                uses_custom_html=uses_custom_html,
                input_tokens=input_tokens,
                meta=meta,
                definition=definition,
                submit_button=submit_button,
                auto_run=auto_run)
            return dashboard_description

    # Return empty proto in default case
    return DashboardDescription()