Skip to content

Commit

Permalink
chg: Improve error message, add comments, rename whitelist->allowedlist
Browse files Browse the repository at this point in the history
  • Loading branch information
Rafiot committed Sep 15, 2020
1 parent 73b56a6 commit 50e5f15
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
23 changes: 22 additions & 1 deletion pymisp/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@


def get_uuid_or_id_from_abstract_misp(obj: Union[AbstractMISP, int, str, UUID]) -> Union[str, int]:
"""Extract the relevant ID accordingly to the given type passed as parameter"""
if isinstance(obj, UUID):
return str(obj)
if isinstance(obj, (int, str)):
Expand Down Expand Up @@ -188,6 +189,7 @@ def version(self) -> Dict:

@property
def pymisp_version_master(self) -> Dict:
"""PyMISP version as defined in the main repository"""
return self.pymisp_version_main

@property
Expand Down Expand Up @@ -215,36 +217,44 @@ def misp_instance_version_master(self) -> Dict:
return {'error': 'Impossible to retrieve the version of the master branch.'}

def update_misp(self) -> Dict:
"""Trigger a server update"""
response = self._prepare_request('POST', 'servers/update')
return self._check_json_response(response)

def set_server_setting(self, setting: str, value: Union[str, int, bool], force: bool = False) -> Dict:
"""Set a setting on the MISP instance"""
data = {'value': value, 'force': force}
response = self._prepare_request('POST', f'servers/serverSettingsEdit/{setting}', data=data)
return self._check_json_response(response)

def get_server_setting(self, setting: str) -> Dict:
"""Get a setting from the MISP instance"""
response = self._prepare_request('GET', f'servers/getSetting/{setting}')
return self._check_json_response(response)

def server_settings(self) -> Dict:
"""Get all the settings from the server"""
response = self._prepare_request('GET', 'servers/serverSettings')
return self._check_json_response(response)

def restart_workers(self) -> Dict:
"""Restart all the workers"""
response = self._prepare_request('POST', 'servers/restartWorkers')
return self._check_json_response(response)

def db_schema_diagnostic(self) -> Dict:
"""Get the schema diagnostic"""
response = self._prepare_request('GET', 'servers/dbSchemaDiagnostic')
return self._check_json_response(response)

def toggle_global_pythonify(self) -> None:
"""Toggle the pythonify variable for the class"""
self.global_pythonify = not self.global_pythonify

# ## BEGIN Event ##

def events(self, pythonify: bool = False) -> Union[Dict, List[MISPEvent]]:
"""Get all the events from the MISP instance"""
r = self._prepare_request('GET', 'events/index')
events_r = self._check_json_response(r)
if not (self.global_pythonify or pythonify) or 'errors' in events_r:
Expand Down Expand Up @@ -424,6 +434,7 @@ def update_object_templates(self) -> Dict:
# ## BEGIN Attribute ###

