Пример #1
0
def real_jail_session(token):
    """
    Create a Jail service for testing, using TCP sockets, uses the token for auth
    """
    auth_interceptor = Auth().get_auth_interceptor(allow_jailed=True)

    with futures.ThreadPoolExecutor(1) as executor:
        server = grpc.server(executor, interceptors=[auth_interceptor])
        port = server.add_secure_port("localhost:0",
                                      grpc.local_server_credentials())
        servicer = Jail()
        jail_pb2_grpc.add_JailServicer_to_server(servicer, server)
        server.start()

        call_creds = grpc.metadata_call_credentials(
            CookieMetadataPlugin(token))
        comp_creds = grpc.composite_channel_credentials(
            grpc.local_channel_credentials(), call_creds)

        try:
            with grpc.secure_channel(f"localhost:{port}",
                                     comp_creds) as channel:
                yield jail_pb2_grpc.JailStub(channel)
        finally:
            server.stop(None).wait()
Пример #2
0
    auth_pb2_grpc.add_AuthServicer_to_server(auth, open_server)
    bugs_pb2_grpc.add_BugsServicer_to_server(Bugs(), open_server)
    resources_pb2_grpc.add_ResourcesServicer_to_server(Resources(),
                                                       open_server)
    open_server.start()

    jailed_server = grpc.server(
        futures.ThreadPoolExecutor(2),
        interceptors=[
            ErrorSanitizationInterceptor(),
            LoggingInterceptor(),
            auth.get_auth_interceptor(allow_jailed=True),
        ],
    )
    jailed_server.add_insecure_port("[::]:1754")
    jail_pb2_grpc.add_JailServicer_to_server(Jail(), jailed_server)
    jailed_server.start()

    servicer = API()
    server = grpc.server(
        futures.ThreadPoolExecutor(2),
        interceptors=[
            ErrorSanitizationInterceptor(),
            LoggingInterceptor(),
            auth.get_auth_interceptor(allow_jailed=False),
        ],
    )
    server.add_insecure_port("[::]:1751")

    account_pb2_grpc.add_AccountServicer_to_server(Account(), server)
    api_pb2_grpc.add_APIServicer_to_server(servicer, server)