Your organization may require you to connect to databases using secure SSL connections so all traffic communicating with the database is encrypted. In this post, we provide guidance on how to connect to an Amazon Aurora PostgreSQL database from a C++ application using the libpq library. We show you how to enforce SSL connections to your Aurora PostgreSQL database and connect to this from a C++ application using a secured SSL connection.
You can also apply the same principles to an Amazon RDS for PostgreSQL database.
You need the following prerequisites:
- An AWS account.
- An Aurora PostgreSQL database with SSL enforced.
- Visual Studio
Enforcing an SSL Connection to an Aurora PostgreSQL database
For this post, we use Aurora PostgreSQL 11.7. We must first enforce SSL connections to our Aurora PostgreSQL database. We need to enable the rds.force_ssl parameter through the parameter groups so we can enforce SSL connections from the server side. If we don’t specify this, the clients may be able to connect to the database server without SSL. The following table summarizes the different SSL modes. For more information, see libpq SSL Support.
|sslmode||Eavesdropping protection||MITM protection||Statement|
|disable||No||No||I don’t care about security, and I don’t want to pay the overhead of encryption.|
|allow||Maybe||No||I don’t care about security, but I will pay the overhead of encryption if the server insists on it.|
|prefer||Maybe||No||I don’t care about encryption, but I wish to pay the overhead of encryption if the server supports it. This is default.|
|require||Yes||No||I want my data to be encrypted, and I accept the overhead. I trust that the network will make sure I always connect to the server I want.|
|verify-ca||Yes||Depends on CA-policy||I want my data encrypted, and I accept the overhead. I want to be sure that I connect to a server that I trust.|
|verify-full||Yes||Yes||I want my data encrypted, and I accept the overhead. I want to be sure that I connect to a server I trust, and that it’s the one I specify.|
To enforce an SSL connection on your database, complete the following steps:
- On the Amazon RDS console, choose Parameter groups.
- Choose Create parameter group.
- On the Create parameter group page, for Parameter group family, choose the version of Aurora PostgreSQL what you’re using.
- For Type select DB Cluster Parameter Group.
- For Group name, enter a name for your group.
- For Description, enter a description
- Choose Create.
- On the Parameter groups page, search for your new group and select it.
- For Parameters, search for ‘force_ssl’ and select it.
- Choose Edit Parameters.
- Select the force_ssl parameter and change the Value to 1.
- Choose Save changes.
- On the Databases page, select the database for which you want to enforce the SSL connections.
- Choose Modify.
- In the Database options section, choose the DB cluster parameter group you just created.
- Choose Continue.
- On the next page, under Scheduling of modifications, select when to apply the changes (for this post, we select Apply Immediately.
- Choose Modify cluster.
If you’re using Aurora PostgreSQL version 11 or above and already using non-default parameter group, you don’t need to stop and start the database cluster in the following steps.
- To stop the database, select it and choose Stop from the Actions drop-down menu.
- Choose Stop database when prompted.
- When the database has stopped, start the database again by choosing Start from the Actions drop-down menu.
Download the Root Certificate
Download the root certificate that works for all AWS Regions, excluding opt-in Regions. Save this root certificate in the directory of your choice. You reference this directory from your code in the next section.
Connecting to the Aurora PostgreSQL database
To connect the database from a C++ application, complete the following steps:
- Open the Visual Studio IDE and create a new C++ console application.
- After you create the application, change the Debug type for the project to x64.
We need to setup the project so it build’s successfully.
- From the Project drop-down menu, choose Properties.
- When the Properties dialog box opens for the project, under C/C++, choose General.
- For Additional Include Directories, choose Edit.
- In the Additional Include Directories dialog box, choose the New line insert
- Navigate to the include directory in the PostgreSQL install folder (similar to C:Program FilesPostgreSQL<version number>include).Your version number might be different, but the library is backwards compatible.
- Choose Select Folder.
The include directory should now be added to the list.
- Choose OK.
- In the Project Properties dialog box choose Apply.
- On the Properties page, under Configuration Properties, choose VC++ Directories.
- For Library Directories choose Edit.
- In the Libraries Directories dialog box, choose the New line insert
- Choose the lib directory (C:Program FilesPostgreSQL<version number>lib).
- Choose Select Folder.You can see the lib folder added to the Library Directories.
- Choose OK.
- In the Properties dialog box, choose
- Choose OK.
- Choose Local Windows Debugger to compile the program as is and then stop the program.
- Locate the bin directory for PostgreSQL (C:Program FilesPostgreSQL<version number>bin).
- Copy the following highlighted files from bin to the debug folder of your application
- Replace the C++ code in your console application with the following code (replace the placeholder text with your database connection information).
The connection string requires you to provide the location of the root certificate.
- Download the rds-ca-2019-root.pem certificate from Using SSL/TLS to Encrypt a Connection to a DB Cluster and store it in a folder.
- Provide a path to that folder for the
sslrootcertparameter in the connection string below.
rds-ca-2019-root.pemexpires in August 2024. You must update the certificate when it expires.
In the preceding code, we assign the parameter sslmode a value of
verify-full. As per libpq SSL Support, “If sslmode is set to verify-full, libpq will also verify that the server host name matches its certificate. The SSL connection will fail if the server certificate cannot be verified. verify-full is recommended in most security-sensitive environments”.
- Run the program by choosing Local Windows Debugger as shown below.
If you configured everything correctly you should be able to connect to the database with SSL. See the following screenshot.
In this post we provided information on how to enforce SSL connection to an Aurora PostgreSQL database (the same concept applies to Amazon RDS for PostgreSQL). We then downloaded the root certificate from Amazon. With the preceding C++ code, you can connect to Aurora PostgreSQL or Amazon RDS for PostgreSQL and enforce SSL connections to your PostgreSQL database to meet your organization’s security requirements of encrypted connections.
About the Author
Zafar Kapadia is a Sr. Cloud Application Architect at Amazon Web Services. He specializes in Application Development and Optimization. He is also an avid cricketer and plays in various local leagues.
Li Liu is a Database Cloud Architect with Amazon Web Services.