示例#1
0
文件: models.py 项目: yehias/irma
 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]
示例#2
0
 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("whatever", "whatever", e)
     self.assertEqual(context.exception.title, "database_error")
示例#3
0
文件: sqlobjects.py 项目: yehias/irma
 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)
示例#4
0
文件: sqlobjects.py 项目: yehias/irma
 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)
示例#5
0
文件: sqlobjects.py 项目: yehias/irma
 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)
示例#6
0
 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:
         query = cls.query_joined(session)
         return query.filter(cls.external_id == str(external_id)).one()
     except NoResultFound as e:
         raise IrmaDatabaseResultNotFound(e)
     except MultipleResultsFound as e:
         raise IrmaDatabaseError(e)
示例#7
0
文件: models.py 项目: yehias/irma
    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:
            filext_plus_cls = with_polymorphic(FileExt, [cls])
            return session.query(filext_plus_cls).filter(
                cls.external_id == str(external_id)).one()
        except NoResultFound as e:
            raise IrmaDatabaseResultNotFound(e)
        except MultipleResultsFound as e:
            raise IrmaDatabaseError(e)
示例#8
0
文件: models.py 项目: yehias/irma
 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
示例#9
0
 def create(cls, fileobj, sha256, path, session):
     try:
         # Create a new file
         time = compat.timestamp()
         sha1 = sha1sum(fileobj)
         md5 = md5sum(fileobj)
         # determine file mimetype
         magic = Magic()
         # magic only deal with buffer
         # feed it with a 4MB buffer
         mimetype = magic.from_buffer(fileobj.read(2 ** 22))
         size = save_to_file(fileobj, path)
         log.debug("not present, saving, sha256 %s sha1 %s"
                   "md5 %s size %s mimetype: %s",
                   sha256, sha1, md5, size, mimetype)
         file = File(sha256, sha1, md5, size, mimetype, path, time, time)
         session.add(file)
         session.commit()
         return file
     except IntegrityError:
         raise IrmaDatabaseError("Integrity error")
示例#10
0
文件: models.py 项目: yehias/irma
 def create(cls, fileobj, sha256, path, session):
     try:
         # Create a new file
         time = compat.timestamp()
         sha1 = sha1sum(fileobj)
         md5 = md5sum(fileobj)
         # determine file mimetype
         magic = Magic()
         # magic only deal with buffer
         # feed it with a 4MB buffer
         mimetype = magic.from_buffer(fileobj.read(2**22))
         size = save_to_file(fileobj, path)
         log.debug(
             "not present, saving, sha256 %s sha1 %s"
             "md5 %s size %s mimetype: %s", sha256, sha1, md5, size,
             mimetype)
         file = File(sha256, sha1, md5, size, mimetype, path, time, time)
         session.add(file)
         session.commit()
         return file
     except IntegrityError as e:
         try:
             session.rollback()
             file = File.load_from_sha256(sha256, session)
             log.debug("Integrity error but load "
                       "successful for file.%s", sha256)
             return file
         except NoResultFound:
             log.debug("Integrity error load failed for file.%s", sha256)
             pass
         log.error(
             "Database integrity error: refuse to insert file.%s. %s",
             sha256,
             e,
         )
         raise IrmaDatabaseError("Integrity error")
示例#11
0
 def remove_tag(self, tagid, session):
     asked_tag = session.query(Tag).filter_by(id=tagid).one()
     if asked_tag not in self.tags:
         raise IrmaDatabaseError("Removing a not present Tag")
     self.tags.remove(asked_tag)
示例#12
0
 def add_tag(self, tagid, session):
     asked_tag = session.query(Tag).filter_by(id=tagid).one()
     if asked_tag in self.tags:
         raise IrmaDatabaseError("Adding an already present Tag")
     self.tags.append(asked_tag)
示例#13
0
name = sys.argv[1]
rmqvhost = sys.argv[2]
ftpuser = sys.argv[3]

# Auto-create directory for sqlite db
db_name = os.path.abspath(config.sqldb.dbname)
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
engine = create_engine(config.sqldb.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)
        print("rmqvhost {0} is already assigned to user {1}. "