def attributes(self, pythonify: bool = False) -> Union[Dict, List[MISPAttribute]]:
"""Get all the attributes from the MISP instance"""
r = self._prepare_request('GET', 'attributes/index')
attributes_r = self._check_json_response(r)
if not (self.global_pythonify or pythonify) or 'errors' in attributes_r:
Expand Down Expand Up @@ -519,6 +530,7 @@ def delete_attribute(self, attribute: Union[MISPAttribute, int, str, UUID], hard
# ## BEGIN Attribute Proposal ###

def attribute_proposals(self, event: Optional[Union[MISPEvent, int, str, UUID]] = None, pythonify: bool = False) -> Union[Dict, List[MISPShadowAttribute]]:
"""Get all the attribute proposals"""
if event:
event_id = get_uuid_or_id_from_abstract_misp(event)
r = self._prepare_request('GET', f'shadowAttributes/index/{event_id}')
Expand All @@ -535,6 +547,7 @@ def attribute_proposals(self, event: Optional[Union[MISPEvent, int, str, UUID]]
return to_return

def get_attribute_proposal(self, proposal: Union[MISPShadowAttribute, int, str, UUID], pythonify: bool = False) -> Union[Dict, MISPShadowAttribute]:
"""Get an attribute proposal"""
proposal_id = get_uuid_or_id_from_abstract_misp(proposal)
r = self._prepare_request('GET', f'shadowAttributes/view/{proposal_id}')
attribute_proposal = self._check_json_response(r)
Expand Down Expand Up @@ -1166,6 +1179,7 @@ def server_push(self, server: Union[MISPServer, int, str, UUID], event: Optional
return self._check_json_response(response)

def test_server(self, server: Union[MISPServer, int, str, UUID]) -> Dict:
"""Test if a sync link is working as expected"""
server_id = get_uuid_or_id_from_abstract_misp(server)
response = self._prepare_request('POST', f'servers/testConnection/{server_id}')
return self._check_json_response(response)
Expand Down Expand Up @@ -1409,6 +1423,7 @@ def accept_user_registration(self, registration: Union[MISPInbox, int, str, UUID
role: Optional[Union[MISPRole, int, str]] = None,
perm_sync: bool = False, perm_publish: bool = False, perm_admin: bool = False,
unsafe_fallback: bool = False):
"""Accept a user registration"""
registration_id = get_uuid_or_id_from_abstract_misp(registration)
if role:
role_id = role_id = get_uuid_or_id_from_abstract_misp(role)
Expand Down Expand Up @@ -1446,6 +1461,7 @@ def accept_user_registration(self, registration: Union[MISPInbox, int, str, UUID
return self._check_json_response(r)

def discard_user_registration(self, registration: Union[MISPInbox, int, str, UUID]):
"""Discard a user registration"""
registration_id = get_uuid_or_id_from_abstract_misp(registration)
r = self._prepare_request('POST', f'users/discardRegistrations/{registration_id}')
return self._check_json_response(r)
Expand All @@ -1468,6 +1484,7 @@ def roles(self, pythonify: bool = False) -> Union[Dict, List[MISPRole]]:
return to_return

def set_default_role(self, role: Union[MISPRole, int, str, UUID]) -> Dict:
"""Set a default role for the new user accounts"""
role_id = get_uuid_or_id_from_abstract_misp(role)
url = urljoin(self.root_url, f'/admin/roles/set_default/{role_id}')
response = self._prepare_request('POST', url)
Expand Down Expand Up @@ -1964,6 +1981,7 @@ def request_community_access(self, community: Union[MISPCommunity, int, str, UUI
message: Optional[str] = None, sync: bool = False,
anonymise_requestor_server: bool = False,
mock: bool = False) -> Dict:
"""Request the access to a community"""
community_id = get_uuid_or_id_from_abstract_misp(community)
to_post = {'org_name': requestor_organisation_name,
'org_uuid': requestor_organisation_uuid,
Expand Down Expand Up @@ -1992,11 +2010,13 @@ def event_delegations(self, pythonify: bool = False) -> Union[Dict, List[MISPEve
return to_return

def accept_event_delegation(self, delegation: Union[MISPEventDelegation, int, str], pythonify: bool = False) -> Dict:
"""Accept the delegation of an event"""
delegation_id = get_uuid_or_id_from_abstract_misp(delegation)
r = self._prepare_request('POST', f'eventDelegations/acceptDelegation/{delegation_id}')
return self._check_json_response(r)

def discard_event_delegation(self, delegation: Union[MISPEventDelegation, int, str], pythonify: bool = False) -> Dict:
"""Discard the delegation of an event"""
delegation_id = get_uuid_or_id_from_abstract_misp(delegation)
r = self._prepare_request('POST', f'eventDelegations/deleteDelegation/{delegation_id}')
return self._check_json_response(r)
Expand All @@ -2005,7 +2025,8 @@ def delegate_event(self, event: Optional[Union[MISPEvent, int, str, UUID]] = Non
organisation: Optional[Union[MISPOrganisation, int, str, UUID]] = None,
event_delegation: Optional[MISPEventDelegation] = None,
distribution: int = -1, message: str = '', pythonify: bool = False) -> Union[Dict, MISPEventDelegation]:
'''Note: distribution == -1 means recipient decides'''
'''Delegates an event.
Note: distribution == -1 means recipient decides'''
if event and organisation:
event_id = get_uuid_or_id_from_abstract_misp(event)
organisation_id = get_uuid_or_id_from_abstract_misp(organisation)
Expand Down
6 changes: 5 additions & 1 deletion pymisp/mispevent.py
Original file line number Diff line number Diff line change
Expand Up @@ -1529,7 +1529,11 @@ def from_dict(self, **kwargs):
kwargs = kwargs['Feed']
super().from_dict(**kwargs)
if hasattr(self, 'settings'):
self.settings = json.loads(self.settings)
try:
self.settings = json.loads(self.settings)
except json.decoder.JSONDecodeError as e:
logger.error("Failed to parse feed settings: {}".format(self.settings))
raise e


class MISPWarninglist(AbstractMISP):
Expand Down
10 changes: 5 additions & 5 deletions tests/testlive_comprehensive.py
Original file line number Diff line number Diff line change
Expand Up @@ -2752,11 +2752,11 @@ def missing_methods(self):
"warninglists/enableWarninglist",
"warninglists/getToggleField",
"warninglists/delete",
"admin/whitelists/add",
"admin/whitelists/index",
"admin/whitelists/edit",
"admin/whitelists/delete",
"whitelists/index"
"admin/allowedlists/add",
"admin/allowedlists/index",
"admin/allowedlists/edit",
"admin/allowedlists/delete",
"allowedlists/index"
]
missing = self.admin_misp_connector.get_all_functions(True)
with open('all_missing.json', 'w') as f:
Expand Down

0 comments on commit 50e5f15

Please sign in to comment.