在使用SSL之前,MongoDB要求必须要有包含公钥证书和相关私钥的.pem文件,MongoDB可以使用由权威CA机构签发的证书,也可以使用自签署的证书,此处以自签署证书为例进行说明。
ca_mongodb
|__rootCA
|
|__serverCA
|
|__clientCA
#使用RSA加密算法生成一个根证书密钥文件
cd /usr/local
openssl genrsa -out ./ca_mongodb/rootCA/root.key 2048
#自签发CA证书
[root@tomas1 local]# openssl req -new -x509 -days 3650 -key ./ca_mongodb/rootCA/root.key -out ./ca_mongodb/rootCA/root.crt -subj "/C=CN/ST=ShangHai/O=bigdata/CN=root/[email protected]"
#合并证书和私钥成pem文件
cat ./ca_mongodb/rootCA/root.key ./ca_mongodb/rootCA/root.crt > ./ca_mongodb/rootCA/root.pem
#使用RSA加密算法生成服务端密钥文件
openssl genrsa -out ./ca_mongodb/serverCA/server.key 2048
#根据密钥文件生成一个服务端证书文件请求
openssl req -key ./ca_mongodb/serverCA/server.key -new -out ./ca_mongodb/serverCA/server.req -subj "/C=CN/ST=ShangHai/O=bigdata/CN=server/[email protected]"
注:需要将CN配置为节点的主机名hostname,建议每个节点生成一个服务端证书
#使用根证书签发服务端证书
openssl x509 -req -in ./ca_mongodb/serverCA/server.req -CA ./ca_mongodb/rootCA/root.pem -CAcreateserial -CAserial ./ca_mongodb/serial -out ./ca_mongodb/serverCA/server.crt -days 3650
#合并证书和私钥成pem文件
cat ./ca_mongodb/serverCA/server.key ./ca_mongodb/serverCA/server.crt > ./ca_mongodb/serverCA/server.pem
#验证服务端证书
[root@tomas1 local]# openssl verify -CAfile ./ca_mongodb/rootCA/root.pem ./ca_mongodb/serverCA/server.pem
./ca_mongodb/serverCA/server.pem: OK
#使用RSA加密算法生成客户端密钥文件
openssl genrsa -out ./ca_mongodb/clientCA/client.key 2048
#根据密钥文件生成一个客户端证书文件请求
openssl req -key ./ca_mongodb/clientCA/client.key -new -out ./ca_mongodb/clientCA/client.req -subj "/C=CN/ST=ShangHai/O=bigdata/CN=client/[email protected]"
#使用根证书签发客户端证书
openssl x509 -req -in ./ca_mongodb/clientCA/client.req -CA ./ca_mongodb/rootCA/root.pem -CAcreateserial -CAserial ./ca_mongodb/serial -out ./ca_mongodb/clientCA/client.crt -days 3650
#合并证书和私钥成pem文件
cat ./ca_mongodb/clientCA/client.key ./ca_mongodb/clientCA/client.crt > ./ca_mongodb/clientCA/client.pem
#验证客户端证书
[root@tomas1 local]# openssl verify -CAfile ./ca_mongodb/rootCA/root.pem ./ca_mongodb/clientCA/client.pem
./ca_mongodb/clientCA/client.pem: OK
在集群各节点的安装目录下新建CAfiles目录,用于保存根证书和服务端证书
/usr/local/mongodb
mkdir CAfiles
分发证书
scp ./ca_mongodb/rootCA/root.pem ./ca_mongodb/serverCA/server.pem root@hadoopSvr3:/usr/local/mongodb/CAfiles
各节点security部分新增如下配置
security:
clusterAuthMode: sendKeyFile
authorization: enabled
注:
1、clusterAuthMode之所以设置为sendKeyFile,是因为先前使用的是keyFile认证模式,是了实现平滑升级,所以暂时设置为sendKeyFile
2、mongos中没有authorization: enabled配置项
各节点net部分新增如下配置
net:
ssl:
mode: allowSSL
PEMKeyFile: /usr/local/mongodb/CAfiles/server.pem
CAFile: /usr/local/mongodb/CAfiles/root.pem
allowInvalidHostnames: true
注:这里之所以配置allowInvalidHostnames为true,是因为整个集群各节点使用同一个服务端证书
重启各节点。
具体操作可参考:https://docs.mongodb.com/v3.4/tutorial/configure-ssl-clients/
比如mongo shell连接mongos,采用如下命令
mongo --host *.*.*.*:27017 --ssl --sslPEMKeyFile /usr/local/ca_mongodb/clientCA/client.pem --sslCAFile /usr/local/ca_mongodb/rootCA/root.crt --sslAllowInvalidHostnames -u *** -p *** --authenticationDatabase "admin"
mongo shell采用集群管理员账号登陆mongos
mongo --host *.*.*.*:27017 --ssl --sslPEMKeyFile /usr/local/ca_mongodb/clientCA/client.pem --sslCAFile /usr/local/ca_mongodb/rootCA/root.crt --sslAllowInvalidHostnames -u *** -p *** --authenticationDatabase "admin"
切换到admin数据库,执行如下命名为该sslmode为"preferSSL"
mongos> use admin
switched to db admin
mongos> db.adminCommand( { setParameter: 1, sslMode: "preferSSL" } )
{ "was" : "allowSSL", "ok" : 1 }
修改集群认证模式为"sendX509"
mongos> db.adminCommand( { setParameter: 1, clusterAuthMode: "sendX509" } )
{ "was" : "sendKeyFile", "ok" : 1 }
配置副本集、各数据切片的各个节点均需要执行类似上述操作,用各副本集的管理帐号依次登录各成员进行操作。如配置配置副本集(25,26,41)
mongo shell采用集群管理员账号登陆配置副本集成员25
mongo --host *.*.*.*:27019 --ssl --sslPEMKeyFile /usr/local/ca_mongodb/clientCA/client.pem --sslCAFile /usr/local/ca_mongodb/rootCA/root.crt --sslAllowInvalidHostnames -u *** -p *** --authenticationDatabase "admin"
切换到admin数据库,执行如下命名为该sslmode为"preferSSL"
configReplSet:SECONDARY> use admin
switched to db admin
configReplSet:SECONDARY> db.adminCommand( { setParameter: 1, sslMode: "preferSSL" } )
{ "was" : "allowSSL", "ok" : 1 }
修改集群认证模式为"sendX509"
configReplSet:SECONDARY> db.adminCommand( { setParameter: 1, clusterAuthMode: "sendX509" } )
{ "was" : "sendKeyFile", "ok" : 1 }
配置副本集成员26、41做类似操作。
配置mongos,切换到admin数据库,执行如下命名为该sslmode为"requireSSL"
mongos> use admin
switched to db admin
mongos> db.adminCommand( { setParameter: 1, sslMode: "requireSSL" } )
{ "was" : "preferSSL", "ok" : 1 }
修改集群认证模式为"x509"
mongos> db.adminCommand( { setParameter: 1, clusterAuthMode: "x509" } )
{ "was" : "sendX509", "ok" : 1 }
配置副本集、各数据切片的各个节点均需要执行类似上述操作,用各副本集的管理帐号依次登录各成员进行操作。
集群各节点至少需要包含如下TLS/SSL相关配置
security:
clusterAuthMode: x509
authorization: enabled
net:
ssl:
mode: requireSSL
PEMKeyFile: /usr/local/mongodb/CAfiles/server.pem
CAFile: /usr/local/mongodb/CAfiles/root.pem
allowInvalidHostnames: true
注:authorization配置项仅适用于mongod实例,不适用于mongos