예제 #1
0
class User(Base, SQLDatabaseObject):
    __tablename__ = '{0}user'.format(tables_prefix)

    # SQLite fix for auto increment on ids
    # see http://docs.sqlalchemy.org/en/latest/dialects/sqlite.html
    if config.get_sql_db_uri_params()[0] == 'sqlite':
        __table_args__ = {'sqlite_autoincrement': True}

    # Fields
    id = Column(Integer,
                autoincrement=True,
                nullable=False,
                primary_key=True,
                name='id')
    name = Column(String, nullable=False, name='name')
    rmqvhost = Column(String, index=True, nullable=False, name='rmqvhost')
    ftpuser = Column(String, nullable=False, name='ftpuser')
    scans = relationship("Scan", backref="user")

    def __init__(self, name, rmqvhost, ftpuser):
        self.name = name
        self.rmqvhost = rmqvhost
        self.ftpuser = ftpuser

    @staticmethod
    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)
예제 #2
0
파일: sql.py 프로젝트: dewiestr/irma-brain
def sql_db_connect():
    """Connection to DB
    """
    try:
        uri_params = config.get_sql_db_uri_params()
        # TODO args* style argument
        SQLDatabase.connect(uri_params[0], uri_params[1], uri_params[2],
                            uri_params[3], uri_params[4], uri_params[5])
    except Exception as e:
        msg = "SQL: can't connect"
        log.info("msg", exc_info=True)
        raise IrmaDatabaseError(msg)
예제 #3
0
파일: sql.py 프로젝트: deloittem/irma-brain
def sql_db_connect():
    """Connection to DB
    """
    try:
        uri_params = config.get_sql_db_uri_params()
        # TODO args* style argument
        SQLDatabase.connect(uri_params[0], uri_params[1], uri_params[2],
                            uri_params[3], uri_params[4], uri_params[5],
                            debug=config.sql_debug_enabled())
    except Exception as e:
        log.exception(e)
        msg = "SQL: can't connect"
        raise IrmaDatabaseError(msg)
예제 #4
0
class Scan(Base, SQLDatabaseObject):
    __tablename__ = '{0}scan'.format(tables_prefix)
    # SQLite fix for auto increment on ids
    # see http://docs.sqlalchemy.org/en/latest/dialects/sqlite.html
    if config.get_sql_db_uri_params()[0] == 'sqlite':
        __table_args__ = {'sqlite_autoincrement': True}

    # Fields
    id = Column(Integer,
                autoincrement=True,
                nullable=False,
                primary_key=True,
                name='id')
    scan_id = Column(String, index=True, nullable=False, name='scan_id')
    status = Column(Integer, nullable=False, name='status')
    timestamp = Column(Float(precision=2), nullable=False, name='timestamp')
    # Many to one Scan <-> User
    user_id = Column(
        Integer,
        ForeignKey('{0}user.id'.format(tables_prefix)),
        index=True,
        nullable=False,
    )
    jobs = relationship("Job", backref="scan", lazy='subquery')

    def __init__(self, frontend_scanid, user_id):
        self.scan_id = frontend_scanid
        self.status = IrmaScanStatus.empty
        self.timestamp = timestamp()
        self.user_id = user_id

    @property
    def files(self):
        return set(job.filename for job in self.jobs)

    @property
    def nb_files(self):
        return len(self.files)

    @classmethod
    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)
예제 #5
0
class Probe(Base, SQLDatabaseObject):
    __tablename__ = '{0}probe'.format(tables_prefix)

    # SQLite fix for auto increment on ids
    # see http://docs.sqlalchemy.org/en/latest/dialects/sqlite.html
    if config.get_sql_db_uri_params()[0] == 'sqlite':
        __table_args__ = {'sqlite_autoincrement': True}

    # Fields
    id = Column(Integer,
                autoincrement=True,
                nullable=False,
                primary_key=True,
                name='id')
    name = Column(String, nullable=False, index=True, name='name')
    display_name = Column(String,
                          nullable=False,
                          index=True,
                          name='display_name')
    category = Column(String, nullable=False, name='category')
    mimetype_regexp = Column(String, name='mimetype_regexp')
    online = Column(Boolean, name='online')

    def __init__(self, name, display_name, category, mimetype_regexp, online):
        self.name = name
        self.display_name = display_name
        self.category = category
        self.mimetype_regexp = mimetype_regexp
        self.online = online

    @classmethod
    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)

    @classmethod
    def all(cls, session):
        return session.query(cls).all()
예제 #6
0
if len(sys.argv) not in (4, 5):
    print("usage: {0} <username> <rmqvhost> <ftpuser>\n"
          "      with <username> a string\n"
          "           <rmqvhost> the rmqvhost used for the frontend\n"
          "           <ftpuser> the ftpuser used by the frontend\n"
          "example: {0} test1 mqfrontend frontend"
          "".format(sys.argv[0]))
    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
