def fetch_probe_result(self, probe): pr_list = [x for x in self.probe_results if x.name == probe] if len(pr_list) > 1: raise IrmaDatabaseError("Integrity error: multiple results for " "file {0} probe {1}".format(self.name, probe)) elif len(pr_list) == 0: raise IrmaDatabaseError("No result created for " "file {0} probe {1}".format(self.name, probe)) return pr_list[0]
def get_by_name(cls, name, session): try: return session.query(cls).filter(Probe.name == name).one() except NoResultFound as e: raise IrmaDatabaseResultNotFound(e) except MultipleResultsFound as e: raise IrmaDatabaseError(e)
def get_scan(cls, scan_id, user_id, session): try: return session.query(cls).filter(cls.scan_id == scan_id and cls.user_id == user_id).one() except NoResultFound as e: raise IrmaDatabaseResultNotFound(e) except MultipleResultsFound as e: raise IrmaDatabaseError(e)
def test_process_database_error(self): error = IrmaDatabaseError("message_error") with self.assertRaises(api_errors.HTTPInternalServerError) as context: try: raise error except Exception as e: api_errors.IrmaExceptionHandler.process_error(e) self.assertEqual(context.exception.title, "database_error")
def get_by_rmqvhost(session, rmqvhost=None): # FIXME: get rmq_vhost dynamically if rmqvhost is None: rmqvhost = config.brain_config['broker_frontend'].vhost try: return session.query(User).filter(User.rmqvhost == rmqvhost).one() except NoResultFound as e: raise IrmaDatabaseResultNotFound(e) except MultipleResultsFound as e: raise IrmaDatabaseError(e)
def test004_process_database_error(self, m_sys, m_abort): m_sys.exc_info.return_value = (MagicMock(), None, MagicMock()) error = IrmaDatabaseError("message_error") exp_code = 402 exp_error = api_errors.ApiError("database_error") api_errors.process_error(error) m_abort.assert_called_once() call_args = m_abort.call_args[0] self.assertEqual(call_args[0], exp_code) self.assertEqual(str(call_args[1]), str(exp_error))
def load_from_ext_id(cls, external_id, session): """Find the object in the database :param external_id: the id to look for :param session: the session to use :rtype: cls :return: the object that corresponds to the external_id :raise: IrmaDatabaseResultNotFound, IrmaDatabaseError """ try: return session.query(cls).filter( cls.external_id == external_id).one() except NoResultFound as e: raise IrmaDatabaseResultNotFound(e) except MultipleResultsFound as e: raise IrmaDatabaseError(e)
def load_from_sha256(cls, sha256, session): """Find the object in the database, update data if file was previously deleted :param sha256: the sha256 to look for :param session: the session to use :rtype: cls :return: the object that corresponds to the sha256 :raise: IrmaDatabaseResultNotFound, IrmaDatabaseError, IrmaFileSystemError """ try: asked_file = session.query(cls).filter(cls.sha256 == sha256).one() except NoResultFound as e: raise IrmaDatabaseResultNotFound(e) except MultipleResultsFound as e: raise IrmaDatabaseError(e) # Check if file is still present if asked_file.path is not None and not os.path.exists(asked_file.path): asked_file.path = None return asked_file
sys.exit(1) name = sys.argv[1] rmqvhost = sys.argv[2] ftpuser = sys.argv[3] # Auto-create directory for sqlite db db_name = os.path.abspath(config.get_sql_db_uri_params()[5]) dirname = os.path.dirname(db_name) if not os.path.exists(dirname): print("SQL directory does not exist {0}" "..creating".format(dirname)) os.makedirs(dirname) os.chmod(dirname, 0o777) elif not (os.path.isdir(dirname)): print("Error. SQL directory is a not a dir {0}" "".format(dirname)) raise IrmaDatabaseError("Can not create Brain database dir") if not os.path.exists(db_name): # touch like method to create a rw-rw-rw- file for db open(db_name, 'a').close() os.chmod(db_name, 0o666) # Retrieve database informations url = config.get_sql_url() engine = create_engine(url, echo=config.sql_debug_enabled()) # and create Database in case Base.metadata.create_all(engine) with session_transaction() as session: try: user = User.get_by_rmqvhost(session, rmqvhost=rmqvhost)
def remove_tag(self, tagid, session): asked_tag = Tag.find_by_id(tagid, session) if asked_tag not in self.tags: raise IrmaDatabaseError("Removing a not present Tag") self.tags.remove(asked_tag)
def add_tag(self, tagid, session): asked_tag = Tag.find_by_id(tagid, session) if asked_tag in self.tags: raise IrmaDatabaseError("Adding an already present Tag") self.tags.append(asked_tag)