Ejemplo n.º 1
0
async def backend(backend_factory, request, fixtures_customization,
                  backend_addr):
    populated = not fixtures_customization.get("backend_not_populated", False)
    config = {}
    tmpdir = tempfile.mkdtemp(prefix="tmp-email-folder-")
    config["email_config"] = MockedEmailConfig(
        sender="Parsec <*****@*****.**>", tmpdir=tmpdir)
    config["backend_addr"] = backend_addr
    if fixtures_customization.get("backend_spontaneous_organization_boostrap",
                                  False):
        config["spontaneous_organization_bootstrap"] = True
    if fixtures_customization.get("backend_has_webhook", False):
        # Invalid port, hence we should crash if by mistake we try to reach this url
        config[
            "organization_bootstrap_webhook_url"] = "http://example.com:888888/webhook"

    async with backend_factory(populated=populated, config=config) as backend:
        yield backend
Ejemplo n.º 2
0
def run_cmd(
    host: str,
    port: int,
    db: str,
    db_min_connections: int,
    db_max_connections: int,
    maximum_database_connection_attempts: int,
    pause_before_retry_database_connection: float,
    blockstore: BaseBlockStoreConfig,
    administration_token: str,
    spontaneous_organization_bootstrap: bool,
    organization_bootstrap_webhook: str,
    organization_initial_active_users_limit: int,
    organization_initial_user_profile_outsider_allowed: bool,
    backend_addr: BackendAddr,
    email_host: str,
    email_port: int,
    email_host_user: Optional[str],
    email_host_password: Optional[str],
    email_use_ssl: bool,
    email_use_tls: bool,
    email_sender: str,
    forward_proto_enforce_https: Optional[Tuple[bytes, bytes]],
    ssl_keyfile: Optional[Path],
    ssl_certfile: Optional[Path],
    log_level: str,
    log_format: str,
    log_file: Optional[str],
    sentry_dsn: Optional[str],
    sentry_environment: str,
    debug: bool,
    dev: bool,
) -> None:
    # Start a local backend

    with cli_exception_handler(debug):
        ssl_context: Optional[ssl.SSLContext]
        if ssl_certfile or ssl_keyfile:
            ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
            if ssl_certfile:
                ssl_context.load_cert_chain(ssl_certfile, ssl_keyfile)
            else:
                ssl_context.load_default_certs()
        else:
            ssl_context = None

        email_config: EmailConfig
        if email_host == "MOCKED":
            tmpdir = tempfile.mkdtemp(prefix="tmp-email-folder-")
            if email_sender:
                email_config = MockedEmailConfig(sender=email_sender,
                                                 tmpdir=tmpdir)
            else:
                email_config = MockedEmailConfig(sender=DEFAULT_EMAIL_SENDER,
                                                 tmpdir=tmpdir)
        else:
            if not email_sender:
                raise ValueError(
                    "--email-sender is required when --email-host is provided")
            email_config = SmtpEmailConfig(
                host=email_host,
                port=email_port,
                host_user=email_host_user,
                host_password=email_host_password,
                use_ssl=email_use_ssl,
                use_tls=email_use_tls,
                sender=email_sender,
            )

        app_config = BackendConfig(
            administration_token=administration_token,
            db_url=db,
            db_min_connections=db_min_connections,
            db_max_connections=db_max_connections,
            blockstore_config=blockstore,
            email_config=email_config,
            ssl_context=True if ssl_context else False,
            forward_proto_enforce_https=forward_proto_enforce_https,
            backend_addr=backend_addr,
            debug=debug,
            organization_bootstrap_webhook_url=organization_bootstrap_webhook,
            organization_spontaneous_bootstrap=
            spontaneous_organization_bootstrap,
            organization_initial_active_users_limit=
            organization_initial_active_users_limit,
            organization_initial_user_profile_outsider_allowed=
            organization_initial_user_profile_outsider_allowed,
        )

        click.echo(f"Starting Parsec Backend on {host}:{port}"
                   f" (db={app_config.db_type}"
                   f" blockstore={app_config.blockstore_config.type}"
                   f" email={email_config.type}"
                   f" telemetry={'on' if sentry_dsn else 'off'}"
                   f" backend_addr={app_config.backend_addr}"
                   ")")
        try:
            retry_policy = RetryPolicy(maximum_database_connection_attempts,
                                       pause_before_retry_database_connection)
            trio_run(_run_backend,
                     host,
                     port,
                     ssl_context,
                     retry_policy,
                     app_config,
                     use_asyncio=True)
        except KeyboardInterrupt:
            click.echo("bye ;-)")
