diff --git a/common/src/main/java/com/alibaba/nacos/common/paramcheck/AbstractParamChecker.java b/common/src/main/java/com/alibaba/nacos/common/paramcheck/AbstractParamChecker.java index 6bb827ad7c8..dca59cf9698 100644 --- a/common/src/main/java/com/alibaba/nacos/common/paramcheck/AbstractParamChecker.java +++ b/common/src/main/java/com/alibaba/nacos/common/paramcheck/AbstractParamChecker.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.common.paramcheck; import java.util.List; +import java.util.function.Function; /** * The type Abstract param checker. @@ -46,6 +47,15 @@ public AbstractParamChecker() { */ public abstract ParamCheckResponse checkParamInfoList(List 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 paramInfos, Function extensionsParamChecker); + /** * Init param check rule. */ diff --git a/common/src/main/java/com/alibaba/nacos/common/paramcheck/DefaultParamChecker.java b/common/src/main/java/com/alibaba/nacos/common/paramcheck/DefaultParamChecker.java index d720828558f..228891dc1c6 100644 --- a/common/src/main/java/com/alibaba/nacos/common/paramcheck/DefaultParamChecker.java +++ b/common/src/main/java/com/alibaba/nacos/common/paramcheck/DefaultParamChecker.java @@ -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; /** @@ -58,6 +60,11 @@ public String getCheckerType() { @Override public ParamCheckResponse checkParamInfoList(List paramInfos) { + return this.checkParamInfoList(paramInfos, null); + } + + @Override + public ParamCheckResponse checkParamInfoList(List paramInfos, Function extensionsParamChecker) { ParamCheckResponse paramCheckResponse = new ParamCheckResponse(); if (paramInfos == null) { paramCheckResponse.setSuccess(true); @@ -65,6 +72,9 @@ public ParamCheckResponse checkParamInfoList(List paramInfos) { } for (ParamInfo paramInfo : paramInfos) { paramCheckResponse = checkParamInfoFormat(paramInfo); + if (Objects.nonNull(extensionsParamChecker)) { + paramCheckResponse = extensionsParamChecker.apply(paramInfo); + } if (!paramCheckResponse.isSuccess()) { return paramCheckResponse; } diff --git a/common/src/test/java/com/alibaba/nacos/common/paramcheck/MockParamChecker.java b/common/src/test/java/com/alibaba/nacos/common/paramcheck/MockParamChecker.java index 2660ee6c78f..9ced54bd2d6 100644 --- a/common/src/test/java/com/alibaba/nacos/common/paramcheck/MockParamChecker.java +++ b/common/src/test/java/com/alibaba/nacos/common/paramcheck/MockParamChecker.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.common.paramcheck; import java.util.List; +import java.util.function.Function; public class MockParamChecker extends AbstractParamChecker { @@ -30,6 +31,11 @@ public ParamCheckResponse checkParamInfoList(List paramInfos) { return new ParamCheckResponse(); } + @Override + public ParamCheckResponse checkParamInfoList(List paramInfos, Function extensionsParamChecker) { + return new ParamCheckResponse(); + } + @Override public void initParamCheckRule() { } diff --git a/core/src/main/java/com/alibaba/nacos/core/paramcheck/ParamCheckerFilter.java b/core/src/main/java/com/alibaba/nacos/core/paramcheck/ParamCheckerFilter.java index 6b1d1418bb1..c0ae7bbe354 100644 --- a/core/src/main/java/com/alibaba/nacos/core/paramcheck/ParamCheckerFilter.java +++ b/core/src/main/java/com/alibaba/nacos/core/paramcheck/ParamCheckerFilter.java @@ -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; @@ -79,7 +80,8 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha List 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 { diff --git a/core/src/main/java/com/alibaba/nacos/core/remote/grpc/RemoteParamCheckFilter.java b/core/src/main/java/com/alibaba/nacos/core/remote/grpc/RemoteParamCheckFilter.java index 78875fbf31c..d3c0112339f 100644 --- a/core/src/main/java/com/alibaba/nacos/core/remote/grpc/RemoteParamCheckFilter.java +++ b/core/src/main/java/com/alibaba/nacos/core/remote/grpc/RemoteParamCheckFilter.java @@ -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; @@ -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); } diff --git a/core/src/main/java/com/alibaba/nacos/core/utils/NamespaceParamCheckUtils.java b/core/src/main/java/com/alibaba/nacos/core/utils/NamespaceParamCheckUtils.java new file mode 100644 index 00000000000..eb1d7bda05f --- /dev/null +++ b/core/src/main/java/com/alibaba/nacos/core/utils/NamespaceParamCheckUtils.java @@ -0,0 +1,63 @@ +/* + * 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.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; + +/** + *

+ * namespace checker util. + *

+ * + * @author fuhouyu + * @since 2024/11/30 17:47 + */ +public class NamespaceParamCheckUtils { + + private NamespaceParamCheckUtils() { + + } + + /** + * check namespaceId exists. + * if namespace is null or empty. 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)) { + 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; + } +}