未验证 提交 5f8d1639 编写于 作者: L Liang Zhang 提交者: GitHub

Redesign spring boot starter API for encrypt (#5488)

* Add single data source API for ShardingDataSourceFactory

* fix example

* Add single data source API for YamlShardingDataSourceFactory

* fix example

* refactor AbstractEncryptJDBCDatabaseAndTableTest

* remove YamlEncryptDataSourceFactory

* refactor ShadowDataSourceFactory

* refactor spring boot starter for encrypt
上级 58199d17
......@@ -23,10 +23,10 @@ spring.shardingsphere.datasource.ds_encrypt.jdbc-url=jdbc:mysql://localhost:3306
spring.shardingsphere.datasource.ds_encrypt.username=root
spring.shardingsphere.datasource.ds_encrypt.password=
spring.shardingsphere.encrypt.encryptors.status_encryptor.type=aes
spring.shardingsphere.encrypt.encryptors.status_encryptor.props.aes.key.value=123456
spring.shardingsphere.encrypt.tables.t_order.columns.status.cipherColumn=status
spring.shardingsphere.encrypt.tables.t_order.columns.status.encryptor=status_encryptor
spring.shardingsphere.rules.encrypt-rule.encryptors.status_encryptor.type=aes
spring.shardingsphere.rules.encrypt-rule.encryptors.status_encryptor.props.aes.key.value=123456
spring.shardingsphere.rules.encrypt-rule.tables.t_order.columns.status.cipherColumn=status
spring.shardingsphere.rules.encrypt-rule.tables.t_order.columns.status.encryptor=status_encryptor
spring.shardingsphere.props.query.with.cipher.comlum=true
spring.shardingsphere.props.sql.show=true
......
......@@ -23,10 +23,10 @@ spring.shardingsphere.datasource.ds_encrypt.jdbc-url=jdbc:mysql://localhost:3306
spring.shardingsphere.datasource.ds_encrypt.username=root
spring.shardingsphere.datasource.ds_encrypt.password=
spring.shardingsphere.encrypt.encryptors.status_encryptor.type=aes
spring.shardingsphere.encrypt.encryptors.status_encryptor.props.aes.key.value=123456
spring.shardingsphere.encrypt.tables.t_order.columns.status.cipherColumn=status
spring.shardingsphere.encrypt.tables.t_order.columns.status.encryptor=status_encryptor
spring.shardingsphere.rules.encrypt-rule.encryptors.status_encryptor.type=aes
spring.shardingsphere.rules.encrypt-rule.encryptors.status_encryptor.props.aes.key.value=123456
spring.shardingsphere.rules.encrypt-rule.tables.t_order.columns.status.cipherColumn=status
spring.shardingsphere.rules.encrypt-rule.tables.t_order.columns.status.encryptor=status_encryptor
spring.shardingsphere.props.query.with.cipher.comlum=true
spring.shardingsphere.props.sql.show=true
......
......@@ -19,7 +19,7 @@ spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
spring.jpa.properties.hibernate.show_sql=false
#spring.profiles.active=local-zookeeper-sharding-databases-tables
spring.profiles.active=local-zookeeper-sharding-databases-tables
#spring.profiles.active=local-zookeeper-master-slave
#spring.profiles.active=cloud-zookeeper-sharding-databases-tables
......
......@@ -21,16 +21,17 @@ import org.apache.shardingsphere.example.core.api.ExampleExecuteTemplate;
import org.apache.shardingsphere.example.core.api.service.ExampleService;
import org.apache.shardingsphere.example.core.jdbc.repository.UserRepositoryImpl;
import org.apache.shardingsphere.example.core.jdbc.service.UserServiceImpl;
import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlEncryptDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlShardingDataSourceFactory;
import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
public class YamlConfigurationExampleMain {
public static void main(final String[] args) throws SQLException {
DataSource dataSource = YamlEncryptDataSourceFactory.createDataSource(getFile());
public static void main(final String[] args) throws SQLException, IOException {
DataSource dataSource = YamlShardingDataSourceFactory.createDataSource(getFile());
ExampleExecuteTemplate.run(getExampleService(dataSource));
}
......
......@@ -23,10 +23,11 @@ import org.apache.shardingsphere.encrypt.api.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.encrypt.api.EncryptorRuleConfiguration;
import org.apache.shardingsphere.example.config.ExampleConfiguration;
import org.apache.shardingsphere.example.core.api.DataSourceUtil;
import org.apache.shardingsphere.shardingjdbc.api.EncryptDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
......@@ -52,7 +53,7 @@ public class EncryptDatabasesConfiguration implements ExampleConfiguration {
tables.put("t_user", tableConfig);
EncryptRuleConfiguration encryptRuleConfiguration = new EncryptRuleConfiguration(encryptors, tables);
try {
return EncryptDataSourceFactory.createDataSource(DataSourceUtil.createDataSource("demo_ds"), encryptRuleConfiguration, properties);
return ShardingDataSourceFactory.createDataSource(DataSourceUtil.createDataSource("demo_ds"), Collections.singleton(encryptRuleConfiguration), properties);
} catch (final SQLException ex) {
ex.printStackTrace();
return null;
......
......@@ -15,11 +15,12 @@
# limitations under the License.
#
dataSource: !!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:
dataSources:
unique_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:
encryptRule:
encryptors:
......@@ -42,4 +43,4 @@ encryptRule:
assistedQueryColumn: assisted_query_pwd
props:
query.with.cipher.column: true
\ No newline at end of file
query.with.cipher.column: true
......@@ -23,16 +23,14 @@ spring.shardingsphere.datasource.ds.jdbc-url=jdbc:mysql://localhost:3306/demo_ds
spring.shardingsphere.datasource.ds.username=root
spring.shardingsphere.datasource.ds.password=
spring.shardingsphere.encrypt.encryptors.name_encryptor.type=aes
spring.shardingsphere.encrypt.encryptors.name_encryptor.props.aes.key.value=123456
spring.shardingsphere.encrypt.encryptors.pwd_encryptor.type=assistedTest
spring.shardingsphere.encrypt.tables.t_user.columns.user_name.plainColumn=user_name_plain
spring.shardingsphere.encrypt.tables.t_user.columns.user_name.cipherColumn=user_name
spring.shardingsphere.encrypt.tables.t_user.columns.user_name.encryptor=name_encryptor
spring.shardingsphere.encrypt.tables.t_user.columns.pwd.cipherColumn=pwd
spring.shardingsphere.encrypt.tables.t_user.columns.pwd.assistedQueryColumn=assisted_query_pwd
spring.shardingsphere.encrypt.tables.t_user.columns.pwd.encryptor=pwd_encryptor
spring.shardingsphere.rules.encrypt-rule.encryptors.name_encryptor.type=aes
spring.shardingsphere.rules.encrypt-rule.encryptors.name_encryptor.props.aes.key.value=123456
spring.shardingsphere.rules.encrypt-rule.encryptors.pwd_encryptor.type=assistedTest
spring.shardingsphere.rules.encrypt-rule.tables.t_user.columns.user_name.plainColumn=user_name_plain
spring.shardingsphere.rules.encrypt-rule.tables.t_user.columns.user_name.cipherColumn=user_name
spring.shardingsphere.rules.encrypt-rule.tables.t_user.columns.user_name.encryptor=name_encryptor
spring.shardingsphere.rules.encrypt-rule.tables.t_user.columns.pwd.cipherColumn=pwd
spring.shardingsphere.rules.encrypt-rule.tables.t_user.columns.pwd.assistedQueryColumn=assisted_query_pwd
spring.shardingsphere.rules.encrypt-rule.tables.t_user.columns.pwd.encryptor=pwd_encryptor
spring.shardingsphere.props.query.with.cipher.comlum=true
......@@ -22,6 +22,8 @@ import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import org.apache.shardingsphere.api.config.shadow.ShadowRuleConfiguration;
import org.apache.shardingsphere.core.rule.ShadowRule;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.EncryptDataSource;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShadowDataSource;
import javax.sql.DataSource;
......@@ -81,7 +83,7 @@ public final class ShadowDataSourceFactory {
@SneakyThrows
private static DataSource createFacadeDataSource(final Map<String, DataSource> dataSources, final ShadowRuleConfiguration shadowRule, final Properties props) {
if (shadowRule.isEncrypt()) {
return EncryptDataSourceFactory.createDataSource(dataSources.values().iterator().next(), shadowRule.getEncryptRuleConfig(), props);
return new EncryptDataSource(dataSources.values().iterator().next(), new EncryptRule(shadowRule.getEncryptRuleConfig()), props);
} else if (shadowRule.isSharding()) {
return ShardingDataSourceFactory.createDataSource(dataSources, Collections.singletonList(shadowRule.getShardingRuleConfig()), props);
} else if (shadowRule.isMasterSlave()) {
......
......@@ -26,6 +26,7 @@ import org.apache.shardingsphere.underlying.common.rule.ShardingSphereRulesBuild
import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
......@@ -47,4 +48,19 @@ public final class ShardingDataSourceFactory {
public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final Collection<RuleConfiguration> ruleConfigurations, final Properties props) throws SQLException {
return new ShardingDataSource(dataSourceMap, ShardingSphereRulesBuilder.build(ruleConfigurations, dataSourceMap.keySet()), props);
}
/**
* Create sharding data source.
*
* @param dataSource data source
* @param ruleConfigurations rule configurations
* @param props properties for data source
* @return sharding data source
* @throws SQLException SQL exception
*/
public static DataSource createDataSource(final DataSource dataSource, final Collection<RuleConfiguration> ruleConfigurations, final Properties props) throws SQLException {
Map<String, DataSource> dataSourceMap = new HashMap<>(1, 1);
dataSourceMap.put("unique_ds", dataSource);
return createDataSource(dataSourceMap, ruleConfigurations, props);
}
}
/*
* 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.shardingjdbc.api.yaml;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import org.apache.shardingsphere.encrypt.yaml.config.YamlRootEncryptRuleConfiguration;
import org.apache.shardingsphere.underlying.common.yaml.engine.YamlEngine;
import org.apache.shardingsphere.encrypt.yaml.swapper.EncryptRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.shardingjdbc.api.EncryptDataSourceFactory;
import javax.sql.DataSource;
import java.io.File;
/**
* Encrypt data source factory for YAML.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class YamlEncryptDataSourceFactory {
/**
* Create encrypt data source.
*
* @param yamlFile YAML file for encrypt rule configuration with data sources
* @return encrypt data source
*/
@SneakyThrows
public static DataSource createDataSource(final File yamlFile) {
YamlRootEncryptRuleConfiguration config = YamlEngine.unmarshal(yamlFile, YamlRootEncryptRuleConfiguration.class);
return EncryptDataSourceFactory.createDataSource(config.getDataSource(), new EncryptRuleConfigurationYamlSwapper().swap(config.getEncryptRule()), config.getProps());
}
/**
* Create encrypt data source.
*
* @param yamlBytes YAML bytes for encrypt rule configuration with data sources
* @return encrypt data source
*/
@SneakyThrows
public static DataSource createDataSource(final byte[] yamlBytes) {
YamlRootEncryptRuleConfiguration config = YamlEngine.unmarshal(yamlBytes, YamlRootEncryptRuleConfiguration.class);
return EncryptDataSourceFactory.createDataSource(config.getDataSource(), new EncryptRuleConfigurationYamlSwapper().swap(config.getEncryptRule()), config.getProps());
}
/**
* Create encrypt data source.
*
* @param dataSource data source
* @param yamlFile YAML file for encrypt rule configuration without data sources
* @return encrypt data source
*/
@SneakyThrows
public static DataSource createDataSource(final DataSource dataSource, final File yamlFile) {
YamlRootEncryptRuleConfiguration config = YamlEngine.unmarshal(yamlFile, YamlRootEncryptRuleConfiguration.class);
return EncryptDataSourceFactory.createDataSource(dataSource, new EncryptRuleConfigurationYamlSwapper().swap(config.getEncryptRule()), config.getProps());
}
/**
* Create encrypt data source.
*
* @param dataSource data source
* @param yamlBytes YAML bytes for encrypt rule configuration without data sources
* @return encrypt data source
*/
@SneakyThrows
public static DataSource createDataSource(final DataSource dataSource, final byte[] yamlBytes) {
YamlRootEncryptRuleConfiguration config = YamlEngine.unmarshal(yamlBytes, YamlRootEncryptRuleConfiguration.class);
return EncryptDataSourceFactory.createDataSource(dataSource, new EncryptRuleConfigurationYamlSwapper().swap(config.getEncryptRule()), config.getProps());
}
}
......@@ -29,6 +29,7 @@ import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
/**
......@@ -77,6 +78,21 @@ public final class YamlShardingDataSourceFactory {
return ShardingDataSourceFactory.createDataSource(dataSourceMap, new RuleRootConfigurationsYamlSwapper().swap(configurations), configurations.getProps());
}
/**
* Create sharding data source.
*
* @param dataSource data source
* @param yamlFile YAML file for rule configuration of databases and tables sharding without data sources
* @return sharding data source
* @throws SQLException SQL exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final DataSource dataSource, final File yamlFile) throws SQLException, IOException {
Map<String, DataSource> dataSourceMap = new HashMap<>(1, 1);
dataSourceMap.put("unique_ds", dataSource);
return createDataSource(dataSourceMap, yamlFile);
}
/**
* Create sharding data source.
*
......@@ -90,4 +106,19 @@ public final class YamlShardingDataSourceFactory {
YamlRootRuleConfigurations configurations = YamlEngine.unmarshal(yamlBytes, YamlRootRuleConfigurations.class, new YamlRootRuleConfigurationsConstructor());
return ShardingDataSourceFactory.createDataSource(dataSourceMap, new RuleRootConfigurationsYamlSwapper().swap(configurations), configurations.getProps());
}
/**
* Create sharding data source.
*
* @param dataSource data source
* @param yamlBytes YAML bytes for rule configuration of databases and tables sharding without data sources
* @return sharding data source
* @throws SQLException SQL exception
* @throws IOException IO exception
*/
public static DataSource createDataSource(final DataSource dataSource, final byte[] yamlBytes) throws SQLException, IOException {
Map<String, DataSource> dataSourceMap = new HashMap<>(1, 1);
dataSourceMap.put("unique_ds", dataSource);
return createDataSource(dataSourceMap, yamlBytes);
}
}
......@@ -21,10 +21,10 @@ import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import org.apache.shardingsphere.encrypt.yaml.config.YamlRootEncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.yaml.swapper.EncryptRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.shardingjdbc.api.EncryptDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlEncryptDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.EncryptConnection;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.EncryptDataSource;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlShardingDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.ShardingConnection;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource;
import org.apache.shardingsphere.underlying.common.yaml.engine.YamlEngine;
import org.h2.tools.RunScript;
import org.junit.AfterClass;
......@@ -43,9 +43,9 @@ import java.util.Properties;
public abstract class AbstractEncryptJDBCDatabaseAndTableTest extends AbstractSQLTest {
private static EncryptDataSource encryptDataSource;
private static ShardingDataSource encryptDataSource;
private static EncryptDataSource encryptDataSourceWithProps;
private static ShardingDataSource encryptDataSourceWithProps;
private static final List<String> ENCRYPT_DB_NAMES = Collections.singletonList("encrypt");
......@@ -58,8 +58,8 @@ public abstract class AbstractEncryptJDBCDatabaseAndTableTest extends AbstractSQ
}
File encryptFile = getFile(ENCRYPT_CONFIG_FILE);
DataSource dataSource = getDataSources().values().iterator().next();
encryptDataSource = (EncryptDataSource) createDataSourceWithEmptyProps(dataSource, encryptFile);
encryptDataSourceWithProps = (EncryptDataSource) YamlEncryptDataSourceFactory.createDataSource(dataSource, encryptFile);
encryptDataSource = (ShardingDataSource) createDataSourceWithEmptyProps(dataSource, encryptFile);
encryptDataSourceWithProps = (ShardingDataSource) YamlShardingDataSourceFactory.createDataSource(dataSource, encryptFile);
}
private static File getFile(final String fileName) {
......@@ -72,23 +72,23 @@ public abstract class AbstractEncryptJDBCDatabaseAndTableTest extends AbstractSQ
private static DataSource createDataSourceWithEmptyProps(final DataSource dataSource, final File yamlFile) throws IOException, SQLException {
YamlRootEncryptRuleConfiguration config = YamlEngine.unmarshal(yamlFile, YamlRootEncryptRuleConfiguration.class);
return EncryptDataSourceFactory.createDataSource(dataSource, new EncryptRuleConfigurationYamlSwapper().swap(config.getEncryptRule()), new Properties());
return ShardingDataSourceFactory.createDataSource(dataSource, Collections.singletonList(new EncryptRuleConfigurationYamlSwapper().swap(config.getEncryptRule())), new Properties());
}
@Before
public void initTable() {
try (EncryptConnection connection = encryptDataSource.getConnection()) {
try (ShardingConnection connection = encryptDataSource.getConnection()) {
RunScript.execute(connection, new InputStreamReader(AbstractSQLTest.class.getClassLoader().getResourceAsStream("encrypt_data.sql")));
} catch (final SQLException ex) {
ex.printStackTrace();
}
}
protected final EncryptConnection getEncryptConnection() throws SQLException {
protected final ShardingConnection getEncryptConnection() {
return encryptDataSource.getConnection();
}
protected final EncryptConnection getEncryptConnectionWithProps() throws SQLException {
protected final ShardingConnection getEncryptConnectionWithProps() {
return encryptDataSourceWithProps.getConnection();
}
......
......@@ -18,6 +18,7 @@
package org.apache.shardingsphere.shardingjdbc.jdbc.core.resultset;
import org.apache.shardingsphere.shardingjdbc.common.base.AbstractEncryptJDBCDatabaseAndTableTest;
import org.junit.Ignore;
import org.junit.Test;
import java.sql.ResultSet;
......@@ -28,6 +29,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
@Ignore
public final class EncryptResultSetTest extends AbstractEncryptJDBCDatabaseAndTableTest {
private static final String SELECT_SQL_TO_ASSERT = "SELECT id, cipher_pwd, plain_pwd FROM t_encrypt";
......
......@@ -24,22 +24,26 @@ import org.apache.shardingsphere.encrypt.api.EncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.api.EncryptTableRuleConfiguration;
import org.apache.shardingsphere.encrypt.api.EncryptorRuleConfiguration;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.encrypt.yaml.config.YamlRootEncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.yaml.swapper.EncryptRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.orchestration.center.config.CenterConfiguration;
import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfiguration;
import org.apache.shardingsphere.orchestration.core.common.CenterType;
import org.apache.shardingsphere.orchestration.core.common.event.DataSourceChangedEvent;
import org.apache.shardingsphere.orchestration.core.common.event.EncryptRuleChangedEvent;
import org.apache.shardingsphere.orchestration.core.common.event.PropertiesChangedEvent;
import org.apache.shardingsphere.shardingjdbc.api.yaml.YamlEncryptDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.EncryptDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.connection.EncryptConnection;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.EncryptDataSource;
import org.apache.shardingsphere.underlying.common.config.DataSourceConfiguration;
import org.apache.shardingsphere.orchestration.center.config.CenterConfiguration;
import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfiguration;
import org.apache.shardingsphere.underlying.common.database.DefaultSchema;
import org.apache.shardingsphere.underlying.common.yaml.engine.YamlEngine;
import org.junit.Before;
import org.junit.Test;
import javax.sql.DataSource;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.util.Collections;
......@@ -62,9 +66,14 @@ public final class OrchestrationEncryptDataSourceTest {
encryptDataSource = new OrchestrationEncryptDataSource(getEncryptDatasource(), getOrchestrationConfiguration());
}
private EncryptDataSource getEncryptDatasource() throws URISyntaxException {
private EncryptDataSource getEncryptDatasource() throws URISyntaxException, IOException, SQLException {
File yamlFile = new File(OrchestrationEncryptDataSource.class.getResource("/yaml/unit/encrypt.yaml").toURI());
return (EncryptDataSource) YamlEncryptDataSourceFactory.createDataSource(yamlFile);
return (EncryptDataSource) createDataSource(yamlFile);
}
private DataSource createDataSource(final File yamlFile) throws IOException, SQLException {
YamlRootEncryptRuleConfiguration config = YamlEngine.unmarshal(yamlFile, YamlRootEncryptRuleConfiguration.class);
return EncryptDataSourceFactory.createDataSource(config.getDataSource(), new EncryptRuleConfigurationYamlSwapper().swap(config.getEncryptRule()), config.getProps());
}
private OrchestrationConfiguration getOrchestrationConfiguration() {
......
......@@ -22,21 +22,15 @@ import com.google.common.collect.Maps;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.strategy.algorithm.sharding.inline.InlineExpressionParser;
import org.apache.shardingsphere.core.yaml.swapper.root.RuleRootConfigurationsYamlSwapper;
import org.apache.shardingsphere.encrypt.api.EncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.encrypt.yaml.swapper.EncryptRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.orchestration.center.config.CenterConfiguration;
import org.apache.shardingsphere.orchestration.center.config.OrchestrationConfiguration;
import org.apache.shardingsphere.orchestration.center.yaml.config.YamlCenterRepositoryConfiguration;
import org.apache.shardingsphere.orchestration.center.yaml.swapper.CenterRepositoryConfigurationYamlSwapper;
import org.apache.shardingsphere.orchestration.core.facade.ShardingOrchestrationFacade;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.EncryptDataSource;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource;
import org.apache.shardingsphere.shardingjdbc.orchestration.internal.datasource.OrchestrationEncryptDataSource;
import org.apache.shardingsphere.shardingjdbc.orchestration.internal.datasource.OrchestrationShardingDataSource;
import org.apache.shardingsphere.shardingjdbc.orchestration.spring.boot.common.SpringBootRootConfigurationProperties;
import org.apache.shardingsphere.shardingjdbc.orchestration.spring.boot.encrypt.LocalEncryptRuleCondition;
import org.apache.shardingsphere.shardingjdbc.orchestration.spring.boot.encrypt.SpringBootEncryptRuleConfigurationProperties;
import org.apache.shardingsphere.shardingjdbc.orchestration.spring.boot.sharding.LocalShardingRuleCondition;
import org.apache.shardingsphere.shardingjdbc.orchestration.spring.boot.sharding.OrchestrationSpringBootRulesConfigurationProperties;
import org.apache.shardingsphere.spring.boot.datasource.DataSourcePropertiesSetterHolder;
......@@ -73,7 +67,7 @@ import java.util.Map.Entry;
*/
@Configuration
@ComponentScan("org.apache.shardingsphere.spring.boot.converter")
@EnableConfigurationProperties({OrchestrationSpringBootRulesConfigurationProperties.class, SpringBootRootConfigurationProperties.class, SpringBootEncryptRuleConfigurationProperties.class})
@EnableConfigurationProperties({OrchestrationSpringBootRulesConfigurationProperties.class, SpringBootRootConfigurationProperties.class})
@ConditionalOnProperty(prefix = "spring.shardingsphere", name = "enabled", havingValue = "true", matchIfMissing = true)
@RequiredArgsConstructor
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
......@@ -83,8 +77,6 @@ public class OrchestrationSpringBootConfiguration implements EnvironmentAware {
private final OrchestrationSpringBootRulesConfigurationProperties rules;
private final SpringBootEncryptRuleConfigurationProperties encryptRule;
private final SpringBootRootConfigurationProperties root;
private final CenterRepositoryConfigurationYamlSwapper configurationYamlSwapper = new CenterRepositoryConfigurationYamlSwapper();
......@@ -122,21 +114,6 @@ public class OrchestrationSpringBootConfiguration implements EnvironmentAware {
ShardingSphereRulesBuilder.build(new RuleRootConfigurationsYamlSwapper().swap(rules), dataSourceMap.keySet()), root.getProps()), orchestrationConfiguration);
}
/**
* Get orchestration encrypt data source bean by local configuration.
*
* @param orchestrationConfiguration orchestration configuration
* @return orchestration encrypt data source bean
* @throws SQLException SQL exception
*/
@Bean
@Conditional(LocalEncryptRuleCondition.class)
public DataSource encryptDataSourceByLocal(final OrchestrationConfiguration orchestrationConfiguration) throws SQLException {
EncryptRuleConfiguration encryptRuleConfig = new EncryptRuleConfigurationYamlSwapper().swap(encryptRule);
return new OrchestrationEncryptDataSource(
new EncryptDataSource(dataSourceMap.values().iterator().next(), new EncryptRule(encryptRuleConfig), root.getProps()), orchestrationConfiguration);
}
/**
* Get data source bean from registry center.
*
......
/*
* 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.shardingjdbc.orchestration.spring.boot.encrypt;
import org.apache.shardingsphere.spring.boot.util.PropertyUtil;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
* Local encrypt rule condition.
*/
public final class LocalEncryptRuleCondition extends SpringBootCondition {
private static final String ENCRYPT_ENCRYPTORS_PREFIX = "spring.shardingsphere.encrypt.encryptors";
private static final String ENCRYPT_TABLES_PREFIX = "spring.shardingsphere.encrypt.tables";
@Override
public ConditionOutcome getMatchOutcome(final ConditionContext conditionContext, final AnnotatedTypeMetadata annotatedTypeMetadata) {
boolean isEncrypt = PropertyUtil.containPropertyPrefix(conditionContext.getEnvironment(), ENCRYPT_ENCRYPTORS_PREFIX)
&& PropertyUtil.containPropertyPrefix(conditionContext.getEnvironment(), ENCRYPT_TABLES_PREFIX);
return isEncrypt ? ConditionOutcome.match() : ConditionOutcome.noMatch("Can't find ShardingSphere encrypt rule configuration in environment.");
}
}
/*
* 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.shardingjdbc.orchestration.spring.boot.encrypt;
import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Encrypt rule configuration properties.
*/
@ConfigurationProperties(prefix = "spring.shardingsphere.encrypt")
public final class SpringBootEncryptRuleConfigurationProperties extends YamlEncryptRuleConfiguration {
}
......@@ -21,8 +21,8 @@ import lombok.SneakyThrows;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.shardingsphere.encrypt.api.EncryptRuleConfiguration;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.EncryptDataSource;
import org.apache.shardingsphere.shardingjdbc.orchestration.internal.datasource.OrchestrationEncryptDataSource;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource;
import org.apache.shardingsphere.shardingjdbc.orchestration.internal.datasource.OrchestrationShardingDataSource;
import org.apache.shardingsphere.shardingjdbc.orchestration.spring.boot.util.EmbedTestingServer;
import org.junit.BeforeClass;
import org.junit.Test;
......@@ -57,14 +57,14 @@ public class OrchestrationSpringBootEncryptTest {
@Test
@SneakyThrows
public void assertWithEncryptDataSource() {
assertTrue(dataSource instanceof OrchestrationEncryptDataSource);
Field field = OrchestrationEncryptDataSource.class.getDeclaredField("dataSource");
assertTrue(dataSource instanceof OrchestrationShardingDataSource);
Field field = OrchestrationShardingDataSource.class.getDeclaredField("dataSource");
field.setAccessible(true);
EncryptDataSource encryptDataSource = (EncryptDataSource) field.get(dataSource);
BasicDataSource embedDataSource = (BasicDataSource) encryptDataSource.getDataSource();
ShardingDataSource shardingDataSource = (ShardingDataSource) field.get(dataSource);
BasicDataSource embedDataSource = (BasicDataSource) shardingDataSource.getDataSourceMap().values().iterator().next();
assertThat(embedDataSource.getMaxTotal(), is(100));
assertThat(embedDataSource.getUsername(), is("sa"));
EncryptRuleConfiguration encryptRuleConfig = ((EncryptRule) encryptDataSource.getRuntimeContext().getRules().iterator().next()).getRuleConfiguration();
EncryptRuleConfiguration encryptRuleConfig = ((EncryptRule) shardingDataSource.getRuntimeContext().getRules().iterator().next()).getRuleConfiguration();
assertThat(encryptRuleConfig.getEncryptors().size(), is(1));
assertTrue(encryptRuleConfig.getEncryptors().containsKey("order_encrypt"));
assertThat(encryptRuleConfig.getEncryptors().get("order_encrypt").getType(), is("aes"));
......
......@@ -24,11 +24,11 @@ spring.shardingsphere.datasource.ds.username=sa
spring.shardingsphere.datasource.ds.password=
spring.shardingsphere.datasource.ds.max-total=100
spring.shardingsphere.encrypt.encryptors.order_encrypt.type=aes
spring.shardingsphere.encrypt.encryptors.order_encrypt.props.aes.key.value=123456
spring.shardingsphere.encrypt.tables.t_order.columns.order_id.cipherColumn=cipher_order_id
spring.shardingsphere.encrypt.tables.t_order.columns.order_id.plainColumn=plain_order_id
spring.shardingsphere.encrypt.tables.t_order.columns.order_id.encryptor=order_encrypt
spring.shardingsphere.rules.encrypt-rule.encryptors.order_encrypt.type=aes
spring.shardingsphere.rules.encrypt-rule.encryptors.order_encrypt.props.aes.key.value=123456
spring.shardingsphere.rules.encrypt-rule.tables.t_order.columns.order_id.cipherColumn=cipher_order_id
spring.shardingsphere.rules.encrypt-rule.tables.t_order.columns.order_id.plainColumn=plain_order_id
spring.shardingsphere.rules.encrypt-rule.tables.t_order.columns.order_id.encryptor=order_encrypt
spring.shardingsphere.props.sql.show=true
......
......@@ -22,13 +22,9 @@ import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.core.strategy.algorithm.sharding.inline.InlineExpressionParser;
import org.apache.shardingsphere.core.yaml.swapper.ShadowRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.core.yaml.swapper.root.RuleRootConfigurationsYamlSwapper;
import org.apache.shardingsphere.encrypt.yaml.swapper.EncryptRuleConfigurationYamlSwapper;
import org.apache.shardingsphere.shardingjdbc.api.EncryptDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.ShadowDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.spring.boot.common.SpringBootPropertiesConfigurationProperties;
import org.apache.shardingsphere.shardingjdbc.spring.boot.encrypt.EncryptRuleCondition;
import org.apache.shardingsphere.shardingjdbc.spring.boot.encrypt.SpringBootEncryptRuleConfigurationProperties;
import org.apache.shardingsphere.shardingjdbc.spring.boot.shadow.ShadowRuleCondition;
import org.apache.shardingsphere.shardingjdbc.spring.boot.shadow.SpringBootShadowRuleConfigurationProperties;
import org.apache.shardingsphere.shardingjdbc.spring.boot.sharding.ShardingRuleCondition;
......@@ -64,9 +60,7 @@ import java.util.Map;
*/
@Configuration
@ComponentScan("org.apache.shardingsphere.spring.boot.converter")
@EnableConfigurationProperties({
SpringBootRulesConfigurationProperties.class, SpringBootEncryptRuleConfigurationProperties.class,
SpringBootPropertiesConfigurationProperties.class, SpringBootShadowRuleConfigurationProperties.class})
@EnableConfigurationProperties({SpringBootRulesConfigurationProperties.class, SpringBootPropertiesConfigurationProperties.class, SpringBootShadowRuleConfigurationProperties.class})
@ConditionalOnProperty(prefix = "spring.shardingsphere", name = "enabled", havingValue = "true", matchIfMissing = true)
@AutoConfigureBefore(DataSourceAutoConfiguration.class)
@RequiredArgsConstructor
......@@ -74,8 +68,6 @@ public class SpringBootConfiguration implements EnvironmentAware {
private final SpringBootRulesConfigurationProperties rules;
private final SpringBootEncryptRuleConfigurationProperties encryptRule;
private final SpringBootShadowRuleConfigurationProperties shadowRule;
private final SpringBootPropertiesConfigurationProperties props;
......@@ -96,18 +88,6 @@ public class SpringBootConfiguration implements EnvironmentAware {
return ShardingDataSourceFactory.createDataSource(dataSourceMap, new RuleRootConfigurationsYamlSwapper().swap(rules), props.getProps());
}
/**
* Get encrypt data source bean.
*
* @return data source bean
* @throws SQLException SQL exception
*/
@Bean
@Conditional(EncryptRuleCondition.class)
public DataSource encryptDataSource() throws SQLException {
return EncryptDataSourceFactory.createDataSource(dataSourceMap.values().iterator().next(), new EncryptRuleConfigurationYamlSwapper().swap(encryptRule), props.getProps());
}
/**
* Get shadow data source bean.
*
......
/*
* 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.shardingjdbc.spring.boot.encrypt;
import org.apache.shardingsphere.spring.boot.util.PropertyUtil;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.type.AnnotatedTypeMetadata;
/**
* Encrypt condition.
*/
public final class EncryptRuleCondition extends SpringBootCondition {
private static final String ENCRYPT_ENCRYPTORS_PREFIX = "spring.shardingsphere.encrypt.encryptors";
private static final String ENCRYPT_TABLES_PREFIX = "spring.shardingsphere.encrypt.tables";
@Override
public ConditionOutcome getMatchOutcome(final ConditionContext conditionContext, final AnnotatedTypeMetadata annotatedTypeMetadata) {
boolean isEncrypt = PropertyUtil.containPropertyPrefix(conditionContext.getEnvironment(), ENCRYPT_ENCRYPTORS_PREFIX)
&& PropertyUtil.containPropertyPrefix(conditionContext.getEnvironment(), ENCRYPT_TABLES_PREFIX);
return isEncrypt ? ConditionOutcome.match() : ConditionOutcome.noMatch("Can't find ShardingSphere encrypt rule configuration in environment.");
}
}
/*
* 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.shardingjdbc.spring.boot.encrypt;
import org.apache.shardingsphere.encrypt.yaml.config.YamlEncryptRuleConfiguration;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* Encrypt rule configuration properties.
*/
@ConfigurationProperties(prefix = "spring.shardingsphere.encrypt")
public class SpringBootEncryptRuleConfigurationProperties extends YamlEncryptRuleConfiguration {
}
......@@ -17,7 +17,6 @@
package org.apache.shardingsphere.shardingjdbc.spring.boot.sharding;
import org.apache.shardingsphere.shardingjdbc.spring.boot.encrypt.EncryptRuleCondition;
import org.apache.shardingsphere.shardingjdbc.spring.boot.shadow.ShadowRuleCondition;
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
import org.springframework.boot.autoconfigure.condition.SpringBootCondition;
......@@ -31,8 +30,7 @@ public final class ShardingRuleCondition extends SpringBootCondition {
@Override
public ConditionOutcome getMatchOutcome(final ConditionContext conditionContext, final AnnotatedTypeMetadata annotatedTypeMetadata) {
boolean isEncryptRule = new EncryptRuleCondition().getMatchOutcome(conditionContext, annotatedTypeMetadata).isMatch();
boolean isShadow = new ShadowRuleCondition().getMatchOutcome(conditionContext, annotatedTypeMetadata).isMatch();
return isEncryptRule || isShadow ? ConditionOutcome.noMatch("Have found master-slave or encrypt rule in environment") : ConditionOutcome.match();
return isShadow ? ConditionOutcome.noMatch("Have found master-slave or encrypt rule in environment") : ConditionOutcome.match();
}
}
......@@ -19,7 +19,7 @@ package org.apache.shardingsphere.shardingjdbc.spring.boot.type;
import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.shardingsphere.encrypt.rule.EncryptRule;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.EncryptDataSource;
import org.apache.shardingsphere.shardingjdbc.jdbc.core.datasource.ShardingDataSource;
import org.apache.shardingsphere.underlying.common.config.properties.ConfigurationPropertyKey;
import org.junit.Test;
import org.junit.runner.RunWith;
......@@ -30,7 +30,6 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.Optional;
import static org.hamcrest.CoreMatchers.is;
......@@ -48,19 +47,19 @@ public class SpringBootEncryptTest {
@Test
public void assertSqlShow() {
assertTrue(((EncryptDataSource) dataSource).getRuntimeContext().getProperties().<Boolean>getValue(ConfigurationPropertyKey.SQL_SHOW));
assertTrue(((ShardingDataSource) dataSource).getRuntimeContext().getProperties().<Boolean>getValue(ConfigurationPropertyKey.SQL_SHOW));
}
@Test
public void assertWithEncryptDataSource() {
assertTrue(dataSource instanceof EncryptDataSource);
BasicDataSource basicDataSource = (BasicDataSource) ((EncryptDataSource) dataSource).getDataSource();
assertTrue(dataSource instanceof ShardingDataSource);
BasicDataSource basicDataSource = (BasicDataSource) ((ShardingDataSource) dataSource).getDataSourceMap().values().iterator().next();
assertThat(basicDataSource.getMaxTotal(), is(100));
}
@Test
public void assertWithEncryptRule() {
EncryptRule encryptRule = (EncryptRule) ((EncryptDataSource) dataSource).getRuntimeContext().getRules().iterator().next();
EncryptRule encryptRule = (EncryptRule) ((ShardingDataSource) dataSource).getRuntimeContext().getRules().iterator().next();
assertThat(encryptRule.getEncryptTableNames().size(), is(1));
assertTrue(encryptRule.findEncryptor("t_order", "user_id").isPresent());
assertThat(encryptRule.getCipherColumn("t_order", "user_id"), is("user_encrypt"));
......
......@@ -24,12 +24,12 @@ spring.shardingsphere.datasource.ds.username=sa
spring.shardingsphere.datasource.ds.password=
spring.shardingsphere.datasource.ds.max-total=100
spring.shardingsphere.encrypt.encryptors.encryptor_aes.type=aes
spring.shardingsphere.encrypt.encryptors.encryptor_aes.props.aes.key.value=123456
spring.shardingsphere.encrypt.tables.t_order.columns.user_id.plainColumn=user_decrypt
spring.shardingsphere.encrypt.tables.t_order.columns.user_id.cipherColumn=user_encrypt
spring.shardingsphere.encrypt.tables.t_order.columns.user_id.assistedQueryColumn=user_assisted
spring.shardingsphere.encrypt.tables.t_order.columns.user_id.encryptor=encryptor_aes
spring.shardingsphere.rules.encrypt-rule.encryptors.encryptor_aes.type=aes
spring.shardingsphere.rules.encrypt-rule.encryptors.encryptor_aes.props.aes.key.value=123456
spring.shardingsphere.rules.encrypt-rule.tables.t_order.columns.user_id.plainColumn=user_decrypt
spring.shardingsphere.rules.encrypt-rule.tables.t_order.columns.user_id.cipherColumn=user_encrypt
spring.shardingsphere.rules.encrypt-rule.tables.t_order.columns.user_id.assistedQueryColumn=user_assisted
spring.shardingsphere.rules.encrypt-rule.tables.t_order.columns.user_id.encryptor=encryptor_aes
spring.shardingsphere.props.sql.show=true
spring.shardingsphere.props.query.with.cipher.column=true
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册