<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Help with the Splunk Add-on Builder - creating a custom Python app in Splunk Dev</title>
    <link>https://community.splunk.com/t5/Splunk-Dev/Help-with-the-Splunk-Add-on-Builder-creating-a-custom-Python-app/m-p/704548#M11743</link>
    <description>&lt;P&gt;I can't see any obvious issue with your code. What happens if you include debug log statements that output the report_id value, and then the resulting URL?&lt;/P&gt;&lt;P&gt;Assuming logging mode is set to debug:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;    helper.log_debug(f"Report ID is: {report_id}")

    url = f"https://example_url/{report_id}/download"
    
    helper.log_debug(f"URL is: {url}")
    
    headers = {
        "accept": "application/json",
        "Authorization": f"Bearer {jwt_token}",
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 16 Nov 2024 06:54:09 GMT</pubDate>
    <dc:creator>marnall</dc:creator>
    <dc:date>2024-11-16T06:54:09Z</dc:date>
    <item>
      <title>Help with the Splunk Add-on Builder - creating a custom Python app</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Help-with-the-Splunk-Add-on-Builder-creating-a-custom-Python-app/m-p/704513#M11742</link>
      <description>&lt;P&gt;Hi Everyone,&lt;BR /&gt;&lt;BR /&gt;The issue with the code below appears to be with the values of the {report_id} variable not being passed correctly to the&amp;nbsp;download_report function, in particular this line:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;url = f"https://example_url/{report_id}/download"&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If I hardcode the url with a valid token, instead of the {report_id} variable,&amp;nbsp; the report gets downloaded, as expected.&lt;/P&gt;&lt;P&gt;Any help would be much appreciated !&lt;/P&gt;&lt;P&gt;Full code below:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import requests
 
def collect_events(helper, ew):
    """
    Main function to authenticate, generate report ID, and download the report.
    """
    username = helper.get_arg('username')
    password = helper.get_arg('password')
    auth_url = "https://example_url/auth"
    headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
    }
    data = {
        'username': username,
        'password': password,
        'token': 'true',
        'permissions': 'true',
    }

    try:
        # Step 1: Authenticate to get the JWT token
        auth_response = requests.post(auth_url, headers=headers, data=data)
        if auth_response.status_code == 201:
            jwt_token = auth_response.text.strip()  # Extract and clean the token
            if jwt_token:
                # Log and create an event for the JWT token
                event = helper.new_event(
                    data=f"JWT Token: {jwt_token}"
                )
                ew.write_event(event)

                # Step 2: Generate the report ID
                report_id = generate_report_id(jwt_token, helper)
                if report_id:
                    # Log and create an event for the report ID
                    event = helper.new_event(
                        data=f"Report ID: {report_id}"
                    )
                    ew.write_event(event)

                    # Step 3: Download the report
                    file_path = download_report(jwt_token, report_id, helper)
                    if file_path:
                        helper.log_info(f"Report successfully downloaded to: {file_path}")
                    else:
                        raise ValueError("Failed to download the report.")
                else:
                    raise ValueError("Failed to generate report ID.")
            else:
                raise ValueError("JWT token not found in response.")
        else:
            raise ValueError(f"Failed to get JWT: {auth_response.status_code}, {auth_response.text}")
    except Exception as e:
        helper.log_error(f"Error in collect_events: {e}")

 
def generate_report_id(jwt_token, helper):
    url = "https://example_url"
    headers = {
        "accept": "application/json",
        "Authorization": f"Bearer {jwt_token}"
    }
    params = {
        "havingQuery": "isSecurity: true",
        "platform": "Windows"
    }
 
    try:
        response = requests.get(url, headers=headers, params=params)
        if response.status_code in (200, 201):
            report_data = response.json()
            report_id = report_data.get('reportId')
            if report_id:
                return report_id
            else:
                raise ValueError("Report ID not found in response.")
        else:
            raise ValueError(f"Failed to generate report ID: {response.status_code}, {response.text}")
    except Exception as e:
        helper.log_error(f"Error while generating report ID: {e}")
        raise ValueError(f"Error while generating report ID: {e}")

def download_report(jwt_token, report_id, helper):
    """
    Downloads the report using the JWT token and report ID.
    """

    url = f"https://example_url/{report_id}/download"
    headers = {
        "accept": "application/json",
        "Authorization": f"Bearer {jwt_token}",
    }

    try:
        # Make the request to download the report
        response = helper.send_http_request(url, method="GET", headers=headers, verify=True)
        
        if response.status_code in (200, 201):
            # Save the report content to a file
            sanitized_report_id = "".join(c if c.isalnum() else "_" for c in report_id)
            file_path = f"C:\\Program Files\\Splunk\\etc\\apps\\splunk_app_addon-builder\\local\\temp\\{sanitized_report_id}.csv.gz"
            
            with open(file_path, "wb") as file:
                file.write(response.content)
            
            helper.log_info(f"Report downloaded successfully to: {file_path}")
            return file_path
        else:
            raise ValueError(f"Failed to download report: {response.status_code}, {response.text}")
    except Exception as e:
        helper.log_error(f"Error while downloading report: {e}")
        raise ValueError(f"Error while downloading report: {e}")&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 15 Nov 2024 14:38:38 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Help-with-the-Splunk-Add-on-Builder-creating-a-custom-Python-app/m-p/704513#M11742</guid>
      <dc:creator>tomapatan</dc:creator>
      <dc:date>2024-11-15T14:38:38Z</dc:date>
    </item>
    <item>
      <title>Re: Help with the Splunk Add-on Builder - creating a custom Python app</title>
      <link>https://community.splunk.com/t5/Splunk-Dev/Help-with-the-Splunk-Add-on-Builder-creating-a-custom-Python-app/m-p/704548#M11743</link>
      <description>&lt;P&gt;I can't see any obvious issue with your code. What happens if you include debug log statements that output the report_id value, and then the resulting URL?&lt;/P&gt;&lt;P&gt;Assuming logging mode is set to debug:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;    helper.log_debug(f"Report ID is: {report_id}")

    url = f"https://example_url/{report_id}/download"
    
    helper.log_debug(f"URL is: {url}")
    
    headers = {
        "accept": "application/json",
        "Authorization": f"Bearer {jwt_token}",
    }&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 16 Nov 2024 06:54:09 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Splunk-Dev/Help-with-the-Splunk-Add-on-Builder-creating-a-custom-Python-app/m-p/704548#M11743</guid>
      <dc:creator>marnall</dc:creator>
      <dc:date>2024-11-16T06:54:09Z</dc:date>
    </item>
  </channel>
</rss>

