首页 > 解决方案 > 无法使用 SSL 连接到 Mongodb

问题描述

我正常设置了 Mongodb(没有安全性)并验证一切正常。现在尝试添加 TLS/SSL。我按照以下说明操作:

https://docs.mongodb.com/manual/appendix/security/appendixA-openssl-ca/ https://docs.mongodb.com/manual/appendix/security/appendixB-openssl-server/

没有错误。一切都成功了。通过以下方式验证了 pem:

openssl verify -CAfile mongodb.pem mongodb01.pem

mongodb01.pem: OK

更新了我的配置文件:

net:
  port: 27017
  bindIp: mongodb01.xxx.com
  tls:
    mode: requireTLS
    certificateKeyFile: C:\Program Files\MongoDB\Server\4.4\bin\mongodb01.pem
    CAFile: C:\Program Files\MongoDB\Server\4.4\bin\mongodb.pem

CA 证书是为 *.xxx.com 创建的,服务器证书是为 mongodb01.xxx.com 创建的。注意:mongodb01.xxx.com 在我的主机文件中被伪造为 127.0.0.1。我之前已经做过很多次了,当假的 dns 名称技巧都在同一台机器上时,它确实有效。

然后我运行 mongodb:

mongod --config "C:\Program Files\MongoDB\Server\4.4\bin\mongod.cfg"

但是当我尝试时:

openssl s_client -connect mongodb01.xxx.com:27017

verify error:num=20:unable to get local issuer certificate
verify error:num=21:unable to verify the first certificate

编辑:

所以我取得了一些进展。我不认为 MongoDB 正在接受 CAFile。当我将根证书和中间证书添加到 mongodb01.pem (以及所有 3 个使用相同密钥签名的证书)中时,证书链通过,但它返回一个自签名证书错误(如预期的那样)。

我还没有尝试从 Java 调用它,我试图让它首先在 Compass 中工作。但是,Compass 抱怨自签名证书。我尝试将证书放在我信任的商店中,但 Compass 仍然拒绝连接。

标签: mongodbopensslwindows-10

解决方案


您需要向 s_client 提供 CA 证书,除非该证书是由知名 CA 颁发的,并且其 CA 证书位于 openssl 附带的捆绑包中(假设甚至存在这样的捆绑包)。

使用-CApath-CAfile选项指定 CA 证书。


推荐阅读