Exemplo n.º 1
0
def create_app(config='config/genocrowd.ini',
               app_name='genocrowd',
               blueprints=None):
    """Create the Genocrowd app

    Parameters
    ----------
    config : str, optional
        Path to the config file
    app_name : str, optional
        Application name
    blueprints : None, optional
        Flask blueprints

    Returns
    -------
    Flask
        Genocrowd Flask application
    """
    conf = configparser.ConfigParser()
    conf.read(config)
    sentry_dsn = None
    try:
        sentry_dsn = conf['sentry']['server_dsn']
    except Exception:
        pass
    if sentry_dsn:
        version = get_distribution('genocrowd').version
        name = get_distribution('genocrowd').project_name
        sentry_sdk.init(dsn=sentry_dsn,
                        release="{}@{}".format(name, version),
                        integrations=[FlaskIntegration(),
                                      CeleryIntegration()])
    app = Flask(app_name, static_folder='static', template_folder='templates')
    app.iniconfig = FlaskIni()
    with app.app_context():
        app.iniconfig.read(config)
        proxy_path = None
        try:
            proxy_path = app.iniconfig.get('genocrowd', 'reverse_proxy_path')
            app.config['REVERSE_PROXY_PATH'] = proxy_path
        except Exception:
            pass
        mongo_dbname = app.iniconfig.get('flask', 'mongo_dbname')
        app.config['MONGO_DBNAME'] = mongo_dbname
        mongo_uri = app.iniconfig.get('flask', 'mongo_uri')
        app.config['MONGO_URI'] = mongo_uri
        if not mongo_uri:
            raise Exception("Missing mongo_uri in config file")
        if not mongo_dbname:
            raise Exception("Missing mongo_dbname in config file")
        app.mongo = PyMongo(app)
        app.bcrypt = Bcrypt(app)
        users = app.mongo.db.users
        app.mongo.db.genes
        app.mongo.db.answers
        groups = app.mongo.db.groups

        app.genocrowd_admin_email = app.iniconfig.get('genocrowd',
                                                      'genocrowd_admin_email')
        app.genocrowd_admin_password = app.iniconfig.get(
            'genocrowd', 'genocrowd_admin_password')

        app.apollo_admin_email = app.iniconfig.get('genocrowd',
                                                   'apollo_admin_email')
        app.apollo_admin_password = app.iniconfig.get('genocrowd',
                                                      'apollo_admin_password')
        app.apollo_dataset_path = app.iniconfig.get('genocrowd',
                                                    'apollo_dataset_path')
        app.apollo_org_id = app.iniconfig.get('genocrowd', 'apollo_org_id')
        app.apollo_url = app.iniconfig.get('genocrowd', 'apollo_url')
        # We don't want ending slash
        if app.apollo_url.endswith("/"):
            app.apollo_url = app.apollo_url[:-1]

        app.apollo_url_ext = app.iniconfig.get('genocrowd', 'apollo_url_ext')
        # We don't want ending slash
        if app.apollo_url_ext.endswith("/"):
            app.apollo_url_ext = app.apollo_url_ext[:-1]

        configure_logging(app)

        if users.find_one() is None:
            # Create default admin user
            local_auth = LocalAuth(app, None)
            local_auth.add_user_to_database('admin', app.genocrowd_admin_email,
                                            app.genocrowd_admin_password,
                                            'admin', 'admin')

        if blueprints is None:
            blueprints = BLUEPRINTS

        for blueprint in blueprints:
            app.register_blueprint(blueprint)

        if groups.find_one() is None:
            # Initiate the groups database
            data = Data(app, None)
            data.initiate_groups()

    if proxy_path:
        ReverseProxyPrefixFix(app)
    return app
Exemplo n.º 2
0
import sentry_sdk
from sentry_sdk.integrations.celery import CeleryIntegration
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.redis import RedisIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration

from extensions import celery
from app import create_app

import config as c

app = create_app(for_celery=True)
app.app_context().push()

# add more external integrations below
if c.CELERY_SENTRY_DSN:
    sentry_sdk.init(
        c.CELERY_SENTRY_DSN,
        integrations=[
            CeleryIntegration(),
            FlaskIntegration(),
            RedisIntegration(),
            SqlalchemyIntegration(),
        ],
    )
Exemplo n.º 3
0
def create_app(
    name: str = __name__,
    mode: ServerModes = ServerModes.NORMAL,
    options: Optional[Dict[str, bool]] = None,
) -> Flask:
    """ Create the server istance for Flask application """

    if PRODUCTION and TESTING and not FORCE_PRODUCTION_TESTS:  # pragma: no cover
        print_and_exit("Unable to execute tests in production")

    # TERM is not catched by Flask
    # https://github.com/docker/compose/issues/4199#issuecomment-426109482
    # signal.signal(signal.SIGTERM, teardown_handler)
    # SIGINT is registered as STOPSIGNAL in Dockerfile
    signal.signal(signal.SIGINT, teardown_handler)

    # Flask app instance
    # template_folder = template dir for output in HTML
    microservice = Flask(
        name, template_folder=os.path.join(ABS_RESTAPI_PATH, "templates")
    )

    # CORS
    if not PRODUCTION:
        cors = CORS(
            allow_headers=[
                "Content-Type",
                "Authorization",
                "X-Requested-With",
                "x-upload-content-length",
                "x-upload-content-type",
                "content-range",
            ],
            supports_credentials=["true"],
            methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
        )

        cors.init_app(microservice)
        log.debug("CORS Injected")

    # Flask configuration from config file
    microservice.config.from_object(config)
    log.debug("Flask app configured")

    if PRODUCTION:
        log.info("Production server mode is ON")

    endpoints_loader = EndpointsLoader()
    mem.configuration = endpoints_loader.load_configuration()

    mem.initializer = Meta.get_class("initialization", "Initializer")
    if not mem.initializer:  # pragma: no cover
        print_and_exit("Invalid Initializer class")

    mem.customizer = Meta.get_instance("customization", "Customizer")
    if not mem.customizer:  # pragma: no cover
        print_and_exit("Invalid Customizer class")

    if not isinstance(mem.customizer, BaseCustomizer):  # pragma: no cover
        print_and_exit("Invalid Customizer class, it should inherit BaseCustomizer")

    Connector.init_app(app=microservice, worker_mode=(mode == ServerModes.WORKER))

    # Initialize reading of all files
    mem.geo_reader = geolite2.reader()
    # when to close??
    # geolite2.close()

    if mode == ServerModes.INIT:
        Connector.project_init(options=options)

    if mode == ServerModes.DESTROY:
        Connector.project_clean()

    # Restful plugin with endpoint mapping (skipped in INIT|DESTROY|WORKER modes)
    if mode == ServerModes.NORMAL:

        logging.getLogger("werkzeug").setLevel(logging.ERROR)
        # ignore warning messages from apispec
        warnings.filterwarnings(
            "ignore", message="Multiple schemas resolved to the name "
        )
        mem.cache = Cache.get_instance(microservice)

        endpoints_loader.load_endpoints()
        mem.authenticated_endpoints = endpoints_loader.authenticated_endpoints
        mem.private_endpoints = endpoints_loader.private_endpoints

        # Triggering automatic mapping of REST endpoints
        rest_api = Api(catch_all_404s=True)

        for endpoint in endpoints_loader.endpoints:
            # Create the restful resource with it;
            # this method is from RESTful plugin
            rest_api.add_resource(endpoint.cls, *endpoint.uris)

        # HERE all endpoints will be registered by using FlaskRestful
        rest_api.init_app(microservice)

        # APISpec configuration
        api_url = get_backend_url()
        scheme, host = api_url.rstrip("/").split("://")

        spec = APISpec(
            title=get_project_configuration(
                "project.title", default="Your application name"
            ),
            version=get_project_configuration("project.version", default="0.0.1"),
            openapi_version="2.0",
            # OpenApi 3 not working with FlaskApiSpec
            # -> Duplicate parameter with name body and location body
            # https://github.com/jmcarp/flask-apispec/issues/170
            # Find other warning like this by searching:
            # **FASTAPI**
            # openapi_version="3.0.2",
            plugins=[MarshmallowPlugin()],
            host=host,
            schemes=[scheme],
            tags=endpoints_loader.tags,
        )
        # OpenAPI 3 changed the definition of the security level.
        # Some changes needed here?
        api_key_scheme = {"type": "apiKey", "in": "header", "name": "Authorization"}
        spec.components.security_scheme("Bearer", api_key_scheme)

        microservice.config.update(
            {
                "APISPEC_SPEC": spec,
                # 'APISPEC_SWAGGER_URL': '/api/swagger',
                "APISPEC_SWAGGER_URL": None,
                # 'APISPEC_SWAGGER_UI_URL': '/api/swagger-ui',
                # Disable Swagger-UI
                "APISPEC_SWAGGER_UI_URL": None,
            }
        )

        mem.docs = FlaskApiSpec(microservice)

        # Clean app routes
        ignore_verbs = {"HEAD", "OPTIONS"}

        for rule in microservice.url_map.iter_rules():

            endpoint = microservice.view_functions[rule.endpoint]
            if not hasattr(endpoint, "view_class"):
                continue

            newmethods = ignore_verbs.copy()
            rulename = str(rule)

            for verb in rule.methods - ignore_verbs:
                method = verb.lower()
                if method in endpoints_loader.uri2methods[rulename]:
                    # remove from flask mapping
                    # to allow 405 response
                    newmethods.add(verb)

            rule.methods = newmethods

        # Register swagger. Note: after method mapping cleaning
        with microservice.app_context():
            for endpoint in endpoints_loader.endpoints:
                try:
                    mem.docs.register(endpoint.cls)
                except TypeError as e:  # pragma: no cover
                    print(e)
                    log.error("Cannot register {}: {}", endpoint.cls.__name__, e)

    # marshmallow errors handler
    microservice.register_error_handler(422, handle_marshmallow_errors)

    # Logging responses
    microservice.after_request(handle_response)

    if SENTRY_URL is not None:  # pragma: no cover

        if PRODUCTION:
            sentry_sdk.init(
                dsn=SENTRY_URL,
                # already catched by handle_marshmallow_errors
                ignore_errors=[werkzeug.exceptions.UnprocessableEntity],
                integrations=[FlaskIntegration()],
            )
            log.info("Enabled Sentry {}", SENTRY_URL)
        else:
            # Could be enabled in print mode
            # sentry_sdk.init(transport=print)
            log.info("Skipping Sentry, only enabled in PRODUCTION mode")

    log.info("Boot completed")

    return microservice
