From 57bb8cea1a0d3307e191d5746ab3111165c90a08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Tue, 22 Jan 2019 11:49:01 +0100 Subject: [PATCH] chg: force to_ids to be a boolean, as MISP is expecting. fix #320 --- pymisp/aping.py | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/pymisp/aping.py b/pymisp/aping.py index 097b8f9..848de01 100644 --- a/pymisp/aping.py +++ b/pymisp/aping.py @@ -4,7 +4,7 @@ from .exceptions import MISPServerError, NewEventError, UpdateEventError, UpdateAttributeError, PyMISPNotImplementedYet from .api import PyMISP, everything_broken from .mispevent import MISPEvent, MISPAttribute, MISPSighting, MISPLog -from typing import TypeVar, Optional, Tuple, List, Dict +from typing import TypeVar, Optional, Tuple, List, Dict, Union from datetime import date, datetime import csv @@ -17,6 +17,7 @@ SearchParameterTypes = TypeVar('SearchParameterTypes', str, List[SearchType], Di DateTypes = TypeVar('DateTypes', datetime, date, SearchType, float) DateInterval = TypeVar('DateInterval', DateTypes, Tuple[DateTypes, DateTypes]) +ToIDSType = TypeVar('ToIDSType', str, int, bool) logger = logging.getLogger('pymisp') @@ -183,8 +184,8 @@ class ExpandedPyMISP(PyMISP): url = urljoin(self.root_url, url_path) response = self._prepare_request('POST', url, data=query) normalized_response = self._check_response(response) - if isinstance(normalized_response, str) or (isinstance(normalized_response, dict) and - normalized_response.get('errors')): + if isinstance(normalized_response, str) or (isinstance(normalized_response, dict) + and normalized_response.get('errors')): return normalized_response elif pythonify: to_return = [] @@ -227,7 +228,7 @@ class ExpandedPyMISP(PyMISP): timestamp: Optional[DateInterval]=None, published: Optional[bool]=None, enforce_warninglist: Optional[bool]=None, enforceWarninglist: Optional[bool]=None, - to_ids: Optional[str]=None, + to_ids: Optional[Union[ToIDSType, List[ToIDSType]]]=None, deleted: Optional[str]=None, include_event_uuid: Optional[str]=None, includeEventUuid: Optional[str]=None, event_timestamp: Optional[DateTypes]=None, @@ -260,7 +261,7 @@ class ExpandedPyMISP(PyMISP): :param timestamp: Restrict the results by the timestamp (last edit). Any event with a timestamp newer than the given timestamp will be returned. In case you are dealing with /attributes as scope, the attribute's timestamp will be used for the lookup. :param published: Set whether published or unpublished events should be returned. Do not set the parameter if you want both. :param enforce_warninglist: Remove any attributes from the result that would cause a hit on a warninglist entry. - :param to_ids: By default (0) all attributes are returned that match the other filter parameters, irregardless of their to_ids setting. To restrict the returned data set to to_ids only attributes set this parameter to 1. You can only use the special "exclude" setting to only return attributes that have the to_ids flag disabled. + :param to_ids: By default all attributes are returned that match the other filter parameters, irregardless of their to_ids setting. To restrict the returned data set to to_ids only attributes set this parameter to 1. 0 for the ones with to_ids set to False. :param deleted: If this parameter is set to 1, it will return soft-deleted attributes along with active ones. By using "only" as a parameter it will limit the returned data set to soft-deleted data only. :param include_event_uuid: Instead of just including the event ID, also include the event UUID in each of the attributes. :param event_timestamp: Only return attributes from events that have received a modification after the given timestamp. @@ -337,8 +338,8 @@ class ExpandedPyMISP(PyMISP): query['published'] = published query['enforceWarninglist'] = enforce_warninglist if to_ids is not None: - if str(to_ids) not in ['0', '1', 'exclude']: - raise ValueError('to_ids has to be in {}'.format(', '.join(['0', '1', 'exclude']))) + if int(to_ids) not in [0, 1]: + raise ValueError('to_ids has to be in {}'.format(', '.join([0, 1]))) query['to_ids'] = to_ids query['deleted'] = deleted query['includeEventUuid'] = include_event_uuid @@ -358,8 +359,8 @@ class ExpandedPyMISP(PyMISP): normalized_response = self._check_response(response) if return_format == 'csv' and pythonify and not headerless: return self._csv_to_dict(normalized_response) - elif isinstance(normalized_response, str) or (isinstance(normalized_response, dict) and - normalized_response.get('errors')): + elif isinstance(normalized_response, str) or (isinstance(normalized_response, dict) + and normalized_response.get('errors')): return normalized_response elif return_format == 'json' and pythonify: # The response is in json, we can convert it to a list of pythonic MISP objects