def put(self, uid=None): if not uid: try: data = convert_rest_request(request.data) except TypeError as type_error: return error_message(str(type_error), self.URL, request_data=request.data) result = self._process_data(data) if 'error_message' in result: logging.warning( 'Submission not according to API guidelines! (data could not be parsed)' ) return error_message(result['error_message'], self.URL, request_data=data) logging.debug('Upload Successful!') return success_message(result, self.URL, request_data=data) else: try: update = get_update(request.args) except ValueError as value_error: return error_message(str(value_error), self.URL, request_data={'uid': uid}) return self._update_analysis(uid, update)
def put(self): ''' The request data should have the form {"uid_list": uid_list, "<optional>redo": True} return value: the result dict from the compare ''' try: data = convert_rest_request(request.data) except TypeError as type_error: return error_message(str(type_error), self.URL, request_data=request.data) try: uid_string = ';'.join(data['uid_list']) compare_id = normalize_compare_id(uid_string) redo = data.get('redo', False) except (AttributeError, TypeError, KeyError): return error_message( 'Request should be of the form {"uid_list": uid_list, "redo": boolean}', self.URL, request_data=data) with ConnectTo(CompareDbInterface, self.config) as db_compare_service: if not db_compare_service.compare_result_is_in_db( compare_id) or redo: return self.start_compare(db_compare_service, compare_id, data, redo) return error_message( 'Compare already exists. Use "redo" to force re-compare.', self.URL, request_data=data, return_code=200)
def post(self): ''' The request data should have the form {"rule_file": rule_string, 'uid': firmware_uid} The uid parameter is optional and can be specified if the user want's to search in the files of a single firmware. rule_string can be something like "rule rule_name {strings: $a = \"foobar\" condition: $a}" ''' try: data = convert_rest_request(request.data) yara_rules = self._get_yara_rules(data) uid = self._get_firmware_uid(data) except TypeError as type_error: return error_message(str(type_error), self.URL, request_data=request.data) except RestBinarySearchException as exception: return error_message(exception.get_message(), self.URL, request_data=request.data) with ConnectTo(InterComFrontEndBinding, self.config) as intercom: search_id = intercom.add_binary_search_request(yara_rules, uid) return success_message( { 'message': 'Started binary search. Please use GET and the search_id to get the results' }, self.URL, request_data={'search_id': search_id})
def put(self): ''' The request data should have the form {"uid_list": uid_list, "<optional>redo": True} return value: the result dict from the compare ''' try: data = convert_rest_request(request.data) except TypeError as type_error: return error_message(str(type_error), self.URL, request_data=request.data) try: uid_string = ';'.join(data['uid_list']) compare_id = unify_string_list(uid_string) if 'redo' in data.keys(): redo = data['redo'] else: redo = False except Exception: # FIXME Please specify Exception types - would think at least TypeError might occur return error_message('Request should be of the form {"uid_list": uid_list, "redo": boolean}', self.URL, request_data=data) with ConnectTo(CompareDbInterface, self.config) as db_compare_service: if not db_compare_service.compare_result_is_in_db(compare_id) or redo: err = db_compare_service.object_existence_quick_check(compare_id) if err is not None: return error_message(err, self.URL, request_data=data, return_code=404) with ConnectTo(InterComFrontEndBinding, self.config) as intercom: intercom.add_compare_task(compare_id, force=redo) return success_message({'message': 'Compare started. Please use GET to get the results.'}, self.URL, request_data=data, return_code=202) return error_message('Compare already exists. Use "redo" to force re-compare.', self.URL, request_data=data, return_code=200)
def put(self): ''' The request data should have the form {"uid_list": uid_list, "<optional>redo": True} return value: the result dict from the compare ''' try: data = convert_rest_request(request.data) except TypeError as type_error: return error_message(str(type_error), self.URL, request_data=request.data) try: uid_string = ';'.join(data['uid_list']) compare_id = normalize_compare_id(uid_string) redo = data.get('redo', False) except (AttributeError, TypeError, KeyError): return error_message( 'Request should be of the form {"uid_list": uid_list, "redo": boolean}', self.URL, request_data=data) with ConnectTo(CompareDbInterface, self.config) as db_compare_service: if not db_compare_service.compare_result_is_in_db( compare_id) or redo: try: db_compare_service.check_objects_exist(compare_id) except FactCompareException as exception: return error_message(exception.get_message(), self.URL, request_data=data, return_code=404) with ConnectTo(InterComFrontEndBinding, self.config) as intercom: intercom.add_compare_task(compare_id, force=redo) return success_message( { 'message': 'Compare started. Please use GET to get the results.' }, self.URL, request_data=data, return_code=202) return error_message( 'Compare already exists. Use "redo" to force re-compare.', self.URL, request_data=data, return_code=200)
def _put_without_uid(self): try: data = convert_rest_request(request.data) except TypeError as type_error: return error_message(str(type_error), self.URL, request_data=request.data) result = self._process_data(data) if 'error_message' in result: logging.warning( 'Submission not according to API guidelines! (data could not be parsed)' ) return error_message(result['error_message'], self.URL, request_data=data) logging.debug('Upload Successful!') return success_message(result, self.URL, request_data=data)
def test_convert_rest_request_succeeds(data): assert isinstance(convert_rest_request(data), dict)
def test_convert_rest_request_fails(data): with pytest.raises(TypeError): convert_rest_request(data)