Exemplo n.º 4
0
    # Edita archivo hosts si es necesario
    if os.environ['EDIT_HOSTS'] == 'true':
        host_ip = os.environ['HOST_IP']
        host_ad = os.environ['HOST_AD']
        hosts = Hosts()
        new_entry = HostsEntry(entry_type='ipv4',
                               address=host_ip,
                               names=[host_ad])
        hosts.add([new_entry])
        hosts.write()


# Configura sentry
sentry_sdk.init(
    dsn=os.environ['SENTRY_DSN'],
    integrations=[FlaskIntegration(), CeleryIntegration()],
)

# Obtiene las variables de ambiente
STP_PRIVATE_LOCATION = os.environ['STP_PRIVATE_LOCATION']
STP_BUCKET_S3 = os.environ['STP_BUCKET_S3']
STP_PRIVATE_KEY = os.environ['STP_PRIVATE_KEY']
STP_WSDL = os.environ['STP_WSDL']
STP_EMPRESA = os.environ['STP_EMPRESA']
STP_PREFIJO = os.environ['STP_PREFIJO']
DATABASE_URI = os.environ['DATABASE_URI']

app = Flask('speid')

app.config['MONGODB_HOST'] = DATABASE_URI
Exemplo n.º 5
0
def init_sentry(sentry_config):
    sentry_sdk.init(dsn=sentry_config["SENTRY_DSN"],
                    release=sentry_config["RELEASE"],
                    integrations=[FlaskIntegration()])
Exemplo n.º 6
0
def init_logging():
    flask_env = os.getenv('FLASK_ENV')
    sentry_dns = os.getenv('SENTRY_DSN')
    if flask_env != 'dev' and sentry_dns is not None:
        sentry_logging = LoggingIntegration(
            level=logging.INFO,  # Capture info and above as breadcrumbs
            event_level=logging.ERROR  # Send errors as events
        )
        sentry_sdk.init(dsn=sentry_dns,
                        environment=flask_env,
                        integrations=[sentry_logging,
                                      FlaskIntegration()])

        with configure_scope() as scope:
            scope.user = {'node_id': common.node_id()}

    logging_config = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'f': {
                'format': '%(asctime)s [%(levelname)s] %(message)s',
                'datefmt': '%Y-%m-%d %H:%M:%S'
            },
        },
        'handlers': {
            'console': {
                'class': 'logging.StreamHandler',
                'formatter': 'f',
                'level': 'INFO',
            },
            'file': {
                'class': 'logging.handlers.RotatingFileHandler',
                'level': 'INFO',
                'formatter': 'f',
                'filename': 'logs/validation_node.log',
                'mode': 'a',
            },
            'error_file': {
                'class': 'logging.handlers.RotatingFileHandler',
                'level': 'INFO',
                'formatter': 'f',
                'filename': 'logs/gunicorn.error.log',
                'mode': 'a',
            },
            'access_file': {
                'class': 'logging.FileHandler',
                'formatter': 'f',
                'filename': 'logs/gunicorn.access.log',
            },
        },
        'root': {
            'handlers': ['console', 'file'],
            'level': 'INFO',
            'propagate': True,
        },
        'loggers': {
            'gunicorn.error': {
                'level': 'INFO',
                'handlers': ['console', 'error_file'],
                'propagate': True
            },
            'gunicorn.access': {
                'level': 'INFO',
                'handlers': ['access_file'],
                'propagate': False
            },
        }
    }
    dictConfig(logging_config)
