One of the most demanded challenges in the modern world of Internet of Things is to gain the highest level of security. That’s why today we are going to consider the process of establishing secure SSL connection to your PostgreSQL container, hosted at Jelastic Cloud. When striving to keep information in your PostgreSQL database safe, the first thing you need to do is to encrypt all connections to it for protecting authentication credentials (usernames/passwords) and processed data from interception.
Below, we’ll explore the appropriate database server adjustment and certificates generation, required for SSL enabling. Then, we’ll discover how to add certs to a client machine and, lastly, will establish a secure connection to our server via pgAdmin. So, let’s go on!
PostgreSQL Server Configuration
Within the instruction below, we’ll consider two use cases for two separate environments: with PostgreSQL database single node and clustered two nodes with master-slave replication.
In case of a single node installation, you can simply create it via environment topology wizard.
- For quick setup of a clustered solution, you can use the pre-configured PostgreSQL Database Replication package, available via the Marketplace > Clusters section.
To easily find the required solution, use the search bar at the frame top. Click on Install for the appropriate solution plank. Depending on the preferred topology, you’ll see a new environment of a single or two interconnected database nodes (like in the image above) appeared at your Jelastic dashboard.
1.To start with SSL connection setup, you have to access your database server through Jelastic SSH Gate - this can be accomplished in two ways.
- The easier one is to utilize an embedded Web SSH client and establish the connection directly through your Jelastic Dashboard (note that this option is available starting from Jelastic 5.4 platform version).
- Another way to enter the required container is to use your local SSH client. In case you haven’t performed the similar operation before, you’ll need to:
2. Once you’ve entered your PostgreSQL database server via SSH, you’ll need to add the following three files to its /var/lib/pgsql/data directory to make it work via SSL:
- server.key – private key
- server.crt – server certificate
- root.crt – trusted root certificate
If you already have a set of such certification files being pre-generated, you can just upload them to the above-mentioned folder (the appropriate option is available within the embedded Configuration Manager at Jelastic dashboard) and jump to the 6th step of the current instruction.
Otherwise, pay attention to the 3rd-5th steps below, where we briefly describe how to generate them on your own.
3. To create and configure the first above mentioned file (server.key), perform the following steps:
- execute the commands:
openssl genrsa -des3 -out server.key 1024 During the generation, you’ll be asked for a pass phrase – specify any and confirm it to finish the creation.
- in order to work with this key further, it’s required to remove the pass phrase you’ve added previously - execute the following command for this:
openssl rsa -in server.key -out server.key Re-enter pass phrase one more time for confirmation.
- finally, set the appropriate permission and ownership rights for your private key file with the next commands:
chmod 400 server.key
chown postgres.postgres server.key
4. Next, you need to create a server certificate based on your server.key file, e.g.:
openssl req -new -key server.key -days 3650 -out server.crt -x509 -subj '/C=US/ST=California/L=PaloAlto/O=Jelastic/CN=mysite.com/emailAddressfirstname.lastname@example.org'
5. Since we are going to sign certificates by ourselves, the generated server certificate can be also used as a trusted root certificate, so just make its copy with the appropriate name:
cp server.crt root.crt Now, as you have all three certificate files, you can proceed to PostgreSQL database configurations, required for actual SSL activation and usage.
6. In the same folder, open the pg_hba.conf file for editing (either with any preferable terminal editor - vim, for example - or directly through the dashboard).
Replace its default content with the following lines:
Save the updated file.
7. To finish configurations, you need to apply some more changes to the postgresql.conf file.
Navigate to its Security and Authentication section (approximately at the 80th line) and activate the SSL usage itself through uncommenting the same-named setting and changing its value to “on”. Also, append the new ssl_ca_file parameter below:
Don’t forget to save these changes.
8. Lastly, restart your PostgreSQL server in order to apply new settings.
sudo service postgresql restart
Now, let’s create one more set of SSL certificate files for client instance, in order to support secure connection at both sides.
1.Return to the terminal window with the established SSH connection to your PostgreSQL server you’ve operated through during server setup (or reconnect to it) – you’ll need your server certificates for further actions.
Once inside, generate a private key for a client (also without a pass phrase, just as it was done in the previous section), for example within the tmp directory:
openssl genrsa -des3 -out /tmp/postgresql.key 1024
openssl rsa -in /tmp/postgresql.key -out /tmp/postgresql.key 2. Next, create SSL certificate for your PostgreSQL database user (webadmin, by default) and sign it with our trusted root.crt file on a server.
openssl req -new -key /tmp/postgresql.key -out /tmp/postgresql.csr -subj '/C=US/ST=California/L=PaloAlto/O=Jelastic/CN=webadmin'
openssl x509 -req -in /tmp/postgresql.csr -CA root.crt -CAkey server.key -out /tmp/postgresql.crt -CAcreateserial
3. After these files (i.e. postgresql.key, postgresql.crt and root.crt) are ready, you need to move them to the .postgresql folder on your client machine. You can use the Jelastic dashboard UI to get them (just download them or copy/paste the files' content).
Also, if needed, you can set the key read permission for owner only with the chmod 400 ~/.postgresql/postgresql.key command to achieve more security.
Establish Connection via PgAdmin
Eventually, after server and client configurations are done, you are ready to establish the connection. In our case, we’ll use the pgAdmin 3 tool as an example, so get this application (or another preferred one) installed beforehand.
We’ll consider the latter case: access environment Settings, switch to the Endpoints section and Add new endpoint with the same-named button at the top pane. 2. Now, when you have an access point, run your pgAdmin 3 client and select the New Server Registration option. In the Properties tab of the opened window, specify the following data:
- Name – any desired connection name (e.g. ssl-to-pgsql)
- Host – access point you’ve added in the first step (Public IP address or endpoint Access URL without port number)
- Port – use the default 5432 port number for External IP or endpoint’s Public port (denoted in the same-named section of the appropriate column)
- Username – database user you’ve set the SSL certificate and configurations for (i.e. webadmin by default)
- Password – the corresponding user’s password (sent via email for webadmin or the one you’ve set otherwise)
The rest of the fields can be left unchanged or adjusted according to your requirements.
3. Next, switch to the SSL tab and, for the same-named line, select the require option from the drop-down list. That’s all! The required certificates will be loaded automatically during the first connection establishment (if not you can choose them manually), so just click OK to start managing your database via secure connection.
Now you can bind your application to the required database (use the Connect to Database guide for that) and enable SSL configurations for your project to encrypt your data while its fetching or transferring.
Go ahead and secure your project – just register for free at one of the globally-presented Jelastic Cloud Hosting Providers.