Skip to content

Commit

Permalink
Add assistedQueryColumn for EncryptRule. (apache#12139)
Browse files Browse the repository at this point in the history
* Add assistedQueryColumn.

* Add newline.

* Add newline.
  • Loading branch information
totalo authored Sep 2, 2021
1 parent f92e878 commit 22116e8
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ private static EncryptTableRuleConfiguration createEncryptTableRuleConfiguration
}

private static EncryptColumnRuleConfiguration createEncryptColumnRuleConfiguration(final String tableName, final EncryptColumnSegment columnSegment) {
// TODO add assistedQueryColumn
return new EncryptColumnRuleConfiguration(columnSegment.getName(), columnSegment.getCipherColumn(), null, columnSegment.getPlainColumn(), getEncryptorName(tableName, columnSegment.getName()));
return new EncryptColumnRuleConfiguration(columnSegment.getName(), columnSegment.getCipherColumn(), columnSegment.getAssistedQueryColumn(),
columnSegment.getPlainColumn(), getEncryptorName(tableName, columnSegment.getName()));
}

private static Map<String, ShardingSphereAlgorithmConfiguration> createEncryptorConfigurations(final EncryptRuleSegment ruleSegment) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ public void assertCovert() {
assertThat(encryptRuleConfiguration.getTables().iterator().next().getColumns().iterator().next().getLogicColumn(), is("user_id"));
assertThat(encryptRuleConfiguration.getTables().iterator().next().getColumns().iterator().next().getCipherColumn(), is("user_cipher"));
assertThat(encryptRuleConfiguration.getTables().iterator().next().getColumns().iterator().next().getPlainColumn(), is("user_plain"));
assertThat(encryptRuleConfiguration.getTables().iterator().next().getColumns().iterator().next().getAssistedQueryColumn(), is("assisted_column"));
assertThat(encryptRuleConfiguration.getTables().iterator().next().getColumns().iterator().next().getEncryptorName(), is("t_encrypt_user_id"));
}

