Kri*_*urg 5 ssl proxy go docker grpc
我有一个 gRPC 客户端和服务器,两者都使用 ssl 证书进行保护。如果没有代理,这些工作就很好。作为测试,当我故意创建有缺陷的证书时,它会失败。稍后在本文中证明这不是证书问题。
gRPC 服务器代码:
// Creates a new gRPC server
// Create the TLS credentials
creds, err := credentials.NewServerTLSFromFile("configs/cert/servercert.pem", "configs/cert/serverkey.pem")
if err != nil {
log.Fatalf("could not load TLS keys: %s", err)
}
// Create an array of gRPC options with the credentials
opts := []grpc.ServerOption{grpc.Creds(creds)}
// create a gRPC server object
s := grpc.NewServer(opts...)
Run Code Online (Sandbox Code Playgroud)
gRPC 客户端代码:
// Create the client TLS credentials
creds, err := credentials.NewClientTLSFromFile("configs/cert/servercert.pem", "")
if err != nil {
log.Fatalf("could not load tls cert: %s", err)
}
conn, err := grpc.Dial(grpcUri, grpc.WithTransportCredentials(creds))
if err != nil {
log.Fatalf("Unable to connect: %v", err)
}
Run Code Online (Sandbox Code Playgroud)
现在我尝试使用转发代理(我已经测试过并且在正常的 HTTP api 请求上工作正常)。然而,它经常在通过代理的 gRPC 请求上失败。
我正在使用cuttle,它内部使用goproxy并进行以下设置。请注意,InsecureSkipVerify
布尔值已被尝试过true
和false
。根据我对 SSL 的(有限)了解,这需要如此,false
因为它将在线检查证书,并且这些证书是自签名的,所以自然会失败。然而,我再次尝试了true
两者false
// Config proxy.
proxy := goproxy.NewProxyHttpServer()
proxy.Tr = &http.Transport{
// Config TLS cert verification.
TLSClientConfig: &tls.Config{InsecureSkipVerify: !cfg.TLSVerify},
Proxy: http.ProxyFromEnvironment,
}
Run Code Online (Sandbox Code Playgroud)
在 gRPC 客户端和服务器之间运行代理会导致以下错误:
传输:身份验证握手失败:x509:由未知颁发机构签名的证书(可能是因为在尝试验证候选颁发机构证书“测试服务器”时“x509:无效签名:父证书无法签署此类证书”
这表明这是一个证书问题,但是,正如前面所述和测试的那样,gRPC 在没有代理的情况下可以完美工作。
另请注意:我不想在代理后面运行 gRPC,但由于开发环境而被迫这样做。gRPC 服务器和代理运行在同一台 docker 机器上。具有相同的 IP 地址将导致以下配置,这只会相互抵消(相信我,我无论如何都尝试过)。
ENV http_proxy 192.168.99.100:3128
ENV https_proxy 192.168.99.100:3128
ENV no_proxy 192.168.99.100 # <- this would be the gRPC server IP, which is the same as the proxy. resulting in nothing being run through a proxy.
Run Code Online (Sandbox Code Playgroud)
分割 docker 中的 IP 地址可以解决这个问题,但是,我什么也学不到,并且想解决这个问题。我尝试了像这里回答的配置来设置不同的docker内部IP,但是,该IP将保持为空(仅设置网络)并且在新IP上访问只会超时。
背景:
TLS 连接的每一端都需要预先安排的信任。大多数客户端在连接到远程主机时使用系统信任链(GeoTrust、DigiCert CA 的可信证书都列在那里,允许您安全地访问https://facebook.com、https://google.com等网站)。 )
go
,当使用 TLS 时,在联系服务器时将默认使用系统信任链。开发自定义解决方案时,您的应用程序服务器的公共证书很可能不在该系统信任链中。所以你有两个选择:
InsecureSkipVerify: true
(不要这样做!)您的应用程序服务器很可能有一个自签名证书,因此很容易获得其中的公共证书部分。您还可以使用openssl等工具查看服务器的公共证书- 使用链接的解决方案,您不仅可以为您自己的开发服务器获取公共证书,还可以为任何其他远程服务获取公共证书 - 只需提供主机名和端口。
所以只是总结一下你的情况。你有:
Client <- TLS -> Server
Run Code Online (Sandbox Code Playgroud)
但想要:
Client <-TLS-> Proxy <-TLS-> Server
Run Code Online (Sandbox Code Playgroud)
因此,您的客户端现在不需要信任服务器,而只需信任代理 - 因为它只直接与代理通信。代理很可能有一个自签名证书(请参阅上面有关如何提取信任证书的信息)。一旦你有了这个,更新你的go
代码以使用这个自定义信任文件,如下所示:
// Get the SystemCertPool, continue with an empty pool on error
rootCAs, err := x509.SystemCertPool() // <- probably not needed, if we're only ever talking to this single proxy
if err != nil || rootCAs == nil {
rootCAs = x509.NewCertPool()
}
// Read in the custom trust file
certs, err := ioutil.ReadFile(localTrustFile)
if err != nil {
log.Fatalf("Failed to append %q to RootCAs: %v", localTrustFile, err)
}
// Append our cert to the system pool
if ok := rootCAs.AppendCertsFromPEM(certs); !ok {
log.Fatalf("failed to append custom cert")
}
tlsConfig := &tls.Config{
RootCAs: rootCAs,
}
Run Code Online (Sandbox Code Playgroud)
代理还需要信任服务器 - 因此如果服务器的证书不在系统信任链中,那么它将需要类似上面的 tls.Config 设置。
归档时间: |
|
查看次数: |
3276 次 |
最近记录: |