Skip to content

Commit bb9f053

Browse files
authored
Merge pull request #835 from tomking2/feature/search_sharinggroup
new: Search by sharing groups
2 parents cd4b5d5 + 1ac66a9 commit bb9f053

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed

pymisp/api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2413,6 +2413,7 @@ def search(self, controller: str = 'events', return_format: str = 'json',
24132413
include_decay_score: Optional[bool] = None, includeDecayScore: Optional[bool] = None,
24142414
object_name: Optional[str] = None,
24152415
exclude_decayed: Optional[bool] = None,
2416+
sharinggroup: Optional[Union[int, List[int]]] = None,
24162417
pythonify: Optional[bool] = False,
24172418
**kwargs) -> Union[Dict, str, List[Union[MISPEvent, MISPAttribute, MISPObject]]]:
24182419
'''Search in the MISP instance
@@ -2453,6 +2454,7 @@ def search(self, controller: str = 'events', return_format: str = 'json',
24532454
:param include_correlations: [JSON Only - attribute] Include the correlations of the matching attributes.
24542455
:param object_name: [objects controller only] Search for objects with that name
24552456
:param exclude_decayed: [attributes controller only] Exclude the decayed attributes from the response
2457+
:param sharinggroup: Filter by sharing group ID(s)
24562458
:param pythonify: Returns a list of PyMISP Objects instead of the plain json output. Warning: it might use a lot of RAM
24572459
24582460
Deprecated:
@@ -2553,6 +2555,8 @@ def search(self, controller: str = 'events', return_format: str = 'json',
25532555
query['includeCorrelations'] = self._make_misp_bool(include_correlations)
25542556
query['object_name'] = object_name
25552557
query['excludeDecayed'] = self._make_misp_bool(exclude_decayed)
2558+
query['sharinggroup'] = sharinggroup
2559+
25562560
url = urljoin(self.root_url, f'{controller}/restSearch')
25572561
if return_format == 'stix-xml':
25582562
response = self._prepare_request('POST', url, data=query, output_type='xml')

tests/testlive_comprehensive.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2235,6 +2235,51 @@ def test_sharing_group(self):
22352235
finally:
22362236
self.admin_misp_connector.delete_sharing_group(sharing_group.id)
22372237

2238+
def test_sharing_group_search(self):
2239+
# Add sharing group
2240+
sg = MISPSharingGroup()
2241+
sg.name = 'Testcases SG'
2242+
sg.releasability = 'Testing'
2243+
sharing_group = self.admin_misp_connector.add_sharing_group(sg, pythonify=True)
2244+
# Add the org to the sharing group
2245+
self.admin_misp_connector.add_org_to_sharing_group(
2246+
sharing_group,
2247+
self.test_org, extend=True
2248+
)
2249+
# Add event
2250+
event = self.create_simple_event()
2251+
event.distribution = Distribution.sharing_group
2252+
event.sharing_group_id = sharing_group.id
2253+
# Create two attributes, one specifically for the sharing group,
2254+
# another which inherits the event's SG
2255+
event.add_attribute('ip-dst', '8.8.8.8', distribution=4, sharing_group_id=sharing_group.id)
2256+
event.add_attribute('ip-dst', '9.9.9.9')
2257+
event = self.user_misp_connector.add_event(event)
2258+
attribute_ids = {a.id for a in event.attributes}
2259+
try:
2260+
# Try to query for the event
2261+
events = self.user_misp_connector.search(sharinggroup=sharing_group.id, controller="events")
2262+
# There should be one event
2263+
self.assertTrue(len(events) == 1)
2264+
# This event should be the one we added
2265+
self.assertEqual(events[0].id, event.id)
2266+
# Make sure the search isn't just returning everything
2267+
events = self.user_misp_connector.search(sharinggroup=99999, controller="events")
2268+
2269+
self.assertTrue(len(events) == 0)
2270+
2271+
# Try to query for the attributes
2272+
attributes = self.user_misp_connector.search(sharinggroup=sharing_group.id, controller="attributes")
2273+
searched_attribute_ids = {a.id for a in attributes}
2274+
# There should be two attributes
2275+
# The extra 1 is the random UUID now created in the event
2276+
self.assertTrue(len(attributes) == 2 + 1)
2277+
# We should not be missing any of the attributes
2278+
self.assertFalse(attribute_ids.difference(searched_attribute_ids))
2279+
finally:
2280+
self.admin_misp_connector.delete_sharing_group(sharing_group.id)
2281+
self.user_misp_connector.delete_event(event.id)
2282+
22382283
def test_feeds(self):
22392284
# Add
22402285
feed = MISPFeed()

0 commit comments

Comments
 (0)