Skip to content

Commit

Permalink
fix: alibaba#12889 ,when namespace is not exists,then throw error
Browse files Browse the repository at this point in the history
  • Loading branch information
fuhouyu committed Nov 30, 2024
1 parent f943e72 commit 2678342
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 2 deletions.
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,13 +60,21 @@ 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);
return paramCheckResponse;
}
for (ParamInfo paramInfo : paramInfos) {
paramCheckResponse = checkParamInfoFormat(paramInfo);
if (Objects.nonNull(extensionsParamChecker)) {
paramCheckResponse = extensionsParamChecker.apply(paramInfo);
}
if (!paramCheckResponse.isSuccess()) {
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,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;

/**
* <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. 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;
}
}

0 comments on commit 2678342

Please sign in to comment.