Skip to content

Commit

Permalink
Change the digest-algorithm-name property of AESEncryptAlgorithm to r…
Browse files Browse the repository at this point in the history
…equire.
  • Loading branch information
iamhucong committed Jul 24, 2024
1 parent 80fb2de commit 251c58a
Show file tree
Hide file tree
Showing 62 changed files with 74 additions and 133 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ weight = 5

可配置属性:

| *名称* | *数据类型* | *说明* |
|-----------------------|--------|------------------------------|
| aes-key-value | String | AES 使用的 KEY |
| digest-algorithm-name | String | AES KEY 的摘要算法 (可选,默认值:SHA-1) |
| *名称* | *数据类型* | *说明* |
|-----------------------|--------|---------------|
| aes-key-value | String | AES 使用的 KEY |
| digest-algorithm-name | String | AES KEY 的摘要算法 |

### 辅助查询加密算法

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ Type: AES

Attributes:

| *Name* | *DataType* | *Description* |
|-----------------------|------------|-----------------------------------------------------|
| aes-key-value | String | AES KEY |
| digest-algorithm-name | String | AES KEY DIGEST ALGORITHM (optional, default: SHA-1) |
| *Name* | *DataType* | *Description* |
|-----------------------|------------|--------------------------|
| aes-key-value | String | AES KEY |
| digest-algorithm-name | String | AES KEY DIGEST ALGORITHM |

### Assisted Encrypt Algorithm

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,26 @@
package org.apache.shardingsphere.encrypt.config;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.encrypt.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.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

/**
* 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 = rebuildEncryptorsWithDefaultProperties(encryptors);
}

private Map<String, AlgorithmConfiguration> rebuildEncryptorsWithDefaultProperties(final Map<String, AlgorithmConfiguration> encryptors) {
Map<String, AlgorithmConfiguration> result = new HashMap<>(encryptors.size(), 1F);
for (Entry<String, AlgorithmConfiguration> entry : encryptors.entrySet()) {
// todo Replace with MultiSourceProperties, MultiSourceProperties need support marshal.
Properties props = new Properties();
props.putAll(entry.getValue().getProps());
Properties defaultProps = TypedSPILoader.findUninitedService(EncryptAlgorithm.class, entry.getValue().getType()).map(EncryptAlgorithm::getMetaData)
.map(EncryptAlgorithmMetaData::getDefaultProps).orElseGet(Properties::new);
defaultProps.forEach(props::putIfAbsent);
result.put(entry.getKey(), new AlgorithmConfiguration(entry.getValue().getType(), props));
}
return result;
}

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

import java.util.Properties;

/**
* Encrypt algorithm meta data.
*/
Expand All @@ -36,6 +34,4 @@ 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, new Properties());
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(false, true, false);

