def test_get_root_path_with_relative_path(env_app_root): assert qpylib.get_root_path( 'my', 'other', 'directory') == '/opt/app-root/my/other/directory'
def test_get_root_path_with_no_relative_path(env_app_root): assert qpylib.get_root_path() == '/opt/app-root'
def test_get_root_path_with_env_var_missing(): with pytest.raises(KeyError, match='Environment variable APP_ROOT is not set'): qpylib.get_root_path()
def test_get_root_path_with_relative_path_appends_relative_path(): assert qpylib.get_root_path('my/other/directory') == '/my/other/directory'
def test_get_root_path_with_no_relative_path_returns_slash(): assert qpylib.get_root_path() == '/'
def create_app(): # Create a Flask instance. qflask = Flask(__name__) csrf = CSRFProtect() csrf.init_app(qflask) # Retrieve QRadar app id. qradar_app_id = qpylib.get_app_id() # Create unique session cookie name for this app. qflask.config['SESSION_COOKIE_NAME'] = 'session_{0}'.format(qradar_app_id) secret_key = "" try: # Read in secret key secret_key_store = Encryption({'name': 'secret_key', 'user': '******'}) secret_key = secret_key_store.decrypt() except EncryptionError: # If secret key file doesn't exist/fail to decrypt it, # generate a new random password for it and encrypt it secret_key = secrets.token_urlsafe(64) secret_key_store = Encryption({'name': 'secret_key', 'user': '******'}) secret_key_store.encrypt(secret_key) qflask.config["SECRET_KEY"] = secret_key # Initialize database settings and flask configuration options via json file with open(qpylib.get_root_path( "container/conf/config.json")) as config_json_file: config_json = json.load(config_json_file) qflask.config.update(config_json) # Hide server details in endpoint responses. # pylint: disable=unused-variable @qflask.after_request def obscure_server_header(resp): resp.headers['Server'] = 'QRadar App {0}'.format(qradar_app_id) return resp # Register q_url_for function for use with Jinja2 templates. qflask.add_template_global(qpylib.q_url_for, 'q_url_for') # Initialize logging. qpylib.create_log() # To enable app health checking, the QRadar App Framework # requires every Flask app to define a /debug endpoint. # The endpoint function should contain a trivial implementation # that returns a simple confirmation response message. @qflask.route('/debug') def debug(): return 'Pong!' # Import additional endpoints. # For more information see: # https://flask.palletsprojects.com/en/1.1.x/tutorial/views from . import views qflask.register_blueprint(views.viewsbp) # NOTE: This sample app does not deal with migration of db schema between app versions as its v1.0.0. # If you have multiple versions of your application and the schema changes between them you would # need to add your own migration process at this point so that the schema is updated and loaded. # Also worth versioning your schema changes as well so you can perform the migration. db_host = qflask.config["DB_HOST"] db_port = qflask.config["DB_PORT"] db_user = qflask.config["DB_USER"] db_name = qflask.config["DB_NAME"] # create db if it doesnt exist and load schema if not db_exists(db_host, db_port, db_user, db_name): schema_file_path = qpylib.get_root_path("container/conf/db/schema.sql") create_db(db_host, db_port, db_user, db_name) execute_schema_sql(db_host, db_port, db_user, db_name, schema_file_path) return qflask