You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@tsfile.apache.org by "zhujt20 (via GitHub)" <gi...@apache.org> on 2024/09/03 13:40:20 UTC

[PR] Use reflection method to generate encrypt instances [tsfile]

zhujt20 opened a new pull request, #233:
URL: https://github.com/apache/tsfile/pull/233

   Use reflection method instead of direct ways to generate encrypt instances.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@tsfile.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Use reflection method to generate encrypt instances [tsfile]

Posted by "zhujt20 (via GitHub)" <gi...@apache.org>.
zhujt20 commented on code in PR #233:
URL: https://github.com/apache/tsfile/pull/233#discussion_r1810453818


##########
java/tsfile/src/main/java/org/apache/tsfile/encrypt/EncryptUtils.java:
##########
@@ -109,37 +158,61 @@ public static IEncryptor getDefaultEncryptor() {
         md.update("IoTDB is the best".getBytes());
         md.update(TSFileDescriptor.getInstance().getConfig().getEncryptKey().getBytes());
         dataEncryptKey = md.digest();
-      } catch (Exception e1) {
-        throw new EncryptException("md5 function not found while using md5 to generate data key");
+      } catch (Exception e) {
+        throw new EncryptException(
+            "md5 function not found while using md5 to generate data key", e);
       }
     } else {
-      encryptType = EncryptionType.UNENCRYPTED;
+      encryptType = "org.apache.tsfile.encrypt.UNENCRYPTED";
       dataEncryptKey = null;
     }
