Exemplo n.º 1
0
    async def _backend_factory(populated=True, config={}, event_bus=None):
        config = BackendConfig(
            **{
                "administration_token": "s3cr3t",
                "db_drop_deleted_data": False,
                "db_min_connections": 1,
                "db_max_connections": 5,
                "debug": False,
                "db_url": backend_store,
                "blockstore_config": blockstore,
                **config,
            })
        if not event_bus:
            event_bus = event_bus_factory()
        # TODO: backend connection to postgresql will timeout if we use a trio
        # mock clock with autothreshold. We should detect this and do something here...
        async with backend_app_factory(config, event_bus=event_bus) as backend:
            if populated:
                with freeze_time("2000-01-01"):
                    binder = backend_data_binder_factory(backend)
                    await binder.bind_organization(coolorg, alice)
                    await binder.bind_organization(
                        expiredorg,
                        expiredalice,
                        expiration_date=pendulum.now())
                    await binder.bind_organization(otherorg, otheralice)
                    await binder.bind_device(alice2)
                    await binder.bind_device(adam)
                    await binder.bind_device(bob)

            yield backend
Exemplo n.º 2
0
async def _run_backend(
    host: str,
    port: int,
    ssl_context: Optional[ssl.SSLContext],
    retry_policy: RetryPolicy,
    app_config: BackendConfig,
) -> None:
    # Loop over connection attempts
    while True:
        try:
            # New connection attempt
            retry_policy.new_attempt()

            # Run the backend app (and connect to the database)
            async with backend_app_factory(config=app_config) as backend:

                # Connection is successful, reset the retry policy
                retry_policy.success()

                # Serve backend through TCP
                await _serve_backend(backend, host, port, ssl_context)

        except ConnectionError as exc:
            # The maximum number of attempt is reached
            if retry_policy.is_expired():
                raise
            # Connection with the DB is dead, restart everything
            logger.warning(
                f"Database connection lost ({exc}), retrying in {retry_policy.pause_before_retry} seconds"
            )
            await retry_policy.pause()
Exemplo n.º 3
0
    async def _backend_factory(populated=True, config={}, event_bus=None):
        ssl_context = fixtures_customization.get("backend_over_ssl", False)
        nonlocal backend_store, blockstore
        if fixtures_customization.get("backend_force_mocked"):
            backend_store = "MOCKED"
            assert fixtures_customization.get("blockstore_mode",
                                              "NO_RAID") == "NO_RAID"
            blockstore = MockedBlockStoreConfig()

        config = BackendConfig(
            **{
                "administration_token": "s3cr3t",
                "db_min_connections": 1,
                "db_max_connections": 5,
                "debug": False,
                "db_url": backend_store,
                "blockstore_config": blockstore,
                "email_config": None,
                "backend_addr": None,
                "forward_proto_enforce_https": None,
                "ssl_context": ssl_context if ssl_context else False,
                "organization_bootstrap_webhook_url": None,
                "organization_spontaneous_bootstrap": False,
                **config,
            })

        if not event_bus:
            event_bus = event_bus_factory()
        async with backend_app_factory(config, event_bus=event_bus) as backend:
            if populated:
                with freeze_time("2000-01-01"):
                    binder = backend_data_binder_factory(backend)
                    await binder.bind_organization(
                        coolorg,
                        alice,
                        initial_user_manifest=fixtures_customization.get(
                            "alice_initial_remote_user_manifest", "v1"),
                    )
                    await binder.bind_organization(expiredorg, expiredorgalice)
                    await backend.organization.update(
                        expiredorg.organization_id, is_expired=True)
                    await binder.bind_organization(otherorg, otheralice)
                    await binder.bind_device(alice2, certifier=alice)
                    await binder.bind_device(
                        adam,
                        certifier=alice2,
                        initial_user_manifest=fixtures_customization.get(
                            "adam_initial_remote_user_manifest", "v1"),
                    )
                    await binder.bind_device(
                        bob,
                        certifier=adam,
                        initial_user_manifest=fixtures_customization.get(
                            "bob_initial_remote_user_manifest", "v1"),
                    )

            yield backend
Exemplo n.º 4
0
        async def _run_backend():
            async with backend_app_factory(config=config) as backend:

                async def _serve_client(stream):
                    if ssl_context:
                        stream = trio.SSLStream(stream, ssl_context, server_side=True)

                    try:
                        await backend.handle_client(stream)

                    except Exception:
                        # If we are here, something unexpected happened...
                        logger.exception("Unexpected crash")
                        await stream.aclose()

                await trio.serve_tcp(_serve_client, port, host=host)
Exemplo n.º 5
0
    async def _backend_factory(populated=True, config={}, event_bus=None):
        ssl_context = fixtures_customization.get("backend_over_ssl", False)
        config = BackendConfig(
            **{
                "administration_token": "s3cr3t",
                "db_min_connections": 1,
                "db_max_connections": 5,
                "debug": False,
                "db_url": backend_store,
                "blockstore_config": blockstore,
                "email_config": None,
                "backend_addr": None,
                "forward_proto_enforce_https": None,
                "ssl_context": ssl_context if ssl_context else False,
                "spontaneous_organization_bootstrap": False,
                "organization_bootstrap_webhook_url": None,
                **config,
            })

        if not event_bus:
            event_bus = event_bus_factory()
        # TODO: backend connection to postgresql will timeout if we use a trio
        # mock clock with autothreshold. We should detect this and do something here...
        async with backend_app_factory(config, event_bus=event_bus) as backend:
            if populated:
                with freeze_time("2000-01-01"):
                    binder = backend_data_binder_factory(backend)
                    await binder.bind_organization(coolorg, alice)
                    await binder.bind_organization(
                        expiredorg,
                        expiredorgalice,
                        expiration_date=pendulum.now())
                    await binder.bind_organization(otherorg, otheralice)
                    await binder.bind_device(alice2, certifier=alice)
                    await binder.bind_device(adam, certifier=alice2)
                    await binder.bind_device(bob, certifier=adam)

            yield backend