Skip to content

Commit 41e5558

Browse files
[ADD] HBA_EXTRA_RULES support to allow custom pg_hba.conf rules
1 parent 11e7071 commit 41e5558

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ ENV CERTS="{}" \
1515
WAN_DATABASES='["all"]' \
1616
WAN_HBA_TPL="{connection} {db} {user} {cidr} {meth}" \
1717
WAN_TLS=1 \
18-
WAN_USERS='["all"]'
18+
WAN_USERS='["all"]' \
19+
HBA_EXTRA_RULES=""
1920
RUN apk add --no-cache python3 \
2021
&& mkdir -p /etc/postgres \
2122
&& chmod a=rwx /etc/postgres

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,17 @@ Wether to enable or not TLS in WAN connections.
105105

106106
Users allowed to connect from WAN.
107107

108+
#### `HBA_EXTRA_RULES`
109+
110+
JSON array of additional pg_hba.conf rules to append. Each array element should be a string representing a valid pg_hba.conf line.
111+
112+
Example HBA_EXTRA_RULES format in an .env file:
113+
114+
HBA_EXTRA_RULES=["host all all 192.168.1.0/24 md5", "hostssl mydb myuser 10.0.0.0/8 scram-sha-256"]
115+
116+
This adds the following lines to pg_hba.conf:
117+
118+
host all all 192.168.1.0/24 md5
119+
hostssl mydb myuser 10.0.0.0/8 scram-sha-256
120+
108121
[`Dockerfile`]: https://github.com/Tecnativa/docker-postgres-autoconf/blob/master/Dockerfile

autoconf-entrypoint

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ WAN_USERS = json.loads(os.environ["WAN_USERS"])
3333
PGSSLCERT = os.environ.get("PGSSLCERT")
3434
PGSSLKEY = os.environ.get("PGSSLKEY")
3535
PGSSLROOTCERT = os.environ.get("PGSSLROOTCERT")
36+
HBA_EXTRA_RULES = os.environ.get("HBA_EXTRA_RULES", "")
3637

3738
# Configuration file templates
3839
CONF_FOLDER = "/etc/postgres"
@@ -86,6 +87,17 @@ for filen in (PGSSLCERT, PGSSLKEY, PGSSLROOTCERT):
8687
if ssl_conf:
8788
ssl_conf.append("ssl = on")
8889

90+
# Parse extra rules for pg_hba.conf
91+
extra_hba_rules = []
92+
if HBA_EXTRA_RULES:
93+
try:
94+
extra_hba_rules = json.loads(HBA_EXTRA_RULES)
95+
if not isinstance(extra_hba_rules, list):
96+
raise ValueError("HBA_EXTRA_RULES must be a JSON array")
97+
except json.JSONDecodeError:
98+
print("Invalid JSON in HBA_EXTRA_RULES", file=sys.stderr)
99+
sys.exit(1)
100+
89101
# Generate LAN auth configuration
90102
for interface in netifaces.interfaces():
91103
for type_, addresses in netifaces.ifaddresses(interface).items():
@@ -123,6 +135,13 @@ if WAN_CONNECTION != "hostssl" or ssl_conf:
123135
)
124136
)
125137

138+
# Append extra rules to hba_conf
139+
for rule in extra_hba_rules:
140+
if not isinstance(rule, str):
141+
print("Each rule in HBA_EXTRA_RULES must be a string", file=sys.stderr)
142+
sys.exit(1)
143+
hba_conf.append(rule)
144+
126145
# Write postgres configuration files
127146
with open(CONF_FILE, "w") as conf_file:
128147
conf_file.write(

0 commit comments

Comments
 (0)