Exemplo n.º 7
0
def create_app(
    name: str = __name__,
    mode: ServerModes = ServerModes.NORMAL,
    options: Optional[Dict[str, bool]] = None,
) -> Flask:
    """Create the server istance for Flask application"""

    if PRODUCTION and TESTING and not FORCE_PRODUCTION_TESTS:  # pragma: no cover
        print_and_exit("Unable to execute tests in production")

    # TERM is not catched by Flask
    # https://github.com/docker/compose/issues/4199#issuecomment-426109482
    # signal.signal(signal.SIGTERM, teardown_handler)
    # SIGINT is registered as STOPSIGNAL in Dockerfile
    signal.signal(signal.SIGINT, teardown_handler)

    # Flask app instance
    # template_folder = template dir for output in HTML
    flask_app = Flask(name, template_folder=str(ABS_RESTAPI_PATH.joinpath("templates")))

    # CORS
    if not PRODUCTION:

        if TESTING:
            cors_origin = "*"
        else:  # pragma: no cover
            cors_origin = get_frontend_url()
            # Beware, this only works because get_frontend_url never append a port
            cors_origin += ":*"

        CORS(
            flask_app,
            allow_headers=[
                "Content-Type",
                "Authorization",
                "X-Requested-With",
                "x-upload-content-length",
                "x-upload-content-type",
                "content-range",
            ],
            supports_credentials=["true"],
            methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"],
            resources={r"*": {"origins": cors_origin}},
        )

        log.debug("CORS Enabled")

    # Flask configuration from config file
    flask_app.config.from_object(config)
    flask_app.json_encoder = ExtendedJSONEncoder

    # Used to force flask to avoid json sorting and ensure that
    # the output to reflect the order of field in the Marshmallow schema
    flask_app.config["JSON_SORT_KEYS"] = False

    log.debug("Flask app configured")

    if PRODUCTION:
        log.info("Production server mode is ON")

    endpoints_loader = EndpointsLoader()

    if HOST_TYPE == DOCS:  # pragma: no cover
        log.critical("Creating mocked configuration")
        mem.configuration = {}

        log.critical("Loading Mocked Initializer and Customizer classes")
        from restapi.mocks import Customizer, Initializer

        mem.initializer = Initializer
        mem.customizer = Customizer()

    else:

        mem.configuration = endpoints_loader.load_configuration()
        mem.initializer = Meta.get_class("initialization", "Initializer")
        if not mem.initializer:  # pragma: no cover
            print_and_exit("Invalid Initializer class")

        customizer = Meta.get_class("customization", "Customizer")
        if not customizer:  # pragma: no cover
            print_and_exit("Invalid Customizer class")
        mem.customizer = customizer()

    if not isinstance(mem.customizer, BaseCustomizer):  # pragma: no cover
        print_and_exit("Invalid Customizer class, it should inherit BaseCustomizer")

    Connector.init_app(app=flask_app, worker_mode=(mode == ServerModes.WORKER))

    # Initialize reading of all files
    mem.geo_reader = geolite2.reader()
    # when to close??
    # geolite2.close()

    if mode == ServerModes.INIT:
        Connector.project_init(options=options)

    if mode == ServerModes.DESTROY:
        Connector.project_clean()

    # Restful plugin with endpoint mapping (skipped in INIT|DESTROY|WORKER modes)
    if mode == ServerModes.NORMAL:

        logging.getLogger("werkzeug").setLevel(logging.ERROR)

        # warnings levels:
        # default  # Warn once per call location
        # error    # Convert to exceptions
        # always   # Warn every time
        # module   # Warn once per calling module
        # once     # Warn once per Python process
        # ignore   # Never warn

        # Types of warnings:
        # Warning: This is the base class of all warning category classes
        # UserWarning: The default category for warn().
        # DeprecationWarning: Base category for warnings about deprecated features when
        #                     those warnings are intended for other Python developers
        # SyntaxWarning: Base category for warnings about dubious syntactic features.
        # RuntimeWarning: Base category for warnings about dubious runtime features.
        # FutureWarning: Base category for warnings about deprecated features when those
        #                warnings are intended for end users
        # PendingDeprecationWarning: Base category for warnings about features that will
        #                            be deprecated in the future (ignored by default).
        # ImportWarning: Base category for warnings triggered during the process of
        #                importing a module
        # UnicodeWarning: Base category for warnings related to Unicode.
        # BytesWarning: Base category for warnings related to bytes and bytearray.
        # ResourceWarning: Base category for warnings related to resource usage

        if TESTING:
            warnings.simplefilter("always", Warning)
            warnings.simplefilter("error", UserWarning)
            warnings.simplefilter("error", DeprecationWarning)
            warnings.simplefilter("error", SyntaxWarning)
            warnings.simplefilter("error", RuntimeWarning)
            warnings.simplefilter("error", FutureWarning)
            # warnings about features that will be deprecated in the future
            warnings.simplefilter("default", PendingDeprecationWarning)
            warnings.simplefilter("error", ImportWarning)
            warnings.simplefilter("error", UnicodeWarning)
            warnings.simplefilter("error", BytesWarning)
            # Can't set this an error due to false positives with downloads
            # a lot of issues like: https://github.com/pallets/flask/issues/2468
            warnings.simplefilter("always", ResourceWarning)
            warnings.simplefilter("default", Neo4jExperimentalWarning)

            # Remove me in a near future, this is due to hypothesis with pytest 7
            # https://github.com/HypothesisWorks/hypothesis/issues/3222
            warnings.filterwarnings(
                "ignore", message="A private pytest class or function was used."
            )

        elif PRODUCTION:  # pragma: no cover
            warnings.simplefilter("ignore", Warning)
            warnings.simplefilter("always", UserWarning)
            warnings.simplefilter("default", DeprecationWarning)
            warnings.simplefilter("ignore", SyntaxWarning)
            warnings.simplefilter("ignore", RuntimeWarning)
            warnings.simplefilter("ignore", FutureWarning)
            warnings.simplefilter("ignore", PendingDeprecationWarning)
            warnings.simplefilter("ignore", ImportWarning)
            warnings.simplefilter("ignore", UnicodeWarning)
            warnings.simplefilter("ignore", BytesWarning)
            warnings.simplefilter("ignore", ResourceWarning)
            # even if ignore it is raised once
            # because of the imports executed before setting this to ignore
            warnings.simplefilter("ignore", Neo4jExperimentalWarning)
        else:  # pragma: no cover
            warnings.simplefilter("default", Warning)
            warnings.simplefilter("always", UserWarning)
            warnings.simplefilter("always", DeprecationWarning)
            warnings.simplefilter("default", SyntaxWarning)
            warnings.simplefilter("default", RuntimeWarning)
            warnings.simplefilter("always", FutureWarning)
            warnings.simplefilter("default", PendingDeprecationWarning)
            warnings.simplefilter("default", ImportWarning)
            warnings.simplefilter("default", UnicodeWarning)
            warnings.simplefilter("default", BytesWarning)
            warnings.simplefilter("always", ResourceWarning)
            # even if ignore it is raised once
            # because of the imports executed before setting this to ignore
            warnings.simplefilter("ignore", Neo4jExperimentalWarning)

        # ignore warning messages from apispec
        warnings.filterwarnings(
            "ignore", message="Multiple schemas resolved to the name "
        )

        # ignore warning messages on flask socket after teardown
        warnings.filterwarnings("ignore", message="unclosed <socket.socket")

        # from flask_caching 1.10.1 with python 3.10 on core tests...
        # try to remove this once upgraded flask_caching in a near future
        warnings.filterwarnings(
            "ignore",
            message="_SixMetaPathImporter.find_spec",
        )

        # Raised from sentry_sdk 1.5.11 with python 3.10 events
        warnings.filterwarnings(
            "ignore",
            message="SelectableGroups dict interface is deprecated. Use select.",
        )

        mem.cache = Cache.get_instance(flask_app)

        endpoints_loader.load_endpoints()
        mem.authenticated_endpoints = endpoints_loader.authenticated_endpoints
        mem.private_endpoints = endpoints_loader.private_endpoints

        for endpoint in endpoints_loader.endpoints:
            ename = endpoint.cls.__name__.lower()
            endpoint_view = endpoint.cls.as_view(ename)
            for url in endpoint.uris:
                flask_app.add_url_rule(url, view_func=endpoint_view)

        # APISpec configuration
        api_url = get_backend_url()
        scheme, host = api_url.rstrip("/").split("://")

        spec = APISpec(
            title=get_project_configuration(
                "project.title", default="Your application name"
            ),
            version=get_project_configuration("project.version", default="0.0.1"),
            openapi_version="2.0",
            # OpenApi 3 not working with FlaskApiSpec
            # -> Duplicate parameter with name body and location body
            # https://github.com/jmcarp/flask-apispec/issues/170
            # Find other warning like this by searching:
            # **FASTAPI**
            # openapi_version="3.0.2",
            plugins=[MarshmallowPlugin()],
            host=host,
            schemes=[scheme],
            tags=endpoints_loader.tags,
        )
        # OpenAPI 3 changed the definition of the security level.
        # Some changes needed here?

        if Env.get_bool("AUTH_ENABLE"):
            api_key_scheme = {"type": "apiKey", "in": "header", "name": "Authorization"}
            spec.components.security_scheme("Bearer", api_key_scheme)

        flask_app.config.update(
            {
                "APISPEC_SPEC": spec,
                # 'APISPEC_SWAGGER_URL': '/api/swagger',
                "APISPEC_SWAGGER_URL": None,
                # 'APISPEC_SWAGGER_UI_URL': '/api/swagger-ui',
                # Disable Swagger-UI
                "APISPEC_SWAGGER_UI_URL": None,
            }
        )

        mem.docs = FlaskApiSpec(flask_app)

        # Clean app routes
        ignore_verbs = {"HEAD", "OPTIONS"}

        for rule in flask_app.url_map.iter_rules():

            view_function = flask_app.view_functions[rule.endpoint]
            if not hasattr(view_function, "view_class"):
                continue

            newmethods = ignore_verbs.copy()
            rulename = str(rule)

            if rule.methods:
                for verb in rule.methods - ignore_verbs:
                    method = verb.lower()
                    if method in endpoints_loader.uri2methods[rulename]:
                        # remove from flask mapping
                        # to allow 405 response
                        newmethods.add(verb)

            rule.methods = newmethods

        # Register swagger. Note: after method mapping cleaning
        with flask_app.app_context():
            for endpoint in endpoints_loader.endpoints:
                try:
                    mem.docs.register(endpoint.cls)
                except TypeError as e:  # pragma: no cover
                    print(e)
                    log.error("Cannot register {}: {}", endpoint.cls.__name__, e)

    # marshmallow errors handler
    # Can't get the typing to work with flask 2.1
    flask_app.register_error_handler(422, handle_marshmallow_errors)  # type: ignore
    flask_app.register_error_handler(400, handle_http_errors)  # type: ignore
    flask_app.register_error_handler(404, handle_http_errors)  # type: ignore
    flask_app.register_error_handler(405, handle_http_errors)  # type: ignore
    flask_app.register_error_handler(500, handle_http_errors)  # type: ignore

    # flask_app.before_request(inspect_request)
    # Logging responses
    # Can't get the typing to work with flask 2.1
    flask_app.after_request(handle_response)  # type: ignore

    if SENTRY_URL is not None:  # pragma: no cover

        if PRODUCTION:
            sentry_sdk_init(
                dsn=SENTRY_URL,
                # already catched by handle_marshmallow_errors
                ignore_errors=[werkzeug.exceptions.UnprocessableEntity],
                integrations=[FlaskIntegration()],
            )
            log.info("Enabled Sentry {}", SENTRY_URL)
        else:
            # Could be enabled in print mode
            # sentry_sdk_init(transport=print)
            log.info("Skipping Sentry, only enabled in PRODUCTION mode")

    log.info("Boot completed")
    if PRODUCTION and not TESTING and name == MAIN_SERVER_NAME:  # pragma: no cover
        save_event_log(
            event=Events.server_startup,
            payload={"server": name},
            user=None,
            target=None,
        )

    return flask_app
