Skip to content

Commit

Permalink
Refactor support encrypt config queryWithCipherColumn with column levels
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaoguhong committed May 18, 2022
1 parent 5baaccd commit 3acb32a
Show file tree
Hide file tree
Showing 25 changed files with 45 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,8 @@ public final class EncryptColumnRuleConfiguration {

private final Boolean queryWithCipherColumn;

public EncryptColumnRuleConfiguration(final String logicColumn, final String cipherColumn, final String assistedQueryColumn, final String plainColumn, final String encryptorName) {
this(logicColumn, null, cipherColumn, null, assistedQueryColumn, null, plainColumn, null, encryptorName, null);
}

public EncryptColumnRuleConfiguration(final String logicColumn, final String cipherColumn, final String assistedQueryColumn, final String plainColumn, final String encryptorName,
final Boolean queryWithCipherColumn) {
this(logicColumn, null, cipherColumn, null, assistedQueryColumn, null, plainColumn, null, encryptorName, queryWithCipherColumn);
}

public EncryptColumnRuleConfiguration(final String logicColumn, final String logicDataType, final String cipherColumn, final String cipherDataType, final String assistedQueryColumn,
final String assistedQueryDataType, final String plainColumn, final String plainDataType, final String encryptorName) {
this(logicColumn, logicDataType, cipherColumn, cipherDataType, assistedQueryColumn, assistedQueryDataType, plainColumn, plainDataType, encryptorName, null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ public final class EncryptColumn {

private final Boolean queryWithCipherColumn;

public EncryptColumn(final EncryptColumnDataType logicDataType, final String cipherColumn, final EncryptColumnDataType cipherDataType, final String assistedQueryColumn,
final EncryptColumnDataType assistedQueryDataType, final String plainColumn, final EncryptColumnDataType plainDataType, final String encryptorName) {
this(logicDataType, cipherColumn, cipherDataType, assistedQueryColumn, assistedQueryDataType, plainColumn, plainDataType, encryptorName, null);
}

/**
* Get assisted query column.
*
Expand All @@ -71,5 +66,4 @@ public Optional<String> getAssistedQueryColumn() {
public Optional<String> getPlainColumn() {
return Strings.isNullOrEmpty(plainColumn) ? Optional.empty() : Optional.of(plainColumn);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -284,16 +284,6 @@ public Optional<String> findPlainColumn(final String logicTable, final String lo
return originColumnName.isPresent() && tables.containsKey(logicTable.toLowerCase()) ? tables.get(logicTable.toLowerCase()).findPlainColumn(originColumnName.get()) : Optional.empty();
}

/**
* Judge whether table is support QueryWithCipherColumn or not.
*
* @param tableName table name
* @return whether table is support QueryWithCipherColumn or not
*/
public boolean isQueryWithCipherColumn(final String tableName) {
return findEncryptTable(tableName).flatMap(EncryptTable::getQueryWithCipherColumn).orElse(queryWithCipherColumn);
}

/**
* Judge whether column is support QueryWithCipherColumn or not.
*
Expand All @@ -302,7 +292,7 @@ public boolean isQueryWithCipherColumn(final String tableName) {
* @return whether column is support QueryWithCipherColumn or not
*/
public boolean isQueryWithCipherColumn(final String logicTable, final String logicColumn) {
return findEncryptTable(logicTable).flatMap(encryptTable -> encryptTable.getQueryWithCipherColumn(logicColumn)).orElse(isQueryWithCipherColumn(logicTable));
return findEncryptTable(logicTable).flatMap(encryptTable -> encryptTable.getQueryWithCipherColumn(logicColumn)).orElse(queryWithCipherColumn);
}

private Optional<String> findOriginColumnName(final String logicTable, final String logicColumn) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public Optional<Boolean> getQueryWithCipherColumn() {
* @return query with cipher column
*/
public Optional<Boolean> getQueryWithCipherColumn(final String logicColumn) {
return findEncryptColumn(logicColumn).map(EncryptColumn::getQueryWithCipherColumn);
return Optional.ofNullable(findEncryptColumn(logicColumn).map(EncryptColumn::getQueryWithCipherColumn).orElse(queryWithCipherColumn));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ private EncryptColumn mockEncryptColumn() {
EncryptColumnDataType assistedQueryDataType = new EncryptColumnDataType("varchar(200) not null", dataTypes);
EncryptColumnDataType plainDataType = new EncryptColumnDataType("int(20) unsigned not null default 0", dataTypes);
return new EncryptColumn(logicDataType, "cipher_certificate_number", cipherDataType, "assisted_certificate_number",
assistedQueryDataType, "certificate_number_plain", plainDataType, "test");
assistedQueryDataType, "certificate_number_plain", plainDataType, "test", null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ private EncryptColumn mockEncryptColumn() {
EncryptColumnDataType cipherDataType = new EncryptColumnDataType("varchar(200) not null default ''", dataTypes);
EncryptColumnDataType assistedQueryDataType = new EncryptColumnDataType("varchar(200) not null", dataTypes);
EncryptColumnDataType plainDataType = new EncryptColumnDataType("int(20) unsigned not null default 0", dataTypes);
return new EncryptColumn(logicDataType, "cipher_certificate_number", cipherDataType, "assisted_certificate_number", assistedQueryDataType, "certificate_number_plain", plainDataType, "test");
return new EncryptColumn(logicDataType, "cipher_certificate_number", cipherDataType, "assisted_certificate_number", assistedQueryDataType, "certificate_number_plain", plainDataType,
"test", null);
}

private EncryptColumn mockNewEncryptColumn() {
Expand All @@ -101,7 +102,7 @@ private EncryptColumn mockNewEncryptColumn() {
EncryptColumnDataType assistedQueryDataType = new EncryptColumnDataType("varchar(200) not null", dataTypes);
EncryptColumnDataType plainDataType = new EncryptColumnDataType("int(20) unsigned not null default 0", dataTypes);
return new EncryptColumn(logicDataType, "cipher_certificate_number_new", cipherDataType,
"assisted_certificate_number_new", assistedQueryDataType, "certificate_number_new_plain", plainDataType, "test");
"assisted_certificate_number_new", assistedQueryDataType, "certificate_number_new_plain", plainDataType, "test", null);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,6 @@ private EncryptColumn mockEncryptColumn() {
EncryptColumnDataType assistedQueryDataType = new EncryptColumnDataType("varchar(200) not null", dataTypes);
EncryptColumnDataType plainDataType = new EncryptColumnDataType("int(20) unsigned not null default 0", dataTypes);
return new EncryptColumn(logicDataType, "cipher_certificate_number", cipherDataType,
"assisted_certificate_number", assistedQueryDataType, "certificate_number_plain", plainDataType, "test");
"assisted_certificate_number", assistedQueryDataType, "certificate_number_plain", plainDataType, "test", null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private EncryptRule mockEncryptRule() {
when(result.findPlainColumn("doctor1", "mobile")).thenReturn(Optional.of("Mobile"));
when(result.findEncryptTable("doctor")).thenReturn(Optional.of(encryptTable1));
when(result.findEncryptTable("doctor1")).thenReturn(Optional.of(encryptTable2));
EncryptColumn column = new EncryptColumn(null, "mobile", null, null, null, "mobile", null, null);
EncryptColumn column = new EncryptColumn(null, "mobile", null, null, null, "mobile", null, null, null);
when(result.findEncryptColumn("doctor", "mobile")).thenReturn(Optional.of(column));
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ public final class EncryptColumnTest {

@Test
public void assertGetAssistedQueryColumn() {
assertTrue(new EncryptColumn(null, "cipherColumn", null, "assistedQueryColumn", null, "plainColumn", null, "encryptorName").getAssistedQueryColumn().isPresent());
assertTrue(new EncryptColumn(null, "cipherColumn", null, "assistedQueryColumn", null, "plainColumn", null, "encryptorName", null).getAssistedQueryColumn().isPresent());
}

@Test
public void assertGetPlainColumn() {
assertTrue(new EncryptColumn(null, "cipherColumn", null, "assistedQueryColumn", null, "plainColumn", null, "encryptorName").getPlainColumn().isPresent());
assertTrue(new EncryptColumn(null, "cipherColumn", null, "assistedQueryColumn", null, "plainColumn", null, "encryptorName", null).getPlainColumn().isPresent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public final class EncryptRuleTest {

@Test
public void assertNewInstanceWithAlgorithmProvidedEncryptRuleConfiguration() {
EncryptColumnRuleConfiguration encryptColumnConfig = new EncryptColumnRuleConfiguration("encrypt_column", "encrypt_cipher", "", "", "test_encryptor");
EncryptColumnRuleConfiguration encryptColumnConfig = new EncryptColumnRuleConfiguration("encrypt_column", "encrypt_cipher", "", "", "test_encryptor", null);
EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration("t_encrypt", Collections.singletonList(encryptColumnConfig), null);
AlgorithmProvidedEncryptRuleConfiguration ruleConfig = new AlgorithmProvidedEncryptRuleConfiguration(
Collections.singleton(tableConfig), Collections.singletonMap("test_encryptor", new CoreEncryptAlgorithmFixture()), true);
Expand All @@ -61,15 +61,15 @@ public void assertNewInstanceWithAlgorithmProvidedEncryptRuleConfiguration() {
@Test(expected = IllegalArgumentException.class)
public void assertNewInstanceWithInvalidConfiguration() {
ShardingSphereAlgorithmConfiguration encryptAlgorithmConfig = new ShardingSphereAlgorithmConfiguration("CORE.FIXTURE", new Properties());
EncryptColumnRuleConfiguration encryptColumnConfig = new EncryptColumnRuleConfiguration("encrypt_column", "encrypt_cipher", "", "", "test_encryptor");
EncryptColumnRuleConfiguration encryptColumnConfig = new EncryptColumnRuleConfiguration("encrypt_column", "encrypt_cipher", "", "", "test_encryptor", null);
EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration("t_encrypt", Collections.singletonList(encryptColumnConfig), null);
EncryptRuleConfiguration ruleConfig = new EncryptRuleConfiguration(Collections.singleton(tableConfig), Collections.singletonMap("invalid_encryptor", encryptAlgorithmConfig));
new EncryptRule(ruleConfig, Collections.emptyMap());
}

@Test(expected = IllegalArgumentException.class)
public void assertNewInstanceWithInvalidAlgorithmProvidedEncryptRuleConfiguration() {
EncryptColumnRuleConfiguration encryptColumnConfig = new EncryptColumnRuleConfiguration("encrypt_column", "encrypt_cipher", "", "", "test_encryptor");
EncryptColumnRuleConfiguration encryptColumnConfig = new EncryptColumnRuleConfiguration("encrypt_column", "encrypt_cipher", "", "", "test_encryptor", null);
EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration("t_encrypt", Collections.singletonList(encryptColumnConfig), null);
AlgorithmProvidedEncryptRuleConfiguration ruleConfig = new AlgorithmProvidedEncryptRuleConfiguration(
Collections.singleton(tableConfig), Collections.singletonMap("invalid_encryptor", new CoreEncryptAlgorithmFixture()), true);
Expand Down Expand Up @@ -148,7 +148,6 @@ public void assertIsQueryWithCipherColumn() {
AlgorithmProvidedEncryptRuleConfiguration ruleConfig = new AlgorithmProvidedEncryptRuleConfiguration(
Collections.singleton(tableConfig), Collections.singletonMap("test_encryptor", new CoreEncryptAlgorithmFixture()), true);
EncryptRule actual = new EncryptRule(ruleConfig, Collections.emptyMap());
assertTrue(actual.isQueryWithCipherColumn("t_encrypt"));
assertFalse(actual.isQueryWithCipherColumn("t_encrypt", "encrypt_column"));
}

Expand Down Expand Up @@ -186,19 +185,19 @@ public void assertGetSchemaMetaData() {
private EncryptRuleConfiguration createEncryptRuleConfiguration() {
ShardingSphereAlgorithmConfiguration queryAssistedEncryptConfig = new ShardingSphereAlgorithmConfiguration("CORE.QUERY_ASSISTED.FIXTURE", new Properties());
ShardingSphereAlgorithmConfiguration metaDataAwareEncryptConfig = new ShardingSphereAlgorithmConfiguration("CORE.METADATA_AWARE.FIXTURE", new Properties());
EncryptColumnRuleConfiguration pwdColumnConfig = new EncryptColumnRuleConfiguration("pwd", "pwd_cipher", "", "pwd_plain", "test_encryptor");
EncryptColumnRuleConfiguration creditCardColumnConfig = new EncryptColumnRuleConfiguration("credit_card", "credit_card_cipher", "", "credit_card_plain", "test_encryptor");
EncryptColumnRuleConfiguration nameColumnConfig = new EncryptColumnRuleConfiguration("name", "name_cipher", "", "name_plain", "customized_encryptor");
EncryptColumnRuleConfiguration pwdColumnConfig = new EncryptColumnRuleConfiguration("pwd", "pwd_cipher", "", "pwd_plain", "test_encryptor", null);
EncryptColumnRuleConfiguration creditCardColumnConfig = new EncryptColumnRuleConfiguration("credit_card", "credit_card_cipher", "", "credit_card_plain", "test_encryptor", null);
EncryptColumnRuleConfiguration nameColumnConfig = new EncryptColumnRuleConfiguration("name", "name_cipher", "", "name_plain", "customized_encryptor", null);
EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration("t_encrypt", Arrays.asList(pwdColumnConfig, creditCardColumnConfig, nameColumnConfig), null);
return new EncryptRuleConfiguration(Collections.singleton(tableConfig), getEncryptors(queryAssistedEncryptConfig, metaDataAwareEncryptConfig));
}

private EncryptRuleConfiguration createEncryptRuleConfigurationWithUpperCaseLogicTable() {
ShardingSphereAlgorithmConfiguration queryAssistedEncryptConfig = new ShardingSphereAlgorithmConfiguration("CORE.QUERY_ASSISTED.FIXTURE", new Properties());
ShardingSphereAlgorithmConfiguration metaDataAwareEncryptConfig = new ShardingSphereAlgorithmConfiguration("CORE.METADATA_AWARE.FIXTURE", new Properties());
EncryptColumnRuleConfiguration pwdColumnConfig = new EncryptColumnRuleConfiguration("pwd", "pwd_cipher", "", "pwd_plain", "test_encryptor");
EncryptColumnRuleConfiguration creditCardColumnConfig = new EncryptColumnRuleConfiguration("credit_card", "credit_card_cipher", "", "credit_card_plain", "test_encryptor");
EncryptColumnRuleConfiguration nameColumnConfig = new EncryptColumnRuleConfiguration("name", "name_cipher", "", "name_plain", "customized_encryptor");
EncryptColumnRuleConfiguration pwdColumnConfig = new EncryptColumnRuleConfiguration("pwd", "pwd_cipher", "", "pwd_plain", "test_encryptor", null);
EncryptColumnRuleConfiguration creditCardColumnConfig = new EncryptColumnRuleConfiguration("credit_card", "credit_card_cipher", "", "credit_card_plain", "test_encryptor", null);
EncryptColumnRuleConfiguration nameColumnConfig = new EncryptColumnRuleConfiguration("name", "name_cipher", "", "name_plain", "customized_encryptor", null);
EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration("T_ENCRYPT", Arrays.asList(pwdColumnConfig, creditCardColumnConfig, nameColumnConfig), null);
return new EncryptRuleConfiguration(Collections.singleton(tableConfig), getEncryptors(queryAssistedEncryptConfig, metaDataAwareEncryptConfig));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class EncryptTableTest {
@Before
public void setUp() {
encryptTable = new EncryptTable(new EncryptTableRuleConfiguration("t_encrypt",
Collections.singleton(new EncryptColumnRuleConfiguration("logicColumn", "cipherColumn", "assistedQueryColumn", "plainColumn", "myEncryptor")), null), Collections.emptyMap());
Collections.singleton(new EncryptColumnRuleConfiguration("logicColumn", "cipherColumn", "assistedQueryColumn", "plainColumn", "myEncryptor", null)), null), Collections.emptyMap());
}

@Test
Expand Down Expand Up @@ -124,7 +124,7 @@ public void assertGetLogicAndCipherColumns() {
@Test
public void assertGetQueryWithCipherColumn() {
encryptTable = new EncryptTable(new EncryptTableRuleConfiguration("t_encrypt",
Collections.singleton(new EncryptColumnRuleConfiguration("logicColumn", "cipherColumn", "assistedQueryColumn", "plainColumn", "myEncryptor")), true), Collections.emptyMap());
Collections.singleton(new EncryptColumnRuleConfiguration("logicColumn", "cipherColumn", "assistedQueryColumn", "plainColumn", "myEncryptor", null)), true), Collections.emptyMap());
Optional<Boolean> actual = encryptTable.getQueryWithCipherColumn();
assertTrue(actual.isPresent());
assertTrue(actual.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ public final class EncryptTableRuleConfigurationYamlSwapperTest {
@Test
public void assertSwapToYamlConfiguration() {
Collection<EncryptColumnRuleConfiguration> encryptColumnRuleConfigs = Arrays.asList(
new EncryptColumnRuleConfiguration("encrypt_column_1", "encrypt_cipher_1", "", "", "test_encryptor_1"),
new EncryptColumnRuleConfiguration("encrypt_column_2", "encrypt_cipher_2", "", "", "test_encryptor_2"),
new EncryptColumnRuleConfiguration("encrypt_column_3", "encrypt_cipher_3", "", "", "test_encryptor_3"));
new EncryptColumnRuleConfiguration("encrypt_column_1", "encrypt_cipher_1", "", "", "test_encryptor_1", null),
new EncryptColumnRuleConfiguration("encrypt_column_2", "encrypt_cipher_2", "", "", "test_encryptor_2", null),
new EncryptColumnRuleConfiguration("encrypt_column_3", "encrypt_cipher_3", "", "", "test_encryptor_3", null));
EncryptTableRuleConfiguration encryptTableRuleConfig = new EncryptTableRuleConfiguration("test_table", encryptColumnRuleConfigs, null);
YamlEncryptTableRuleConfiguration actualYamlEncryptTableRuleConfig = tableRuleConfigYamlSwapper.swapToYamlConfiguration(encryptTableRuleConfig);
Map<String, YamlEncryptColumnRuleConfiguration> actualColumns = actualYamlEncryptTableRuleConfig.getColumns();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ private static EncryptTableRuleConfiguration createEncryptTableRuleConfiguration
private static EncryptColumnRuleConfiguration createEncryptColumnRuleConfiguration(final String tableName, final EncryptColumnSegment columnSegment) {
return new EncryptColumnRuleConfiguration(columnSegment.getName(), columnSegment.getDataType(), columnSegment.getCipherColumn(),
columnSegment.getCipherDataType(), columnSegment.getAssistedQueryColumn(), columnSegment.getAssistedQueryDataType(),
columnSegment.getPlainColumn(), columnSegment.getPlainDataType(), getEncryptorName(tableName, columnSegment.getName()));
columnSegment.getPlainColumn(), columnSegment.getPlainDataType(), getEncryptorName(tableName, columnSegment.getName()), columnSegment.getQueryWithCipherColumn());
}

private static Map<String, ShardingSphereAlgorithmConfiguration> createEncryptorConfigurations(final EncryptRuleSegment ruleSegment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void assertCovert() {
}

private Collection<EncryptColumnSegment> createColumns() {
return Collections.singleton(new EncryptColumnSegment("user_id", "user_cipher", "user_plain", "assisted_column", new AlgorithmSegment("MD5", createProperties())));
return Collections.singleton(new EncryptColumnSegment("user_id", "user_cipher", "user_plain", "assisted_column", new AlgorithmSegment("MD5", createProperties()), null));
}

private Properties createProperties() {
Expand Down
Loading

0 comments on commit 3acb32a

Please sign in to comment.