How to encrypt a PostgreSQL database at rest and in transit

PostgreSQL is a robust open source object-relational database system with many capabilities that securely store and process even the most complex data requests.

Due to its well-established design, reliability, data integrity, extensive feature set, extensibility, and the commitment of the open source community to continuously provide superior solutions, PostgreSQL has earned a solid reputation.

Not surprisingly, PostgreSQL has emerged as the preferred open source relational database among several individuals and organizations.

In this blog we will discuss how we can encrypt a PostgreSQL database at rest and in transit.

Data encryption

Encryption is the process of changing data in such a way that it is unreadable to anyone except those with special knowledge (using a “key”) that allows them to modify the information back to its original, readable form.

Data encryption helps prevent unauthorized persons from reading information.

alt

Data at rest

When data is not actively traveling from device to device or network to network, such as data stored on a hard drive, laptop, flash drive, or otherwise archived/stored, it is known as data at rest.

It therefore aims to secure inactive information stored on any computer or network.

For example, your PostgreSQL data_directory, MySQL/MariaDB data_dir, or MongoDB’s dbPath storage locations.

Data in transmission

Data in motion or in transit is when it is actively traveling from one place to another, such as over the Internet or a private network.

Its protection in transit is its security as it moves from network to network or when moved from a local storage system to a cloud storage device.

Data transmitted over an insecure connection can be intercepted by an attacker by eavesdropping. This can be prevented by using TLS/SSL encryption to encrypt the data transmitted over the wire between the database client and the database server.

To learn more about encryption, you can check out the blog.

Database encryption

Database encryption offers enhanced security for your data in transit and at rest. Recent security breaches have led many organizations to take encryption seriously.

Because they often contain the most important assets for most organizations, database servers are often the target of attackers.

An attacker is likely to exploit the data on your server after gaining access to sensitive information.

Why do we need to encrypt the database?

Encrypting your data is highly recommended, especially for companies in the financial, healthcare or e-commerce industries.

People are very aware of data security and privacy and want their data to be secure and used only when necessary.

Database encryption has the following excellent benefits:

  • Although security breaches and data breaches are inevitable. However, with improved security and encryption techniques, hackers can be prevented from decrypting or analyzing data to gain additional insight.
  • One of the most important criteria when it comes to security laws like PCI-DSS is encryption. This is an essential prerequisite.
  • With centralized key management and simple encryption APIs, encryption key management is perfect for protecting sensitive data.

PostgreSQL database encryption methods

PostgreSQL supports multiple levels of encryption and flexibility in protecting data from exposure to network security breaches, rogue administrators, and database server losses.

You may also need to encrypt sensitive data, such as medical information or financial transactions.

Password encryption

An administrator cannot verify the real password assigned to a database user since user passwords are stored as hash values. Passwords for PostgreSQL databases are different from those for operating system users.

Each database user password is stored in the pg_authid system directory. Password authentication will always fail for a user with no password set since the password saved for that user is null.

Avg

  • Passwords are encrypted so that someone with unauthorized access to the database cannot see them in plain text.
  • Password encryption helps comply with security laws and regulations such as GDPR, HIPAA and others.

Against

  • Password encryption may have a small negative impact on authentication speed.
  • Especially when it comes to password resets and user administration, maintaining encrypted passwords can be more difficult than maintaining plain text passwords.

How to achieve

There are numerous password-based authentication techniques. Both of these approaches work similarly, but differ in how the user’s credentials are stored on the server and how the client’s password is transmitted over the network.

  • scram-sha-256
  • md5
  • password

Encrypt data over the network

All data transmitted over the network, including passwords, queries and returned data, is encrypted via an SSL connection.

Administrators can choose which hosts require SSL-encrypted connections (hostssl) and which can use unencrypted connections (host) using the pg_hba.conf file. Clients can also choose to use only SSL to connect to servers.

You can also encrypt your transmissions using SSH or Tunnel.

Avg

  • Data encryption ensures that information cannot be read or intercepted by unauthorized persons while it is being transmitted over the network.
  • To prevent tampering, SSL/TLS ensures that data is transmitted without modification.

Against

  • Data encryption increases computational costs, which, especially on busy systems, can cause a slight drop in speed.
  • It can be difficult and require careful configuration to set up and manage SSL/TLS certificates, especially in a distributed or clustered environment.

How to achieve

