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

[#3063] fixed parameter validation boundary problem #3064

Merged
merged 3 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -48,17 +48,17 @@ public class NacosNamingMaintainService implements NamingMaintainService {

private NamingProxy serverProxy;

public NacosNamingMaintainService(String serverList) {
public NacosNamingMaintainService(String serverList) throws NacosException {
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverList);
init(properties);
}

public NacosNamingMaintainService(Properties properties) {
public NacosNamingMaintainService(Properties properties) throws NacosException {
init(properties);
}

private void init(Properties properties) {
private void init(Properties properties) throws NacosException {
ValidatorUtils.checkInitParam(properties);
namespace = InitUtils.initNamespaceForNaming(properties);
InitUtils.initSerialization();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,17 +73,17 @@ public class NacosNamingService implements NamingService {

private NamingProxy serverProxy;

public NacosNamingService(String serverList) {
public NacosNamingService(String serverList) throws NacosException {
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.SERVER_ADDR, serverList);
init(properties);
}

public NacosNamingService(Properties properties) {
public NacosNamingService(Properties properties) throws NacosException {
init(properties);
}

private void init(Properties properties) {
private void init(Properties properties) throws NacosException {
ValidatorUtils.checkInitParam(properties);
this.namespace = InitUtils.initNamespaceForNaming(properties);
InitUtils.initSerialization();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.nacos.client.utils;

import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.common.utils.StringUtils;

import java.util.Properties;
Expand All @@ -28,34 +29,15 @@
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
@SuppressWarnings("all")
public final class ValidatorUtils {

private static final Pattern CONTEXT_PATH_MATCH = Pattern.compile("(\\/)\\1+");
private static final Pattern IP_MATCH = Pattern.compile("([^\\/:]+)(:\\d+)");

public static void checkInitParam(Properties properties) {
checkServerAddr(properties.getProperty(PropertyKeyConst.SERVER_ADDR));
public static void checkInitParam(Properties properties) throws NacosException {
checkContextPath(properties.getProperty(PropertyKeyConst.CONTEXT_PATH));
}

public static void checkServerAddr(String serverAddr) {
if (StringUtils.isEmpty(serverAddr)) {
throw new IllegalArgumentException("Please set the serverAddr");
}
String[] addrs;
if (serverAddr.contains(StringUtils.COMMA)) {
addrs = serverAddr.split(StringUtils.COMMA);
} else {
addrs = new String[]{serverAddr};
}
for (String addr : addrs) {
Matcher matcher = IP_MATCH.matcher(addr.trim());
if (!matcher.find()) {
throw new IllegalArgumentException("Incorrect serverAddr address : " + addr + ", example should like ip:port or domain:port");
}
}
}

public static void checkContextPath(String contextPath) {
if (contextPath == null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,50 +56,4 @@ public void test_context_path_illegal_4() {
ValidatorUtils.checkContextPath(contextPath4);
}

@Test
public void test_server_addr() {
String serverAddr = "127.0.0.1:8848";
ValidatorUtils.checkServerAddr(serverAddr);
String serverAddrs = "127.0.0.1:8848,127.0.0.1:80,127.0.0.1:8809";
ValidatorUtils.checkServerAddr(serverAddrs);
}

@Test
public void test_server_addr_k8s() {
String serverAddr = "busybox-1.busybox-subdomain.default.svc.cluster.local:80";
ValidatorUtils.checkServerAddr(serverAddr);
String serverAddrs = "busybox-1.busybox-subdomain.default.svc.cluster.local:80,busybox-1.busybox-subdomain.default.svc.cluster.local:8111, busybox-1.busybox-subdomain.default.svc.cluster.local:8098";
ValidatorUtils.checkServerAddr(serverAddrs);
}

@Test(expected = IllegalArgumentException.class)
public void test_server_addr_err() {
String serverAddr = "127.0.0.1";
ValidatorUtils.checkServerAddr(serverAddr);
}

@Test(expected = IllegalArgumentException.class)
public void test_server_addr_illegal_err() {
String serverAddr = "127.0.0.1:";
ValidatorUtils.checkServerAddr(serverAddr);
}

@Test(expected = IllegalArgumentException.class)
public void test_server_addrs_err() {
String serverAddrs = "127.0.0.1:8848,127.0.0.1,127.0.0.1:8809";
ValidatorUtils.checkServerAddr(serverAddrs);
}

@Test(expected = IllegalArgumentException.class)
public void test_server_addr_k8s_err() {
String serverAddr = "busybox-1.busybox-subdomain.default.svc.cluster.local";
ValidatorUtils.checkServerAddr(serverAddr);
}

@Test(expected = IllegalArgumentException.class)
public void test_server_addrs_k8s_err() {
String serverAddrs = "busybox-1.busybox-subdomain.default.svc.cluster.local,busybox-1.busybox-subdomain.default.svc.cluster.local:8111, busybox-1.busybox-subdomain.default.svc.cluster.local:8098";
ValidatorUtils.checkServerAddr(serverAddrs);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ public static boolean isBlank(String str) {
return true;
}

public static boolean isAllBlank(String... strs) {
for (String str : strs) {
if (isNotBlank(str)) {
return false;
}
}
return true;
}

public static boolean isNotBlank(String str) {
return !isBlank(str);
}
Expand Down