Exemplo n.º 8
0
from entropy_helper import EntropyHelper
from util import number_format, detect_browser_and_platform, get_tool_recommendation
from db import Db

app = Flask(__name__)
app.secret_key = config.secret_key
app.debug = config.debug
app.permanent_session_lifetime = timedelta(days=config.epoch_days)
app.config.update(
    USE_MATOMO=config.use_matomo,
    MATOMO_URL=config.matomo_url,
    MATOMO_SITE_ID=config.matomo_site_id,
)

if config.sentry_dsn:
    sentry_sdk.init(config.sentry_dsn, integrations=[FlaskIntegration()])


def read_keyfile():
    global key
    with open(
            config.keyfile,
            'rb',
    ) as fp:
        key = fp.read(16)


read_keyfile()


def require_admin_pass(route):
Exemplo n.º 9
0
# Initial require, the above line contains our endpoints.

config = json.load(open('config.json'))
endpoints = None

app = Flask(__name__, template_folder='views', static_folder='views/assets')
app.register_blueprint(dash)

app.config['SECRET_KEY'] = config['client_secret']
os.environ['OAUTHLIB_INSECURE_TRANSPORT'] = 'true'

if 'sentry_dsn' in config:
    import sentry_sdk
    from sentry_sdk.integrations.flask import FlaskIntegration

    sentry_sdk.init(config['sentry_dsn'], integrations=[FlaskIntegration()])


@app.before_first_request
def init_app():
    def run_gc_forever(loop):
        asyncio.set_event_loop(loop)
        try:
            loop.run_forever()
        except (SystemExit, KeyboardInterrupt):
            loop.close()

    gc_loop = asyncio.new_event_loop()
    gc_thread = threading.Thread(target=run_gc_forever, args=(gc_loop, ))
    gc_thread.start()
    g.gc_loop = gc_loop
Exemplo n.º 10
0
def create_app():
    global app_created
    if not app_created:
        BlueprintsManager.register(app)
    Migrate(app, db)

    app.config.from_object(env('APP_CONFIG',
                               default='config.ProductionConfig'))

    if not app.config['SECRET_KEY']:
        if app.config['PRODUCTION']:
            app.logger.error(
                'SECRET_KEY must be set in .env or environment variables in production'
            )
            exit(1)
        else:
            random_secret = secrets.token_hex()
            app.logger.warning(
                f'Using random secret "{ random_secret }" for development server. '
                'This is NOT recommended. Set proper SECRET_KEY in .env or environment variables'
            )
            app.config['SECRET_KEY'] = random_secret

    db.init_app(app)

    if app.config['CACHING']:
        cache.init_app(app, config={'CACHE_TYPE': 'simple'})
    else:
        cache.init_app(app, config={'CACHE_TYPE': 'null'})

    stripe.api_key = 'SomeStripeKey'
    app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False
    app.config['FILE_SYSTEM_STORAGE_FILE_VIEW'] = 'static'

    app.logger.addHandler(logging.StreamHandler(sys.stdout))
    app.logger.setLevel(logging.ERROR)

    # set up jwt
    app.config['JWT_HEADER_TYPE'] = 'JWT'
    app.config['JWT_ACCESS_TOKEN_EXPIRES'] = timedelta(days=1)
    app.config['JWT_REFRESH_TOKEN_EXPIRES'] = timedelta(days=365)
    app.config['JWT_ERROR_MESSAGE_KEY'] = 'error'
    app.config['JWT_TOKEN_LOCATION'] = ['cookies', 'headers']
    app.config['JWT_REFRESH_COOKIE_PATH'] = '/v1/auth/token/refresh'
    app.config['JWT_SESSION_COOKIE'] = False
    app.config['JWT_BLACKLIST_ENABLED'] = True
    app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['refresh']
    _jwt = JWTManager(app)
    _jwt.user_loader_callback_loader(jwt_user_loader)
    _jwt.token_in_blacklist_loader(is_token_blacklisted)

    # setup celery
    app.config['CELERY_BROKER_URL'] = app.config['REDIS_URL']
    app.config['CELERY_RESULT_BACKEND'] = app.config['CELERY_BROKER_URL']
    app.config['CELERY_ACCEPT_CONTENT'] = ['json', 'application/text']

    CORS(app, resources={r"/*": {"origins": "*"}})
    AuthManager.init_login(app)

    if app.config['TESTING'] and app.config['PROFILE']:
        # Profiling
        app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30])

    # development api
    with app.app_context():
        from app.api.admin_statistics_api.events import event_statistics
        from app.api.auth import auth_routes
        from app.api.custom.attendees import attendee_blueprint
        from app.api.bootstrap import api_v1
        from app.api.celery_tasks import celery_routes
        from app.api.event_copy import event_copy
        from app.api.exports import export_routes
        from app.api.imports import import_routes
        from app.api.uploads import upload_routes
        from app.api.users import user_misc_routes
        from app.api.orders import order_misc_routes
        from app.api.role_invites import role_invites_misc_routes
        from app.api.auth import authorised_blueprint
        from app.api.admin_translations import admin_blueprint
        from app.api.orders import alipay_blueprint
        from app.api.settings import admin_misc_routes
        from app.api.server_version import info_route
        from app.api.custom.orders import ticket_blueprint
        from app.api.custom.orders import order_blueprint
        from app.api.custom.invoices import event_blueprint

        app.register_blueprint(api_v1)
        app.register_blueprint(event_copy)
        app.register_blueprint(upload_routes)
        app.register_blueprint(export_routes)
        app.register_blueprint(import_routes)
        app.register_blueprint(celery_routes)
        app.register_blueprint(auth_routes)
        app.register_blueprint(event_statistics)
        app.register_blueprint(user_misc_routes)
        app.register_blueprint(attendee_blueprint)
        app.register_blueprint(order_misc_routes)
        app.register_blueprint(role_invites_misc_routes)
        app.register_blueprint(authorised_blueprint)
        app.register_blueprint(admin_blueprint)
        app.register_blueprint(alipay_blueprint)
        app.register_blueprint(admin_misc_routes)
        app.register_blueprint(info_route)
        app.register_blueprint(ticket_blueprint)
        app.register_blueprint(order_blueprint)
        app.register_blueprint(event_blueprint)

        add_engine_pidguard(db.engine)

        if app.config['SQLALCHEMY_DATABASE_URI'].startswith("sqlite://"):
            sqlite_datetime_fix()

    sa.orm.configure_mappers()

    if app.config['SERVE_STATIC']:
        app.add_url_rule('/static/<path:filename>',
                         endpoint='static',
                         view_func=app.send_static_file)

    # sentry
    if not app_created and 'SENTRY_DSN' in app.config:
        sentry_sdk.init(
            app.config['SENTRY_DSN'],
            integrations=[
                FlaskIntegration(),
                RedisIntegration(),
                CeleryIntegration(),
                SqlalchemyIntegration(),
            ],
        )

    # redis
    redis_store.init_app(app)

    # Initialize Extensions
    shell.init_app(app)
    limiter.init_app(app)

    app_created = True
    return app
