Skip to content

Commit

Permalink
Add PostgreSQL SSL
Browse files Browse the repository at this point in the history
  • Loading branch information
danthonywalker committed Jan 3, 2025
1 parent 6237832 commit 878a140
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
34 changes: 17 additions & 17 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
FROM node:20.8.0 as base
FROM node:20.8.0 AS base
WORKDIR /pedestrian
# Install Chrome dependencies for puppeteer
RUN apt-get update \
&& apt-get install -y wget gnupg \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
&& apt-get update \
&& apt-get install -y google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/* \
# Add puppeteer user for Chrome sandbox
&& groupadd -r pptruser \
&& useradd -r -g pptruser -G audio,video pptruser \
&& mkdir -p /home/pptruser/Downloads \
&& chown -R pptruser:pptruser /home/pptruser \
&& chown -R pptruser:pptruser /pedestrian
&& apt-get install -y wget gnupg \
&& wget -q -O - https://dl-ssl.google.com/linux/linux_signing_key.pub | apt-key add - \
&& sh -c 'echo "deb [arch=amd64] http://dl.google.com/linux/chrome/deb/ stable main" >> /etc/apt/sources.list.d/google.list' \
&& apt-get update \
&& apt-get install -y google-chrome-stable fonts-ipafont-gothic fonts-wqy-zenhei fonts-thai-tlwg fonts-kacst fonts-freefont-ttf libxss1 \
--no-install-recommends \
&& rm -rf /var/lib/apt/lists/* \
# Add puppeteer user for Chrome sandbox
&& groupadd -r pptruser \
&& useradd -r -g pptruser -G audio,video pptruser \
&& mkdir -p /home/pptruser/Downloads \
&& chown -R pptruser:pptruser /home/pptruser \
&& chown -R pptruser:pptruser /pedestrian
# Use puppeteer user for Chrome sandbox
USER pptruser
COPY package*.json ./

FROM base as build
FROM base AS build
# Install runtime and build dependencies
RUN npm ci
# Copy source code into current image
COPY . .
# Test source code
RUN npm test \
# Build source code
&& npm run build
# Build source code
&& npm run build

FROM base
# Install runtime dependencies
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ Required environment variables must be configured in a `.env` file at the projec
| POSTGRESQL_DATABASE || db | |
| POSTGRESQL_USER || user | |
| POSTGRESQL_PASSWORD || password | |
| POSTGRESQL_SSL || false | |
| POSTGRESQL_SSL_CA || ./ca.crt | |
| PROJECT_NAME || Pedestrian | |
| REDIS_HOST || redis | |
| REDIS_PORT || 6379 | |
Expand Down
2 changes: 2 additions & 0 deletions src/shared/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export default {
PostgresqlHost: env.POSTGRESQL_HOST ?? "postgres",
PostgresqlPassword: env.POSTGRESQL_PASSWORD ?? "password",
PostgresqlPort: Number(env.POSTGRESQL_PORT ?? 5432),
PostgresqlSsl: env.POSTGRESQL_SSL === true.toString(),
PostgresqlSslCa: env.POSTGRESQL_SSL_CA ?? "./ca.crt",
PostgresqlUser: env.POSTGRESQL_USER ?? "user",
ProjectName: env.PROJECT_NAME ?? "Pedestrian",
RedisCluster: env.REDIS_CLUSTER === true.toString(),
Expand Down
10 changes: 10 additions & 0 deletions src/shared/postgresql.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { PoolClient } from "pg";

import * as fs from "node:fs";
import { Pool } from "pg";
import { Histogram } from "prom-client";

Expand All @@ -22,11 +23,20 @@ const databaseRequestDuration = new Histogram({
});
// endregion

let postgresqlSsl;
if (Environment.PostgresqlSsl) {
const caFile = fs.readFileSync(Environment.PostgresqlSslCa);
postgresqlSsl = {
ca: caFile.toString(),
};
}

const postgresql = new Pool({
database: Environment.PostgresqlDatabase,
host: Environment.PostgresqlHost,
password: Environment.PostgresqlPassword,
port: Environment.PostgresqlPort,
ssl: postgresqlSsl,
user: Environment.PostgresqlUser,
});

Expand Down

0 comments on commit 878a140

Please sign in to comment.