Keri sisuni

Renewing PostgreSQL cluster certificates

Preparations

Certificate renewal

Before performing any actions on the PostgreSQL cluster nodes, please prepare new certificates for each of the cluster nodes. As a reminder, certificates must comply with several constraints specified in the PostgreSQL cluster prerequisites.

If you want to make the change on the fly without impacting production, you must meet the following conditions:

  • The certification authority has not been renewed since the last generation of PostgreSQL node certificates
  • The certification authority will not expire during the validity period of the new certificates
  • You can still generate and sign certificates with the same certification authority that was used when the previous PostgreSQL node certificates were generated
  • The PostgreSQL node certificates have not yet expired

If these conditions are met, then you can consider renewing the certificates on the fly without interrupting production.

To regenerate certificates using the openssl PKI indicated, optionally, in the PostgreSQL cluster installation documentation, you must reuse the certification authority key and then replay the certificate generation commands. These commands overwrite the files from the previous generation: back them up first, for example with the following commands:

1
2
mkdir /root/psql-certificates/old/
cp /root/psql-certificates/*.* /root/psql-certificates/old/

If it is not possible to renew the certificates on the fly and you are using a PKI via openssl, we recommend that you create a new one.

Preparing the certificates

If the certification authority has changed, you will need to send in /tmp/ the root certification authority certificate with a .crt extension to each of the PostgreSQL nodes.

You will also need to send in /tmp/ the certificate and its associated private key to each of the PostgreSQL nodes. If you only have one .p12 file available, please run the following commands to extract the certificate (psql.crt) and private key (psql.key):

Value Variable Definition
P12_CERT Name of the .p12 certificate file of the node.
1
2
openssl pkcs12 -clcerts -nokeys -out psql.crt -in P12_CERT
openssl pkcs12 -nocerts -noenc -out psql.key -in P12_CERT

The -noenc option writes the key without encryption, as etcd and patroni use it.

Certificate renewal

Warning!

If you can and wish to perform a live update of the certificates with minimal production downtime (a few seconds and user logout), work on one node at a time, always keeping at least two PostgreSQL nodes operational, in the following order:

  1. Renew the certificates of the two REPLICA nodes, one after the other (see how to retrieve information from the LEADER node).
  2. Switch the LEADER to one of the nodes whose certificates have already been renewed: this is the action that logs users out and makes the platform unavailable for a few seconds.
  3. Renew the certificates of the former LEADER, now a REPLICA.

Restarting a REPLICA node does not interrupt the service; a restarted node takes a few minutes to rejoin the cluster.

To simplify the following actions, please customise the variables in the commands below with your values (modify the customisation for each PostgreSQL node):

Value Variable Definition
PSQL_CERT New PostgreSQL node certificate.
PSQL_KEY Key associated with the new PostgreSQL node certificate.
PSQL_CA Certificate from the root certification authority that was used to generate the PostgreSQL node certificate.

Connect via SSH with the systancia account to the PostgreSQL node whose certificates you want to update, switch to the root account with the su - command (the root account is not accepted directly over SSH), and run the following commands to replace the certificates:

1
2
3
4
5
6
cp /tmp/PSQL_CERT /etc/etcd/peer.crt
cp /tmp/PSQL_CERT /etc/etcd/client.crt
cp /tmp/PSQL_CERT /etc/patroni/client.crt
cp /tmp/PSQL_KEY /etc/etcd/peer.key
cp /tmp/PSQL_KEY /etc/etcd/client.key
cp /tmp/PSQL_KEY /etc/patroni/client.key

If the certification authority has changed, also run the following commands:

1
2
3
4
cp /tmp/PSQL_CA /etc/etcd/ca.crt
cp /tmp/PSQL_CA /etc/patroni/ca.crt
cp /tmp/PSQL_CA /usr/local/share/ca-certificates/PSQL-CA-`date +%Y-%m-%d`.crt
update-ca-certificates

To finalise the certificate changes, you must restart the PostgreSQL node:

1
reboot

Before moving on to the next node, wait until the node is back in the cluster with the streaming state (or running for the LEADER):

1
patronictl -c /etc/patroni/config.yml list

You can then check the certificates of the node, in particular the certificate presented by each service.

Once the node has restarted, delete the files placed in /tmp/, since the private key is stored there unencrypted (as well as the .p12 file, if you placed one there):

1
2
rm /tmp/PSQL_CERT /tmp/PSQL_KEY
rm -f /tmp/PSQL_CA