Skip to content

Commit 261a15c

Browse files
committed
Accept SSL root certificates
1 parent a517684 commit 261a15c

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ This is the complete complete list of environmental variables that can be set.
5252
| CACHE_EXPIRESIN | No | 3600 | [Max age in seconds](https://github.com/fastify/fastify-caching) |
5353
| CACHE_SERVERCACHE | No | undefined | Max age in seconds for [shared cache](https://github.com/fastify/fastify-caching) (i.e. CDN) |
5454
| RATE_MAX | No | undefined | Requests per minute [rate limiter](https://github.com/fastify/fastify-rate-limit) (limiter not used if RATE_LIMIT not set) |
55+
| SSL_ROOT_CERT_PATH | No | undefined | Path to a CA certificate if using TLS/SSL |
5556

5657

5758
### Step 3: fire it up!
@@ -151,3 +152,15 @@ map.on('load', function() {
151152
### Changes require a Restart
152153

153154
If you modify code or add a route, dirt will not see it until dirt is restarted.
155+
156+
### TLS/SSL
157+
158+
If you see an error like
159+
160+
```
161+
no pg_hba.conf entry for host <host>, user <user>, database <database>, no encryption
162+
```
163+
164+
you made need to connect to your server over SSL. Obtain a CA certificate and set `SSL_ROOT_CERT_PATH=<path to the certificate>` in `.env`.
165+
166+
If you can't get a certificate or want to bypass the error, you can try setting `NODE_TLS_REJECT_UNAUTHORIZED=0`. Note that this is unsafe and is not recommended in production.

index.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const fs = require('fs')
12
const path = require('path')
23
require("dotenv").config()
34

@@ -18,9 +19,14 @@ if (!("POSTGRES_CONNECTION" in process.env)) {
1819
}
1920

2021
// POSTGRES CONNECTION
21-
fastify.register(require('@fastify/postgres'), {
22-
connectionString: process.env.POSTGRES_CONNECTION
23-
})
22+
const postgresConfig = { connectionString: process.env.POSTGRES_CONNECTION }
23+
24+
if (process.env.SSL_ROOT_CERT_PATH) {
25+
const ca = fs.readFileSync(process.env.SSL_ROOT_CERT_PATH).toString()
26+
postgresConfig.ssl = { ca }
27+
}
28+
29+
fastify.register(require('@fastify/postgres'), postgresConfig)
2430

2531
// COMPRESSION
2632
// add x-protobuf

0 commit comments

Comments
 (0)