尝试通过 TLS 将 Compass 连接到 VPS 上的 MongoDB 实例的久经考验的方法。如果证书来自 LetsEnctrypt CA(使用 CertBot 生成),则需要 2 个文件:
这个效果很好。连接已建立。
但是,这对于使用 OpenSSL 生成的自定义 CA 不起作用。 MongoDB 日志提供错误“不合适的证书用途”
我按照这个优秀的播放列表来创建 OpenSSL 证书链: 科技实验室
我相信我做得正确,因为所有 3 个证书都是不同的,并且临时证书和主机证书都会再次验证根证书。
我的根 CA 是:
-----BEGIN CERTIFICATE-----
MIIFETCCAvmgAwIBAgIUbGwDAa8nWxKRBW4q3xpoEEo/C2YwDQYJKoZIhvcNAQEN
BQAwGDEWMBQGA1UEAwwNR0lGVEJVVFRPTiBDQTAeFw0yNDEyMTIyMjE3NDNaFw0z
NDEyMTAyMjE3NDNaMBgxFjAUBgNVBAMMDUdJRlRCVVRUT04gQ0EwggIiMA0GCSqG
SIb3DQEBAQUAA4ICDwAwggIKAoICAQDiNZ7a/FEjYuGjJ5jYICo/45PGcXXOScep
Jrf3r4OX/CzIPYX8Z87sm19FA3oPx8alNkxVax86w0gT7s153f4AoDHuHxLpJSuB
zxTN5PYJAR/vi1YUlpc1w7H5eBokUz2++29DLvrJs9EKYynC76uNSmLsfrg/d5gq
fJASkyCW6ARDSx3xAZuwMpZVW7Go+56G9J+w0y0cGMWRpOBcURTdfTJcSBVZil5+
EteQcyqm2Rs/jTgcNefvjkPOJOFVAOc2B2BL4XuZRKR8VbPTqQKRYCvg6KEWNzAf
mzDo5E87CwPu1f1gQ5XtKuLhdIu0rswMM6eIlhWvhpUrsrNJqBwnjyZngzV/CZr6
s3uOsKFJXt+vv0u7hR1T34PMG4MmE2sNmPYB/5wDUn/fs/M1jEc82GjmoyCNfVhM
n9dtkeK178NZh7yqAD5XitAxavj85UOoF0h5jUnItbszEI2tkvO6xufEo9TgC2y6
KBjyFi+o9QgtzxymMMM/wTxqNEgHDdtOgjXhhdBa+QfBnzQxKaOMNsPA+ncX5Z6G
L0kqeS3xkRlqR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CSqGSIb3DQEBCwUAA4ICAQBtaH73uOIp/zXxMeZrFNPINnh80Z60
kBInqtdVd8LBKgdJCiXu0uVlh+1Do4xszQi7YtJUgUgLckdueXOq9DUHvOXNXfLB
ByCsqDrYMd5+imckwLDYjARMT/Ih9GbFNUEYbtym5k/pHxKJahLEWSUKeLKXCnNT
yHYAXqbzxapltQekG1+OofU9wGXQVxBRg65NPaCyfOFldPWeh38kKw8A49kQwc5T
x29oxcECHiqxz4RwvxKYlamoG/d0njlK3y3aAxMou31YhON9EltMEnGVM+awzfcn
cr5xUSRK0MPYwwqiSUZySSpyC+bA/1rx1Bd2t8Oohnqe9ZzznJS5qhHuza3ngGzo
dFp4CMe+yHbaLTSF4I8zxSh8qd0QCtmnIz0UdZ4IMegqHGfH3OsRdLjBhbfaYw2H
De7s/+34LASGKV9jABYIT8jYQS9QHMBocqa/xnYKuIJ/mCl2g2nXx3Zf+AiEP965
JFUIu/6syKmzb8vygEYkQCEW/z45UcQdmH6KFwHQ5FB15b/zN/0z3l/rQXJ9zel/
y0sAEd/wZ+7z2ry2SUeMVFf1hejutZ5AD9u+q3MMZ8REqGM+r7tg9uSkKs0lf1wP
bH9nU1oXBrNQPqMzkEvwObIJEPP/AwuT6R73LbfEBXXLiDxhouYQUIb2oIKg0iHr
yeC0jEePh5kYWw==
-----END CERTIFICATE----
非常感谢任何有关导致问题的原因的想法!
这里是生成证书的配置文件和命令:
# root-ca.conf
[req]
distinguished_name = req_distinguished_name
prompt = no
[req_distinguished_name]
C = AU
O = giftbutton
OU = My Division
CN = Root CA
[v3_ca]
keyUsage = critical, keyCertSign, cRLSign
basicConstraints = critical, CA:true
subjectKeyIdentifier = hash
# intermediate-ca.conf
[req]
distinguished_name = req_distinguished_name
prompt = no
[req_distinguished_name]
C = AU
O = giftbutton
OU = My Division
CN = Intermediate CA
[v3_ca]
keyUsage = critical, keyCertSign, cRLSign
basicConstraints = critical, CA:true
subjectKeyIdentifier = hash
# server.conf
[req]
distinguished_name = req_distinguished_name
prompt = no
[req_distinguished_name]
C = AU
O = giftbutton
OU = My Division
CN = Mongo Server
[v3_ca]
keyUsage = digitalSignature, keyEncipherment
extendedKeyUsage = serverAuth
subjectAltName = @alt_names
[alt_names]
DNS.1 = giftbutton.com
# client.conf
[req]
distinguished_name = req_distinguished_name
prompt = no
[req_distinguished_name]
C = AU
O = giftbutton
OU = My Division
CN = Mongo Client
[v3_ca]
keyUsage = digitalSignature
extendedKeyUsage = clientAuth
许多教程都使用
openssl genrsa ...
生成公钥/私钥。当您仅将密钥用于一个证书时,使用证书自动创建密钥会更容易(选项 -newkey 4096
)。少了一个命令。
通常,当您需要证书时,您会创建一个证书请求并将其发送给拥有 CA 的个人/部门。他们接受您的证书请求,使用其 CA 对其进行签名,然后将签名的证书返回给您。如果您是 CA 的所有者,则不需要此步骤。您可以使用单个命令创建并签署证书请求。又少了一个命令。
看起来您喜欢使用服务器和客户端证书以及中间 CA。因此,最终会产生由 4 个命令创建的 4 个证书:
openssl req -config root-ca.conf -newkey 4096 -keyout root-ca.key -noenc -new -x509 -days 3650 -sha256 -copy_extensions copyall -extensions v3_ca -out root-ca.crt
openssl req -config intermediate-ca.conf -newkey 4096 -keyout intermediate-ca.key -noenc -new -x509 -days 3650 -CA root-ca.crt -CAkey root-ca.key -sha256 -copy_extensions copyall -extensions v3_ca -out intermediate-ca.crt
openssl req -config server.conf -newkey 4096 -keyout server.key -noenc -new -x509 -days 3650 -CA intermediate-ca.crt -CAkey intermediate-ca.key -sha256 -copy_extensions copyall -extensions v3_ca -out server.crt
openssl req -config client.conf -newkey 4096 -keyout client.key -noenc -new -x509 -days 3650 -CA intermediate-ca.crt -CAkey intermediate-ca.key -sha256 -copy_extensions copyall -extensions v3_ca -out client.crt
为了使用它们,您必须将它们组合成文件:
cat intermediate-ca.crt root-ca.crt > ca-chain.crt
cat client.crt client.key > client.pem
cat server.crt server.key > server.pem
然后就可以使用了。在服务器端使用
net:
port: 27017
bindIpAll: true
tls:
mode: requireTLS
certificateKeyFile: server.pem
CAFile: ca-chain.crt
在 Compass 中使用这样的连接字符串:
mongodb://user:[email protected]:27017/?authSource=admin&tls=true&tlsCertificateKeyFile=client.pem&tlsCAFile=ca-chain.crt
注:
下载最新版本的 openssl(版本 3.4)。在旧版本中,不支持
-copy_extensions copyall
选项,您需要将 [v3_ca]
部分放入扩展配置文件中,并使用 -extensions v3_ca -extfile ...
加载此文件。
正如已经提到的,我建议下载并安装XCA。它使用起来非常简单,您可以通过简单的复制/粘贴或拖放来导入现有(工作)证书。然后,您可以检查属性,并根据您的需要创建类似的证书或请求,并且可以以您想要的任何格式导出它们。它确实是了解 x.509 证书秘密的有用工具。