【问题标题】:Mongodb connection attempt failed: SSLHandshakeFailed: SSL peer certificate validation failed: self signed certificateMongodb 连接尝试失败:SSLHandshakeFailed:SSL 对等证书验证失败:自签名证书
【发布时间】:2019-04-24 01:53:15
【问题描述】:

我在 Ubuntu 16.04.5 LTS 服务器上设置了一个具有 SSL 安全性的 mongoDB 数据库,并且我已经使用了几个月。为了在 mongo 上设置 SSL,我遵循了 Rajan Maharjan 在 medium.com (link) 上的教程。在一段时间未使用后返回我的服务器,我收到以下错误消息:

SSL peer certificate validation failed: certificate has expired

查看mongo日志,发现:

[PeriodicTaskRunner] Server certificate is now invalid. It expired on 2018-11-10T08:10:11.000Z

因此,我按照与第一次创建证书时相同的步骤,使用 rootCA.key 文件重新生成并重新签署了所有证书。我重新启动了 mongod:

service mongod restart

现在我有“自签名证书”错误:

MongoDB shell version v4.0.2
connecting to: mongodb://IP:port/
2018-11-21T13:11:10.584+0000 E NETWORK  [js] SSL peer certificate     
validation failed: self signed certificate
2018-11-21T13:11:10.584+0000 E QUERY    [js] Error: 
couldn't connect to server IP:port, connection attempt failed: 
SSLHandshakeFailed: SSL peer certificate validation failed: self signed certificate :
connect@src/mongo/shell/mongo.js:257:13
@(connect):1:6
exception: connect failed

我的连接字符串如下:

mongo --ssl --sslCAFile /PATH/TO/rootCA.pem --sslPEMKeyFile /PATH/TO/mongodb.pem --host IP:port

我没有找到任何关于为 mongoDB 重新签名证书的资源。任何帮助将不胜感激。

编辑

/etc/mongod.conf:

# mongod.conf

# for documentation of all options, see:
#   http://docs.mongodb.org/manual/reference/configuration-options/

# Where and how to store data.
storage:
  dbPath: /var/lib/mongodb
  journal:
    enabled: true
#  engine:
#  mmapv1:
#  wiredTiger:

# where to write logging data.
systemLog:
  destination: file
  logAppend: true
  path: /var/log/mongodb/mongod.log

# network interfaces
net:
  port: PORT
  bindIp: IP
  ssl:
    mode: requireSSL
    PEMKeyFile: /etc/ssl/mongodb.pem
    CAFile: /etc/ssl/rootCA.pem

# how the process runs
 processManagement:
  timeZoneInfo: /usr/share/zoneinfo

#security:
security:
   authorization: "enabled"

#operationProfiling:

#replication:

#sharding:

## Enterprise-Only Options:

#auditLog:

#snmp:

【问题讨论】:

  • 您是否使用了正确的 sslCAFile?不应该是/PATH/TO/abc.crt吗?
  • @AniketMaithani 根据您的建议,我尝试使用 .crt,我在日志中得到 Error: network error while attempting to run command 'isMaster' on hostSSL peer certificate validation failed: self signed certificate。我的问题中的连接字符串与我之前使用的相同并且它曾经工作过。
  • 在 mongo 连接字符串中添加--sslAllowInvalidCertificates 选项会产生以下错误消息:日志中的[js] Error: network error while attempting to run command 'isMaster' on hostSSL peer certificate validation failed: self signed certificate
  • 嘿@Nicolas 你可以在这里发布你的 /etc/mongod.conf 文件吗?同时尝试使用以下内容:dpaste.de/U4Lm
  • 嗨@AniketMaithani 我编辑了我的问题以添加 .conf 文件。我尝试了您的解决方案(并在 .conf 文件中注释掉了 rootCA.pem 行)但使用mongod --sslMode requireSSL --sslPEMKeyFile /etc/ssl/mongodb.pem 我得到exception in initAndListen: NonExistentPath: Data directory /data/db not found., terminating 并且在重新启动服务时,在日志中出现以下错误WARNING: No SSL certificate validation can be performed since no CA file has been provided

标签: mongodb ssl openssl ubuntu-16.04


【解决方案1】:

我按照以下步骤设法让它再次工作(由于某种原因,以前没有工作):

  • /etc/ssl/中删除mongodb.*文件
  • 使用以下命令重新生成证书:
    • openssl genrsa -out mongodb.key 2048
    • openssl req -new -key mongodb.key -out mongodb.csr
    • Common Name (eg, YOUR name) []字段中的服务器地址
    • openssl x509 -req -in mongodb.csr -CA rootCA.pem -CAkey /PATH/TO/rootCA.key -CAcreateserial -out mongodb.crt -days 500 -sha256
    • cat mongodb.key mongodb.crt > mongodb.pem
  • chmod 666 mongodb.pem
  • 服务 mongod 重启
  • cat /var/log/mongodb/mongod.log(检查状态

并启动 mongo shell

  • mongo --ssl --sslCAFile /PATH/TO/rootCA.pem --sslPEMKeyFile /PATH/TO/mongodb.pem --host IP:PORT

感谢@AniketMaithani 尝试帮助我解决此问题。

【讨论】:

  • 你好 Nicolas Gaborel。我们在哪里可以找到本地 mongodb 上的 rootCA.key?
  • @GayathriB 你可以用openssl生成自己的密钥,这个教程很不错:medium.com/@rajanmaharjan/…
  • 嗨,尼古拉斯·加博雷尔。我已关注此链接,并且能够连接到服务器。但是这个过程只是为了测试目的,对吧?我们应该向客户端提供 server.pem 以便连接到服务器。这里有机会加密 server.pem。那么,有没有其他方法可以使用 SSL 证书连接 MongoDB 服务器?
  • @GayathriB 我在内部使用了这个 SSL 认证,而不是用于公共网络连接。我不确定在这种情况下最佳做法是什么,我想您应该查看一些网络安全教程或仅在服务器端处理您的数据库。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-18
  • 2021-05-20
  • 2021-12-06
  • 2018-04-09
相关资源
最近更新 更多