In today's data-driven landscape, protecting your MySQL databases is paramount. Binary log encryption(called binlog encryption) in MySQL 8.0 offers a powerful solution to safeguard sensitive information. This feature is available from the MySQL version 8.0.14 or above.
Our previous blogs discussed about table space encryption in MySQL and Percona servers. In Mydbops, we are giving high importance about achieving security compliances.
The binary log records changes made to the databases so that it can be used to replicate the same to the slaves and also for the point in time recovery (PITR). So, it means that if someone has access to the binary logs, they can reproduce our entire database in many forms. As a DBA, we need to make sure that the binary log files are protected from users who are having access to the file system and also, log files need to be encrypted to follow the security compliance requirements by some clients.
These new features came as a rescue to satisfy those requirements. Now we will have a look at how to use and maintain it.
Note: To explore this feature, I have installed MySQL 8.0.18 in our testing environment.
Below are the topics that we are going to discuss in this blog:
- Enabling Encryption
- Rotating Key Manually
- High Level Architecture
- To access the encrypted binary log
- To decrypt the encrypted binary log
Enabling Encryption
Let’s start this by enabling the binlog encryption. Let us list down the current binary logs and their status.
MySQL Server has one binary log and it is not encrypted it is not encrypted by default.
To proceed further, make sure you are having SUPER or BINLOG_ENCRYPTION_ADMIN privilege for your user to enable the encryption online.
Now, we can observe the latest binary log is encrypted. If the encrypted binary logs were the only one to be present in the server we can safely purge(remove) them.
Currently, the server has encrypted binary logs. Also, we can observe the increase in the size of the encrypted binary log (empty) when compared with unencrypted ones. It is because of the 512 bytes encrypted header and the header is never replicated.
Rotating Encryption Key
The encryption of binary logs is made and it is time to rotate the encryption key. This must be done periodically to comply with security compliances. We can rotate the encryption key manually.
The below process is performed by the server while rotating the key.
- The binary and relay log files are flushed (rotated).
- A new binary log encryption key is generated with the next available sequence number, stored on the keyring, and used as the new binary log master key.
- Binary log encryption keys that are no longer in use for any files after the re-encryption process are removed from the keyring.
Also, we are having one more variable binlog_rotate_encryption_master_key_at_startup which ensures the binary log encryption key has to be rotated at server startup.
High-Level Architecture
Binlog Encryption is designed to use two-tier encryption.
- File Passwords
- Replication Encryption key
The File Password is used to encrypt/decrypt binary log content and Replication Encryption Key is used to encrypt/decrypt the file password in the encrypted binary log header.
A single replication encryption key may be used to encrypt/decrypt many binary and relay log files passwords, while a file password is intended to encrypt/decrypt a single binary or relay log file.
Multiple file passwords rely on the single encryption key whereas file password is used for the individual log files.
Okay, what happens if the encryption key REK1 is rotated?
The server generates a new replication encryption key REKj and it iterates over all encrypted log files to re-encrypt their password (Iterate from the last file to the first file ) by overwriting the encrypted file header with the new one. So, my new encryption key can decrypt all the available file passwords.
Compare the encrypted log file with unencrypted one
The only difference is the binlog header.
Here is the detailed header of the encrypted binlog:
Magic Number
It is needed to distinguish from an encrypted to an unencrypted binary log files.
An encrypted binlog file has 0xFD62696E . An unencrypted binlog file : 0xFE62696E
Form an encrypted log file:
Form an unencrypted log file:
Replication logs encryption version
The current value is 1. It might change in the future.
Replication encryption key ID
The key ID of the replication master key that encrypted the file password. It is a combination of MySQLReplicationKey, UUID, and SEQ_NO. It can be identified in the following format.
Here is an example:
Now Let us try rotating the encryption key.
After successful rotation, the encryption key is
We can observe that My SEQ_NO is incremented by 1. It will keep going on all the encryption key rotations.
Encrypted File Password
It is used to generate the key for encrypting/decrypting the binary log content.
IV for encrypting file password:
It is used with the replication master key to encrypt the file password.
Padding
Unused header space will be filled with 0.
How to access the encrypted binary log using mysqlbinlog utility ?
Sometimes, It is necessary to decode the binary log for PITR / to get to know the write pattern. Using mysqlbinlog, we can’t directly decode the encrypted binlog as it is not having access to the keyring file.
For example:
But it is possible to get it by making a request to the MySQL server
How to decrypt an encrypted binary log file?
The manual decryption of encrypted binary log is possible when the value of the key that encrypted its file password is known. We can use this amazing post https://mysql.wisborg.dk/2019/01/28/automatic-decryption-of-mysql-binary-logs-using-python/ to decrypt the binary log. To use this script, we need only have access to the keyring file.
Note: Even if we do not have enabled binary log on the server, we can still use it to encrypt relay log files on the slave servers.
Ready to elevate your MySQL security with binary log encryption? Mydbops offers expert guidance and comprehensive MySQL services to ensure optimal data protection. Contact us today to explore our tailored solutions!
{{cta}}