Skip to content

Commit da8a8fb

Browse files
committed
Allow raw SSL root cert in environment variables
This is mainly for Docker, where it's easier to supply the cert in an environment variable rather than mounting a volume with the file.
1 parent 42f9c8f commit da8a8fb

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +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_PATH | No | undefined | Path to a CA certificate if using TLS/SSL |
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. |
5657

5758

5859
### Step 3: fire it up!
@@ -163,4 +164,10 @@ no pg_hba.conf entry for host <host>, user <user>, database <database>, no encry
163164

164165
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.
165166

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+
166173
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

+8-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ if (!("POSTGRES_CONNECTION" in process.env)) {
2121
// POSTGRES CONNECTION
2222
const postgresConfig = { connectionString: process.env.POSTGRES_CONNECTION }
2323

24-
if (process.env.SSL_ROOT_CERT_PATH) {
25-
const ca = fs.readFileSync(process.env.SSL_ROOT_CERT_PATH).toString()
26-
postgresConfig.ssl = { ca }
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+
}
2732
}
2833

2934
fastify.register(require('@fastify/postgres'), postgresConfig)

0 commit comments

Comments
 (0)