[TMFB-4547]nacos没有对clusterName进行校验。出现实例成功注册,但实例查不出来的情况 (#8371)
This commit is contained in:
parent
39015341af
commit
0a74419c50
@ -33,6 +33,8 @@ public class NacosDeserializationException extends NacosRuntimeException {
|
||||
|
||||
private static final String MSG_FOR_SPECIFIED_CLASS = "Nacos deserialize for class [%s] failed. ";
|
||||
|
||||
private static final String ERROR_MSG_FOR_SPECIFIED_CLASS = "Nacos deserialize for class [%s] failed, cause error[%s]. ";
|
||||
|
||||
private Class<?> targetClass;
|
||||
|
||||
public NacosDeserializationException() {
|
||||
@ -53,12 +55,12 @@ public class NacosDeserializationException extends NacosRuntimeException {
|
||||
}
|
||||
|
||||
public NacosDeserializationException(Class<?> targetClass, Throwable throwable) {
|
||||
super(DESERIALIZE_ERROR_CODE, String.format(MSG_FOR_SPECIFIED_CLASS, targetClass.getName()), throwable);
|
||||
super(DESERIALIZE_ERROR_CODE, String.format(ERROR_MSG_FOR_SPECIFIED_CLASS, targetClass.getName(), throwable.getMessage()), throwable);
|
||||
this.targetClass = targetClass;
|
||||
}
|
||||
|
||||
public NacosDeserializationException(Type targetType, Throwable throwable) {
|
||||
super(DESERIALIZE_ERROR_CODE, String.format(MSG_FOR_SPECIFIED_CLASS, targetType.toString()), throwable);
|
||||
super(DESERIALIZE_ERROR_CODE, String.format(ERROR_MSG_FOR_SPECIFIED_CLASS, targetType.toString(), throwable.getMessage()), throwable);
|
||||
}
|
||||
|
||||
public Class<?> getTargetClass() {
|
||||
|
@ -37,6 +37,8 @@ import static com.alibaba.nacos.api.common.Constants.NUMBER_PATTERN;
|
||||
public class Instance implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -742906310567291979L;
|
||||
|
||||
private static final String CLUSTER_NAME_SYNTAX = "[0-9a-zA-Z-]+";
|
||||
|
||||
/**
|
||||
* unique id of this instance.
|
||||
@ -136,6 +138,7 @@ public class Instance implements Serializable {
|
||||
|
||||
public void setClusterName(final String clusterName) {
|
||||
this.clusterName = clusterName;
|
||||
checkClusterNameFormat();
|
||||
}
|
||||
|
||||
public String getServiceName() {
|
||||
@ -264,5 +267,19 @@ public class Instance implements Serializable {
|
||||
}
|
||||
return getMetadata().get(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* validate the cluster name.
|
||||
*
|
||||
* <p>the cluster name only the arabic numerals, letters and endashes are allowed.
|
||||
*
|
||||
* @throws IllegalArgumentException the cluster name is null, or the cluster name is
|
||||
* illegal
|
||||
*/
|
||||
public void checkClusterNameFormat() {
|
||||
if (!StringUtils.isEmpty(clusterName) && !clusterName.matches(CLUSTER_NAME_SYNTAX)) {
|
||||
throw new IllegalArgumentException("cluster name can only have these characters: 0-9a-zA-Z-, current: " + clusterName);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 1999-2020 Alibaba Group Holding Ltd.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.alibaba.nacos.api.naming.pojo;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.junit.Test;
|
||||
|
||||
public class InstanceTest extends TestCase {
|
||||
|
||||
@Test
|
||||
public void testCheckClusterNameFormat() {
|
||||
Instance instance = new Instance();
|
||||
instance.setClusterName("demo");
|
||||
assertEquals("demo", instance.getClusterName());
|
||||
try {
|
||||
instance.setClusterName("demo,demo1,demo2");
|
||||
} catch (Exception e) {
|
||||
assertEquals("cluster name can only have these characters: 0-9a-zA-Z-, current: demo,demo1,demo2", e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user