Esempio n. 1
0
def database(app, request):
    """Session-wide test database."""

    def teardown():
        try:
            db.engine.execute('DROP TABLE vulnerability CASCADE')
        except Exception:
            pass
        try:
            db.engine.execute('DROP TABLE vulnerability_template CASCADE')
        except Exception:
            pass
        db.drop_all()

    # Disable check_vulnerability_host_service_source_code constraint because
    # it doesn't work in sqlite
    vuln_constraints = db.metadata.tables['vulnerability'].constraints
    vuln_constraints.remove(next(
        constraint for constraint in vuln_constraints
        if constraint.name == 'check_vulnerability_host_service_source_code'))

    db.app = app
    db.create_all()

    request.addfinalizer(teardown)
    return db
Esempio n. 2
0
 def _create_tables(self, conn_string):
     print('Creating tables')
     from faraday.server.models import db
     current_app.config['SQLALCHEMY_DATABASE_URI'] = conn_string
     try:
         db.create_all()
     except OperationalError as ex:
         if 'could not connect to server' in ex.message:
             print(
                 'ERROR: {red}PostgreSQL service{white} is not running. Please verify that it is running in port 5432 before executing setup script.'
                 .format(red=Fore.RED, white=Fore.WHITE))
             sys.exit(1)
         elif 'password authentication failed' in ex.message:
             print('ERROR: ')
             sys.exit(1)
         else:
             raise
     except ProgrammingError as ex:
         print(ex)
         print('Please check postgres user permissions.')
         sys.exit(1)
     except ImportError as ex:
         if 'psycopg2' in ex:
             print(
                 'ERROR: Missing python depency {red}psycopg2{white}. Please install it with {blue}pip install psycopg2'
                 .format(red=Fore.RED, white=Fore.WHITE, blue=Fore.BLUE))
             sys.exit(1)
         else:
             raise
     else:
         from alembic.config import Config
         from alembic import command
         alembic_cfg = Config(os.path.join(FARADAY_BASE, 'alembic.ini'))
         os.chdir(FARADAY_BASE)
         command.stamp(alembic_cfg, "head")
Esempio n. 3
0
    def _create_tables(self, conn_string):
        print('Creating tables')
        from faraday.server.models import db  # pylint:disable=import-outside-toplevel
        current_app.config['SQLALCHEMY_DATABASE_URI'] = conn_string

        # Check if the alembic_version exists
        # Taken from https://stackoverflow.com/a/24089729
        (result, ) = list(
            db.session.execute("select to_regclass('alembic_version')"))
        exists = result[0] is not None

        if exists:
            print(
                "Faraday tables already exist in the database. No tables will "
                "be created. If you want to ugprade the schema to the latest "
                "version, you should run \"faraday-manage migrate\".")
            return

        try:
            db.create_all()
        except OperationalError as ex:
            if 'could not connect to server' in str(ex):
                print(
                    f'ERROR: {Fore.RED}PostgreSQL service{Fore.WHITE} is not running. Please verify that it is running in port 5432 before executing setup script.'
                )
                sys.exit(1)
            elif 'password authentication failed' in str(ex):
                print('ERROR: ')
                sys.exit(1)
            else:
                raise
        except ProgrammingError as ex:
            print(ex)
            print('Please check postgres user permissions.')
            sys.exit(1)
        except ImportError as ex:
            if 'psycopg2' in str(ex):
                print(
                    f'ERROR: Missing python depency {Fore.RED}psycopg2{Fore.WHITE}. Please install it with {Fore.BLUE}pip install psycopg2'
                )
                sys.exit(1)
            else:
                raise
        else:
            alembic_cfg = Config(FARADAY_BASE / 'alembic.ini')
            os.chdir(FARADAY_BASE)
            command.stamp(alembic_cfg, "head")
            # TODO ADD RETURN TO PREV DIR
        self._create_roles(conn_string)
        self._create_initial_notifications_config()
Esempio n. 4
0
    def _create_tables(self, conn_string):
        print('Creating tables')
        from faraday.server.models import db
        current_app.config['SQLALCHEMY_DATABASE_URI'] = conn_string

        # Check if the alembic_version exists
        # Taken from https://stackoverflow.com/a/24089729
        (result, ) = list(
            db.session.execute("select to_regclass('alembic_version')"))
        exists = result[0] is not None

        if exists:
            print(
                "Faraday tables already exist in the database. No tables will "
                "be created. If you want to ugprade the schema to the latest "
                "version, you should run \"faraday-manage migrate\".")
            return

        try:
            db.create_all()
        except OperationalError as ex:
            if 'could not connect to server' in ex.message:
                print(
                    'ERROR: {red}PostgreSQL service{white} is not running. Please verify that it is running in port 5432 before executing setup script.'
                    .format(red=Fore.RED, white=Fore.WHITE))
                sys.exit(1)
            elif 'password authentication failed' in ex.message:
                print('ERROR: ')
                sys.exit(1)
            else:
                raise
        except ProgrammingError as ex:
            print(ex)
            print('Please check postgres user permissions.')
            sys.exit(1)
        except ImportError as ex:
            if 'psycopg2' in ex:
                print(
                    'ERROR: Missing python depency {red}psycopg2{white}. Please install it with {blue}pip install psycopg2'
                    .format(red=Fore.RED, white=Fore.WHITE, blue=Fore.BLUE))
                sys.exit(1)
            else:
                raise
        else:
            from alembic.config import Config
            from alembic import command
            alembic_cfg = Config(os.path.join(FARADAY_BASE, 'alembic.ini'))
            os.chdir(FARADAY_BASE)
            command.stamp(alembic_cfg, "head")