Webbanditten

Untitled

Dec 19th, 2018
379
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.10 KB | None | 0 0
  1. This is Google's cache of https://blog.ls42.de/post/postgres-ssl-with-caddy/. It is a snapshot of the page as it appeared on 14 Dec 2018 09:51:16 GMT. The current page could have changed in the meantime. Learn more.
  2. Full versionText-only versionView source
  3. Tip: To quickly find your search term on this page, press Ctrl+F or ⌘-F (Mac) and use the find bar.
  4. PostgreSQL with Let's Encrypt
  5. Home
  6. Posts
  7. About me
  8. Disclosure | Impressum
  9. PostgreSQL with Let's Encrypt February 12, 2018
  10.  
  11. Summary
  12. I decided to play around with PostgreSQL and created a setup where it is running inside Docker and Caddy is taking care of generating Let’s Encrypt certificates. I’ll show the necessary steps and post the configuration snippets here.
  13.  
  14. Lets assume PostgreSQL will be reachable via the FQDN db.example.com. I also assume that you have Caddy configured and running on your linux server.
  15.  
  16. Caddy Configuration
  17. Caddy is usually delivering websites. What it also does, is automatically generating SSL certificates via the ACME protocol. In Layman’s terms: It asks the Let’s Encrypt institution to give us a SSL certificate that is considered as secure by most current operation systems [citation needed].
  18.  
  19. But we can use Caddy for this feature alone: Just get the certificate. It still listens on the ports 80 and 443, but always delivers a 404 error if not further configured. In my current webserver setup this is particularly useful, because it is running already and I know how it works. I don’t need to have another tool like Certbot installed.
  20.  
  21. Given DNS is already configured, you have to tell Caddy to handle the ACME request. This is the corresponding /etc/caddy/Caddyfile:
  22.  
  23. [...]
  24. db.example.com
  25. [...]
  26. That’s it so far. Restart Caddy, I do it with sudo systemctl caddy restart. Caddy might take a while, but eventually will have the certificates generated and be listening on port 80 and 443 for the hostname db.example.com. When you access those, you’ll receive a 404 error. That’s fine, because were not interested in HTTP(S) traffic at all, we want to secure port 5432, the default port for the PostgreSQL daemon.
  27.  
  28. The default location for Caddy certificates is /etc/ssl/caddy. Given our hostname of db.example.com they relevant files can be located here:
  29.  
  30. /etc/ssl/caddy/acme/acme-v01.api.letsencrypt.org/sites/db.example.com/db.example.com.crt
  31. /etc/ssl/caddy/acme/acme-v01.api.letsencrypt.org/sites/db.example.com/db.example.com.key
  32. Configuration of Caddy has concluded.
  33.  
  34. Docker Configuration
  35. Let me start with a version of a docker-compose.yml that I use for the PostgreSQL container:
  36.  
  37. db:
  38. image: 'postgres:alpine'
  39. restart: always
  40. hostname: 'db.example.com'
  41. container_name: 'db'
  42. environment:
  43. POSTGRES_PASSWORD: "hey-ho-lets-go"
  44. ports:
  45. - 0.0.0.0:5432:5432
  46. volumes:
  47. - db:/var/lib/postgresql/data
  48. - /etc/ssl/caddy/acme/acme-v01.api.letsencrypt.org/sites/db.example.com:/var/lib/postgresql/data/ssl
  49. This tells docker to do the following (brief version):
  50.  
  51. Create a new container from the postgres:alpine image and name it db
  52. Create a volume named db and mount it inside the container at /var/lib/postgresql/data
  53. Mount a host directory inside the container. This is the location of our SSL files and it will be mounted inside the postgres data directory
  54. Save the file somewhere and run docker-compose up -d in that directory. You can see the new volume and container with the following commands:
  55.  
  56. # docker volume ls
  57. DRIVER VOLUME NAME
  58. local db
  59. # docker container ls
  60. NAMES IMAGE STATUS CONTAINER ID
  61. db postgres:alpine Up About an hour ca111ecef743
  62. PostgreSQL Configuration
  63. Enter the container and change a few settings.
  64.  
  65. # docker exec -ti -u postgres -w /var/lib/postgresql/data db /bin/sh
  66. $ echo "host all all 0.0.0.0/0 trust" >> pg_hba.conf
  67. $ echo "ssl = on" >> postgresql.conf
  68. $ echo "ssl_cert_file = 'ssl/db.example.com.crt'" >> postgresql.conf
  69. $ echo "ssl_key_file = 'ssl/db.example.com.key'" >> postgresql.conf
  70. Lines starting with $ are inside the container. We did the following:
  71.  
  72. Enter the container as the postgres user
  73. Tell PostgreSQL that connection from all interfaces for all users are okay. You should consider setting up a dedicated user for external access and change the second all in that line to that users name
  74. Tell PostgreSQL to enable SSL and where it can find the certificate and and the key
  75. After that leave the container again and restart it:
  76.  
  77. $ exit
  78. # docker restart db
  79. If all went well you should now be able to connect to your PostgreSQL database from any host, with a valid SSL certificate. I used Postico and it worked flawlessly.
  80.  
  81. Conclusion
  82. Using Docker and Caddy I’m able to run a secure PostgreSQL installation and connect from anywhere without worrying (too much) about security issues. I consider the times where I have to use snakeoil certificates over.
  83.  
  84. If you want to talk about this drop me an e-mail or leave a comment on lobste.rs.
  85.  
  86. Thanks!
  87.  
  88. © All rights reserved. Powered by Hugo and Minimal
Advertisement
Add Comment
Please, Sign In to add comment