From 8c23a2def78e2e715084f6bc871ffa0a051d3467 Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Tue, 16 Jan 2024 21:56:34 +0100 Subject: [PATCH 1/2] chg: [internal] User faster method to convert bytes to str --- pymisp/abstract.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pymisp/abstract.py b/pymisp/abstract.py index 8a4be01..88198af 100644 --- a/pymisp/abstract.py +++ b/pymisp/abstract.py @@ -247,8 +247,9 @@ class AbstractMISP(MutableMapping, MISPFileCache, metaclass=ABCMeta): option |= orjson.OPT_SORT_KEYS if indent: option |= orjson.OPT_INDENT_2 - - return dumps(self, default=pymisp_json_default, option=option).decode("utf-8") + # orjson dumps method returns bytes instead of bytes, to keep compatibility with json + # we have to convert output to str + return str(dumps(self, default=pymisp_json_default, option=option)) return dumps(self, default=pymisp_json_default, sort_keys=sort_keys, indent=indent) From 292948aa580dd19a04f676f31d0e3697f52d88fe Mon Sep 17 00:00:00 2001 From: Jakub Onderka Date: Tue, 16 Jan 2024 21:57:06 +0100 Subject: [PATCH 2/2] chg: [internal] Simplify code --- pymisp/api.py | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/pymisp/api.py b/pymisp/api.py index 85f6b00..53dab93 100644 --- a/pymisp/api.py +++ b/pymisp/api.py @@ -69,7 +69,7 @@ def get_uuid_or_id_from_abstract_misp(obj: Union[AbstractMISP, int, str, UUID, d if isinstance(obj, (int, str)): return obj - if isinstance(obj, dict) and len(obj.keys()) == 1: + if isinstance(obj, dict) and len(obj) == 1: # We have an object in that format: {'Event': {'id': 2, ...}} # We need to get the content of that dictionary obj = obj[list(obj.keys())[0]] @@ -186,6 +186,7 @@ class PyMISP: self.__session.headers['Accept-Encoding'] = ', '.join(('br', 'gzip', 'deflate')) if http_headers: self.__session.headers.update(http_headers) + self._user_agent = f'PyMISP {__version__} - Python {".".join(str(x) for x in sys.version_info[:2])}' self.global_pythonify = False @@ -2622,10 +2623,10 @@ class PyMISP: ''' - return_formats = ['openioc', 'json', 'xml', 'suricata', 'snort', 'text', 'rpz', 'csv', 'cache', 'stix-xml', - 'stix', 'stix2', 'yara', 'yara-json', 'attack', 'attack-sightings', 'context', 'context-markdown'] + return_formats = ('openioc', 'json', 'xml', 'suricata', 'snort', 'text', 'rpz', 'csv', 'cache', 'stix-xml', + 'stix', 'stix2', 'yara', 'yara-json', 'attack', 'attack-sightings', 'context', 'context-markdown') - if controller not in ['events', 'attributes', 'objects']: + if controller not in ('events', 'attributes', 'objects'): raise ValueError('controller has to be in {}'.format(', '.join(['events', 'attributes', 'objects']))) # Deprecated stuff / synonyms @@ -2996,7 +2997,7 @@ class PyMISP: query.pop('pythonify') if log_id is not None: query['id'] = query.pop('log_id') - if created is not None and isinstance(created, (datetime)): + if created is not None and isinstance(created, datetime): query['created'] = query.pop('created').timestamp() response = self._prepare_request('POST', 'admin/logs/index', data=query) @@ -3349,7 +3350,7 @@ class PyMISP: """ query: Dict[str, Any] = {'setting': user_setting} if isinstance(value, dict): - value = dumps(value).decode("utf-8") if HAS_ORJSON else dumps(value) + value = str(dumps(value)) if HAS_ORJSON else dumps(value) query['value'] = value if user: query['user_id'] = get_uuid_or_id_from_abstract_misp(user) @@ -3739,7 +3740,7 @@ class PyMISP: data = {k: v for k, v in data.items() if v is not None} d = dumps(data, default=pymisp_json_default) - logger.debug(f'{request_type} - {url}') + logger.debug('%s - %s', request_type, url) if d is not None: logger.debug(d) @@ -3749,9 +3750,7 @@ class PyMISP: url = f'{url}/{to_append_url}' req = requests.Request(request_type, url, data=d, params=params) - user_agent = f'PyMISP {__version__} - Python {".".join(str(x) for x in sys.version_info[:2])}' - if self.tool: - user_agent = f'{user_agent} - {self.tool}' + user_agent = f'{self._user_agent} - {self.tool}' if self.tool else self._user_agent req.auth = self.auth prepped = self.__session.prepare_request(req) prepped.headers.update(