Skip to content

Commit

Permalink
Create MariaDB encryption keys in buendia-mysql postinst.
Browse files Browse the repository at this point in the history
  • Loading branch information
schuyler committed Feb 7, 2020
1 parent 98dc9ef commit 81d4197
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
22 changes: 22 additions & 0 deletions packages/buendia-mysql/control/postinst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,28 @@ set -e; . /usr/share/buendia/utils.sh

case $1 in
configure)
if [ ! -r /etc/mysql/keyfile.enc ]; then
# Key #1 is required by MariaDB for encrypting system data. It will be used
# for other purposes as well, if no other keys are defined.
# https://mariadb.com/kb/en/encryption-key-management/#using-multiple-encryption-keys
( echo -n '1;'; openssl rand -hex 32 ) > /etc/mysql/keyfile

# Encrypt the keyfile using the Buendia system key and remove the plaintext version
openssl enc -aes-256-cbc -md sha1 -in /etc/mysql/keyfile -out /etc/mysql/keyfile.enc \
-pass file:/usr/share/buendia/system.key
rm /etc/mysql/keyfile
fi

# Ensure that MariaDB can read the encrypted key.
chown root:mysql /etc/mysql/keyfile.enc
chmod 640 /etc/mysql/keyfile.enc

# Ensure that MariaDB can read the system key.
chown root:mysql /usr/share/buendia/system.key
chmod 640 /usr/share/buendia/system.key

service mysql restart

buendia-reconfigure mysql
service cron start
;;
Expand Down
5 changes: 4 additions & 1 deletion packages/buendia-mysql/control/preinst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
set -e; . /usr/share/buendia/utils.sh

case $1 in
install|upgrade) service_if_exists cron stop ;;
install|upgrade)
service_if_exists cron stop
service_if_exists mysql stop
;;
*) exit 1
esac

2 comments on commit 81d4197

@zestyping
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the thinking behind having two separate keys (system and mysql)?

@schuyler
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to be a best practice recommended by the MariaDB documentation. I think the primary reasons are that MariaDB AES keys must be a specific length (64 bits) and that MariaDB is designed to permit multiple encryption keys to be registered. Their solution to having all the database secrets stored securely at rest is to encrypt them with a separate key.

https://mariadb.com/kb/en/file-key-management-encryption-plugin/#encrypting-the-key-file

I intend to write a project-wide document on the design of the encryption-at-rest and data theft prevention set up.

Please sign in to comment.