Merge pull request #794 from tomking2/feature/org_user_search

chg: Add ability to search against orgs and users by freetext search (both) or organisation (users)
JakubOnderka-patch-1
Raphaël Vinot 2021-10-14 14:46:34 +02:00 committed by GitHub
commit e84d5a11f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 5 deletions

View File

@ -2030,13 +2030,18 @@ class PyMISP:
# ## BEGIN Organisation ### # ## BEGIN Organisation ###
def organisations(self, scope="local", pythonify: bool = False) -> Union[Dict, List[MISPOrganisation]]: def organisations(self, scope="local", search: str = None, pythonify: bool = False) -> Union[Dict, List[MISPOrganisation]]:
"""Get all the organisations """Get all the organisations
:param scope: scope of organizations to get :param scope: scope of organizations to get
:param search: The search to make against the list of organisations
:param pythonify: Returns a list of PyMISP Objects instead of the plain json output. Warning: it might use a lot of RAM :param pythonify: Returns a list of PyMISP Objects instead of the plain json output. Warning: it might use a lot of RAM
""" """
r = self._prepare_request('GET', f'organisations/index/scope:{scope}') url_path = f'organisations/index/scope:{scope}'
if search:
url_path += f"/searchall:{search}"
r = self._prepare_request('GET', url_path)
organisations = self._check_json_response(r) organisations = self._check_json_response(r)
if not (self.global_pythonify or pythonify) or 'errors' in organisations: if not (self.global_pythonify or pythonify) or 'errors' in organisations:
return organisations return organisations
@ -2118,12 +2123,20 @@ class PyMISP:
# ## BEGIN User ### # ## BEGIN User ###
def users(self, pythonify: bool = False) -> Union[Dict, List[MISPUser]]: def users(self, search: str = None, organisation: int = None, pythonify: bool = False) -> Union[Dict, List[MISPUser]]:
"""Get all the users """Get all the users, or a filtered set of users
:param search: The search to make against the list of users
:param organisation: The ID of an organisation to filter against
:param pythonify: Returns a list of PyMISP Objects instead of the plain json output. Warning: it might use a lot of RAM :param pythonify: Returns a list of PyMISP Objects instead of the plain json output. Warning: it might use a lot of RAM
""" """
r = self._prepare_request('GET', 'admin/users/index') urlpath = 'admin/users/index'
if search:
urlpath += f'/value:{search}'
if organisation:
organisation_id = get_uuid_or_id_from_abstract_misp(organisation)
urlpath += f"/searchorg:{organisation_id}"
r = self._prepare_request('GET', urlpath)
users = self._check_json_response(r) users = self._check_json_response(r)
if not (self.global_pythonify or pythonify) or 'errors' in users: if not (self.global_pythonify or pythonify) or 'errors' in users:
return users return users

View File

@ -1771,6 +1771,33 @@ class TestComprehensive(unittest.TestCase):
organisation = self.admin_misp_connector.update_organisation(organisation, pythonify=True) organisation = self.admin_misp_connector.update_organisation(organisation, pythonify=True)
self.assertEqual(organisation.name, 'blah', organisation) self.assertEqual(organisation.name, 'blah', organisation)
def test_org_search(self):
orgs = self.admin_misp_connector.organisations(pythonify=True)
org_name = 'ORGNAME'
# Search by the org name
orgs = self.admin_misp_connector.organisations(search=org_name, pythonify=True)
# There should be one org returned
self.assertTrue(len(orgs) == 1)
# This org should have the name ORGNAME
self.assertEqual(orgs[0].name, org_name)
def test_user_search(self):
users = self.admin_misp_connector.users(pythonify=True)
emailAddr = users[0].email
users = self.admin_misp_connector.users(search=emailAddr)
self.assertTrue(len(users) == 1)
self.assertEqual(users[0]['User']['email'], emailAddr)
users = self.admin_misp_connector.users(
search=emailAddr,
organisation=users[0]['Organisation']['id'],
pythonify=True
)
self.assertTrue(len(users) == 1)
self.assertEqual(users[0].email, emailAddr)
def test_attribute(self): def test_attribute(self):
first = self.create_simple_event() first = self.create_simple_event()
second = self.create_simple_event() second = self.create_simple_event()