<?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: REST API Modular Input: What to put into custom authentication and response handlers to handle a sequence of https requests? in All Apps and Add-ons</title>
    <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/REST-API-Modular-Input-What-to-put-into-custom-authentication/m-p/114915#M8481</link>
    <description>&lt;P&gt;Is the 401 occurring on the Login HTTP GET or the Endpoint HTTP GET ?&lt;/P&gt;</description>
    <pubDate>Thu, 29 Jan 2015 14:05:34 GMT</pubDate>
    <dc:creator>Damien_Dallimor</dc:creator>
    <dc:date>2015-01-29T14:05:34Z</dc:date>
    <item>
      <title>REST API Modular Input: What to put into custom authentication and response handlers to handle a sequence of https requests?</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/REST-API-Modular-Input-What-to-put-into-custom-authentication/m-p/114910#M8476</link>
      <description>&lt;P&gt;Hello Splunkers,&lt;/P&gt;

&lt;P&gt;currently I am struggling with the REST-TA Modular Input. We are trying to access the WLAN Controller of Ubiquity Unifi WLAN Access Points, which offers a REST API. We want to retrieve the event log.&lt;/P&gt;

&lt;P&gt;The sequence of actions needed to retrieve the event log is:&lt;BR /&gt;
 1. login into the Unifi Controller with &lt;CODE&gt;&lt;A href="https://unifi.domain.de:7777/login?username=XXX&amp;amp;login=login&amp;amp;password=YYY" target="test_blank"&gt;https://unifi.domain.de:7777/login?username=XXX&amp;amp;login=login&amp;amp;password=YYY&lt;/A&gt;&lt;/CODE&gt;&lt;BR /&gt;
 2. retrieve the event log with &lt;CODE&gt;&lt;A href="https://unifi.domain.de:7777/api/stat/event" target="test_blank"&gt;https://unifi.domain.de:7777/api/stat/event&lt;/A&gt;&lt;/CODE&gt;, this returns a JSON structure&lt;BR /&gt;
 3. logout with &lt;CODE&gt;&lt;A href="https://unifi.domain.de:7777/logout" target="test_blank"&gt;https://unifi.domain.de:7777/logout&lt;/A&gt;&lt;/CODE&gt;&lt;/P&gt;

&lt;P&gt;The difference to all other questions regarding REST-TA is: Before actually GETting the information requested, a https-Login-packet is required, and afterwards a https-Logout-packet must be sent to the REST-API-provider.&lt;/P&gt;