Exemplo n.º 11
0
from opendc.models.user import User
from opendc.util import rest, path_parser, database
from opendc.util.exceptions import AuthorizationTokenError, RequestInitializationError
from opendc.util.json import JSONEncoder

load_dotenv()

TEST_MODE = "OPENDC_FLASK_TESTING" in os.environ

# Setup Sentry if DSN is specified
if 'SENTRY_DSN' in os.environ:
    import sentry_sdk
    from sentry_sdk.integrations.flask import FlaskIntegration

    sentry_sdk.init(integrations=[FlaskIntegration()], traces_sample_rate=0.1)

# Set up database if not testing
if not TEST_MODE:
    database.DB.initialize_database(user=os.environ['OPENDC_DB_USERNAME'],
                                    password=os.environ['OPENDC_DB_PASSWORD'],
                                    database=os.environ['OPENDC_DB'],
                                    host=os.environ.get(
                                        'OPENDC_DB_HOST', 'localhost'))

# Set up the core app
FLASK_CORE_APP = Flask(__name__)
FLASK_CORE_APP.testing = TEST_MODE
FLASK_CORE_APP.config['SECRET_KEY'] = os.environ['OPENDC_FLASK_SECRET']
FLASK_CORE_APP.json_encoder = JSONEncoder
Exemplo n.º 12
0
import functools
import os

from flask import Flask, Response, send_file, request
from flask_cors import CORS, cross_origin

app = Flask(__name__)
CORS(app)

SENTRY_SDK = os.getenv("SENTRY_SDK")

if SENTRY_SDK:
    import sentry_sdk
    from sentry_sdk.integrations.flask import FlaskIntegration

    sentry_sdk.init(SENTRY_SDK, integrations=[FlaskIntegration()])


def handle_text_turtle(filename='profile.ttl', **kwargs):
    return send_file(filename, mimetype='text/turtle')


def handle_other(filename='profile.ttl',
                 format='trig',
                 mimetype='application/trig'):
    import rdflib

    g = rdflib.Graph()
    g.parse(source=filename, format='text/turtle')
    return Response(g.serialize(format=format), mimetype=mimetype)
Exemplo n.º 13
0
from sentry_sdk.integrations.rq import RqIntegration

app = flask.Flask(__name__)
redis_conn = redis.Redis()
http = urllib3.PoolManager()
queue = rq.Queue(connection=redis_conn)


def write_event(event):
    with open("events", "a") as f:
        f.write(json.dumps(event))
        f.write("\n")


sentry_sdk.init(
    integrations=[FlaskIntegration(), RqIntegration()],
    traces_sample_rate=1.0,
    traceparent_v2=True,
    debug=True,
    transport=write_event,
)


def decode_base64(encoded, redis_key):
    time.sleep(1)
    r = http.request("GET", "http://httpbin.org/base64/{}".format(encoded))
    redis_conn.set(redis_key, r.data)


@app.route("/")
def index():
Exemplo n.º 14
0
from bu_cascade.asset_tools import find, update
from flask import Flask, render_template
from flask_classy import FlaskView, route
import sentry_sdk

# Imports from elsewhere in this project
from mail import send_message
from config import WSDL, AUTH, SITE_ID, STAGING_DESTINATION_ID, XML_URL


app = Flask(__name__)
app.config.from_object('config')

if app.config['SENTRY_URL']:
    from sentry_sdk.integrations.flask import FlaskIntegration
    sentry_sdk.init(dsn=app.config['SENTRY_URL'], integrations=[FlaskIntegration()])


class CascadeBlockProcessor:
    def __init__(self):
        self.cascade = Cascade(WSDL, AUTH, SITE_ID, STAGING_DESTINATION_ID)
        self.codes_found_in_cascade = []
        self.codes_not_found_in_banner = []

    def get_new_banner_data(self):
        return json.loads(requests.get('https://wsapi.bethel.edu/program-data').content)

    def process_all_blocks(self, time_to_wait, send_email_after):
        new_banner_data = self.get_new_banner_data()

        if len(new_banner_data) == 0:
Exemplo n.º 15
0
def create_app(config_name="default"):
    application = Flask(__name__, instance_relative_config=True)

    from jinja2 import select_autoescape

    # CONFIG
    from config import configs

    application.config.from_object(configs[config_name])

    print(f"DB INFO: using {application.config['INFO_USED_DB']}")

    # APPS
    db.init_app(application)
    migrate.init_app(application, db)
    security.init_app(application, user_datastore)
    babel.init_app(application)
    turbo.init_app(application)
    mail.init_app(application)
    dropzone.init_app(application)

    if application.config["SENTRY_MONITORING"]:
        import sentry_sdk
        from sentry_sdk.integrations.flask import FlaskIntegration
        from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration

        sentry_sdk.init(
            dsn="https://[email protected]/5776351",
            integrations=[FlaskIntegration(), SqlalchemyIntegration()],
            traces_sample_rate=1.0,
        )
    else:
        print("No Sentry monitoring.")

    # LOGGING
    # from .config.config_logging import db_handler, gunicorn_logger

    # application.logger.addHandler(gunicorn_logger)
    # application.logger.addHandler(db_handler)

    # CONTROLLERS
    from .controllers import register_all_controllers  # noqa: F401

    register_all_controllers(application)

    from .controllers import register_error_handlers  # noqa: F401

    register_error_handlers(application)

    # MODULES
    from app.modules.auth.auth import blueprint as google_oauth_bp

    application.register_blueprint(google_oauth_bp, url_prefix="/oauth/")

    if application.config["APP_STATE"] in ("test", "testing"):
        with application.app_context():
            # from app.helpers.tests.fill_db import db_fill
            db.create_all()
            print("Created database")

            # db_fill()

    application.jinja_options = {
        "autoescape": select_autoescape(enabled_extensions=("html", "html.j2", "xml")),
        "line_statement_prefix": "#",
        "line_comment_prefix": "##",
    }

    return application
Exemplo n.º 16
0
import sentry_sdk
from flask import Flask, session
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.redis import RedisIntegration

from bot.data import database, logger
from bot.functions import user_setup

sentry_sdk.init(
    release=f"{os.getenv('CURRENT_PLATFORM')} Release " +
    (f"{os.getenv('GIT_REV')[:8]}"
     if os.getenv("CURRENT_PLATFORM") != "Heroku" else
     f"{os.getenv('HEROKU_RELEASE_VERSION')}:{os.getenv('HEROKU_SLUG_DESCRIPTION')}"
     ),
    dsn=os.getenv("SENTRY_API_DSN"),
    integrations=[FlaskIntegration(), RedisIntegration()],
)
app = Flask(__name__)
app.config["SEND_FILE_MAX_AGE_DEFAULT"] = 0
app.config["SESSION_COOKIE_SAMESITE"] = "Strict"
app.config["SESSION_COOKIE_SECURE"] = True
app.secret_key = os.getenv("FLASK_SECRET_KEY")
FRONTEND_URL = os.getenv("FRONTEND_URL")
DATABASE_SESSION_EXPIRE = 172800  # 2 days


@app.after_request  # enable CORS
def after_request(response):
    header = response.headers
    header["Access-Control-Allow-Origin"] = FRONTEND_URL
    header["Access-Control-Allow-Credentials"] = "true"
Exemplo n.º 17
0
from belastingen.api.belastingen.exceptions import K2bAuthenticationError
from belastingen.api.belastingen.key2belastingen import K2bConnection
from belastingen.config import (
    get_sentry_dsn,
    get_tma_certificate,
    get_K2B_api_location,
    get_bearer_token,
)

logger = logging.getLogger(__name__)
app = Flask(__name__)


