由于x509证书依赖于旧版通用名称字段,因此无法使用Golang连接到服务器
我正在尝试连接 mongodb 服务器,要连接我必须提供 CA 证书文件和 tls 证书文件。
当我使用以下命令时,我没有问题
$ mongo --host customhost:port DB --authenticationDatabase=DB -u ACCOUNT -p PWD --tls --tlsCAFile /etc/ca-files/new-mongo.ca.crt --tlsCertificateKeyFile /etc/ca-files/new-mongo-client.pem
$ mongo --host customhost:port DB --authenticationDatabase=DB -u ACCOUNT -p PWD --tls --tlsCAFile /etc/ca-filfailed to connect: x509: certificate relies on legacy Common Name field, use SANs or temporarily enable Common Name matching with GODEBUG=x509ignoreCN=0
failed to connect: x509: certificate relies on legacy Common Name field, use SANs or temporarily enable Common Name matching with GODEBUG=x509ignoreCN=0
但是,当我尝试与 mongo 连接(并且仅使用 tls 客户端进行测试)时,出现以下错误:
如果我使用 env 变量一切正常,但我想知道如何修复它而不必使用它。
证书:
我有点卡住了,不知道问题是我的 tls 代码配置和我加载证书的方式
const CONFIG_DB_CA = "/etc/ca-files/new-mongo.ca.crt"
func main() {
cer, err := tls.LoadX509KeyPair("mongo-server.crt", "mongo-server.key")
if err != nil {
log.Println(err)
return
}
roots := x509.NewCertPool()
ca, err := ioutil.ReadFile(CONFIG_DB_CA)
if err != nil {
fmt.Printf("Failed to read or open CA File: %s.n", CONFIG_DB_CA)
return
}
roots.AppendCertsFromPEM(ca)
tlsConfig := &tls.Config{
Certificates: []tls.Certificate{cer},
RootCAs: roots,
}
conn, err := tls.Dial("tcp", "customhost:port", tlsConfig)
if err != nil {
fmt.Printf("failed to connect: %v.n", err)
return
}
err = conn.VerifyHostname("customhost")
if err != nil {
panic("Hostname doesn't match with certificate: " + err.Error())
}
for i, cert := range conn.ConnectionState().PeerCertificates {
prefix := fmt.Sprintf("CERT%d::", i+1)
fmt.Printf("%sIssuer: %sn", prefix, cert.Issuer)
fmt.Printf("%sExpiry: %vn", prefix, cert.NotAfter.Format(time.RFC850))
fmt.Printf("%sDNSNames: %vnn", prefix, cert.DNSNames)
}
fmt.Printf("Success!")
}
,还是来自 SSL 证书配置错误但证书看起来不错。我觉得加载的证书因任何原因被忽略
谢谢你的帮助
THE END
二维码