You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2021/02/09 10:27:14 UTC
[shardingsphere] branch master updated: Rename secure unmarshal
with YAML (#9405)
This is an automated email from the ASF dual-hosted git repository.
menghaoran pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 1082c70 Rename secure unmarshal with YAML (#9405)
1082c70 is described below
commit 1082c7089abcccd4a1e1f585b9bbfa1eb2710e85
Author: Liang Zhang <te...@163.com>
AuthorDate: Tue Feb 9 18:26:36 2021 +0800
Rename secure unmarshal with YAML (#9405)
* For code format
* Rename secureUnmarshal
* Revise javadoc
* Rename secureUnmarshal
---
.../config/listener/PropertiesChangedListener.java | 2 +-
.../metadata/DataSourceChangedListener.java | 2 +-
.../listener/metadata/RuleChangedListener.java | 2 +-
.../yaml/config/YamlConfigurationConverter.java | 7 +++----
.../infra/yaml/engine/YamlEngine.java | 16 +++++++--------
.../ShardingSphereFilterYamlConstructor.java | 8 ++++----
.../constructor/ShardingSphereYamlConstructor.java | 2 +-
.../config/YamlEngineUserConfigurationTest.java | 8 ++++----
.../infra/yaml/engine/YamlEngineTest.java | 24 +++++++++++-----------
9 files changed, 35 insertions(+), 36 deletions(-)
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/PropertiesChangedListener.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/PropertiesChangedListener.java
index daba89b..01482b3 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/PropertiesChangedListener.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/PropertiesChangedListener.java
@@ -40,6 +40,6 @@ public final class PropertiesChangedListener extends PostGovernanceRepositoryEve
@Override
protected Optional<GovernanceEvent> createEvent(final DataChangedEvent event) {
- return Optional.of(new PropertiesChangedEvent(YamlEngine.unmarshalWithFilter(event.getValue(), Properties.class)));
+ return Optional.of(new PropertiesChangedEvent(YamlEngine.secureUnmarshal(event.getValue(), Properties.class)));
}
}
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/metadata/DataSourceChangedListener.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/metadata/DataSourceChangedListener.java
index c615294..8c9b583 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/metadata/DataSourceChangedListener.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/metadata/DataSourceChangedListener.java
@@ -62,7 +62,7 @@ public final class DataSourceChangedListener extends PostGovernanceRepositoryEve
}
private DataSourceChangedEvent createDataSourceChangedEvent(final String schemaName, final DataChangedEvent event) {
- YamlDataSourceConfigurationWrap result = YamlEngine.unmarshalWithFilter(event.getValue(), YamlDataSourceConfigurationWrap.class);
+ YamlDataSourceConfigurationWrap result = YamlEngine.secureUnmarshal(event.getValue(), YamlDataSourceConfigurationWrap.class);
Preconditions.checkState(null != result && !result.getDataSources().isEmpty(), "No available data sources to load for governance.");
return new DataSourceChangedEvent(schemaName, result.getDataSources().entrySet().stream()
.collect(Collectors.toMap(Entry::getKey, entry -> new DataSourceConfigurationYamlSwapper().swapToObject(entry.getValue()), (oldValue, currentValue) -> oldValue, LinkedHashMap::new)));
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/metadata/RuleChangedListener.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/metadata/RuleChangedListener.java
index e6dcec3..568ec6b 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/metadata/RuleChangedListener.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/config/listener/metadata/RuleChangedListener.java
@@ -76,7 +76,7 @@ public final class RuleChangedListener extends PostGovernanceRepositoryEventList
}
private Collection<RuleConfiguration> getRuleConfigurations(final String yamlContent) {
- Collection<YamlRuleConfiguration> rules = YamlEngine.unmarshalWithFilter(yamlContent, YamlRuleConfigurationWrap.class).getRules();
+ Collection<YamlRuleConfiguration> rules = YamlEngine.secureUnmarshal(yamlContent, YamlRuleConfigurationWrap.class).getRules();
Preconditions.checkState(!rules.isEmpty(), "No available rule to load for governance.");
return new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(rules);
}
diff --git a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/yaml/config/YamlConfigurationConverter.java b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/yaml/config/YamlConfigurationConverter.java
index 9c9e37d..972aae3 100644
--- a/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/yaml/config/YamlConfigurationConverter.java
+++ b/shardingsphere-governance/shardingsphere-governance-core/src/main/java/org/apache/shardingsphere/governance/core/yaml/config/YamlConfigurationConverter.java
@@ -48,7 +48,7 @@ public final class YamlConfigurationConverter {
* @return data source configurations
*/
public static Map<String, DataSourceConfiguration> convertDataSourceConfigurations(final String yamlContent) {
- YamlDataSourceConfigurationWrap result = YamlEngine.unmarshalWithFilter(yamlContent, YamlDataSourceConfigurationWrap.class);
+ YamlDataSourceConfigurationWrap result = YamlEngine.secureUnmarshal(yamlContent, YamlDataSourceConfigurationWrap.class);
if (null == result.getDataSources() || result.getDataSources().isEmpty()) {
return new LinkedHashMap<>();
}
@@ -63,8 +63,7 @@ public final class YamlConfigurationConverter {
* @return rule configurations
*/
public static Collection<RuleConfiguration> convertRuleConfigurations(final String yamlContent) {
- return new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(
- YamlEngine.unmarshalWithFilter(yamlContent, YamlRuleConfigurationWrap.class).getRules());
+ return new YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(YamlEngine.secureUnmarshal(yamlContent, YamlRuleConfigurationWrap.class).getRules());
}
/**
@@ -84,6 +83,6 @@ public final class YamlConfigurationConverter {
* @return properties
*/
public static Properties convertProperties(final String yamlContent) {
- return YamlEngine.unmarshalWithFilter(yamlContent, Properties.class);
+ return YamlEngine.secureUnmarshal(yamlContent, Properties.class);
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/YamlEngine.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/YamlEngine.java
index a069621..600ced1 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/YamlEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/YamlEngine.java
@@ -89,7 +89,7 @@ public final class YamlEngine {
}
/**
- * Unmarshal YAML.
+ * Secure unmarshal YAML.
*
* @param yamlContent YAML content
* @param classType class type
@@ -97,31 +97,31 @@ public final class YamlEngine {
* @param <T> type of class
* @return object from YAML
*/
- public static <T> T unmarshal(final String yamlContent, final Class<T> classType, final Collection<Class<?>> acceptedClasses) {
+ public static <T> T secureUnmarshal(final String yamlContent, final Class<T> classType, final Collection<Class<?>> acceptedClasses) {
return new Yaml(new ShardingSphereFilterYamlConstructor(classType, acceptedClasses)).loadAs(yamlContent, classType);
}
/**
- * Unmarshal YAML.
+ * Secure unmarshal YAML.
*
* @param yamlContent YAML content
* @param acceptedClasses accepted classes
* @return map from YAML
*/
- public static Map<?, ?> unmarshal(final String yamlContent, final Collection<Class<?>> acceptedClasses) {
+ public static Map<?, ?> secureUnmarshal(final String yamlContent, final Collection<Class<?>> acceptedClasses) {
return Strings.isNullOrEmpty(yamlContent) ? new LinkedHashMap<>() : (Map) new Yaml(new ShardingSphereFilterYamlConstructor(acceptedClasses)).load(yamlContent);
}
/**
- * Unmarshal YAML with filter.
+ * Secure unmarshal YAML.
*
* @param yamlContent YAML content
- * @param classType class type
+ * @param acceptedClass accepted class
* @param <T> type of class
* @return object from YAML
*/
- public static <T> T unmarshalWithFilter(final String yamlContent, final Class<T> classType) {
- return new Yaml(new ShardingSphereFilterYamlConstructor(classType, Collections.singletonList(classType))).loadAs(yamlContent, classType);
+ public static <T> T secureUnmarshal(final String yamlContent, final Class<T> acceptedClass) {
+ return new Yaml(new ShardingSphereFilterYamlConstructor(acceptedClass, Collections.singletonList(acceptedClass))).loadAs(yamlContent, acceptedClass);
}
/**
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/constructor/ShardingSphereFilterYamlConstructor.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/constructor/ShardingSphereFilterYamlConstructor.java
index 5e1006f..292a602 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/constructor/ShardingSphereFilterYamlConstructor.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/constructor/ShardingSphereFilterYamlConstructor.java
@@ -37,12 +37,12 @@ public final class ShardingSphereFilterYamlConstructor extends ShardingSphereYam
}
@Override
- protected Class<?> getClassForName(final String name) throws ClassNotFoundException {
+ protected Class<?> getClassForName(final String className) throws ClassNotFoundException {
for (Class<?> each : acceptedClasses) {
- if (name.equals(each.getName())) {
- return super.getClassForName(name);
+ if (className.equals(each.getName())) {
+ return super.getClassForName(className);
}
}
- throw new IllegalArgumentException(String.format("Class is not accepted: %s", name));
+ throw new IllegalArgumentException(String.format("Class is not accepted: %s", className));
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/constructor/ShardingSphereYamlConstructor.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/constructor/ShardingSphereYamlConstructor.java
index 0a7fdcf..1bc32c0 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/constructor/ShardingSphereYamlConstructor.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/constructor/ShardingSphereYamlConstructor.java
@@ -45,7 +45,7 @@ public class ShardingSphereYamlConstructor extends Constructor {
}
@Override
- protected Construct getConstructor(final Node node) {
+ protected final Construct getConstructor(final Node node) {
return typeConstructs.getOrDefault(node.getType(), super.getConstructor(node));
}
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/auth/builtin/yaml/config/YamlEngineUserConfigurationTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/auth/builtin/yaml/config/YamlEngineUserConfigurationTest.java
index 63dc17a..01c4cb0 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/auth/builtin/yaml/config/YamlEngineUserConfigurationTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/auth/builtin/yaml/config/YamlEngineUserConfigurationTest.java
@@ -38,14 +38,14 @@ public final class YamlEngineUserConfigurationTest {
@SuppressWarnings("unchecked")
@Test
- public void assertUnmarshalMap() {
- Map<String, Object> actual = (Map<String, Object>) YamlEngine.unmarshal("password: pwd", Collections.emptyList());
+ public void assertSecureUnmarshalMap() {
+ Map<String, Object> actual = (Map<String, Object>) YamlEngine.secureUnmarshal("password: pwd", Collections.emptyList());
assertThat(actual.get("password").toString(), is("pwd"));
}
@Test
- public void assertUnmarshalProperties() {
- Properties actual = YamlEngine.unmarshalWithFilter("password: pwd", Properties.class);
+ public void assertSecureUnmarshalProperties() {
+ Properties actual = YamlEngine.secureUnmarshal("password: pwd", Properties.class);
assertThat(actual.getProperty("password"), is("pwd"));
}
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/YamlEngineTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/YamlEngineTest.java
index d1c1530..7e82ef4 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/YamlEngineTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/YamlEngineTest.java
@@ -75,14 +75,14 @@ public final class YamlEngineTest {
@Test
@SuppressWarnings("unchecked")
- public void assertUnmarshalWithYamlContent() {
- Map<String, Object> actual = (Map<String, Object>) YamlEngine.unmarshal("name: test", Collections.emptyList());
+ public void assertSecureUnmarshalWithYamlContent() {
+ Map<String, Object> actual = (Map<String, Object>) YamlEngine.secureUnmarshal("name: test", Collections.emptyList());
assertThat(actual.get("name").toString(), is("test"));
}
@Test
- public void assertUnmarshalProperties() {
- Properties actual = YamlEngine.unmarshalWithFilter("password: pwd", Properties.class);
+ public void assertSecureUnmarshalProperties() {
+ Properties actual = YamlEngine.secureUnmarshal("password: pwd", Properties.class);
assertThat(actual.getProperty("password"), is("pwd"));
}
@@ -94,22 +94,22 @@ public final class YamlEngineTest {
}
@Test(expected = ConstructorException.class)
- public void assertUnmarshalMapWithIllegalClasses() {
- YamlEngine.unmarshal("url: !!java.net.URLClassLoader [[!!java.net.URL [\"http://localhost\"]]]", Collections.emptyList());
+ public void assertSecureUnmarshalMapWithIllegalClasses() {
+ YamlEngine.secureUnmarshal("url: !!java.net.URLClassLoader [[!!java.net.URL [\"http://localhost\"]]]", Collections.emptyList());
}
@SuppressWarnings("unchecked")
@Test
- public void assertUnmarshalMapWithAcceptedClasses() {
+ public void assertSecureUnmarshalMapWithAcceptedClasses() {
Collection<Class<?>> acceptedClasses = new LinkedList<>();
acceptedClasses.add(URLClassLoader.class);
acceptedClasses.add(URL.class);
- Map<String, URLClassLoader> actual = (Map) YamlEngine.unmarshal("url: !!java.net.URLClassLoader [[!!java.net.URL [\"http://localhost\"]]]", acceptedClasses);
+ Map<String, URLClassLoader> actual = (Map) YamlEngine.secureUnmarshal("url: !!java.net.URLClassLoader [[!!java.net.URL [\"http://localhost\"]]]", acceptedClasses);
assertThat(actual.get("url").getClass().getName(), is(URLClassLoader.class.getName()));
}
@Test
- public void assertUnmarshalWithAcceptedClass() throws IOException {
+ public void assertSecureUnmarshalWithAcceptedClass() throws IOException {
URL url = getClass().getClassLoader().getResource("yaml/accepted-class.yaml");
assertNotNull(url);
StringBuilder yamlContent = new StringBuilder();
@@ -125,7 +125,7 @@ public final class YamlEngineTest {
acceptedClasses.add(URLClassLoader.class);
acceptedClasses.add(URL.class);
acceptedClasses.add(YamlRootRuleConfigurations.class);
- YamlRootRuleConfigurations actual = YamlEngine.unmarshal(yamlContent.toString(), YamlRootRuleConfigurations.class, acceptedClasses);
+ YamlRootRuleConfigurations actual = YamlEngine.secureUnmarshal(yamlContent.toString(), YamlRootRuleConfigurations.class, acceptedClasses);
assertThat(actual.getProps().size(), is(2));
assertThat(actual.getProps().getProperty("normal"), is("normal"));
assertTrue(actual.getProps().containsKey("url"));
@@ -133,7 +133,7 @@ public final class YamlEngineTest {
}
@Test(expected = ConstructorException.class)
- public void assertUnmarshalWithoutAcceptedClass() throws IOException {
+ public void assertSecureUnmarshalWithoutAcceptedClass() throws IOException {
URL url = getClass().getClassLoader().getResource("yaml/accepted-class.yaml");
assertNotNull(url);
StringBuilder yamlContent = new StringBuilder();
@@ -145,6 +145,6 @@ public final class YamlEngineTest {
yamlContent.append(line).append("\n");
}
}
- YamlEngine.unmarshalWithFilter(yamlContent.toString(), YamlRootRuleConfigurations.class);
+ YamlEngine.secureUnmarshal(yamlContent.toString(), YamlRootRuleConfigurations.class);
}
}