Use X.509 Certificates to Authenticate Clients on Self-Managed Deployments使用X.509证书对自我管理部署上的客户端进行身份验证
The following procedure sets up X.509 certificate authentication for client authentication on a standalone 以下过程为独立mongod instance. This is also known as Mutual TLS or mTLS.mongod实例上的客户端身份验证设置X.509证书身份验证。这也被称为双向TLS或mTLS。
To use X.509 authentication for replica sets or sharded clusters, see Use X.509 Certificates for Membership Authentication with Self-Managed MongoDB.要对副本集或分片集群使用X.509身份验证,请参阅使用X.509证书进行自我管理MongoDB的成员身份验证。
Prerequisites先决条件
A full description of TLS/SSL, PKI (Public Key Infrastructure) certificates, in particular X.509 certificates, and Certificate Authority is beyond the scope of this document. This tutorial assumes prior knowledge of TLS/SSL as well as access to valid X.509 certificates.TLS/SSL、PKI(公钥基础设施)证书(特别是X.509证书)和证书颁发机构的完整描述超出了本文的范围。本教程假设您具备TLS/SSL的先验知识以及有效X.509证书的访问权限。
Certificate Authority证书颁发机构
For production use, your MongoDB deployment should use valid certificates generated and signed by a certificate authority. You or your organization can generate and maintain an independent certificate authority, or use certificates generated by third-party TLS vendors. Obtaining and managing certificates is beyond the scope of this documentation.对于生产使用,MongoDB部署应该使用由证书颁发机构生成和签名的有效证书。您或组织可以生成和维护独立的证书颁发机构,或使用第三方TLS供应商生成的证书。获取和管理证书超出了本文档的范围。
To use X.509 authentication, 要使用X.509身份验证,必须指定--tlsCAFile or net.tls.CAFile must be specified unless you are using --tlsCertificateSelector or --net.tls.certificateSelector.--tlsCAFile或net.tls.CAFile,除非您使用的是--tlsCertificateSelector或--nettlscertificateSelector。
Client X.509 Certificate客户端X.509证书
You must have valid X.509 certificates. The client X.509 certificates must meet the client certificate requirements.您必须拥有有效的X.509证书。客户端X.509证书必须满足客户端证书要求。
If you specify 如果指定--tlsAllowInvalidCertificates or net.tls.allowInvalidCertificates: true, an invalid certificate is sufficient only to establish a TLS connection but it is insufficient for authentication.--tlsAllowInvalidCertificates或net.tls.allowInvalidCertificates: true,则无效证书仅足以建立TLS连接,但不足以进行身份验证。
Procedure过程
Deploy with X.509 Authentication使用X.509身份验证进行部署
Command Options命令选项
You can configure a 您可以从命令行配置mongod instance for X.509 authentication from the command-line.mongod实例进行X.509身份验证。
To configure a standalone 要配置独立的mongod instance, run the following command:mongod实例,请运行以下命令:
mongod --tlsMode requireTLS \
--tlsCertificateKeyFile <path to TLS/SSL certificate and key PEM file> \
--tlsCAFile <path to root CA PEM file> --bind_ip <hostnames>
Include additional options as required for your configuration.根据配置要求,包括其他选项。
The X.509 configuration requires:X.509配置要求:
--tlsMode | requireTLS.requireTLS。 |
--tlsCertificateKeyFile | |
--tlsCAFile |
Configuration File配置文件
You can configure a 您可以在配置文件中为X.509身份验证配置mongod for X.509 authentication in the configuration file.mongod。
To configure a standalone 要配置独立的mongod instance, add the following configuration options to your configuration file:mongod实例,请在配置文件中添加以下配置选项:
net:
tls:
mode: requireTLS
certificateKeyFile: <path to TLS/SSL certificate and key PEM file>
CAFile: <path to root CA PEM file>
Include additional options as required for your configuration.根据配置要求,包括其他选项。
The X.509 configuration requires:X.509配置要求:
net.tls.mode | requireTLS.requireTLS。 |
net.tls.certificateKeyFile | |
net.tls.CAFile |
To set up X.509 authentication for replica sets or sharded clusters, see Use X.509 Certificates for Membership Authentication with Self-Managed MongoDB.要为副本集或分片集群设置X.509身份验证,请参阅使用X.509证书进行自我管理MongoDB的成员身份验证。
Add X.509 Certificate subject as a User将X.509证书subject添加为用户
subject as a UserTo authenticate with a client certificate, you must first add the value of the 要使用客户端证书进行身份验证,您必须首先将客户端证书中的subject from the client certificate as a MongoDB user to the $external database. subject值作为MongoDB用户添加到$external数据库中。Each unique X.509 client certificate corresponds to a single MongoDB user. You cannot use a single client certificate to authenticate more than one MongoDB user.每个唯一的X.509客户端证书对应一个MongoDB用户。您不能使用单个客户端证书对多个MongoDB用户进行身份验证。
Note
Username Requirements用户名要求
To use Client Sessions and Causal Consistency Guarantees with要对$externalauthentication users (Kerberos, LDAP, or X.509 users), usernames cannot be greater than 10k bytes.$external身份验证用户(Kerberos、LDAP或X.509用户)使用客户端会话和因果一致性保证,用户名不能大于10k字节。The RDNs in thesubjectstring must be compatible with the RFC2253 standard.subject字符串中的RDN必须与RFC2253标准兼容。
You can retrieve the您可以使用以下命令从客户端证书中检索RFC2253formattedsubjectfrom the client certificate with the following command:RFC2253格式的subject:openssl x509 -in <pathToClientPEM> -inform PEM -subject -nameopt RFC2253The command returns the该命令返回subjectstring and the certificate:subject字符串和证书:subject= CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry
-----BEGIN CERTIFICATE-----
# ...
-----END CERTIFICATE-----Add the将RFC2253compliant value of thesubjectas a user. Omit spaces as needed.subject的RFC2253兼容值添加为用户。根据需要省略空格。The following example adds a user and grants the user以下示例添加了一个用户,并在readWriterole in thetestdatabase and theuserAdminAnyDatabaserole:test数据库中授予用户readWrite角色和userAdminAnyDatabase角色:db.getSiblingDB("$external").runCommand(
{
createUser: "CN=myName,OU=myOrgUnit,O=myOrg,L=myLocality,ST=myState,C=myCountry",
roles: [
{ role: "readWrite", db: "test" },
{ role: "userAdminAnyDatabase", db: "admin" }
],
writeConcern: { w: "majority" , wtimeout: 5000 }
}
)See Manage Users and Roles on Self-Managed Deployments for details on adding a user with roles.有关添加具有角色的用户的详细信息,请参阅自我管理部署上的管理用户和角色。
Authenticate with a X.509 Certificate使用X.509证书进行身份验证
After you have added the X.509 client certificate subject as a corresponding MongoDB user, you can authenticate with the client certificate:将X.509客户端证书主体添加为相应的MongoDB用户后,您可以使用客户端证书进行身份验证:
Connection String连接字符串
To authenticate during connection, run the following command:要在连接期间进行身份验证,请运行以下命令:
mongosh --tls --tlsCertificateKeyFile <path to client PEM file> \
--tlsCAFile <path to root CA PEM file> \
--authenticationDatabase '$external' \
--authenticationMechanism MONGODB-X509
--tls | |
--tlsCertificateKeyFile | |
--tlsCAFile | mongod instance.mongod实例提供的证书。 |
--authenticationDatabase | '$external'.'$external'。 |
--authenticationMechanism | MONGODB-X509.MONGODB-X509。 |
Authenticate after Connection连接后进行身份验证
You can connect without authentication and use the 您可以在不进行身份验证的情况下进行连接,并在连接后使用db.auth() method to authenticate after connection.db.auth()方法进行身份验证。
For example, if using 例如如果使用mongosh,mongosh,
Connect将mongoshto themongod:mongosh连接到mongod:mongosh --tls --tlsCertificateKeyFile <path to client PEM file> \
--tlsCAFile <path to root CA PEM file>Option选项Notes备注--tls--tlsCertificateKeyFileSpecify the client's X.509 file.指定客户端的X.509文件。--tlsCAFileSpecify the Certificate Authority file to verify the certificate presented by the指定证书颁发机构文件以验证mongodormongosinstance.mongod或mongos实例提供的证书。To authenticate, use the要进行身份验证,请使用db.auth()method in the$externaldatabase. For themechanismfield, specify"MONGODB-X509".$external数据库中的db.auth()方法。对于机制字段,指定"MONGODB-X509"。db.getSiblingDB("$external").auth(
{
mechanism: "MONGODB-X509"
}
)
Next Steps后续步骤
To use X.509 authentication for replica sets or sharded clusters, see Use X.509 Certificates for Membership Authentication with Self-Managed MongoDB.要对副本集或分片集群使用X.509身份验证,请参阅使用X.509证书进行自我管理MongoDB的成员身份验证。