예제 #7
0
import config.parser as config
from sqlalchemy.engine import Engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
from lib.common.utils import UUID
from lib.irma.common.exceptions import IrmaDatabaseError, \
    IrmaDatabaseResultNotFound
from lib.irma.common.utils import IrmaScanStatus
from lib.common.compat import timestamp
from lib.irma.database.sqlhandler import SQLDatabase
from lib.irma.database.sqlobjects import SQLDatabaseObject

# SQLite fix for ForeignKey support
# see http://docs.sqlalchemy.org/en/latest/dialects/sqlite.html
if config.get_sql_db_uri_params()[0] == 'sqlite':

    @event.listens_for(Engine, "connect")
    def set_sqlite_pragma(dbapi_connection, connection_record):
        cursor = dbapi_connection.cursor()
        cursor.execute("PRAGMA foreign_keys=ON")
        cursor.close()


Base = declarative_base()
tables_prefix = '{0}_'.format(config.get_sql_db_tables_prefix())


class Scan(Base, SQLDatabaseObject):
    __tablename__ = '{0}scan'.format(tables_prefix)
    # SQLite fix for auto increment on ids
예제 #8
0
# of this distribution and at:
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# No part of the project, including this file, may be copied,
# modified, propagated, or distributed except according to the
# terms contained in the LICENSE file.

import config.parser as config
from sqlalchemy import create_engine


def generate_url(dbms, dialect, username, passwd, host, dbname):
    if dialect:
        dbms = "{0}+{1}".format(dbms, dialect)
    host_and_id = ''
    if host and username:
        if passwd:
            host_and_id = "{0}:{1}@{2}".format(username, passwd, host)
        else:
            host_and_id = "{0}@{1}".format(username, host)
    return "{0}://{1}/{2}".format(dbms, host_and_id, dbname)


# Retrieve database informations
uri_params = config.get_sql_db_uri_params()
url = generate_url(uri_params[0], uri_params[1], uri_params[2], uri_params[3],
                   uri_params[4], uri_params[5])

engine = create_engine(url, echo=config.sql_debug_enabled())
예제 #9
0
from sqlalchemy import Column, Integer, Float, String, event, ForeignKey
import config.parser as config
from sqlalchemy.engine import Engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
from lib.irma.common.exceptions import IrmaDatabaseError, IrmaDatabaseResultNotFound
from lib.irma.common.utils import IrmaScanStatus
from lib.common.compat import timestamp
from lib.irma.database.sqlhandler import SQLDatabase
from lib.irma.database.sqlobjects import SQLDatabaseObject


# SQLite fix for ForeignKey support
# see http://docs.sqlalchemy.org/en/latest/dialects/sqlite.html
if config.get_sql_db_uri_params()[0] == "sqlite":

    @event.listens_for(Engine, "connect")
    def set_sqlite_pragma(dbapi_connection, connection_record):
        cursor = dbapi_connection.cursor()
        cursor.execute("PRAGMA foreign_keys=ON")
        cursor.close()

    # 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, 0777)
    elif not (os.path.isdir(dirname)):
예제 #10
0
if len(sys.argv) not in (4, 5):
    print("usage: {0} <username> <rmqvhost> <ftpuser>\n"
          "      with <username> a string\n"
          "           <rmqvhost> the rmqvhost used for the frontend\n"
          "           <ftpuser> the ftpuser used by the frontend\n"
          "example: {0} test1 mqfrontend frontend"
          "".format(sys.argv[0]))
    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, 0777)
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, 0666)
예제 #11
0
from sqlalchemy.engine import Engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
from lib.common.utils import UUID
from lib.irma.common.exceptions import IrmaDatabaseError, \
    IrmaDatabaseResultNotFound
from lib.irma.common.utils import IrmaScanStatus
from lib.common.compat import timestamp
from lib.irma.database.sqlhandler import SQLDatabase
from lib.irma.database.sqlobjects import SQLDatabaseObject


# SQLite fix for ForeignKey support
# see http://docs.sqlalchemy.org/en/latest/dialects/sqlite.html
if config.get_sql_db_uri_params()[0] == 'sqlite':
    @event.listens_for(Engine, "connect")
    def set_sqlite_pragma(dbapi_connection, connection_record):
        cursor = dbapi_connection.cursor()
        cursor.execute("PRAGMA foreign_keys=ON")
        cursor.close()

Base = declarative_base()
tables_prefix = '{0}_'.format(config.get_sql_db_tables_prefix())


class Scan(Base, SQLDatabaseObject):
    __tablename__ = '{0}scan'.format(tables_prefix)
    # SQLite fix for auto increment on ids
    # see http://docs.sqlalchemy.org/en/latest/dialects/sqlite.html
    if config.get_sql_db_uri_params()[0] == 'sqlite':