Skip to content

Commit e613109

Browse files
authored
Merge pull request #64 from rbrtmrtn/ssl
Add SSL config
2 parents bd44874 + da8a8fb commit e613109

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ 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 | No | undefined | Contents of a CA certificate for connecting over SSL. Use this if you need to store the entire certificate in an environment variable, e.g. for Docker. |
56+
| SSL_ROOT_CERT_PATH | No | undefined | Path to a CA certificate file for connecting over SSL. Note that setting `SSL_ROOT_CERT` overrides this. |
5557

5658

5759
### Step 3: fire it up!
@@ -151,3 +153,21 @@ map.on('load', function() {
151153
### Changes require a Restart
152154

153155
If you modify code or add a route, dirt will not see it until dirt is restarted.
156+
157+
### TLS/SSL
158+
159+
If you see an error like
160+
161+
```
162+
no pg_hba.conf entry for host <host>, user <user>, database <database>, no encryption
163+
```
164+
165+
you may need to connect to your server over SSL. Obtain a CA certificate and set `SSL_ROOT_CERT_PATH=<path to the certificate>` in `.env`. If you're still getting an error, check the end of your connection string for `?sslmode=require` and try removing it. You should still be able to connect over SSL.
166+
167+
If you're running Dirt on Docker, it may be easier to pass the contents of the certificate with `SSL_ROOT_CERT`. Example:
168+
169+
```bash
170+
docker run -dp 3000:3000 -e POSTGRES_CONNECTION=<connection string> -e SSL_ROOT_CERT=$(cat ca.crt) dirt
171+
```
172+
173+
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

Lines changed: 14 additions & 3 deletions
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,19 @@ 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) {
25+
postgresConfig.ssl = {
26+
ca: process.env.SSL_ROOT_CERT
27+
}
28+
} else if (process.env.SSL_ROOT_CERT_PATH) {
29+
postgresConfig.ssl = {
30+
ca: fs.readFileSync(process.env.SSL_ROOT_CERT_PATH).toString()
31+
}
32+
}
33+
34+
fastify.register(require('@fastify/postgres'), postgresConfig)
2435

2536
// COMPRESSION
2637
// add x-protobuf

0 commit comments

Comments
 (0)