예제 #1
0
def test_get_app_id_raises_error_when_env_field_contains_string(
        env_qradar_app_id_string):
    with pytest.raises(
            ValueError,
            match=
            'Environment variable QRADAR_APP_ID has non-numeric value qradar_app_id'
    ):
        qpylib.get_app_id()
예제 #2
0
def get_context():
    """
    GetContext gets the app ID and IP and returns a JSON object with
    both bundled into it
    """
    # Get the context provided by the REST call
    context = request.args.get("context")
    # Return the app ID and the IP address
    return json.dumps({"app_id": qpylib.get_app_id(), "ip": context})
예제 #3
0
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

    # 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)

    return qflask
예제 #4
0
def create_app():
    # Create a Flask instance.
    qflask = Flask(__name__)

    # 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)

    # 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!'

    # Flask-Babel is an extension to Flask that adds i18n and l10n support
    # to any Flask application with the help of babel, pytz and speaklater.
    babel = Babel(qflask)

    # Try to select the language from the user accept header the browser transmits.
    # We support en/es/fr in this example.
    # The best match wins.
    @babel.localeselector
    def get_locale():
        return request.accept_languages.best_match(LANGUAGES.keys())

    # 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)

    return qflask
예제 #5
0
def getIPMetadata():
    app_id = qpylib.get_app_id()
    context = request.args.get('context')

    metadata_dict = {
        'key':
        'exampleIPMetadataProvider',
        'label':
        'Extra metadata:',
        'value':
        'Metadata value',
        'html':
        render_template('metadata_ip.html', ip_address=context, app_id=app_id)
    }

    return json.dumps(metadata_dict)
예제 #6
0
def create_app():
    # Create a Flask instance.
    qflask = Flask(__name__)

    # 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)

    # 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)

    return qflask
예제 #7
0
def test_get_app_id_returns_zero_when_field_missing_from_env():
    assert qpylib.get_app_id() == 0
예제 #8
0
def test_get_app_id_returns_value_from_env(env_qradar_app_id):
    assert qpylib.get_app_id() == 1005
예제 #9
0
def test_get_app_id_returns_value_from_manifest(mock_root_path,
                                                mock_get_manifest_location):
    assert qpylib.get_app_id() == 1005
예제 #10
0
def test_get_app_id_returns_zero_when_field_missing_from_manifest(
        mock_root_path, mock_get_manifest_location):
    assert qpylib.get_app_id() == 0
예제 #11
0
def hello():
    app_id = qpylib.get_app_id()
    return render_template('index.html', app_id=app_id)
예제 #12
0
def obscure_server_header(resp):
    resp.headers['Server'] = 'QRadar App {0}'.format(qpylib.get_app_id())
    return resp
예제 #13
0
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
예제 #14
0
def test_get_app_id_returns_zero_when_field_missing_from_manifest(
        mock_manifest):
    assert qpylib.get_app_id() == 0
예제 #15
0
def test_get_app_id_returns_value_from_manifest(mock_manifest):
    assert qpylib.get_app_id() == 1005