MySQL 5.7 TDE

一. 简介

MySQL 5.7之后支持通过mysql keyring(一种两层加密架构,由一个master key和多个tablespace key组成;当InnoDB表被加密的时候,其实是对tablespace key加密并存储在表空间文件头里,当访问被加密的InnoDB表的时候,InnoDB使用master key进行解密)对独立表空间中的InnoDB表的数据进行加密,来提升物理文件的安全性。

企业版与非企业版插件区别

MySQL Enterprise Edition offers the keyring_okv plugin,When InnoDB tablespace encryption uses OKV for encryption key management, the feature is referred to as “MySQL Enterprise Transparent Data Encryption (TDE)”.

non-enterprise editions of MySQL uses the keyring_file plugin for encryption key management.

加密方式为AES
InnoDB tablespace encryption supports the Advanced Encryption Standard (AES) block-based encryption algorithm

加密键
tablespace key
master key rotation

Only one keyring plugin should be enabled at a time. Enabling multiple keyring plugins is not supported.

二. 配置

  • MySQL 5.7.11之后,所有版本包含keyring_file插件
  • MySQL 5.7.12之后,企业版包含keyring_okv插件
  • MySQL 5.7.13之后,所有版本包含key管理的SQL接口(UDF,用户定义函数)

配置文件】 在my.cnf里添加2行
early-plugin-load=keyring_file.so
keyring_file_data=/usr/local/mysql/keyring/keyring

添加目录
Linux > mkdir -p /usr/local/mysql/keyring
Linux > chown -R mysql:mysql /usr/local/mysql/keyring/
Linux > chmod 750 /usr/local/mysql/keyring/

安装插件
mysql> INSTALL PLUGIN keyring_file soname 'keyring_file.so';

卸载插件】 忽略
mysql > UNINSTALL PLUGIN keyring_file;

秘钥备份
Linux > mkdir /root/backup
Linux > cp /usr/local/mysql/keyring/keyring /root/backup

插件检查
mysql > show plugins ; 查看插件是否启用

旋转主加密密钥
mysql > ALTER INSTANCE ROTATE INNODB MASTER KEY;

三. 使用

加密表创建
CREATE TABLE t1 (c1 INT) ENCRYPTION='Y';

查看所有加密表
select * from information_schema.tables where create_options like '%ENCRYPTION%="y"%';

修改表加密
ALTER TABLE t1 ENCRYPTION='Y';
ALTER TABLE t1 ENCRYPTION='N';

查看秘钥
show variables like '%keyring%';

四. 限制

InnoDB表空间加密限制

  • 高级加密标准(AES)是唯一支持的加密算法。 InnoDB表空间加密为表空间密钥加密和密码分组链接(CBC)块加密模式使用电子密码本(ECB)块加密模式进行数据加密。

  • 支持改变表的ENCRYPTION属性ALGORITHM = COPY操作。 ALGORITHM = INPLACE不支持。

  • InnoDB表空间加密仅支持存储在单独表空间的InnoDB表。存储在其他InnoDB表空间类型(包括常规表空间,系统表空间,撤消日志表空间和临时表空间)中的表不支持加密。

  • 无法将加密表从单独表空间移动或复制到不支持的InnoDB表空间类型。

  • 表空间加密仅适用于表空间中的数据。数据在重做日志撤消日志二进制日志中不加密。

  • 直接从keyring_file插件迁移到keyring_okv插件目前不受支持。更改密钥环插件需要解密表,卸载当前的密钥环插件,安装和配置其他密钥环插件,并重新加密表。

五. 还原测试

备注:发现master key在线删除后,加密表还是可以访问,所以猜测,每次mysql启动时候会调用master key对加密表进行解密,而不是实时解密,实际通过观察errorlog中启动流程发现确实如此。

mysqldump】 备份文件可以还原到开启加密插件的mysql实例上,会自动生成master key

xtranbackup】在没有master key的情况下,不可以打开加密表,可以打开非加密表。

物理备份】 在没有master key的情况下,不可以打开加密表,可以打开非加密表。
物理备份】 在有master key的情况下,可以打开加密表。

[ERROR] InnoDB: Encryption can't find master key, please check the keyring plugin is loaded.
[ERROR] InnoDB: Encryption information in datafile: ./test/emp.ibd can't be decrypted , please confirm the keyfile is match and keyring plugin is loaded.

丢失master key 或 master key错误

[ERROR] InnoDB: Encryption information in datafile: ./test/emp.ibd can't be decrypted , please confirm the keyfile is match and keyring plugin is loaded.
[Warning] InnoDB: Ignoring tablespace `test/emp` because it could not be opened.

六. 性能测试

实验环境: 5.7.19-log MySQL Community Server
实验目的:验证表空间加密和正常表空间对SQL语句的性能影响
实验步骤:

  1. 复制两张表到测试MySQL中
  2. 一张表加密,另外一张表不加密
  3. 使用mysqlslap分别对select、update、insert语句进行测试

实验结果:

SQL类型 select update insert
非加密表 8.762秒 32.447秒 24.064秒
加密表 8.899秒 32.505秒 26.154秒

结论:

官方提供的数据是加密表大约影响5%左右的性能,我们测试下来对SQL的性能加密表和非加密表影响不大,完全可以满足公司需要。

七.秘钥备份

秘钥在MySQL实例启动时需要对加密表空间进行解密,如果秘钥丢失可能会导致无法打开加密表空间,造成数据丢失,所以秘钥是很重要的文件。
正式项目需要考虑秘钥异机备份事宜,将keyring_file_data参数对应的秘钥目录下所有文件进行复制。

八.参考文献

https://zhuanlan.zhihu.com/p/29761390
https://dev.mysql.com/doc/refman/5.7/en/innodb-tablespace-encryption.html
http://www.dbsec.cn/research/research/20170802.html

posted @ 2018-08-22 14:15  Xuty  阅读(2551)  评论(0编辑  收藏  举报