Exemplo n.º 1
0
    async def get_database(self, name: str) -> IDatabase:
        if name not in self.app:
            config = deepcopy(self.config)
            config['dsn'] = self.get_dsn(name)
            factory = get_utility(IDatabaseConfigurationFactory,
                                  name=config['storage'])
            self.app[name] = await apply_coroutine(factory, name, config)
            await notify(DatabaseInitializedEvent(self.app[name]))

        return self.app[name]
Exemplo n.º 2
0
    async def get_database(self, name: str) -> IDatabase:
        if name not in self.app:
            config = deepcopy(self.config)
            config.update({
                'dsn': self.get_dsn(name),
                'objects_table_name': name + '_objects',
                'blobs_table_name': name + '_blobs'
            })
            if self.config['storage_id'] in self._pools:
                config['pool'] = self._pools[self.config['storage_id']]
            factory = get_utility(IDatabaseConfigurationFactory,
                                  name=config['storage'])
            self.app[name] = await apply_coroutine(factory, name, config)

            if self.config['storage_id'] not in self._pools:
                storage = self.app[name].storage
                self._pools[self.config[
                    'storage_id']] = await storage.get_pool(  # noqa
                        **_get_connection_options(self.config))
            await notify(DatabaseInitializedEvent(self.app[name]))

        return self.app[name]
Exemplo n.º 3
0
async def make_app(config_file=None,
                   settings=None,
                   loop=None,
                   server_app=None):
    """
    Make application from configuration

    :param config_file: path to configuration file to load
    :param settings: dictionary of settings
    :param loop: if not using with default event loop
    :param settings: provide your own aiohttp application
    """
    # reset app_settings
    startup_vars = {}
    for key in app_settings.keys():
        if key[0] == "_":
            startup_vars[key] = app_settings[key]

    app_settings.clear()
    app_settings.update(startup_vars)
    app_settings.update(deepcopy(default_settings))

    if loop is None:
        loop = asyncio.get_event_loop()

    if config_file is not None:
        with open(config_file, "r") as config:
            settings = json.load(config)
    elif settings is None:
        raise Exception("Neither configuration or settings")

    # Create root Application
    root = ApplicationRoot(config_file, loop)
    provide_utility(root, IApplication, "root")

    # Initialize global (threadlocal) ZCA configuration
    config = root.config = ConfigurationMachine()

    app_configurator = ApplicationConfigurator(
        settings.get("applications") or [], config, root, settings)

    configure.scan("guillotina.renderers")
    configure.scan("guillotina.api")
    configure.scan("guillotina.content")
    configure.scan("guillotina.registry")
    configure.scan("guillotina.auth")
    configure.scan("guillotina.json")
    configure.scan("guillotina.behaviors")
    configure.scan("guillotina.languages")
    configure.scan("guillotina.permissions")
    configure.scan("guillotina.security.security_local")
    configure.scan("guillotina.security.policy")
    configure.scan("guillotina.catalog.index")
    configure.scan("guillotina.catalog.catalog")
    configure.scan("guillotina.files")
    configure.scan("guillotina.annotations")
    configure.scan("guillotina.constraintypes")
    configure.scan("guillotina.subscribers")
    configure.scan("guillotina.db.strategies")
    configure.scan("guillotina.db.storages.vacuum")
    configure.scan("guillotina.db.cache")
    configure.scan("guillotina.db.writer")
    configure.scan("guillotina.db.factory")
    configure.scan("guillotina.exc_resp")
    configure.scan("guillotina.fields")
    configure.scan("guillotina.migrations")

    # always load guillotina
    app_configurator.configure_application("guillotina")
    app_configurator.configure_all_applications()

    apply_concrete_behaviors()

    # update *after* plugins loaded
    update_app_settings(settings)

    if "logging" in app_settings:
        try:
            logging.config.dictConfig(app_settings["logging"])
        except Exception:
            app_logger.error("Could not setup logging configuration",
                             exc_info=True)

    # Make and initialize aiohttp app
    if server_app is None:
        server_app = make_aiohttp_application()
    root.app = server_app
    server_app.root = root
    server_app.config = config

    for k, v in _moved.items():
        # for b/w compatibility, convert these
        if k in app_settings:
            app_settings[v] = app_settings[k]
            del app_settings[k]

    optimize_settings(app_settings)

    await notify(ApplicationConfiguredEvent(server_app, loop))

    for key, dbconfig in list_or_dict_items(app_settings["databases"]):
        factory = get_utility(IDatabaseConfigurationFactory,
                              name=dbconfig["storage"])
        root[key] = await factory(key, dbconfig, loop)
        await notify(DatabaseInitializedEvent(root[key]))

    for key, file_path in list_or_dict_items(app_settings["static"]):
        path = resolve_path(file_path).resolve()
        if not path.exists():
            raise Exception("Invalid static directory {}".format(file_path))
        if path.is_dir():
            root[key] = StaticDirectory(path)
        else:
            root[key] = StaticFile(path)

    for key, file_path in list_or_dict_items(app_settings["jsapps"]):
        path = resolve_path(file_path).resolve()
        if not path.exists() or not path.is_dir():
            raise Exception("Invalid jsapps directory {}".format(file_path))
        root[key] = JavaScriptApplication(path)

    root.set_root_user(app_settings["root_user"])

    if app_settings.get("jwk") and app_settings.get("jwk").get(
            "k") and app_settings.get("jwk").get("kty"):
        key = jwk.JWK.from_json(json.dumps(app_settings.get("jwk")))
        app_settings["jwk"] = key
        # {"k":"QqzzWH1tYqQO48IDvW7VH7gvJz89Ita7G6APhV-uLMo","kty":"oct"}

    if not app_settings.get("debug") and app_settings["jwt"].get("secret"):
        # validate secret
        secret = app_settings["jwt"]["secret"]
        if secret == "secret":
            app_logger.warning(
                "You are using a very insecure secret key in production mode. "
                "It is strongly advised that you provide a better value for "
                "`jwt.secret` in your config.")
        elif not secure_passphrase(app_settings["jwt"]["secret"]):
            app_logger.warning(
                "You are using a insecure secret key in production mode. "
                "It is recommended that you provide a more complex value for "
                "`jwt.secret` in your config.")

    # Set router root
    server_app.router.set_root(root)
    server_app.on_cleanup.append(cleanup_app)

    for key, util in app_settings["load_utilities"].items():
        app_logger.info("Adding " + key + " : " + util["provides"])
        await notify(BeforeAsyncUtilityLoadedEvent(key, util))
        result = root.add_async_utility(key, util, loop=loop)
        if result is not None:
            await notify(AfterAsyncUtilityLoadedEvent(key, util, *result))

    # Load cached Schemas
    load_cached_schema()

    await notify(ApplicationInitializedEvent(server_app, loop))

    return server_app
