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 When the namespace does not exist, it is still possible to create configuration items #12890

Open
wants to merge 15 commits into
base: develop
Choose a base branch
from
Open
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 @@ -17,6 +17,7 @@
package com.alibaba.nacos.common.paramcheck;

import java.util.List;
import java.util.function.Function;

/**
* The type Abstract param checker.
Expand Down Expand Up @@ -46,6 +47,15 @@ public AbstractParamChecker() {
*/
public abstract ParamCheckResponse checkParamInfoList(List<ParamInfo> paramInfos);

/**
* Check param info list param check response.
*
* @param paramInfos the param infos
* @param extensionsParamChecker custom param checker function, accept params return paramCheckResponse.
* @return the param check response
*/
public abstract ParamCheckResponse checkParamInfoList(List<ParamInfo> paramInfos, Function<ParamInfo, ParamCheckResponse> extensionsParamChecker);

/**
* Init param check rule.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -58,6 +60,11 @@ public String getCheckerType() {

@Override
public ParamCheckResponse checkParamInfoList(List<ParamInfo> paramInfos) {
return this.checkParamInfoList(paramInfos, null);
}

@Override
public ParamCheckResponse checkParamInfoList(List<ParamInfo> paramInfos, Function<ParamInfo, ParamCheckResponse> extensionsParamChecker) {
ParamCheckResponse paramCheckResponse = new ParamCheckResponse();
if (paramInfos == null) {
paramCheckResponse.setSuccess(true);
Expand All @@ -68,6 +75,14 @@ public ParamCheckResponse checkParamInfoList(List<ParamInfo> paramInfos) {
if (!paramCheckResponse.isSuccess()) {
return paramCheckResponse;
}
// if extensionsParamChecker exists, check params
if (Objects.nonNull(extensionsParamChecker)) {
paramCheckResponse = extensionsParamChecker.apply(paramInfo);
// if not success ,return
if (!paramCheckResponse.isSuccess()) {
return paramCheckResponse;
}
}
}
paramCheckResponse.setSuccess(true);
return paramCheckResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.nacos.common.paramcheck;

import java.util.List;
import java.util.function.Function;

public class MockParamChecker extends AbstractParamChecker {

Expand All @@ -30,6 +31,11 @@ public ParamCheckResponse checkParamInfoList(List<ParamInfo> paramInfos) {
return new ParamCheckResponse();
}

@Override
public ParamCheckResponse checkParamInfoList(List<ParamInfo> paramInfos, Function<ParamInfo, ParamCheckResponse> extensionsParamChecker) {
return new ParamCheckResponse();
}

@Override
public void initParamCheckRule() {
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.alibaba.nacos.common.paramcheck.ParamInfo;
import com.alibaba.nacos.core.code.ControllerMethodsCache;
import com.alibaba.nacos.core.exception.ErrorCode;
import com.alibaba.nacos.core.utils.NamespaceParamCheckUtils;
import com.alibaba.nacos.plugin.control.Loggers;

import javax.servlet.Filter;
Expand Down Expand Up @@ -79,7 +80,8 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
List<ParamInfo> paramInfoList = httpParamExtractor.extractParam(req);
ParamCheckerManager paramCheckerManager = ParamCheckerManager.getInstance();
AbstractParamChecker paramChecker = paramCheckerManager.getParamChecker(ServerParamCheckConfig.getInstance().getActiveParamChecker());
ParamCheckResponse paramCheckResponse = paramChecker.checkParamInfoList(paramInfoList);
ParamCheckResponse paramCheckResponse = paramChecker.checkParamInfoList(paramInfoList,
paramInfo -> NamespaceParamCheckUtils.checkNamespaceExists(paramInfo.getNamespaceId()));
if (paramCheckResponse.isSuccess()) {
chain.doFilter(req, resp);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.alibaba.nacos.core.paramcheck.ExtractorManager;
import com.alibaba.nacos.core.paramcheck.ServerParamCheckConfig;
import com.alibaba.nacos.core.remote.AbstractRequestFilter;
import com.alibaba.nacos.core.utils.NamespaceParamCheckUtils;
import com.alibaba.nacos.plugin.control.Loggers;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -60,7 +61,8 @@ protected Response filter(Request request, RequestMeta meta, Class handlerClazz)
ParamCheckerManager paramCheckerManager = ParamCheckerManager.getInstance();
AbstractParamChecker paramChecker = paramCheckerManager.getParamChecker(
ServerParamCheckConfig.getInstance().getActiveParamChecker());
ParamCheckResponse checkResponse = paramChecker.checkParamInfoList(paramInfoList);
ParamCheckResponse checkResponse = paramChecker.checkParamInfoList(paramInfoList,
paramInfo -> NamespaceParamCheckUtils.checkNamespaceExists(paramInfo.getNamespaceId()));
if (!checkResponse.isSuccess()) {
return generateFailResponse(request, checkResponse.getMessage(), handlerClazz);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright 2024-2024 the original author or authors.
*
* 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
*
* https://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.core.utils;

import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.common.paramcheck.ParamCheckResponse;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.core.namespace.repository.NamespacePersistService;
import com.alibaba.nacos.sys.utils.ApplicationUtils;

import java.util.Objects;

/**
* <p>
* namespace checker util.
* </p>
*
* @author fuhouyu
* @since 2024/11/30 17:47
*/
public class NamespaceParamCheckUtils {

private NamespaceParamCheckUtils() {

}

/**
* check namespaceId exists.
* if namespace is null or empty or default public. return true.
* else query namespace by id. when not exists,then return false.
* @param namespaceId namespaceId
* @return paramCheckResponse
*/
public static ParamCheckResponse checkNamespaceExists(String namespaceId) {
ParamCheckResponse paramCheckResponse = new ParamCheckResponse();
if (StringUtils.isEmpty(namespaceId) || Objects.equals(Constants.DEFAULT_NAMESPACE_ID, namespaceId)) {
paramCheckResponse.setSuccess(true);
return paramCheckResponse;
}
NamespacePersistService namespacePersistService = ApplicationUtils.getBean(NamespacePersistService.class);
int count = namespacePersistService.tenantInfoCountByTenantId(namespaceId);
// if namespaceId is not exists, return false.
if (count == 0) {
paramCheckResponse.setSuccess(false);
paramCheckResponse.setMessage("namespaceId [ " + namespaceId + " ] not exist");
return paramCheckResponse;
}
// else return true
paramCheckResponse.setSuccess(true);
return paramCheckResponse;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.client.naming.remote.NamingClientProxy;
import com.alibaba.nacos.common.utils.ReflectUtils;
import com.alibaba.nacos.core.service.NamespaceOperationService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.server.LocalServerPort;

Expand All @@ -47,6 +49,9 @@ public abstract class AbstractInstanceOperate_ITCase {

private NamingService naming;

@Autowired
private NamespaceOperationService namespaceOperationService;

@LocalServerPort
private int port;

Expand Down Expand Up @@ -172,10 +177,11 @@ private void verifyInstanceList(List<Instance> instances, int size, String servi

@Test
public void regService() throws NacosException, InterruptedException {

final String namespaceId = "t3";
this.namespaceOperationService.createNamespace(namespaceId, namespaceId, namespaceId);
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:" + port);
properties.put(PropertyKeyConst.NAMESPACE, "t3");
properties.put(PropertyKeyConst.NAMESPACE, namespaceId);
properties.put(PropertyKeyConst.CONTEXT_PATH, contextPath);

naming = NamingFactory.createNamingService(properties);
Expand All @@ -187,6 +193,7 @@ public void regService() throws NacosException, InterruptedException {
List<Instance> instances = naming.getAllInstances(serviceName);

assertEquals(1, instances.size());
this.namespaceOperationService.removeNamespace(namespaceId);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.core.service.NamespaceOperationService;
import com.alibaba.nacos.test.base.Params;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpMethod;
Expand All @@ -55,6 +57,9 @@ class CPInstancesAPI_ITCase extends NamingBase {

private NamingService naming2;

@Autowired
private NamespaceOperationService namespaceOperationService;

@LocalServerPort
private int port;

Expand All @@ -75,10 +80,16 @@ void setUp() throws Exception {
properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1" + ":" + port);
naming2 = NamingFactory.createNamingService(properties);
isNamingServerReady();

// creat namespace
this.namespaceOperationService.createNamespace(TEST_NAMESPACE_1, TEST_NAMESPACE_1, TEST_NAMESPACE_1);
this.namespaceOperationService.createNamespace(TEST_NAMESPACE_2, TEST_NAMESPACE_2, TEST_NAMESPACE_2);
}

@AfterEach
void cleanup() throws Exception {
this.namespaceOperationService.removeNamespace(TEST_NAMESPACE_1);
this.namespaceOperationService.removeNamespace(TEST_NAMESPACE_2);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import com.alibaba.nacos.api.naming.listener.NamingEvent;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;
import com.alibaba.nacos.core.service.NamespaceOperationService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.server.LocalServerPort;

Expand Down Expand Up @@ -58,12 +60,19 @@
"server.servlet.context-path=/nacos"}, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
class MultiTenant_ITCase {

private static final String NAMESPACE_1 = "namespace-1";

private static final String NAMESPACE_2 = "namespace-2";

private NamingService naming;

private NamingService naming1;

private NamingService naming2;

@Autowired
private NamespaceOperationService namespaceOperationService;

@LocalServerPort
private int port;

Expand All @@ -84,13 +93,17 @@ void init() throws Exception {
break;
}

// creat namespace
this.namespaceOperationService.createNamespace(NAMESPACE_1, NAMESPACE_1, NAMESPACE_1);
this.namespaceOperationService.createNamespace(NAMESPACE_2, NAMESPACE_2, NAMESPACE_2);

Properties properties = new Properties();
properties.put(PropertyKeyConst.NAMESPACE, "namespace-1");
properties.put(PropertyKeyConst.NAMESPACE, NAMESPACE_1);
properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1" + ":" + port);
naming1 = NamingFactory.createNamingService(properties);

properties = new Properties();
properties.put(PropertyKeyConst.NAMESPACE, "namespace-2");
properties.put(PropertyKeyConst.NAMESPACE, NAMESPACE_2);
properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1" + ":" + port);
naming2 = NamingFactory.createNamingService(properties);
}
Expand Down
Loading
Loading