stop startup if dubbo application name configuration is invalid (#13343)

This commit is contained in:
zrlw 2024-01-12 22:55:18 +08:00 committed by GitHub
parent 05d44abe93
commit 9d04bc5c25
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 17 additions and 6 deletions

View File

@ -491,6 +491,12 @@ public class ConfigValidationUtils {
+ "Please add <dubbo:application name=\"...\" /> to your spring config.");
}
String name = config.getName();
if (!checkName(NAME, name)) {
throw new IllegalStateException(
String.format("please correct dubbo application name: %s at your spring config.", name));
}
// backward compatibility
ScopeModel scopeModel = ScopeModelUtil.getOrDefaultApplicationModel(config.getScopeModel());
PropertiesConfiguration configuration = scopeModel.modelEnvironment().getPropertiesConfiguration();
@ -504,7 +510,6 @@ public class ConfigValidationUtils {
}
}
checkName(NAME, config.getName());
checkMultiName(OWNER, config.getOwner());
checkName(ORGANIZATION, config.getOrganization());
checkName(ARCHITECTURE, config.getArchitecture());
@ -732,8 +737,8 @@ public class ConfigValidationUtils {
checkProperty(property, value, MAX_PATH_LENGTH, null);
}
public static void checkName(String property, String value) {
checkProperty(property, value, MAX_LENGTH, PATTERN_NAME);
public static boolean checkName(String property, String value) {
return checkProperty(property, value, MAX_LENGTH, PATTERN_NAME);
}
public static void checkHost(String property, String value) {
@ -789,9 +794,9 @@ public class ConfigValidationUtils {
}
}
public static void checkProperty(String property, String value, int maxlength, Pattern pattern) {
public static boolean checkProperty(String property, String value, int maxlength, Pattern pattern) {
if (StringUtils.isEmpty(value)) {
return;
return false;
}
if (value.length() > maxlength) {
logger.error(
@ -800,6 +805,7 @@ public class ConfigValidationUtils {
"",
"Parameter value format error. Invalid " + property + "=\"" + value + "\" is longer than "
+ maxlength);
return false;
}
if (pattern != null) {
Matcher matcher = pattern.matcher(value);
@ -811,7 +817,9 @@ public class ConfigValidationUtils {
"Parameter value format error. Invalid " + property
+ "=\"" + value + "\" contains illegal "
+ "character, only digit, letter, '-', '_' or '.' is legal.");
return false;
}
}
return true;
}
}

View File

@ -873,7 +873,7 @@ class ReferenceConfigTest {
referenceConfig.setGeneric("true");
DubboBootstrap.getInstance()
.application("demo app")
.application("demo-app")
.reference(referenceConfig)
.initialize();

View File

@ -100,6 +100,9 @@ class ConfigValidationUtilsTest {
mockedStatic
.when(() -> ConfigValidationUtils.validateApplicationConfig(any()))
.thenCallRealMethod();
mockedStatic
.when(() -> ConfigValidationUtils.checkName(any(), any()))
.thenReturn(true);
ApplicationConfig config = new ApplicationConfig();
Assertions.assertThrows(IllegalStateException.class, () -> {
ConfigValidationUtils.validateApplicationConfig(config);