def _get_parameters_from_request(request_parameters): query = get_query(request_parameters) recursive = get_boolean_from_request(request_parameters, 'recursive') inverted = get_boolean_from_request(request_parameters, 'inverted') offset, limit = get_paging(request.args) if recursive and not query: raise ValueError( 'Recursive search is only permissible with non-empty query') if inverted and not recursive: raise ValueError('Inverted flag can only be used with recursive') return query, recursive, inverted, offset, limit
def get(self, uid): ''' The uid of the file_object in question has to be given in the url The return format will be {"binary": b64_encoded_binary, "file_name": file_name} ''' with ConnectTo(FrontEndDbInterface, self.config) as db_service: existence = db_service.existence_quick_check(uid) if not existence: return error_message('No firmware with UID {} found in database'.format(uid), self.URL, request_data={'uid': uid}, return_code=404) try: tar_flag = get_boolean_from_request(request.args, 'tar') except ValueError as value_error: return error_message(str(value_error), self.URL, request_data=dict(uid=uid, tar=request.args.get('tar'))) with ConnectTo(InterComFrontEndBinding, self.config) as intercom: if not tar_flag: binary, file_name = intercom.get_binary_and_filename(uid) else: binary, file_name = intercom.get_repacked_binary_and_file_name(uid) response = { 'binary': standard_b64encode(binary).decode(), 'file_name': file_name, 'SHA256': get_sha256(binary) } return success_message(response, self.URL, request_data={'uid': uid, 'tar': tar_flag})
def test_get_boolean_from_request(): assert not get_boolean_from_request(None, 'flag') with pytest.raises(ValueError): get_boolean_from_request(dict(flag='bad_string'), 'flag') with pytest.raises(ValueError): get_boolean_from_request(dict(flag='2'), 'flag') no_flag = get_boolean_from_request(dict(), 'flag') assert not no_flag false_result = get_boolean_from_request(dict(flag='false'), 'flag') assert not false_result good_result = get_boolean_from_request(dict(flag='true'), 'flag') assert good_result
def _get_with_uid(self, uid): summary = get_boolean_from_request(request.args, 'summary') if summary: with ConnectTo(FrontEndDbInterface, self.config) as connection: firmware = connection.get_complete_object_including_all_summaries( uid) else: with ConnectTo(FrontEndDbInterface, self.config) as connection: firmware = connection.get_firmware(uid) if not firmware or not isinstance(firmware, Firmware): return error_message('No firmware with UID {} found'.format(uid), self.URL, dict(uid=uid)) fitted_firmware = self._fit_firmware(firmware) return success_message(dict(firmware=fitted_firmware), self.URL, request_data=dict(uid=uid))
def get(self, uid): ''' Request a specific firmware Get the analysis results of a specific firmware by providing the corresponding uid ''' summary = get_boolean_from_request(request.args, 'summary') if summary: with ConnectTo(FrontEndDbInterface, self.config) as connection: firmware = connection.get_complete_object_including_all_summaries( uid) else: with ConnectTo(FrontEndDbInterface, self.config) as connection: firmware = connection.get_firmware(uid) if not firmware or not isinstance(firmware, Firmware): return error_message(f'No firmware with UID {uid} found', self.URL, dict(uid=uid)) fitted_firmware = self._fit_firmware(firmware) return success_message(dict(firmware=fitted_firmware), self.URL, request_data=dict(uid=uid))