Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #12466] serviceMetadata environment variables config #12477

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 All @@ -28,28 +30,32 @@
* @author zhuoguang
*/
public class DefaultParamChecker extends AbstractParamChecker {

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

不要修改缩进, 请使用nacos code style进行格式化

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的老师,晚点我修改下,这个没注意。~

private Pattern namespaceShowNamePattern;

private Pattern namespaceIdPattern;

private Pattern dataIdPattern;

private Pattern serviceNamePattern;

private Pattern groupPattern;

private Pattern clusterPattern;

private Pattern ipPattern;

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;
}

@Override
public ParamCheckResponse checkParamInfoList(List<ParamInfo> paramInfos) {
ParamCheckResponse paramCheckResponse = new ParamCheckResponse();
Expand All @@ -66,13 +72,14 @@ public ParamCheckResponse checkParamInfoList(List<ParamInfo> paramInfos) {
paramCheckResponse.setSuccess(true);
return paramCheckResponse;
}

@Override
public void initParamCheckRule() {
this.paramCheckRule = new ParamCheckRule();
initFormatPattern();
replaceParamCheckRuleByEnv();
}

private void initFormatPattern() {
this.namespaceShowNamePattern = Pattern.compile(this.paramCheckRule.namespaceShowNamePatternString);
this.namespaceIdPattern = Pattern.compile(this.paramCheckRule.namespaceIdPatternString);
Expand All @@ -82,7 +89,17 @@ private void initFormatPattern() {
this.clusterPattern = Pattern.compile(this.paramCheckRule.clusterPatternString);
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 @@ -138,7 +155,7 @@ public ParamCheckResponse checkParamInfoFormat(ParamInfo paramInfo) {
paramCheckResponse.setSuccess(true);
return paramCheckResponse;
}

/**
* Check namespace show name format.
*
Expand All @@ -165,7 +182,7 @@ public ParamCheckResponse checkNamespaceShowNameFormat(String namespaceShowName)
paramCheckResponse.setSuccess(true);
return paramCheckResponse;
}

/**
* Check namespace id format.
*
Expand All @@ -192,7 +209,7 @@ public ParamCheckResponse checkNamespaceIdFormat(String namespaceId) {
paramCheckResponse.setSuccess(true);
return paramCheckResponse;
}

/**
* Check data id format.
*
Expand All @@ -219,7 +236,7 @@ public ParamCheckResponse checkDataIdFormat(String dataId) {
paramCheckResponse.setSuccess(true);
return paramCheckResponse;
}

/**
* Check service name format.
*
Expand All @@ -246,7 +263,7 @@ public ParamCheckResponse checkServiceNameFormat(String serviceName) {
paramCheckResponse.setSuccess(true);
return paramCheckResponse;
}

/**
* Check group format.
*
Expand All @@ -273,7 +290,7 @@ public ParamCheckResponse checkGroupFormat(String group) {
paramCheckResponse.setSuccess(true);
return paramCheckResponse;
}

/**
* Check cluster format.
*
Expand All @@ -296,7 +313,7 @@ public ParamCheckResponse checkClusterFormat(String clusterString) {
paramCheckResponse.setSuccess(true);
return paramCheckResponse;
}

/**
* Check single cluster format.
*
Expand All @@ -309,7 +326,7 @@ public ParamCheckResponse checkSingleClusterFormat(String cluster) {
paramCheckResponse.setSuccess(true);
return paramCheckResponse;
}

if (cluster.length() > paramCheckRule.maxClusterLength) {
paramCheckResponse.setSuccess(false);
paramCheckResponse.setMessage(String.format("Param 'cluster' is illegal, the param length should not exceed %d.",
Expand All @@ -324,7 +341,7 @@ public ParamCheckResponse checkSingleClusterFormat(String cluster) {
paramCheckResponse.setSuccess(true);
return paramCheckResponse;
}

/**
* Check ip format.
*
Expand All @@ -351,7 +368,7 @@ public ParamCheckResponse checkIpFormat(String ip) {
paramCheckResponse.setSuccess(true);
return paramCheckResponse;
}

/**
* Check port format.
*
Expand Down Expand Up @@ -382,7 +399,7 @@ public ParamCheckResponse checkPortFormat(String port) {
paramCheckResponse.setSuccess(true);
return paramCheckResponse;
}

/**
* Check metadata format.
*
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 @@ -31,9 +32,12 @@
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 @@ -273,12 +277,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
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,9 @@ private Mac getMacInstance(Key key) {
Mac instance = Mac.getInstance(jcaName);
instance.init(key);
return instance;
} catch (NoSuchAlgorithmException | InvalidKeyException e) {
} catch (NoSuchAlgorithmException e) {
throw new IllegalArgumentException("No Such Algorithm: " + jcaName);
} catch (InvalidKeyException e) {
throw new IllegalArgumentException("Invalid key: " + key);
}
}
Expand Down
Loading