Skip to content

Commit 4348595

Browse files
committed
Fix TLS certificate documentation for mongoose v8
1 parent aa4b38a commit 4348595

File tree

3 files changed

+44
-34
lines changed

3 files changed

+44
-34
lines changed

docs/guides.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ integrating Mongoose with external tools and frameworks.
4141
* [Transactions](transactions.html)
4242
* [MongoDB Driver Deprecation Warnings](deprecations.html)
4343
* [Testing with Jest](jest.html)
44-
* [SSL Connections](tutorials/ssl.html)
44+
* [TLS/SSL Connections](tutorials/ssl.html)
4545
* [MongoDB Client Side Field Level Encryption](field-level-encryption.html)
4646

4747
## Other Guides

docs/migrating_to_8.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ There's a few noteable changes in MongoDB Node driver v6 that affect Mongoose:
7171

7272
1. The `ObjectId` constructor no longer accepts strings of length 12. In Mongoose 7, `new mongoose.Types.ObjectId('12charstring')` was perfectly valid. In Mongoose 8, `new mongoose.Types.ObjectId('12charstring')` throws an error.
7373

74+
1. Deprecated SSL options have been removed
75+
76+
- `sslCA` -> `tlsCAFile`
77+
- `sslCRL` -> `tlsCRLFile`
78+
- `sslCert` -> `tlsCertificateKeyFile`
79+
- `sslKey` -> `tlsCertificateKeyFile`
80+
- `sslPass` -> `tlsCertificateKeyFilePassword`
81+
- `sslValidate` -> `tlsAllowInvalidCertificates`
82+
- `tlsCertificateFile` -> `tlsCertificateKeyFile`
83+
7484
<h2 id="removed-findoneandremove"><a href="#removed-findoneandremove">Removed <code>findOneAndRemove()</code></a></h2>
7585

7686
In Mongoose 7, `findOneAndRemove()` was an alias for `findOneAndDelete()` that Mongoose supported for backwards compatibility.

docs/tutorials/ssl.md

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
# SSL Connections
1+
# TSL/SSL Connections
22