Exemplo n.º 4
0
async def make_app(config_file=None,
                   settings=None,
                   loop=None,
                   server_app=None):

    # reset app_settings
    app_settings.clear()
    app_settings.update(default_settings)

    if loop is None:
        loop = asyncio.get_event_loop()

    # chainmap task factory is actually very important
    # default task factory uses inheritance in a way
    # that bubbles back down. So it's possible for a sub-task
    # to clear out the request of the parent task
    loop.set_task_factory(aiotask_context.chainmap_task_factory)

    if config_file is not None:
        with open(config_file, 'r') as config:
            settings = json.load(config)
    elif settings is None:
        raise Exception('Neither configuration or settings')

    # Create root Application
    root = ApplicationRoot(config_file, loop)
    provide_utility(root, IApplication, 'root')

    # Initialize global (threadlocal) ZCA configuration
    config = root.config = ConfigurationMachine()

    import guillotina
    import guillotina.db.factory
    import guillotina.db.writer
    import guillotina.db.db
    configure.scan('guillotina.renderers')
    configure.scan('guillotina.api')
    configure.scan('guillotina.content')
    configure.scan('guillotina.registry')
    configure.scan('guillotina.auth')
    configure.scan('guillotina.json')
    configure.scan('guillotina.behaviors')
    configure.scan('guillotina.languages')
    configure.scan('guillotina.permissions')
    configure.scan('guillotina.security.security_local')
    configure.scan('guillotina.security.policy')
    configure.scan('guillotina.auth.participation')
    configure.scan('guillotina.catalog.index')
    configure.scan('guillotina.catalog.catalog')
    configure.scan('guillotina.files')
    configure.scan('guillotina.annotations')
    configure.scan('guillotina.constraintypes')
    configure.scan('guillotina.subscribers')
    configure.scan('guillotina.db.strategies')
    configure.scan('guillotina.db.cache')
    configure.scan('guillotina.exc_resp')
    configure.scan('guillotina.fields')
    load_application(guillotina, root, settings)
    config.execute_actions()
    config.commit()

    configured = ['guillotina']
    for module_name in settings.get('applications') or []:
        configure_application(module_name, config, root, settings, configured)

    apply_concrete_behaviors()

    # update *after* plugins loaded
    update_app_settings(settings)

    if 'logging' in app_settings:
        logging.config.dictConfig(app_settings['logging'])

    # Make and initialize aiohttp app
    if server_app is None:
        server_app = make_aiohttp_application()
    root.app = server_app
    server_app.root = root
    server_app.config = config

    optimize_settings(app_settings)

    await notify(ApplicationConfiguredEvent(server_app, loop))

    for key, dbconfig in list_or_dict_items(app_settings['databases']):
        factory = get_utility(IDatabaseConfigurationFactory,
                              name=dbconfig['storage'])
        root[key] = await factory(key, dbconfig, loop)
        await notify(DatabaseInitializedEvent(root[key]))

    for key, file_path in list_or_dict_items(app_settings['static']):
        path = resolve_path(file_path).resolve()
        if not path.exists():
            raise Exception('Invalid static directory {}'.format(file_path))
        if path.is_dir():
            root[key] = StaticDirectory(path)
        else:
            root[key] = StaticFile(path)

    for key, file_path in list_or_dict_items(app_settings['jsapps']):
        path = resolve_path(file_path).resolve()
        if not path.exists() or not path.is_dir():
            raise Exception('Invalid jsapps directory {}'.format(file_path))
        root[key] = JavaScriptApplication(path)

    root.set_root_user(app_settings['root_user'])

    if RSA is not None and not app_settings.get('rsa'):
        key = RSA.generate(2048)
        pub_jwk = {'k': key.publickey().exportKey('PEM')}
        priv_jwk = {'k': key.exportKey('PEM')}
        app_settings['rsa'] = {'pub': pub_jwk, 'priv': priv_jwk}

    # Set router root
    server_app.router.set_root(root)
    server_app.on_cleanup.append(cleanup_app)

    for util in app_settings.get('utilities') or []:
        logger.warn('Adding : ' + util['provides'])
        root.add_async_utility(util['provides'], util, loop=loop)

    for key, util in app_settings['load_utilities'].items():
        logger.info('Adding ' + key + ' : ' + util['provides'])
        root.add_async_utility(key, util, loop=loop)

    # Load cached Schemas
    load_cached_schema()

    await notify(ApplicationInitializedEvent(server_app, loop))

    return server_app
