* FIX [ISSUE #12446] macInstance 补充日志 * UPDATE [ISSUE #12466] serviceMetadata environment variables config * fixed DefaultParamChecker and DefaultParamCheckerTest codeStyle
This commit is contained in:
parent
bcbdb863ba
commit
c56c4153eb
@ -16,6 +16,8 @@
|
|||||||
|
|
||||||
package com.alibaba.nacos.common.paramcheck;
|
package com.alibaba.nacos.common.paramcheck;
|
||||||
|
|
||||||
|
import com.alibaba.nacos.common.utils.NumberUtils;
|
||||||
|
import com.alibaba.nacos.common.utils.PropertyUtils;
|
||||||
import com.alibaba.nacos.common.utils.StringUtils;
|
import com.alibaba.nacos.common.utils.StringUtils;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -45,6 +47,10 @@ public class DefaultParamChecker extends AbstractParamChecker {
|
|||||||
|
|
||||||
private static final String CHECKER_TYPE = "default";
|
private static final String CHECKER_TYPE = "default";
|
||||||
|
|
||||||
|
private static final String MAX_METADATA_LENGTH_PROP_NAME = "nacos.naming.service.metadata.length";
|
||||||
|
|
||||||
|
private static final String MAX_METADATA_LENGTH_ENV_NAME = "NACOS_NAMING_SERVICE_METADATA_LENGTH";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getCheckerType() {
|
public String getCheckerType() {
|
||||||
return CHECKER_TYPE;
|
return CHECKER_TYPE;
|
||||||
@ -71,6 +77,7 @@ public class DefaultParamChecker extends AbstractParamChecker {
|
|||||||
public void initParamCheckRule() {
|
public void initParamCheckRule() {
|
||||||
this.paramCheckRule = new ParamCheckRule();
|
this.paramCheckRule = new ParamCheckRule();
|
||||||
initFormatPattern();
|
initFormatPattern();
|
||||||
|
replaceParamCheckRuleByEnv();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initFormatPattern() {
|
private void initFormatPattern() {
|
||||||
@ -83,6 +90,16 @@ public class DefaultParamChecker extends AbstractParamChecker {
|
|||||||
this.ipPattern = Pattern.compile(this.paramCheckRule.ipPatternString);
|
this.ipPattern = Pattern.compile(this.paramCheckRule.ipPatternString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* if environment variables exists, it will be replaced.
|
||||||
|
*/
|
||||||
|
private void replaceParamCheckRuleByEnv() {
|
||||||
|
String maxMetadataLength = PropertyUtils.getProperty(MAX_METADATA_LENGTH_PROP_NAME, MAX_METADATA_LENGTH_ENV_NAME);
|
||||||
|
if (StringUtils.isNotBlank(maxMetadataLength)) {
|
||||||
|
this.paramCheckRule.maxMetadataLength = NumberUtils.toInt(maxMetadataLength);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check param info format.
|
* Check param info format.
|
||||||
*
|
*
|
||||||
@ -207,8 +224,8 @@ public class DefaultParamChecker extends AbstractParamChecker {
|
|||||||
}
|
}
|
||||||
if (dataId.length() > paramCheckRule.maxDataIdLength) {
|
if (dataId.length() > paramCheckRule.maxDataIdLength) {
|
||||||
paramCheckResponse.setSuccess(false);
|
paramCheckResponse.setSuccess(false);
|
||||||
paramCheckResponse.setMessage(String.format("Param 'dataId' is illegal, the param length should not exceed %d.",
|
paramCheckResponse.setMessage(
|
||||||
paramCheckRule.maxDataIdLength));
|
String.format("Param 'dataId' is illegal, the param length should not exceed %d.", paramCheckRule.maxDataIdLength));
|
||||||
return paramCheckResponse;
|
return paramCheckResponse;
|
||||||
}
|
}
|
||||||
if (!dataIdPattern.matcher(dataId).matches()) {
|
if (!dataIdPattern.matcher(dataId).matches()) {
|
||||||
@ -234,8 +251,8 @@ public class DefaultParamChecker extends AbstractParamChecker {
|
|||||||
}
|
}
|
||||||
if (serviceName.length() > paramCheckRule.maxServiceNameLength) {
|
if (serviceName.length() > paramCheckRule.maxServiceNameLength) {
|
||||||
paramCheckResponse.setSuccess(false);
|
paramCheckResponse.setSuccess(false);
|
||||||
paramCheckResponse.setMessage(String.format("Param 'serviceName' is illegal, the param length should not exceed %d.",
|
paramCheckResponse.setMessage(
|
||||||
paramCheckRule.maxServiceNameLength));
|
String.format("Param 'serviceName' is illegal, the param length should not exceed %d.", paramCheckRule.maxServiceNameLength));
|
||||||
return paramCheckResponse;
|
return paramCheckResponse;
|
||||||
}
|
}
|
||||||
if (!serviceNamePattern.matcher(serviceName).matches()) {
|
if (!serviceNamePattern.matcher(serviceName).matches()) {
|
||||||
@ -261,8 +278,8 @@ public class DefaultParamChecker extends AbstractParamChecker {
|
|||||||
}
|
}
|
||||||
if (group.length() > paramCheckRule.maxGroupLength) {
|
if (group.length() > paramCheckRule.maxGroupLength) {
|
||||||
paramCheckResponse.setSuccess(false);
|
paramCheckResponse.setSuccess(false);
|
||||||
paramCheckResponse.setMessage(String.format("Param 'group' is illegal, the param length should not exceed %d.",
|
paramCheckResponse.setMessage(
|
||||||
paramCheckRule.maxGroupLength));
|
String.format("Param 'group' is illegal, the param length should not exceed %d.", paramCheckRule.maxGroupLength));
|
||||||
return paramCheckResponse;
|
return paramCheckResponse;
|
||||||
}
|
}
|
||||||
if (!groupPattern.matcher(group).matches()) {
|
if (!groupPattern.matcher(group).matches()) {
|
||||||
@ -312,8 +329,8 @@ public class DefaultParamChecker extends AbstractParamChecker {
|
|||||||
|
|
||||||
if (cluster.length() > paramCheckRule.maxClusterLength) {
|
if (cluster.length() > paramCheckRule.maxClusterLength) {
|
||||||
paramCheckResponse.setSuccess(false);
|
paramCheckResponse.setSuccess(false);
|
||||||
paramCheckResponse.setMessage(String.format("Param 'cluster' is illegal, the param length should not exceed %d.",
|
paramCheckResponse.setMessage(
|
||||||
paramCheckRule.maxClusterLength));
|
String.format("Param 'cluster' is illegal, the param length should not exceed %d.", paramCheckRule.maxClusterLength));
|
||||||
return paramCheckResponse;
|
return paramCheckResponse;
|
||||||
}
|
}
|
||||||
if (!clusterPattern.matcher(cluster).matches()) {
|
if (!clusterPattern.matcher(cluster).matches()) {
|
||||||
@ -339,8 +356,7 @@ public class DefaultParamChecker extends AbstractParamChecker {
|
|||||||
}
|
}
|
||||||
if (ip.length() > paramCheckRule.maxIpLength) {
|
if (ip.length() > paramCheckRule.maxIpLength) {
|
||||||
paramCheckResponse.setSuccess(false);
|
paramCheckResponse.setSuccess(false);
|
||||||
paramCheckResponse.setMessage(String.format("Param 'ip' is illegal, the param length should not exceed %d.",
|
paramCheckResponse.setMessage(String.format("Param 'ip' is illegal, the param length should not exceed %d.", paramCheckRule.maxIpLength));
|
||||||
paramCheckRule.maxIpLength));
|
|
||||||
return paramCheckResponse;
|
return paramCheckResponse;
|
||||||
}
|
}
|
||||||
if (!ipPattern.matcher(ip).matches()) {
|
if (!ipPattern.matcher(ip).matches()) {
|
||||||
@ -369,14 +385,14 @@ public class DefaultParamChecker extends AbstractParamChecker {
|
|||||||
portInt = Integer.parseInt(port);
|
portInt = Integer.parseInt(port);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
paramCheckResponse.setSuccess(false);
|
paramCheckResponse.setSuccess(false);
|
||||||
paramCheckResponse.setMessage(String.format("Param 'port' is illegal, the value should be between %d and %d.",
|
paramCheckResponse.setMessage(
|
||||||
paramCheckRule.minPort, paramCheckRule.maxPort));
|
String.format("Param 'port' is illegal, the value should be between %d and %d.", paramCheckRule.minPort, paramCheckRule.maxPort));
|
||||||
return paramCheckResponse;
|
return paramCheckResponse;
|
||||||
}
|
}
|
||||||
if (portInt > paramCheckRule.maxPort || portInt < paramCheckRule.minPort) {
|
if (portInt > paramCheckRule.maxPort || portInt < paramCheckRule.minPort) {
|
||||||
paramCheckResponse.setSuccess(false);
|
paramCheckResponse.setSuccess(false);
|
||||||
paramCheckResponse.setMessage(String.format("Param 'port' is illegal, the value should be between %d and %d.",
|
paramCheckResponse.setMessage(
|
||||||
paramCheckRule.minPort, paramCheckRule.maxPort));
|
String.format("Param 'port' is illegal, the value should be between %d and %d.", paramCheckRule.minPort, paramCheckRule.maxPort));
|
||||||
return paramCheckResponse;
|
return paramCheckResponse;
|
||||||
}
|
}
|
||||||
paramCheckResponse.setSuccess(true);
|
paramCheckResponse.setSuccess(true);
|
||||||
@ -406,8 +422,8 @@ public class DefaultParamChecker extends AbstractParamChecker {
|
|||||||
}
|
}
|
||||||
if (totalLength > paramCheckRule.maxMetadataLength) {
|
if (totalLength > paramCheckRule.maxMetadataLength) {
|
||||||
paramCheckResponse.setSuccess(false);
|
paramCheckResponse.setSuccess(false);
|
||||||
paramCheckResponse.setMessage(String.format("Param 'Metadata' is illegal, the param length should not exceed %d.",
|
paramCheckResponse.setMessage(
|
||||||
paramCheckRule.maxMetadataLength));
|
String.format("Param 'Metadata' is illegal, the param length should not exceed %d.", paramCheckRule.maxMetadataLength));
|
||||||
return paramCheckResponse;
|
return paramCheckResponse;
|
||||||
}
|
}
|
||||||
paramCheckResponse.setSuccess(true);
|
paramCheckResponse.setSuccess(true);
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package com.alibaba.nacos.common.paramcheck;
|
package com.alibaba.nacos.common.paramcheck;
|
||||||
|
|
||||||
|
import com.alibaba.nacos.common.utils.RandomUtils;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
@ -32,8 +33,11 @@ class DefaultParamCheckerTest {
|
|||||||
|
|
||||||
DefaultParamChecker paramChecker;
|
DefaultParamChecker paramChecker;
|
||||||
|
|
||||||
|
int maxMetadataLength = RandomUtils.nextInt(1024, 10240);
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() throws Exception {
|
void setUp() throws Exception {
|
||||||
|
System.setProperty("nacos.naming.service.metadata.length", String.valueOf(maxMetadataLength));
|
||||||
paramChecker = new DefaultParamChecker();
|
paramChecker = new DefaultParamChecker();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -75,8 +79,7 @@ class DefaultParamCheckerTest {
|
|||||||
paramInfo.setNamespaceShowName("hsbfkj@$!#khdkad");
|
paramInfo.setNamespaceShowName("hsbfkj@$!#khdkad");
|
||||||
actual = paramChecker.checkParamInfoList(paramInfos);
|
actual = paramChecker.checkParamInfoList(paramInfos);
|
||||||
assertFalse(actual.isSuccess());
|
assertFalse(actual.isSuccess());
|
||||||
assertEquals("Param 'namespaceShowName' is illegal, illegal characters should not appear in the param.",
|
assertEquals("Param 'namespaceShowName' is illegal, illegal characters should not appear in the param.", actual.getMessage());
|
||||||
actual.getMessage());
|
|
||||||
// Success
|
// Success
|
||||||
paramInfo.setNamespaceShowName("测试");
|
paramInfo.setNamespaceShowName("测试");
|
||||||
actual = paramChecker.checkParamInfoList(paramInfos);
|
actual = paramChecker.checkParamInfoList(paramInfos);
|
||||||
@ -98,8 +101,7 @@ class DefaultParamCheckerTest {
|
|||||||
paramInfo.setNamespaceId("hsbfkj@$!#khdkad");
|
paramInfo.setNamespaceId("hsbfkj@$!#khdkad");
|
||||||
actual = paramChecker.checkParamInfoList(paramInfos);
|
actual = paramChecker.checkParamInfoList(paramInfos);
|
||||||
assertFalse(actual.isSuccess());
|
assertFalse(actual.isSuccess());
|
||||||
assertEquals("Param 'namespaceId/tenant' is illegal, illegal characters should not appear in the param.",
|
assertEquals("Param 'namespaceId/tenant' is illegal, illegal characters should not appear in the param.", actual.getMessage());
|
||||||
actual.getMessage());
|
|
||||||
// Success
|
// Success
|
||||||
paramInfo.setNamespaceId("123-ashdal");
|
paramInfo.setNamespaceId("123-ashdal");
|
||||||
actual = paramChecker.checkParamInfoList(paramInfos);
|
actual = paramChecker.checkParamInfoList(paramInfos);
|
||||||
@ -273,12 +275,12 @@ class DefaultParamCheckerTest {
|
|||||||
paramInfo.setMetadata(metadata);
|
paramInfo.setMetadata(metadata);
|
||||||
// Max length
|
// Max length
|
||||||
metadata.put("key1", "");
|
metadata.put("key1", "");
|
||||||
metadata.put("key2", buildStringLength(1024));
|
metadata.put("key2", buildStringLength(maxMetadataLength));
|
||||||
ParamCheckResponse actual = paramChecker.checkParamInfoList(paramInfos);
|
ParamCheckResponse actual = paramChecker.checkParamInfoList(paramInfos);
|
||||||
assertFalse(actual.isSuccess());
|
assertFalse(actual.isSuccess());
|
||||||
assertEquals("Param 'Metadata' is illegal, the param length should not exceed 1024.", actual.getMessage());
|
assertEquals(String.format("Param 'Metadata' is illegal, the param length should not exceed %d.", maxMetadataLength), actual.getMessage());
|
||||||
// Success
|
// Success
|
||||||
metadata.put("key2", "Any key and value, only require length sum not more than 1024.");
|
metadata.put("key2", String.format("Any key and value, only require length sum not more than %d.", maxMetadataLength));
|
||||||
actual = paramChecker.checkParamInfoList(paramInfos);
|
actual = paramChecker.checkParamInfoList(paramInfos);
|
||||||
assertTrue(actual.isSuccess());
|
assertTrue(actual.isSuccess());
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user