- Security >
- Authentication >
- x.509 >
- Use x.509 Certificates to Authenticate Clients
Use x.509 Certificates to Authenticate Clients¶
On this page
The following procedure sets up x.509 certificate authentication for
client authentication on a standalone mongod instance.
To use x.509 authentication for replica sets or sharded clusters, see Use x.509 Certificate for Membership Authentication.
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.
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.
To use x.509 authentication, --tlsCAFile or net.tls.CAFile
must be specified unless you are using --tlsCertificateSelector
or --net.tls.certificateSelector.
Client x.509 Certificate¶
You must have valid x.509 certificates. The client x.509 certificates must meet the client certificate requirements.
Starting in MongoDB 4.2, if you specify
--tlsAllowInvalidateCertificates or
net.tls.allowInvalidCertificates: true when using x.509
authentication, an invalid certificate is only sufficient to
establish a TLS connection but it is insufficient for
authentication.
Procedure¶
Deploy with x.509 Authentication¶
- Command Options
- Configuration File
You can configure a mongod instance for x.509
authentication from the command-line.
To configure a standalone mongod instance, run
the following command:
Include additional options as required for your configuration.
The x.509 configuration requires:
| Option | Notes |
|---|---|
--tlsMode |
Specify requireTLS. |
--tlsCertificateKeyFile |
Specify the instance’s x.509 certificate to present to clients. |
--tlsCAFile |
Specify the Certificate Authority file to verify the certificates presented to the instance. |
You can configure a mongod for x.509
authentication in the configuration file.
To configure a standalone mongod instance, add
the following configuration options to your configuration
file:
Include additional options as required for your configuration.
The x.509 configuration requires:
| Option | Notes |
|---|---|
net.tls.mode |
Specify requireTLS. |
net.tls.certificateKeyFile |
Specify the instance’s x.509 certificate to present to clients. |
net.tls.CAFile |
Specify the Certificate Authority file to verify the certificates presented to the instance. |
To set up x.509 authentication for replica sets or sharded clusters, see Use x.509 Certificate for Membership Authentication.
Add x.509 Certificate subject as a User¶
To 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. 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.
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. - The RDNs in the
subjectstring must be compatible with the RFC2253 standard.
You can retrieve the
RFC2253formattedsubjectfrom the client certificate with the following command:The command returns the
subjectstring and the certificate:Add the
RFC2253compliant value of thesubjectas a user. Omit spaces as needed.The following example adds a user and grants the user
readWriterole in thetestdatabase and theuserAdminAnyDatabaserole:See Manage Users and Roles for details on adding a user with roles.
Authenticate with a x.509 Certificate¶
After you have added the x.509 client certificate subject as a corresponding MongoDB user, you can authenticate with the client certificate:
- Connect with Authentication
- Authenticate after Connection
To authenticate during connection, run the following command:
| Option | Notes |
|---|---|
--tls |
|
--tlsCertificateKeyFile |
Specify the client’s x.509 file. |
--tlsCAFile |
Specify the Certificate Authority file to verify the
certificate presented by the mongod
instance. |
--authenticationDatabase |
Specify '$external'. |
--authenticationMechanism |
Specify MONGODB-X509. |
You can connect without authentication and use the
db.auth() method to authenticate after
connection.
For example, if using mongosh,
Connect
mongoshto themongod:Option Notes --tls--tlsCertificateKeyFileSpecify the client’s x.509 file. --tlsCAFileSpecify the Certificate Authority file to verify the certificate presented by the mongodormongosinstance.To authenticate, use the
db.auth()method in the$externaldatabase. For themechanismfield, specify"MONGODB-X509".
Next Steps¶
To use x.509 authentication for replica sets or sharded clusters, see Use x.509 Certificate for Membership Authentication.