if get_sentry_dsn():  # pragma: no cover
    sentry_sdk.init(
        dsn=get_sentry_dsn(), integrations=[FlaskIntegration()], with_locals=False
    )


def get_bsn_from_request(request):
    """
    Get the BSN based on a request, expecting a SAML token in the headers
    """
    # Load the TMA certificate
    tma_certificate = get_tma_certificate()

    # Decode the BSN from the request with the TMA certificate
    bsn = get_digi_d_bsn(request, tma_certificate)
    return bsn

Exemplo n.º 18
0
def setup_bot(backend_name: str, logger, config, restore=None) -> ErrBot:
    # from here the environment is supposed to be set (daemon / non daemon,
    # config.py in the python path )

    bot_config_defaults(config)

    if hasattr(config, 'BOT_LOG_FORMATTER'):
        format_logs(formatter=config.BOT_LOG_FORMATTER)
    else:
        format_logs(theme_color=config.TEXT_COLOR_THEME)

        if config.BOT_LOG_FILE:
            hdlr = logging.FileHandler(config.BOT_LOG_FILE)
            hdlr.setFormatter(
                logging.Formatter(
                    "%(asctime)s %(levelname)-8s %(name)-25s %(message)s"))
            logger.addHandler(hdlr)

    if hasattr(config, 'BOT_LOG_SENTRY') and config.BOT_LOG_SENTRY:
        sentry_integrations = []

        try:
            import sentry_sdk
            from sentry_sdk.integrations.logging import LoggingIntegration

        except ImportError:
            log.exception(
                "You have BOT_LOG_SENTRY enabled, but I couldn't import modules "
                "needed for Sentry integration. Did you install sentry-sdk? "
                "(See https://docs.sentry.io/platforms/python for installation instructions)"
            )
            exit(-1)

        sentry_logging = LoggingIntegration(
            level=config.SENTRY_LOGLEVEL, event_level=config.SENTRY_EVENTLEVEL)

        sentry_integrations.append(sentry_logging)

        if hasattr(config,
                   'BOT_LOG_SENTRY_FLASK') and config.BOT_LOG_SENTRY_FLASK:
            try:
                from sentry_sdk.integrations.flask import FlaskIntegration
            except ImportError:
                log.exception(
                    "You have BOT_LOG_SENTRY enabled, but I couldn't import modules "
                    "needed for Sentry integration. Did you install sentry-sdk[flask]? "
                    "(See https://docs.sentry.io/platforms/python/flask for installation instructions)"
                )
                exit(-1)

            sentry_integrations.append(FlaskIntegration())

        try:
            if hasattr(config, 'SENTRY_TRANSPORT') and isinstance(
                    config.SENTRY_TRANSPORT, tuple):
                mod = importlib.import_module(config.SENTRY_TRANSPORT[1])
                transport = getattr(mod, config.SENTRY_TRANSPORT[0])

                sentry_sdk.init(dsn=config.SENTRY_DSN,
                                integrations=sentry_integrations,
                                transport=transport)
            else:
                sentry_sdk.init(dsn=config.SENTRY_DSN,
                                integrations=sentry_integrations)
        except ImportError:
            log.exception(
                f'Unable to import selected SENTRY_TRANSPORT - {config.SENTRY_TRANSPORT}'
            )
            exit(-1)

    logger.setLevel(config.BOT_LOG_LEVEL)

    storage_plugin = get_storage_plugin(config)

    # init the botplugin manager
    botplugins_dir = path.join(config.BOT_DATA_DIR, PLUGINS_SUBDIR)
    if not path.exists(botplugins_dir):
        makedirs(botplugins_dir, mode=0o755)

    plugin_indexes = getattr(config, 'BOT_PLUGIN_INDEXES',
                             (PLUGIN_DEFAULT_INDEX, ))
    if isinstance(plugin_indexes, str):
        plugin_indexes = (plugin_indexes, )

    # Extra backend is expected to be a list type, convert string to list.
    extra_backend = getattr(config, 'BOT_EXTRA_BACKEND_DIR', [])
    if isinstance(extra_backend, str):
        extra_backend = [extra_backend]

    backendpm = BackendPluginManager(config, 'errbot.backends', backend_name,
                                     ErrBot, CORE_BACKENDS, extra_backend)

    log.info(f'Found Backend plugin: {backendpm.plugin_info.name}')

    repo_manager = BotRepoManager(storage_plugin, botplugins_dir,
                                  plugin_indexes)

    try:
        bot = backendpm.load_plugin()
        botpm = BotPluginManager(
            storage_plugin,
            config.BOT_EXTRA_PLUGIN_DIR, config.AUTOINSTALL_DEPS,
            getattr(config, 'CORE_PLUGINS',
                    None), lambda name, clazz: clazz(bot, name),
            getattr(config, 'PLUGINS_CALLBACK_ORDER', (None, )))
        bot.attach_storage_plugin(storage_plugin)
        bot.attach_repo_manager(repo_manager)
        bot.attach_plugin_manager(botpm)
        bot.initialize_backend_storage()

        # restore the bot from the restore script
        if restore:
            # Prepare the context for the restore script
            if 'repos' in bot:
                log.fatal('You cannot restore onto a non empty bot.')
                sys.exit(-1)
            log.info(f'**** RESTORING the bot from {restore}')
            restore_bot_from_backup(restore, bot=bot, log=log)
            print('Restore complete. You can restart the bot normally')
            sys.exit(0)

        errors = bot.plugin_manager.update_plugin_places(
            repo_manager.get_all_repos_paths())
        if errors:
            log.error('Some plugins failed to load:\n' +
                      '\n'.join(errors.values()))
            bot._plugin_errors_during_startup = "\n".join(errors.values())
        return bot
    except Exception:
        log.exception("Unable to load or configure the backend.")
        exit(-1)
Exemplo n.º 19
0
def make_application(config_filename=None, tests=False):
    if config_filename is None:
        config_filename = os.environ.get('ACOUSTID_CONFIG')

    script = Script(config_filename, tests=tests)
    script.setup_logging()

    config = script.config

    app = Flask('acoustid.web')
    app.config.update(
        DEBUG=config.website.debug,
        SECRET_KEY=config.website.secret,
        MB_OAUTH_CLIENT_ID=config.website.mb_oauth_client_id,
        MB_OAUTH_CLIENT_SECRET=config.website.mb_oauth_client_secret,
        GOOGLE_OAUTH_CLIENT_ID=config.website.google_oauth_client_id,
        GOOGLE_OAUTH_CLIENT_SECRET=config.website.google_oauth_client_secret,
    )

    app.acoustid_script = script
    app.acoustid_config = config
    app.acoustid_config_filename = config_filename

    app.wsgi_app = ProxyFix(app.wsgi_app)

    sentry_sdk.init(config.sentry.web_dsn,
                    release=GIT_RELEASE,
                    integrations=[FlaskIntegration()])

    # can't use json because of python-openid
    app.session_interface = SecureCookieSessionInterface()
    app.session_interface.serializer = pickle

    @app.context_processor
    def inject_common_values():
        show_donate_banner = False
        if datetime.date.today().month in (11, 12):
            show_donate_banner = True
        return dict(
            account_id=session.get('id'),
            show_maintenace_banner=config.website.maintenance,
            show_donate_banner=show_donate_banner,
            morris_js_version='0.5.1',
            raphael_js_version='2.1.4',
            bootstrap_version='3.3.6',
            jquery_version='1.12.0',
        )

    def get_flask_request_scope():
        try:
            return id(request._get_current_object())
        except RuntimeError:
            return 0

    @app.teardown_request
    def close_db_session(*args, **kwargs):
        db.session.remove()

    @app.route('/_health')
    def health():
        from acoustid.api import get_health_response
        return get_health_response(script, request, require_master=True)

    @app.route('/_health_docker')
    def health_docker():
        from acoustid.api import get_health_response
        return get_health_response(script, request)

    db.session_factory.configure(bind=config.database.create_engine())
    db.session = scoped_session(db.session_factory,
                                scopefunc=get_flask_request_scope)

    app.register_blueprint(general_page)
    app.register_blueprint(user_page)
    app.register_blueprint(apps_page)
    app.register_blueprint(metadata_page)
    app.register_blueprint(stats_page)
    app.register_blueprint(admin_page)

    return app
