Managing an SSL-enabled PostgreSQL server using Ansible
[RPi
PostgreSQL
security
IaaC
programming
Ansible
]
In fall 2023, I’ve bought a set of four Raspberry Pi 4 and made a little rack out of them: A set of four Raspberry Pi 4 Model B (4 GB RAM). I’m using an Utronics RPi rack and PoE so that I use the least amount of cables as possible. The two “boxes” on top of the rack are a master Raspberry Pi (on the left) which serves as a PXE server and a PoE enabled network switch (on the right).
Recently, I’ve been immersed in playing around with geometry data, more specifically playing around with PostGIS. I’ve been currently reading through an excellent publication on PostGIS1. Together with the book, there many helper scripts which create little playground databases and pump data into them. Since my work laptop is the same machine as personal laptop, I really didn’t want to pollute my local PostgreSQL instance with such data.
I’ve also wanted to try out a certificate-based authentication which was mentioned by Stephen Frost on his PostgreSQL conference in 20232. Since I already knew a little bit about Ansible as I had been setting up my RPi cluster using Ansible, I’ve decided to create a separate playbook which would automatically set up a SSL-enabled PostGIS server for me. Side note: since I’m booting my Raspberries in my rack using PXE, I also needed to configure the data directory which are stored on a separate SSD, managed and provided by the “master” Raspberry.
In fact, the only way you can connect to this PostgreSQL database server is via a SSL certificate, provided you’re not logged in to the server directly via SSH -which in my infrastructure is also possible only using certificates. Since I don’t want to use neither DNS domain nor CA-signed certificate, I generated self-signed certificates for the purposes of my PoC.
First, we will setup the PostgreSQL server. Here is what my server.yml
playbook looks like:
A few important notes here. The way we tell PostgreSQL how it should handle authentication is using its feature called host-based authentication, namely the pg_hba.conf
file located directly at the PostgreSQL server. The pg_hba.conf.j2
I’m using is a fork of the “vanilla” pg_hba.conf
and has the following contents:
First two of the three authentication methods are what PostgreSQL calls trust
and what this means in practice is that the user is automatically authorized without any authentication. In general, this is not a recommended method for obvious security reasons. The part local all <user>
tell PostgreSQL to authorize the user <user>
to any database provided he is logged in directly on the PostgreSQL server (that’s the local
part).
The last authentication enables anyone outside the PostgreSQL server authenticate to any database. Again, this rule would be much more strict in any real-world scenario.
-
R. O. Obe and L. S. Hsu, PostGIS in action. Manning, Cop, 2015. Available at: https://www.manning.com/books/postgis-in-action-third-edition ↩
-
Stephen Frost: Advanced Authentication and Encrypted Connections. PGConf.EU 2023. https://www.youtube.com/watch?v=dWO_uA1-Oxo ↩