This topic describes the syntax, parameters, and limitations of creating a column encryption rule, and provides examples to help you quickly learn how to configure column encryption rules.
Prerequisites
Before you use a sensitive rule, enable TDE and create a master key:
-- Set the method for transparent tablespace encryption. Valid values: see tde_method (for example, internal, obcloud, and ocp).
ALTER SYSTEM SET tde_method = '<encryption_method>';
ALTER INSTANCE ROTATE INNODB MASTER KEY; -- The first execution takes about 20 seconds for the key to take effect.
For more information about the tde_method parameter, see tde_method.
Syntax
Before you create a column encryption rule, you must understand its syntax structure. OceanBase Database provides complete DDL statements to manage column encryption rules, including create, drop, and alter operations.
Create a column encryption rule
CREATE SENSITIVE RULE <rule_name>
ON <sensitive_field_list>
USING ENCRYPTION [ = <encryption_method>];
For more information, see CREATE SENSITIVE RULE.
Drop a column encryption rule
When you need to remove a column encryption rule, you can use the following syntax:
DROP SENSITIVE RULE <rule_name>;
For more information, see DROP SENSITIVE RULE.
Alter a column encryption rule
In actual use, you may need to adjust column encryption rules based on business requirements. OceanBase Database provides flexible modification syntax to meet these needs:
-- Add sensitive columns
ALTER SENSITIVE RULE <rule_name> ADD COLUMN <sensitive_field_list>;
-- Drop sensitive columns
ALTER SENSITIVE RULE <rule_name> DROP COLUMN <sensitive_field_list>;
-- Enable the rule
ALTER SENSITIVE RULE <rule_name> ENABLE;
-- Disable the rule
ALTER SENSITIVE RULE <rule_name> DISABLE;
-- Modify the encryption algorithm
ALTER SENSITIVE RULE <rule_name> USING ENCRYPTION [= <encryption_method>];
For more information, see ALTER SENSITIVE RULE.
Examples
The following examples help you better understand column encryption rule operations. They cover the main scenarios of creating, dropping, and altering rules.
Create
-- Create sensitive rule r1, protecting tbl1(a), tbl2(b, c), and db2.tbl3(e,f).
-- If no parameter is specified, the default encryption algorithm AES-256-ECB is used, which is equivalent to USING ENCRYPTION = 'aes-256'.
CREATE SENSITIVE RULE r1 on tbl1(a), tbl2(b, c), db2.tbl3(e,f)
USING ENCRYPTION;
-- Create sensitive rule r2, protecting tbl4(x, y) and tbl5(z).
-- Use the SM4-CBC encryption algorithm.
CREATE SENSITIVE RULE r2 on tbl4(x, y), tbl5(z)
USING ENCRYPTION = 'sm4-cbc';
Drop
DROP SENSITIVE RULE r1;
Alter
-- Add tbl2(c) and db2.tbl3(x) to sensitive rule r1.
ALTER SENSITIVE RULE r1 ADD COLUMN tbl2(c), db2.tbl3(x);
-- Drop tbl2(c) from sensitive rule r1.
ALTER SENSITIVE RULE r1 DROP COLUMN tbl2(c);
-- Enable sensitive rule r1.
ALTER SENSITIVE RULE r1 ENABLE;
-- Disable sensitive rule r1.
ALTER SENSITIVE RULE r1 DISABLE;
-- Change the encryption algorithm of sensitive rule r1 to the default algorithm.
ALTER SENSITIVE RULE r1 USING ENCRYPTION;
-- Change the encryption algorithm of sensitive rule r1 to sm4-cbc.
ALTER SENSITIVE RULE r1 USING ENCRYPTION = 'sm4-cbc';
Complete example: from creating a rule to verifying query results
This example protects the phone column of the enc_demo.enc_employees table in a MySQL-mode test tenant, and shows the query results of a regular user before and after the rule-level plaintext access privilege is granted and revoked. The example is verified with OceanBase Database V5.0.1 and OBClient V2.2.10.
Use an independent test tenant, and make sure that the database, table, user, and rule names in the example have not been used. Replace <host>, <port>, and <tenant_name> in the connection command and <reader_password> in the SQL statements with actual values. Do not overwrite the key configuration of an existing production tenant.
Note
Column encryption controls plaintext access in query results and is not equivalent to tablespace storage encryption. The root user has plaintext access by default, so a newly created regular user, enc_reader, is used to verify the effect of the rule. Querying a table also requires the SELECT privilege. PLAINACCESS does not replace the SELECT privilege.
Connect to the target MySQL tenant as the
rootuser. Note that you are not connecting to the sys tenant.$ obclient -h<host> -P<port> -uroot@<tenant_name> -pConfigure the master key. This example uses the
internalmethod. Tenants that have completed the master key configuration described in the "Prerequisites" section of this topic can skip this step. After the first configuration, wait for the master key to take effect as described in the "Prerequisites" section, and then perform the following steps.obclient> ALTER SYSTEM SET tde_method='internal'; Query OK, 0 rows affected obclient> ALTER INSTANCE ROTATE INNODB MASTER KEY; Query OK, 0 rows affectedCreate a database and a test table, and insert two rows of data.
obclient> CREATE DATABASE enc_demo; Query OK, 1 row affected obclient> USE enc_demo; obclient> CREATE TABLE enc_employees (id INT PRIMARY KEY, phone VARCHAR(32)); Query OK, 0 rows affected obclient> INSERT INTO enc_employees VALUES (1, '13800000001'), (2, '13800000002'); Query OK, 2 rows affected Records: 2 Duplicates: 0 Warnings: 0 obclient> COMMIT; Query OK, 0 rows affected obclient> SELECT id, phone FROM enc_employees ORDER BY id; +------+-------------+ | id | phone | +------+-------------+ | 1 | 13800000001 | | 2 | 13800000002 | +------+-------------+ 2 rows in setCreate a regular user
enc_reader, grant theSELECTprivilege on the table to this user, and then create a sensitive ruler_phoneto protect thephonecolumn. Do not grant the plaintext access privilege at this stage.obclient> CREATE USER enc_reader IDENTIFIED BY '<reader_password>'; Query OK, 0 rows affected obclient> GRANT SELECT ON enc_employees TO enc_reader; Query OK, 0 rows affected obclient> CREATE SENSITIVE RULE r_phone ON enc_employees(phone) USING ENCRYPTION='aes-256'; Query OK, 0 rows affectedKeep the administrator session open. In another terminal, log in to the
enc_demodatabase of the same tenant asenc_readerand query data.$ obclient -h<host> -P<port> -uenc_reader@<tenant_name> -p --binary-as-hex enc_demo--binary-as-hexmakes OBClient display binary ciphertext in hexadecimal format to avoid displaying non-printable characters. The ciphertext depends on the key, and the actual values may differ from this example.obclient> SELECT id, phone FROM enc_employees ORDER BY id; +------+------------------------------------+ | id | phone | +------+------------------------------------+ | 1 | 0x1AE6C8C6D1328E85E8D9E7A547196C4E | | 2 | 0xA34CFA09540F1847FA0E569AFCBF1E78 | +------+------------------------------------+ 2 rows in setThe
idcolumn is displayed normally, while thephonecolumn returns ciphertext. This indicates that a user with only theSELECTprivilege cannot read the plaintext of protected columns.In the administrator session, grant the plaintext access privilege on
r_phonetoenc_reader.obclient> GRANT PLAINACCESS ON SENSITIVE RULE r_phone TO enc_reader; Query OK, 0 rows affectedExit the session of
enc_reader, log in again with the connection command in step 5, and query data.obclient> SELECT id, phone FROM enc_employees ORDER BY id; +------+-------------+ | id | phone | +------+-------------+ | 1 | 13800000001 | | 2 | 13800000002 | +------+-------------+ 2 rows in setThe
phonecolumn returns the phone numbers inserted, indicating that the rule-level plaintext access authorization has taken effect.In the administrator session, revoke the plaintext access privilege but retain the
SELECTprivilege on the table.obclient> REVOKE PLAINACCESS ON SENSITIVE RULE r_phone FROM enc_reader; Query OK, 0 rows affectedExit the session of
enc_reader, log in again with the connection command in step 5, and query data.obclient> SELECT id, phone FROM enc_employees ORDER BY id; +------+------------------------------------+ | id | phone | +------+------------------------------------+ | 1 | 0x1AE6C8C6D1328E85E8D9E7A547196C4E | | 2 | 0xA34CFA09540F1847FA0E569AFCBF1E78 | +------+------------------------------------+ 2 rows in setThe query still succeeds, but the
phonecolumn returns ciphertext again, indicating that the plaintext access privilege has been revoked. This example verifies the authorization result by logging in again. It is not intended to determine when the privileges of an existing session are refreshed.
References
- Overview of column encryption
- Column encryption privilege management
- For more information about the definition and attributes of a sensitive rule, see DBA/CDB_OB_SENSITIVE_RULES.