The Basic Edition of the AnalyticDB for MySQL Confidential Engine for Apache Spark encrypts sensitive data, ensuring that the data is transmitted and stored as ciphertext. Only users who possess the key can decrypt the data, which prevents data breaches. This topic uses an RDS for MySQL database as an example to demonstrate the complete process of using the Basic Edition of the Confidential Engine for Apache Spark: encrypting data, running computations on ciphertext, and decrypting the results.
Prerequisites
An AnalyticDB for MySQL Enterprise Edition, Basic Edition, or Data Lakehouse Edition cluster is created.
A database account is created for the AnalyticDB for MySQL cluster.
If you use an Alibaba Cloud account, you need to only create a privileged account.
If you use a Resource Access Management (RAM) user, you must create a privileged account and a standard account and associate the standard account with the RAM user.
The RAM user has been granted the AliyunADBFullAccess and AliyunADBSparkProcessingDataRole permissions, and read and write permissions on AnalyticDB for MySQL databases and tables. For more information, see Account authorization.
You have installed a Java 8 development environment to run the client tool. This topic uses an ECS instance that runs Linux as an example.
Limitations
The adb-crypto-tool client tool can only encrypt or decrypt data in MySQL and PostgreSQL databases.
NoteThe MySQL or PostgreSQL database can be a cloud database, a self-managed database, or a third-party cloud database.
The confidential engine does not support the Decimal data type or nested data types. You can use the Float or Double data type as an alternative.
For PostgreSQL databases, the confidential engine does not automatically remove trailing spaces from the
Char(n)data type. Use theVarchar(n)type instead.
Data preparation
In this example, an RDS for MySQL database is named adb, and a table named customer is created in the database. The following is a sample statement:
CREATE DATABASE IF NOT EXIST adb;
CREATE TABLE IF NOT EXISTS adb.customer(
id int,
name varchar(1023),
address varchar(1023),
age int
);Insert data into the customer table. The following are sample statements:
INSERT INTO customer (id,name,address,age) VALUES('1', 'james', 'beijing', '10');
INSERT INTO customer (id,name,address,age) VALUES('2', 'bond', 'beijing', '15');
INSERT INTO customer (id,name,address,age) VALUES('3', 'jack', 'shanghai', '20');
INSERT INTO customer (id,name,address,age) VALUES('4', 'lucy', 'hangzhou', '25');
INSERT INTO customer (id,name,address,age) VALUES('5', 'tom', 'qingdao','30');Procedure
Step 1: Install the encryption and decryption client. Download and install the adb-crypto-tool client tool.
Step 2: Encrypt and upload data. Use the client tool to encrypt the data from a plaintext table into a ciphertext dataset and upload it to Object Storage Service (OSS).
Step 3: Execute SQL computations on ciphertext. Use the SQL Console of the AnalyticDB for MySQL Confidential Engine for Apache Spark to run computations on the ciphertext. The results are stored as ciphertext in OSS.
Step 4: Download and decrypt the ciphertext results. Download the ciphertext results to your local machine, and then use the client tool to decrypt them. The decrypted data is automatically uploaded to a plaintext table in your database.
Install the encryption and decryption client
Download the client tool: adb-crypto-tool-1.0.4.zip.
Decompress the adb-crypto-tool-1.0.4.zip package.
unzip adb-crypto-tool-1.0.4.zipAfter you decompress the package, you will find the following three files and folders:
cryptoTool: The entry point of the encryption and decryption tool.
NoteThe cryptoTool requires a bash environment. On a Windows operating system, you can use a tool such as Windows Subsystem for Linux (WSL) to run the tool.
demo.json: A sample configuration file.
dist: The dependency packages for the encryption and decryption tool.
Encrypt and upload data
Prepare an encryption configuration file. You can create a new configuration file by referencing demo.json, or directly modify the
table,jdbcUrl,username, andpasswordparameters in the demo.json file. The example in this topic uses a new configuration file namedencryption_config.json. An example is as follows:{ "dataDir": "file:///tmp", "table": [ "customer" ], "encAlgo": "AES_128_GCM", "mek": "00112233445566778899aabbccdd****", "jdbcUrl": "jdbc:mysql://rm-bp1487t8613zz****.mysql.rds.aliyuncs.com:3306/adb", "username": "demo_user", "password": "demo_password", "saveMode": "Overwrite", "format":"EncBlocksSource" }The following table describes the parameters.
Parameter
Description
dataDirThe local path where the ciphertext dataset is stored.
tableThe name of the table to encrypt. In this example, the table is customer.
encAlgoThe encryption and decryption algorithm for the ciphertext. Only AES_128_GCM is supported.
mekThe master encryption key (MEK) is used to encrypt a data encryption key (DEK). For more information, see Key introduction.
The master encryption key must be a 16-byte hexadecimal string. You can generate the key by using the OpenSSL tool. An example is
00112233445566778899aabbccdd****.WarningThe master encryption key is the root credential for accessing encrypted data. If you lose the key, you can no longer access the data. You must keep the master encryption key secure.
The confidential engine does not provide a service for generating or backing up master encryption keys. You must enter a master encryption key in the required format.
jdbcUrlThe JDBC URL and name of the database. Only MySQL and PostgreSQL databases are supported. The JDBC URL must be in one of the following formats:
MySQL database:
jdbc:mysql://ip:port/database_namePostgreSQL database:
jdbc:postgresql://ip:port/database_name
In this example, the JDBC URL is
jdbc:mysql://rm-bp1487t8613zz****.mysql.rds.aliyuncs.com:3306/adb.userNameThe database account.
passwordThe password of the database account.
saveModeSpecifies the action to take if the destination path for the ciphertext dataset already exists. Valid values:
Append: Appends data.
ErrorIfExists: Reports an error and exits if the destination exists. You can view the error information in the logs.
Ignore: Aborts the write operation if the destination exists. You cannot view error information in the logs.
Overwrite: Overwrites existing data.
formatThe format of the ciphertext. Only EncBlocksSource is supported.
Run the client tool to encrypt data in the
customertable of theadbdatabase. The encrypted dataset is stored in thedataDirpath specified in the configuration file../cryptoTool encrypt encryption_config.jsonUpload the
customerfolder containing the encrypted dataset to an OSS bucket. In this topic, the example path isoss://testBucketName/adb/Spark/customer. For more information, see Simple upload.
Execute SQL computations on ciphertext
Log on to the AnalyticDB for MySQL console. In the upper-left corner of the console, select a region. In the left-side navigation pane, click Clusters. Find the cluster that you want to manage and click the cluster ID.
In the left-side navigation pane, choose Job Development>SQL Development.
In the SQL Console window, select the Spark engine and a job resource group.
Run the following statements to perform SQL computations on the ciphertext.
Enable confidential computing and set the master encryption key.
-- Enable confidential computing. set spark.adb.tee.enabled=true; -- Set the master encryption key. This must match the MEK specified in the encryption configuration file. set spark.adb.tee.encryption.key=00112233445566778899aabbccdd****; -- Create a database to store the ciphertext and the computation results. CREATE DATABASE IF NOT EXISTS test_db;Use the fully encrypted engine to create the
customerexternal table. LOCATION is the OSS path to the ciphertext dataset folder. An example isoss://testBucketName/adb/Spark/customer.set spark.adb.tee.enabled=true; set spark.adb.tee.encryption.key=00112233445566778899aabbccdd****; CREATE TABLE IF NOT EXISTS test_db.customer USING EncBlocksSource LOCATION 'oss://testBucketName/adb/Spark/customer';Create the external table
customer_outputto write the results of the SQL query on thecustomertable, storing the data inoss://testBucketName/adb/Spark/customer_output. Thecustomer_outputexternal table stores the results of the SQL query on thecustomertable.set spark.adb.tee.enabled=true; set spark.adb.tee.encryption.key=00112233445566778899aabbccdd****; CREATE TABLE IF NOT EXISTS test_db.customer_output USING EncBlocksSource LOCATION 'oss://testBucketName/adb/Spark/customer_output' AS SELECT * FROM test_db.customer WHERE id = 1;
Download and decrypt the ciphertext results
Download the encrypted result from the OSS path
oss://testBucketName/adb/Spark/customer_outputto your local machine. For detailed instructions, see Download files.Prepare a decryption configuration file. The following example is a new configuration file named
decryption_config.json.{ "dataDir": "file:///tmp", "table": [ "customer_output" ], "encAlgo": "AES_128_GCM", "outTblPrefix": "abc_", "mek": "00112233445566778899aabbccdd****", "jdbcUrl": "jdbc:mysql://rm-bp1487t8613zz****.mysql.rds.aliyuncs.com:3306/adb", "username": "demo_user", "password": "demo_password", "saveMode": "Overwrite", "format":"EncBlocksSource" }The following table describes the parameters:
Parameter
Description
dataDirThe local path where the ciphertext dataset is stored.
tableThe name of the folder that contains the ciphertext dataset.
encAlgoThe encryption and decryption algorithm for the ciphertext. Only AES_128_GCM is supported.
outTblPrefixThe prefix for the destination table that stores the decrypted data. The final table name is created by combining this prefix with the folder name of the ciphertext dataset.
mekThe master encryption key (MEK) is used to encrypt a data encryption key (DEK). For more information, see Key introduction.
The master encryption key must be a 16-byte hexadecimal string. You can generate the key by using the OpenSSL tool. An example is
00112233445566778899aabbccdd****.WarningThe master encryption key is the root credential for accessing encrypted data. If you lose the key, you can no longer access the data. You must keep the master encryption key secure.
The confidential engine does not provide a service for generating or backing up master encryption keys. You must enter a master encryption key in the required format.
jdbcUrlThe JDBC URL and name of the database. Only MySQL and PostgreSQL databases are supported. The JDBC URL must be in one of the following formats:
MySQL database:
jdbc:mysql://ip:port/database_namePostgreSQL database:
jdbc:postgresql://ip:port/database_name
In this example, the JDBC URL is
jdbc:mysql://rm-bp1487t8613zz****.mysql.rds.aliyuncs.com:3306/adb.userNameThe database account.
passwordThe password of the database account.
saveModeSpecifies the action to take if the destination table for the decrypted data already exists. Valid values:
Append: Appends data.
ErrorIfExists: Reports an error and exits if the destination exists. You can view the error information in the logs.
Ignore: Aborts the write operation if the destination exists. You cannot view error information in the logs.
Overwrite: Overwrites existing data.
formatThe format of the ciphertext. Only EncBlocksSource is supported.
When you run client-side decryption, the decrypted data is automatically inserted into the
adbdatabase in RDS MySQL. The name of the destination table is determined by the folder name of the encrypted dataset and the outTblePrefix parameter. In this example, the table is namedadb_customer_output../cryptoTool decrypt decryption_config.jsonQuery the decrypted data in the RDS for MySQL database to view the plaintext results.
SELECT * FROM abc_customer_output;Expected output:
+--------+----------+----------+----------+ | id | name | address | age | +--------+----------+----------+----------+ | 1 | james | beijing | 10 | +--------+----------+----------+----------+