On this page本页内容
MongoDB supports x.509 certificate authentication for client authentication and internal authentication of the members of replica sets and sharded clusters.
x.509 certificate authentication requires a secure TLS/SSL connection.
Note
Starting in version 4.0, MongoDB disables support for TLS 1.0 encryption on systems where TLS 1.1+ is available. For more details, see Disable TLS 1.0.
For production use, your MongoDB deployment should use valid certificates generated and signed by a single certificate authority. You or your organization can generate and maintain an independent certificate authority, or use certificates generated by a third-party TLS/SSL vendor. Obtaining and managing certificates is beyond the scope of this documentation.
To authenticate to servers, clients can use x.509 certificates instead of usernames and passwords.
The client certificate must have the following properties:
DN
), must differ from that of a Member x.509 Certificate.
At least one of the Organization (O
), Organizational Unit (OU
), or Domain Component (DC
) attributes in the client certificate must differ from those in the net.tls.clusterFile
and net.tls.certificateKeyFile
server certificates.
If the MongoDB deployment has tlsX509ClusterAuthDNOverride
set (available starting in MongoDB 4.2), the client x.509 certificate’s subject must also differ from that value.
Warning
If a client x.509 certificate’s subject has the same O
, OU
, and DC
combination as the Member x.509 Certificate (or tlsX509ClusterAuthDNOverride
if set), the client connection is rejected. Only cluster member x509 certificates should use same O
, OU
, and DC
combinations as this grants full permissions.
Changed in version 4.4.在版本4.4中更改。mongod
/ mongos
logs a warning on connection if the presented x.509 certificate expires within 30
days of the mongod/mongos
host system time. See x.509 Certificates Nearing Expiry Trigger Warnings for more information.
$external
Database¶To authenticate with a client certificate, you must first add the value of the subject
from the client certificate as a MongoDB user. Each unique x.509 client certificate corresponds to a single MongoDB user;
i.e. you cannot use a single client certificate to authenticate more than one MongoDB user.
Add the user in the $external
database; i.e. the Authentication Database is the $external
database
Changed in version 3.6.3:To use sessions with $external
authentication users (i.e. Kerberos, LDAP, x.509 users), the usernames cannot be greater than 10k bytes.
To connect and authenticate using x.509 client certificate:
--tls
(or the deprecated --ssl
option)--tlsCertificateKeyFile
(or the deprecated --sslPEMKeyFile
option)--tlsCertificateKeyFilePassword
(or the deprecated --sslPEMKeyPassword
option) if the certificate key file is encrypted--authenticationDatabase '$external'
--authenticationMechanism MONGODB-X509
--ssl
--sslPEMKeyFile
--sslPEMKeyPassword
option if the --sslPEMKeyFile
is encrypted.--authenticationDatabase '$external'
--authenticationMechanism MONGODB-X509
You can also make the TLS/SSL connection first, and then use db.auth()
in the $external
database to authenticate.
For examples of both cases, see the Authenticate with a x.509 Certificate (Using tls Options) section in Use x.509 Certificates to Authenticate Clients
For internal authentication, members of sharded clusters and replica sets can use x.509 certificates instead of keyfiles, which use the SCRAM authentication mechanism.
The member certificate (net.tls.clusterFile
, if specified, and net.tls.certificateKeyFile
), used to verify membership to the sharded cluster or a replica set, must have the following properties:
DN
), found in the member certificate’s subject
, must specify a non-empty value for at least one of the following attributes: Organization (O
), the Organizational Unit (OU
) or the Domain Component (DC
).O
’s), the Organizational Unit attributes (OU
’s), and the Domain Components (DC
’s) must match those from both the net.tls.clusterFile
and net.tls.certificateKeyFile
certificates for the other cluster members (or the tlsX509ClusterAuthDNOverride
value, if set).
To match, the certificate must match all specifications of these attributes, or even the non-specification of these attributes. The order of the attributes does not matter.
In the following example, the two DN
’s contain matching specifications for O
, OU
as well as the non-specification of the DC
attribute.
However, the following two DN
’s contain a mismatch for the OU
attribute since one contains two OU
specifications and the other, only one specification.
CN
) or one of the Subject Alternative Name (SAN
) entries must match the hostname of the server, used by the other members of the cluster. Starting in MongoDB 4.2, when performing comparison of SAN, MongoDB supports comparison of DNS names or IP addresses. In previous versions, MongoDB only supports comparisons of DNS names.
For example, the certificates for a cluster could have the following subjects:
extendedKeyUsage
)
setting, the value must include clientAuth
(“TLS Web Client Authentication”).
You can also use a certificate that does not include the Extended Key Usage (EKU).
Changed in version 4.4:mongod
/ mongos
logs a warning on connection if the presented x.509 certificate expires within 30
days of the mongod/mongos
host system time. See x.509 Certificates Nearing Expiry Trigger Warnings for more information.
In addition to any TLS/SSL configurations as appropriate for your deployment, include the following to specify x.509 for internal authentication for each member of your replica set (i.e. the mongod
instances) or sharded cluster (i.e. the mongod
and mongos
instances):
security.clusterAuthMode
or --clusterAuthMode
set to x509
net.tls.clusterFile
or --tlsClusterFile
(both new in MongoDB 4.2)However, if no cluster file is specified, members can use their certificate key file specified in net.tls.certificateKeyFile
or --tlsCertificateKeyFile
(both new in MongoDB 4.2) for membership authentication. This certificate key file
is used by mongod
(and mongos
) instances to prove their identity to clients, but can also be used for membership authentication. To use for both client authentication and membership authentication, the certificate must either:
extendedKeyUsage
orextendedKeyUsage
valuesNote
Athough still available, net.ssl.clusterFile
(and the correponding --sslClusterFile
) and net.ssl.PEMKeyFile
(and the corresponding --sslPEMKeyFile
)
are deprecated as of MongoDB 4.2.
For deployments using MongoDB version 4.0 and earlier, use net.ssl.clusterFile
(or the corresponding --sslClusterFile
) and net.ssl.PEMKeyFile
(or the corresponding --sslPEMKeyFile
).
Changed in version 4.4:mongod
/ mongos
logs a warning on connection if the presented x.509 certificate expires within 30
days of the mongod/mongos
host system time. See x.509 Certificates Nearing Expiry Trigger Warnings for more information.
For an example of x.509 internal authentication, see Use x.509 Certificate for Membership Authentication.