MongoDB自签名SSL连接:SSL对等证书验证失败。

9

我按照使用PyMongo创建自签名SSL连接,作者Wan Bachtiar的指南创建了三个.pem文件:server.pem、client.pem和ca.pem。

我正在使用Ubuntu 16.04和MongoDB v3.2.11。

目的是在将MongoDB开放到公共互联网之前保护它。

让我们启动mongod:

$ mongod --auth --port 27017 --dbpath /data/db1 
--sslMode requireSSL --sslPEMKeyFile /etc/ssl/server.pem 
--sslCAFile /etc/ssl/ca.pem --sslAllowInvalidHostnames &

输出:

root@tim:/etc/ssl# 2017-01-13T12:58:55.150+0000 I CONTROL  [initandlisten] MongoDB starting : pid=19058 port=27017 dbpath=/data/db1 64-bit host=tim
2017-01-13T12:58:55.150+0000 I CONTROL  [initandlisten] db version v3.2.11
2017-01-13T12:58:55.151+0000 I CONTROL  [initandlisten] git version: 009580ad490190ba33d1c6253ebd8d91808923e4
2017-01-13T12:58:55.151+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
2017-01-13T12:58:55.152+0000 I CONTROL  [initandlisten] allocator: tcmalloc
2017-01-13T12:58:55.152+0000 I CONTROL  [initandlisten] modules: none
2017-01-13T12:58:55.152+0000 I CONTROL  [initandlisten] build environment:
2017-01-13T12:58:55.152+0000 I CONTROL  [initandlisten]     distmod: ubuntu1604
2017-01-13T12:58:55.152+0000 I CONTROL  [initandlisten]     distarch: x86_64
2017-01-13T12:58:55.152+0000 I CONTROL  [initandlisten]     target_arch: x86_64
2017-01-13T12:58:55.153+0000 I CONTROL  [initandlisten] options: { net: { port: 27017, ssl: { CAFile: "/etc/ssl/ca.pem", PEMKeyFile: "/etc/ssl/server.pem", allowInvalidHostnames: true, mode: "requireSSL" } 
}, security: { authorization: "enabled" }, storage: { dbPath: "/data/db1" } }
2017-01-13T12:58:55.211+0000 I -        [initandlisten] Detected data files in /data/db1 created by the 'wiredTiger' storage engine, so setting the active storage engine to 'wiredTiger'.
2017-01-13T12:58:55.212+0000 W -        [initandlisten] Detected unclean shutdown - /data/db1/mongod.lock is not empty.
2017-01-13T12:58:55.212+0000 W STORAGE  [initandlisten] Recovering data from the last clean checkpoint.
2017-01-13T12:58:55.212+0000 I STORAGE  [initandlisten] wiredtiger_open config: create,cache_size=1G,session_max=20000,eviction=(threads_max=4)
,config_base=false,statistics=(fast),log=(enabled=true,archive=true,path=journal,compressor=snappy),file_manager=(close_idle_time=100000),checkpoint=(wait=60,log_size=2GB),statistics_log=(wait=0),
2017-01-13T12:58:55.886+0000 I CONTROL  [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2017-01-13T12:58:55.886+0000 I CONTROL  [initandlisten]
2017-01-13T12:58:55.895+0000 I FTDC     [initandlisten] Initializing full-time diagnostic data capture with directory '/data/db1/diagnostic.data'
2017-01-13T12:58:55.897+0000 I NETWORK  [initandlisten] waiting for connections on port 27017 ssl
2017-01-13T12:58:55.897+0000 I NETWORK  [HostnameCanonicalizationWorker] Starting hostname canonicalization worker
2017-01-13T12:58:56.026+0000 I FTDC     [ftdc] Unclean full-time diagnostic data capture shutdown detected, found interim file, some metrics may have been lost. OK

在运行mongod之后,我启动了mongo shell:
$ mongo --port 27017 -u "my username" -p "my password" 
--authenticationDatabase "" --ssl --sslPEMKeyFile /etc/ssl/client.pem 
--sslCAFile /etc/ssl/ca.pem --host tim

输出结果类似于Marshall Farrier提出的问题;让我们来看一下。
MongoDB shell version: 3.2.11
connecting to: 127.0.0.1:27017/datatest
2017-01-13T12:35:58.247+0000 I NETWORK  [initandlisten] connection accepted from 127.0.0.1:38902 #8 (1 connection now open)
2017-01-13T12:35:58.259+0000 E NETWORK  [thread1] SSL peer certificate validation failed: self signed certificate
2017-01-13T12:35:58.259+0000 E QUERY    [thread1] Error: socket exception [CONNECT_ERROR] for SSL peer certificate validation failed: self signed certificate :
connect@src/mongo/shell/mongo.js:231:14
@(connect):1:6

2017-01-13T12:35:58.263+0000 E NETWORK  [conn8] SSL peer certificate validation failed: self signed certificate
2017-01-13T12:35:58.263+0000 I NETWORK  [conn8] end connection 127.0.0.1:38902 (0 connections now open)

我做错了什么?

1个回答

12

经过一番搜索,似乎这个错误是由于主机名“CN”不正确导致的。

来自digitalocean

每当您生成CSR时,都会提示您提供有关证书的信息。这些信息称为Distinguished Name(DN)。 DN中的一个重要字段是Common Name(CN),应该是您打算使用证书的主机的确切完全限定域名(FQDN)。

另外,也来自MongoDB 文档

如果您的MongoDB部署使用SSL,则必须指定--host选项。 mongo验证您连接的mongod或mongos的主机名是否与mongod或mongos的--sslPEMKeyFile证书的CN或SAN匹配。如果主机名与CN / SAN不匹配,则mongo将无法连接。

解决方案:

我重新生成了密钥,在CN = <hostname>中用任何其他主机名替换localhost,然后完成了Wan Bachtiar的指南

完成后运行以下命令:

$ mongo --port 27017 -u '<_username_>' -p '<_password_>' 
--authenticationDatabase "<_my db_>" --ssl --sslPEMKeyFile 
/etc/ssl/client.pem  --sslCAFile /etc/ssl/ca.pem --host localhost

注意:MongoDB严格遵守谁有访问哪个数据库的规定,在Mongo shell中进行快速测试:

> show dbs

会返回错误。但是,我的用户实际上只能访问"<my db>"中指定的数据库,因此循环遍历"<my db>"中的行可以完美地工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接