private Collection<EncryptColumnSegment> buildColumns() {
Properties props = new Properties();
props.setProperty("MD5-key", "MD5-value");
return Collections.singleton(new EncryptColumnSegment("user_id", "user_cipher", "user_plain", new AlgorithmSegment("MD5", props)));
return Collections.singleton(new EncryptColumnSegment("user_id", "user_cipher", "user_plain", "assisted_column", new AlgorithmSegment("MD5", props)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void assertCheckSQLStatementWithoutToBeAlteredEncryptors() throws RuleDef
}

private AlterEncryptRuleStatement createSQLStatement(final String encryptorName) {
EncryptColumnSegment columnSegment = new EncryptColumnSegment("user_id", "user_cipher", "user_plain", new AlgorithmSegment(encryptorName, new Properties()));
EncryptColumnSegment columnSegment = new EncryptColumnSegment("user_id", "user_cipher", "user_plain", "assisted_column", new AlgorithmSegment(encryptorName, new Properties()));
EncryptRuleSegment ruleSegment = new EncryptRuleSegment("t_encrypt", Collections.singleton(columnSegment));
return new AlterEncryptRuleStatement(Collections.singleton(ruleSegment));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void assertCheckSQLStatementWithoutToBeCreatedEncryptors() throws RuleDef
}

private CreateEncryptRuleStatement createSQLStatement(final String encryptorName) {
EncryptColumnSegment columnSegment = new EncryptColumnSegment("user_id", "user_cipher", "user_plain", new AlgorithmSegment(encryptorName, new Properties()));
EncryptColumnSegment columnSegment = new EncryptColumnSegment("user_id", "user_cipher", "user_plain", "assisted_column", new AlgorithmSegment(encryptorName, new Properties()));
EncryptRuleSegment ruleSegment = new EncryptRuleSegment("t_encrypt", Collections.singleton(columnSegment));
return new CreateEncryptRuleStatement(Collections.singleton(ruleSegment));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,8 @@ CIPHER
PLAIN
: P L A I N
;

ASSISTED_QUERY_COLUMN
: A S S I S T E D UL_ Q U E R Y UL_ C O L U M N
;

Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ resourceName
;

columnDefinition
: LP NAME EQ columnName (COMMA PLAIN EQ plainColumnName)? COMMA CIPHER EQ cipherColumnName COMMA algorithmDefinition RP
: LP NAME EQ columnName (COMMA PLAIN EQ plainColumnName)? COMMA CIPHER EQ cipherColumnName (COMMA ASSISTED_QUERY_COLUMN EQ assistedQueryColumnName)? COMMA algorithmDefinition RP
;

columnName
Expand All @@ -59,6 +59,10 @@ cipherColumnName
: IDENTIFIER
;

assistedQueryColumnName
: IDENTIFIER
;

algorithmDefinition
: TYPE LP NAME EQ algorithmName (COMMA PROPERTIES LP algorithmProperties? RP)? RP
;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ public ASTNode visitEncryptRuleDefinition(final EncryptRuleDefinitionContext ctx
@Override
public ASTNode visitColumnDefinition(final ColumnDefinitionContext ctx) {
return new EncryptColumnSegment(ctx.columnName().getText(),
ctx.cipherColumnName().getText(), null == ctx.plainColumnName() ? null : ctx.plainColumnName().getText(), (AlgorithmSegment) visit(ctx.algorithmDefinition()));
ctx.cipherColumnName().getText(), null == ctx.plainColumnName() ? null : ctx.plainColumnName().getText(),
null == ctx.assistedQueryColumnName() ? null : ctx.assistedQueryColumnName().getText(), (AlgorithmSegment) visit(ctx.algorithmDefinition()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public final class EncryptColumnSegment implements ASTNode {

private final String plainColumn;

// TODO add assistedQueryColumn
private final String assistedQueryColumn;

private final AlgorithmSegment encryptor;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static void assertIs(final SQLCaseAssertContext assertContext, final Encr
assertThat(assertContext.getText(String.format("`%s`'s assertion error", actual.getClass().getSimpleName())), actual.getName(), is(expected.getName()));
assertThat(assertContext.getText(String.format("`%s`'s assertion error", actual.getClass().getSimpleName())), actual.getPlainColumn(), is(expected.getPlainColumn()));
assertThat(assertContext.getText(String.format("`%s`'s assertion error", actual.getClass().getSimpleName())), actual.getCipherColumn(), is(expected.getCipherColumn()));
assertThat(assertContext.getText(String.format("`%s`'s assertion error", actual.getClass().getSimpleName())), actual.getAssistedQueryColumn(), is(expected.getAssistedQueryColumn()));
AlgorithmAssert.assertIs(assertContext, actual.getEncryptor(), expected.getEncryptor());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public final class ExpectedEncryptColumn extends AbstractExpectedIdentifierSQLSe

@XmlAttribute(name = "cipher-column")
private String cipherColumn;

@XmlAttribute(name = "assisted-query-column")
private String assistedQueryColumn;

@XmlElement
private ExpectedAlgorithm encryptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,19 @@
</column>
</rule>
</alter-encrypt-rule>

<alter-encrypt-rule sql-case-id="alter-encrypt-rule-with-assistedQueryColumn">
<rule name="t_encrypt">
<column name="user_id" plain-column="user_plain" cipher-column="user_cipher" assisted-query-column = "assisted_column">
<encryptor algorithm-name="AES">
<properties>
<property key="aes-key-value" value="123456abc"/>
</properties>
</encryptor>
</column>
<column name="order_id" cipher-column="order_cipher">
<encryptor algorithm-name="MD5"/>
</column>
</rule>
</alter-encrypt-rule>
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,19 @@
</column>
</rule>
</create-encrypt-rule>

<create-encrypt-rule sql-case-id="create-encrypt-rule-with-assistedQueryColumn">
<rule name="t_encrypt">
<column name="user_id" plain-column="user_plain" cipher-column="user_cipher" assisted-query-column = "assisted_column">
<encryptor algorithm-name="AES">
<properties>
<property key="aes-key-value" value="123456abc"/>
</properties>
</encryptor>
</column>
<column name="order_id" cipher-column="order_cipher">
<encryptor algorithm-name="MD5"/>
</column>
</rule>
</create-encrypt-rule>
</sql-parser-test-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@
<distsql-case id="alter-readwrite-splitting-rule" value="ALTER READWRITE_SPLITTING RULE ms_group_0 (AUTO_AWARE_RESOURCE=group_0, TYPE(NAME=random,PROPERTIES(read_weight='2:1'))), ms_group_1 (WRITE_RESOURCE=primary_ds, READ_RESOURCES(replica_ds_0,replica_ds_1),TYPE(NAME=random))" />
<distsql-case id="alter-database-discovery-rule" value="ALTER DB_DISCOVERY RULE ha_group_0 (RESOURCES(resource0,resource1), TYPE(NAME=mgr,PROPERTIES(groupName='92504d5b-6dec',keepAliveCron=''))),ha_group_1 (RESOURCES(resource2,resource3),TYPE(NAME=mgr2,PROPERTIES(groupName='92504d5b-6dec-2',keepAliveCron='')))" />
<distsql-case id="alter-encrypt-rule" value="ALTER ENCRYPT RULE t_encrypt (RESOURCE=ds_1, COLUMNS((NAME=user_id,PLAIN=user_plain,CIPHER=user_cipher,TYPE(NAME=AES,PROPERTIES('aes-key-value'='123456abc'))), (NAME=order_id, CIPHER =order_cipher,TYPE(NAME=MD5))))" />
<distsql-case id="alter-encrypt-rule-with-assistedQueryColumn" value="ALTER ENCRYPT RULE t_encrypt (RESOURCE=ds_1, COLUMNS((NAME=user_id,PLAIN=user_plain,CIPHER=user_cipher,ASSISTED_QUERY_COLUMN=assisted_column, TYPE(NAME=AES,PROPERTIES('aes-key-value'='123456abc'))), (NAME=order_id, CIPHER =order_cipher,TYPE(NAME=MD5))))" />
</sql-cases>
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@
<distsql-case id="create-dynamic-readwrite-splitting-rule" value="CREATE READWRITE_SPLITTING RULE ms_group_1(AUTO_AWARE_RESOURCE=group_0, TYPE(NAME=random,PROPERTIES(read_weight='2:1')))" />
<distsql-case id="create-database-discovery-rule" value="CREATE DB_DISCOVERY RULE ha_group_0 (RESOURCES(resource0,resource1), TYPE(NAME=mgr,PROPERTIES(groupName='92504d5b-6dec',keepAliveCron=''))), ha_group_1 (RESOURCES(resource2,resource3), TYPE(NAME=mgr2,PROPERTIES(groupName='92504d5b-6dec-2',keepAliveCron='')))" />
<distsql-case id="create-encrypt-rule" value="CREATE ENCRYPT RULE t_encrypt (RESOURCE=ds_1, COLUMNS((NAME=user_id,PLAIN=user_plain,CIPHER=user_cipher,TYPE(NAME=AES,PROPERTIES('aes-key-value'='123456abc'))), (NAME=order_id, CIPHER =order_cipher,TYPE(NAME=MD5))))" />
</sql-cases>
<distsql-case id="create-encrypt-rule-with-assistedQueryColumn" value="CREATE ENCRYPT RULE t_encrypt (RESOURCE=ds_1, COLUMNS((NAME=user_id,PLAIN=user_plain,CIPHER=user_cipher,ASSISTED_QUERY_COLUMN=assisted_column, TYPE(NAME=AES,PROPERTIES('aes-key-value'='123456abc'))), (NAME=order_id, CIPHER =order_cipher,TYPE(NAME=MD5))))" />
</sql-cases>

0 comments on commit 22116e8

Please sign in to comment.