Merge branch 'lastinfosec-main' into main

pull/660/head
Raphaël Vinot 2020-11-03 13:31:03 +01:00
commit 56ff22228c
1 changed files with 14 additions and 6 deletions

View File

@ -15,6 +15,7 @@ from uuid import UUID
import warnings
import sys
import copy
from io import BytesIO, StringIO
from . import __version__, everything_broken
from .exceptions import MISPServerError, PyMISPUnexpectedResponse, PyMISPError, NoURL, NoKey
@ -2523,20 +2524,27 @@ class PyMISP:
to_return.append(a)
return to_return
def upload_stix(self, path, version: str = '2'):
def upload_stix(self, path: Optional[Union[str, Path, BytesIO, StringIO]] = None, data: Optional[Union[str, bytes]] = None, version: str = '2'):
"""Upload a STIX file to MISP.
:param path: Path to the STIX on the disk (can be a path-like object, or a pseudofile)
:param data: stix object
:param version: Can be 1 or 2
"""
if isinstance(path, (str, Path)):
with open(path, 'rb') as f:
to_post = f.read()
to_post: Union[str, bytes]
if path is not None:
if isinstance(path, (str, Path)):
with open(path, 'rb') as f:
to_post = f.read()
else:
to_post = path.read()
elif data is not None:
to_post = data
else:
to_post = path.read()
raise MISPServerError("please fill path or data parameter")
if isinstance(to_post, bytes):
to_post = to_post.decode() # type: ignore
to_post = to_post.decode()
if str(version) == '1':
url = urljoin(self.root_url, '/events/upload_stix')