Exemplo n.º 5
0
async def make_app(config_file=None,
                   settings=None,
                   loop=None,
                   server_app=None):
    '''
    Make application from configuration

    :param config_file: path to configuration file to load
    :param settings: dictionary of settings
    :param loop: if not using with default event loop
    :param settings: provide your own aiohttp application
    '''
    # reset app_settings
    startup_vars = {}
    for key in app_settings.keys():
        if key[0] == '_':
            startup_vars[key] = app_settings[key]

    app_settings.clear()
    app_settings.update(startup_vars)
    app_settings.update(default_settings)

    if loop is None:
        loop = asyncio.get_event_loop()

    # chainmap task factory is actually very important
    # default task factory uses inheritance in a way
    # that bubbles back down. So it's possible for a sub-task
    # to clear out the request of the parent task
    loop.set_task_factory(aiotask_context.chainmap_task_factory)

    if config_file is not None:
        with open(config_file, 'r') as config:
            settings = json.load(config)
    elif settings is None:
        raise Exception('Neither configuration or settings')

    # Create root Application
    root = ApplicationRoot(config_file, loop)
    provide_utility(root, IApplication, 'root')

    # Initialize global (threadlocal) ZCA configuration
    config = root.config = ConfigurationMachine()

    import guillotina
    import guillotina.db.factory
    import guillotina.db.writer
    import guillotina.db.db
    configure.scan('guillotina.renderers')
    configure.scan('guillotina.api')
    configure.scan('guillotina.content')
    configure.scan('guillotina.registry')
    configure.scan('guillotina.auth')
    configure.scan('guillotina.json')
    configure.scan('guillotina.behaviors')
    configure.scan('guillotina.languages')
    configure.scan('guillotina.permissions')
    configure.scan('guillotina.security.security_local')
    configure.scan('guillotina.security.policy')
    configure.scan('guillotina.auth.participation')
    configure.scan('guillotina.catalog.index')
    configure.scan('guillotina.catalog.catalog')
    configure.scan('guillotina.files')
    configure.scan('guillotina.annotations')
    configure.scan('guillotina.constraintypes')
    configure.scan('guillotina.subscribers')
    configure.scan('guillotina.db.strategies')
    configure.scan('guillotina.db.cache')
    configure.scan('guillotina.exc_resp')
    configure.scan('guillotina.fields')
    configure.scan('guillotina.migrations')
    load_application(guillotina, root, settings)
    config.execute_actions()
    config.commit()

    configured = ['guillotina']
    for module_name in settings.get('applications') or []:
        configure_application(module_name, config, root, settings, configured)

    apply_concrete_behaviors()

    # update *after* plugins loaded
    update_app_settings(settings)

    if 'logging' in app_settings:
        logging.config.dictConfig(app_settings['logging'])

    # Make and initialize aiohttp app
    if server_app is None:
        server_app = make_aiohttp_application()
    root.app = server_app
    server_app.root = root
    server_app.config = config

    optimize_settings(app_settings)

    await notify(ApplicationConfiguredEvent(server_app, loop))

    for key, dbconfig in list_or_dict_items(app_settings['databases']):
        factory = get_utility(IDatabaseConfigurationFactory,
                              name=dbconfig['storage'])
        root[key] = await factory(key, dbconfig, loop)
        await notify(DatabaseInitializedEvent(root[key]))

    for key, file_path in list_or_dict_items(app_settings['static']):
        path = resolve_path(file_path).resolve()
        if not path.exists():
            raise Exception('Invalid static directory {}'.format(file_path))
        if path.is_dir():
            root[key] = StaticDirectory(path)
        else:
            root[key] = StaticFile(path)

    for key, file_path in list_or_dict_items(app_settings['jsapps']):
        path = resolve_path(file_path).resolve()
        if not path.exists() or not path.is_dir():
            raise Exception('Invalid jsapps directory {}'.format(file_path))
        root[key] = JavaScriptApplication(path)

    root.set_root_user(app_settings['root_user'])

    if app_settings.get('jwk') and\
            app_settings.get('jwk').get('k') and\
            app_settings.get('jwk').get('kty'):
        key = jwk.JWK.from_json(json.dumps(app_settings.get('jwk')))
        app_settings['jwk'] = key
        # {"k":"QqzzWH1tYqQO48IDvW7VH7gvJz89Ita7G6APhV-uLMo","kty":"oct"}

    if not app_settings.get('debug') and app_settings['jwt'].get('secret'):
        # validate secret
        secret = app_settings['jwt']['secret']
        if secret == 'secret':
            logger.warning(
                'You are using a very insecure secret key in production mode. '
                'It is strongly advised that you provide a better value for '
                '`jwt.secret` in your config.')
        elif not secure_passphrase(app_settings['jwt']['secret']):
            logger.warning(
                'You are using a insecure secret key in production mode. '
                'It is recommended that you provide a more complex value for '
                '`jwt.secret` in your config.')

    # Set router root
    server_app.router.set_root(root)
    server_app.on_cleanup.append(cleanup_app)

    for util in app_settings.get('utilities') or []:
        logger.warn('Adding : ' + util['provides'])
        root.add_async_utility(util['provides'], util, loop=loop)

    for key, util in app_settings['load_utilities'].items():
        logger.info('Adding ' + key + ' : ' + util['provides'])
        root.add_async_utility(key, util, loop=loop)

    # Load cached Schemas
    load_cached_schema()

    await notify(ApplicationInitializedEvent(server_app, loop))

    return server_app