You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by pa...@apache.org on 2021/11/09 13:13:44 UTC
[shardingsphere] branch master updated: add generics for
EncryptAlgorithm and QueryAssistedEncryptAlgorithm interfaces (#13516)
This is an automated email from the ASF dual-hosted git repository.
panjuan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 217632c add generics for EncryptAlgorithm and QueryAssistedEncryptAlgorithm interfaces (#13516)
217632c is described below
commit 217632ca459cd4d7ad60b0010bbf07550b99473d
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Tue Nov 9 21:13:01 2021 +0800
add generics for EncryptAlgorithm and QueryAssistedEncryptAlgorithm interfaces (#13516)
---
.../TestQueryAssistedShardingEncryptAlgorithm.java | 8 +--
.../encrypt/spi/EncryptAlgorithm.java | 17 +++--
.../encrypt/spi/QueryAssistedEncryptAlgorithm.java | 11 +--
.../encrypt/algorithm/AESEncryptAlgorithm.java | 14 ++--
.../encrypt/algorithm/MD5EncryptAlgorithm.java | 12 ++--
.../encrypt/algorithm/RC4EncryptAlgorithm.java | 14 ++--
.../encrypt/merge/dql/EncryptMergedResult.java | 5 +-
...OnDuplicateKeyUpdateValueParameterRewriter.java | 15 ++--
.../impl/EncryptInsertValueParameterRewriter.java | 7 +-
.../shardingsphere/encrypt/rule/EncryptRule.java | 8 ++-
.../algorithm/CustomizedEncryptAlgorithmTest.java | 54 ++++++++++++++
.../fixture/CustomizedEncryptAlgorithm.java | 84 ++++++++++++++++++++++
.../encrypt/fixture/TestEncryptAlgorithm.java | 6 +-
.../fixture/TestQueryAssistedEncryptAlgorithm.java | 8 +--
.../encrypt/merge/dql/EncryptMergedResultTest.java | 2 +-
...che.shardingsphere.encrypt.spi.EncryptAlgorithm | 1 +
.../driver/fixture/TestEncryptAlgorithm.java | 6 +-
.../fixture/TestQueryAssistedEncryptAlgorithm.java | 8 +--
.../fixture/NormalEncryptAlgorithmFixture.java | 10 +--
.../QueryAssistedEncryptAlgorithmFixture.java | 14 ++--
20 files changed, 229 insertions(+), 75 deletions(-)
diff --git a/examples/example-core/config-utility/src/main/java/org/apache/shardingsphere/example/fixture/TestQueryAssistedShardingEncryptAlgorithm.java b/examples/example-core/config-utility/src/main/java/org/apache/shardingsphere/example/fixture/TestQueryAssistedShardingEncryptAlgorithm.java
index b196297..c749f83 100644
--- a/examples/example-core/config-utility/src/main/java/org/apache/shardingsphere/example/fixture/TestQueryAssistedShardingEncryptAlgorithm.java
+++ b/examples/example-core/config-utility/src/main/java/org/apache/shardingsphere/example/fixture/TestQueryAssistedShardingEncryptAlgorithm.java
@@ -22,7 +22,7 @@ import lombok.Getter;
import lombok.Setter;
import java.util.Properties;
-public final class TestQueryAssistedShardingEncryptAlgorithm implements QueryAssistedEncryptAlgorithm {
+public final class TestQueryAssistedShardingEncryptAlgorithm implements QueryAssistedEncryptAlgorithm<Object, String> {
@Getter
@Setter
@@ -33,17 +33,17 @@ public final class TestQueryAssistedShardingEncryptAlgorithm implements QueryAss
}
@Override
- public String encrypt(final Object plaintext) {
+ public String encrypt(final Object plainValue) {
return "encryptValue";
}
@Override
- public Object decrypt(final String ciphertext) {
+ public Object decrypt(final String cipherValue) {
return "decryptValue";
}
@Override
- public String queryAssistedEncrypt(final Object plaintext) {
+ public String queryAssistedEncrypt(final Object plainValue) {
return "assistedEncryptValue";
}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-api/src/main/java/org/apache/shardingsphere/encrypt/spi/EncryptAlgorithm.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-api/src/main/java/org/apache/shardingsphere/encrypt/spi/EncryptAlgorithm.java
index a76062e..40e0fbf 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-api/src/main/java/org/apache/shardingsphere/encrypt/spi/EncryptAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-api/src/main/java/org/apache/shardingsphere/encrypt/spi/EncryptAlgorithm.java
@@ -22,22 +22,25 @@ import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmP
/**
* Encrypt algorithm for SPI.
+ *
+ * @param <I> type of plain value
+ * @param <O> type of cipher value
*/
-public interface EncryptAlgorithm extends ShardingSphereAlgorithm, ShardingSphereAlgorithmPostProcessor {
+public interface EncryptAlgorithm<I, O> extends ShardingSphereAlgorithm, ShardingSphereAlgorithmPostProcessor {
/**
* Encode.
*
- * @param plaintext plaintext
- * @return ciphertext
+ * @param plainValue plain value
+ * @return cipher value
*/
- String encrypt(Object plaintext);
+ O encrypt(I plainValue);
/**
* Decode.
*
- * @param ciphertext ciphertext
- * @return plaintext
+ * @param cipherValue cipher value
+ * @return plain value
*/
- Object decrypt(String ciphertext);
+ I decrypt(O cipherValue);
}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-api/src/main/java/org/apache/shardingsphere/encrypt/spi/QueryAssistedEncryptAlgorithm.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-api/src/main/java/org/apache/shardingsphere/encrypt/spi/QueryAssistedEncryptAlgorithm.java
index 0dbd84d..a35e7bb 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-api/src/main/java/org/apache/shardingsphere/encrypt/spi/QueryAssistedEncryptAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-api/src/main/java/org/apache/shardingsphere/encrypt/spi/QueryAssistedEncryptAlgorithm.java
@@ -19,14 +19,17 @@ package org.apache.shardingsphere.encrypt.spi;
/**
* Query assisted encrypt algorithm for SPI.
+ *
+ * @param <I> type of plain value
+ * @param <O> type of cipher value
*/
-public interface QueryAssistedEncryptAlgorithm extends EncryptAlgorithm {
+public interface QueryAssistedEncryptAlgorithm<I, O> extends EncryptAlgorithm<I, O> {
/**
* Query assisted encrypt.
*
- * @param plaintext plaintext
- * @return ciphertext
+ * @param plainValue plain value
+ * @return cipher value
*/
- String queryAssistedEncrypt(Object plaintext);
+ O queryAssistedEncrypt(I plainValue);
}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/AESEncryptAlgorithm.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/AESEncryptAlgorithm.java
index fba8dc7..d09b4d4 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/AESEncryptAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/AESEncryptAlgorithm.java
@@ -40,7 +40,7 @@ import java.util.Properties;
*/
@Getter
@Setter
-public final class AESEncryptAlgorithm implements EncryptAlgorithm {
+public final class AESEncryptAlgorithm implements EncryptAlgorithm<Object, String> {
private static final String AES_KEY = "aes-key-value";
@@ -60,21 +60,21 @@ public final class AESEncryptAlgorithm implements EncryptAlgorithm {
@SneakyThrows(GeneralSecurityException.class)
@Override
- public String encrypt(final Object plaintext) {
- if (null == plaintext) {
+ public String encrypt(final Object plainValue) {
+ if (null == plainValue) {
return null;
}
- byte[] result = getCipher(Cipher.ENCRYPT_MODE).doFinal(String.valueOf(plaintext).getBytes(StandardCharsets.UTF_8));
+ byte[] result = getCipher(Cipher.ENCRYPT_MODE).doFinal(String.valueOf(plainValue).getBytes(StandardCharsets.UTF_8));
return DatatypeConverter.printBase64Binary(result);
}
@SneakyThrows(GeneralSecurityException.class)
@Override
- public Object decrypt(final String ciphertext) {
- if (null == ciphertext) {
+ public Object decrypt(final String cipherValue) {
+ if (null == cipherValue) {
return null;
}
- byte[] result = getCipher(Cipher.DECRYPT_MODE).doFinal(DatatypeConverter.parseBase64Binary(ciphertext));
+ byte[] result = getCipher(Cipher.DECRYPT_MODE).doFinal(DatatypeConverter.parseBase64Binary(cipherValue));
return new String(result, StandardCharsets.UTF_8);
}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/MD5EncryptAlgorithm.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/MD5EncryptAlgorithm.java
index 757cc17..7ddf835 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/MD5EncryptAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/MD5EncryptAlgorithm.java
@@ -29,7 +29,7 @@ import java.util.Properties;
*/
@Getter
@Setter
-public final class MD5EncryptAlgorithm implements EncryptAlgorithm {
+public final class MD5EncryptAlgorithm implements EncryptAlgorithm<Object, String> {
private Properties props = new Properties();
@@ -38,16 +38,16 @@ public final class MD5EncryptAlgorithm implements EncryptAlgorithm {
}
@Override
- public String encrypt(final Object plaintext) {
- if (null == plaintext) {
+ public String encrypt(final Object plainValue) {
+ if (null == plainValue) {
return null;
}
- return DigestUtils.md5Hex(String.valueOf(plaintext));
+ return DigestUtils.md5Hex(String.valueOf(plainValue));
}
@Override
- public Object decrypt(final String ciphertext) {
- return ciphertext;
+ public Object decrypt(final String cipherValue) {
+ return cipherValue;
}
@Override
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/RC4EncryptAlgorithm.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/RC4EncryptAlgorithm.java
index 0040d86..e2254ee 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/RC4EncryptAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/RC4EncryptAlgorithm.java
@@ -31,7 +31,7 @@ import java.util.Properties;
/**
* RC4 encrypt algorithm.
*/
-public final class RC4EncryptAlgorithm implements EncryptAlgorithm {
+public final class RC4EncryptAlgorithm implements EncryptAlgorithm<Object, String> {
private static final String RC4_KEY = "rc4-key-value";
@@ -54,20 +54,20 @@ public final class RC4EncryptAlgorithm implements EncryptAlgorithm {
}
@Override
- public String encrypt(final Object plaintext) {
- if (null == plaintext) {
+ public String encrypt(final Object plainValue) {
+ if (null == plainValue) {
return null;
}
- byte[] result = handle(StringUtils.getBytesUtf8(String.valueOf(plaintext)), key);
+ byte[] result = handle(StringUtils.getBytesUtf8(String.valueOf(plainValue)), key);
return Base64.encodeBase64String(result);
}
@Override
- public Object decrypt(final String ciphertext) {
- if (null == ciphertext) {
+ public Object decrypt(final String cipherValue) {
+ if (null == cipherValue) {
return null;
}
- byte[] result = handle(Base64.decodeBase64(ciphertext), key);
+ byte[] result = handle(Base64.decodeBase64(cipherValue), key);
return new String(result, StandardCharsets.UTF_8);
}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResult.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResult.java
index 7256bf6..f233b08 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResult.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResult.java
@@ -43,6 +43,7 @@ public final class EncryptMergedResult implements MergedResult {
return mergedResult.next();
}
+ @SuppressWarnings({"rawtypes", "unchecked"})
@Override
public Object getValue(final int columnIndex, final Class<?> type) throws SQLException {
if (!queryWithCipherColumn) {
@@ -52,8 +53,8 @@ public final class EncryptMergedResult implements MergedResult {
if (!encryptAlgorithm.isPresent()) {
return mergedResult.getValue(columnIndex, type);
}
- String ciphertext = (String) mergedResult.getValue(columnIndex, String.class);
- return null == ciphertext ? null : encryptAlgorithm.get().decrypt(ciphertext);
+ Object cipherValue = mergedResult.getValue(columnIndex, Object.class);
+ return null == cipherValue ? null : encryptAlgorithm.get().decrypt(cipherValue);
}
@Override
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptInsertOnDuplicateKeyUpdateValueParameterRewriter.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptInsertOnDuplicateKeyUpdateValueParameterRewriter.java
index 05568b3..504167e 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptInsertOnDuplicateKeyUpdateValueParameterRewriter.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptInsertOnDuplicateKeyUpdateValueParameterRewriter.java
@@ -22,11 +22,11 @@ import lombok.Setter;
import org.apache.shardingsphere.encrypt.rewrite.parameter.EncryptParameterRewriter;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.encrypt.spi.QueryAssistedEncryptAlgorithm;
-import org.apache.shardingsphere.infra.rewrite.parameter.builder.ParameterBuilder;
-import org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.GroupedParameterBuilder;
import org.apache.shardingsphere.infra.binder.segment.insert.values.OnDuplicateUpdateContext;
import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
+import org.apache.shardingsphere.infra.rewrite.parameter.builder.ParameterBuilder;
+import org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.GroupedParameterBuilder;
import org.apache.shardingsphere.sql.parser.sql.dialect.handler.dml.InsertStatementHandler;
import java.util.Collection;
@@ -46,6 +46,7 @@ public final class EncryptInsertOnDuplicateKeyUpdateValueParameterRewriter exten
&& InsertStatementHandler.getOnDuplicateKeyColumnsSegment(((InsertStatementContext) sqlStatementContext).getSqlStatement()).isPresent();
}
+ @SuppressWarnings({"rawtypes", "unchecked"})
@Override
public void rewrite(final ParameterBuilder parameterBuilder, final InsertStatementContext insertStatementContext, final List<Object> parameters) {
String tableName = insertStatementContext.getSqlStatement().getTable().getTableName().getIdentifier().getValue();
@@ -55,16 +56,16 @@ public final class EncryptInsertOnDuplicateKeyUpdateValueParameterRewriter exten
for (int index = 0; index < onDuplicateKeyUpdateValueContext.getValueExpressions().size(); index++) {
int columnIndex = index;
String encryptLogicColumnName = onDuplicateKeyUpdateValueContext.getColumn(columnIndex).getIdentifier().getValue();
- Optional<EncryptAlgorithm> encryptorOptional = getEncryptRule().findEncryptor(schemaName, tableName, encryptLogicColumnName);
- encryptorOptional.ifPresent(encryptor -> {
+ Optional<EncryptAlgorithm> encryptor = getEncryptRule().findEncryptor(schemaName, tableName, encryptLogicColumnName);
+ encryptor.ifPresent(optional -> {
Object plainColumnValue = onDuplicateKeyUpdateValueContext.getValue(columnIndex);
- Object cipherColumnValue = encryptorOptional.get().encrypt(plainColumnValue);
+ Object cipherColumnValue = encryptor.get().encrypt(plainColumnValue);
groupedParameterBuilder.getGenericParameterBuilder().addReplacedParameters(columnIndex, cipherColumnValue);
Collection<Object> addedParameters = new LinkedList<>();
- if (encryptor instanceof QueryAssistedEncryptAlgorithm) {
+ if (optional instanceof QueryAssistedEncryptAlgorithm) {
Optional<String> assistedColumnName = getEncryptRule().findAssistedQueryColumn(tableName, encryptLogicColumnName);
Preconditions.checkArgument(assistedColumnName.isPresent(), "Can not find assisted query Column Name");
- addedParameters.add(((QueryAssistedEncryptAlgorithm) encryptor).queryAssistedEncrypt(plainColumnValue));
+ addedParameters.add(((QueryAssistedEncryptAlgorithm) optional).queryAssistedEncrypt(plainColumnValue));
}
if (getEncryptRule().findPlainColumn(tableName, encryptLogicColumnName).isPresent()) {
addedParameters.add(plainColumnValue);
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptInsertValueParameterRewriter.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptInsertValueParameterRewriter.java
index e99bf33..41b7610 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptInsertValueParameterRewriter.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/impl/EncryptInsertValueParameterRewriter.java
@@ -21,11 +21,11 @@ import com.google.common.base.Preconditions;
import org.apache.shardingsphere.encrypt.rewrite.parameter.EncryptParameterRewriter;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.encrypt.spi.QueryAssistedEncryptAlgorithm;
+import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
+import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.infra.rewrite.parameter.builder.ParameterBuilder;
import org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.GroupedParameterBuilder;
import org.apache.shardingsphere.infra.rewrite.parameter.builder.impl.StandardParameterBuilder;
-import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext;
-import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.ExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.common.segment.dml.expr.simple.ParameterMarkerExpressionSegment;
import org.apache.shardingsphere.sql.parser.sql.dialect.handler.dml.InsertStatementHandler;
@@ -89,7 +89,8 @@ public final class EncryptInsertValueParameterRewriter extends EncryptParameterR
}
return columnNames.indexOf(encryptLogicColumnName);
}
-
+
+ @SuppressWarnings({"rawtypes", "unchecked"})
private void encryptInsertValue(final EncryptAlgorithm encryptAlgorithm, final String tableName, final int parameterIndex,
final Object originalValue, final StandardParameterBuilder parameterBuilder, final String encryptLogicColumnName) {
parameterBuilder.addReplacedParameters(parameterIndex, encryptAlgorithm.encrypt(originalValue));
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
index 0ab350f..90fa46c 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java
@@ -54,6 +54,7 @@ public final class EncryptRule implements SchemaRule, TableContainedRule {
ShardingSphereServiceLoader.register(EncryptAlgorithm.class);
}
+ @SuppressWarnings("rawtypes")
private final Map<String, EncryptAlgorithm> encryptors = new LinkedHashMap<>();
private final Map<String, EncryptTable> tables = new LinkedHashMap<>();
@@ -135,6 +136,7 @@ public final class EncryptRule implements SchemaRule, TableContainedRule {
* @param logicColumn logic column name
* @return encryptor
*/
+ @SuppressWarnings("rawtypes")
public Optional<EncryptAlgorithm> findEncryptor(final String logicTable, final String logicColumn) {
return tables.containsKey(logicTable) ? tables.get(logicTable).findEncryptorName(logicColumn).map(encryptors::get) : Optional.empty();
}
@@ -147,6 +149,7 @@ public final class EncryptRule implements SchemaRule, TableContainedRule {
* @param logicColumn logic column name
* @return encryptor
*/
+ @SuppressWarnings("rawtypes")
public Optional<EncryptAlgorithm> findEncryptor(final String schemaName, final String logicTable, final String logicColumn) {
if (!tables.containsKey(logicTable)) {
return Optional.empty();
@@ -156,6 +159,7 @@ public final class EncryptRule implements SchemaRule, TableContainedRule {
return encryptAlgorithm;
}
+ @SuppressWarnings("rawtypes")
private void mergeProps(final EncryptAlgorithm encryptAlgorithm, final Properties encryptProperties) {
Properties props = encryptAlgorithm.getProps();
props.putAll(encryptProperties);
@@ -171,10 +175,11 @@ public final class EncryptRule implements SchemaRule, TableContainedRule {
* @param originalValues original values
* @return encrypt values
*/
+ @SuppressWarnings({"rawtypes", "unchecked"})
public List<Object> getEncryptValues(final String schemaName, final String logicTable, final String logicColumn, final List<Object> originalValues) {
Optional<EncryptAlgorithm> encryptor = findEncryptor(schemaName, logicTable, logicColumn);
Preconditions.checkArgument(encryptor.isPresent(), "Can not find EncryptAlgorithm by %s.%s.", logicTable, logicColumn);
- return originalValues.stream().map(input -> null == input ? null : String.valueOf(encryptor.get().encrypt(input.toString()))).collect(Collectors.toList());
+ return originalValues.stream().map(input -> null == input ? null : encryptor.get().encrypt(input)).collect(Collectors.toList());
}
/**
@@ -228,6 +233,7 @@ public final class EncryptRule implements SchemaRule, TableContainedRule {
* @param originalValues original values
* @return assisted query values
*/
+ @SuppressWarnings({"rawtypes", "unchecked"})
public List<Object> getEncryptAssistedQueryValues(final String schemaName, final String logicTable, final String logicColumn, final List<Object> originalValues) {
Optional<EncryptAlgorithm> encryptor = findEncryptor(schemaName, logicTable, logicColumn);
Preconditions.checkArgument(encryptor.isPresent() && encryptor.get() instanceof QueryAssistedEncryptAlgorithm,
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/CustomizedEncryptAlgorithmTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/CustomizedEncryptAlgorithmTest.java
new file mode 100644
index 0000000..61e7d1e
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/CustomizedEncryptAlgorithmTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.shardingsphere.encrypt.algorithm;
+
+import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
+import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmConfiguration;
+import org.apache.shardingsphere.infra.config.algorithm.ShardingSphereAlgorithmFactory;
+import org.apache.shardingsphere.spi.ShardingSphereServiceLoader;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class CustomizedEncryptAlgorithmTest {
+
+ static {
+ ShardingSphereServiceLoader.register(EncryptAlgorithm.class);
+ }
+
+ private EncryptAlgorithm<Integer, Integer> encryptAlgorithm;
+
+ @Before
+ public void setUp() {
+ encryptAlgorithm = ShardingSphereAlgorithmFactory.createAlgorithm(new ShardingSphereAlgorithmConfiguration("CUSTOMIZED", new Properties()), EncryptAlgorithm.class);
+ }
+
+ @Test
+ public void assertEncrypt() {
+ assertThat(encryptAlgorithm.encrypt(1000), is(1765228022));
+ }
+
+ @Test
+ public void assertDecrypt() {
+ assertThat(encryptAlgorithm.decrypt(1765228022), is(1000));
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/fixture/CustomizedEncryptAlgorithm.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/fixture/CustomizedEncryptAlgorithm.java
new file mode 100644
index 0000000..561390c
--- /dev/null
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/fixture/CustomizedEncryptAlgorithm.java
@@ -0,0 +1,84 @@
+/*
+ * 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.shardingsphere.encrypt.fixture;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
+
+import java.util.Properties;
+
+/**
+ * Customized encrypt algorithm.
+ */
+@Getter
+@Setter
+public final class CustomizedEncryptAlgorithm implements EncryptAlgorithm<Integer, Integer> {
+
+ private static final String TEST_KEY = "TEST";
+
+ private Properties props = new Properties();
+
+ private byte[] key = DigestUtils.sha256(TEST_KEY);
+
+ @Override
+ public void init() {
+ }
+
+ @Override
+ public Integer encrypt(final Integer plainValue) {
+ byte[] bytes = toBytes(plainValue);
+ for (int index = 0; index < 32; index++) {
+ bytes[index % 4] = (byte) (key[index] ^ bytes[index % 4]);
+ }
+ return toInt(bytes);
+ }
+
+ @Override
+ public Integer decrypt(final Integer cipherValue) {
+ byte[] bytes = toBytes(cipherValue);
+ for (int index = 0; index < 32; index++) {
+ bytes[index % 4] = (byte) (key[index] ^ bytes[index % 4]);
+ }
+ return toInt(bytes);
+ }
+
+ @Override
+ public String getType() {
+ return "CUSTOMIZED";
+ }
+
+ private int toInt(final byte[] bytes) {
+ int result = 0;
+ for (int index = 0; index < 4; index++) {
+ result <<= 8;
+ result |= bytes[index] & 0xff;
+ }
+ return result;
+ }
+
+ private byte[] toBytes(final int intValue) {
+ byte[] result = new byte[4];
+ result[0] = (byte) (intValue >>> 24);
+ result[1] = (byte) (intValue >>> 16);
+ result[2] = (byte) (intValue >>> 8);
+ result[3] = (byte) intValue;
+ return result;
+ }
+}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/fixture/TestEncryptAlgorithm.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/fixture/TestEncryptAlgorithm.java
index 0b0380e..25e4a6a 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/fixture/TestEncryptAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/fixture/TestEncryptAlgorithm.java
@@ -19,19 +19,19 @@ package org.apache.shardingsphere.encrypt.fixture;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
-public final class TestEncryptAlgorithm implements EncryptAlgorithm {
+public final class TestEncryptAlgorithm implements EncryptAlgorithm<Object, String> {
@Override
public void init() {
}
@Override
- public String encrypt(final Object plaintext) {
+ public String encrypt(final Object plainValue) {
return "encryptValue";
}
@Override
- public Object decrypt(final String ciphertext) {
+ public Object decrypt(final String cipherValue) {
return "decryptValue";
}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/fixture/TestQueryAssistedEncryptAlgorithm.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/fixture/TestQueryAssistedEncryptAlgorithm.java
index 4046537..0cf0c73 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/fixture/TestQueryAssistedEncryptAlgorithm.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/fixture/TestQueryAssistedEncryptAlgorithm.java
@@ -19,24 +19,24 @@ package org.apache.shardingsphere.encrypt.fixture;
import org.apache.shardingsphere.encrypt.spi.QueryAssistedEncryptAlgorithm;
-public final class TestQueryAssistedEncryptAlgorithm implements QueryAssistedEncryptAlgorithm {
+public final class TestQueryAssistedEncryptAlgorithm implements QueryAssistedEncryptAlgorithm<Object, String> {
@Override
public void init() {
}
@Override
- public String encrypt(final Object plaintext) {
+ public String encrypt(final Object plainValue) {
return "encryptValue";
}
@Override
- public Object decrypt(final String ciphertext) {
+ public Object decrypt(final String cipherValue) {
return "decryptValue";
}
@Override
- public String queryAssistedEncrypt(final Object plaintext) {
+ public String queryAssistedEncrypt(final Object plainValue) {
return "assistedEncryptValue";
}
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResultTest.java b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResultTest.java
index 57bb741..02ab8a1 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResultTest.java
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResultTest.java
@@ -66,7 +66,7 @@ public final class EncryptMergedResultTest {
@Test
public void assertGetValueWithQueryWithCipherColumnAndMatchedEncryptorWithNotNullCiphertext() throws SQLException {
- when(mergedResult.getValue(1, String.class)).thenReturn("VALUE");
+ when(mergedResult.getValue(1, Object.class)).thenReturn("VALUE");
EncryptAlgorithm encryptAlgorithm = mock(EncryptAlgorithm.class);
when(encryptAlgorithm.decrypt("VALUE")).thenReturn("ORIGINAL_VALUE");
when(metaData.findEncryptor(1)).thenReturn(Optional.of(encryptAlgorithm));
diff --git a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/resources/META-INF/services/org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/resources/META-INF/services/org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm
index 19bba7f..bea1e42 100644
--- a/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/resources/META-INF/services/org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm
+++ b/shardingsphere-features/shardingsphere-encrypt/shardingsphere-encrypt-core/src/test/resources/META-INF/services/org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm
@@ -17,3 +17,4 @@
org.apache.shardingsphere.encrypt.fixture.TestEncryptAlgorithm
org.apache.shardingsphere.encrypt.fixture.TestQueryAssistedEncryptAlgorithm
+org.apache.shardingsphere.encrypt.fixture.CustomizedEncryptAlgorithm
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/fixture/TestEncryptAlgorithm.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/fixture/TestEncryptAlgorithm.java
index 122b7bb..ac05eff 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/fixture/TestEncryptAlgorithm.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/fixture/TestEncryptAlgorithm.java
@@ -19,19 +19,19 @@ package org.apache.shardingsphere.driver.fixture;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
-public final class TestEncryptAlgorithm implements EncryptAlgorithm {
+public final class TestEncryptAlgorithm implements EncryptAlgorithm<Object, String> {
@Override
public void init() {
}
@Override
- public String encrypt(final Object plaintext) {
+ public String encrypt(final Object plainValue) {
return "encryptValue";
}
@Override
- public Object decrypt(final String ciphertext) {
+ public Object decrypt(final String cipherValue) {
return "decryptValue";
}
diff --git a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/fixture/TestQueryAssistedEncryptAlgorithm.java b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/fixture/TestQueryAssistedEncryptAlgorithm.java
index c3b3af5..303375d 100644
--- a/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/fixture/TestQueryAssistedEncryptAlgorithm.java
+++ b/shardingsphere-jdbc/shardingsphere-jdbc-core/src/test/java/org/apache/shardingsphere/driver/fixture/TestQueryAssistedEncryptAlgorithm.java
@@ -19,24 +19,24 @@ package org.apache.shardingsphere.driver.fixture;
import org.apache.shardingsphere.encrypt.spi.QueryAssistedEncryptAlgorithm;
-public final class TestQueryAssistedEncryptAlgorithm implements QueryAssistedEncryptAlgorithm {
+public final class TestQueryAssistedEncryptAlgorithm implements QueryAssistedEncryptAlgorithm<Object, String> {
@Override
public void init() {
}
@Override
- public String encrypt(final Object plaintext) {
+ public String encrypt(final Object plainValue) {
return "encryptValue";
}
@Override
- public Object decrypt(final String ciphertext) {
+ public Object decrypt(final String cipherValue) {
return "decryptValue";
}
@Override
- public String queryAssistedEncrypt(final Object plaintext) {
+ public String queryAssistedEncrypt(final Object plainValue) {
return "assistedEncryptValue";
}
diff --git a/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/fixture/NormalEncryptAlgorithmFixture.java b/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/fixture/NormalEncryptAlgorithmFixture.java
index ec9b245..f315347 100644
--- a/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/fixture/NormalEncryptAlgorithmFixture.java
+++ b/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/fixture/NormalEncryptAlgorithmFixture.java
@@ -19,20 +19,20 @@ package org.apache.shardingsphere.sharding.rewrite.fixture;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
-public final class NormalEncryptAlgorithmFixture implements EncryptAlgorithm {
+public final class NormalEncryptAlgorithmFixture implements EncryptAlgorithm<Object, String> {
@Override
public void init() {
}
@Override
- public String encrypt(final Object plaintext) {
- return "encrypt_" + plaintext;
+ public String encrypt(final Object plainValue) {
+ return "encrypt_" + plainValue;
}
@Override
- public Object decrypt(final String ciphertext) {
- return ciphertext.replaceAll("encrypt_", "");
+ public Object decrypt(final String cipherValue) {
+ return cipherValue.replaceAll("encrypt_", "");
}
@Override
diff --git a/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/fixture/QueryAssistedEncryptAlgorithmFixture.java b/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/fixture/QueryAssistedEncryptAlgorithmFixture.java
index c68448e..c228ded 100644
--- a/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/fixture/QueryAssistedEncryptAlgorithmFixture.java
+++ b/shardingsphere-test/shardingsphere-rewrite-test/src/test/java/org/apache/shardingsphere/sharding/rewrite/fixture/QueryAssistedEncryptAlgorithmFixture.java
@@ -19,25 +19,25 @@ package org.apache.shardingsphere.sharding.rewrite.fixture;
import org.apache.shardingsphere.encrypt.spi.QueryAssistedEncryptAlgorithm;
-public final class QueryAssistedEncryptAlgorithmFixture implements QueryAssistedEncryptAlgorithm {
+public final class QueryAssistedEncryptAlgorithmFixture implements QueryAssistedEncryptAlgorithm<Object, String> {
@Override
public void init() {
}
@Override
- public String encrypt(final Object plaintext) {
- return "encrypt_" + plaintext;
+ public String encrypt(final Object plainValue) {
+ return "encrypt_" + plainValue;
}
@Override
- public Object decrypt(final String ciphertext) {
- return ciphertext.replaceAll("encrypt_", "");
+ public Object decrypt(final String cipherValue) {
+ return cipherValue.replaceAll("encrypt_", "");
}
@Override
- public String queryAssistedEncrypt(final Object plaintext) {
- return "assisted_query_" + plaintext;
+ public String queryAssistedEncrypt(final Object plainValue) {
+ return "assisted_query_" + plainValue;
}
@Override