def check_postgres(): with app.app_context(): try: result = str(db.engine.execute("SELECT version()")) return result except sqlalchemy.exc.OperationalError: return None
def app(request): """The Flask API (scope = Session).""" config.DB_NAME = DB_NAME DATABASE_URI = config.DATABASE_URI.format(**config.__dict__) if not database_exists(DATABASE_URI): create_database(DATABASE_URI) print "Test Database: %s" % DATABASE_URI # Config the app _app.config["SQLALCHEMY_DATABASE_URI"] = DATABASE_URI _app.config["SQLALCHEMY_ECHO"] = True # Toggle SQL Alchemy output _app.config["DEBUG"] = True _app.config["TESTING"] = True # Establish an application context before running the tests. ctx = _app.app_context() ctx.push() # Initialize a null cache cache.config = {} cache.init_app(_app) def teardown(): ctx.pop() request.addfinalizer(teardown) return _app
def check_locks_postgresql(): with app.app_context(): psql_status = check_postgres() if psql_status: result = db.engine.execute("""SELECT blocked_locks.pid AS blocked_pid, blocked_activity.usename AS blocked_user, blocking_locks.pid AS blocking_pid, blocking_activity.usename AS blocking_user, blocked_activity.query AS blocked_statement, blocking_activity.query AS current_statement_in_blocking_process FROM pg_catalog.pg_locks blocked_locks JOIN pg_catalog.pg_stat_activity blocked_activity ON blocked_activity.pid = blocked_locks.pid JOIN pg_catalog.pg_locks blocking_locks ON blocking_locks.locktype = blocked_locks.locktype AND blocking_locks.DATABASE IS NOT DISTINCT FROM blocked_locks.DATABASE AND blocking_locks.relation IS NOT DISTINCT FROM blocked_locks.relation AND blocking_locks.page IS NOT DISTINCT FROM blocked_locks.page AND blocking_locks.tuple IS NOT DISTINCT FROM blocked_locks.tuple AND blocking_locks.virtualxid IS NOT DISTINCT FROM blocked_locks.virtualxid AND blocking_locks.transactionid IS NOT DISTINCT FROM blocked_locks.transactionid AND blocking_locks.classid IS NOT DISTINCT FROM blocked_locks.classid AND blocking_locks.objid IS NOT DISTINCT FROM blocked_locks.objid AND blocking_locks.objsubid IS NOT DISTINCT FROM blocked_locks.objsubid AND blocking_locks.pid != blocked_locks.pid JOIN pg_catalog.pg_stat_activity blocking_activity ON blocking_activity.pid = blocking_locks.pid WHERE NOT blocked_locks.GRANTED;""") fetch = result.fetchall() if fetch: return True else: return False else: return None
def process_reports(debug, workspace, polling): try: from requests import ConnectionError except ImportError: print( 'Python requests was not found. Please install it with: pip install requests' ) sys.exit(1) try: from sqlalchemy.exc import OperationalError except ImportError: print( 'SQLAlchemy was not found please install it with: pip install sqlalchemy' ) sys.exit(1) setUpLogger(debug) configuration = _conf() url = '{0}/_api/v2/info'.format( configuration.getServerURI() if FARADAY_UP else SERVER_URL) with app.app_context(): try: check_faraday_server(url) import_external_reports(workspace, polling) except OperationalError as ex: print('{0}'.format(ex)) print( 'Please verify database is running or configuration on server.ini!' ) except ConnectionError: print( 'Can\'t connect to {0}. Please check if the server is running.' .format(url))
def initdb(choose_password): with app.app_context(): InitDB().run(choose_password=choose_password) couchdb_config_present = server.config.couchdb if couchdb_config_present and couchdb_config_present.user and couchdb_config_present.password: print('Importing data from CouchDB, please wait...') ImportCouchDB().run() print('All users from CouchDB were imported. You can login with your old username/password to faraday now.')
def check_postgresql_encoding(): with app.app_context(): psql_status = check_postgres() if psql_status: encoding = db.engine.execute("SHOW SERVER_ENCODING").first()[0] return encoding else: return None
def check_postgres(): with app.app_context(): try: result = (str(db.session.query("version()").one()),db.session.query("current_setting('server_version_num')").one()) return result except sqlalchemy.exc.OperationalError: return False except sqlalchemy.exc.ArgumentError: return None
def check_postgres(): with app.app_context(): try: result = str(db.engine.execute("SELECT version()")) return result except sqlalchemy.exc.OperationalError: return False except sqlalchemy.exc.ArgumentError: return None
def create_tables(): with app.app_context(): # Ugly hack to create tables and also setting alembic revision import server.config conn_string = server.config.database.connection_string from server.commands.initdb import InitDB InitDB()._create_tables(conn_string) click.echo( click.style('Tables created successfully!', fg='green', bold=True))
def changes_password(username, password): with app.app_context(): user = User.query.filter_by(username=username).first() if user: user.password = password db.session.add(user) db.session.commit() print "Password changed succesfully" else: print "User not found in Faraday's Database"
def create_tables(): with app.app_context(): # Ugly hack to create tables and also setting alembic revision import server.config conn_string = server.config.database.connection_string from server.commands.initdb import InitDB InitDB()._create_tables(conn_string) click.echo(click.style( 'Tables created successfully!', fg='green', bold=True))
def initdb(choose_password): with app.app_context(): InitDB().run(choose_password=choose_password) couchdb_config_present = server.config.couchdb if couchdb_config_present and couchdb_config_present.user and couchdb_config_present.password: print('Importing data from CouchDB, please wait...') ImportCouchDB().run() print( 'All users from CouchDB were imported. You can login with your old username/password to faraday now.' )
def check_postgres(): with app.app_context(): try: result = (str(db.session.query("version()").one()), db.session.query( "current_setting('server_version_num')").one()) return result except sqlalchemy.exc.OperationalError: return False except sqlalchemy.exc.ArgumentError: return None
def check_postgresql(): with app.app_context(): try: if not db.session.query(Workspace).count(): logger.warn('No workspaces found. Remeber to execute couchdb importer') except sqlalchemy.exc.ArgumentError: logger.error( '\n\b{RED}Please check you postgresql connection string in server.ini at .faraday on your ohme directory.{WHITE} \n'.format(RED=Fore.RED, WHITE=Fore.WHITE) ) sys.exit(1) except sqlalchemy.exc.OperationalError: logger.error( '\n\n{RED}Could not connect to postgresql.\n{WHITE}Please check: \n{YELLOW} * if database is running \n * configuration settings are correct. \n\n{WHITE}For first time installations execute{WHITE}: \n\n {GREEN} python manage.py initdb\n\n'.format(GREEN=Fore.GREEN, YELLOW=Fore.YELLOW, WHITE=Fore.WHITE, RED=Fore.RED)) sys.exit(1)
def check_postgresql(): with app.app_context(): try: if not db.session.query(Workspace).count(): logger.warn('No workspaces found. Remember to execute CouchDB importer') except sqlalchemy.exc.ArgumentError: logger.error( '\n\b{RED}Please check your PostgreSQL connection string in the file ~/.faraday/config/server.ini on your home directory.{WHITE} \n'.format(RED=Fore.RED, WHITE=Fore.WHITE) ) sys.exit(1) except sqlalchemy.exc.OperationalError: logger.error( '\n\n{RED}Could not connect to PostgreSQL.\n{WHITE}Please check: \n{YELLOW} * if database is running \n * configuration settings are correct. \n\n{WHITE}For first time installations execute{WHITE}: \n\n {GREEN} python manage.py initdb\n\n'.format(GREEN=Fore.GREEN, YELLOW=Fore.YELLOW, WHITE=Fore.WHITE, RED=Fore.RED)) sys.exit(1)
def createsuperuser(username, email, password): with app.app_context(): if db.session.query(User).filter_by(active=True).count() > 0: print("Can't create more users. Please contact support") sys.exit(1) app.user_datastore.create_user(username=username, email=email, password=password, role='admin', is_ldap=False) db.session.commit() click.echo( click.style('User {} created successfully!'.format(username), fg='green', bold=True))
def check_alembic_version(): config = Config() config.set_main_option("script_location", "migrations") script = ScriptDirectory.from_config(config) head_revision = script.get_current_head() with app.app_context(): conn = db.session.connection() context = MigrationContext.configure(conn) if head_revision != context.get_current_revision(): print('--' * 20) print('Missing migrations, please execute: \n\n') print('python manage.py migrate') sys.exit(1)
def create_superuser(username, email, password): with app.app_context(): if db.session.query(User).filter_by(active=True).count() > 0: print("Can't create more users. The comumunity edition only allows one user. Please contact support for further information.") sys.exit(1) app.user_datastore.create_user(username=username, email=email, password=password, role='admin', is_ldap=False) db.session.commit() click.echo(click.style( 'User {} created successfully!'.format(username), fg='green', bold=True))
def run_migrations_online(): """Run migrations in 'online' mode. In this scenario we need to create an Engine and associate a connection with the context. """ with app.app_context(): connectable = db.engine with connectable.connect() as connection: context.configure(connection=connection, target_metadata=target_metadata) with context.begin_transaction(): context.run_migrations()
def run_migrations_online(): """Run migrations in 'online' mode. In this scenario we need to create an Engine and associate a connection with the context. """ with app.app_context(): connectable = db.engine with connectable.connect() as connection: context.configure( connection=connection, target_metadata=target_metadata ) with context.begin_transaction(): context.run_migrations()
def onMessage(self, payload, is_binary): from server.web import app """ We only support JOIN and LEAVE workspace messages. When authentication is implemented we need to verify that the user can join the selected workspace. When authentication is implemented we need to reply the client if the join failed. """ if not is_binary: message = json.loads(payload) if message['action'] == 'JOIN_WORKSPACE': if 'workspace' not in message or 'token' not in message: logger.warning('Invalid join workspace message: ' '{}'.format(message)) self.sendClose() return signer = itsdangerous.TimestampSigner(app.config['SECRET_KEY'], salt="websocket") try: workspace_id = signer.unsign(message['token'], max_age=60) except itsdangerous.BadData as e: self.sendClose() logger.warning('Invalid websocket token for workspace ' '{}'.format(message['workspace'])) logger.exception(e) else: with app.app_context(): workspace = Workspace.query.get(int(workspace_id)) if workspace.name != message['workspace']: logger.warning( 'Trying to join workspace {} with token of ' 'workspace {}. Rejecting.'.format( message['workspace'], workspace.name )) self.sendClose() else: self.factory.join_workspace( self, message['workspace']) if message['action'] == 'LEAVE_WORKSPACE': self.factory.leave_workspace(self, message['workspace'])
def onMessage(self, payload, is_binary): from server.web import app """ We only support JOIN and LEAVE workspace messages. When authentication is implemented we need to verify that the user can join the selected workspace. When authentication is implemented we need to reply the client if the join failed. """ if not is_binary: message = json.loads(payload) if message['action'] == 'JOIN_WORKSPACE': if 'workspace' not in message or 'token' not in message: logger.warning('Invalid join workspace message: ' '{}'.format(message)) self.sendClose() return signer = itsdangerous.TimestampSigner(app.config['SECRET_KEY'], salt="websocket") try: workspace_id = signer.unsign(message['token'], max_age=60) except itsdangerous.BadData as e: self.sendClose() logger.warning('Invalid websocket token for workspace ' '{}'.format(message['workspace'])) logger.exception(e) else: with app.app_context(): workspace = Workspace.query.get(int(workspace_id)) if workspace.name != message['workspace']: logger.warning( 'Trying to join workspace {} with token of ' 'workspace {}. Rejecting.'.format( message['workspace'], workspace.name)) self.sendClose() else: self.factory.join_workspace(self, message['workspace']) if message['action'] == 'LEAVE_WORKSPACE': self.factory.leave_workspace(self, message['workspace'])
def process_reports(debug, workspace, polling): try: from requests import ConnectionError except ImportError: print('Python requests was not found. Please install it with: pip install requests') sys.exit(1) try: from sqlalchemy.exc import OperationalError except ImportError: print('SQLAlchemy was not found please install it with: pip install sqlalchemy') sys.exit(1) setUpLogger(debug) configuration = _conf() url = '{0}/_api/v2/info'.format(configuration.getServerURI() if FARADAY_UP else SERVER_URL) with app.app_context(): try: check_faraday_server(url) import_external_reports(workspace, polling) except OperationalError as ex: print('{0}'.format(ex)) print('Please verify database is running or configuration on server.ini!') except ConnectionError: print('Can\'t connect to {0}. Please check if the server is running.'.format(url))
from server.web import app from server.database import db # Setup the database ctx = app.app_context() ctx.push() db.init_app(app)
def reset_db(): with app.app_context(): reset_db_all()
def import_from_couchdb(): with app.app_context(): ImportCouchDB().run()
def delete_custom_field_main(): with app.app_context(): delete_custom_field_wizard()
def add_custom_field_main(): with app.app_context(): add_custom_field_wizard()
def validate_user_unique_field(ctx, param, value): with app.app_context(): if User.query.filter_by(**{param.name: value}).count(): raise click.ClickException("User already exists") return value