Skip to content

Commit 6db61e4

Browse files
Merge pull request #22 from Tecnativa/add-hba-extra-rules
[ADD] HBA_EXTRA_RULES support to allow custom pg_hba.conf rules
2 parents 11e7071 + eba6aff commit 6db61e4

File tree

5 files changed

+94
-12
lines changed

5 files changed

+94
-12
lines changed

.github/workflows/ci.yaml

+18-11
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,8 @@ jobs:
1616
- uses: actions/checkout@v2
1717
- uses: actions/setup-python@v1
1818
- name: Set PY
19-
run:
20-
echo "PY=$(python -c 'import hashlib,
21-
sys;print(hashlib.sha256(sys.version.encode()+sys.executable.encode()).hexdigest())')"
22-
>> $GITHUB_ENV
19+
run: |
20+
echo "PY=$(python -c 'import hashlib,sys;print(hashlib.sha256(sys.version.encode()+sys.executable.encode()).hexdigest())')" >> $GITHUB_ENV
2321
- uses: actions/cache@v1
2422
with:
2523
path: ~/.cache/pre-commit
@@ -32,7 +30,6 @@ jobs:
3230
strategy:
3331
fail-fast: false
3432
matrix:
35-
# Test modern Odoo versions with latest Postgres version
3633
pg_version:
3734
- "16"
3835
- "15"
@@ -45,29 +42,39 @@ jobs:
4542
env:
4643
# Indicates what's the equivalent to tecnativa/postgres-autoconf:latest image
4744
LATEST_RELEASE: "16-alpine"
48-
# Variables found by default in Docker Hub builder
4945
DOCKER_REPO: tecnativa/postgres-autoconf
5046
DOCKER_TAG: ${{ matrix.pg_version }}-alpine
5147
GIT_SHA1: ${{ github.sha }}
48+
IS_PR: ${{ github.event_name == 'pull_request' }}
5249
steps:
53-
# Prepare
5450
- uses: actions/checkout@v2
5551
- uses: actions/setup-python@v1
5652
- run: pip install -r tests/ci-requirements.txt
53+
5754
# Build images
5855
- run: ./hooks/build
5956
# Test
6057
- run: python -m unittest tests.test -v
58+
- name: Set Docker Tag
59+
run: |
60+
if [ "${{ env.IS_PR }}" = "true" ]; then
61+
echo "DOCKER_TAG=${{ matrix.pg_version }}-test-pr${{ github.event.number }}" >> $GITHUB_ENV
62+
else
63+
echo "DOCKER_TAG=${{ matrix.pg_version }}-alpine" >> $GITHUB_ENV
64+
fi
65+
- name: Tag Docker Image for PR
66+
if: env.IS_PR
67+
run: docker tag ${{ env.DOCKER_REPO }}:${{ matrix.pg_version }}-alpine ${{ env.DOCKER_REPO }}:${{ env.DOCKER_TAG }}
6168
# Push
62-
- name: push to docker hub
63-
if: github.repository == 'Tecnativa/docker-postgres-autoconf' && github.ref == 'refs/heads/master'
69+
- name: Push Docker Image to Docker Hub
70+
if: github.repository == 'Tecnativa/docker-postgres-autoconf' && (env.IS_PR || github.ref == 'refs/heads/master')
6471
env:
6572
REGISTRY_HOST: docker.io
6673
REGISTRY_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
6774
REGISTRY_USERNAME: ${{ secrets.DOCKERHUB_LOGIN }}
6875
run: ./hooks/push
69-
- name: push to github registry
70-
if: github.repository == 'Tecnativa/docker-postgres-autoconf' && github.ref == 'refs/heads/master'
76+
- name: Push Docker Image to GitHub Registry
77+
if: github.repository == 'Tecnativa/docker-postgres-autoconf' && (env.IS_PR || github.ref == 'refs/heads/master')
7178
env:
7279
REGISTRY_HOST: ghcr.io
7380
REGISTRY_TOKEN: ${{ secrets.BOT_TOKEN }}

Dockerfile

+2-1
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

+13
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

+19
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(

tests/test.py

+42
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,48 @@ def test_certs_falsy_lan(self):
299299
with self.assertRaises(ProcessExecutionError):
300300
self._check_password_auth("example.localdomain")
301301

302+
def test_hba_extra_rules_added(self):
303+
"""Test that HBA_EXTRA_RULES lines are added to pg_hba.conf."""
304+
if "9.6" in self.image:
305+
self.skipTest("HBA_EXTRA_RULES not supported in PostgreSQL 9.6")
306+
# Define custom HBA rules
307+
hba_extra_rules = [
308+
"host test_db custom_user 0.0.0.0/0 trust",
309+
"hostssl all all 192.168.0.0/16 md5",
310+
]
311+
312+
# Start the Postgres container with HBA_EXTRA_RULES
313+
self.postgres_container = docker(
314+
"run",
315+
"-d",
316+
"--name",
317+
"postgres_test_hba_extra_rules",
318+
"--network",
319+
"lan",
320+
"-e",
321+
"POSTGRES_DB=test_db",
322+
"-e",
323+
"POSTGRES_USER=test_user",
324+
"-e",
325+
"POSTGRES_PASSWORD=test_password",
326+
"-e",
327+
"HBA_EXTRA_RULES=" + json.dumps(hba_extra_rules),
328+
CONF_EXTRA,
329+
self.image,
330+
).strip()
331+
332+
# Give the container some time to initialize
333+
time.sleep(10)
334+
335+
# Read the pg_hba.conf file content from the container
336+
hba_conf = docker(
337+
"exec", self.postgres_container, "cat", "/etc/postgres/pg_hba.conf"
338+
).strip()
339+
340+
# Check that each rule in hba_extra_rules is present in the file
341+
for rule in hba_extra_rules:
342+
self.assertIn(rule, hba_conf)
343+
302344

303345
if __name__ == "__main__":
304346
unittest.main()

0 commit comments

Comments
 (0)