【发布时间】:2022-01-17 11:50:58
【问题描述】:
我是 Arrow Flight 和 pyarrow (v=6.0.1) 的新手,我正在尝试实现基本身份验证,但我总是遇到错误:
OSError: Stream is closed
我通过顺序运行以下两个文件(分别代表服务器和客户端)创建了一个最小的复制示例:
from typing import Dict, Union
from pyarrow.lib import tobytes
from pyarrow.flight import BasicAuth, FlightUnauthenticatedError, ServerAuthHandler, FlightServerBase
from pyarrow._flight import ServerAuthSender, ServerAuthReader
class ServerBasicAuthHandler(ServerAuthHandler):
def __init__(self, creds: Dict[str, str]):
self.creds = {user.encode(): pw.encode() for user, pw in creds.items()}
def authenticate(self, outgoing: ServerAuthSender, incoming: ServerAuthReader):
buf = incoming.read() # this line raises "OSError: Stream is closed"
auth = BasicAuth.deserialize(buf)
if auth.username not in self.creds:
raise FlightUnauthenticatedError("unknown user")
if self.creds[auth.username] != auth.password:
raise FlightUnauthenticatedError("wrong password")
outgoing.write(tobytes(auth.username))
def is_valid(self, token: bytes) -> Union[bytes, str]:
if not token:
raise FlightUnauthenticatedError("no basic auth provided")
if token not in self.creds:
raise FlightUnauthenticatedError("unknown user")
return token
service = FlightServerBase(
location=f"grpc://[::]:50051",
auth_handler=ServerBasicAuthHandler({"user": "pw"}),
)
service.serve()
from pyarrow.flight import FlightClient
client = FlightClient(location=f"grpc://localhost:50051")
client.authenticate_basic_token("user", "pw")
我基本上是从their tests 复制了ServerAuthHandler 的实现,所以它被证明是有效的。但是,我无法让它工作。
错误消息Stream is closed 难以调试。我不知道它来自哪里,也无法追踪到 pyarrow 实现中的任何地方(无论是 Python 端还是 C++ 端)。我看不出它是从哪里来的。
任何有关如何防止此错误的帮助或提示将不胜感激。
【问题讨论】:
-
仅供参考,如果您想要更完整的示例,我已尝试将此处的答案转换为 Arrow 的文档页面:github.com/apache/arrow-cookbook/pull/123