Exemplo n.º 20
0
import os
import sentry_sdk
import numpy as np
from flask_cors import CORS
from werkzeug.utils import secure_filename
from flask_wtf.csrf import CSRFProtect, CSRFError
from sentry_sdk.integrations.flask import FlaskIntegration
from flask import Flask, request, redirect, url_for, render_template, jsonify
from config import FLASK_SECRET_KEY, UPLOAD_FOLDER, ALLOWED_EXTENSIONS, LABELS, WTF_CSRF_TIME_LIMIT, SENTRY_INIT
from processing import get_label, upload_file_to_s3, allowed_file

# Sentry Initialization
sentry_sdk.init(dsn=SENTRY_INIT, integrations=[FlaskIntegration()])

# Flask config
app = Flask(__name__)
app.secret_key = FLASK_SECRET_KEY
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
app.config['WTF_CSRF_TIME_LIMIT'] = WTF_CSRF_TIME_LIMIT
CSRFProtect(app)
CORS(app)


@app.route("/", methods=["GET"])
def main():
    return render_template('index.html')


@app.route('/', methods=['POST'])
def upload_image():
    if 'file' not in request.files:
Exemplo n.º 21
0
def add_sentry():
    SENTRY_DSN = env.get('SENTRY_DSN', None)
    if SENTRY_DSN:
        sentry_sdk.init(dsn=SENTRY_DSN, integrations=[FlaskIntegration()])
Exemplo n.º 22
0
def create_app():
    """Initialize the App"""
    app = Flask(__name__,
                static_url_path="/static",
                static_folder=path.join(path.abspath(path.dirname(__file__)),
                                        "static"))
    """Flask Config"""
    app_settings = getenv("APP_SETTINGS", "src.config.ProductionConfig")
    app.config.from_object(app_settings)

    if (app_settings == "src.config.ProductionConfig"
            and not app.config.get("SEND_MAIL")):
        app.logger.warning("Sending Emails disabled on production!")
    """Set FLASK_ENV and FLASK_DEBUG cause that doesn't happen auto anymore"""
    if app.config.get("DEBUG"):
        environ["FLASK_ENV"] = "development"  # pragma: nocover
        environ["FLASK_DEBUG"] = "1"  # pragma: nocover

    if app.config.get("SENTRY_DSN"):
        """Initialize Sentry if we're in production"""
        sentry_sdk.init(dsn=app.config.get("SENTRY_DSN"),
                        environment=app.config.get("SENTRY_ENV"),
                        release=f"backend@{__version__}",
                        integrations=[FlaskIntegration(),
                                      CeleryIntegration()],
                        traces_sample_rate=1.0)
    """Setup Extensions"""
    CORS(app)
    db.init_app(app)
    swagger.init_app(app)
    mail.init_app(app)
    bcrypt.init_app(app)
    socketio.init_app(app,
                      cors_allowed_origins="*",
                      json=json,
                      message_queue=app.config.get("SOCKETIO_MESSAGE_QUEUE"))

    from src.common.json import JSONEncoderBase
    app.json_encoder = JSONEncoderBase
    """Register Blueprints"""
    from src.api.auth import auth_blueprint
    from src.api.hackers import hackers_blueprint
    from src.api.sponsors import sponsors_blueprint
    from src.api.stats import stats_blueprint
    from src.api.events import events_blueprint
    from src.api.club_events import club_events_blueprint
    from src.api.email_verification import email_verify_blueprint

    app.register_blueprint(auth_blueprint, url_prefix="/api")
    app.register_blueprint(hackers_blueprint, url_prefix="/api")
    app.register_blueprint(sponsors_blueprint, url_prefix="/api")
    app.register_blueprint(stats_blueprint, url_prefix="/api")
    app.register_blueprint(events_blueprint, url_prefix="/api")
    app.register_blueprint(club_events_blueprint, url_prefix="/api")
    app.register_blueprint(email_verify_blueprint, url_prefix="/api")
    """Register Error Handlers"""
    from src.common import error_handlers

    app.register_error_handler(HTTPException, error_handlers.handle_exception)
    """Initialize Celery"""
    celery = make_celery(app)

    @before_render_template.connect_via(app)
    def _sentry_pre_render_template(sender, template, context, **extra):

        parent = sentry_sdk.Hub.current.scope.span
        if parent is not None:
            span = parent.start_child(op="flask.render_template")

            span.set_data("flask.render_template.sender", sender)
            span.set_data("flask.render_template.template", template)
            span.set_data("flask.render_template.context", context)
            span.set_data("flask.render_template.extra", extra)

            g._sentry_span_render_template = span

    @template_rendered.connect_via(app)
    def _sentry_template_rendered(sender, template, context, **extra):
        span = g.pop("_sentry_span_render_template", None)

        if span is not None:
            span.finish()

    return app, celery
Exemplo n.º 23
0
import logging
import time
import os
import pickle
from pathlib import Path

import sentry_sdk
import tokenize_util
import torch
from flask import Flask, request, jsonify
from infer import infill_with_ilm
from sentry_sdk.integrations.flask import FlaskIntegration
from transformers import GPT2LMHeadModel


sentry_sdk.init(dsn=os.getenv("SENTRY_DSN"), integrations=[FlaskIntegration()])

logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", level=logging.INFO)
logger = logging.getLogger(__name__)

MODEL_DIR = os.environ.get("MODEL_DIR", "/data/")
logging.info(f"MODEL_DIR = {MODEL_DIR}")
# MASK_ID = 103

try:
    if torch.cuda.is_available():
        device = torch.device("cuda")
    else:
        device = torch.device("cpu")

    logger.info(f"infilling is set to run on {device}")
Exemplo n.º 24
0
def create_app():
    import config
    import logging.config
    import os
    from flask_jwt_extended import JWTManager

    from . import api, models
    from app import helpers
    from app import services
    from app import command

    def load_app_config(app):
        """
        Load app's configurations
        :param flask.Flask app:
        :return:
        """
        app.config.from_object(config)
        app.config.from_pyfile('config.py', silent=True)

    app = flask.Flask(__name__,
                      instance_relative_config=True,
                      instance_path=os.path.join(config.ROOT_DIR, 'instance'))
    load_app_config(app)

    # Register new flask project here and get new dsn: https://sentry.io
    dns = SENTRY_DSN if os.environ.get('SEND_REPORT') == 'true' else None

    app.config['SENTRY_CONFIG'] = {
        'ignore_exceptions': [
            NotFoundException, UnAuthorizedException, BadRequestException,
            ForbiddenException
        ],
        'level':
        logging.ERROR,
    }

    sentry_sdk.init(dsn=dns,
                    integrations=[FlaskIntegration()],
                    environment=app.config['ENV_MODE'],
                    in_app_exclude=['app.extensions.exceptions'],
                    before_send=before_send)

    # setup jwt extended
    app.config['JWT_SECRET_KEY'] = os.environ['SECRET_KEY']
    app.config['JWT_TOKEN_LOCATION'] = ['headers']
    # How long an access token should live before it expires. Set by minutes (int)
    app.config['JWT_ACCESS_TOKEN_EXPIRES'] = int(
        os.environ['TOKEN_UPTIME']) * 60
    app.config['JWT_COOKIE_CSRF_PROTECT'] = False
    from app.commons.uet_constant import ACCESS_TOKEN_KEY, EMPTY_STRING
    app.config['JWT_HEADER_NAME'] = ACCESS_TOKEN_KEY
    app.config['JWT_HEADER_TYPE'] = EMPTY_STRING

    jwt = JWTManager(app)

    app.config['CORS_SUPPORTS_CREDENTIALS'] = True

    # setup logging
    logging.config.fileConfig(app.config['LOGGING_CONFIG_FILE'],
                              disable_existing_loggers=False)

    app.secret_key = config.FLASK_APP_SECRET_KEY
    models.init_app(app)
    api.init_app(app)
    services.init_app(app)
    command.init_command(app)

    CORS(app)
    return app
Exemplo n.º 25
0
import logging
import os

from flask import Flask, request, current_app
from flask_basicauth import BasicAuth
from flask_migrate import Migrate
from flask_sqlalchemy import SQLAlchemy

