Database Manual / Self-Managed Deployments / Security / Appendix

Appendix C - OpenSSL Client Certificates for Testing Self-Managed Deployments附录C-用于测试自我管理部署的OpenSSL客户端证书

Warning

Disclaimer

This page is provided for testing purposes only and the certificates are for testing purposes only.此页面仅用于测试目的,证书仅用于测试。

The following tutorial provides some basic steps for creating test X.509 certificates.以下教程提供了创建测试X.509证书的一些基本步骤。

  • Do not use these certificates for production. Instead, follow your security policies.请勿将这些证书用于生产。相反,请遵循安全策略。
  • For information on OpenSSL, refer to the official OpenSSL docs. Although this tutorial uses OpenSSL, the material should not be taken as an authoritative reference on OpenSSL.有关OpenSSL的信息,请参阅OpenSSL官方文档。虽然本教程使用OpenSSL,但本材料不应被视为OpenSSL的权威参考。

Prerequisite先决条件

The procedure outlined on this page uses the test intermediate authority certificate and key mongodb-test-ia.crt and mongodb-test-ia.key created in Appendix A - OpenSSL CA Certificate for Testing Self-Managed Deployments.本页概述的程序使用附录A-用于测试自我管理部署的OpenSSL CA证书中创建的测试中间授权证书和键mongodb-test-ia.crtmongodb-test-ia.key

Procedure过程

The following procedure outlines the steps to create test certificates for MongoDB clients. For steps to create test certificates for MongoDB servers, see Appendix B - OpenSSL Server Certificates for Testing Self-Managed Deployments.以下过程概述了为MongoDB客户端创建测试证书的步骤。有关为MongoDB服务器创建测试证书的步骤,请参阅附录B-用于测试自我管理部署的OpenSSL服务器证书

A. Create the OpenSSL Configuration File创建OpenSSL配置文件

  1. Create a test configuration file openssl-test-client.cnf for your client with the following content:为客户端创建一个测试配置文件openssl-test-client.cnf,内容如下:

    # NOT FOR PRODUCTION USE. OpenSSL configuration file for testing.

    [ req ]
    default_bits = 4096
    default_keyfile = myTestClientCertificateKey.pem ## The default private key file name.
    default_md = sha256
    distinguished_name = req_dn
    req_extensions = v3_req


    [ v3_req ]
    subjectKeyIdentifier = hash
    basicConstraints = CA:FALSE
    keyUsage = critical, digitalSignature, keyEncipherment
    nsComment = "OpenSSL Generated Certificate for TESTING only. NOT FOR PRODUCTION USE."
    extendedKeyUsage = serverAuth, clientAuth


    [ req_dn ]
    countryName = Country Name (2 letter code)
    countryName_default =
    countryName_min = 2
    countryName_max = 2

    stateOrProvinceName = State or Province Name (full name)
    stateOrProvinceName_default = TestClientCertificateState
    stateOrProvinceName_max = 64

    localityName = Locality Name (eg, city)
    localityName_default = TestClientCertificateLocality
    localityName_max = 64

    organizationName = Organization Name (eg, company)
    organizationName_default = TestClientCertificateOrg
    organizationName_max = 64

    organizationalUnitName = Organizational Unit Name (eg, section)
    organizationalUnitName_default = TestClientCertificateOrgUnit
    organizationalUnitName_max = 64
    commonName = Common Name (eg, YOUR name)
    commonName_max = 64
  2. Optional. You can update the default Distinguished Name (DN) values. Ensure that client certificates differ from server certificates with regards to at least one of the following attributes: Organization (O), the Organizational Unit (OU) or the Domain Component (DC).可选。您可以更新默认的可分辨名称(DN)值。确保客户端证书在以下属性中的至少一个方面与服务器证书不同:组织(O)、组织单位(OU)或域组件(DC)。

B. Generate the Test PEM File for Client为客户端生成测试PEM文件

  1. Create the test key file mongodb-test-client.key.创建测试键文件mongodb-test-client.key

    openssl genrsa -out mongodb-test-client.key 4096
  2. Create the test certificate signing request mongodb-test-client.csr. When asked for Distinguished Name values, enter the appropriate values for your test certificate:创建测试证书签名请求mongodb-test-client.csr。当被要求输入可分辨名称值时,请为测试证书输入适当的值:

    Important

    The client certificate subject must differ to a server certificate subject with regards to at least one of the following attributes: Organization (O), the Organizational Unit (OU) or the Domain Component (DC).客户端证书使用者必须在以下至少一个属性上与服务器证书使用者不同:组织(O)、组织单位(OU)或域组件(DC)。

    openssl req -new -key mongodb-test-client.key -out mongodb-test-client.csr -config openssl-test-client.cnf
  3. Create the test client certificate mongodb-test-client.crt.创建测试客户端证书mongodb-test-client.crt

    openssl x509 -sha256 -req -days 365 -in mongodb-test-client.csr -CA mongodb-test-ia.crt -CAkey mongodb-test-ia.key -CAcreateserial -out mongodb-test-client.crt -extfile openssl-test-client.cnf -extensions v3_req
  4. Create the test PEM file for the client.为客户端创建测试PEM文件。

    cat mongodb-test-client.crt mongodb-test-client.key > test-client.pem

    You can use the test PEM file to configure mongosh for TLS/SSL testing. 您可以使用测试PEM文件配置mongosh进行TLS/SSL测试。For example, to connect to a mongod or a mongos:例如,要连接到mongodmongos

    Example示例

    Include the following options for the client:为客户提供以下选项:

    mongosh --tls --host <serverHost> --tlsCertificateKeyFile test-client.pem  --tlsCAFile test-ca.pem
    On macOS,在macOS上,

    If you are testing with Keychain Access to manage certificates, create a PKCS 12 file to add to Keychain Access instead of a PEM file:如果您正在使用Keychain Access进行测试以管理证书,请创建一个PKCS 12文件以添加到Keychain Access中,而不是PEM文件:

    openssl pkcs12 -export -out test-client.pfx -inkey mongodb-test-client.key -in mongodb-test-client.crt -certfile mongodb-test-ia.crt

    Once added to Keychain Access, instead of specifying the Certificate Key file, you can use the --tlsCertificateSelector to specify the certificate to use. 添加到Keychain Access后,您可以使用--tlsCertificateSelector指定要使用的证书,而不是指定证书键文件。If the CA file is also in Keychain Access, you can omit --tlsCAFile as well as in the following example:如果CA文件也在Keychain Access中,则可以省略--tlsCAFile,如下例所示:

    mongosh --tls --tlsCertificateSelector subject="<TestClientCertificateCommonName>"

    For adding certificates to Keychain Access, refer to your official documentation for Keychain Access.要向Keychain Access添加证书,请参阅Keychain Access的官方文档。