TLS Configuration
Configure TLS for secure gRPC/ConnectRPC communication in Connectum services.
Outcome: the server loads a key/certificate pair and serves TLS. Clients must trust its issuer and verify its hostname. Client-certificate enforcement is a separate mTLS task; exact loading fields live in TLSOptions.
TLS Options
The tls option accepts either explicit key/certificate paths or a directory that contains server.key and server.crt. Do not copy this shape into application types; use the generated TLSOptions contract.
Explicit File Paths
Provide paths directly to the key and certificate files:
const server = createServer({
services: [routes],
tls: {
keyPath: './keys/server.key',
certPath: './keys/server.crt',
},
});Paths are resolved relative to the current working directory. Supply keyPath and certPath together. Setting only one of them is a configuration error: readTLSCertificates() throws an Error that names the parameter you set and the one that is missing, before it reads any file, so server.start() rejects with it. Earlier versions silently ignored the single path and read both files from the directory, which started the server with a different certificate than the one you named. An empty string counts as not set.
Precedence when several sources are present: both explicit paths win over dirPath, dirPath wins over TLS_DIR_PATH, and TLS_DIR_PATH wins over the built-in default directory. A single explicit path is never combined with a directory, whichever directory it would come from.
Directory-Based Configuration
Point to a directory containing server.key and server.crt:
const server = createServer({
services: [routes],
tls: {
dirPath: './keys',
},
});The server looks for:
<dirPath>/server.key<dirPath>/server.crt
Do not combine dirPath with only one of keyPath or certPath; that combination is rejected as described above.
Start-up Errors
When a file cannot be read or is not usable, server.start() rejects with an error that names the file by its resolved path and the setting the path came from: tls.keyPath, tls.certPath, tls.dirPath, TLS_DIR_PATH, or the default directory. The files are handed to the server as they are; only when it refuses them are the key and the certificate parsed on their own and then together, with the passphrase and ciphers of http2Options the server used, so the message tells a file that is not PEM from a key that does not belong to the certificate (it names both files). For a file that the server refused, the error it threw is the cause; for a file that could not be read, the file system error is, and its code (for example ENOENT) and path are kept on the new error. A refusal that is not about the files (an invalid ciphers string, say) is reported as the server raised it.
TLS key file "/srv/app/bad.key" (from tls.keyPath) is not a usable PEM private key: error:1E08010C:DECODER routines::unsupportedAn operator who relied on the default directory sees that it was used: the message says "the default TLS directory" and gives the path it tried.
Environment Variable Configuration
When keyPath, certPath, and dirPath are not supplied, readTLSCertificates() uses TLS_DIR_PATH or its environment-dependent default (../../../keys relative to the working directory outside production, the working directory in production). The environment schema does not define TLS_KEY_PATH or TLS_CERT_PATH; pass explicit paths through tls instead. See Environment Configuration for the directory defaults.
TLS Utility Functions
readTLSCertificates()
Reads TLS key and certificate files. It only reads: a failure to read names the file and the setting; whether the content is a usable key and certificate is decided when the server starts:
import { readTLSCertificates } from '@connectum/core';
// With explicit paths
const { key, cert } = readTLSCertificates({
keyPath: './keys/server.key',
certPath: './keys/server.crt',
});
// With directory path
const { key, cert } = readTLSCertificates({
dirPath: './keys',
});
// With default TLS path (from TLS_DIR_PATH env or convention)
const { key, cert } = readTLSCertificates();getTLSPath()
Returns the TLS directory path based on environment and convention:
import { getTLSPath } from '@connectum/core';
const path = getTLSPath();
// In production (NODE_ENV=production): current working directory
// In development: ../../../keys relative to cwd
// Overridden by: TLS_DIR_PATH environment variableSelf-Signed Certificates for Development
Generate self-signed certificates for local development:
# Create keys directory
mkdir -p keys
# Generate self-signed certificate (valid for 365 days)
openssl req -x509 -newkey rsa:4096 \
-keyout keys/server.key \
-out keys/server.crt \
-days 365 \
-nodes \
-addext "subjectAltName=DNS:localhost,IP:127.0.0.1" \
-subj "/C=US/ST=Local/L=Local/O=Dev/CN=localhost"Use in your service:
import { createServer } from '@connectum/core';
import { Healthcheck, healthcheckManager, ServingStatus } from '@connectum/healthcheck';
import { Reflection } from '@connectum/reflection';
const server = createServer({
services: [routes],
port: 5000,
protocols: [Healthcheck({ httpEnabled: true }), Reflection()],
tls: {
dirPath: './keys',
},
});
server.on('ready', () => healthcheckManager.update(ServingStatus.SERVING));
await server.start();Development only
Self-signed certificates should only be used in development. For production, use certificates from a trusted Certificate Authority (CA) or a service like Let's Encrypt.
Testing with TLS
When testing with grpcurl against a TLS-enabled server using self-signed certificates:
The local server above mounts Reflection for grpcurl list and HTTP health for /healthz. TLS configuration alone does not add either endpoint. The certificate includes a localhost Subject Alternative Name so the verified commands can match the server's hostname.
# Skip certificate verification (development only)
grpcurl -insecure localhost:5000 list
# Or provide the CA certificate
grpcurl -cacert keys/server.crt localhost:5000 listWith curl:
# Skip certificate verification
curl -k https://localhost:5000/healthz
# Or provide the CA certificate
curl --cacert keys/server.crt https://localhost:5000/healthzAdditional HTTP/2 Options
Use http2Options for any additional Node.js HTTP/2 secure server options:
const server = createServer({
services: [routes],
port: 5000,
tls: {
keyPath: './keys/server.key',
certPath: './keys/server.crt',
},
http2Options: {
// Minimum TLS version
minVersion: 'TLSv1.3',
// Cipher suites
ciphers: 'TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256',
// Session timeout
sessionTimeout: 300,
},
// Handshake timeout
handshakeTimeout: 30000,
});HTTP/1.1 Support
By default, Connectum allows HTTP/1.1 connections (via ALPN negotiation). This is important for ConnectRPC clients that use HTTP/1.1 with JSON:
const server = createServer({
services: [routes],
tls: { dirPath: './keys' },
allowHTTP1: true, // default: true
});To restrict to HTTP/2 only:
const server = createServer({
services: [routes],
tls: { dirPath: './keys' },
allowHTTP1: false,
});Related
- Security Overview -- back to overview
- Mutual TLS (mTLS) -- client certificate authentication, production best practices
- @connectum/core -- Package Guide
- @connectum/core API -- Full API Reference