import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration

from config import Config

sentry_sdk.init(dsn=Config.SENTRY_DSN, integrations=[FlaskIntegration()])

basic_auth = BasicAuth()
db = SQLAlchemy()
migrate = Migrate()


def create_app(config_class=Config):
    app = Flask(__name__)
    app.config.from_object(config_class)

    basic_auth.init_app(app)
    db.init_app(app)
    migrate.init_app(app, db)

    from app.contracts import bp as contracts_bp
    app.register_blueprint(contracts_bp)
Exemplo n.º 26
0
def create_app(test_config=None):
    if dsn := os.getenv('SENTRY_DSN'):
        sentry_sdk.init(dsn=dsn, integrations=[FlaskIntegration(), SqlalchemyIntegration()])
Exemplo n.º 27
0
# Blog configuration values.

import sentry_sdk
from flask import Flask
from flask_caching import Cache
from playhouse.flask_utils import FlaskDB
from playhouse.pool import PooledMySQLDatabase, PooledPostgresqlDatabase
from sentry_sdk.integrations.flask import FlaskIntegration

import config

DATABASE = PooledPostgresqlDatabase(**config.db)
if config.cache:
    CACHE_TYPE = "redis"
else:
    CACHE_TYPE = "null"
CACHE_REDIS_DB = config.redisDB

if config.sentryDSN:
    sentry_sdk.init(dsn=config.sentryDSN, integrations=[FlaskIntegration()])

# Create a Flask WSGI app and configure it using values from the module.
app = Flask(__name__)
app.config.from_object(__name__)
cache = Cache(app)

flask_db = FlaskDB(app)

db = flask_db.database
Exemplo n.º 28
0
from flask_cors import CORS
from flasgger import Swagger
from sentry_sdk.integrations.flask import FlaskIntegration

from typing import Optional, MutableMapping, Any, Tuple, Union

from multinet import auth
from multinet.auth import google
from multinet import api
from multinet.db import register_legacy_workspaces
from multinet import uploaders, downloaders
from multinet.errors import ServerError
from multinet.util import load_secret_key, regex_allowed_origins, get_allowed_origins

sentry_dsn = os.getenv("SENTRY_DSN", default="")
sentry_sdk.init(dsn=sentry_dsn, integrations=[FlaskIntegration()])


def create_app(config: Optional[MutableMapping] = None) -> Flask:
    """Create a Multinet app instance."""
    app = Flask(__name__)

    if config is not None:
        app.config.update(config)

    CORS(
        app,
        origins=regex_allowed_origins(get_allowed_origins()),
        supports_credentials=True,
    )
    Swagger(app, template_file="swagger/template.yaml")
Exemplo n.º 29
0
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration
from sentry_sdk.integrations.sqlalchemy import SqlalchemyIntegration
from flask_cors import CORS

from model import db
from utils import register_all_error_handlers, rechaptcha

from views import UploadView

app = Flask(__name__)
app.config.from_object(Config)

if Config.SENTRY_DSN:
    sentry_sdk.init(dsn=Config.SENTRY_DSN,
                    integrations=[FlaskIntegration(),
                                  SqlalchemyIntegration()],
                    send_default_pii=True,
                    release=Config.RELEASE_ID,
                    environment=Config.RELEASEMODE)

db.init_app(app)
register_all_error_handlers(app)
CORS(app)
rechaptcha.init_app(app)


@app.before_first_request
def init_db():
    db.create_all()
Exemplo n.º 30
0
def create_app():
    global app_created
    if not app_created:
        BlueprintsManager.register(app)
    Migrate(app, db)

    app.config.from_object(env('APP_CONFIG', default='config.ProductionConfig'))
    db.init_app(app)
    _manager = Manager(app)
    _manager.add_command('db', MigrateCommand)

    if app.config['CACHING']:
        cache.init_app(app, config={'CACHE_TYPE': 'simple'})
    else:
        cache.init_app(app, config={'CACHE_TYPE': 'null'})

    stripe.api_key = 'SomeStripeKey'
    app.secret_key = 'super secret key'
    app.config['JSONIFY_PRETTYPRINT_REGULAR'] = False
    app.config['FILE_SYSTEM_STORAGE_FILE_VIEW'] = 'static'

    app.logger.addHandler(logging.StreamHandler(sys.stdout))
    app.logger.setLevel(logging.ERROR)

    # set up jwt
    app.config['JWT_HEADER_TYPE'] = 'JWT'
    app.config['JWT_ACCESS_TOKEN_EXPIRES'] = timedelta(days=1)
    app.config['JWT_REFRESH_TOKEN_EXPIRES'] = timedelta(days=365)
    app.config['JWT_ERROR_MESSAGE_KEY'] = 'error'
    app.config['JWT_TOKEN_LOCATION'] = ['cookies', 'headers']
    app.config['JWT_REFRESH_COOKIE_PATH'] = '/v1/auth/token/refresh'
    app.config['JWT_SESSION_COOKIE'] = False
    app.config['JWT_BLACKLIST_ENABLED'] = True
    app.config['JWT_BLACKLIST_TOKEN_CHECKS'] = ['refresh']
    _jwt = JWTManager(app)
    _jwt.user_loader_callback_loader(jwt_user_loader)
    _jwt.token_in_blacklist_loader(is_token_blacklisted)

    # setup celery
    app.config['CELERY_BROKER_URL'] = app.config['REDIS_URL']
    app.config['CELERY_RESULT_BACKEND'] = app.config['CELERY_BROKER_URL']
    app.config['CELERY_ACCEPT_CONTENT'] = ['json', 'application/text']

    CORS(app, resources={r"/*": {"origins": "*"}})
    AuthManager.init_login(app)

    if app.config['TESTING'] and app.config['PROFILE']:
        # Profiling
        app.wsgi_app = ProfilerMiddleware(app.wsgi_app, restrictions=[30])

    # development api
    with app.app_context():
        from app.api.admin_statistics_api.events import event_statistics
        from app.api.auth import auth_routes
        from app.api.attendees import attendee_misc_routes
        from app.api.bootstrap import api_v1
        from app.api.celery_tasks import celery_routes
        from app.api.event_copy import event_copy
        from app.api.exports import export_routes
        from app.api.imports import import_routes
        from app.api.uploads import upload_routes
        from app.api.users import user_misc_routes
        from app.api.orders import order_misc_routes
        from app.api.role_invites import role_invites_misc_routes
        from app.api.auth import ticket_blueprint, authorised_blueprint
        from app.api.admin_translations import admin_blueprint
        from app.api.orders import alipay_blueprint
        from app.api.settings import admin_misc_routes

        app.register_blueprint(api_v1)
        app.register_blueprint(event_copy)
        app.register_blueprint(upload_routes)
        app.register_blueprint(export_routes)
        app.register_blueprint(import_routes)
        app.register_blueprint(celery_routes)
        app.register_blueprint(auth_routes)
        app.register_blueprint(event_statistics)
        app.register_blueprint(user_misc_routes)
        app.register_blueprint(attendee_misc_routes)
        app.register_blueprint(order_misc_routes)
        app.register_blueprint(role_invites_misc_routes)
        app.register_blueprint(ticket_blueprint)
        app.register_blueprint(authorised_blueprint)
        app.register_blueprint(admin_blueprint)
        app.register_blueprint(alipay_blueprint)
        app.register_blueprint(admin_misc_routes)

    sa.orm.configure_mappers()

    if app.config['SERVE_STATIC']:
        app.add_url_rule('/static/<path:filename>',
                         endpoint='static',
                         view_func=app.send_static_file)

    # sentry
    if not app_created and 'SENTRY_DSN' in app.config:
        sentry_sdk.init(app.config['SENTRY_DSN'], integrations=[FlaskIntegration()])

    # redis
    redis_store.init_app(app)

    # elasticsearch
    if app.config['ENABLE_ELASTICSEARCH']:
        client.init_app(app)
        connections.add_connection('default', client.elasticsearch)
        with app.app_context():
            try:
                cron_rebuild_events_elasticsearch.delay()
            except Exception:
                pass

    app_created = True
    return app, _manager, db, _jwt