blob: cff495eb8df200030060fb17f3058eed53518275 [file] [log] [blame]
package org.apache.shardingsphere.example.config;
import org.apache.shardingsphere.api.config.encrypt.EncryptColumnRuleConfiguration;
import org.apache.shardingsphere.api.config.encrypt.EncryptRuleConfiguration;
import org.apache.shardingsphere.api.config.encrypt.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.api.config.encrypt.EncryptorRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.ShardingRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.TableRuleConfiguration;
import org.apache.shardingsphere.api.config.sharding.strategy.InlineShardingStrategyConfiguration;
import org.apache.shardingsphere.example.core.api.DataSourceUtil;
import org.apache.shardingsphere.example.core.api.DatabaseType;
import org.apache.shardingsphere.orchestration.config.OrchestrationConfiguration;
import org.apache.shardingsphere.orchestration.reg.api.RegistryCenterConfiguration;
import org.apache.shardingsphere.shardingjdbc.orchestration.api.OrchestrationShardingDataSourceFactory;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public class ShardingEncryptConfiguration implements ExampleConfiguration{
@Override
public DataSource getDataSource() throws SQLException {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getTableRuleConfigs().add(getTableRuleConfiguration());
shardingRuleConfig.setEncryptRuleConfig(getEncryptRuleConfiguration());
return OrchestrationShardingDataSourceFactory.createDataSource(
createDataSourceMap()
, shardingRuleConfig
, new Properties()
,new OrchestrationConfiguration("orchestration-postgresql-sharding-encrypt",getRegistryCenterConfiguration(),true));
}
private static TableRuleConfiguration getTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration("t_user", "demo_ds_${0..1}.t_user_${[0, 1]}");
result.setDatabaseShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id", "demo_ds_${user_id % 2}"));
result.setTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("user_id","t_user_${user_id % 2}"));
return result;
}
private EncryptRuleConfiguration getEncryptRuleConfiguration() {
Properties props = new Properties();
props.setProperty("aes.key.value", "123456");
EncryptorRuleConfiguration encryptorAES = new EncryptorRuleConfiguration("AES", props);
EncryptorRuleConfiguration encryptorMD5 = new EncryptorRuleConfiguration("MD5",new Properties());
Map<String, EncryptColumnRuleConfiguration> columns = new HashMap<>();
EncryptColumnRuleConfiguration columnUserName = new EncryptColumnRuleConfiguration("user_name", "user_name_cipher", "", "aes");
EncryptColumnRuleConfiguration columnPwd = new EncryptColumnRuleConfiguration("pwd_plain", "pwd_cipher", "", "md5");
columns.put("user_name",columnUserName);
columns.put("pwd",columnPwd);
EncryptTableRuleConfiguration tableConfig = new EncryptTableRuleConfiguration(columns);
EncryptRuleConfiguration result = new EncryptRuleConfiguration();
result.getEncryptors().put("aes", encryptorAES);
result.getEncryptors().put("md5",encryptorMD5);
result.getTables().put("t_user", tableConfig);
return result;
}
private static Map<String, DataSource> createDataSourceMap() {
Map<String, DataSource> result = new HashMap<>();
result.put("demo_ds_0", DataSourceUtil.createDataSource("demo_ds_0", DatabaseType.POSTGRESQL));
result.put("demo_ds_1", DataSourceUtil.createDataSource("demo_ds_1", DatabaseType.POSTGRESQL));
return result;
}
private RegistryCenterConfiguration getRegistryCenterConfiguration() {
RegistryCenterConfiguration regConfig = new RegistryCenterConfiguration("zookeeper");
regConfig.setServerLists("localhost:2181");
regConfig.setNamespace("orchestration-raw-jdbc-postgresql");
return regConfig;
}
private static Properties getProperties() {
Properties result = new Properties();
result.setProperty("worker.id", "123");
return result;
}
}