尝试通过 TLS 将 Compass 连接到 VPS 上的 MongoDB 实例的久经考验的方法。如果证书来自 LetsEnctrypt CA(使用 CertBot 生成),则需要 2 个文件:
- CertAndKey.pem,包含主机证书及其私钥;
- intermAndRoot.pem,包含Certbot提供的chain.pem和直接从LetsEncrypt网站下载的rootCA证书。
一切正常。连接已建立。
但是,对于使用 OpenSSL 生成的自定义 CA,同样的方法不起作用。MongoDB 日志提供错误“证书用途不合适”
我按照这个出色的播放列表创建了 OpenSSL 证书链: TechLAB
我相信我做对了,因为所有 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
KBjyF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/TuPDAOBgNVHQ8BAf8EBAMCBaAwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsG
AQUFBwMCMA0GCSqGSIb3DQEBCwUAA4ICAQBtaH73uOIp/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----
非常感谢您对导致该问题的原因的任何想法!
以下是生成证书的配置文件和命令:
许多教程使用 生成公钥/私钥
openssl genrsa ...
。当您仅将密钥用于一个证书时,使用证书 (选项 ) 自动创建密钥会更容易-newkey 4096
。这样可以少一个命令。通常,当您需要证书时,您会创建证书请求并将其发送给拥有 CA 的个人/部门。他们会接收您的证书请求,使用其 CA 对其进行签名,然后将签名的证书返回给您。如果您是 CA 的所有者,则无需执行此步骤。您可以使用单个命令创建和签名证书请求。这样就少了一个命令。
看起来您喜欢使用服务器和客户端证书以及中间 CA。因此,最终通过 4 条命令创建了 4 个证书:
除了配置文件,您还可以将所有参数放在命令行中。因此,也许
-config client.conf
可以使用它-subj "C=AU/O=giftbutton/OU=My\ Division/CN=Mongo\ Client" -addext "keyUsage=critical/keyCertSign/cRLSign" -addext "basicConstraints=critical,CA:true" -addext "subjectKeyIdentifier=hash"
- 但我没有测试!为了使用它们,您必须将它们合并到文件中:
然后它们就可以使用了。在服务器端使用
在 Compass 中,使用如下连接字符串:
笔记:
下载最新版本的 openssl(版本 3.4)。旧版本
-copy_extensions copyall
不支持该选项,您需要将该[v3_ca]
部分放入扩展配置文件中,然后使用 加载此文件-extensions v3_ca -extfile ...
。正如前面提到的,我建议下载并安装XCA。它使用起来非常简单,您可以通过简单的复制/粘贴或拖放导入现有(工作)证书。然后您可以检查属性,并根据需要创建类似的证书或请求,并可以以您想要的任何格式导出它们。它确实是了解 x.509 证书秘密的有用工具。