Merge pull request #795 from tomking2/feature/sharing_group_improvements

Improve sharing groups, new get_sharing_group and return sharing group orgs
JakubOnderka-patch-1
Raphaël Vinot 2021-10-14 14:51:11 +02:00 committed by GitHub
commit ec4172dfba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 68 additions and 0 deletions

View File

@ -1921,6 +1921,21 @@ class PyMISP:
to_return.append(s)
return to_return
def get_sharing_group(self, sharing_group: Union[MISPSharingGroup, int, str, UUID], pythonify: bool = False) -> Union[Dict, MISPSharingGroup]:
"""Get a sharing group
:param sharing_group: sharing group to find
:param pythonify: Returns a PyMISP Object instead of the plain json output
"""
sharing_group_id = get_uuid_or_id_from_abstract_misp(sharing_group)
r = self._prepare_request('GET', f'sharing_groups/view/{sharing_group_id}')
sharing_group_resp = self._check_json_response(r)
if not (self.global_pythonify or pythonify) or 'errors' in sharing_group_resp:
return sharing_group_resp
s = MISPSharingGroup()
s.from_dict(**sharing_group_resp)
return s
def add_sharing_group(self, sharing_group: MISPSharingGroup, pythonify: bool = False) -> Union[Dict, MISPSharingGroup]:
"""Add a new sharing group

View File

@ -126,12 +126,41 @@ class MISPOrganisation(AbstractMISP):
class MISPSharingGroup(AbstractMISP):
def __init__(self):
super().__init__()
self.name: str
self.SharingGroupOrg: List[MISPOrganisation] = []
@property
def orgs(self) -> List[MISPOrganisation]:
return self.SharingGroupOrg
@orgs.setter
def orgs(self, orgs: List[MISPOrganisation]):
"""Set a list of prepared MISPSighting."""
if all(isinstance(x, MISPSighting) for x in orgs):
self.SharingGroupOrg = orgs
else:
raise PyMISPError('All the attributes have to be of type MISPOrganisation.')
def add_org(self, org):
misp_org = MISPOrganisation()
misp_org.from_dict(**org)
self.SharingGroupOrg.append(misp_org)
return misp_org
def from_dict(self, **kwargs):
if 'SharingGroupOrg' in kwargs:
[self.add_org(org) for org in kwargs.pop('SharingGroupOrg')]
if 'SharingGroup' in kwargs:
kwargs = kwargs['SharingGroup']
super().from_dict(**kwargs)
def __repr__(self) -> str:
if hasattr(self, 'name'):
return f'<{self.__class__.__name__}(name={self.name})'
return f'<{self.__class__.__name__}(NotInitialized)'
class MISPShadowAttribute(AbstractMISP):

View File

@ -2208,6 +2208,30 @@ class TestComprehensive(unittest.TestCase):
self.assertFalse(self.admin_misp_connector.sharing_group_exists(sharing_group.id))
self.assertFalse(self.admin_misp_connector.sharing_group_exists(sharing_group.uuid))
def test_sharing_group(self):
# add
sg = MISPSharingGroup()
sg.name = 'Testcases SG'
sg.releasability = 'Testing'
sharing_group = self.admin_misp_connector.add_sharing_group(sg, pythonify=True)
# Add the org to the sharing group
self.admin_misp_connector.add_org_to_sharing_group(
sharing_group,
self.test_org, extend=True
)
try:
# Get the sharing group once again
sharing_group = self.admin_misp_connector.get_sharing_group(sharing_group, pythonify=True)
self.assertTrue(isinstance(sharing_group, MISPSharingGroup))
self.assertEqual(sharing_group.name, 'Testcases SG')
# Check we have the org field present and the first org is our org
self.assertTrue(isinstance(getattr(sharing_group, "orgs"), list))
self.assertEqual(sharing_group.orgs[0].id, self.test_org.id)
finally:
self.admin_misp_connector.delete_sharing_group(sharing_group.id)
def test_feeds(self):
# Add
feed = MISPFeed()