&lt;P&gt;This is the inputs.conf stanza:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[rest://Unifi WLAN Events]
auth_type = custom
custom_auth_handler = MyUnifyAuth
endpoint = &lt;A href="https://unifi.domain.de:7777/api/stat/event" target="test_blank"&gt;https://unifi.domain.de:7777/api/stat/event&lt;/A&gt;
http_method = GET
index = test
index_error_response_codes = 1
polling_interval = 120
response_handler = MyUnifyResponse
response_type = json
sourcetype = _json
streaming_request = 0
disabled = 1
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;And this is the custom authhandlers.py (only additional lines to the shipped file, code is taken mostly from Unifi-API examples at github):&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;import urllib2
import cookielib
#login for Unifi WLAN controller class MyUnifyAuth(AuthBase):
    def __init__(self,**args):
        cj = cookielib.CookieJar()
        self.opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
        pass

    def __call__(self, r):
        loginurl = 'https://unifi.domain.de:7777/login?username=XXX&amp;amp;login=login&amp;amp;password=YYY'
        self.opener.open(loginurl).read()
        return r
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;And the custom responsehandlers.py basically is the default handler, extended with the logout:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;class MyUnifyResponse:

    def __init__(self,**args):
        pass

    def __call__(self, response_object,raw_response_output,response_type,req_args,endpoint):
        self.opener.open('https://unifi.domain.de:7777/logout').read()
        cookies = response_object.cookies
        if cookies:
            req_args["cookies"] = cookies
        print_xml_stream(raw_response_output)    
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;Now for the problems/questions:&lt;/P&gt;

&lt;UL&gt;
&lt;LI&gt;Did I really get the basic API flow correct? Or did I misunderstand the methods completely?&lt;/LI&gt;
&lt;LI&gt;Something must be very wrong: SOS tells me &lt;CODE&gt;ExecProcessor - message from "python /export/server/splunk/etc/apps/rest_ta/bin/rest.py" HTTP Request error: 401 Client Error: Unauthorized&lt;/CODE&gt;&lt;/LI&gt;
&lt;LI&gt;Since the Unifi-API isn't documented, the missing parts are difficult to find, I assume that something is requiring cookies, and that the cookie handling is wrong (wild, but educated, guess...)&lt;/LI&gt;
&lt;LI&gt;I would appreciate hints on how to handle the sequence of https-requests described above, i.e. what to put into authentication and what to put into response handlers.&lt;/LI&gt;
&lt;/UL&gt;

&lt;P&gt;Thanks in advance for helpful hints!!&lt;BR /&gt;
Best regards,&lt;BR /&gt;
Stephan&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jan 2015 10:24:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/REST-API-Modular-Input-What-to-put-into-custom-authentication/m-p/114910#M8476</guid>
      <dc:creator>swasserroth</dc:creator>
      <dc:date>2015-01-29T10:24:59Z</dc:date>
    </item>
    <item>
      <title>Re: REST API Modular Input: What to put into custom authentication and response handlers to handle a sequence of https requests?</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/REST-API-Modular-Input-What-to-put-into-custom-authentication/m-p/114911#M8477</link>
      <description>&lt;P&gt;I'm making a very wild guess here as I know nothing about Unifi. So try the below ideas.&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;[rest://Unifi WLAN Events]
auth_type = custom
custom_auth_handler = MyUnifyAuth
custom_auth_handler_args = username=foo,password=goo,login_url=https://unifi.domain.de:7777/login
endpoint = &lt;A href="https://unifi.domain.de:7777/api/stat/event" target="test_blank"&gt;https://unifi.domain.de:7777/api/stat/event&lt;/A&gt;
http_method = GET
index = test
index_error_response_codes = 1
polling_interval = 120
response_handler = MyUnifyResponse
response_handler_args = logout_url=https://unifi.domain.de:7777/logout
response_type = json
sourcetype = _json
streaming_request = 0
disabled = 1

class MyUnifyAuth(AuthBase):
     def __init__(self,**args):
         self.username = args['username']
         self.password = args['password']
         self.login_url = args['login_url']
         pass

     def __call__(self, r):
         payload = {'username': self.username, 'login': 'login','password':self.password}
         login_response = requests.get(self.login_url,params=payload,verify=false)
         cookies = login_response.cookies
         if cookies:
            r.cookies = cookies
         return r

class MyUnifyResponse:

     def __init__(self,**args):
         self.logout_url = args['logout_url']
         pass

     def __call__(self, response_object,raw_response_output,response_type,req_args,endpoint):
         cookies = response_object.cookies
         if cookies:
             req_args["cookies"] = cookies
         requests.get(self.logout_url,verify=false,cookies=cookies)

         print_xml_stream(raw_response_output) 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 29 Jan 2015 11:51:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/REST-API-Modular-Input-What-to-put-into-custom-authentication/m-p/114911#M8477</guid>
      <dc:creator>Damien_Dallimor</dc:creator>
      <dc:date>2015-01-29T11:51:50Z</dc:date>
    </item>
    <item>
      <title>Re: REST API Modular Input: What to put into custom authentication and response handlers to handle a sequence of https requests?</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/REST-API-Modular-Input-What-to-put-into-custom-authentication/m-p/114912#M8478</link>
      <description>&lt;P&gt;Hi Damien,&lt;BR /&gt;
thanks for the very quick answer!! Now I understand the data flow much better. I had to add &lt;CODE&gt;verify=False&lt;/CODE&gt; to the requests.get, otherwise SSL errors are thrown, because only self signed certificates are used. Otherwise I am using exactl your code.&lt;/P&gt;

&lt;P&gt;I have found out that the login sets a session cookie (destroyed at the end of a session) named "unifises" with a hex string, probably a kind of session id.&lt;/P&gt;

&lt;P&gt;I still get an 401 error, client unauthorized. Since the cookie is returned in r.cookies probably the session is not reused inside the rest-ta? Currently I can't imagine a reason for the 401.&lt;/P&gt;

&lt;P&gt;If it is a certification validation problem: How do I propagate the verify=False to the get-Request of the endpoint-URL?&lt;/P&gt;

&lt;P&gt;Thanks again,&lt;BR /&gt;
Stephan&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jan 2015 13:22:08 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/REST-API-Modular-Input-What-to-put-into-custom-authentication/m-p/114912#M8478</guid>
      <dc:creator>swasserroth</dc:creator>
      <dc:date>2015-01-29T13:22:08Z</dc:date>
    </item>
    <item>
      <title>Re: REST API Modular Input: What to put into custom authentication and response handlers to handle a sequence of https requests?</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/REST-API-Modular-Input-What-to-put-into-custom-authentication/m-p/114913#M8479</link>
      <description>&lt;P&gt;I only have this to go off : &lt;A href="https://github.com/calmh/unifi-api/blob/master/unifi/controller.py"&gt;https://github.com/calmh/unifi-api/blob/master/unifi/controller.py&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;I see that there is some dynamic URL construction going on , is &lt;A href="https://unifi.domain.de:7777/api/stat/event"&gt;https://unifi.domain.de:7777/api/stat/event&lt;/A&gt; the correct URL ?&lt;/P&gt;

&lt;P&gt;Can you trace the requests/responses being sent/received (ie: using a tool like wireshark) , to ensure that the HTTP GET looks correct ?&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jan 2015 13:52:28 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/REST-API-Modular-Input-What-to-put-into-custom-authentication/m-p/114913#M8479</guid>
      <dc:creator>Damien_Dallimor</dc:creator>
      <dc:date>2015-01-29T13:52:28Z</dc:date>
    </item>
    <item>
      <title>Re: REST API Modular Input: What to put into custom authentication and response handlers to handle a sequence of https requests?</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/REST-API-Modular-Input-What-to-put-into-custom-authentication/m-p/114914#M8480</link>
      <description>&lt;P&gt;The URLs for login, logout and REST-API-call to get the events have been checked inside a Firefox-browser, the browser-window displays the json-list of events. These are the URLs as they actually are constructed by the controller.py from github, what we have used to understand the required flow of https-packets.&lt;/P&gt;

&lt;P&gt;Wireshark is not so easy, everything is ssl-encoded...&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jan 2015 13:56:13 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/REST-API-Modular-Input-What-to-put-into-custom-authentication/m-p/114914#M8480</guid>
      <dc:creator>swasserroth</dc:creator>
      <dc:date>2015-01-29T13:56:13Z</dc:date>
    </item>
    <item>
      <title>Re: REST API Modular Input: What to put into custom authentication and response handlers to handle a sequence of https requests?</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/REST-API-Modular-Input-What-to-put-into-custom-authentication/m-p/114915#M8481</link>
      <description>&lt;P&gt;Is the 401 occurring on the Login HTTP GET or the Endpoint HTTP GET ?&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jan 2015 14:05:34 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/REST-API-Modular-Input-What-to-put-into-custom-authentication/m-p/114915#M8481</guid>
      <dc:creator>Damien_Dallimor</dc:creator>
      <dc:date>2015-01-29T14:05:34Z</dc:date>
    </item>
    <item>
      <title>Re: REST API Modular Input: What to put into custom authentication and response handlers to handle a sequence of https requests?</title>
      <link>https://community.splunk.com/t5/All-Apps-and-Add-ons/REST-API-Modular-Input-What-to-put-into-custom-authentication/m-p/114916#M8482</link>
      <description>&lt;P&gt;OK, I am pretty much shure, that this is related to the session-only duration of the cookie.&lt;/P&gt;

&lt;P&gt;What I did inside the MyUnifiAuth class to test this:&lt;BR /&gt;
 - request.get with Login-String, this returned 200&lt;BR /&gt;
 - request.get with event-API-call, this returned 401, and a json-error "api.err.LoginRequired"&lt;BR /&gt;
 Seems that this are actually two sessions...&lt;/P&gt;

&lt;P&gt;Thanks for supporting us!!&lt;BR /&gt;
I think, we should consider other (outside of Splunk) methods to retrieve the event list using an intermediate file.&lt;/P&gt;</description>
      <pubDate>Thu, 29 Jan 2015 14:57:27 GMT</pubDate>
      <guid>https://community.splunk.com/t5/All-Apps-and-Add-ons/REST-API-Modular-Input-What-to-put-into-custom-authentication/m-p/114916#M8482</guid>
      <dc:creator>swasserroth</dc:creator>
      <dc:date>2015-01-29T14:57:27Z</dc:date>
    </item>
  </channel>
</rss>

