Git Product home page Git Product logo

ssl-pasword-postgres's Introduction

Secure certificate and password only Postgres in Docker

Prerequisites

You'll need to have Docker installed and be minimally familiar with how to use it. This is one of the main departures from the guide above which doesn't properly enforce certificates from the server side as far as I can tell.

You'll need to set up certificates. To do this use certstrap for an easier time. This can be installed with Brew on a Mac.

Run the following from within the certs directory to generate your Certificate Authority (CA):

  • certstrap init --common-name myCA

This will generate the following:

  • myCA.crt - the CA certificate
  • myCA.key - the CA certificate key that is used to sign certificate requests
  • myCA.crl - the Certificate Revocation List (a list of revoked certificates)

Server Certificates

Next, request some certificate key pairs from the CA:

certstrap request-cert --common-name postgresdb
certstrap sign postgresdb --CA myCA

These certificates will be the server side certs. See postgresql.conf for how they're used and set up.

Client Certificates

pg_hba.conf forces all clients to connect with both a certificate and password. Do the following to generate a certificate for the postgres user (basically a repeat of the above):

certstrap request-cert --common-name postgres
certstrap sign postgres --CA myCA

Change the permissions on the certificte as required: chmod 0600 <path to certificate>/postgres.key

Note that the cn (Common Name) of the client certificate must be the same as the username. See https://www.postgresql.org/docs/current/auth-cert.html.

Building / starting / connecting to the container

From scratch, you should be able to just docker-compose up the first time.

To rebuild everything (rather than just restart:

docker-compose build --no-cache

Connect to the running database using:

psql "host=localhost port=5433 user=postgres password=postgres sslcert=<path to>/postgres.crt sslkey=<path to>/postgres.key"

To check you have an encrypted connection, you can do the following:

  1. When you initiate the connection with psql you will likely see something like
SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off)
  1. Check pg_stat_ssl:
select * from pg_stat_ssl;

which will confirm that the connection is encrpyted:

id | ssl | version |         cipher         | bits |  client_dn   |              client_serial              | issuer_dn
-----+-----+---------+------------------------+------+--------------+-----------------------------------------+-----------
 115 | t   | TLSv1.3 | TLS_AES_256_GCM_SHA384 |  256 | /CN=postgres | 169828633045461505693128272242085742747 | /CN=myCA

Credits

This was one of a few helpful starting points for the above:

https://dev.to/danvixent/how-to-setup-postgresql-with-ssl-inside-a-docker-container-5f3

ssl-pasword-postgres's People

Contributors

arlobryer avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.