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 d35977ea54344..0ddab4cd49fd3 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 @@ -18,20 +18,15 @@ package org.apache.shardingsphere.example.fixture; import org.apache.shardingsphere.encrypt.api.context.EncryptContext; -import org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm; +import org.apache.shardingsphere.encrypt.api.encrypt.assisted.AssistedEncryptAlgorithm; -public final class TestQueryAssistedShardingEncryptAlgorithm implements StandardEncryptAlgorithm { +public final class TestQueryAssistedShardingEncryptAlgorithm implements AssistedEncryptAlgorithm { @Override public String encrypt(final Object plainValue, final EncryptContext encryptContext) { return "assistedEncryptValue"; } - @Override - public Object decrypt(final String cipherValue, final EncryptContext encryptContext) { - return "decryptValue"; - } - @Override public String getType() { return "assistedTest"; diff --git a/examples/shardingsphere-example-generator/src/main/resources/template/jdbc/java/TestQueryAssistedShardingEncryptAlgorithm.ftl b/examples/shardingsphere-example-generator/src/main/resources/template/jdbc/java/TestQueryAssistedShardingEncryptAlgorithm.ftl index 59ff2eae00c20..e094e78abc238 100644 --- a/examples/shardingsphere-example-generator/src/main/resources/template/jdbc/java/TestQueryAssistedShardingEncryptAlgorithm.ftl +++ b/examples/shardingsphere-example-generator/src/main/resources/template/jdbc/java/TestQueryAssistedShardingEncryptAlgorithm.ftl @@ -20,12 +20,12 @@ package org.apache.shardingsphere.example.${package}.${framework?replace('-', '. import lombok.Getter; import lombok.Setter; -import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm; import org.apache.shardingsphere.encrypt.api.context.EncryptContext; +import org.apache.shardingsphere.encrypt.api.encrypt.assisted.AssistedEncryptAlgorithm; import java.util.Properties; -public final class TestQueryAssistedShardingEncryptAlgorithm implements EncryptAlgorithm { +public final class TestQueryAssistedShardingEncryptAlgorithm implements AssistedEncryptAlgorithm { @Getter private Properties props; @@ -35,11 +35,6 @@ public final class TestQueryAssistedShardingEncryptAlgorithm implements EncryptA return "assistedEncryptValue"; } - @Override - public Object decrypt(final String cipherValue, final EncryptContext encryptContext) { - return "decryptValue"; - } - @Override public String getType() { return "assistedTest"; diff --git a/features/encrypt/api/src/main/java/org/apache/shardingsphere/encrypt/api/encrypt/assisted/AssistedEncryptAlgorithm.java b/features/encrypt/api/src/main/java/org/apache/shardingsphere/encrypt/api/encrypt/assisted/AssistedEncryptAlgorithm.java new file mode 100644 index 0000000000000..7fc39fa4c8512 --- /dev/null +++ b/features/encrypt/api/src/main/java/org/apache/shardingsphere/encrypt/api/encrypt/assisted/AssistedEncryptAlgorithm.java @@ -0,0 +1,29 @@ +/* + * 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.api.encrypt.assisted; + +import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm; + +/** + * Assisted encrypt algorithm. + * + * @param type of plain value + * @param type of cipher value + */ +public interface AssistedEncryptAlgorithm extends EncryptAlgorithm { +} diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/MD5EncryptAlgorithm.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/assisted/MD5AssistedEncryptAlgorithm.java similarity index 77% rename from features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/MD5EncryptAlgorithm.java rename to features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/assisted/MD5AssistedEncryptAlgorithm.java index 48bdb4e85369f..7e33b7bb1c4e4 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/MD5EncryptAlgorithm.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/assisted/MD5AssistedEncryptAlgorithm.java @@ -15,18 +15,18 @@ * limitations under the License. */ -package org.apache.shardingsphere.encrypt.algorithm.encrypt; +package org.apache.shardingsphere.encrypt.algorithm.assisted; import org.apache.commons.codec.digest.DigestUtils; -import org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm; import org.apache.shardingsphere.encrypt.api.context.EncryptContext; +import org.apache.shardingsphere.encrypt.api.encrypt.assisted.AssistedEncryptAlgorithm; import java.util.Properties; /** - * MD5 encrypt algorithm. + * MD5 assisted encrypt algorithm. */ -public final class MD5EncryptAlgorithm implements StandardEncryptAlgorithm { +public final class MD5AssistedEncryptAlgorithm implements AssistedEncryptAlgorithm { private static final String SALT = "salt"; @@ -42,11 +42,6 @@ public String encrypt(final Object plainValue, final EncryptContext encryptConte return null == plainValue ? null : DigestUtils.md5Hex(plainValue + salt); } - @Override - public Object decrypt(final String cipherValue, final EncryptContext encryptContext) { - return cipherValue; - } - @Override public String getType() { return "MD5"; diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/AESEncryptAlgorithm.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/standard/AESEncryptAlgorithm.java similarity index 98% rename from features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/AESEncryptAlgorithm.java rename to features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/standard/AESEncryptAlgorithm.java index 0bdfb4a8ca921..86f8d3281c7c0 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/AESEncryptAlgorithm.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/standard/AESEncryptAlgorithm.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.encrypt.algorithm.encrypt; +package org.apache.shardingsphere.encrypt.algorithm.standard; import com.google.common.base.Strings; import lombok.SneakyThrows; diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/RC4EncryptAlgorithm.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/standard/RC4EncryptAlgorithm.java similarity index 98% rename from features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/RC4EncryptAlgorithm.java rename to features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/standard/RC4EncryptAlgorithm.java index 901e5944451e2..e03dbe7e9ea69 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/RC4EncryptAlgorithm.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/algorithm/standard/RC4EncryptAlgorithm.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.encrypt.algorithm.encrypt; +package org.apache.shardingsphere.encrypt.algorithm.standard; import org.apache.commons.codec.binary.Base64; import org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm; diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptAlgorithmMetaData.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptAlgorithmMetaData.java index 9b0e2acd508a5..b44fd03998761 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptAlgorithmMetaData.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptAlgorithmMetaData.java @@ -48,14 +48,14 @@ public final class EncryptAlgorithmMetaData { private final SelectStatementContext selectStatementContext; /** - * Find encryptor. + * Find standard encryptor. * * @param tableName table name * @param columnName column name - * @return encryptor + * @return standard encryptor */ - public Optional findEncryptor(final String tableName, final String columnName) { - return encryptRule.findEncryptor(tableName, columnName); + public Optional findStandardEncryptor(final String tableName, final String columnName) { + return encryptRule.findStandardEncryptor(tableName, columnName); } /** @@ -92,7 +92,7 @@ private Optional findTableName(final ColumnProjection columnProjection, return Optional.of(tableName); } for (String each : selectStatementContext.getTablesContext().getTableNames()) { - if (encryptRule.findEncryptor(each, columnProjection.getName()).isPresent()) { + if (encryptRule.findStandardEncryptor(each, columnProjection.getName()).isPresent()) { return Optional.of(each); } } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResult.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResult.java index ff691b15be9bf..6bc4ce2d890c0 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResult.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptMergedResult.java @@ -49,7 +49,7 @@ public Object getValue(final int columnIndex, final Class type) throws SQLExc if (!encryptContext.isPresent()) { return mergedResult.getValue(columnIndex, type); } - Optional encryptAlgorithm = metaData.findEncryptor(encryptContext.get().getTableName(), encryptContext.get().getColumnName()); + Optional encryptAlgorithm = metaData.findStandardEncryptor(encryptContext.get().getTableName(), encryptContext.get().getColumnName()); if (!encryptAlgorithm.isPresent()) { return mergedResult.getValue(columnIndex, type); } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptAssignmentParameterRewriter.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptAssignmentParameterRewriter.java index 769e1410e566e..d52a0744ce300 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptAssignmentParameterRewriter.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptAssignmentParameterRewriter.java @@ -71,7 +71,7 @@ public void rewrite(final ParameterBuilder paramBuilder, final SQLStatementConte String tableName = ((TableAvailable) sqlStatementContext).getAllTables().iterator().next().getTableName().getIdentifier().getValue(); String schemaName = sqlStatementContext.getTablesContext().getSchemaName().orElseGet(() -> DatabaseTypeEngine.getDefaultSchemaName(sqlStatementContext.getDatabaseType(), databaseName)); for (AssignmentSegment each : getSetAssignmentSegment(sqlStatementContext.getSqlStatement()).getAssignments()) { - if (each.getValue() instanceof ParameterMarkerExpressionSegment && encryptRule.findEncryptor(tableName, each.getColumns().get(0).getIdentifier().getValue()).isPresent()) { + if (each.getValue() instanceof ParameterMarkerExpressionSegment && encryptRule.findStandardEncryptor(tableName, each.getColumns().get(0).getIdentifier().getValue()).isPresent()) { StandardParameterBuilder standardParamBuilder = paramBuilder instanceof StandardParameterBuilder ? (StandardParameterBuilder) paramBuilder : ((GroupedParameterBuilder) paramBuilder).getParameterBuilders().get(0); diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptInsertOnDuplicateKeyUpdateValueParameterRewriter.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptInsertOnDuplicateKeyUpdateValueParameterRewriter.java index f74daa13e3966..7b760345cc68c 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptInsertOnDuplicateKeyUpdateValueParameterRewriter.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptInsertOnDuplicateKeyUpdateValueParameterRewriter.java @@ -19,6 +19,7 @@ import com.google.common.base.Preconditions; import lombok.Setter; +import org.apache.shardingsphere.encrypt.api.encrypt.assisted.AssistedEncryptAlgorithm; import org.apache.shardingsphere.encrypt.api.encrypt.like.LikeEncryptAlgorithm; import org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm; import org.apache.shardingsphere.encrypt.context.EncryptContextBuilder; @@ -66,7 +67,7 @@ public void rewrite(final ParameterBuilder paramBuilder, final InsertStatementCo String schemaName = insertStatementContext.getTablesContext().getSchemaName().orElseGet(() -> DatabaseTypeEngine.getDefaultSchemaName(insertStatementContext.getDatabaseType(), databaseName)); for (int index = 0; index < onDuplicateKeyUpdateValueContext.getValueExpressions().size(); index++) { String encryptLogicColumnName = onDuplicateKeyUpdateValueContext.getColumn(index).getIdentifier().getValue(); - Optional encryptor = encryptRule.findEncryptor(tableName, encryptLogicColumnName); + Optional encryptor = encryptRule.findStandardEncryptor(tableName, encryptLogicColumnName); if (!encryptor.isPresent()) { continue; } @@ -90,7 +91,7 @@ public void rewrite(final ParameterBuilder paramBuilder, final InsertStatementCo @SuppressWarnings({"rawtypes", "unchecked"}) private Collection buildAddedParams(final String tableName, final String logicColumnName, final Object plainValue, final EncryptContext encryptContext) { Collection result = new LinkedList<>(); - Optional assistedQueryEncryptor = encryptRule.findAssistedQueryEncryptor(tableName, logicColumnName); + Optional assistedQueryEncryptor = encryptRule.findAssistedQueryEncryptor(tableName, logicColumnName); if (assistedQueryEncryptor.isPresent()) { Optional assistedColumnName = encryptRule.findAssistedQueryColumn(tableName, logicColumnName); Preconditions.checkArgument(assistedColumnName.isPresent(), "Can not find assisted query Column Name"); diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptInsertValueParameterRewriter.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptInsertValueParameterRewriter.java index fbc3889c72e61..8adcedc11fd87 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptInsertValueParameterRewriter.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/parameter/rewriter/EncryptInsertValueParameterRewriter.java @@ -18,6 +18,8 @@ package org.apache.shardingsphere.encrypt.rewrite.parameter.rewriter; import lombok.Setter; +import org.apache.shardingsphere.encrypt.api.context.EncryptContext; +import org.apache.shardingsphere.encrypt.api.encrypt.assisted.AssistedEncryptAlgorithm; import org.apache.shardingsphere.encrypt.api.encrypt.like.LikeEncryptAlgorithm; import org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm; import org.apache.shardingsphere.encrypt.context.EncryptContextBuilder; @@ -26,7 +28,6 @@ import org.apache.shardingsphere.encrypt.rewrite.aware.DatabaseNameAware; import org.apache.shardingsphere.encrypt.rewrite.aware.EncryptRuleAware; import org.apache.shardingsphere.encrypt.rule.EncryptRule; -import org.apache.shardingsphere.encrypt.api.context.EncryptContext; import org.apache.shardingsphere.infra.binder.statement.SQLStatementContext; import org.apache.shardingsphere.infra.binder.statement.dml.InsertStatementContext; import org.apache.shardingsphere.infra.database.type.DatabaseTypeEngine; @@ -70,7 +71,7 @@ public void rewrite(final ParameterBuilder paramBuilder, final InsertStatementCo while (descendingColumnNames.hasNext()) { String columnName = descendingColumnNames.next(); EncryptContext encryptContext = EncryptContextBuilder.build(databaseName, schemaName, tableName, columnName); - encryptRule.findEncryptor(tableName, columnName).ifPresent( + encryptRule.findStandardEncryptor(tableName, columnName).ifPresent( optional -> encryptInsertValues((GroupedParameterBuilder) paramBuilder, insertStatementContext, optional, encryptRule.findAssistedQueryEncryptor(tableName, columnName).orElse(null), encryptRule.findLikeQueryEncryptor(tableName, columnName).orElse(null), encryptContext)); @@ -78,8 +79,8 @@ public void rewrite(final ParameterBuilder paramBuilder, final InsertStatementCo } private void encryptInsertValues(final GroupedParameterBuilder paramBuilder, final InsertStatementContext insertStatementContext, - final StandardEncryptAlgorithm encryptAlgorithm, final StandardEncryptAlgorithm assistEncryptAlgorithm, - final LikeEncryptAlgorithm likeEncryptAlgorithm, final EncryptContext encryptContext) { + final StandardEncryptAlgorithm standardEncryptor, final AssistedEncryptAlgorithm assistQueryEncryptor, + final LikeEncryptAlgorithm likeQueryEncryptor, final EncryptContext encryptContext) { int columnIndex = getColumnIndex(paramBuilder, insertStatementContext, encryptContext.getColumnName()); int count = 0; for (List each : insertStatementContext.getGroupedParameters()) { @@ -90,7 +91,7 @@ private void encryptInsertValues(final GroupedParameterBuilder paramBuilder, fin if (expressionSegment instanceof ParameterMarkerExpressionSegment) { Object literalValue = insertStatementContext.getInsertValueContexts().get(count).getLiteralValue(columnIndex) .orElse(null); - encryptInsertValue(encryptAlgorithm, assistEncryptAlgorithm, likeEncryptAlgorithm, paramIndex, literalValue, standardParamBuilder, encryptContext); + encryptInsertValue(standardEncryptor, assistQueryEncryptor, likeQueryEncryptor, paramIndex, literalValue, standardParamBuilder, encryptContext); } } count++; @@ -109,20 +110,19 @@ private int getColumnIndex(final GroupedParameterBuilder paramBuilder, final Ins } @SuppressWarnings({"rawtypes", "unchecked"}) - private void encryptInsertValue(final StandardEncryptAlgorithm encryptor, final StandardEncryptAlgorithm assistEncryptor, final LikeEncryptAlgorithm likeEncryptor, - final int paramIndex, final Object originalValue, final StandardParameterBuilder paramBuilder, - final EncryptContext encryptContext) { - paramBuilder.addReplacedParameters(paramIndex, encryptor.encrypt(originalValue, encryptContext)); + private void encryptInsertValue(final StandardEncryptAlgorithm standardEncryptor, final AssistedEncryptAlgorithm assistQueryEncryptor, final LikeEncryptAlgorithm likeQueryEncryptor, + final int paramIndex, final Object originalValue, final StandardParameterBuilder paramBuilder, final EncryptContext encryptContext) { + paramBuilder.addReplacedParameters(paramIndex, standardEncryptor.encrypt(originalValue, encryptContext)); Collection addedParams = new LinkedList<>(); - if (null != assistEncryptor) { + if (null != assistQueryEncryptor) { Optional assistedColumnName = encryptRule.findAssistedQueryColumn(encryptContext.getTableName(), encryptContext.getColumnName()); ShardingSpherePreconditions.checkState(assistedColumnName.isPresent(), EncryptAssistedQueryColumnNotFoundException::new); - addedParams.add(assistEncryptor.encrypt(originalValue, encryptContext)); + addedParams.add(assistQueryEncryptor.encrypt(originalValue, encryptContext)); } - if (null != likeEncryptor) { + if (null != likeQueryEncryptor) { Optional likeColumnName = encryptRule.findLikeQueryColumn(encryptContext.getTableName(), encryptContext.getColumnName()); ShardingSpherePreconditions.checkState(likeColumnName.isPresent(), EncryptLikeQueryColumnNotFoundException::new); - addedParams.add(likeEncryptor.encrypt(originalValue, encryptContext)); + addedParams.add(likeQueryEncryptor.encrypt(originalValue, encryptContext)); } if (!addedParams.isEmpty()) { if (!paramBuilder.getAddedIndexAndParameters().containsKey(paramIndex)) { diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptAlterTableTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptAlterTableTokenGenerator.java index 8a6a58e710b85..e7a5aaa5ca2c6 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptAlterTableTokenGenerator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptAlterTableTokenGenerator.java @@ -110,7 +110,7 @@ private Collection getAddColumnTokens(final String tableName, final Ad Collection result = new LinkedList<>(); for (ColumnDefinitionSegment each : addColumnDefinitionSegment.getColumnDefinitions()) { String columnName = each.getColumnName().getIdentifier().getValue(); - Optional encryptor = encryptRule.findEncryptor(tableName, columnName); + Optional encryptor = encryptRule.findStandardEncryptor(tableName, columnName); if (encryptor.isPresent()) { result.addAll(getAddColumnTokens(tableName, columnName, addColumnDefinitionSegment, each)); } @@ -141,7 +141,7 @@ private EncryptColumn getEncryptColumn(final String tableName, final String colu @SuppressWarnings("rawtypes") private Optional getAddColumnPositionToken(final String tableName, final AddColumnDefinitionSegment addColumnDefinitionSegment) { Optional encryptor = addColumnDefinitionSegment.getColumnPosition().filter(optional -> null != optional.getColumnName()) - .flatMap(optional -> encryptRule.findEncryptor(tableName, optional.getColumnName().getIdentifier().getValue())); + .flatMap(optional -> encryptRule.findStandardEncryptor(tableName, optional.getColumnName().getIdentifier().getValue())); if (encryptor.isPresent()) { return Optional.of(getPositionColumnToken(addColumnDefinitionSegment.getColumnPosition().get(), tableName)); } @@ -159,7 +159,7 @@ private Collection getModifyColumnTokens(final String tableName, final for (ModifyColumnDefinitionSegment each : columnDefinitionSegments) { ColumnDefinitionSegment segment = each.getColumnDefinition(); String columnName = segment.getColumnName().getIdentifier().getValue(); - Optional encryptor = encryptRule.findEncryptor(tableName, columnName); + Optional encryptor = encryptRule.findStandardEncryptor(tableName, columnName); if (encryptor.isPresent()) { result.addAll(getModifyColumnTokens(tableName, columnName, each)); } @@ -185,7 +185,7 @@ private Collection getModifyColumnTokens(final String tableName, final @SuppressWarnings("rawtypes") private Optional getColumnPositionToken(final String tableName, final ColumnPositionSegment columnPositionSegment) { Optional encryptor = Optional.of(columnPositionSegment).filter(optional -> null != optional.getColumnName()) - .flatMap(optional -> encryptRule.findEncryptor(tableName, optional.getColumnName().getIdentifier().getValue())); + .flatMap(optional -> encryptRule.findStandardEncryptor(tableName, optional.getColumnName().getIdentifier().getValue())); return encryptor.isPresent() ? Optional.of(getPositionColumnToken(columnPositionSegment, tableName)) : Optional.empty(); } @@ -200,8 +200,8 @@ private Collection getChangeColumnTokens(final String tableName, final private Collection getChangeColumnTokensEach(final String tableName, final ChangeColumnDefinitionSegment segment) { isSameEncryptColumn(tableName, segment); - if (!encryptRule.findEncryptor(tableName, segment.getPreviousColumn().getIdentifier().getValue()).isPresent() - || !encryptRule.findEncryptor(tableName, segment.getColumnDefinition().getColumnName().getIdentifier().getValue()).isPresent()) { + if (!encryptRule.findStandardEncryptor(tableName, segment.getPreviousColumn().getIdentifier().getValue()).isPresent() + || !encryptRule.findStandardEncryptor(tableName, segment.getColumnDefinition().getColumnName().getIdentifier().getValue()).isPresent()) { return Collections.emptyList(); } Collection result = new LinkedList<>(); @@ -212,8 +212,8 @@ private Collection getChangeColumnTokensEach(final String ta @SuppressWarnings("rawtypes") private void isSameEncryptColumn(final String tableName, final ChangeColumnDefinitionSegment segment) { - Optional previousAlgorithm = encryptRule.findEncryptor(tableName, segment.getPreviousColumn().getIdentifier().getValue()); - Optional currentAlgorithm = encryptRule.findEncryptor(tableName, segment.getColumnDefinition().getColumnName().getIdentifier().getValue()); + Optional previousAlgorithm = encryptRule.findStandardEncryptor(tableName, segment.getPreviousColumn().getIdentifier().getValue()); + Optional currentAlgorithm = encryptRule.findStandardEncryptor(tableName, segment.getColumnDefinition().getColumnName().getIdentifier().getValue()); if (!previousAlgorithm.isPresent() && !currentAlgorithm.isPresent()) { return; } @@ -274,7 +274,7 @@ private Collection getDropColumnTokens(final String tableName, final D Collection result = new LinkedList<>(); for (ColumnSegment each : dropColumnDefinitionSegment.getColumns()) { String columnName = each.getQualifiedName(); - Optional encryptor = encryptRule.findEncryptor(tableName, columnName); + Optional encryptor = encryptRule.findStandardEncryptor(tableName, columnName); if (encryptor.isPresent()) { result.addAll(getDropColumnTokens(tableName, columnName, each, dropColumnDefinitionSegment)); } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptAssignmentTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptAssignmentTokenGenerator.java index 343d79032b2b2..59aa702c196ed 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptAssignmentTokenGenerator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptAssignmentTokenGenerator.java @@ -67,7 +67,7 @@ public Collection generateSQLTokens(final SQLStatementCo String tableName = ((TableAvailable) sqlStatementContext).getAllTables().iterator().next().getTableName().getIdentifier().getValue(); String schemaName = sqlStatementContext.getTablesContext().getSchemaName().orElseGet(() -> DatabaseTypeEngine.getDefaultSchemaName(sqlStatementContext.getDatabaseType(), databaseName)); for (AssignmentSegment each : getSetAssignmentSegment(sqlStatementContext.getSqlStatement()).getAssignments()) { - if (encryptRule.findEncryptor(tableName, each.getColumns().get(0).getIdentifier().getValue()).isPresent()) { + if (encryptRule.findStandardEncryptor(tableName, each.getColumns().get(0).getIdentifier().getValue()).isPresent()) { generateSQLToken(schemaName, tableName, each).ifPresent(result::add); } } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptCreateTableTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptCreateTableTokenGenerator.java index 43edb863eb5a0..d8264853cbfb9 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptCreateTableTokenGenerator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptCreateTableTokenGenerator.java @@ -59,7 +59,7 @@ public Collection generateSQLTokens(final CreateTableStatementContext for (int index = 0; index < columns.size(); index++) { ColumnDefinitionSegment each = columns.get(index); String columnName = each.getColumnName().getIdentifier().getValue(); - Optional encryptor = encryptRule.findEncryptor(tableName, columnName); + Optional encryptor = encryptRule.findStandardEncryptor(tableName, columnName); if (encryptor.isPresent()) { result.addAll(getColumnTokens(tableName, columnName, each, columns, index)); } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptIndexColumnTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptIndexColumnTokenGenerator.java index 39bd7775ce3cf..42f45dff04eff 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptIndexColumnTokenGenerator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptIndexColumnTokenGenerator.java @@ -57,7 +57,7 @@ public Collection generateSQLTokens(final SQLStatementContext sqlSt Collection result = new LinkedList<>(); String tableName = sqlStatementContext.getTablesContext().getTableNames().iterator().next(); for (ColumnSegment each : ((IndexAvailable) sqlStatementContext).getIndexColumns()) { - encryptRule.findEncryptor(tableName, each.getIdentifier().getValue()).flatMap(optional -> getColumnToken(tableName, each)).ifPresent(result::add); + encryptRule.findStandardEncryptor(tableName, each.getIdentifier().getValue()).flatMap(optional -> getColumnToken(tableName, each)).ifPresent(result::add); } return result; } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptInsertOnUpdateTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptInsertOnUpdateTokenGenerator.java index 2ecf267871242..20e66fdbda742 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptInsertOnUpdateTokenGenerator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptInsertOnUpdateTokenGenerator.java @@ -74,10 +74,10 @@ public Collection generateSQLTokens(final InsertStatemen String schemaName = insertStatementContext.getTablesContext().getSchemaName().orElseGet(() -> DatabaseTypeEngine.getDefaultSchemaName(insertStatementContext.getDatabaseType(), databaseName)); String tableName = insertStatement.getTable().getTableName().getIdentifier().getValue(); for (AssignmentSegment each : onDuplicateKeyColumnsSegments) { - boolean leftEncryptorPresent = encryptRule.findEncryptor(tableName, each.getColumns().get(0).getIdentifier().getValue()).isPresent(); + boolean leftEncryptorPresent = encryptRule.findStandardEncryptor(tableName, each.getColumns().get(0).getIdentifier().getValue()).isPresent(); if (each.getValue() instanceof FunctionSegment && "VALUES".equalsIgnoreCase(((FunctionSegment) each.getValue()).getFunctionName())) { ColumnSegment rightColumn = (ColumnSegment) ((FunctionSegment) each.getValue()).getParameters().stream().findFirst().get(); - boolean rightEncryptorPresent = encryptRule.findEncryptor(tableName, rightColumn.getIdentifier().getValue()).isPresent(); + boolean rightEncryptorPresent = encryptRule.findStandardEncryptor(tableName, rightColumn.getIdentifier().getValue()).isPresent(); if (!leftEncryptorPresent && !rightEncryptorPresent) { continue; } @@ -126,8 +126,8 @@ private EncryptAssignmentToken generateValuesSQLToken(final String tableName, fi ColumnSegment valueColumnSegment = (ColumnSegment) functionSegment.getParameters().stream().findFirst().get(); String valueColumn = valueColumnSegment.getIdentifier().getValue(); EncryptFunctionAssignmentToken result = new EncryptFunctionAssignmentToken(columnSegment.getStartIndex(), assignmentSegment.getStopIndex()); - boolean cipherColumnPresent = encryptRule.findEncryptor(tableName, column).isPresent(); - boolean cipherValueColumnPresent = encryptRule.findEncryptor(tableName, valueColumn).isPresent(); + boolean cipherColumnPresent = encryptRule.findStandardEncryptor(tableName, column).isPresent(); + boolean cipherValueColumnPresent = encryptRule.findStandardEncryptor(tableName, valueColumn).isPresent(); if (cipherColumnPresent && cipherValueColumnPresent) { String cipherColumn = encryptRule.getCipherColumn(tableName, column); String cipherValueColumn = encryptRule.getCipherColumn(tableName, valueColumn); diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptInsertValuesTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptInsertValuesTokenGenerator.java index 1db417be3d425..6d54474a1e355 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptInsertValuesTokenGenerator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptInsertValuesTokenGenerator.java @@ -18,6 +18,8 @@ package org.apache.shardingsphere.encrypt.rewrite.token.generator; import lombok.Setter; +import org.apache.shardingsphere.encrypt.api.context.EncryptContext; +import org.apache.shardingsphere.encrypt.api.encrypt.assisted.AssistedEncryptAlgorithm; import org.apache.shardingsphere.encrypt.api.encrypt.like.LikeEncryptAlgorithm; import org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm; import org.apache.shardingsphere.encrypt.context.EncryptContextBuilder; @@ -25,7 +27,6 @@ import org.apache.shardingsphere.encrypt.rewrite.aware.EncryptRuleAware; import org.apache.shardingsphere.encrypt.rewrite.token.pojo.EncryptInsertValuesToken; import org.apache.shardingsphere.encrypt.rule.EncryptRule; -import org.apache.shardingsphere.encrypt.api.context.EncryptContext; import org.apache.shardingsphere.infra.binder.segment.insert.values.InsertValueContext; import org.apache.shardingsphere.infra.binder.segment.insert.values.expression.DerivedLiteralExpressionSegment; import org.apache.shardingsphere.infra.binder.segment.insert.values.expression.DerivedParameterMarkerExpressionSegment; @@ -131,13 +132,13 @@ private void encryptToken(final InsertValue insertValueToken, final String schem Iterator descendingColumnNames = insertStatementContext.getDescendingColumnNames(); while (descendingColumnNames.hasNext()) { String columnName = descendingColumnNames.next(); - Optional encryptor = encryptRule.findEncryptor(tableName, columnName); - if (encryptor.isPresent()) { + Optional standardEncryptor = encryptRule.findStandardEncryptor(tableName, columnName); + if (standardEncryptor.isPresent()) { int columnIndex = useDefaultInsertColumnsToken.map(optional -> ((UseDefaultInsertColumnsToken) optional).getColumns().indexOf(columnName)) .orElseGet(() -> insertStatementContext.getColumnNames().indexOf(columnName)); Object originalValue = insertValueContext.getLiteralValue(columnIndex).orElse(null); EncryptContext encryptContext = EncryptContextBuilder.build(databaseName, schemaName, tableName, columnName); - setCipherColumn(insertValueToken, encryptor.get(), columnIndex, encryptContext, insertValueContext.getValueExpressions().get(columnIndex), originalValue); + setCipherColumn(insertValueToken, standardEncryptor.get(), columnIndex, encryptContext, insertValueContext.getValueExpressions().get(columnIndex), originalValue); int indexDelta = 1; if (encryptRule.findAssistedQueryEncryptor(tableName, columnName).isPresent()) { addAssistedQueryColumn(insertValueToken, encryptRule.findAssistedQueryEncryptor(tableName, columnName).get(), columnIndex, encryptContext, @@ -146,29 +147,28 @@ private void encryptToken(final InsertValue insertValueToken, final String schem } if (encryptRule.findLikeQueryEncryptor(tableName, columnName).isPresent()) { addLikeQueryColumn(insertValueToken, encryptRule.findLikeQueryEncryptor(tableName, columnName).get(), columnIndex, encryptContext, insertValueContext, originalValue, indexDelta); - indexDelta++; } } } } - private void addAssistedQueryColumn(final InsertValue insertValueToken, final StandardEncryptAlgorithm encryptAlgorithm, final int columnIndex, + private void addAssistedQueryColumn(final InsertValue insertValueToken, final AssistedEncryptAlgorithm assistQueryEncryptor, final int columnIndex, final EncryptContext encryptContext, final InsertValueContext insertValueContext, final Object originalValue, final int indexDelta) { if (encryptRule.findAssistedQueryColumn(encryptContext.getTableName(), encryptContext.getColumnName()).isPresent()) { DerivedSimpleExpressionSegment derivedExpressionSegment = isAddLiteralExpressionSegment(insertValueContext, columnIndex) - ? new DerivedLiteralExpressionSegment(encryptAlgorithm.encrypt(originalValue, encryptContext)) + ? new DerivedLiteralExpressionSegment(assistQueryEncryptor.encrypt(originalValue, encryptContext)) : new DerivedParameterMarkerExpressionSegment(getParameterIndexCount(insertValueToken)); insertValueToken.getValues().add(columnIndex + indexDelta, derivedExpressionSegment); } } - private void addLikeQueryColumn(final InsertValue insertValueToken, final LikeEncryptAlgorithm encryptAlgorithm, final int columnIndex, + private void addLikeQueryColumn(final InsertValue insertValueToken, final LikeEncryptAlgorithm likeQueryEncryptor, final int columnIndex, final EncryptContext encryptContext, final InsertValueContext insertValueContext, final Object originalValue, final int indexDelta) { if (encryptRule.findLikeQueryColumn(encryptContext.getTableName(), encryptContext.getColumnName()).isPresent()) { DerivedSimpleExpressionSegment derivedExpressionSegment = isAddLiteralExpressionSegment(insertValueContext, columnIndex) - ? new DerivedLiteralExpressionSegment(encryptAlgorithm.encrypt(originalValue, encryptContext)) + ? new DerivedLiteralExpressionSegment(likeQueryEncryptor.encrypt(originalValue, encryptContext)) : new DerivedParameterMarkerExpressionSegment(getParameterIndexCount(insertValueToken)); insertValueToken.getValues().add(columnIndex + indexDelta, derivedExpressionSegment); } diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptProjectionTokenGenerator.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptProjectionTokenGenerator.java index 05386081ce61b..9a7ada2092689 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptProjectionTokenGenerator.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptProjectionTokenGenerator.java @@ -113,7 +113,7 @@ private SubstitutableColumnNameToken generateSQLToken(final ShorthandProjectionS List projections = new LinkedList<>(); for (ColumnProjection each : columnProjections) { String tableName = columnTableNames.get(each.getExpression()); - if (null == tableName || !encryptRule.findEncryptor(tableName, each.getName()).isPresent()) { + if (null == tableName || !encryptRule.findStandardEncryptor(tableName, each.getName()).isPresent()) { projections.add(new ColumnProjection(each.getOwner(), each.getName(), each.getAlias().orElse(null))); } else { projections.addAll(generateProjections(tableName, each, subqueryType, true, segment)); diff --git a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java index a4b4eb78f8a92..4b8ba30d6728c 100644 --- a/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java +++ b/features/encrypt/core/src/main/java/org/apache/shardingsphere/encrypt/rule/EncryptRule.java @@ -19,6 +19,8 @@ import lombok.Getter; import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration; +import org.apache.shardingsphere.encrypt.api.context.EncryptContext; +import org.apache.shardingsphere.encrypt.api.encrypt.assisted.AssistedEncryptAlgorithm; import org.apache.shardingsphere.encrypt.api.encrypt.like.LikeEncryptAlgorithm; import org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm; import org.apache.shardingsphere.encrypt.context.EncryptContextBuilder; @@ -26,7 +28,6 @@ import org.apache.shardingsphere.encrypt.exception.metadata.EncryptEncryptorNotFoundException; import org.apache.shardingsphere.encrypt.exception.metadata.EncryptLikeQueryEncryptorNotFoundException; import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm; -import org.apache.shardingsphere.encrypt.api.context.EncryptContext; import org.apache.shardingsphere.infra.config.rule.RuleConfiguration; import org.apache.shardingsphere.infra.rule.identifier.scope.DatabaseRule; import org.apache.shardingsphere.infra.rule.identifier.type.ColumnContainedRule; @@ -56,6 +57,9 @@ public final class EncryptRule implements DatabaseRule, TableContainedRule, Colu @SuppressWarnings("rawtypes") private final Map likeEncryptors = new LinkedHashMap<>(); + @SuppressWarnings("rawtypes") + private final Map assistedEncryptors = new LinkedHashMap<>(); + private final Map tables = new LinkedHashMap<>(); public EncryptRule(final EncryptRuleConfiguration ruleConfig) { @@ -68,9 +72,13 @@ public EncryptRule(final EncryptRuleConfiguration ruleConfig) { private void putAllEncryptors(final String encryptorName, final EncryptAlgorithm algorithm) { if (algorithm instanceof StandardEncryptAlgorithm) { standardEncryptors.put(encryptorName, (StandardEncryptAlgorithm) algorithm); - } else { + } + if (algorithm instanceof LikeEncryptAlgorithm) { likeEncryptors.put(encryptorName, (LikeEncryptAlgorithm) algorithm); } + if (algorithm instanceof AssistedEncryptAlgorithm) { + assistedEncryptors.put(encryptorName, (AssistedEncryptAlgorithm) algorithm); + } } /** @@ -95,29 +103,29 @@ public Optional findEncryptColumn(final String logicTable, final } /** - * Find encryptor. + * Find standard encryptor. * * @param logicTable logic table name * @param logicColumn logic column name - * @return encryptor + * @return standard encryptor */ @SuppressWarnings("rawtypes") - public Optional findEncryptor(final String logicTable, final String logicColumn) { + public Optional findStandardEncryptor(final String logicTable, final String logicColumn) { String lowerCaseLogicTable = logicTable.toLowerCase(); return tables.containsKey(lowerCaseLogicTable) ? tables.get(lowerCaseLogicTable).findEncryptorName(logicColumn).map(standardEncryptors::get) : Optional.empty(); } /** - * Find assist encryptor. + * Find assisted encryptor. * * @param logicTable logic table name * @param logicColumn logic column name - * @return encryptor + * @return assisted encryptor */ @SuppressWarnings("rawtypes") - public Optional findAssistedQueryEncryptor(final String logicTable, final String logicColumn) { + public Optional findAssistedQueryEncryptor(final String logicTable, final String logicColumn) { String lowerCaseLogicTable = logicTable.toLowerCase(); - return tables.containsKey(lowerCaseLogicTable) ? tables.get(lowerCaseLogicTable).findAssistedQueryEncryptorName(logicColumn).map(standardEncryptors::get) : Optional.empty(); + return tables.containsKey(lowerCaseLogicTable) ? tables.get(lowerCaseLogicTable).findAssistedQueryEncryptorName(logicColumn).map(assistedEncryptors::get) : Optional.empty(); } /** @@ -145,7 +153,7 @@ public Optional findLikeQueryEncryptor(final String logicT */ public List getEncryptValues(final String databaseName, final String schemaName, final String logicTable, final String logicColumn, final List originalValues) { @SuppressWarnings("rawtypes") - Optional encryptor = findEncryptor(logicTable, logicColumn); + Optional encryptor = findStandardEncryptor(logicTable, logicColumn); EncryptContext encryptContext = EncryptContextBuilder.build(databaseName, schemaName, logicTable, logicColumn); ShardingSpherePreconditions.checkState(encryptor.isPresent(), () -> new EncryptEncryptorNotFoundException(String.format("Can not find StandardEncryptAlgorithm by %s.%s.", logicTable, logicColumn))); @@ -230,19 +238,19 @@ public Collection getAssistedQueryColumns(final String logicTable) { */ public List getEncryptAssistedQueryValues(final String databaseName, final String schemaName, final String logicTable, final String logicColumn, final List originalValues) { @SuppressWarnings("rawtypes") - Optional encryptor = findAssistedQueryEncryptor(logicTable, logicColumn); + Optional assistedQueryEncryptor = findAssistedQueryEncryptor(logicTable, logicColumn); EncryptContext encryptContext = EncryptContextBuilder.build(databaseName, schemaName, logicTable, logicColumn); - ShardingSpherePreconditions.checkState(encryptor.isPresent(), - () -> new EncryptAssistedQueryEncryptorNotFoundException(String.format("Can not find assist encryptor by %s.%s.", logicTable, logicColumn))); - return getEncryptAssistedQueryValues(encryptor.get(), originalValues, encryptContext); + ShardingSpherePreconditions.checkState(assistedQueryEncryptor.isPresent(), + () -> new EncryptAssistedQueryEncryptorNotFoundException(String.format("Can not find assisted encryptor by %s.%s.", logicTable, logicColumn))); + return getEncryptAssistedQueryValues(assistedQueryEncryptor.get(), originalValues, encryptContext); } @SuppressWarnings("unchecked") - private List getEncryptAssistedQueryValues(@SuppressWarnings("rawtypes") final StandardEncryptAlgorithm encryptor, + private List getEncryptAssistedQueryValues(@SuppressWarnings("rawtypes") final AssistedEncryptAlgorithm assistedQueryEncryptor, final List originalValues, final EncryptContext encryptContext) { List result = new LinkedList<>(); for (Object each : originalValues) { - result.add(null == each ? null : encryptor.encrypt(each, encryptContext)); + result.add(null == each ? null : assistedQueryEncryptor.encrypt(each, encryptContext)); } return result; } @@ -259,18 +267,19 @@ private List getEncryptAssistedQueryValues(@SuppressWarnings("rawtypes") */ public List getEncryptLikeQueryValues(final String databaseName, final String schemaName, final String logicTable, final String logicColumn, final List originalValues) { @SuppressWarnings("rawtypes") - Optional encryptor = findLikeQueryEncryptor(logicTable, logicColumn); + Optional likeQueryEncryptor = findLikeQueryEncryptor(logicTable, logicColumn); EncryptContext encryptContext = EncryptContextBuilder.build(databaseName, schemaName, logicTable, logicColumn); - ShardingSpherePreconditions.checkState(encryptor.isPresent(), - () -> new EncryptLikeQueryEncryptorNotFoundException(String.format("Can not find like encryptor by %s.%s.", logicTable, logicColumn))); - return getEncryptLikeQueryValues(encryptor.get(), originalValues, encryptContext); + ShardingSpherePreconditions.checkState(likeQueryEncryptor.isPresent(), + () -> new EncryptLikeQueryEncryptorNotFoundException(String.format("Can not find like query encryptor by %s.%s.", logicTable, logicColumn))); + return getEncryptLikeQueryValues(likeQueryEncryptor.get(), originalValues, encryptContext); } @SuppressWarnings("unchecked") - private List getEncryptLikeQueryValues(@SuppressWarnings("rawtypes") final LikeEncryptAlgorithm encryptor, final List originalValues, final EncryptContext encryptContext) { + private List getEncryptLikeQueryValues(@SuppressWarnings("rawtypes") final LikeEncryptAlgorithm likeQueryEncryptor, final List originalValues, + final EncryptContext encryptContext) { List result = new LinkedList<>(); for (Object each : originalValues) { - result.add(null == each ? null : encryptor.encrypt(each, encryptContext)); + result.add(null == each ? null : likeQueryEncryptor.encrypt(each, encryptContext)); } return result; } diff --git a/features/encrypt/core/src/main/resources/META-INF/services/org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm b/features/encrypt/core/src/main/resources/META-INF/services/org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm index bc088cee521dc..9f336b168092f 100644 --- a/features/encrypt/core/src/main/resources/META-INF/services/org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm +++ b/features/encrypt/core/src/main/resources/META-INF/services/org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm @@ -15,7 +15,7 @@ # limitations under the License. # -org.apache.shardingsphere.encrypt.algorithm.encrypt.MD5EncryptAlgorithm -org.apache.shardingsphere.encrypt.algorithm.encrypt.AESEncryptAlgorithm -org.apache.shardingsphere.encrypt.algorithm.encrypt.RC4EncryptAlgorithm +org.apache.shardingsphere.encrypt.algorithm.standard.AESEncryptAlgorithm +org.apache.shardingsphere.encrypt.algorithm.standard.RC4EncryptAlgorithm org.apache.shardingsphere.encrypt.algorithm.like.CharDigestLikeEncryptAlgorithm +org.apache.shardingsphere.encrypt.algorithm.assisted.MD5AssistedEncryptAlgorithm diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/MD5EncryptAlgorithmTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/assisted/MD5AssistedEncryptAlgorithmTest.java similarity index 82% rename from features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/MD5EncryptAlgorithmTest.java rename to features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/assisted/MD5AssistedEncryptAlgorithmTest.java index baa527fafc705..8e6804555b0ab 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/MD5EncryptAlgorithmTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/assisted/MD5AssistedEncryptAlgorithmTest.java @@ -15,11 +15,11 @@ * limitations under the License. */ -package org.apache.shardingsphere.encrypt.algorithm.encrypt; +package org.apache.shardingsphere.encrypt.algorithm.assisted; -import org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm; -import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm; import org.apache.shardingsphere.encrypt.api.context.EncryptContext; +import org.apache.shardingsphere.encrypt.api.encrypt.assisted.AssistedEncryptAlgorithm; +import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm; import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader; import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; @@ -31,14 +31,14 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.mockito.Mockito.mock; -class MD5EncryptAlgorithmTest { +class MD5AssistedEncryptAlgorithmTest { - private StandardEncryptAlgorithm encryptAlgorithm; + private AssistedEncryptAlgorithm encryptAlgorithm; @SuppressWarnings("unchecked") @BeforeEach void setUp() { - encryptAlgorithm = (StandardEncryptAlgorithm) TypedSPILoader.getService(EncryptAlgorithm.class, "MD5"); + encryptAlgorithm = (AssistedEncryptAlgorithm) TypedSPILoader.getService(EncryptAlgorithm.class, "MD5"); } @Test @@ -56,9 +56,4 @@ void assertEncryptWhenConfigSalt() { encryptAlgorithm.init(PropertiesBuilder.build(new Property("salt", "202cb962ac5907"))); assertThat(encryptAlgorithm.encrypt("test", mock(EncryptContext.class)), is("0c243d2934937738f36514035d95344a")); } - - @Test - void assertDecrypt() { - assertThat(encryptAlgorithm.decrypt("test", mock(EncryptContext.class)).toString(), is("test")); - } } diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/AESEncryptAlgorithmTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/standard/AESEncryptAlgorithmTest.java similarity index 98% rename from features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/AESEncryptAlgorithmTest.java rename to features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/standard/AESEncryptAlgorithmTest.java index 638c6f965df51..a0dcc8b63c89d 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/AESEncryptAlgorithmTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/standard/AESEncryptAlgorithmTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.encrypt.algorithm.encrypt; +package org.apache.shardingsphere.encrypt.algorithm.standard; import org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm; import org.apache.shardingsphere.encrypt.exception.algorithm.EncryptAlgorithmInitializationException; diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/RC4EncryptAlgorithmTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/standard/RC4EncryptAlgorithmTest.java similarity index 98% rename from features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/RC4EncryptAlgorithmTest.java rename to features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/standard/RC4EncryptAlgorithmTest.java index 36ebc1e12e9b2..57e6005752d23 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/encrypt/RC4EncryptAlgorithmTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/standard/RC4EncryptAlgorithmTest.java @@ -15,7 +15,7 @@ * limitations under the License. */ -package org.apache.shardingsphere.encrypt.algorithm.encrypt; +package org.apache.shardingsphere.encrypt.algorithm.standard; import org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm; import org.apache.shardingsphere.encrypt.exception.algorithm.EncryptAlgorithmInitializationException; diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/fixture/CoreQueryAssistedEncryptAlgorithmFixture.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/fixture/CoreQueryAssistedEncryptAlgorithmFixture.java index 7dda76b9dbdfb..e1cb1138e4e52 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/fixture/CoreQueryAssistedEncryptAlgorithmFixture.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/fixture/CoreQueryAssistedEncryptAlgorithmFixture.java @@ -17,21 +17,16 @@ package org.apache.shardingsphere.encrypt.fixture; -import org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm; import org.apache.shardingsphere.encrypt.api.context.EncryptContext; +import org.apache.shardingsphere.encrypt.api.encrypt.assisted.AssistedEncryptAlgorithm; -public final class CoreQueryAssistedEncryptAlgorithmFixture implements StandardEncryptAlgorithm { +public final class CoreQueryAssistedEncryptAlgorithmFixture implements AssistedEncryptAlgorithm { @Override public String encrypt(final Object plainValue, final EncryptContext encryptContext) { return "assistedEncryptValue"; } - @Override - public Object decrypt(final String cipherValue, final EncryptContext encryptContext) { - return "decryptValue"; - } - @Override public String getType() { return "CORE.QUERY_ASSISTED.FIXTURE"; diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptAlgorithmMetaDataTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptAlgorithmMetaDataTest.java index 7af648afeae21..f6edd85e3289d 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptAlgorithmMetaDataTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/merge/dql/EncryptAlgorithmMetaDataTest.java @@ -17,10 +17,10 @@ package org.apache.shardingsphere.encrypt.merge.dql; +import org.apache.shardingsphere.encrypt.api.context.EncryptContext; import org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm; -import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm; import org.apache.shardingsphere.encrypt.rule.EncryptRule; -import org.apache.shardingsphere.encrypt.api.context.EncryptContext; +import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm; import org.apache.shardingsphere.infra.binder.segment.select.projection.ProjectionsContext; import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.ColumnProjection; import org.apache.shardingsphere.infra.binder.segment.select.projection.impl.DerivedProjection; @@ -31,6 +31,7 @@ import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema; import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader; +import org.apache.shardingsphere.test.util.PropertiesBuilder; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -90,7 +91,8 @@ void setUp() { when(selectStatementContext.getDatabaseType()).thenReturn(new MySQLDatabaseType()); when(database.getName()).thenReturn(DefaultDatabase.LOGIC_NAME); when(database.getSchema(DefaultDatabase.LOGIC_NAME)).thenReturn(schema); - encryptAlgorithm = (StandardEncryptAlgorithm) TypedSPILoader.getService(EncryptAlgorithm.class, "MD5"); + encryptAlgorithm = + (StandardEncryptAlgorithm) TypedSPILoader.getService(EncryptAlgorithm.class, "AES", PropertiesBuilder.build(new PropertiesBuilder.Property("aes-key-value", "123456abc"))); } @Test @@ -110,7 +112,7 @@ void assertFindEncryptContextByMetaData() { void assertFindEncryptContextByStatementContext() { when(tablesContext.findTableNamesByColumnProjection(Collections.singletonList(columnProjection), schema)).thenReturn(Collections.emptyMap()); when(tablesContext.getTableNames()).thenReturn(Arrays.asList("t_user", "t_user_item", "t_order_item")); - when(encryptRule.findEncryptor("t_order_item", "id")).thenReturn(Optional.of(encryptAlgorithm)); + when(encryptRule.findStandardEncryptor("t_order_item", "id")).thenReturn(Optional.of(encryptAlgorithm)); EncryptAlgorithmMetaData encryptAlgorithmMetaData = new EncryptAlgorithmMetaData(database, encryptRule, selectStatementContext); Optional actual = encryptAlgorithmMetaData.findEncryptContext(1); assertTrue(actual.isPresent()); @@ -129,11 +131,11 @@ void assertFindEncryptContextWhenColumnProjectionIsNotExist() { @SuppressWarnings("rawtypes") @Test - void assertFindEncryptor() { - when(encryptRule.findEncryptor("t_order", "id")).thenReturn(Optional.of(encryptAlgorithm)); + void assertFindStandardEncryptor() { + when(encryptRule.findStandardEncryptor("t_order", "id")).thenReturn(Optional.of(encryptAlgorithm)); EncryptAlgorithmMetaData encryptAlgorithmMetaData = new EncryptAlgorithmMetaData(database, encryptRule, selectStatementContext); - Optional actualEncryptor = encryptAlgorithmMetaData.findEncryptor("t_order", "id"); + Optional actualEncryptor = encryptAlgorithmMetaData.findStandardEncryptor("t_order", "id"); assertTrue(actualEncryptor.isPresent()); - assertThat(actualEncryptor.get().getType(), is("MD5")); + assertThat(actualEncryptor.get().getType(), is("AES")); } } diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptAlterTableTokenGeneratorTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptAlterTableTokenGeneratorTest.java index e3a44585ad168..f4629b3da097a 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptAlterTableTokenGeneratorTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptAlterTableTokenGeneratorTest.java @@ -66,8 +66,8 @@ private EncryptRule mockEncryptRule() { EncryptTable encryptTable = mock(EncryptTable.class); when(encryptTable.getLogicColumns()).thenReturn(Collections.singleton("t_encrypt")); StandardEncryptAlgorithm encryptAlgorithm = mock(StandardEncryptAlgorithm.class); - when(result.findEncryptor("t_encrypt", "certificate_number")).thenReturn(Optional.of(encryptAlgorithm)); - when(result.findEncryptor("t_encrypt", "certificate_number_new")).thenReturn(Optional.of(encryptAlgorithm)); + when(result.findStandardEncryptor("t_encrypt", "certificate_number")).thenReturn(Optional.of(encryptAlgorithm)); + when(result.findStandardEncryptor("t_encrypt", "certificate_number_new")).thenReturn(Optional.of(encryptAlgorithm)); when(result.findEncryptTable("t_encrypt")).thenReturn(Optional.of(encryptTable)); when(result.findEncryptColumn("t_encrypt", "certificate_number")).thenReturn(Optional.of(mockEncryptColumn())); when(result.findEncryptColumn("t_encrypt", "certificate_number_new")).thenReturn(Optional.of(mockNewEncryptColumn())); diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptCreateTableTokenGeneratorTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptCreateTableTokenGeneratorTest.java index 14b6c5848d025..e4ac3d9793477 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptCreateTableTokenGeneratorTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptCreateTableTokenGeneratorTest.java @@ -89,7 +89,7 @@ private EncryptRule buildEncryptRule() { EncryptRule result = mock(EncryptRule.class); EncryptTable encryptTable = mock(EncryptTable.class); when(encryptTable.getLogicColumns()).thenReturn(Collections.singletonList("t_encrypt")); - when(result.findEncryptor("t_encrypt", "certificate_number")).thenReturn(Optional.of(mock(StandardEncryptAlgorithm.class))); + when(result.findStandardEncryptor("t_encrypt", "certificate_number")).thenReturn(Optional.of(mock(StandardEncryptAlgorithm.class))); when(result.findEncryptTable("t_encrypt")).thenReturn(Optional.of(encryptTable)); EncryptColumn column = mockEncryptColumn(); when(result.getCipherColumn("t_encrypt", "certificate_number")).thenReturn(column.getCipherColumn()); diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptOrderByItemTokenGeneratorTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptOrderByItemTokenGeneratorTest.java index bb22e95df537c..51f54d58d8ca7 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptOrderByItemTokenGeneratorTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/impl/EncryptOrderByItemTokenGeneratorTest.java @@ -85,7 +85,7 @@ private EncryptRule buildEncryptRule() { when(result.getCipherColumn("t_encrypt", "certificate_number")).thenReturn("cipher_certificate_number"); when(result.findAssistedQueryColumn("t_encrypt", "certificate_number")).thenReturn(Optional.of("assisted_certificate_number")); when(encryptTable.findEncryptorName("certificate_number")).thenReturn(Optional.of("encryptor_name")); - when(result.findEncryptor("t_encrypt", "certificate_number")).thenReturn(Optional.of(mock(StandardEncryptAlgorithm.class))); + when(result.findStandardEncryptor("t_encrypt", "certificate_number")).thenReturn(Optional.of(mock(StandardEncryptAlgorithm.class))); when(result.findEncryptTable("t_encrypt")).thenReturn(Optional.of(encryptTable)); return result; } diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptInsertOnUpdateTokenGeneratorTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptInsertOnUpdateTokenGeneratorTest.java index b5ab573edb502..aab15c5b00e23 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptInsertOnUpdateTokenGeneratorTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/EncryptInsertOnUpdateTokenGeneratorTest.java @@ -64,8 +64,8 @@ void setup() { private EncryptRule mockEncryptRule() { EncryptRule result = mock(EncryptRule.class); when(result.getCipherColumn("t_user", "mobile")).thenReturn("cipher_mobile"); - when(result.findEncryptor("t_user", "mobile")).thenReturn(Optional.of(mock(StandardEncryptAlgorithm.class))); - when(result.findEncryptor("t_user", "cipher_mobile")).thenReturn(Optional.of(mock(StandardEncryptAlgorithm.class))); + when(result.findStandardEncryptor("t_user", "mobile")).thenReturn(Optional.of(mock(StandardEncryptAlgorithm.class))); + when(result.findStandardEncryptor("t_user", "cipher_mobile")).thenReturn(Optional.of(mock(StandardEncryptAlgorithm.class))); when(result.getEncryptValues(null, "db_test", "t_user", "mobile", Collections.singletonList(0))).thenReturn(Collections.singletonList("encryptValue")); return result; } diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptAssignmentTokenGeneratorTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptAssignmentTokenGeneratorTest.java index 1451784164130..95be0645b10af 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptAssignmentTokenGeneratorTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/EncryptAssignmentTokenGeneratorTest.java @@ -78,7 +78,7 @@ void setup() { private EncryptRule mockEncryptRule() { EncryptRule result = mock(EncryptRule.class, RETURNS_DEEP_STUBS); - when(result.findEncryptor("table", "columns")).thenReturn(Optional.of(mock(StandardEncryptAlgorithm.class))); + when(result.findStandardEncryptor("table", "columns")).thenReturn(Optional.of(mock(StandardEncryptAlgorithm.class))); return result; } diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/fixture/EncryptGeneratorFixtureBuilder.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/fixture/EncryptGeneratorFixtureBuilder.java index 82a9042bd9b75..2b718feaf7a86 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/fixture/EncryptGeneratorFixtureBuilder.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rewrite/token/generator/fixture/EncryptGeneratorFixtureBuilder.java @@ -76,11 +76,12 @@ public final class EncryptGeneratorFixtureBuilder { * @return created encrypt rule */ public static EncryptRule createEncryptRule() { - EncryptColumnRuleConfiguration pwdColumnConfig = - new EncryptColumnRuleConfiguration("pwd", "pwd_cipher", "pwd_assist", "pwd_like", "test_encryptor", "test_encryptor", "like_encryptor"); Map encryptors = new LinkedHashMap<>(2, 1); - encryptors.put("test_encryptor", new AlgorithmConfiguration("CORE.QUERY_ASSISTED.FIXTURE", new Properties())); + encryptors.put("standard_encryptor", new AlgorithmConfiguration("CORE.FIXTURE", new Properties())); + encryptors.put("assisted_encryptor", new AlgorithmConfiguration("CORE.QUERY_ASSISTED.FIXTURE", new Properties())); encryptors.put("like_encryptor", new AlgorithmConfiguration("CORE.QUERY_LIKE.FIXTURE", new Properties())); + EncryptColumnRuleConfiguration pwdColumnConfig = + new EncryptColumnRuleConfiguration("pwd", "pwd_cipher", "pwd_assist", "pwd_like", "standard_encryptor", "assisted_encryptor", "like_encryptor"); return new EncryptRule( new EncryptRuleConfiguration(Collections.singleton(new EncryptTableRuleConfiguration("t_user", Collections.singletonList(pwdColumnConfig))), encryptors)); } diff --git a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java index 77695116071c5..06dac9aa53fae 100644 --- a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java +++ b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/rule/EncryptRuleTest.java @@ -47,13 +47,13 @@ void assertFindEncryptTable() { } @Test - void assertFindEncryptor() { - assertTrue(new EncryptRule(createEncryptRuleConfiguration()).findEncryptor("t_encrypt", "pwd").isPresent()); + void assertFindStandardEncryptor() { + assertTrue(new EncryptRule(createEncryptRuleConfiguration()).findStandardEncryptor("t_encrypt", "pwd").isPresent()); } @Test - void assertNotFindEncryptor() { - assertFalse(new EncryptRule(createEncryptRuleConfiguration()).findEncryptor("t_encrypt", "other_column").isPresent()); + void assertNotFindStandardEncryptor() { + assertFalse(new EncryptRule(createEncryptRuleConfiguration()).findStandardEncryptor("t_encrypt", "other_column").isPresent()); } @Test @@ -132,41 +132,45 @@ void assertTheSameLogicTable() { } private EncryptRuleConfiguration createEncryptRuleConfiguration() { + AlgorithmConfiguration standardEncryptConfig = new AlgorithmConfiguration("CORE.FIXTURE", new Properties()); AlgorithmConfiguration queryAssistedEncryptConfig = new AlgorithmConfiguration("CORE.QUERY_ASSISTED.FIXTURE", new Properties()); AlgorithmConfiguration queryLikeEncryptConfig = new AlgorithmConfiguration("CORE.QUERY_LIKE.FIXTURE", new Properties()); EncryptColumnRuleConfiguration pwdColumnConfig = - new EncryptColumnRuleConfiguration("pwd", "pwd_cipher", "pwd_assist", "pwd_like", "test_encryptor", "test_encryptor", "like_encryptor"); + new EncryptColumnRuleConfiguration("pwd", "pwd_cipher", "pwd_assist", "pwd_like", "standard_encryptor", "assisted_encryptor", "like_encryptor"); EncryptColumnRuleConfiguration creditCardColumnConfig = new EncryptColumnRuleConfiguration("credit_card", "credit_card_cipher", "", "", "test_encryptor"); EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration("t_encrypt", Arrays.asList(pwdColumnConfig, creditCardColumnConfig)); - return new EncryptRuleConfiguration(Collections.singleton(tableConfig), getEncryptors(queryAssistedEncryptConfig, queryLikeEncryptConfig)); + return new EncryptRuleConfiguration(Collections.singleton(tableConfig), getEncryptors(standardEncryptConfig, queryAssistedEncryptConfig, queryLikeEncryptConfig)); } @Test void assertAssistedQueryEncryptorNameSpecified() { EncryptColumnRuleConfiguration pwdColumnConfig = - new EncryptColumnRuleConfiguration("pwd", "pwd_cipher", "pwd_assist", "", "test_encryptor", "assisted_query_test_encryptor", null); + new EncryptColumnRuleConfiguration("pwd", "pwd_cipher", "pwd_assist", "", "standard_encryptor", "assisted_query_test_encryptor", null); assertThat(pwdColumnConfig.getAssistedQueryEncryptorName(), is("assisted_query_test_encryptor")); } @Test void assertLikeQueryEncryptorNameSpecified() { EncryptColumnRuleConfiguration pwdColumnConfig = - new EncryptColumnRuleConfiguration("pwd", "pwd_cipher", "", "pwd_like", "test_encryptor", "", "like_query_test_encryptor"); + new EncryptColumnRuleConfiguration("pwd", "pwd_cipher", "", "pwd_like", "standard_encryptor", "", "like_query_test_encryptor"); assertThat(pwdColumnConfig.getLikeQueryEncryptorName(), is("like_query_test_encryptor")); } private EncryptRuleConfiguration createEncryptRuleConfigurationWithUpperCaseLogicTable() { + AlgorithmConfiguration standardEncryptConfig = new AlgorithmConfiguration("CORE.FIXTURE", new Properties()); AlgorithmConfiguration queryAssistedEncryptConfig = new AlgorithmConfiguration("CORE.QUERY_ASSISTED.FIXTURE", new Properties()); AlgorithmConfiguration queryLikeEncryptConfig = new AlgorithmConfiguration("CORE.QUERY_LIKE.FIXTURE", new Properties()); EncryptColumnRuleConfiguration pwdColumnConfig = new EncryptColumnRuleConfiguration("pwd", "pwd_cipher", "", "", "test_encryptor"); EncryptColumnRuleConfiguration creditCardColumnConfig = new EncryptColumnRuleConfiguration("credit_card", "credit_card_cipher", "", "", "test_encryptor"); EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration("T_ENCRYPT", Arrays.asList(pwdColumnConfig, creditCardColumnConfig)); - return new EncryptRuleConfiguration(Collections.singleton(tableConfig), getEncryptors(queryAssistedEncryptConfig, queryLikeEncryptConfig)); + return new EncryptRuleConfiguration(Collections.singleton(tableConfig), getEncryptors(standardEncryptConfig, queryAssistedEncryptConfig, queryLikeEncryptConfig)); } - private Map getEncryptors(final AlgorithmConfiguration queryAssistedEncryptConfig, final AlgorithmConfiguration queryLikeEncryptConfig) { + private Map getEncryptors(final AlgorithmConfiguration standardEncryptConfig, final AlgorithmConfiguration queryAssistedEncryptConfig, + final AlgorithmConfiguration queryLikeEncryptConfig) { Map result = new HashMap<>(2, 1); - result.put("test_encryptor", queryAssistedEncryptConfig); + result.put("standard_encryptor", standardEncryptConfig); + result.put("assisted_encryptor", queryAssistedEncryptConfig); result.put("like_encryptor", queryLikeEncryptConfig); return result; } diff --git a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoaderTest.java b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoaderTest.java index 796a3ae0adf68..daf185372aea7 100644 --- a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoaderTest.java +++ b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/config/ProxyConfigurationLoaderTest.java @@ -125,12 +125,13 @@ private void assertEncryptRuleConfiguration(final YamlProxyDatabaseConfiguration private void assertEncryptRuleConfiguration(final YamlEncryptRuleConfiguration actual) { assertThat(actual.getEncryptors().size(), is(2)); assertTrue(actual.getEncryptors().containsKey("aes_encryptor")); - assertTrue(actual.getEncryptors().containsKey("md5_encryptor")); + assertTrue(actual.getEncryptors().containsKey("rc4_encryptor")); YamlAlgorithmConfiguration aesEncryptAlgorithmConfig = actual.getEncryptors().get("aes_encryptor"); assertThat(aesEncryptAlgorithmConfig.getType(), is("AES")); assertThat(aesEncryptAlgorithmConfig.getProps().getProperty("aes-key-value"), is("123456abc")); - YamlAlgorithmConfiguration md5EncryptAlgorithmConfig = actual.getEncryptors().get("md5_encryptor"); - assertThat(md5EncryptAlgorithmConfig.getType(), is("MD5")); + YamlAlgorithmConfiguration md5EncryptAlgorithmConfig = actual.getEncryptors().get("rc4_encryptor"); + assertThat(md5EncryptAlgorithmConfig.getType(), is("RC4")); + assertThat(md5EncryptAlgorithmConfig.getProps().getProperty("rc4-key-value"), is("123456abc")); } private void assertDataSourceConfiguration(final YamlProxyDataSourceConfiguration actual, final String expectedURL) { diff --git a/proxy/backend/core/src/test/resources/conf/config_loader/config-encrypt.yaml b/proxy/backend/core/src/test/resources/conf/config_loader/config-encrypt.yaml index 5ea1246cdc694..9f836f58d9f5e 100644 --- a/proxy/backend/core/src/test/resources/conf/config_loader/config-encrypt.yaml +++ b/proxy/backend/core/src/test/resources/conf/config_loader/config-encrypt.yaml @@ -38,11 +38,13 @@ rules: encryptorName: aes_encryptor name: cipherColumn: name - encryptorName: md5_encryptor + encryptorName: rc4_encryptor encryptors: aes_encryptor: type: AES props: aes-key-value: 123456abc - md5_encryptor: - type: MD5 + rc4_encryptor: + type: RC4 + props: + rc4-key-value: 123456abc diff --git a/proxy/backend/core/src/test/resources/conf/convert/config-encrypt.yaml b/proxy/backend/core/src/test/resources/conf/convert/config-encrypt.yaml index 4bde8f92df5be..e8943046cafbc 100644 --- a/proxy/backend/core/src/test/resources/conf/convert/config-encrypt.yaml +++ b/proxy/backend/core/src/test/resources/conf/convert/config-encrypt.yaml @@ -44,8 +44,10 @@ rules: type: AES props: aes-key-value: 123456abc - md5_encryptor: - type: MD5 + rc4_encryptor: + type: RC4 + props: + rc4-key-value: 123456abc like_encryptor: type: CHAR_DIGEST_LIKE tables: @@ -55,9 +57,9 @@ rules: cipherColumn: user_cipher encryptorName: aes_encryptor assistedQueryColumn: user_assisted - assistedQueryEncryptorName: md5_encryptor + assistedQueryEncryptorName: rc4_encryptor likeQueryColumn: user_like likeQueryEncryptorName: like_encryptor order_id: cipherColumn: order_cipher - encryptorName: md5_encryptor + encryptorName: rc4_encryptor diff --git a/proxy/backend/core/src/test/resources/conf/convert/config-mix.yaml b/proxy/backend/core/src/test/resources/conf/convert/config-mix.yaml index 9a17666529d23..6f92c21fa201a 100644 --- a/proxy/backend/core/src/test/resources/conf/convert/config-mix.yaml +++ b/proxy/backend/core/src/test/resources/conf/convert/config-mix.yaml @@ -123,8 +123,10 @@ rules: type: AES props: aes-key-value: 123456abc - md5_encryptor: - type: MD5 + rc4_encryptor: + type: RC4 + props: + rc4-key-value: 123456abc like_encryptor: type: CHAR_DIGEST_LIKE tables: @@ -134,9 +136,9 @@ rules: cipherColumn: user_cipher encryptorName: aes_encryptor assistedQueryColumn: user_assisted - assistedQueryEncryptorName: md5_encryptor + assistedQueryEncryptorName: rc4_encryptor likeQueryColumn: user_like likeQueryEncryptorName: like_encryptor order_id: cipherColumn: order_cipher - encryptorName: md5_encryptor + encryptorName: rc4_encryptor diff --git a/proxy/backend/core/src/test/resources/conf/import/config-encrypt.yaml b/proxy/backend/core/src/test/resources/conf/import/config-encrypt.yaml index 9381385153581..99ba7813d5d92 100644 --- a/proxy/backend/core/src/test/resources/conf/import/config-encrypt.yaml +++ b/proxy/backend/core/src/test/resources/conf/import/config-encrypt.yaml @@ -44,8 +44,10 @@ rules: type: AES props: aes-key-value: 123456abc - md5_encryptor: - type: MD5 + rc4_encryptor: + type: RC4 + props: + rc4-key-value: 123456abc tables: t_encrypt: columns: @@ -54,4 +56,4 @@ rules: encryptorName: aes_encryptor order_id: cipherColumn: order_cipher - encryptorName: md5_encryptor + encryptorName: rc4_encryptor diff --git a/proxy/backend/core/src/test/resources/expected/convert-encrypt.yaml b/proxy/backend/core/src/test/resources/expected/convert-encrypt.yaml index 6b2f8c7ca73ca..8882645ae9399 100644 --- a/proxy/backend/core/src/test/resources/expected/convert-encrypt.yaml +++ b/proxy/backend/core/src/test/resources/expected/convert-encrypt.yaml @@ -31,6 +31,6 @@ PROPERTIES('minPoolSize'='1', 'connectionTimeoutMilliseconds'='30000', 'maxLifet CREATE ENCRYPT RULE t_encrypt ( COLUMNS( -(NAME=user_id, CIPHER=user_cipher, ASSISTED_QUERY_COLUMN=user_assisted, LIKE_QUERY_COLUMN=user_like, ENCRYPT_ALGORITHM(TYPE(NAME='aes', PROPERTIES('aes-key-value'='123456abc'))), ASSISTED_QUERY_ALGORITHM(TYPE(NAME='md5')), LIKE_QUERY_ALGORITHM(TYPE(NAME='char_digest_like'))), -(NAME=order_id, CIPHER=order_cipher, ENCRYPT_ALGORITHM(TYPE(NAME='md5'))) +(NAME=user_id, CIPHER=user_cipher, ASSISTED_QUERY_COLUMN=user_assisted, LIKE_QUERY_COLUMN=user_like, ENCRYPT_ALGORITHM(TYPE(NAME='aes', PROPERTIES('aes-key-value'='123456abc'))), ASSISTED_QUERY_ALGORITHM(TYPE(NAME='rc4', PROPERTIES('rc4-key-value'='123456abc'))), LIKE_QUERY_ALGORITHM(TYPE(NAME='char_digest_like'))), +(NAME=order_id, CIPHER=order_cipher, ENCRYPT_ALGORITHM(TYPE(NAME='rc4', PROPERTIES('rc4-key-value'='123456abc')))) )); diff --git a/proxy/backend/core/src/test/resources/expected/convert-mix.yaml b/proxy/backend/core/src/test/resources/expected/convert-mix.yaml index f52d1b4243877..f5d20d85d4fba 100644 --- a/proxy/backend/core/src/test/resources/expected/convert-mix.yaml +++ b/proxy/backend/core/src/test/resources/expected/convert-mix.yaml @@ -59,8 +59,8 @@ READ_STORAGE_UNITS(ds_4,ds_5) CREATE ENCRYPT RULE t_encrypt ( COLUMNS( -(NAME=user_id, CIPHER=user_cipher, ASSISTED_QUERY_COLUMN=user_assisted, LIKE_QUERY_COLUMN=user_like, ENCRYPT_ALGORITHM(TYPE(NAME='aes', PROPERTIES('aes-key-value'='123456abc'))), ASSISTED_QUERY_ALGORITHM(TYPE(NAME='md5')), LIKE_QUERY_ALGORITHM(TYPE(NAME='char_digest_like'))), -(NAME=order_id, CIPHER=order_cipher, ENCRYPT_ALGORITHM(TYPE(NAME='md5'))) +(NAME=user_id, CIPHER=user_cipher, ASSISTED_QUERY_COLUMN=user_assisted, LIKE_QUERY_COLUMN=user_like, ENCRYPT_ALGORITHM(TYPE(NAME='aes', PROPERTIES('aes-key-value'='123456abc'))), ASSISTED_QUERY_ALGORITHM(TYPE(NAME='rc4', PROPERTIES('rc4-key-value'='123456abc'))), LIKE_QUERY_ALGORITHM(TYPE(NAME='char_digest_like'))), +(NAME=order_id, CIPHER=order_cipher, ENCRYPT_ALGORITHM(TYPE(NAME='rc4', PROPERTIES('rc4-key-value'='123456abc')))) )); CREATE SHARDING TABLE RULE t_order ( diff --git a/proxy/bootstrap/src/main/resources/conf/config-encrypt.yaml b/proxy/bootstrap/src/main/resources/conf/config-encrypt.yaml index 72539d33e143b..6e413af590ad2 100644 --- a/proxy/bootstrap/src/main/resources/conf/config-encrypt.yaml +++ b/proxy/bootstrap/src/main/resources/conf/config-encrypt.yaml @@ -51,8 +51,10 @@ # type: AES # props: # aes-key-value: 123456abc -# md5_encryptor: -# type: MD5 +# rc4_encryptor: +# type: RC4 +# props: +# rc4-key-value: 123456abc # tables: # t_encrypt: # columns: @@ -61,7 +63,7 @@ # encryptorName: aes_encryptor # order_id: # cipherColumn: order_encrypt -# encryptorName: md5_encryptor +# encryptorName: rc4_encryptor ###################################################################################################### # @@ -98,8 +100,10 @@ # type: AES # props: # aes-key-value: 123456abc -# md5_encryptor: -# type: MD5 +# rc4_encryptor: +# type: RC4 +# props: +# rc4-key-value: 123456abc # tables: # t_encrypt: # columns: @@ -108,4 +112,4 @@ # encryptorName: aes_encryptor # order_id: # cipherColumn: order_cipher -# encryptorName: md5_encryptor +# encryptorName: rc4_encryptor diff --git a/test/e2e/driver/src/test/java/org/apache/shardingsphere/test/e2e/driver/fixture/encrypt/JDBCQueryAssistedEncryptAlgorithmFixture.java b/test/e2e/driver/src/test/java/org/apache/shardingsphere/test/e2e/driver/fixture/encrypt/JDBCQueryAssistedEncryptAlgorithmFixture.java index 70e385c247c4c..bcfc186e8c542 100644 --- a/test/e2e/driver/src/test/java/org/apache/shardingsphere/test/e2e/driver/fixture/encrypt/JDBCQueryAssistedEncryptAlgorithmFixture.java +++ b/test/e2e/driver/src/test/java/org/apache/shardingsphere/test/e2e/driver/fixture/encrypt/JDBCQueryAssistedEncryptAlgorithmFixture.java @@ -17,21 +17,16 @@ package org.apache.shardingsphere.test.e2e.driver.fixture.encrypt; -import org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm; import org.apache.shardingsphere.encrypt.api.context.EncryptContext; +import org.apache.shardingsphere.encrypt.api.encrypt.assisted.AssistedEncryptAlgorithm; -public final class JDBCQueryAssistedEncryptAlgorithmFixture implements StandardEncryptAlgorithm { +public final class JDBCQueryAssistedEncryptAlgorithmFixture implements AssistedEncryptAlgorithm { @Override public String encrypt(final Object plainValue, final EncryptContext encryptContext) { return "assistedEncryptValue"; } - @Override - public Object decrypt(final String cipherValue, final EncryptContext encryptContext) { - return "decryptValue"; - } - @Override public String getType() { return "JDBC.QUERY_ASSISTED.FIXTURE"; diff --git a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/fixture/encrypt/RewriteQueryAssistedEncryptAlgorithmFixture.java b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/fixture/encrypt/RewriteQueryAssistedEncryptAlgorithmFixture.java index 4fc354cf814cf..1381a68fe1c7c 100644 --- a/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/fixture/encrypt/RewriteQueryAssistedEncryptAlgorithmFixture.java +++ b/test/it/rewriter/src/test/java/org/apache/shardingsphere/test/it/rewrite/fixture/encrypt/RewriteQueryAssistedEncryptAlgorithmFixture.java @@ -17,10 +17,10 @@ package org.apache.shardingsphere.test.it.rewrite.fixture.encrypt; -import org.apache.shardingsphere.encrypt.api.encrypt.standard.StandardEncryptAlgorithm; import org.apache.shardingsphere.encrypt.api.context.EncryptContext; +import org.apache.shardingsphere.encrypt.api.encrypt.assisted.AssistedEncryptAlgorithm; -public final class RewriteQueryAssistedEncryptAlgorithmFixture implements StandardEncryptAlgorithm { +public final class RewriteQueryAssistedEncryptAlgorithmFixture implements AssistedEncryptAlgorithm { @Override public String encrypt(final Object plainValue, final EncryptContext encryptContext) { @@ -30,14 +30,6 @@ public String encrypt(final Object plainValue, final EncryptContext encryptConte return "assisted_query_" + plainValue; } - @Override - public Object decrypt(final String cipherValue, final EncryptContext encryptContext) { - if (null == cipherValue) { - return null; - } - return cipherValue.replaceAll("encrypt_", ""); - } - @Override public String getType() { return "REWRITE.ASSISTED_QUERY.FIXTURE";