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 create a column encryption rule, you need to configure the key retrieval method and obtain the 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>';
-- Create the keystore
ADMINISTER KEY MANAGEMENT CREATE KEYSTORE keystore_name IDENTIFIED BY oceanbase_password;
-- Open the keystore
ADMINISTER KEY MANAGEMENT SET KEYSTORE OPEN IDENTIFIED BY oceanbase_password;
-- Set the master key. The first execution takes about 20 seconds for the master key to take effect.
ADMINISTER KEY MANAGEMENT SET KEY IDENTIFIED BY oceanbase_password;
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 user2.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), user2.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 user2.tbl3(x) to sensitive rule r1.
ALTER SENSITIVE RULE r1 ADD COLUMN tbl2(c), user2.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 SYS.ENC_EMPLOYEES table in an Oracle-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 table, user, and rule names in the example have not been used. Replace <host>, <port>, and <tenant_name> in the connection command and <keystore_password> 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 SYS user has plaintext access by default, so a newly created regular user, enc_reader, is used to verify the effect of the rule. This user also requires the CREATE SESSION privilege and the SELECT privilege on the table. PLAINACCESS does not replace these privileges.
Connect to the target Oracle tenant as the
SYSuser. Note that you are not connecting to the sys tenant.$ obclient -h<host> -P<port> -uSYS@<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> ADMINISTER KEY MANAGEMENT CREATE KEYSTORE enc_keystore IDENTIFIED BY "<keystore_password>"; Query OK, 0 rows affected obclient> ADMINISTER KEY MANAGEMENT SET KEYSTORE OPEN IDENTIFIED BY "<keystore_password>"; Query OK, 0 rows affected obclient> ADMINISTER KEY MANAGEMENT SET KEY IDENTIFIED BY "<keystore_password>"; Query OK, 0 rows affectedCreate a test table in the
SYSschema, insert two rows of data, and commit the transaction.obclient> CREATE TABLE enc_employees (id NUMBER PRIMARY KEY, phone VARCHAR2(32)); Query OK, 0 rows affected obclient> INSERT INTO enc_employees VALUES (1, '13800000001'); Query OK, 1 row affected obclient> INSERT INTO enc_employees VALUES (2, '13800000002'); Query OK, 1 row affected 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 the privileges to log in and query 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 CREATE SESSION TO enc_reader; 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 same tenant as
enc_readerand query data. The table belongs to theSYSschema, so specifySYS.enc_employeesin the query.$ obclient -h<host> -P<port> -uenc_reader@<tenant_name> -p --binary-as-hex--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 SYS.enc_employees ORDER BY id; +------+------------------------------------+ | ID | PHONE | +------+------------------------------------+ | 1 | 0x48D5F55A7C163CC204D02C15917B00E6 | | 2 | 0x4AC723FA73DD69C982C8A1038E0AD489 | +------+------------------------------------+ 2 rows in setThe
IDcolumn is displayed normally, while thePHONEcolumn returns ciphertext. This indicates that a user without the plaintext access privilege cannot read the plaintext of protected columns.In the
SYSsession, grant the plaintext access privilege onr_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 SYS.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
SYSsession, revoke the plaintext access privilege but retain the privileges to log in and query 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 SYS.enc_employees ORDER BY id; +------+------------------------------------+ | ID | PHONE | +------+------------------------------------+ | 1 | 0x48D5F55A7C163CC204D02C15917B00E6 | | 2 | 0x4AC723FA73DD69C982C8A1038E0AD489 | +------+------------------------------------+ 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.
View
View name |
Description |
|---|---|
| DBA_OB_SENSITIVE_RULES | View the definitions and attributes of all sensitive rules. |
| DBA_OB_SENSITIVE_COLUMNS | View all protected columns. |