Skip to content

Commit

Permalink
Add EncryptAlgorithmMetaData defaultProperties. (#30905)
Browse files Browse the repository at this point in the history
  • Loading branch information
iamhucong authored Apr 21, 2024
1 parent 406339a commit aa34033
Show file tree
Hide file tree
Showing 29 changed files with 114 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,36 @@
package org.apache.shardingsphere.encrypt.api.config;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.encrypt.api.config.rule.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;
import org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import org.apache.shardingsphere.infra.config.rule.function.EnhancedRuleConfiguration;
import org.apache.shardingsphere.infra.config.rule.scope.DatabaseRuleConfiguration;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;

import java.util.Collection;
import java.util.Map;

/**
* Encrypt rule configuration.
*/
@RequiredArgsConstructor
@Getter
public final class EncryptRuleConfiguration implements DatabaseRuleConfiguration, EnhancedRuleConfiguration {

private final Collection<EncryptTableRuleConfiguration> tables;

private final Map<String, AlgorithmConfiguration> encryptors;

public EncryptRuleConfiguration(final Collection<EncryptTableRuleConfiguration> tables, final Map<String, AlgorithmConfiguration> encryptors) {
this.tables = tables;
this.encryptors = encryptors;
for (AlgorithmConfiguration each : encryptors.values()) {
TypedSPILoader.findUninitedService(EncryptAlgorithm.class, each.getType()).map(EncryptAlgorithm::getMetaData).map(EncryptAlgorithmMetaData::getDefaultProps)
.ifPresent(each.getProps()::putAll);
}
}

@Override
public boolean isEmpty() {
return tables.isEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Properties;

/**
* Encrypt algorithm meta data.
*/
Expand All @@ -32,4 +34,6 @@ public final class EncryptAlgorithmMetaData {
private final boolean supportEquivalentFilter;

private final boolean supportLike;

private final Properties defaultProps;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
public final class MD5AssistedEncryptAlgorithm implements EncryptAlgorithm {

@Getter
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(false, true, false);
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(false, true, false, new Properties());

private MessageDigestAlgorithm digestAlgorithm;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,27 @@ public final class AESEncryptAlgorithm implements EncryptAlgorithm {
private static final String DIGEST_ALGORITHM_NAME = "digest-algorithm-name";

@Getter
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(true, true, false);
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(true, true, false, getDefaultProperties());

private byte[] secretKey;

private Properties getDefaultProperties() {
Properties result = new Properties();
result.setProperty(DIGEST_ALGORITHM_NAME, MessageDigestAlgorithms.SHA_1);
return result;
}

@Override
public void init(final Properties props) {
secretKey = getSecretKey(props);
Properties properties = new Properties(metaData.getDefaultProps());
properties.putAll(props);
secretKey = getSecretKey(properties);
}

private byte[] getSecretKey(final Properties props) {
String aesKey = props.getProperty(AES_KEY);
ShardingSpherePreconditions.checkNotEmpty(aesKey, () -> new AlgorithmInitializationException(this, "%s can not be null or empty", AES_KEY));
String digestAlgorithm = props.getProperty(DIGEST_ALGORITHM_NAME, MessageDigestAlgorithms.SHA_1);
String digestAlgorithm = props.getProperty(DIGEST_ALGORITHM_NAME);
return Arrays.copyOf(DigestUtils.getDigest(digestAlgorithm.toUpperCase()).digest(aesKey.getBytes(StandardCharsets.UTF_8)), 16);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;
import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;

import java.util.Properties;

@Getter
public final class CoreEncryptAlgorithmFixture implements EncryptAlgorithm {

private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(true, true, false);
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(true, true, false, new Properties());

@Override
public String encrypt(final Object plainValue, final AlgorithmSQLContext algorithmSQLContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;
import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;

import java.util.Properties;

@Getter
public final class CoreQueryAssistedEncryptAlgorithmFixture implements EncryptAlgorithm {

private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(false, true, false);
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(false, true, false, new Properties());

@Override
public String encrypt(final Object plainValue, final AlgorithmSQLContext algorithmSQLContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;
import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;

import java.util.Properties;

@Getter
public final class CoreQueryLikeEncryptAlgorithmFixture implements EncryptAlgorithm {

private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(false, false, true);
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(false, false, true, new Properties());

@Override
public String encrypt(final Object plainValue, final AlgorithmSQLContext algorithmSQLContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;
import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;

import java.util.Properties;

@Getter
public final class DistSQLEncryptAlgorithmFixture implements EncryptAlgorithm {

private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(true, true, false);
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(true, true, false, new Properties());

@Override
public String encrypt(final Object plainValue, final AlgorithmSQLContext algorithmSQLContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@ public static <T extends TypedSPI> Optional<T> findService(final Class<T> servic
return Optional.empty();
}

/**
* Find uninited service.
*
* @param serviceInterface typed SPI service interface
* @param type type
* @param <T> SPI class type
* @return found service
*/
public static <T extends TypedSPI> Optional<T> findUninitedService(final Class<T> serviceInterface, final Object type) {
if (null == type) {
return findDefaultService(serviceInterface);
}
for (T each : ShardingSphereServiceLoader.getServiceInstances(serviceInterface)) {
if (matchesType(type, each)) {
return Optional.of(each);
}
}
return Optional.empty();
}

private static <T extends TypedSPI> Optional<T> findDefaultService(final Class<T> serviceInterface) {
for (T each : ShardingSphereServiceLoader.getServiceInstances(serviceInterface)) {
if (!each.isDefault()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ PROPERTIES('minPoolSize'='1','connectionTimeoutMilliseconds'='30000','maxLifetim

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='aes', PROPERTIES('aes-key-value'='123456abc'))), LIKE_QUERY_ALGORITHM(TYPE(NAME='core.query_like.fixture'))),
(NAME=order_id, CIPHER=order_cipher, ENCRYPT_ALGORITHM(TYPE(NAME='aes', PROPERTIES('aes-key-value'='123456abc'))))
(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', 'digest-algorithm-name'='SHA-1'))), ASSISTED_QUERY_ALGORITHM(TYPE(NAME='aes', PROPERTIES('aes-key-value'='123456abc', 'digest-algorithm-name'='SHA-1'))), LIKE_QUERY_ALGORITHM(TYPE(NAME='core.query_like.fixture'))),
(NAME=order_id, CIPHER=order_cipher, ENCRYPT_ALGORITHM(TYPE(NAME='aes', PROPERTIES('aes-key-value'='123456abc', 'digest-algorithm-name'='SHA-1'))))
));
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ TYPE(NAME='random')

CREATE ENCRYPT RULE t_encrypt (
COLUMNS(
(NAME=user_id, CIPHER=user_cipher, ASSISTED_QUERY_COLUMN=user_assisted, ENCRYPT_ALGORITHM(TYPE(NAME='aes', PROPERTIES('aes-key-value'='123456abc'))), ASSISTED_QUERY_ALGORITHM(TYPE(NAME='aes', PROPERTIES('aes-key-value'='123456abc')))),
(NAME=order_id, CIPHER=order_cipher, ENCRYPT_ALGORITHM(TYPE(NAME='aes', PROPERTIES('aes-key-value'='123456abc'))))
(NAME=user_id, CIPHER=user_cipher, ASSISTED_QUERY_COLUMN=user_assisted, ENCRYPT_ALGORITHM(TYPE(NAME='aes', PROPERTIES('aes-key-value'='123456abc', 'digest-algorithm-name'='SHA-1'))), ASSISTED_QUERY_ALGORITHM(TYPE(NAME='aes', PROPERTIES('aes-key-value'='123456abc', 'digest-algorithm-name'='SHA-1')))),
(NAME=order_id, CIPHER=order_cipher, ENCRYPT_ALGORITHM(TYPE(NAME='aes', PROPERTIES('aes-key-value'='123456abc', 'digest-algorithm-name'='SHA-1'))))
));

CREATE SHARDING TABLE RULE t_order (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;
import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;

import java.util.Properties;

@Getter
public final class JDBCEncryptAlgorithmFixture implements EncryptAlgorithm {

private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(true, true, false);
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(true, true, false, new Properties());

@Override
public String encrypt(final Object plainValue, final AlgorithmSQLContext algorithmSQLContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;
import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;

import java.util.Properties;

@Getter
public final class JDBCQueryAssistedEncryptAlgorithmFixture implements EncryptAlgorithm {

private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(false, true, false);
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(false, true, false, new Properties());

@Override
public String encrypt(final Object plainValue, final AlgorithmSQLContext algorithmSQLContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public final class ITEncryptLikeAlgorithmFixture implements EncryptAlgorithm {
private static final int MAX_NUMERIC_LETTER_CHAR = 255;

@Getter
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(false, true, true);
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(false, true, true, new Properties());

private int delta;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
<column name="like_query_type" />
<column name="like_query_props" />
</metadata>
<row values="t_user| pwd| pwd_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abcd&quot;}| | | | " />
<row values="t_user| pwd| pwd_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abcd&quot;,&quot;digest-algorithm-name&quot;:&quot;SHA-1&quot;}| | | | "/>
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
<column name="like_query_type" />
<column name="like_query_props" />
</metadata>
<row values="t_user| pwd| pwd_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;}| | | | " />
<row values="t_user| pwd| pwd_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;,&quot;digest-algorithm-name&quot;:&quot;SHA-1&quot;}| | | | "/>
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
<column name="like_query_type" />
<column name="like_query_props" />
</metadata>
<row values="t_user| pwd| pwd_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;}| | | | " />
<row values="t_user| pwd| pwd_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;,&quot;digest-algorithm-name&quot;:&quot;SHA-1&quot;}| | | | "/>
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
<column name="like_query_type" />
<column name="like_query_props" />
</metadata>
<row values="t_user| pwd| pwd_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;}| | | | " />
<row values="t_user_details| number| number_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;}| | | | " />
<row values="t_user_details| number_new| number_new_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;}| | | | " />
<row values="t_user| pwd| pwd_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;,&quot;digest-algorithm-name&quot;:&quot;SHA-1&quot;}| | | | "/>
<row values="t_user_details| number| number_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;,&quot;digest-algorithm-name&quot;:&quot;SHA-1&quot;}| | | | "/>
<row values="t_user_details| number_new| number_new_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;,&quot;digest-algorithm-name&quot;:&quot;SHA-1&quot;}| | | | "/>
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
<column name="like_query_type" />
<column name="like_query_props" />
</metadata>
<row values="t_user| user_name| user_name_cipher| | user_name_like| AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;}| | | IT.ENCRYPT.LIKE.FIXTURE| {&quot;mask&quot;:4093}" />
<row values="t_user| password| password_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;}| | | | " />
<row values="t_user| email| email_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;}| | | | " />
<row values="t_user| telephone| user_telephone_cipher| | user_telephone_like| AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;}| | | IT.ENCRYPT.LIKE.FIXTURE| {&quot;mask&quot;:4093}" />
<row values="t_user| user_name| user_name_cipher| | user_name_like| AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;,&quot;digest-algorithm-name&quot;:&quot;SHA-1&quot;}| | | IT.ENCRYPT.LIKE.FIXTURE| {&quot;mask&quot;:4093}"/>
<row values="t_user| password| password_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;,&quot;digest-algorithm-name&quot;:&quot;SHA-1&quot;}| | | | "/>
<row values="t_user| email| email_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;,&quot;digest-algorithm-name&quot;:&quot;SHA-1&quot;}| | | | "/>
<row values="t_user| telephone| user_telephone_cipher| | user_telephone_like| AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;,&quot;digest-algorithm-name&quot;:&quot;SHA-1&quot;}| | | IT.ENCRYPT.LIKE.FIXTURE| {&quot;mask&quot;:4093}"/>
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
<column name="like_query_type" />
<column name="like_query_props" />
</metadata>
<row values="t_user| user_name| user_name_cipher| | user_name_like| AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;}| | | IT.ENCRYPT.LIKE.FIXTURE| {&quot;mask&quot;:4093}" />
<row values="t_user| password| password_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;}| | | | " />
<row values="t_user| email| email_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;}| | | | " />
<row values="t_user| telephone| user_telephone_cipher| | user_telephone_like| AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;}| | | IT.ENCRYPT.LIKE.FIXTURE| {&quot;mask&quot;:4093}" />
<row values="t_user_details| number| number_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;}| | | | " />
<row values="t_user_details| number_new| number_new_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;}| | | | " />
<row values="t_merchant| business_code| business_code_cipher| | business_code_like| AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;}| | | IT.ENCRYPT.LIKE.FIXTURE | {&quot;mask&quot;:4093} " />
<row values="t_merchant| telephone| merchant_telephone_cipher| | merchant_telephone_like| AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;}| | | IT.ENCRYPT.LIKE.FIXTURE | {&quot;mask&quot;:4093} " />
<row values="t_user| user_name| user_name_cipher| | user_name_like| AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;,&quot;digest-algorithm-name&quot;:&quot;SHA-1&quot;}| | | IT.ENCRYPT.LIKE.FIXTURE| {&quot;mask&quot;:4093}"/>
<row values="t_user| password| password_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;,&quot;digest-algorithm-name&quot;:&quot;SHA-1&quot;}| | | | "/>
<row values="t_user| email| email_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;,&quot;digest-algorithm-name&quot;:&quot;SHA-1&quot;}| | | | "/>
<row values="t_user| telephone| user_telephone_cipher| | user_telephone_like| AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;,&quot;digest-algorithm-name&quot;:&quot;SHA-1&quot;}| | | IT.ENCRYPT.LIKE.FIXTURE| {&quot;mask&quot;:4093}"/>
<row values="t_user_details| number| number_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;,&quot;digest-algorithm-name&quot;:&quot;SHA-1&quot;}| | | | "/>
<row values="t_user_details| number_new| number_new_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;,&quot;digest-algorithm-name&quot;:&quot;SHA-1&quot;}| | | | "/>
<row values="t_merchant| business_code| business_code_cipher| | business_code_like| AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;,&quot;digest-algorithm-name&quot;:&quot;SHA-1&quot;}| | | IT.ENCRYPT.LIKE.FIXTURE | {&quot;mask&quot;:4093} "/>
<row values="t_merchant| telephone| merchant_telephone_cipher| | merchant_telephone_like| AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;,&quot;digest-algorithm-name&quot;:&quot;SHA-1&quot;}| | | IT.ENCRYPT.LIKE.FIXTURE | {&quot;mask&quot;:4093} "/>
</dataset>
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
<column name="like_query_type" />
<column name="like_query_props" />
</metadata>
<row values="t_user| pwd| pwd_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;}| | | | " />
<row values="t_user| pwd| pwd_cipher| | | AES| {&quot;aes-key-value&quot;:&quot;123456abc&quot;,&quot;digest-algorithm-name&quot;:&quot;SHA-1&quot;}| | | | "/>
</dataset>
Loading

0 comments on commit aa34033

Please sign in to comment.