Skip to content

Commit

Permalink
Use safeConstructor instead of constructor when parsing yaml (apache#…
Browse files Browse the repository at this point in the history
…7437)

(cherry picked from commit 041f8bb)
  • Loading branch information
kevinw66 authored and vio-lin committed Jul 5, 2021
1 parent 5f9ea31 commit e919816
Show file tree
Hide file tree
Showing 7 changed files with 272 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,36 @@

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.PojoUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.rpc.cluster.configurator.parser.model.ConfigItem;
import org.apache.dubbo.rpc.cluster.configurator.parser.model.ConfiguratorConfig;

import org.yaml.snakeyaml.TypeDescription;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONValidator;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.constructor.SafeConstructor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static org.apache.dubbo.rpc.cluster.Constants.OVERRIDE_PROVIDERS_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.ANYHOST_VALUE;
import static org.apache.dubbo.common.constants.RegistryConstants.APP_DYNAMIC_CONFIGURATORS_CATEGORY;
import static org.apache.dubbo.common.constants.RegistryConstants.DYNAMIC_CONFIGURATORS_CATEGORY;
import static org.apache.dubbo.rpc.cluster.Constants.OVERRIDE_PROVIDERS_KEY;

/**
* Config parser
*/
public class ConfigParser {

public static List<URL> parseConfigurators(String rawConfig) {
public static List<URL> parseConfigurators(String rawConfig) throws Exception {
// compatible url JsonArray, such as [ "override://xxx", "override://xxx" ]
if (isJsonArray(rawConfig)) {
return parseJsonArray(rawConfig);
}

List<URL> urls = new ArrayList<>();
ConfiguratorConfig configuratorConfig = parseObject(rawConfig);

Expand All @@ -56,14 +63,19 @@ public static List<URL> parseConfigurators(String rawConfig) {
return urls;
}

private static <T> T parseObject(String rawConfig) {
Constructor constructor = new Constructor(ConfiguratorConfig.class);
TypeDescription itemDescription = new TypeDescription(ConfiguratorConfig.class);
itemDescription.addPropertyParameters("items", ConfigItem.class);
constructor.addTypeDescription(itemDescription);
private static List<URL> parseJsonArray(String rawConfig) {
List<URL> urls = new ArrayList<>();
List<String> list = JSON.parseArray(rawConfig, String.class);
if (!CollectionUtils.isEmpty(list)) {
list.forEach(u -> urls.add(URL.valueOf(u)));
}
return urls;
}

Yaml yaml = new Yaml(constructor);
return yaml.load(rawConfig);
private static <T> T parseObject(String rawConfig) throws Exception {
Yaml yaml = new Yaml(new SafeConstructor());
Map<String, Object> map = yaml.load(rawConfig);
return (T) PojoUtils.mapToPojo(map, ConfiguratorConfig.class);
}

private static List<URL> serviceItemToUrls(ConfigItem item, ConfiguratorConfig config) {
Expand Down Expand Up @@ -200,4 +212,14 @@ private static List<String> parseAddresses(ConfigItem item) {
}
return addresses;
}

private static boolean isJsonArray(String rawConfig) {
try {
JSONValidator validator = JSONValidator.from(rawConfig);
return validator.validate() && validator.getType() == JSONValidator.Type.Array;
} catch (Exception e) {
// ignore exception and return false
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
package org.apache.dubbo.rpc.cluster.router.condition.config.model;

import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.PojoUtils;

import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.constructor.SafeConstructor;

import java.util.Map;

/**
* %YAML1.2
Expand All @@ -37,11 +40,10 @@
*/
public class ConditionRuleParser {

public static ConditionRouterRule parse(String rawRule) {
Constructor constructor = new Constructor(ConditionRouterRule.class);

Yaml yaml = new Yaml(constructor);
ConditionRouterRule rule = yaml.load(rawRule);
public static ConditionRouterRule parse(String rawRule) throws Exception {
Yaml yaml = new Yaml(new SafeConstructor());
Map<String, Object> map = yaml.load(rawRule);
ConditionRouterRule rule = PojoUtils.mapToPojo(map, ConditionRouterRule.class);
rule.setRawRule(rawRule);
if (CollectionUtils.isEmpty(rule.getConditions())) {
rule.setValid(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,22 @@
package org.apache.dubbo.rpc.cluster.router.tag.model;

import org.apache.dubbo.common.utils.CollectionUtils;
import org.yaml.snakeyaml.TypeDescription;
import org.apache.dubbo.common.utils.PojoUtils;

import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.constructor.SafeConstructor;

import java.util.Map;

/**
*
*/
public class TagRuleParser {

public static TagRouterRule parse(String rawRule) {
Constructor constructor = new Constructor(TagRouterRule.class);
TypeDescription tagDescription = new TypeDescription(TagRouterRule.class);
tagDescription.addPropertyParameters("tags", Tag.class);
constructor.addTypeDescription(tagDescription);

Yaml yaml = new Yaml(constructor);
TagRouterRule rule = yaml.load(rawRule);
public static TagRouterRule parse(String rawRule) throws Exception {
Yaml yaml = new Yaml(new SafeConstructor());
Map<String, Object> map = yaml.load(rawRule);
TagRouterRule rule = PojoUtils.mapToPojo(map, TagRouterRule.class);
rule.setRawRule(rawRule);
if (CollectionUtils.isEmpty(rule.getTags())) {
rule.setValid(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void parseConfiguratorsServiceGroupVersionTest() throws Exception {
}

@Test
public void parseConfiguratorsServiceMultiAppsTest() throws IOException {
public void parseConfiguratorsServiceMultiAppsTest() throws Exception {
try (InputStream yamlStream = this.getClass().getResourceAsStream("/ServiceMultiApps.yml")) {
List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
Assertions.assertNotNull(urls);
Expand All @@ -112,7 +112,7 @@ public void parseConfiguratorsServiceNoRuleTest() {
}

@Test
public void parseConfiguratorsAppMultiServicesTest() throws IOException {
public void parseConfiguratorsAppMultiServicesTest() throws Exception {
try (InputStream yamlStream = this.getClass().getResourceAsStream("/AppMultiServices.yml")) {
String yamlFile = streamToString(yamlStream);
List<URL> urls = ConfigParser.parseConfigurators(yamlFile);
Expand All @@ -129,7 +129,7 @@ public void parseConfiguratorsAppMultiServicesTest() throws IOException {


@Test
public void parseConfiguratorsAppAnyServicesTest() throws IOException {
public void parseConfiguratorsAppAnyServicesTest() throws Exception {
try (InputStream yamlStream = this.getClass().getResourceAsStream("/AppAnyServices.yml")) {
List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
Assertions.assertNotNull(urls);
Expand All @@ -144,7 +144,7 @@ public void parseConfiguratorsAppAnyServicesTest() throws IOException {
}

@Test
public void parseConfiguratorsAppNoServiceTest() throws IOException {
public void parseConfiguratorsAppNoServiceTest() throws Exception {
try (InputStream yamlStream = this.getClass().getResourceAsStream("/AppNoService.yml")) {
List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
Assertions.assertNotNull(urls);
Expand All @@ -159,7 +159,7 @@ public void parseConfiguratorsAppNoServiceTest() throws IOException {
}

@Test
public void parseConsumerSpecificProvidersTest() throws IOException {
public void parseConsumerSpecificProvidersTest() throws Exception {
try (InputStream yamlStream = this.getClass().getResourceAsStream("/ConsumerSpecificProviders.yml")) {
List<URL> urls = ConfigParser.parseConfigurators(streamToString(yamlStream));
Assertions.assertNotNull(urls);
Expand All @@ -174,4 +174,20 @@ public void parseConsumerSpecificProvidersTest() throws IOException {
}
}

@Test
public void parseURLJsonArrayCompatible() throws Exception {

String configData = "[\"override://0.0.0.0/com.xx.Service?category=configurators&timeout=6666&disabled=true&dynamic=false&enabled=true&group=dubbo&priority=1&version=1.0\" ]";

List<URL> urls = ConfigParser.parseConfigurators(configData);

Assertions.assertNotNull(urls);
Assertions.assertEquals(1, urls.size());
URL url = urls.get(0);

Assertions.assertEquals("0.0.0.0", url.getAddress());
Assertions.assertEquals("com.xx.Service", url.getServiceInterface());
Assertions.assertEquals(6666, url.getParameter(TIMEOUT_KEY, 0));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ private void setData(String path, String data) throws Exception {
* </pre>
*/
@Test
public void tagRouterRuleParseTest(){
public void tagRouterRuleParseTest() throws Exception {
String tagRouterRuleConfig = "---\n" +
"force: false\n" +
"runtime: true\n" +
Expand Down
Loading

0 comments on commit e919816

Please sign in to comment.