未验证 提交 eb29dfd6 编写于 作者: Z zhyee 提交者: GitHub

add encrypt shadow example (#5746)

上级 df08f42a
......@@ -27,6 +27,8 @@ public enum ShardingType {
SHARDING_SHADOW_DATABASES,
ENCRYPT_SHADOW,
MASTER_SLAVE,
SHARDING_MASTER_SLAVE,
......
......@@ -34,6 +34,7 @@ import java.sql.SQLException;
public class JavaConfigurationExampleMain {
private static ShardingType shardingType = ShardingType.SHADOW;
//private static ShardingType shardingType = ShardingType.ENCRYPT_SHADOW;
// private static ShardingType shardingType = ShardingType.SHARDING_SHADOW_DATABASES;
public static void main(final String[] args) throws SQLException {
......
......@@ -31,6 +31,7 @@ import java.sql.SQLException;
public class YamlConfigurationExampleMain {
private static ShardingType shardingType = ShardingType.SHADOW;
// private static ShardingType shardingType = ShardingType.ENCRYPT_SHADOW;
// private static ShardingType shardingType = ShardingType.SHARDING_SHADOW_DATABASES;
public static void main(final String[] args) throws SQLException, IOException {
......
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.shardingsphere.example.shadow.table.raw.jdbc.config;
import org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory;
import org.apache.shardingsphere.encrypt.api.config.EncryptColumnRuleConfiguration;
import org.apache.shardingsphere.encrypt.api.config.EncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.api.config.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.encrypt.api.config.EncryptorRuleConfiguration;
import org.apache.shardingsphere.example.config.ExampleConfiguration;
import org.apache.shardingsphere.example.core.api.DataSourceUtil;
import org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
public final class EncryptShadowDatabasesConfiguration implements ExampleConfiguration {
@Override
public DataSource getDataSource() throws SQLException {
ShadowRuleConfiguration shadowRuleConfiguration = new ShadowRuleConfiguration("shadow", Collections.singletonMap("ds", "ds_0"));
Map<String, DataSource> dataSourceMap = new HashMap<>();
dataSourceMap.put("ds", DataSourceUtil.createDataSource("demo_ds"));
dataSourceMap.put("ds_0", DataSourceUtil.createDataSource("shadow_demo_ds"));
EncryptRuleConfiguration encryptRuleConfiguration = new EncryptRuleConfiguration(getEncryptorRuleConfiguration(), getEncryptTableRuleConfiguration());
Properties properties = new Properties();
properties.setProperty("sql.show", "true");
properties.setProperty("query.with.cipher.column", "true");
return ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, Arrays.asList(shadowRuleConfiguration, encryptRuleConfiguration), properties);
}
private Map<String, EncryptorRuleConfiguration> getEncryptorRuleConfiguration() {
Map<String, EncryptorRuleConfiguration> result = new HashMap<>();
Properties properties = new Properties();
properties.setProperty("aes.key.value", "123456");
EncryptorRuleConfiguration nameEncryptorRuleConfiguration = new EncryptorRuleConfiguration("aes", properties);
EncryptorRuleConfiguration pwdEncryptorRuleConfiguration = new EncryptorRuleConfiguration("assistedTest", null);
result.put("name_encryptror", nameEncryptorRuleConfiguration);
result.put("pwd_encryptror", pwdEncryptorRuleConfiguration);
return result;
}
private Map<String, EncryptTableRuleConfiguration> getEncryptTableRuleConfiguration() {
Map<String, EncryptTableRuleConfiguration> result = new HashMap<>();
Map<String, EncryptColumnRuleConfiguration> columns = new HashMap<>();
columns.put("user_name", new EncryptColumnRuleConfiguration("user_name_plain", "user_name", "", "name_encryptror"));
columns.put("pwd", new EncryptColumnRuleConfiguration("", "pwd", "assisted_query_pwd", "pwd_encryptror"));
result.put("t_user",new EncryptTableRuleConfiguration(columns));
return result;
}
}
......@@ -31,16 +31,11 @@ import java.util.Map;
public final class ShadowDatabasesConfiguration implements ExampleConfiguration {
@Override
public DataSource getDataSource() {
public DataSource getDataSource() throws SQLException {
ShadowRuleConfiguration shadowRuleConfiguration = new ShadowRuleConfiguration("shadow", Collections.singletonMap("ds", "ds_0"));
Map<String, DataSource> dataSourceMap = new HashMap<>();
dataSourceMap.put("ds", DataSourceUtil.createDataSource("demo_ds"));
dataSourceMap.put("ds_0", DataSourceUtil.createDataSource("shadow_demo_ds"));
try {
return ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, Collections.singleton(shadowRuleConfiguration), null);
} catch (final SQLException ex) {
ex.printStackTrace();
return null;
}
return ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, Collections.singleton(shadowRuleConfiguration), null);
}
}
......@@ -17,6 +17,7 @@
package org.apache.shardingsphere.example.shadow.table.raw.jdbc.factory;
import org.apache.shardingsphere.example.shadow.table.raw.jdbc.config.EncryptShadowDatabasesConfiguration;
import org.apache.shardingsphere.example.shadow.table.raw.jdbc.config.ShadowDatabasesConfiguration;
import org.apache.shardingsphere.example.shadow.table.raw.jdbc.config.ShardingShadowDatabasesConfiguration;
import org.apache.shardingsphere.example.type.ShardingType;
......@@ -32,6 +33,8 @@ public class DataSourceFactory {
return new ShadowDatabasesConfiguration().getDataSource();
case SHARDING_SHADOW_DATABASES:
return new ShardingShadowDatabasesConfiguration().getDataSource();
case ENCRYPT_SHADOW:
return new EncryptShadowDatabasesConfiguration().getDataSource();
default:
throw new UnsupportedOperationException(shardingType.name());
}
......
......@@ -33,6 +33,8 @@ public class YamlDataSourceFactory {
return YamlShardingSphereDataSourceFactory.createDataSource(getFile("/META-INF/sharding-shadow-databases.yaml"));
case SHADOW:
return YamlShardingSphereDataSourceFactory.createDataSource(getFile("/META-INF/shadow-databases.yaml"));
case ENCRYPT_SHADOW:
return YamlShardingSphereDataSourceFactory.createDataSource(getFile("/META-INF/encrypt-shadow-databases.yaml"));
default:
throw new UnsupportedOperationException(shardingType.name());
}
......
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
dataSources:
ds: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
ds_0: !!com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/shadow_demo_ds?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
rules:
- !SHADOW
column: shadow
shadowMappings:
ds: ds_0
- !ENCRYPT
encryptors:
name_encryptror:
type: aes
props:
aes.key.value: 123456
pwd_encryptror:
type: assistedTest
tables:
t_user:
columns:
user_name:
plainColumn: user_name_plain
cipherColumn: user_name
encryptor: name_encryptror
pwd:
cipherColumn: pwd
encryptor: pwd_encryptror
assistedQueryColumn: assisted_query_pwd
props:
query.with.cipher.column: true
sql.show: true
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册