AlienVault-OTX / OTX-Python-SDK

The Python SDK for AlienVault OTX
Other
357 stars 162 forks source link

Error when requesting a non-existent pulse #39

Open Te-k opened 6 years ago

Te-k commented 6 years ago

When requesting an non-existent pulse, with pip version 1.2:

indicators = otx.get_pulse_indicators("aaaaaaaaaa")
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-6-78f47ae1d67a> in <module>()
----> 1 indicators = otx.get_pulse_indicators("aaaaaaaaaa")

/home/user/envs/analysis3/lib/python3.5/site-packages/OTXv2.py in get_pulse_indicators(self, pulse_id, limit)
    412         next_page_url = self.create_url(PULSE_DETAILS + str(pulse_id) + "/indicators", limit=limit)
    413         while next_page_url:
--> 414             json_data = self.get(next_page_url)
    415             for r in json_data["results"]:
    416                 indicators.append(r)

/home/user/envs/analysis3/lib/python3.5/site-packages/OTXv2.py in get(self, url)
     81             else:
     82                 raise e
---> 83         data = response.read().decode('utf-8')
     84         json_data = json.loads(data)
     85         return json_data

AttributeError: 'NoneType' object has no attribute 'read'

With masterbranch (commit da9cd3472f84d167ac7cab5da600c76f6f04f520 ):

indicators = otx.get_pulse_indicators("aaaaaaaaaa")
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-3-78f47ae1d67a> in <module>()
----> 1 indicators = otx.get_pulse_indicators("aaaaaaaaaa")

/home/user/envs/analysis3/lib/python3.5/site-packages/OTXv2.py in get_pulse_indicators(self, pulse_id, limit)
    402         :return: Indicator list
    403         """
--> 404         return self.walkapi(self.create_url(PULSE_DETAILS + str(pulse_id) + "/indicators", limit=limit))
    405 
    406 

/home/user/envs/analysis3/lib/python3.5/site-packages/OTXv2.py in walkapi(self, url, iter, max_page)
    272             return self.walkapi_iter(url, max_page=max_page)
    273         else:
--> 274             return list(self.walkapi_iter(url, max_page=max_page))
    275 
    276     def getall(self, modified_since=None, author_name=None, limit=20, max_page=None, iter=False):

/home/user/envs/analysis3/lib/python3.5/site-packages/OTXv2.py in walkapi_iter(self, url, max_page)
    263                 break
    264 
--> 265             data = self.get(next_page_url)
    266             for el in data['results']:
    267                 yield el

/home/user/envs/analysis3/lib/python3.5/site-packages/OTXv2.py in get(self, url, **kwargs)
    112                 proxies=self.proxies,
    113             )
--> 114             return self.handle_response_errors(response).json()
    115         except requests.exceptions.RetryError:
    116             raise RetryError()

/home/user/envs/analysis3/lib/python3.5/site-packages/OTXv2.py in handle_response_errors(cls, response)
     95             raise BadRequest(_response_json())
     96         elif str(response.status_code)[0] != "2":
---> 97             raise Exception("Unexpected http code: %r, response=%r", response.status_code, _response_json())
     98 
     99         return response

Exception: ('Unexpected http code: %r, response=%r', 404, {'internal_error': 'Unable to decode response json: Expecting value: line 1 column 1 (char 0)'})

It would be way better to raise a custom exception here.

fr1d4yyy commented 6 years ago

i'm also getting "AttributeError: 'NoneType' object has no attribute 'read'"

Any update how to fix this issue?

Te-k commented 6 years ago

@fr1d4yyy If it is the same exception than the one in my code, it only means that the pulse requested does not exist. It is ugly and it would be better to have either a custom exception or return code, but it is totally usable (if it happens in another case, you should open a new issue I think)

rustybrooks commented 5 years ago

I fixed this in a few places - there are probably more, I'll look into it more when I have time.

There are actually 2 separate issues here. One is that for historical reasons, you won't even really get a 404 if your pulse_id isn't recognizable to the web server (it needs to be a 24-character hex string). So I made it so that get_pulse_details and get_pulse_indicators will raise BadRequest if the pulse_id doesn't match expectations.

If the pulse_id is valid, but is not found, a new exception will be thrown, called NotFound.

I added a few simple tests for this, seems to be working ok. Code is in master, will be in a patch release soon.