Skip to content

Commit

Permalink
Merge pull request apache#25529 from strongduanmu/dev-0509
Browse files Browse the repository at this point in the history
Split assisted encrypt algorithm to AssistedEncryptAlgorithm api interface
  • Loading branch information
FlyingZC authored May 9, 2023
2 parents 93606ea + 7e912bf commit e55d450
Show file tree
Hide file tree
Showing 42 changed files with 212 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object, String> {
public final class TestQueryAssistedShardingEncryptAlgorithm implements AssistedEncryptAlgorithm<Object, String> {

@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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object, String> {
public final class TestQueryAssistedShardingEncryptAlgorithm implements AssistedEncryptAlgorithm<Object, String> {

@Getter
private Properties props;
Expand All @@ -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";
Expand Down
Original file line number Diff line number Diff line change
@@ -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 <I> type of plain value
* @param <O> type of cipher value
*/
public interface AssistedEncryptAlgorithm<I, O> extends EncryptAlgorithm<I, O> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object, String> {
public final class MD5AssistedEncryptAlgorithm implements AssistedEncryptAlgorithm<Object, String> {

private static final String SALT = "salt";

Expand All @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<StandardEncryptAlgorithm> findEncryptor(final String tableName, final String columnName) {
return encryptRule.findEncryptor(tableName, columnName);
public Optional<StandardEncryptAlgorithm> findStandardEncryptor(final String tableName, final String columnName) {
return encryptRule.findStandardEncryptor(tableName, columnName);
}

/**
Expand Down Expand Up @@ -92,7 +92,7 @@ private Optional<String> 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public Object getValue(final int columnIndex, final Class<?> type) throws SQLExc
if (!encryptContext.isPresent()) {
return mergedResult.getValue(columnIndex, type);
}
Optional<StandardEncryptAlgorithm> encryptAlgorithm = metaData.findEncryptor(encryptContext.get().getTableName(), encryptContext.get().getColumnName());
Optional<StandardEncryptAlgorithm> encryptAlgorithm = metaData.findStandardEncryptor(encryptContext.get().getTableName(), encryptContext.get().getColumnName());
if (!encryptAlgorithm.isPresent()) {
return mergedResult.getValue(columnIndex, type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<StandardEncryptAlgorithm> encryptor = encryptRule.findEncryptor(tableName, encryptLogicColumnName);
Optional<StandardEncryptAlgorithm> encryptor = encryptRule.findStandardEncryptor(tableName, encryptLogicColumnName);
if (!encryptor.isPresent()) {
continue;
}
Expand All @@ -90,7 +91,7 @@ public void rewrite(final ParameterBuilder paramBuilder, final InsertStatementCo
@SuppressWarnings({"rawtypes", "unchecked"})
private Collection<Object> buildAddedParams(final String tableName, final String logicColumnName, final Object plainValue, final EncryptContext encryptContext) {
Collection<Object> result = new LinkedList<>();
Optional<StandardEncryptAlgorithm> assistedQueryEncryptor = encryptRule.findAssistedQueryEncryptor(tableName, logicColumnName);
Optional<AssistedEncryptAlgorithm> assistedQueryEncryptor = encryptRule.findAssistedQueryEncryptor(tableName, logicColumnName);
if (assistedQueryEncryptor.isPresent()) {
Optional<String> assistedColumnName = encryptRule.findAssistedQueryColumn(tableName, logicColumnName);
Preconditions.checkArgument(assistedColumnName.isPresent(), "Can not find assisted query Column Name");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -70,16 +71,16 @@ 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));
}
}

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<Object> each : insertStatementContext.getGroupedParameters()) {
Expand All @@ -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++;
Expand All @@ -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<Object> addedParams = new LinkedList<>();
if (null != assistEncryptor) {
if (null != assistQueryEncryptor) {
Optional<String> 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<String> 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)) {
Expand Down
Loading

0 comments on commit e55d450

Please sign in to comment.