private MessageDigestAlgorithm digestAlgorithm;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@
import lombok.Getter;
import lombok.SneakyThrows;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.codec.digest.MessageDigestAlgorithms;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithmMetaData;
import org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;
import org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
import org.apache.shardingsphere.infra.util.props.MultiSourceProperties;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
Expand All @@ -48,26 +46,20 @@ 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, getDefaultProperties());
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(true, true, false);

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) {
Properties multiSourceProperties = new MultiSourceProperties(props, metaData.getDefaultProps());
secretKey = getSecretKey(multiSourceProperties);
secretKey = getSecretKey(props);
}

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);
ShardingSpherePreconditions.checkNotEmpty(digestAlgorithm, () -> new AlgorithmInitializationException(this, "%s can not be null or empty", 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 @@ -26,11 +26,9 @@
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Answers;
import org.mockito.MockedStatic;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand All @@ -45,27 +43,17 @@ class AESEncryptAlgorithmTest {

@BeforeEach
void setUp() {
encryptAlgorithm = TypedSPILoader.getService(EncryptAlgorithm.class, "AES", PropertiesBuilder.build(new Property("aes-key-value", "test")));
encryptAlgorithm = TypedSPILoader.getService(EncryptAlgorithm.class, "AES", PropertiesBuilder.build(new Property("aes-key-value", "test"), new Property("digest-algorithm-name", "SHA-1")));
}

@Test
void assertDefaultDigestAlgorithm() throws NoSuchAlgorithmException {
MockedStatic<DigestUtils> digestUtilsMockedStatic = mockStatic(DigestUtils.class);
digestUtilsMockedStatic.when(() -> DigestUtils.getDigest("SHA-1")).thenReturn(MessageDigest.getInstance("SHA-1"));
TypedSPILoader.getService(EncryptAlgorithm.class, "AES", PropertiesBuilder.build(new Property("aes-key-value", "test")));
void assertDigestAlgorithm() {
MockedStatic<DigestUtils> digestUtilsMockedStatic = mockStatic(DigestUtils.class, Answers.CALLS_REAL_METHODS);
TypedSPILoader.getService(EncryptAlgorithm.class, "AES", PropertiesBuilder.build(new Property("aes-key-value", "test"), new Property("digest-algorithm-name", "SHA-1")));
digestUtilsMockedStatic.verify(() -> DigestUtils.getDigest("SHA-1"), times(1));
digestUtilsMockedStatic.close();
}

@Test
void assertSHA512DigestAlgorithm() throws NoSuchAlgorithmException {
MockedStatic<DigestUtils> digestUtilsMockedStatic = mockStatic(DigestUtils.class);
digestUtilsMockedStatic.when(() -> DigestUtils.getDigest("SHA-512")).thenReturn(MessageDigest.getInstance("SHA-512"));
TypedSPILoader.getService(EncryptAlgorithm.class, "AES", PropertiesBuilder.build(new Property("aes-key-value", "test"), new Property("digest-algorithm-name", "SHA-512")));
digestUtilsMockedStatic.verify(() -> DigestUtils.getDigest("SHA-512"), times(1));
digestUtilsMockedStatic.close();
}

@Test
void assertCreateNewInstanceWithoutAESKey() {
assertThrows(AlgorithmInitializationException.class, () -> TypedSPILoader.getService(EncryptAlgorithm.class, "AES"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
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, new Properties());
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(true, true, false);

@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,12 +22,10 @@
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, new Properties());
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(false, true, false);

@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,12 +22,10 @@
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, new Properties());
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(false, false, true);

@Override
public String encrypt(final Object plainValue, final AlgorithmSQLContext algorithmSQLContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.apache.shardingsphere.encrypt.rule;

import org.apache.commons.codec.digest.MessageDigestAlgorithms;
import org.apache.shardingsphere.encrypt.config.EncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.config.rule.EncryptColumnItemRuleConfiguration;
import org.apache.shardingsphere.encrypt.config.rule.EncryptColumnRuleConfiguration;
Expand Down Expand Up @@ -94,18 +93,6 @@ void assertLikeQueryEncryptorNameSpecified() {
assertThat(pwdColumnConfig.getLikeQuery().get().getEncryptorName(), is("like_query_test_encryptor"));
}

@Test
void assertAESEncryptRuleDefaultProps() {
EncryptRuleConfiguration defaultPropsEncryptRuleConfig = new EncryptRuleConfiguration(Collections.emptyList(),
Collections.singletonMap("aes_encryptor", new AlgorithmConfiguration("AES", new Properties())));
assertThat(defaultPropsEncryptRuleConfig.getEncryptors().get("aes_encryptor").getProps().getProperty(DIGEST_ALGORITHM_NAME), is(MessageDigestAlgorithms.SHA_1));
Properties props = new Properties();
props.put(DIGEST_ALGORITHM_NAME, MessageDigestAlgorithms.SHA_256);
EncryptRuleConfiguration sha256EncryptRuleConfig = new EncryptRuleConfiguration(Collections.emptyList(),
Collections.singletonMap("aes_encryptor", new AlgorithmConfiguration("AES", props)));
assertThat(sha256EncryptRuleConfig.getEncryptors().get("aes_encryptor").getProps().getProperty(DIGEST_ALGORITHM_NAME), is(MessageDigestAlgorithms.SHA_256));
}

private Map<String, AlgorithmConfiguration> getEncryptors(final AlgorithmConfiguration standardEncryptConfig, final AlgorithmConfiguration queryAssistedEncryptConfig,
final AlgorithmConfiguration queryLikeEncryptConfig) {
Map<String, AlgorithmConfiguration> result = new HashMap<>(3, 1F);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ rules:
type: AES
props:
aes-key-value: 123456abc
digest-algorithm-name: SHA-1
assisted_encryptor:
type: AES
props:
aes-key-value: 123456abc
digest-algorithm-name: SHA-1
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
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, new Properties());
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(true, true, false);

@Override
public String encrypt(final Object plainValue, final AlgorithmSQLContext algorithmSQLContext) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ void assertCreateEncryptRuleWithIfNotExists() {

private CreateEncryptRuleStatement createAESEncryptRuleSQLStatement(final boolean ifNotExists) {
EncryptColumnSegment encryptColumnSegment = new EncryptColumnSegment("user_id",
new EncryptColumnItemSegment("user_cipher", new AlgorithmSegment("AES", PropertiesBuilder.build(new Property("aes-key-value", "abc")))),
new EncryptColumnItemSegment("user_cipher", new AlgorithmSegment("AES", PropertiesBuilder.build(new Property("aes-key-value", "abc"), new Property("digest-algorithm-name", "SHA-1")))),
new EncryptColumnItemSegment("assisted_column", null),
new EncryptColumnItemSegment("like_column", null));
Collection<EncryptRuleSegment> rules = new LinkedList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,26 +65,6 @@ 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 @@ -46,3 +46,4 @@ rules:
type: AES
props:
aes-key-value: 123456abc
digest-algorithm-name: SHA-1
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ rules:
type: AES
props:
aes-key-value: 123456abc
digest-algorithm-name: SHA-1
like_encryptor:
type: CORE.QUERY_LIKE.FIXTURE
tables:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ rules:
type: AES
props:
aes-key-value: 123456abc
digest-algorithm-name: SHA-1
tables:
t_encrypt:
columns:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ rules:
type: AES
props:
aes-key-value: 123456abc
digest-algorithm-name: SHA-1
tables:
t_encrypt:
columns:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
# type: AES
# props:
# aes-key-value: 123456abc
# digest-algorithm-name: SHA-1
# tables:
# t_encrypt:
# columns:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@
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, new Properties());
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(true, true, false);

@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,12 +22,10 @@
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, new Properties());
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(false, true, false);

@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, new Properties());
private final EncryptAlgorithmMetaData metaData = new EncryptAlgorithmMetaData(false, true, true);

private int delta;

Expand Down
Loading

0 comments on commit 251c58a

Please sign in to comment.