Skip to content

Commit 23a6a7d

Browse files
committed
feat(db): MTLS for redis/db
1 parent 9c27a4d commit 23a6a7d

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

src/app/api/redis.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
KEY_TOP_PODCASTS,
88
} from '@/data/constants';
99
import type { IEpisodeListing, IPodcast, IShortUrl } from '@/types';
10+
import { mtls } from '@/server/mtls';
1011

1112
/**
1213
* Redis cached entity with timestamp
@@ -61,6 +62,7 @@ const redis = new Redis({
6162
host: process.env.KV_REDIS_HOST,
6263
password: process.env.KV_REDIS_PASS,
6364
port: parseInt(process.env.KV_REDIS_PORT || '0', 10),
65+
tls: mtls,
6466
});
6567

6668
/**

src/server/db.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import postgres from 'postgres';
22

3+
import { mtls } from './mtls';
4+
35
const connectionString = process.env.DATABASE_URL;
46

57
if (!connectionString) {
@@ -10,6 +12,7 @@ export const sql = postgres(connectionString, {
1012
max: 10,
1113
idle_timeout: 20,
1214
connect_timeout: 10,
15+
ssl: mtls,
1316
types: {
1417
bigint: {
1518
to: 20,

src/server/mtls.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const pem = (value?: string) => value?.replace(/\\n/g, '\n');
2+
3+
const ca = pem(process.env.MTLS_CA);
4+
5+
export const mtls = ca
6+
? {
7+
ca,
8+
cert: pem(process.env.MTLS_CERT),
9+
key: pem(process.env.MTLS_KEY),
10+
rejectUnauthorized: true,
11+
}
12+
: undefined;

0 commit comments

Comments
 (0)