3-
Mongoose supports connecting to [MongoDB clusters that require SSL connections](https://www.mongodb.com/docs/manual/tutorial/configure-ssl/). Setting the `ssl` option to `true` in [`mongoose.connect()`](../api/mongoose.html#mongoose_Mongoose-connect) or your connection string is enough to connect to a MongoDB cluster using SSL:
3+
Mongoose supports connecting to [MongoDB clusters that require TLS/SSL connections](https://www.mongodb.com/docs/manual/tutorial/configure-ssl/). Setting the either the `tls` or `ssl` option to `true` in [`mongoose.connect()`](../api/mongoose.html#mongoose_Mongoose-connect) or your connection string is enough to connect to a MongoDB cluster using TLS/SSL:
44

55
```javascript
66
mongoose.connect('mongodb://127.0.0.1:27017/test', { ssl: true });
@@ -12,31 +12,29 @@ mongoose.connect('mongodb://127.0.0.1:27017/test?ssl=true');
1212
The `ssl` option defaults to `false` for connection strings that start with `mongodb://`. However,
1313
the `ssl` option defaults to `true` for connection strings that start with `mongodb+srv://`. So if you are using an srv connection string to connect to [MongoDB Atlas](https://www.mongodb.com/cloud/atlas), SSL is enabled by default.
1414

15-
If you try to connect to a MongoDB cluster that requires SSL without enabling the `ssl` option, `mongoose.connect()`
16-
will error out with the below error:
15+
If you try to connect to a MongoDB cluster that requires TLS/SSL without enabling the `tls`/`ssl` option, `mongoose.connect()` will error out with the below error:
1716

1817
```no-highlight
1918
MongooseServerSelectionError: connection <monitor> to 127.0.0.1:27017 closed
2019
at NativeConnection.Connection.openUri (/node_modules/mongoose/lib/connection.js:800:32)
2120
...
2221
```
2322

24-
## SSL Validation
23+
## TLS/SSL Validation
2524

26-
By default, Mongoose validates the SSL certificate against a [certificate authority](https://en.wikipedia.org/wiki/Certificate_authority) to ensure the SSL certificate is valid. To disable this validation, set the `sslValidate` option
27-
to `false`.
25+
By default, Mongoose validates the TLS/SSL certificate against a [certificate authority](https://en.wikipedia.org/wiki/Certificate_authority) to ensure the TLS/SSL certificate is valid. To disable this validation, set the `tlsAllowInvalidCertificates` (or `tlsInsecure`) option to `true`.
2826

2927
```javascript
3028
mongoose.connect('mongodb://127.0.0.1:27017/test', {
31-
ssl: true,
32-
sslValidate: false
29+
tls: true,
30+
tlsAllowInvalidCertificates: true,
3331
});
3432
```
3533

36-
In most cases, you should not disable SSL validation in production. However, `sslValidate: false` is often helpful
37-
for debugging SSL connection issues. If you can connect to MongoDB with `sslValidate: false`, but not with
38-
`sslValidate: true`, then you can confirm Mongoose can connect to the server and the server is configured to use
39-
SSL correctly, but there's some issue with the SSL certificate.
34+
In most cases, you should not disable TLS/SSL validation in production. However, `tlsAllowInvalidCertificates: true` is often helpful
35+
for debugging SSL connection issues. If you can connect to MongoDB with `tlsAllowInvalidCertificates: true`, but not with
36+
`tlsAllowInvalidCertificates: false`, then you can confirm Mongoose can connect to the server and the server is configured to use
37+
TLS/SSL correctly, but there's some issue with the certificate.
4038

4139
For example, a common issue is the below error message:
4240

@@ -45,17 +43,16 @@ MongooseServerSelectionError: unable to verify the first certificate
4543
```
4644

4745
This error is often caused by [self-signed MongoDB certificates](https://medium.com/@rajanmaharjan/secure-your-mongodb-connections-ssl-tls-92e2addb3c89) or other situations where the certificate sent by the MongoDB
48-
server is not registered with an established certificate authority. The solution is to set the `sslCA` option, which essentially sets a list of allowed SSL certificates.
46+
server is not registered with an established certificate authority. The solution is to set the `tlsCAFile` option, which essentially sets a list of allowed SSL certificates.
4947

5048
```javascript
5149
await mongoose.connect('mongodb://127.0.0.1:27017/test', {
52-
ssl: true,
53-
sslValidate: true,
50+
tls: true,
5451
// For example, see https://medium.com/@rajanmaharjan/secure-your-mongodb-connections-ssl-tls-92e2addb3c89
5552
// for where the `rootCA.pem` file comes from.
5653
// Please note that, in Mongoose >= 5.8.3, `sslCA` needs to be
5754
// the **path to** the CA file, **not** the contents of the CA file
58-
sslCA: `${__dirname}/rootCA.pem`
55+
tlsCAFile: `${__dirname}/rootCA.pem`,
5956
});
6057
```
6158

@@ -66,7 +63,7 @@ MongooseServerSelectionError: Hostname/IP does not match certificate's altnames:
6663
```
6764

6865
The SSL certificate's [common name](https://knowledge.digicert.com/solution/SO7239.html) **must** line up with the host name
69-
in your connection string. If the SSL certificate is for `hostname2.mydomain.com`, your connection string must connect to `hostname2.mydomain.com`, not any other hostname or IP address that may be equivalent to `hostname2.mydomain.com`. For replica sets, this also means that the SSL certificate's common name must line up with the [machine's `hostname`](../connections.html#replicaset-hostnames).
66+
in your connection string. If the SSL certificate is for `hostname2.mydomain.com`, your connection string must connect to `hostname2.mydomain.com`, not any other hostname or IP address that may be equivalent to `hostname2.mydomain.com`. For replica sets, this also means that the SSL certificate's common name must line up with the [machine's `hostname`](../connections.html#replicaset-hostnames). To disable this validation, set the `tlsAllowInvalidHostnames` option to `true`.
7067

7168
## X.509 Authentication
7269

@@ -75,40 +72,43 @@ If you're using [X.509 authentication](https://www.mongodb.com/docs/drivers/node
7572
```javascript
7673
// Do this:
7774
const username = 'myusername';
78-
await mongoose.connect(`mongodb://${encodeURIComponent(username)}@127.0.0.1:27017/test`, {
79-
ssl: true,
80-
sslValidate: true,
81-
sslCA: `${__dirname}/rootCA.pem`,
82-
authMechanism: 'MONGODB-X509'
83-
});
75+
await mongoose.connect(
76+
`mongodb://${encodeURIComponent(username)}@127.0.0.1:27017/test`,
77+
{
78+
tls: true,
79+
tlsCAFile: `${__dirname}/rootCA.pem`,
80+
authMechanism: 'MONGODB-X509',
81+
}
82+
);
8483

8584
// Not this:
8685
await mongoose.connect('mongodb://127.0.0.1:27017/test', {
87-
ssl: true,
88-
sslValidate: true,
89-
sslCA: `${__dirname}/rootCA.pem`,
86+
tls: true,
87+
tlsCAFile: `${__dirname}/rootCA.pem`,
9088
authMechanism: 'MONGODB-X509',
91-
auth: { username }
89+
auth: { username },
9290
});
9391
```
9492

9593
## X.509 Authentication with MongoDB Atlas
9694

97-
With MongoDB Atlas, X.509 certificates are not Root CA certificates and will not work with the `sslCA` parameter as self-signed certificates would. If the `sslCA` parameter is used an error similar to the following would be raised:
95+
With MongoDB Atlas, X.509 certificates are not Root CA certificates and will not work with the `tlsCAFile` parameter as self-signed certificates would. If the `tlsCAFile` parameter is used an error similar to the following would be raised:
9896

9997
```no-highlight
10098
MongoServerSelectionError: unable to get local issuer certificate
10199
```
102100

103-
To connect to a MongoDB Atlas cluster using X.509 authentication the correct option to set is `tlsCertificateKeyFile`. The connection string already specifies the `authSource` and `authMechanism`, and the DNS `TXT` record would supply the parameter and value for `sslValidate`, however they're included below as `connect()` options for completeness:
101+
To connect to a MongoDB Atlas cluster using X.509 authentication the correct option to set is `tlsCertificateKeyFile`. The connection string already specifies the `authSource` and `authMechanism`, however they're included below as `connect()` options for completeness:
104102

105103
```javascript
106-
const url = 'mongodb+srv://xyz.mongodb.net/test?authSource=%24external&authMechanism=MONGODB-X509';
104+
const url =
105+
'mongodb+srv://xyz.mongodb.net/test?authSource=%24external&authMechanism=MONGODB-X509';
107106
await mongoose.connect(url, {
108-
sslValidate: true,
107+
tls: true,
108+
// location of a local .pem file that contains both the client's certificate and key, e.g.
109109
tlsCertificateKeyFile: '/path/to/certificate.pem',
110110
authMechanism: 'MONGODB-X509',
111-
authSource: '$external'
111+
authSource: '$external',
112112
});
113113
```
114114

0 commit comments

Comments
 (0)