MariaDB Encryption
MariaDB Encryption
MariaDB Encryption
Additionally, encryption is supported for the Aria storage engine, but only for
tables created with ROW_FORMAT=PAGE (the default).
MariaDB allows the user to configure flexibly what to encrypt. In XtraDB or InnoDB,
one can choose to encrypt:
•everything — all tablespaces (with all tables)
•individual tables
•everything, excluding individual tables
To encrypt the key file use the The OpenSSL command line utility. For example:
openssl enc -aes-256-cbc -md sha1 -k secret -in keys.txt -out keys.enc
file_key_management plugin
[mysqld]
file_key_management_filename=/home/mdb/keys.enc
file_key_management_filekey=secret
file_key_management_encryption_algorithm=aes_cbc
Encrypting data
Note, that generally you should not enable only innodb-encrypt-tables while keeping
innodb-encrypt-log disabled.
In this setup log files will contain your data unencrypted.
The opposite case is fine, you may want to enable only innodb-encrypt-log, disable
innodb-encrypt-tables,
and activate encryption per table with the ENCRYPTED=YESoption.
[mysqld]
plugin-load-add=file_key_management.so
file-key-management
file-key-management-filename=/mount/usb1/keys.txt
innodb-encrypt-tables
http://dbversity.com/mariadb-data-encryption-at-rest/
pwd
ll -lhtr
Generate keys:
ll -lhtr /opt/rh/rh-mariadb101/root/usr/lib64/mysql/plugin/file_key_management.so
cat /etc/opt/rh/rh-mariadb101/my.cnf
plugin-load = file_key_management.so
file_key_management_encryption_algorithm = aes_cbc
file_key_management_filename = /etc/opt/rh/rh-
mariadb101/pki/key.txt
innodb-encrypt-tables
innodb-encrypt-log
innodb-encryption-threads=4
innodb_file_per_table = ON
Stopping rh-mariadb101-mariadb: [ OK ]
Starting rh-mariadb101-mariadb: [ OK ]
https://mariadb.com/resources/blog/table-and-tablespace-encryption-on-mariadb-10-1/
To encrypt the data in a MariaDB 10.1 database, you can enable data-at-rest
encryption.
MariaDB allows the option to select the most suitable level of the encryption in
MariaDB:
Temporary files,
Aria tables,
InnoDB tablespaces,
InnoDB tables,
InnoDB log files and Binlogs.
There are different methods for creating encryption keys, depending on the
encryption plugin used.
◦file_key_management plugin
◦Amazon Web Services (AWS) Key Management Service (KMS) Encryption Plugin:
Encryption plugins are responsible for both key management and for the actual
encryption and decryption of data.
The key file is a text file containing a key identifier and the hex-encoded key.
Specified Table encryption means that the administrator chooses which tables to
encrypt.
This allows you to balance security with speed.
To use table encryption, you have to:
[mariadb]
plugin-load-add=file_key_management
file_key_management_filename=/mnt/usb/secret.txt
file_key_management_encryption_algorithm=AES_CTR
We recommend that you place the encryption key file on an external storage device
(e.g., a USB drive).
This external storage can be unmounted after the MariaDB server is started and
stored in secure location.
After this, the database developer may select which tables contain sensitive data
for encryption.
Encryption can be enabled for a table when it’s created or using the ALTER TABLE
statement
See examples of how to create an encrypted table: using either a create table or an
alter table statement
https://mariadb.com/resources/blog/table-and-tablespace-encryption-on-mariadb-10-1/
Note that the InnoDB redo-log is not encrypted by default, even when the tables
are.
Consider also using encryption for the redo-log.
InnoDB redo-logs can be encrypted with this one line to the MariaDB configuration
file:
[mariadb]
innodb-encrypt-log
With tablespace encryption, all InnoDB tables and tablespaces are encrypted
including the system tablespace.
When configurating the server for the type of encryption, we recommended that you
also enable InnoDB redo-log encryption.
[mariadb]
innodb-encrypt-tables
innodb-encrypt-log
innodb-encryption-threads = 4
plugin-load-add=file_key_management
file_key_management_filename=/mnt/usb/secret.txt
file_key_management_encryption_algorithm=AES_CTR
# for monitoring
innodb-tablespaces-encryption
After adding the above setting and restarted the server to implement them,
all existing tables and all new tables will be encrypted—unless specified otherwise
for a particular table.
Despite the configuration, MariaDB does allow encryption to be disabled for tables
that don’t require encryption.
This can be done when the table is created or by altering it later with the ALTER
TABLE statement.
If you don’t want users to be able to create tables without encryption, you can set
the server to force encryption.
Just add the following line to the MariaDB confirguration file:
innodb-encrypt-tables=FORCE
https://severalnines.com/blog/database-security-fully-ssl-encrypt-mysql-galera-
cluster-clustercontrol
https://severalnines.com/blog/database-security-backup-encryption-transit-rest
https://severalnines.com/blog/how-encrypt-your-mysql-mariadb-backups
Testing ###############################
cd in data dir: Run the command below and it will prompt for a password
file_key_management_filename = /etc/mysql/keys.enc
file_key_management_filekey = FILE:/etc/mysql/.key
[mysqld]
plugin-load-add=file_key_management
innodb-encrypt-tables
innodb-encrypt-log
innodb-encryption-threads = 4
#file_key_management_filename=/mnt/usb/secret.txt
file_key_management_filename=/glide/mysql/data/key.txt
#file_key_management_encryption_algorithm=AES_CTR
file_key_management_encryption_algorithm=aes_cbc
# for monitoring
innodb-tablespaces-encryption
The File Key Management plugin supports two encryption algorithms: AES_CBC and
AES_CTR.
The recommended algorithm is CTR, but CTR is only available when MariaDB is built
with recent versions of OpenSSL.
Encrypting the key:
[mysqld]
plugin-load-add=file_key_management
innodb-encrypt-tables
innodb-encrypt-log
innodb-encryption-threads = 4
#file_key_management_filename=/mnt/usb/secret.txt
#file_key_management_filename=/glide/mysql/data/key.txt
file_key_management_filename = /etc/mysql/keys.enc
file_key_management_filekey = FILE:/glide/mysql/data/key.txt
#file_key_management_encryption_algorithm=AES_CTR
file_key_management_encryption_algorithm=aes_cbc
# for monitoring
innodb-tablespaces-encryption
#########################################################################
innodb-encrypt-tables
innodb-encrypt-log
innodb-encryption-threads = 4
plugin-load-add=file_key_management
file_key_management_filename=/glide/mysql/data/key.txt
file_key_management_encryption_algorithm=aes_cbc
# for monitoring
innodb-tablespaces-encryption
cat key.txt
1;47C3075BA2C137A665BF2E25BD963D9E;8D96C9337136FA7A4F2DEE7CE2F13A45669DF4781ADD1C31
5E839038D53AEA20
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
--> We only have a key id of 1 in the key.txt file so command above is expected to
fail.
[root@mafiscotech02 data]# ll
total 178252
-rw-rw----. 1 mysql mysql 16384 Dec 6 18:26 aria_log.00000001
-rw-rw----. 1 mysql mysql 52 Dec 6 18:26 aria_log_control
-rw-rw----. 1 mysql mysql 77594624 Dec 6 18:36 ibdata1
-rw-rw----. 1 mysql mysql 52428800 Dec 6 18:36 ib_logfile0
-rw-rw----. 1 mysql mysql 52428800 Dec 4 14:18 ib_logfile1
-rw-r--r--. 1 mysql mysql 100 Dec 6 18:20 key.txt
-rw-rw----. 1 mysql mysql 6 Dec 6 18:26 mafiscotech02.localdomain.pid
-rw-rw----. 1 mysql mysql 0 Dec 4 14:26 multi-master.info
drwx------. 2 mysql mysql 4096 Dec 4 14:18 mysql
-rw-rw----. 1 mysql mysql 16562 Dec 6 18:26 mysqld.log
drwx------. 2 mysql mysql 20 Dec 4 14:18 performance_schema
-rw-rw----. 1 mysql mysql 24576 Dec 6 18:26 tc.log
drwx------. 2 mysql mysql 78 Dec 6 18:36 test
[root@mafiscotech02 data]# cd test/
[root@mafiscotech02 test]# ll
total 200
-rw-rw----. 1 mysql mysql 1569 Dec 6 18:35 table1.frm
-rw-rw----. 1 mysql mysql 98304 Dec 6 18:35 table1.ibd
-rw-rw----. 1 mysql mysql 1590 Dec 6 18:36 table2.frm
-rw-rw----. 1 mysql mysql 98304 Dec 6 18:36 table2.ibd
[root@mafiscotech02 test]# strings table1.frm | head
ENCRYPTED
PRIMARY
InnoDB
col1
secret
[root@mafiscotech02 test]# which xxd
/usr/bin/xxd
[root@mafiscotech02 test]# xxd table1.ibd | head
0000000: 05c1 6fdf 0000 0000 0000 0000 0000 0000 ..o.............
0000010: 0000 0000 0018 c0e6 0008 0000 0000 0000 ................
0000020: 0000 0000 0004 0000 0004 0000 0000 0000 ................
0000030: 0006 0000 0040 0000 0000 0000 0004 0000 .....@..........
0000040: 0000 ffff ffff 0000 ffff ffff 0000 0000 ................
0000050: 0001 0000 0000 009e 0000 0000 009e 0000 ................
0000060: 0000 ffff ffff 0000 ffff ffff 0000 0000 ................
0000070: 0000 0000 0003 0000 0000 ffff ffff 0000 ................
0000080: ffff ffff 0000 0000 0001 0000 0002 0026 ...............&
0000090: 0000 0002 0026 0000 0000 0000 0000 ffff .....&..........
[root@mafiscotech02 test]#