【问题标题】:Accessing underlying connection in GRPC server with unix socket使用 unix 套接字访问 GRPC 服务器中的底层连接
【发布时间】:2022-01-10 00:06:03
【问题描述】:

想知道是否有办法访问底层 net.Conn 以使用 SO_PEERCRED 检索用户凭据并在服务器处理请求之前对其进行验证。

来自https://blog.jbowen.dev/2019/09/using-so_peercred-in-go/,需要net.UnixConn 来返回用于验证的unix.Ucred。因此,如果服务器请求处理程序可以通过某种方式访问​​ net.Conn,这应该很容易

我查看了一个 UnaryServerInterceptor,但 UnaryServerInterceptor 中提供的任何内容似乎都没有包含 net.Conn

func interceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
    log.Printf("Intercepted: %+v %+v", info.Server, req) // anything here?
    return handler(ctx, req)
}

【问题讨论】:

    标签: go grpc grpc-go


    【解决方案1】:

    接口方法TransportCredentials.ServerHandshake就是你需要的接缝。您的实现可以从输入net.Conn 中读取,并将凭证作为AuthInfo 返回。然后在您的处理程序代码中,您可以通过peer.FromContext 从上下文中获取凭证。或者,如果您希望在到达处理程序代码之前进行身份验证,您可以直接在 TransportCredentials.ServerHandshake 中或通过拦截器进行。

    另见:https://groups.google.com/g/grpc-io/c/FeQV7NXpeqA

    【讨论】:

    • 在实现 TransportCredentials.ServerHandshake 并将其添加到服务器后,我能够使其正常工作。我花了一点时间找到正确的服务器选项; s := grpc.NewServer(grpc.Creds(&ServerAuthCredentials{}))type ServerAuthCredentials struct { credentials.TransportCredentials}
    猜你喜欢
    • 1970-01-01
    • 2019-07-17
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2020-02-15
    • 2011-04-27
    相关资源
    最近更新 更多