A configuration file kept in the data directory of the database cluster, usually called pg_hba.conf, manages client authentication. Host-based authentication is called HBA.

When initdb initializes the data directory, the default pg_hba.conf file is installed. However, you can use the hba_file configuration parameter to specify a different location for the authentication configuration file.

SSL host authentication

SSL certificates can be exchanged between the client and the server. This offers more robust identity verification than using passwords alone, although it requires additional setup on both sides.

It does not allow the computer to impersonate the server for the brief purpose of reading the password sent by the client.

Furthermore, it helps prevent man-in-the-middle attacks, in which a computer located between the client and the server assumes the role of the server and reads and transfers all data between the two.

Avg

  • By using SSL host authentication, users can be sure that they are connecting to a valid server and not a malicious or fake one.
  • By pretending to be a server, SSL host authentication prevents hackers from eavesdropping on client-server conversations.

Against

  • SSL certificates for host authentication can be difficult to set up and maintain, especially in setups with several servers and clients.
  • Ensuring the security of host authentication requires careful handling of SSL certificates and keys. Key loss or compromise can lead to security breaches.

How to achieve

For enhanced security, PostgreSQL comes with the native ability to encrypt client/server communications over SSL connections.

For this to work, PostgreSQL support must be enabled at build time, and OpenSSL must be installed on both client and server systems.

OpenSSL supports numerous authentication techniques and ciphers of varying strength. While a list of ciphers can be supplied in the OpenSSL configuration file, you can change the postgresql.conf ssl_ciphers to specifically name the ciphers for use by the database server.

To set up SSL, make sure the parameter value ssl = on is set in postgresql.conf

After generating the SSL you can mention the path of the SSL files in the options below
ssl_cert_file=’server.crt’
ssl_key_file=’server.key’

Other methods for data encryption-

Data partition encryption

Encrypting the database while on disk is the first technique. It is the most basic file system level encryption currently in use. If the drives or the computer as a whole are stolen, this method prevents unencrypted data from being read from them.

Because the operating system provides an unencrypted view of the data when the file system is mounted, it does not protect against attacks during that time.

However, the encryption key must be transferred to the operating system in order for the file system to be mounted. Sometimes the key is kept on the host computer that mounts the disk.

Avg

  • Data partition encryption provides an additional level of protection, protecting private data from unwanted access.
  • It helps to comply with legal obligations and regulations on privacy and data protection, such as GDPR, HIPAA, etc.
  • It allows encryption of specific partitions, giving the user precise control over the data that is encrypted.

Against

  • Data partition encryption can be difficult to manage and implement, and requires additional work.
  • Encryption keys must be managed properly, and this can be difficult to do securely.
  • Excessive performance can be introduced by encrypting and decrypting data, especially for large data sets.

Encryption for specific columns

Certain fields can be stored encrypted thanks to the pgcrypto module. If there is only a small amount of sensitive data, this is helpful. After the data is decrypted on the server and delivered to the client, the client provides the decryption key.

While the data is being decrypted and transferred between the client and the server, the decrypted data and the decryption key are kept in a file on the server for a short time.

This creates a brief opportunity for someone with full access to the database server, such as a system administrator, to intercept data and keys.

Avg

  • You can preserve sensitive data while leaving other data unencrypted by encrypting certain columns; this can help comply with legal requirements such as GDPR.
  • Encrypting individual columns can be less computationally demanding than entire tables, thus reducing the performance impact.

Against

  • Although column-specific encryption protects individual columns, if no security measures are taken with those columns, other columns become vulnerable to unwanted access.
  • Your database design and application code become more complex when you implement and manage column-specific encryption.

Conclusion

Sensitive and valuable amounts of data in an organization are growing simultaneously because sensitive data is always present, even in our daily personal lives.

The key is to understand that not all data needs to be encrypted. Certain types of data may not require encryption because they are regularly shared globally or used for different purposes.

Consider the pros and cons of implementing encryption in your database. Finding the right location and application method for it allows you to create a safe environment without sacrificing efficiency.

To see the encryption and decryption of admin configuration password in magento you can check the blog.

YOU NEED HELP?

Was this guide helpful? Please share your feedback in the comments below.

In case you have any problems/queries regarding the module, raise the ticket at
https://webkul.uvdesk.com/en/customer/create-ticket/

For any additional information or inquiries, contact us at [email protected].

Thanks for reading!!

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *