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

Fix#3973 #3974

Merged
merged 10 commits into from
Oct 13, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.alibaba.nacos.config.server.utils.MD5Util;
import com.alibaba.nacos.config.server.utils.ParamUtils;
import com.alibaba.nacos.config.server.utils.RequestUtil;
import com.alibaba.nacos.config.server.utils.NamespaceUtil;
import com.alibaba.nacos.config.server.utils.TimeUtils;
import com.alibaba.nacos.config.server.utils.ZipUtils;
import com.alibaba.nacos.sys.utils.InetUtils;
Expand Down Expand Up @@ -90,8 +91,6 @@ public class ConfigController {

private static final Logger LOGGER = LoggerFactory.getLogger(ConfigController.class);

private static final String NAMESPACE_PUBLIC_KEY = "public";

private static final String EXPORT_CONFIG_FILE_NAME = "nacos_config_export_";

private static final String EXPORT_CONFIG_FILE_NAME_EXT = ".zip";
Expand Down Expand Up @@ -196,7 +195,7 @@ public void getConfig(HttpServletRequest request, HttpServletResponse response,
throws IOException, ServletException, NacosException {
// check tenant
ParamUtils.checkTenant(tenant);
tenant = processTenant(tenant);
tenant = NamespaceUtil.processNamespaceParameter(tenant);
// check params
ParamUtils.checkParam(dataId, group, "datumId", "content");
ParamUtils.checkParam(tag);
Expand Down Expand Up @@ -469,7 +468,7 @@ public ResponseEntity<byte[]> exportConfig(@RequestParam(value = "dataId", requi
@RequestParam(value = "tenant", required = false, defaultValue = StringUtils.EMPTY) String tenant,
@RequestParam(value = "ids", required = false) List<Long> ids) {
ids.removeAll(Collections.singleton(null));
tenant = processTenant(tenant);
tenant = NamespaceUtil.processNamespaceParameter(tenant);
List<ConfigAllInfo> dataList = persistService.findAllConfigInfo4Export(dataId, group, tenant, appName, ids);
List<ZipUtils.ZipItem> zipItemList = new ArrayList<>();
StringBuilder metaData = null;
Expand Down Expand Up @@ -527,12 +526,12 @@ public RestResult<Map<String, Object>> importAndPublishConfig(HttpServletRequest
return ResultBuilder.buildResult(ResultCodeEnum.DATA_EMPTY, failedData);
}

if (StringUtils.isNotBlank(namespace)) {
if (persistService.tenantInfoCountByTenantId(namespace) <= 0) {
failedData.put("succCount", 0);
return ResultBuilder.buildResult(ResultCodeEnum.NAMESPACE_NOT_EXIST, failedData);
}
namespace = NamespaceUtil.processNamespaceParameter(namespace);
if (StringUtils.isNotBlank(namespace) && persistService.tenantInfoCountByTenantId(namespace) <= 0) {
failedData.put("succCount", 0);
return ResultBuilder.buildResult(ResultCodeEnum.NAMESPACE_NOT_EXIST, failedData);
}

List<ConfigAllInfo> configInfoList = null;
try {
ZipUtils.UnZipResult unziped = ZipUtils.unzip(file.getBytes());
Expand Down Expand Up @@ -628,10 +627,9 @@ public RestResult<Map<String, Object>> cloneConfig(HttpServletRequest request,
return ResultBuilder.buildResult(ResultCodeEnum.NO_SELECTED_CONFIG, failedData);
}
configBeansList.removeAll(Collections.singleton(null));

if (NAMESPACE_PUBLIC_KEY.equalsIgnoreCase(namespace)) {
namespace = "";
} else if (persistService.tenantInfoCountByTenantId(namespace) <= 0) {

namespace = NamespaceUtil.processNamespaceParameter(namespace);
if (StringUtils.isNotBlank(namespace) && persistService.tenantInfoCountByTenantId(namespace) <= 0) {
failedData.put("succCount", 0);
return ResultBuilder.buildResult(ResultCodeEnum.NAMESPACE_NOT_EXIST, failedData);
}
Expand Down Expand Up @@ -690,11 +688,4 @@ public RestResult<Map<String, Object>> cloneConfig(HttpServletRequest request,
return ResultBuilder.buildSuccessResult("Clone Completed Successfully", saveResult);
}

private String processTenant(String tenant) {
if (StringUtils.isEmpty(tenant) || NAMESPACE_PUBLIC_KEY.equalsIgnoreCase(tenant)) {
return "";
}
return tenant;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 1999-2018 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.config.server.utils;

import org.apache.commons.lang3.StringUtils;

/**
* namespace(tenant) util.
* Because config and naming treat namespace(tenant) differently,
* this tool class can only be used by the config module.
* @author klw(213539@qq.com)
* @date 2020/10/12 17:56
*/
public class NamespaceUtil {

private static final String NAMESPACE_PUBLIC_KEY = "public";

private static final String NAMESPACE_NULL_KEY = "null";

/**
* Treat the namespace(tenant) parameters with values of "public" and "null" as an empty string.
* @param tenant namespace(tenant) id
* @return java.lang.String A namespace(tenant) string processed
*/
public static String processNamespaceParameter(String tenant) {
if (StringUtils.isBlank(tenant) || NAMESPACE_PUBLIC_KEY.equalsIgnoreCase(tenant) || NAMESPACE_NULL_KEY
.equalsIgnoreCase(tenant)) {
return "";
}
return tenant.trim();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 1999-2018 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.config.server.utils;

import org.junit.Assert;
import org.junit.Test;

/**
* test NamespaceUtil.
*
* @author klw(213539 @ qq.com)
* @date 2020/10/13 9:46
*/
public class NamespaceUtilTest {

@Test
public void testProcessTenantParameter() {
String strPublic = "public";
String strNull = "null";
String strEmpty = "";
String strAbc = "abc";
String strdef123 = "def123";
String strAbcHasSpace = " abc ";
Assert.assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(strPublic));
Assert.assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(strNull));
Assert.assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(strEmpty));
Assert.assertEquals(strEmpty, NamespaceUtil.processNamespaceParameter(null));
Assert.assertEquals(strAbc, NamespaceUtil.processNamespaceParameter(strAbc));
Assert.assertEquals(strdef123, NamespaceUtil.processNamespaceParameter(strdef123));
Assert.assertEquals(strAbc, NamespaceUtil.processNamespaceParameter(strAbcHasSpace));
}

}