Skip to content

Commit ec36b68

Browse files
committed
feat: enable ssl postgres
Signed-off-by: Anju Pathak <anjupathak9810@gmail.com>
1 parent a8453d3 commit ec36b68

3 files changed

Lines changed: 56 additions & 1 deletion

File tree

django-postgres/django_postgres/django_postgres/settings.py

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"""
1212

1313
from pathlib import Path
14+
import os
1415

1516
# Build paths inside the project like this: BASE_DIR / 'subdir'.
1617
BASE_DIR = Path(__file__).resolve().parent.parent
@@ -27,6 +28,7 @@
2728

2829
ALLOWED_HOSTS = ["*"]
2930

31+
SSL_ENABLED = os.getenv('POSTGRES_SSL_ENABLED', 'false').lower() == 'true'
3032

3133
# Application definition
3234

@@ -71,6 +73,30 @@
7173

7274
WSGI_APPLICATION = 'django_postgres.wsgi.application'
7375

76+
def _db_options_from_env():
77+
"""
78+
Build psycopg2 OPTIONS dict, adding SSL keys only if requested.
79+
"""
80+
opts = {}
81+
sslmode = os.getenv("DB_SSLMODE", "disable").strip()
82+
if sslmode and sslmode.lower() != "disable":
83+
# Common values: require | verify-ca | verify-full
84+
opts["sslmode"] = sslmode
85+
# Optional files if you want verification / mTLS
86+
rootcert = os.getenv("DB_SSLROOTCERT", "").strip()
87+
sslcert = os.getenv("DB_SSLCERT", "").strip()
88+
sslkey = os.getenv("DB_SSLKEY", "").strip()
89+
sslcrl = os.getenv("DB_SSLCRL", "").strip()
90+
if rootcert:
91+
opts["sslrootcert"] = rootcert
92+
if sslcert:
93+
opts["sslcert"] = sslcert
94+
if sslkey:
95+
opts["sslkey"] = sslkey
96+
if sslcrl:
97+
opts["sslcrl"] = sslcrl
98+
return opts
99+
74100

75101
# Database
76102
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
@@ -83,10 +109,10 @@
83109
'PASSWORD': 'postgres',
84110
'HOST': '0.0.0.0',
85111
'PORT': '5432',
112+
'OPTIONS': _db_options_from_env(),
86113
}
87114
}
88115

89-
90116
# Password validation
91117
# https://docs.djangoproject.com/en/4.2/ref/settings/#auth-password-validators
92118

django-postgres/django_postgres/docker-compose.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,17 @@ services:
1212
volumes:
1313
- ./sql/init.sql:/docker-entrypoint-initdb.d/init.sql
1414

15+
postgres_ssl:
16+
image: postgres:latest
17+
environment:
18+
POSTGRES_DB: usersdb
19+
POSTGRES_USER: postgres
20+
POSTGRES_PASSWORD: postgres
21+
ports:
22+
- "5432:5432"
23+
volumes:
24+
- pgdata_ssl:/var/lib/postgresql/data
25+
- ./sql/enable-ssl.sh:/docker-entrypoint-initdb.d/enable-ssl.sh:ro
26+
27+
volumes:
28+
pgdata_ssl:
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
# Generate a self-signed server cert the first time the cluster initializes
5+
if [[ ! -f "$PGDATA/server.crt" ]]; then
6+
openssl req -new -x509 -days 365 -nodes -text \
7+
-subj "/CN=postgres_ssl" \
8+
-keyout "$PGDATA/server.key" \
9+
-out "$PGDATA/server.crt"
10+
chmod 600 "$PGDATA/server.key" "$PGDATA/server.crt"
11+
chown postgres:postgres "$PGDATA/server.key" "$PGDATA/server.crt"
12+
echo "ssl = on" >> "$PGDATA/postgresql.conf"
13+
echo "ssl_cert_file = 'server.crt'" >> "$PGDATA/postgresql.conf"
14+
echo "ssl_key_file = 'server.key'" >> "$PGDATA/postgresql.conf"
15+
fi

0 commit comments

Comments
 (0)