This is an automated email from the ASF dual-hosted git repository.
yiguolei pushed a commit to branch branch-4.1
in repository https://gitbox.apache.org/repos/asf/doris.git
The following commit(s) were added to refs/heads/branch-4.1 by this push:
new 1786adf035e [refactor](tde) Add local root key metadata for TDE
(branch-4.1) (#66948) (#71126)
1786adf035e is described below
commit 1786adf035ec8ffb282beec2bfda7abb30a9a3b6
Author: Luwei <[email protected]>
AuthorDate: Mon Nov 30 21:05:27 2026 +0800
[refactor](tde) Add local root key metadata for TDE (branch-4.1) (#66948)
(#71126)
### What problem does this PR solve?
Issue Number: None
Related PR: #66948
Problem Summary: Backport #66948 to branch-4.1. FE needs a persistent
reference to a file-based TDE root key. Store the file path and SHA-256
digest with the key metadata, and keep the Base64 key in memory. Add the
FE configuration field and both rotation property names used by the
upstream change.
### Details
The release branch recognizes `aws_kms` and `local`. Its configuration
descriptions list those two values in English and Chinese. The optional
fields preserve readability of existing key records. The file path
defaults to an empty string. File loading, SQL privileges and the
rotation implementation retain their current behavior. Digest
calculation is confined to an explicit call on the key. Older readers
can read new records and discard the extra fields if they rewrite them.
### Release note
None
### Testing
- `./run-fe-ut.sh --run org.apache.doris.encryption.RootKeyInfoTest`
passed all four local-key tests. They cover metadata copying, digest
verification, JSON and key-store serialization, and exclusion of the
Base64 key.
- `mvn -pl fe-common,fe-core checkstyle:check` passed for both FE
modules.
- The candidate read a key store written with the baseline metadata
class. The baseline reader also read a key store written with the
candidate class. The stored digest matched an independent SHA-256 test
vector.
---
.../main/java/org/apache/doris/common/Config.java | 10 +-
.../org/apache/doris/encryption/RootKeyInfo.java | 40 +++++++
.../commands/AdminRotateTdeRootKeyCommand.java | 6 ++
.../apache/doris/encryption/RootKeyInfoTest.java | 118 +++++++++++++++++++++
4 files changed, 172 insertions(+), 2 deletions(-)
diff --git a/fe/fe-common/src/main/java/org/apache/doris/common/Config.java
b/fe/fe-common/src/main/java/org/apache/doris/common/Config.java
index ecf15e99583..cbc282656ed 100644
--- a/fe/fe-common/src/main/java/org/apache/doris/common/Config.java
+++ b/fe/fe-common/src/main/java/org/apache/doris/common/Config.java
@@ -4175,11 +4175,17 @@ public class Config extends ConfigBase {
public static String doris_tde_key_region = "";
@ConfField(mutable = true, description = {
- "TDE(透明数据加密)的密钥提供方,目前支持 aws_kms",
- "The key provider for TDE (Transparent Data Encryption), currently
supports aws_kms"
+ "TDE(透明数据加密)的密钥提供方,可选 aws_kms 或 local。使用 local 时须设置
doris_tde_root_key_file。",
+ "The TDE key provider (aws_kms or local). For local mode, set
doris_tde_root_key_file to a key file path."
})
public static String doris_tde_key_provider = "";
+ @ConfField(description = {
+ "TDE 本地根密钥文件的路径,文件内容为 Base64 编码的密钥。",
+ "Path to the root key file for TDE local mode. The file content must
be a Base64-encoded key."
+ })
+ public static String doris_tde_root_key_file = "";
+
@ConfField(mutable = true, description = {
"数据加密所使用的算法,默认 AES256,后续可能置空由 KMS 自动决定",
"The encryption algorithm used for data, default is AES256, may be set
to empty later for KMS to decide"
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/encryption/RootKeyInfo.java
b/fe/fe-core/src/main/java/org/apache/doris/encryption/RootKeyInfo.java
index e2aa248ce2e..797495e7212 100644
--- a/fe/fe-core/src/main/java/org/apache/doris/encryption/RootKeyInfo.java
+++ b/fe/fe-core/src/main/java/org/apache/doris/encryption/RootKeyInfo.java
@@ -19,6 +19,8 @@ package org.apache.doris.encryption;
import com.google.gson.annotations.SerializedName;
+import java.security.MessageDigest;
+import java.util.Base64;
import java.util.Objects;
public class RootKeyInfo {
@@ -55,6 +57,9 @@ public class RootKeyInfo {
this.ak = info.ak;
this.sk = info.sk;
this.password = info.password;
+ this.rootKeyFilePath = info.rootKeyFilePath;
+ this.rootKeyHash = info.rootKeyHash;
+ this.rootKeyBase64 = info.rootKeyBase64;
}
@SerializedName(value = "type")
@@ -80,5 +85,40 @@ public class RootKeyInfo {
@SerializedName(value = "password")
public String password;
+
+ // File path of the local root key. This is a persisted key reference,
similar to cmkId for KMS.
+ @SerializedName(value = "rootKeyFilePath")
+ public String rootKeyFilePath;
+
+ // SHA-256 hash of the root key (Base64 encoded). Used to verify the root
key on FE restart.
+ @SerializedName(value = "rootKeyHash")
+ public String rootKeyHash;
+
+ // Root key in Base64 encoding. Used for passing the key during rotation,
not persisted to the edit log.
+ public transient String rootKeyBase64;
+
+ public void setRootKeyHashFromKey(byte[] rootKeyBytes) {
+ try {
+ MessageDigest digest = MessageDigest.getInstance("SHA-256");
+ byte[] hash = digest.digest(rootKeyBytes);
+ this.rootKeyHash = Base64.getEncoder().encodeToString(hash);
+ } catch (Exception e) {
+ throw new RuntimeException("Failed to compute root key hash", e);
+ }
+ }
+
+ public boolean verifyRootKey(byte[] rootKeyBytes) {
+ if (this.rootKeyHash == null || this.rootKeyHash.isEmpty()) {
+ return true;
+ }
+ try {
+ MessageDigest digest = MessageDigest.getInstance("SHA-256");
+ byte[] hash = digest.digest(rootKeyBytes);
+ String inputHash = Base64.getEncoder().encodeToString(hash);
+ return this.rootKeyHash.equals(inputHash);
+ } catch (Exception e) {
+ throw new RuntimeException("Failed to verify root key", e);
+ }
+ }
}
diff --git
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AdminRotateTdeRootKeyCommand.java
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AdminRotateTdeRootKeyCommand.java
index 469c871f3a9..e9b75c087e8 100644
---
a/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AdminRotateTdeRootKeyCommand.java
+++
b/fe/fe-core/src/main/java/org/apache/doris/nereids/trees/plans/commands/AdminRotateTdeRootKeyCommand.java
@@ -47,6 +47,12 @@ public class AdminRotateTdeRootKeyCommand extends Command
implements ForwardWith
public static final String DORIS_TDE_KEY_REGION = "doris_tde_key_region";
+ // File path containing the new key for local key rotation.
+ public static final String DORIS_TDE_KEY_NEW_KEY_FILE =
"doris_tde_key_new_key_file";
+
+ // File path containing the original key for verification during local key
rotation.
+ public static final String DORIS_TDE_KEY_ORIGINAL_KEY_FILE =
"doris_tde_key_original_key_file";
+
private final Map<String, String> properties;
public AdminRotateTdeRootKeyCommand(Map<String, String> properties) {
diff --git
a/fe/fe-core/src/test/java/org/apache/doris/encryption/RootKeyInfoTest.java
b/fe/fe-core/src/test/java/org/apache/doris/encryption/RootKeyInfoTest.java
new file mode 100644
index 00000000000..de70506694b
--- /dev/null
+++ b/fe/fe-core/src/test/java/org/apache/doris/encryption/RootKeyInfoTest.java
@@ -0,0 +1,118 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements. See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership. The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License. You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied. See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+package org.apache.doris.encryption;
+
+import org.apache.doris.encryption.EncryptionKey.Algorithm;
+import org.apache.doris.encryption.EncryptionKey.KeyType;
+import org.apache.doris.encryption.RootKeyInfo.RootKeyType;
+import org.apache.doris.persist.gson.GsonUtils;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.nio.charset.StandardCharsets;
+import java.util.Base64;
+
+public class RootKeyInfoTest {
+ @Test
+ public void testLocalRootKeyInfoSerialization() {
+ RootKeyInfo rootKeyInfo = createLocalRootKeyInfo();
+
+ String json = GsonUtils.GSON.toJson(rootKeyInfo);
+ Assertions.assertTrue(json.contains("rootKeyFilePath"));
+ Assertions.assertTrue(json.contains("rootKeyHash"));
+ Assertions.assertFalse(json.contains("rootKeyBase64"));
+ Assertions.assertFalse(json.contains(rootKeyInfo.rootKeyBase64));
+
+ RootKeyInfo restored = GsonUtils.GSON.fromJson(json,
RootKeyInfo.class);
+ Assertions.assertEquals(RootKeyType.LOCAL, restored.type);
+ Assertions.assertEquals(rootKeyInfo.rootKeyFilePath,
restored.rootKeyFilePath);
+ Assertions.assertEquals(rootKeyInfo.rootKeyHash, restored.rootKeyHash);
+ Assertions.assertNull(restored.rootKeyBase64);
+ }
+
+ @Test
+ public void testLocalRootKeyHashVerification() {
+ byte[] rootKey = "local-root-key".getBytes(StandardCharsets.UTF_8);
+ RootKeyInfo rootKeyInfo = new RootKeyInfo();
+
+ Assertions.assertTrue(rootKeyInfo.verifyRootKey(rootKey));
+
+ rootKeyInfo.setRootKeyHashFromKey(rootKey);
+ Assertions.assertTrue(rootKeyInfo.verifyRootKey(rootKey));
+
Assertions.assertFalse(rootKeyInfo.verifyRootKey("different-key".getBytes(StandardCharsets.UTF_8)));
+ }
+
+ @Test
+ public void testCopyLocalRootKeyInfo() {
+ RootKeyInfo rootKeyInfo = createLocalRootKeyInfo();
+
+ RootKeyInfo copied = new RootKeyInfo(rootKeyInfo);
+
+ Assertions.assertEquals(rootKeyInfo.rootKeyFilePath,
copied.rootKeyFilePath);
+ Assertions.assertEquals(rootKeyInfo.rootKeyHash, copied.rootKeyHash);
+ Assertions.assertEquals(rootKeyInfo.rootKeyBase64,
copied.rootKeyBase64);
+ }
+
+ @Test
+ public void testKeyManagerStoreSerializationKeepsLocalRootKeyReference()
throws Exception {
+ RootKeyInfo rootKeyInfo = createLocalRootKeyInfo();
+
+ EncryptionKey masterKey = new EncryptionKey();
+ masterKey.id = "1";
+ masterKey.version = 1;
+ masterKey.parentId = "local";
+ masterKey.parentVersion = 1;
+ masterKey.type = KeyType.MASTER_KEY;
+ masterKey.algorithm = Algorithm.AES256;
+ masterKey.ciphertext = "ciphertext";
+ masterKey.crc = 1234;
+
+ KeyManagerStore store = new KeyManagerStore();
+ store.setRootKeyInfo(rootKeyInfo);
+ store.addMasterKey(masterKey);
+
+ ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
+ store.write(new DataOutputStream(byteOutput));
+
+ KeyManagerStore restored = KeyManagerStore.read(new DataInputStream(
+ new ByteArrayInputStream(byteOutput.toByteArray())));
+
+ Assertions.assertEquals(RootKeyType.LOCAL,
restored.getRootKeyInfo().type);
+ Assertions.assertEquals(rootKeyInfo.rootKeyFilePath,
restored.getRootKeyInfo().rootKeyFilePath);
+ Assertions.assertEquals(rootKeyInfo.rootKeyHash,
restored.getRootKeyInfo().rootKeyHash);
+ Assertions.assertNull(restored.getRootKeyInfo().rootKeyBase64);
+ Assertions.assertEquals(1, restored.getMasterKeys().size());
+ Assertions.assertEquals(masterKey.ciphertext,
restored.getMasterKeys().get(0).ciphertext);
+ }
+
+ private RootKeyInfo createLocalRootKeyInfo() {
+ RootKeyInfo rootKeyInfo = new RootKeyInfo();
+ rootKeyInfo.type = RootKeyType.LOCAL;
+ rootKeyInfo.cmkId = "local";
+ rootKeyInfo.rootKeyFilePath =
"/opt/apache-doris/fe/conf/doris_tde_root_key";
+ rootKeyInfo.rootKeyHash = "hash";
+ rootKeyInfo.rootKeyBase64 = Base64.getEncoder().encodeToString(
+ "secret".getBytes(StandardCharsets.UTF_8));
+ return rootKeyInfo;
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]