Ejemplo n.º 3
0
def run_cmd(
    host,
    port,
    db,
    db_min_connections,
    db_max_connections,
    db_first_tries_number,
    db_first_tries_sleep,
    blockstore,
    administration_token,
    spontaneous_organization_bootstrap,
    organization_bootstrap_webhook,
    backend_addr,
    email_host,
    email_port,
    email_host_user,
    email_host_password,
    email_use_ssl,
    email_use_tls,
    email_sender,
    forward_proto_enforce_https,
    ssl_keyfile,
    ssl_certfile,
    log_level,
    log_format,
    log_file,
    log_filter,
    sentry_url,
    debug,
    dev,
):

    # Start a local backend
    configure_logging(log_level, log_format, log_file, log_filter)
    if sentry_url:
        configure_sentry_logging(sentry_url)

    with cli_exception_handler(debug):

        if ssl_certfile or ssl_keyfile:
            ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
            if ssl_certfile:
                ssl_context.load_cert_chain(ssl_certfile, ssl_keyfile)
            else:
                ssl_context.load_default_certs()
        else:
            ssl_context = None

        if email_host == "MOCKED":
            tmpdir = tempfile.mkdtemp(prefix="tmp-email-folder-")
            if email_sender:
                email_config = MockedEmailConfig(sender=email_sender,
                                                 tmpdir=tmpdir)
            else:
                email_config = MockedEmailConfig(sender=DEFAULT_EMAIL_SENDER,
                                                 tmpdir=tmpdir)
        else:
            if not email_sender:
                raise ValueError(
                    "--email-sender is required when --email-host is provided")
            email_config = SmtpEmailConfig(
                host=email_host,
                port=email_port,
                host_user=email_host_user,
                host_password=email_host_password,
                use_ssl=email_use_ssl,
                use_tls=email_use_tls,
                sender=email_sender,
            )

        config = BackendConfig(
            administration_token=administration_token,
            db_url=db,
            db_min_connections=db_min_connections,
            db_max_connections=db_max_connections,
            db_first_tries_number=db_first_tries_number,
            db_first_tries_sleep=db_first_tries_sleep,
            spontaneous_organization_bootstrap=
            spontaneous_organization_bootstrap,
            organization_bootstrap_webhook_url=organization_bootstrap_webhook,
            blockstore_config=blockstore,
            email_config=email_config,
            ssl_context=True if ssl_context else False,
            forward_proto_enforce_https=forward_proto_enforce_https,
            backend_addr=backend_addr,
            debug=debug,
        )

        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)

        click.echo(f"Starting Parsec Backend on {host}:{port}"
                   f" (db={config.db_type}"
                   f" blockstore={config.blockstore_config.type}"
                   f" backend_addr={config.backend_addr}"
                   f" email_config={str(email_config)})")
        try:
            trio_run(_run_backend, use_asyncio=True)
        except KeyboardInterrupt:
            click.echo("bye ;-)")
Ejemplo n.º 4
0
def run_cmd(
    host,
    port,
    db,
    db_min_connections,
    db_max_connections,
    maximum_database_connection_attempts,
    pause_before_retry_database_connection,
    blockstore,
    administration_token,
    spontaneous_organization_bootstrap,
    organization_bootstrap_webhook,
    backend_addr,
    email_host,
    email_port,
    email_host_user,
    email_host_password,
    email_use_ssl,
    email_use_tls,
    email_sender,
    forward_proto_enforce_https,
    ssl_keyfile,
    ssl_certfile,
    log_level,
    log_format,
    log_file,
    sentry_url,
    debug,
    dev,
):

    # Start a local backend
    configure_logging(log_level=log_level,
                      log_format=log_format,
                      log_file=log_file)
    if sentry_url:
        configure_sentry_logging(sentry_url)

    with cli_exception_handler(debug):

        if ssl_certfile or ssl_keyfile:
            ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
            if ssl_certfile:
                ssl_context.load_cert_chain(ssl_certfile, ssl_keyfile)
            else:
                ssl_context.load_default_certs()
        else:
            ssl_context = None

        if email_host == "MOCKED":
            tmpdir = tempfile.mkdtemp(prefix="tmp-email-folder-")
            if email_sender:
                email_config = MockedEmailConfig(sender=email_sender,
                                                 tmpdir=tmpdir)
            else:
                email_config = MockedEmailConfig(sender=DEFAULT_EMAIL_SENDER,
                                                 tmpdir=tmpdir)
        else:
            if not email_sender:
                raise ValueError(
                    "--email-sender is required when --email-host is provided")
            email_config = SmtpEmailConfig(
                host=email_host,
                port=email_port,
                host_user=email_host_user,
                host_password=email_host_password,
                use_ssl=email_use_ssl,
                use_tls=email_use_tls,
                sender=email_sender,
            )

        app_config = BackendConfig(
            administration_token=administration_token,
            db_url=db,
            db_min_connections=db_min_connections,
            db_max_connections=db_max_connections,
            spontaneous_organization_bootstrap=
            spontaneous_organization_bootstrap,
            organization_bootstrap_webhook_url=organization_bootstrap_webhook,
            blockstore_config=blockstore,
            email_config=email_config,
            ssl_context=True if ssl_context else False,
            forward_proto_enforce_https=forward_proto_enforce_https,
            backend_addr=backend_addr,
            debug=debug,
        )

        click.echo(f"Starting Parsec Backend on {host}:{port}"
                   f" (db={app_config.db_type}"
                   f" blockstore={app_config.blockstore_config.type}"
                   f" backend_addr={app_config.backend_addr}"
                   f" email_config={str(email_config)})")
        try:
            retry_policy = RetryPolicy(maximum_database_connection_attempts,
                                       pause_before_retry_database_connection)
            trio_run(_run_backend,
                     host,
                     port,
                     ssl_context,
                     retry_policy,
                     app_config,
                     use_asyncio=True)
        except KeyboardInterrupt:
            click.echo("bye ;-)")