-    return IEncryptor.getEncryptor(encryptType, dataEncryptKey);
+    try {
+      Class<?> encryptTypeClass = Class.forName(encryptType);
+      java.lang.reflect.Constructor<?> constructor =
+          encryptTypeClass.getDeclaredConstructor(byte[].class);
+      return ((IEncrypt) constructor.newInstance(dataEncryptKey));

Review Comment:
   Actually the constructor here will only be used once.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@tsfile.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Use reflection method to generate encrypt instances [tsfile]

Posted by "zhujt20 (via GitHub)" <gi...@apache.org>.
zhujt20 commented on code in PR #233:
URL: https://github.com/apache/tsfile/pull/233#discussion_r1810460727


##########
java/tsfile/src/main/java/org/apache/tsfile/encrypt/IDecryptor.java:
##########
@@ -20,47 +20,30 @@
 package org.apache.tsfile.encrypt;
 
 import org.apache.tsfile.exception.encrypt.EncryptException;
-import org.apache.tsfile.exception.encrypt.EncryptKeyLengthNotMatchException;
 import org.apache.tsfile.file.metadata.enums.EncryptionType;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.crypto.BadPaddingException;
-import javax.crypto.Cipher;
-import javax.crypto.IllegalBlockSizeException;
-import javax.crypto.NoSuchPaddingException;
-import javax.crypto.spec.IvParameterSpec;
-import javax.crypto.spec.SecretKeySpec;
-
-import java.security.InvalidAlgorithmParameterException;
-import java.security.InvalidKeyException;
-import java.security.NoSuchAlgorithmException;
-import java.util.Arrays;
+import java.lang.reflect.InvocationTargetException;
 
 /** encrypt data according to tsfileconfig. */
 public interface IDecryptor {
 
   static final Logger logger = LoggerFactory.getLogger(IDecryptor.class);
 
-  static IDecryptor getDecryptor(String name, byte[] key) {
-    return getDecryptor(EncryptionType.valueOf(name), key);
-  }
-
-  static IDecryptor getDecryptor(EncryptionType name, byte[] key) {
-    if (name == null) {
-      return new NoDecryptor();
-    }
-    switch (name) {
-      case UNENCRYPTED:
-        return new NoDecryptor();
-      case SM4128:
-        return new SM4128Decryptor(key);
-      case AES128:
-        return new AES128Decryptor(key);
-      default:
-        logger.warn("Unknown encryption type: {}", name);
-        return new NoDecryptor();
+  static IDecryptor getDecryptor(String type, byte[] key) {
+    try {
+      Class<?> encryptClass = Class.forName(type);
+      java.lang.reflect.Constructor<?> constructor =
+          encryptClass.getDeclaredConstructor(byte[].class);
+      return ((IEncrypt) constructor.newInstance(key)).getDecryptor();

Review Comment:
   For decrypt, there may be different kinds of constructors to be used in the future. So maybe storing them is a good idea, but where should I implement this?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@tsfile.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Use reflection method to generate encrypt instances [tsfile]

Posted by "jt2594838 (via GitHub)" <gi...@apache.org>.
jt2594838 commented on code in PR #233:
URL: https://github.com/apache/tsfile/pull/233#discussion_r1810373139


##########
java/tsfile/src/main/java/org/apache/tsfile/encrypt/AES128Encryptor.java:
##########
@@ -0,0 +1,86 @@
+/*
+ * 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.tsfile.encrypt;
+
+import org.apache.tsfile.exception.encrypt.EncryptException;
+import org.apache.tsfile.exception.encrypt.EncryptKeyLengthNotMatchException;
+import org.apache.tsfile.file.metadata.enums.EncryptionType;
+
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+
+import java.security.InvalidAlgorithmParameterException;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+
+public class AES128Encryptor implements IEncryptor {
+  private final Cipher AES;
+
+  private final SecretKeySpec secretKeySpec;
+
+  private final IvParameterSpec ivParameterSpec;
+
+  AES128Encryptor(byte[] key) {
+    if (key.length != 16) {
+      throw new EncryptKeyLengthNotMatchException(16, key.length);
+    }
+    secretKeySpec = new SecretKeySpec(key, "AES");
+    // Create IV parameter
+    ivParameterSpec = new IvParameterSpec(key);
+    try {
+      // Create Cipher instance and initialize it for encryption in CTR mode without padding
+      this.AES = Cipher.getInstance("AES/CTR/NoPadding");
+      AES.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
+    } catch (InvalidAlgorithmParameterException
+        | NoSuchPaddingException
+        | NoSuchAlgorithmException
+        | InvalidKeyException e) {
+      throw new EncryptException("AES128Encryptor init failed ", e);
+    }
+  }
+
+  @Override
+  public byte[] encrypt(byte[] data) {
+    try {
+      return AES.doFinal(data);
+      //      return AES.doFinal(data);

Review Comment:
   Remove, check other places.



##########
java/tsfile/src/main/java/org/apache/tsfile/encrypt/EncryptUtils.java:
##########
@@ -109,37 +158,61 @@ public static IEncryptor getDefaultEncryptor() {
         md.update("IoTDB is the best".getBytes());
         md.update(TSFileDescriptor.getInstance().getConfig().getEncryptKey().getBytes());
         dataEncryptKey = md.digest();
-      } catch (Exception e1) {
-        throw new EncryptException("md5 function not found while using md5 to generate data key");
+      } catch (Exception e) {
+        throw new EncryptException(
+            "md5 function not found while using md5 to generate data key", e);
       }
     } else {
-      encryptType = EncryptionType.UNENCRYPTED;
+      encryptType = "org.apache.tsfile.encrypt.UNENCRYPTED";
       dataEncryptKey = null;
     }
-    return IEncryptor.getEncryptor(encryptType, dataEncryptKey);
+    try {
+      Class<?> encryptTypeClass = Class.forName(encryptType);
+      java.lang.reflect.Constructor<?> constructor =
+          encryptTypeClass.getDeclaredConstructor(byte[].class);
+      return ((IEncrypt) constructor.newInstance(dataEncryptKey));

Review Comment:
   The constructor may be stored as a field to reduce the number of reflection calls.



##########
java/tsfile/src/main/java/org/apache/tsfile/encrypt/AES128Decryptor.java:
##########
@@ -0,0 +1,85 @@
+/*
+ * 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.tsfile.encrypt;
+
+import org.apache.tsfile.exception.encrypt.EncryptException;
+import org.apache.tsfile.exception.encrypt.EncryptKeyLengthNotMatchException;
+import org.apache.tsfile.file.metadata.enums.EncryptionType;
+
+import javax.crypto.BadPaddingException;
+import javax.crypto.Cipher;
+import javax.crypto.IllegalBlockSizeException;
+import javax.crypto.NoSuchPaddingException;
+import javax.crypto.spec.IvParameterSpec;
+import javax.crypto.spec.SecretKeySpec;
+
+import java.security.InvalidAlgorithmParameterException;
+import java.security.InvalidKeyException;
+import java.security.NoSuchAlgorithmException;
+
+public class AES128Decryptor implements IDecryptor {
+  private final Cipher AES;
+
+  private final SecretKeySpec secretKeySpec;
+
+  private final IvParameterSpec ivParameterSpec;
+
+  AES128Decryptor(byte[] key) {
+    if (key.length != 16) {
+      throw new EncryptKeyLengthNotMatchException(16, key.length);
+    }
+    secretKeySpec = new SecretKeySpec(key, "AES");
+    // Create IV parameter
+    ivParameterSpec = new IvParameterSpec(key);
+    try {
+      // Create Cipher instance and initialize it for encryption in CTR mode without padding
+      this.AES = Cipher.getInstance("AES/CTR/NoPadding");
+      AES.init(Cipher.DECRYPT_MODE, secretKeySpec, ivParameterSpec);
+    } catch (InvalidAlgorithmParameterException
+        | NoSuchPaddingException
+        | NoSuchAlgorithmException
+        | InvalidKeyException e) {
+      throw new EncryptException("AES128Decryptor init failed ", e);
+    }
+  }
+
+  @Override
+  public byte[] decrypt(byte[] data) {
+    try {
+      return AES.doFinal(data);
+    } catch (IllegalBlockSizeException | BadPaddingException e) {
+      throw new EncryptException("AES128Decryptor decrypt failed ", e);
+    }
+  }
+
+  @Override
+  public byte[] decrypt(byte[] data, int offset, int size) {
+    //    return decrypt(Arrays.copyOfRange(data, offset, offset + size));

Review Comment:
   Remove



##########
java/tsfile/src/main/java/org/apache/tsfile/read/TsFileSequenceReader.java:
##########
@@ -1721,8 +1721,7 @@ private static ByteBuffer decrypt(IDecryptor decryptor, ByteBuffer buffer) {
       return buffer;
     }
     return ByteBuffer.wrap(
-        decryptor.decrypt(
-            buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining()));
+        decryptor.decrypt(buffer.array(), buffer.position(), buffer.remaining()));

Review Comment:
   Why is buffer.arrayOffset() removed?



##########
java/tsfile/src/main/java/org/apache/tsfile/encrypt/IDecryptor.java:
##########
@@ -20,47 +20,30 @@
 package org.apache.tsfile.encrypt;
 
 import org.apache.tsfile.exception.encrypt.EncryptException;
-import org.apache.tsfile.exception.encrypt.EncryptKeyLengthNotMatchException;
 import org.apache.tsfile.file.metadata.enums.EncryptionType;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.crypto.BadPaddingException;
-import javax.crypto.Cipher;
-import javax.crypto.IllegalBlockSizeException;
-import javax.crypto.NoSuchPaddingException;
-import javax.crypto.spec.IvParameterSpec;
-import javax.crypto.spec.SecretKeySpec;
-
-import java.security.InvalidAlgorithmParameterException;
-import java.security.InvalidKeyException;
-import java.security.NoSuchAlgorithmException;
-import java.util.Arrays;
+import java.lang.reflect.InvocationTargetException;
 
 /** encrypt data according to tsfileconfig. */
 public interface IDecryptor {
 
   static final Logger logger = LoggerFactory.getLogger(IDecryptor.class);
 
-  static IDecryptor getDecryptor(String name, byte[] key) {
-    return getDecryptor(EncryptionType.valueOf(name), key);
-  }
-
-  static IDecryptor getDecryptor(EncryptionType name, byte[] key) {
-    if (name == null) {
-      return new NoDecryptor();
-    }
-    switch (name) {
-      case UNENCRYPTED:
-        return new NoDecryptor();
-      case SM4128:
-        return new SM4128Decryptor(key);
-      case AES128:
-        return new AES128Decryptor(key);
-      default:
-        logger.warn("Unknown encryption type: {}", name);
-        return new NoDecryptor();
+  static IDecryptor getDecryptor(String type, byte[] key) {
+    try {
+      Class<?> encryptClass = Class.forName(type);
+      java.lang.reflect.Constructor<?> constructor =
+          encryptClass.getDeclaredConstructor(byte[].class);
+      return ((IEncrypt) constructor.newInstance(key)).getDecryptor();

Review Comment:
   May try using a concurrent map to store the constructor.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@tsfile.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Use reflection method to generate encrypt instances [tsfile]

Posted by "jt2594838 (via GitHub)" <gi...@apache.org>.
jt2594838 commented on code in PR #233:
URL: https://github.com/apache/tsfile/pull/233#discussion_r1810511092


##########
java/tsfile/src/main/java/org/apache/tsfile/encrypt/IDecryptor.java:
##########
@@ -20,47 +20,30 @@
 package org.apache.tsfile.encrypt;
 
 import org.apache.tsfile.exception.encrypt.EncryptException;
-import org.apache.tsfile.exception.encrypt.EncryptKeyLengthNotMatchException;
 import org.apache.tsfile.file.metadata.enums.EncryptionType;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.crypto.BadPaddingException;
-import javax.crypto.Cipher;
-import javax.crypto.IllegalBlockSizeException;
-import javax.crypto.NoSuchPaddingException;
-import javax.crypto.spec.IvParameterSpec;
-import javax.crypto.spec.SecretKeySpec;
-
-import java.security.InvalidAlgorithmParameterException;
-import java.security.InvalidKeyException;
-import java.security.NoSuchAlgorithmException;
-import java.util.Arrays;
+import java.lang.reflect.InvocationTargetException;
 
 /** encrypt data according to tsfileconfig. */
 public interface IDecryptor {
 
   static final Logger logger = LoggerFactory.getLogger(IDecryptor.class);
 
-  static IDecryptor getDecryptor(String name, byte[] key) {
-    return getDecryptor(EncryptionType.valueOf(name), key);
-  }
-
-  static IDecryptor getDecryptor(EncryptionType name, byte[] key) {
-    if (name == null) {
-      return new NoDecryptor();
-    }
-    switch (name) {
-      case UNENCRYPTED:
-        return new NoDecryptor();
-      case SM4128:
-        return new SM4128Decryptor(key);
-      case AES128:
-        return new AES128Decryptor(key);
-      default:
-        logger.warn("Unknown encryption type: {}", name);
-        return new NoDecryptor();
+  static IDecryptor getDecryptor(String type, byte[] key) {
+    try {
+      Class<?> encryptClass = Class.forName(type);
+      java.lang.reflect.Constructor<?> constructor =
+          encryptClass.getDeclaredConstructor(byte[].class);
+      return ((IEncrypt) constructor.newInstance(key)).getDecryptor();

Review Comment:
   Just like the logger above, add a static member.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@tsfile.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Use reflection method to generate encrypt instances [tsfile]

Posted by "jt2594838 (via GitHub)" <gi...@apache.org>.
jt2594838 merged PR #233:
URL: https://github.com/apache/tsfile/pull/233


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@tsfile.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Use reflection method to generate encrypt instances [tsfile]

Posted by "zhujt20 (via GitHub)" <gi...@apache.org>.
zhujt20 commented on code in PR #233:
URL: https://github.com/apache/tsfile/pull/233#discussion_r1810449782


##########
java/tsfile/src/main/java/org/apache/tsfile/encrypt/IDecryptor.java:
##########
@@ -20,47 +20,30 @@
 package org.apache.tsfile.encrypt;
 
 import org.apache.tsfile.exception.encrypt.EncryptException;
-import org.apache.tsfile.exception.encrypt.EncryptKeyLengthNotMatchException;
 import org.apache.tsfile.file.metadata.enums.EncryptionType;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.crypto.BadPaddingException;
-import javax.crypto.Cipher;
-import javax.crypto.IllegalBlockSizeException;
-import javax.crypto.NoSuchPaddingException;
-import javax.crypto.spec.IvParameterSpec;
-import javax.crypto.spec.SecretKeySpec;
-
-import java.security.InvalidAlgorithmParameterException;
-import java.security.InvalidKeyException;
-import java.security.NoSuchAlgorithmException;
-import java.util.Arrays;
+import java.lang.reflect.InvocationTargetException;
 
 /** encrypt data according to tsfileconfig. */
 public interface IDecryptor {
 
   static final Logger logger = LoggerFactory.getLogger(IDecryptor.class);
 
-  static IDecryptor getDecryptor(String name, byte[] key) {
-    return getDecryptor(EncryptionType.valueOf(name), key);
-  }
-
-  static IDecryptor getDecryptor(EncryptionType name, byte[] key) {
-    if (name == null) {
-      return new NoDecryptor();
-    }
-    switch (name) {
-      case UNENCRYPTED:
-        return new NoDecryptor();
-      case SM4128:
-        return new SM4128Decryptor(key);
-      case AES128:
-        return new AES128Decryptor(key);
-      default:
-        logger.warn("Unknown encryption type: {}", name);
-        return new NoDecryptor();
+  static IDecryptor getDecryptor(String type, byte[] key) {
+    try {
+      Class<?> encryptClass = Class.forName(type);
+      java.lang.reflect.Constructor<?> constructor =
+          encryptClass.getDeclaredConstructor(byte[].class);
+      return ((IEncrypt) constructor.newInstance(key)).getDecryptor();

Review Comment:
    storing the constructor we often use is good, but I don't understand why using a concurrent map to store the constructor and where to implement it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@tsfile.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Use reflection method to generate encrypt instances [tsfile]

Posted by "zhujt20 (via GitHub)" <gi...@apache.org>.
zhujt20 commented on code in PR #233:
URL: https://github.com/apache/tsfile/pull/233#discussion_r1810449782


##########
java/tsfile/src/main/java/org/apache/tsfile/encrypt/IDecryptor.java:
##########
@@ -20,47 +20,30 @@
 package org.apache.tsfile.encrypt;
 
 import org.apache.tsfile.exception.encrypt.EncryptException;
-import org.apache.tsfile.exception.encrypt.EncryptKeyLengthNotMatchException;
 import org.apache.tsfile.file.metadata.enums.EncryptionType;
 
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import javax.crypto.BadPaddingException;
-import javax.crypto.Cipher;
-import javax.crypto.IllegalBlockSizeException;
-import javax.crypto.NoSuchPaddingException;
-import javax.crypto.spec.IvParameterSpec;
-import javax.crypto.spec.SecretKeySpec;
-
-import java.security.InvalidAlgorithmParameterException;
-import java.security.InvalidKeyException;
-import java.security.NoSuchAlgorithmException;
-import java.util.Arrays;
+import java.lang.reflect.InvocationTargetException;
 
 /** encrypt data according to tsfileconfig. */
 public interface IDecryptor {
 
   static final Logger logger = LoggerFactory.getLogger(IDecryptor.class);
 
-  static IDecryptor getDecryptor(String name, byte[] key) {
-    return getDecryptor(EncryptionType.valueOf(name), key);
-  }
-
-  static IDecryptor getDecryptor(EncryptionType name, byte[] key) {
-    if (name == null) {
-      return new NoDecryptor();
-    }
-    switch (name) {
-      case UNENCRYPTED:
-        return new NoDecryptor();
-      case SM4128:
-        return new SM4128Decryptor(key);
-      case AES128:
-        return new AES128Decryptor(key);
-      default:
-        logger.warn("Unknown encryption type: {}", name);
-        return new NoDecryptor();
+  static IDecryptor getDecryptor(String type, byte[] key) {
+    try {
+      Class<?> encryptClass = Class.forName(type);
+      java.lang.reflect.Constructor<?> constructor =
+          encryptClass.getDeclaredConstructor(byte[].class);
+      return ((IEncrypt) constructor.newInstance(key)).getDecryptor();

Review Comment:
    storing the constructor we often use is good, but I don't understand why using a concurrent map to store the constructor and where to implement it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@tsfile.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org