pgsql/ssl
author Tomas Zeman <tzeman@volny.cz>
Tue, 12 Nov 2013 16:06:45 +0100
changeset 35 1c1f40be923d
parent 18 24a81ad5593d
permissions -rw-r--r--
freebsd/ssh-vpn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     1
http://www.howtoforge.com/postgresql-ssl-certificates
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     2
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     3
On the server, three certificates are required in the data directory. CentOS default is /var/lib/pgsql/data/:
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     4
root.crt (trusted root certificate)
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     5
server.crt (server certificate)
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     6
server.key (private key)
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     7
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     8
Issue commands as root.
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
     9
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    10
sudo -
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    11
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    12
cd /var/lib/pgsql/data
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    13
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    14
Generate a private key (you must provide a passphrase).
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    15
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    16
openssl genrsa -des3 -out server.key 1024
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    17
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    18
Remove the passphrase.
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    19
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    20
openssl rsa -in server.key -out server.key
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    21
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    22
Set appropriate permission and owner on the private key file.
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    23
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    24
chmod 400 server.key
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    25
chown postgres.postgres server.key
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    26
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    27
Create the server certificate.
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    28
-subj is a shortcut to avoid prompting for the info.
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    29
-x509 produces a self signed certificate rather than a certificate request.
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    30
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    31
openssl req -new -key server.key -days 3650 -out server.crt -x509 -subj '/C=CA/ST=British Columbia/L=Comox/O=TheBrain.ca/CN=thebrain.ca/emailAddress=info@thebrain.ca'
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    32
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    33
Since we are self-signing, we use the server certificate as the trusted root certificate.
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    34
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    35
cp server.crt root.crt
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    36
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    37
You need to edit postgresql.conf to actually activate ssl:
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    38
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    39
ssl = on
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    40
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    41
On the client, we need three files. For Windows, these files must be in %appdata%\postgresql\ directory. For Linux ~/.postgresql/ directory.
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    42
root.crt (trusted root certificate)
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    43
postgresql.crt (client certificate)
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    44
postgresql.key (private key)
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    45
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    46
Generate the the needed files on the server machine, and then copy them to the client. We'll generate the needed files in the /tmp/ directory.
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    47
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    48
First create the private key postgresql.key for the client machine, and remove the passphrase.
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    49
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    50
openssl genrsa -des3 -out /tmp/postgresql.key 1024
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    51
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    52
openssl rsa -in /tmp/postgresql.key -out /tmp/postgresql.key
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    53
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    54
Then create the certificate postgresql.crt. It must be signed by our trusted root (which is using the private key file on the server machine). Also, the certificate common name (CN) must be set to the database user name we'll connect as.
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    55
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    56
openssl req -new -key /tmp/postgresql.key -out /tmp/postgresql.csr -subj '/C=CA/ST=British Columbia/L=Comox/O=TheBrain.ca/CN=www-data'
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    57
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    58
openssl x509 -req -in /tmp/postgresql.csr -CA root.crt -CAkey server.key -out /tmp/postgresql.crt -CAcreateserial
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    59
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    60
Copy the three files we created from the server /tmp/ directory to the client machine.
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    61
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    62
Copy the trusted root certificate root.crt from the server machine to the client machine (for Windows pgadmin %appdata%\postgresql\ or for Linux pgadmin ~/.postgresql/). Change the file permission of postgresql.key to restrict access to just you (probably not needed on Windows as the restricted access is already inherited). Remove the files from the server /tmp/ directory.
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    63
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    64
You must add "clientcert=1" to hostssl options for checking the client certificates, otherwise everyone will be granted access in your setup:
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    65
hostssl all postgres 0.0.0.0/0 trust clientcert=1
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    66
24a81ad5593d Postgres ssl howto
Tomas Zeman <tzeman@volny.cz>
parents:
diff changeset
    67