Skip to content

Commit

Permalink
[ISSUE #12466] serviceMetadata environment variables config (#12477)
Browse files Browse the repository at this point in the history
* FIX [ISSUE #12446] macInstance 补充日志

* UPDATE [ISSUE #12466] serviceMetadata environment variables config

* fixed DefaultParamChecker and DefaultParamCheckerTest codeStyle
  • Loading branch information
fuhouyu committed Aug 12, 2024
1 parent bcbdb86 commit c56c415
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

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 java.util.List;
Expand Down Expand Up @@ -45,6 +47,10 @@ public class DefaultParamChecker extends AbstractParamChecker {

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
public String getCheckerType() {
return CHECKER_TYPE;
Expand All @@ -71,6 +77,7 @@ public ParamCheckResponse checkParamInfoList(List<ParamInfo> paramInfos) {
public void initParamCheckRule() {
this.paramCheckRule = new ParamCheckRule();
initFormatPattern();
replaceParamCheckRuleByEnv();
}

private void initFormatPattern() {
Expand All @@ -83,6 +90,16 @@ private void initFormatPattern() {
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.
*
Expand Down Expand Up @@ -207,8 +224,8 @@ public ParamCheckResponse checkDataIdFormat(String dataId) {
}
if (dataId.length() > paramCheckRule.maxDataIdLength) {
paramCheckResponse.setSuccess(false);
paramCheckResponse.setMessage(String.format("Param 'dataId' is illegal, the param length should not exceed %d.",
paramCheckRule.maxDataIdLength));
paramCheckResponse.setMessage(
String.format("Param 'dataId' is illegal, the param length should not exceed %d.", paramCheckRule.maxDataIdLength));
return paramCheckResponse;
}
if (!dataIdPattern.matcher(dataId).matches()) {
Expand All @@ -234,8 +251,8 @@ public ParamCheckResponse checkServiceNameFormat(String serviceName) {
}
if (serviceName.length() > paramCheckRule.maxServiceNameLength) {
paramCheckResponse.setSuccess(false);
paramCheckResponse.setMessage(String.format("Param 'serviceName' is illegal, the param length should not exceed %d.",
paramCheckRule.maxServiceNameLength));
paramCheckResponse.setMessage(
String.format("Param 'serviceName' is illegal, the param length should not exceed %d.", paramCheckRule.maxServiceNameLength));
return paramCheckResponse;
}
if (!serviceNamePattern.matcher(serviceName).matches()) {
Expand All @@ -261,8 +278,8 @@ public ParamCheckResponse checkGroupFormat(String group) {
}
if (group.length() > paramCheckRule.maxGroupLength) {
paramCheckResponse.setSuccess(false);
paramCheckResponse.setMessage(String.format("Param 'group' is illegal, the param length should not exceed %d.",
paramCheckRule.maxGroupLength));
paramCheckResponse.setMessage(
String.format("Param 'group' is illegal, the param length should not exceed %d.", paramCheckRule.maxGroupLength));
return paramCheckResponse;
}
if (!groupPattern.matcher(group).matches()) {
Expand Down Expand Up @@ -312,8 +329,8 @@ public ParamCheckResponse checkSingleClusterFormat(String cluster) {

if (cluster.length() > paramCheckRule.maxClusterLength) {
paramCheckResponse.setSuccess(false);
paramCheckResponse.setMessage(String.format("Param 'cluster' is illegal, the param length should not exceed %d.",
paramCheckRule.maxClusterLength));
paramCheckResponse.setMessage(
String.format("Param 'cluster' is illegal, the param length should not exceed %d.", paramCheckRule.maxClusterLength));
return paramCheckResponse;
}
if (!clusterPattern.matcher(cluster).matches()) {
Expand All @@ -339,8 +356,7 @@ public ParamCheckResponse checkIpFormat(String ip) {
}
if (ip.length() > paramCheckRule.maxIpLength) {
paramCheckResponse.setSuccess(false);
paramCheckResponse.setMessage(String.format("Param 'ip' is illegal, the param length should not exceed %d.",
paramCheckRule.maxIpLength));
paramCheckResponse.setMessage(String.format("Param 'ip' is illegal, the param length should not exceed %d.", paramCheckRule.maxIpLength));
return paramCheckResponse;
}
if (!ipPattern.matcher(ip).matches()) {
Expand Down Expand Up @@ -369,14 +385,14 @@ public ParamCheckResponse checkPortFormat(String port) {
portInt = Integer.parseInt(port);
} catch (Exception e) {
paramCheckResponse.setSuccess(false);
paramCheckResponse.setMessage(String.format("Param 'port' is illegal, the value should be between %d and %d.",
paramCheckRule.minPort, paramCheckRule.maxPort));
paramCheckResponse.setMessage(
String.format("Param 'port' is illegal, the value should be between %d and %d.", paramCheckRule.minPort, paramCheckRule.maxPort));
return paramCheckResponse;
}
if (portInt > paramCheckRule.maxPort || portInt < paramCheckRule.minPort) {
paramCheckResponse.setSuccess(false);
paramCheckResponse.setMessage(String.format("Param 'port' is illegal, the value should be between %d and %d.",
paramCheckRule.minPort, paramCheckRule.maxPort));
paramCheckResponse.setMessage(
String.format("Param 'port' is illegal, the value should be between %d and %d.", paramCheckRule.minPort, paramCheckRule.maxPort));
return paramCheckResponse;
}
paramCheckResponse.setSuccess(true);
Expand Down Expand Up @@ -406,8 +422,8 @@ public ParamCheckResponse checkMetadataFormat(Map<String, String> metadata) {
}
if (totalLength > paramCheckRule.maxMetadataLength) {
paramCheckResponse.setSuccess(false);
paramCheckResponse.setMessage(String.format("Param 'Metadata' is illegal, the param length should not exceed %d.",
paramCheckRule.maxMetadataLength));
paramCheckResponse.setMessage(
String.format("Param 'Metadata' is illegal, the param length should not exceed %d.", paramCheckRule.maxMetadataLength));
return paramCheckResponse;
}
paramCheckResponse.setSuccess(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.alibaba.nacos.common.paramcheck;

import com.alibaba.nacos.common.utils.RandomUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -32,8 +33,11 @@ class DefaultParamCheckerTest {

DefaultParamChecker paramChecker;

int maxMetadataLength = RandomUtils.nextInt(1024, 10240);

@BeforeEach
void setUp() throws Exception {
System.setProperty("nacos.naming.service.metadata.length", String.valueOf(maxMetadataLength));
paramChecker = new DefaultParamChecker();
}

Expand Down Expand Up @@ -75,8 +79,7 @@ void testCheckParamInfoForNamespaceShowName() {
paramInfo.setNamespaceShowName("hsbfkj@$!#khdkad");
actual = paramChecker.checkParamInfoList(paramInfos);
assertFalse(actual.isSuccess());
assertEquals("Param 'namespaceShowName' is illegal, illegal characters should not appear in the param.",
actual.getMessage());
assertEquals("Param 'namespaceShowName' is illegal, illegal characters should not appear in the param.", actual.getMessage());
// Success
paramInfo.setNamespaceShowName("测试");
actual = paramChecker.checkParamInfoList(paramInfos);
Expand All @@ -98,8 +101,7 @@ void testCheckParamInfoForNamespaceId() {
paramInfo.setNamespaceId("hsbfkj@$!#khdkad");
actual = paramChecker.checkParamInfoList(paramInfos);
assertFalse(actual.isSuccess());
assertEquals("Param 'namespaceId/tenant' is illegal, illegal characters should not appear in the param.",
actual.getMessage());
assertEquals("Param 'namespaceId/tenant' is illegal, illegal characters should not appear in the param.", actual.getMessage());
// Success
paramInfo.setNamespaceId("123-ashdal");
actual = paramChecker.checkParamInfoList(paramInfos);
Expand Down Expand Up @@ -273,12 +275,12 @@ void testCheckParamInfoForMetadata() {
paramInfo.setMetadata(metadata);
// Max length
metadata.put("key1", "");
metadata.put("key2", buildStringLength(1024));
metadata.put("key2", buildStringLength(maxMetadataLength));
ParamCheckResponse actual = paramChecker.checkParamInfoList(paramInfos);
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
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);
assertTrue(actual.isSuccess());
}
Expand Down

0 comments on commit c56c415

Please sign in to comment.