From cae21e2dcde57c61a486839a58c34ed63f8d657f Mon Sep 17 00:00:00 2001 From: Sunrisea <49605583+Sunrisea@users.noreply.github.com> Date: Mon, 10 Jul 2023 14:43:51 +0800 Subject: [PATCH] [ISSUE #10734] Implement grpc server interceptor and grpc param extractors (#10745) * For #10734,Implement grpc server interceptor and grpc param extractors * For #10734,add unit test for grpc server interceptor and grpc param extractors * For #10734,alter the test case * For #10734,delete the ConnectionSetupRequestParamExtractor --- .../paramcheck/RpcParamExtractorManager.java | 4 ++ .../BatchInstanceRequestParamExtractor.java | 62 ++++++++++++++++ ...onfigBatchListenRequestParamExtractor.java | 54 ++++++++++++++ .../impl/ConfigRequestParamExtractor.java | 53 ++++++++++++++ .../impl/InstanceRequestParamExtractor.java | 55 ++++++++++++++ .../ServiceListRequestParamExtractor.java | 46 ++++++++++++ .../ServiceQueryRequestParamExtractor.java | 55 ++++++++++++++ ...SubscribeServiceRequestParamExtractor.java | 54 ++++++++++++++ .../nacos/core/remote/BaseRpcServer.java | 2 +- .../core/remote/grpc/BaseGrpcServer.java | 4 +- .../grpc/GrpcServerParamCheckInterceptor.java | 69 ++++++++++++++++++ ....core.paramcheck.AbstractRpcParamExtractor | 23 ++++++ ...atchInstanceRequestParamExtractorTest.java | 46 ++++++++++++ ...gBatchListenRequestParamExtractorTest.java | 46 ++++++++++++ .../impl/ConfigRequestParamExtractorTest.java | 71 +++++++++++++++++++ .../InstanceRequestParamExtractorTest.java | 46 ++++++++++++ .../ServiceListRequestParamExtractorTest.java | 46 ++++++++++++ ...ServiceQueryRequestParamExtractorTest.java | 46 ++++++++++++ ...cribeServiceRequestParamExtractorTest.java | 47 ++++++++++++ .../AbstractInstanceOperate_ITCase.java | 6 +- 20 files changed, 829 insertions(+), 6 deletions(-) create mode 100644 core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/BatchInstanceRequestParamExtractor.java create mode 100644 core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/ConfigBatchListenRequestParamExtractor.java create mode 100644 core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/ConfigRequestParamExtractor.java create mode 100644 core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/InstanceRequestParamExtractor.java create mode 100644 core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/ServiceListRequestParamExtractor.java create mode 100644 core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/ServiceQueryRequestParamExtractor.java create mode 100644 core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/SubscribeServiceRequestParamExtractor.java create mode 100644 core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcServerParamCheckInterceptor.java create mode 100644 core/src/main/resources/META-INF/services/com.alibaba.nacos.core.paramcheck.AbstractRpcParamExtractor create mode 100644 core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/BatchInstanceRequestParamExtractorTest.java create mode 100644 core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/ConfigBatchListenRequestParamExtractorTest.java create mode 100644 core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/ConfigRequestParamExtractorTest.java create mode 100644 core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/InstanceRequestParamExtractorTest.java create mode 100644 core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/ServiceListRequestParamExtractorTest.java create mode 100644 core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/ServiceQueryRequestParamExtractorTest.java create mode 100644 core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/SubscribeServiceRequestParamExtractorTest.java diff --git a/core/src/main/java/com/alibaba/nacos/core/paramcheck/RpcParamExtractorManager.java b/core/src/main/java/com/alibaba/nacos/core/paramcheck/RpcParamExtractorManager.java index 10402a74a83..5545a79e12d 100644 --- a/core/src/main/java/com/alibaba/nacos/core/paramcheck/RpcParamExtractorManager.java +++ b/core/src/main/java/com/alibaba/nacos/core/paramcheck/RpcParamExtractorManager.java @@ -18,6 +18,7 @@ import com.alibaba.nacos.api.remote.request.Request; import com.alibaba.nacos.common.spi.NacosServiceLoader; +import com.alibaba.nacos.common.utils.StringUtils; import java.util.Collection; import java.util.List; @@ -60,6 +61,9 @@ public static RpcParamExtractorManager getInstance() { } public AbstractRpcParamExtractor getExtractor(String type) { + if (StringUtils.isBlank(type)) { + return DEFAULT_EXTRACTOR; + } AbstractRpcParamExtractor extractor = extractorMap.get(type); if (extractor == null) { extractor = DEFAULT_EXTRACTOR; diff --git a/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/BatchInstanceRequestParamExtractor.java b/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/BatchInstanceRequestParamExtractor.java new file mode 100644 index 00000000000..a79aad53491 --- /dev/null +++ b/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/BatchInstanceRequestParamExtractor.java @@ -0,0 +1,62 @@ +/* + * Copyright 1999-2023 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.core.paramcheck.impl; + +import com.alibaba.nacos.api.naming.pojo.Instance; +import com.alibaba.nacos.api.naming.remote.request.BatchInstanceRequest; +import com.alibaba.nacos.api.remote.request.Request; +import com.alibaba.nacos.common.paramcheck.ParamCheckUtils; +import com.alibaba.nacos.common.paramcheck.ParamInfo; +import com.alibaba.nacos.core.paramcheck.AbstractRpcParamExtractor; + +import java.util.List; + +/** + * Param Extractor and check for grpc batch instance request{@link BatchInstanceRequest}. + * + * @author zhuoguang + */ +public class BatchInstanceRequestParamExtractor extends AbstractRpcParamExtractor { + + @Override + public void init() { + addTargetRequest(BatchInstanceRequest.class.getSimpleName()); + } + + @Override + public void extractParamAndCheck(Request request) throws Exception { + BatchInstanceRequest req = (BatchInstanceRequest) request; + ParamInfo paramInfo = new ParamInfo(); + paramInfo.setNamespaceId(req.getNamespace()); + paramInfo.setServiceName(req.getServiceName()); + paramInfo.setGroup(req.getGroupName()); + ParamCheckUtils.checkParamInfoFormat(paramInfo); + List instanceList = req.getInstances(); + if (instanceList == null) { + return; + } + for (Instance instance : instanceList) { + ParamInfo instanceParamInfo = new ParamInfo(); + instanceParamInfo.setIp(instance.getIp()); + instanceParamInfo.setPort(String.valueOf(instance.getPort())); + instanceParamInfo.setServiceName(instance.getServiceName()); + instanceParamInfo.setCluster(instance.getClusterName()); + instanceParamInfo.setMetadata(instance.getMetadata()); + ParamCheckUtils.checkParamInfoFormat(instanceParamInfo); + } + } +} diff --git a/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/ConfigBatchListenRequestParamExtractor.java b/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/ConfigBatchListenRequestParamExtractor.java new file mode 100644 index 00000000000..59dcf413e47 --- /dev/null +++ b/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/ConfigBatchListenRequestParamExtractor.java @@ -0,0 +1,54 @@ +/* + * Copyright 1999-2023 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.core.paramcheck.impl; + +import com.alibaba.nacos.api.config.remote.request.ConfigBatchListenRequest; +import com.alibaba.nacos.api.remote.request.Request; +import com.alibaba.nacos.common.paramcheck.ParamCheckUtils; +import com.alibaba.nacos.common.paramcheck.ParamInfo; +import com.alibaba.nacos.core.paramcheck.AbstractRpcParamExtractor; + +import java.util.List; + +/** + * Param extractor and checker for grpc config batch listen request{@link ConfigBatchListenRequest}. + * + * @author zhuoguang + */ +public class ConfigBatchListenRequestParamExtractor extends AbstractRpcParamExtractor { + + @Override + public void init() { + addTargetRequest(ConfigBatchListenRequest.class.getSimpleName()); + } + + @Override + public void extractParamAndCheck(Request request) throws Exception { + ConfigBatchListenRequest req = (ConfigBatchListenRequest) request; + List configListenContextList = req.getConfigListenContexts(); + if (configListenContextList == null) { + return; + } + for (ConfigBatchListenRequest.ConfigListenContext configListenContext : configListenContextList) { + ParamInfo configListContextParamInfo = new ParamInfo(); + configListContextParamInfo.setNamespaceId(configListenContext.getTenant()); + configListContextParamInfo.setGroup(configListenContext.getGroup()); + configListContextParamInfo.setDataId(configListenContext.getDataId()); + ParamCheckUtils.checkParamInfoFormat(configListContextParamInfo); + } + } +} diff --git a/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/ConfigRequestParamExtractor.java b/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/ConfigRequestParamExtractor.java new file mode 100644 index 00000000000..b4613ef9e12 --- /dev/null +++ b/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/ConfigRequestParamExtractor.java @@ -0,0 +1,53 @@ +/* + * Copyright 1999-2023 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.core.paramcheck.impl; + +import com.alibaba.nacos.api.config.remote.request.AbstractConfigRequest; +import com.alibaba.nacos.api.config.remote.request.ConfigPublishRequest; +import com.alibaba.nacos.api.config.remote.request.ConfigQueryRequest; +import com.alibaba.nacos.api.config.remote.request.ConfigRemoveRequest; +import com.alibaba.nacos.api.config.remote.request.cluster.ConfigChangeClusterSyncRequest; +import com.alibaba.nacos.api.remote.request.Request; +import com.alibaba.nacos.common.paramcheck.ParamCheckUtils; +import com.alibaba.nacos.common.paramcheck.ParamInfo; +import com.alibaba.nacos.core.paramcheck.AbstractRpcParamExtractor; + +/** + * The type Config request param extractor {@link AbstractConfigRequest}. + * + * @author zhuoguang + */ +public class ConfigRequestParamExtractor extends AbstractRpcParamExtractor { + + @Override + public void init() { + addTargetRequest(ConfigRemoveRequest.class.getSimpleName()); + addTargetRequest(ConfigQueryRequest.class.getSimpleName()); + addTargetRequest(ConfigPublishRequest.class.getSimpleName()); + addTargetRequest(ConfigChangeClusterSyncRequest.class.getSimpleName()); + } + + @Override + public void extractParamAndCheck(Request request) throws Exception { + AbstractConfigRequest req = (AbstractConfigRequest) request; + ParamInfo paramInfo = new ParamInfo(); + paramInfo.setDataId(req.getDataId()); + paramInfo.setGroup(req.getGroup()); + paramInfo.setNamespaceId(req.getTenant()); + ParamCheckUtils.checkParamInfoFormat(paramInfo); + } +} diff --git a/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/InstanceRequestParamExtractor.java b/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/InstanceRequestParamExtractor.java new file mode 100644 index 00000000000..a33a3cc715f --- /dev/null +++ b/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/InstanceRequestParamExtractor.java @@ -0,0 +1,55 @@ +/* + * Copyright 1999-2023 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.core.paramcheck.impl; + +import com.alibaba.nacos.api.naming.pojo.Instance; +import com.alibaba.nacos.api.naming.remote.request.InstanceRequest; +import com.alibaba.nacos.api.remote.request.Request; +import com.alibaba.nacos.common.paramcheck.ParamCheckUtils; +import com.alibaba.nacos.common.paramcheck.ParamInfo; +import com.alibaba.nacos.core.paramcheck.AbstractRpcParamExtractor; + +/** + * Param extractor for {@link InstanceRequest}. + * + * @author zhuoguang + */ +public class InstanceRequestParamExtractor extends AbstractRpcParamExtractor { + + @Override + public void init() { + addTargetRequest(InstanceRequest.class.getSimpleName()); + } + + @Override + public void extractParamAndCheck(Request request) throws Exception { + InstanceRequest req = (InstanceRequest) request; + ParamInfo paramInfo = new ParamInfo(); + paramInfo.setNamespaceId(req.getNamespace()); + paramInfo.setServiceName(req.getServiceName()); + paramInfo.setGroup(req.getGroupName()); + Instance instance = req.getInstance(); + if (instance == null) { + return; + } + paramInfo.setIp(instance.getIp()); + paramInfo.setPort(String.valueOf(instance.getPort())); + paramInfo.setCluster(instance.getClusterName()); + paramInfo.setMetadata(instance.getMetadata()); + ParamCheckUtils.checkParamInfoFormat(paramInfo); + } +} diff --git a/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/ServiceListRequestParamExtractor.java b/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/ServiceListRequestParamExtractor.java new file mode 100644 index 00000000000..e9cc3916709 --- /dev/null +++ b/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/ServiceListRequestParamExtractor.java @@ -0,0 +1,46 @@ +/* + * Copyright 1999-2023 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.core.paramcheck.impl; + +import com.alibaba.nacos.api.naming.remote.request.ServiceListRequest; +import com.alibaba.nacos.api.remote.request.Request; +import com.alibaba.nacos.common.paramcheck.ParamCheckUtils; +import com.alibaba.nacos.common.paramcheck.ParamInfo; +import com.alibaba.nacos.core.paramcheck.AbstractRpcParamExtractor; + +/** + * Param extractor for {@link ServiceListRequest}. + * + * @author zhuoguang + */ +public class ServiceListRequestParamExtractor extends AbstractRpcParamExtractor { + + @Override + public void init() { + addTargetRequest(ServiceListRequest.class.getSimpleName()); + } + + @Override + public void extractParamAndCheck(Request request) throws Exception { + ServiceListRequest req = (ServiceListRequest) request; + ParamInfo paramInfo = new ParamInfo(); + paramInfo.setNamespaceId(req.getNamespace()); + paramInfo.setServiceName(req.getServiceName()); + paramInfo.setGroup(req.getGroupName()); + ParamCheckUtils.checkParamInfoFormat(paramInfo); + } +} diff --git a/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/ServiceQueryRequestParamExtractor.java b/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/ServiceQueryRequestParamExtractor.java new file mode 100644 index 00000000000..bc781d9af48 --- /dev/null +++ b/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/ServiceQueryRequestParamExtractor.java @@ -0,0 +1,55 @@ +/* + * Copyright 1999-2023 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.core.paramcheck.impl; + +import com.alibaba.nacos.api.naming.remote.request.ServiceQueryRequest; +import com.alibaba.nacos.api.remote.request.Request; +import com.alibaba.nacos.common.paramcheck.ParamCheckUtils; +import com.alibaba.nacos.common.paramcheck.ParamInfo; +import com.alibaba.nacos.common.utils.StringUtils; +import com.alibaba.nacos.core.paramcheck.AbstractRpcParamExtractor; + +/** + * Param extractor for {@link ServiceQueryRequest}. + * + * @author zhuoguang + */ +public class ServiceQueryRequestParamExtractor extends AbstractRpcParamExtractor { + + @Override + public void init() { + addTargetRequest(ServiceQueryRequest.class.getSimpleName()); + } + + @Override + public void extractParamAndCheck(Request request) throws Exception { + ServiceQueryRequest req = (ServiceQueryRequest) request; + ParamInfo paramInfo = new ParamInfo(); + paramInfo.setNamespaceId(req.getNamespace()); + paramInfo.setServiceName(req.getServiceName()); + paramInfo.setGroup(req.getGroupName()); + paramInfo.setPort(String.valueOf(req.getUdpPort())); + ParamCheckUtils.checkParamInfoFormat(paramInfo); + String clusterString = req.getCluster(); + if (StringUtils.isNotBlank(clusterString)) { + String[] clusters = clusterString.split(","); + for (String cluster : clusters) { + ParamCheckUtils.checkClusterFormat(cluster); + } + } + } +} diff --git a/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/SubscribeServiceRequestParamExtractor.java b/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/SubscribeServiceRequestParamExtractor.java new file mode 100644 index 00000000000..0193ac7a466 --- /dev/null +++ b/core/src/main/java/com/alibaba/nacos/core/paramcheck/impl/SubscribeServiceRequestParamExtractor.java @@ -0,0 +1,54 @@ +/* + * Copyright 1999-2023 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.core.paramcheck.impl; + +import com.alibaba.nacos.api.naming.remote.request.SubscribeServiceRequest; +import com.alibaba.nacos.api.remote.request.Request; +import com.alibaba.nacos.common.paramcheck.ParamCheckUtils; +import com.alibaba.nacos.common.paramcheck.ParamInfo; +import com.alibaba.nacos.common.utils.StringUtils; +import com.alibaba.nacos.core.paramcheck.AbstractRpcParamExtractor; + +/** + * Param extractor for {@link SubscribeServiceRequest}. + * + * @author zhuoguang + */ +public class SubscribeServiceRequestParamExtractor extends AbstractRpcParamExtractor { + + @Override + public void init() { + addTargetRequest(SubscribeServiceRequest.class.getSimpleName()); + } + + @Override + public void extractParamAndCheck(Request request) throws Exception { + SubscribeServiceRequest req = (SubscribeServiceRequest) request; + ParamInfo paramInfo = new ParamInfo(); + paramInfo.setNamespaceId(req.getNamespace()); + paramInfo.setServiceName(req.getServiceName()); + paramInfo.setGroup(req.getGroupName()); + ParamCheckUtils.checkParamInfoFormat(paramInfo); + String clusterString = req.getClusters(); + if (StringUtils.isNotBlank(clusterString)) { + String[] clusters = clusterString.split(","); + for (String cluster : clusters) { + ParamCheckUtils.checkClusterFormat(cluster); + } + } + } +} diff --git a/core/src/main/java/com/alibaba/nacos/core/remote/BaseRpcServer.java b/core/src/main/java/com/alibaba/nacos/core/remote/BaseRpcServer.java index fac469256c2..a60ee1b4e30 100644 --- a/core/src/main/java/com/alibaba/nacos/core/remote/BaseRpcServer.java +++ b/core/src/main/java/com/alibaba/nacos/core/remote/BaseRpcServer.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2020 Alibaba Group Holding Ltd. + * Copyright 1999-2023 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. diff --git a/core/src/main/java/com/alibaba/nacos/core/remote/grpc/BaseGrpcServer.java b/core/src/main/java/com/alibaba/nacos/core/remote/grpc/BaseGrpcServer.java index 9007aea2512..983fe041524 100644 --- a/core/src/main/java/com/alibaba/nacos/core/remote/grpc/BaseGrpcServer.java +++ b/core/src/main/java/com/alibaba/nacos/core/remote/grpc/BaseGrpcServer.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2020 Alibaba Group Holding Ltd. + * Copyright 1999-2023 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. @@ -87,7 +87,7 @@ public ConnectionType getConnectionType() { @Override public void startServer() throws Exception { final MutableHandlerRegistry handlerRegistry = new MutableHandlerRegistry(); - addServices(handlerRegistry, new GrpcConnectionInterceptor()); + addServices(handlerRegistry, new GrpcConnectionInterceptor(), new GrpcServerParamCheckInterceptor()); NettyServerBuilder builder = NettyServerBuilder.forPort(getServicePort()).executor(getRpcExecutor()); if (rpcServerTlsConfig.getEnableTls()) { diff --git a/core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcServerParamCheckInterceptor.java b/core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcServerParamCheckInterceptor.java new file mode 100644 index 00000000000..195af4c6b45 --- /dev/null +++ b/core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcServerParamCheckInterceptor.java @@ -0,0 +1,69 @@ +/* + * Copyright 1999-2023 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.core.remote.grpc; + +import com.alibaba.nacos.api.grpc.auto.Payload; +import com.alibaba.nacos.api.remote.request.Request; +import com.alibaba.nacos.common.remote.client.grpc.GrpcUtils; +import com.alibaba.nacos.core.paramcheck.AbstractRpcParamExtractor; +import com.alibaba.nacos.core.paramcheck.RpcParamExtractorManager; +import com.alibaba.nacos.sys.env.EnvUtil; +import io.grpc.ForwardingServerCallListener; +import io.grpc.Metadata; +import io.grpc.ServerCall; +import io.grpc.ServerCallHandler; +import io.grpc.ServerInterceptor; +import io.grpc.Status; + +/** + * Grpc server interceptor for param check. + * + * @author zhuoguang + */ +public class GrpcServerParamCheckInterceptor implements ServerInterceptor { + + @Override + public ServerCall.Listener interceptCall(ServerCall call, Metadata headers, + ServerCallHandler next) { + return new ForwardingServerCallListener.SimpleForwardingServerCallListener(next.startCall(call, headers)) { + @Override + public void onMessage(T message) { + boolean ifParamCheck = EnvUtil.getProperty("nacos.paramcheck", Boolean.class, true); + if (!ifParamCheck) { + super.onMessage(message); + return; + } + Payload payload = (Payload) message; + String type = payload.getMetadata().getType(); + Object parseObj; + try { + parseObj = GrpcUtils.parse(payload); + if (parseObj instanceof Request) { + Request request = (Request) parseObj; + RpcParamExtractorManager extractorManager = RpcParamExtractorManager.getInstance(); + AbstractRpcParamExtractor extractor = extractorManager.getExtractor(type); + extractor.extractParamAndCheck(request); + } + super.onMessage(message); + } catch (Exception e) { + call.close(Status.INVALID_ARGUMENT.withDescription(e.getMessage()), headers); + } + + } + }; + } +} diff --git a/core/src/main/resources/META-INF/services/com.alibaba.nacos.core.paramcheck.AbstractRpcParamExtractor b/core/src/main/resources/META-INF/services/com.alibaba.nacos.core.paramcheck.AbstractRpcParamExtractor new file mode 100644 index 00000000000..9410d161628 --- /dev/null +++ b/core/src/main/resources/META-INF/services/com.alibaba.nacos.core.paramcheck.AbstractRpcParamExtractor @@ -0,0 +1,23 @@ +# +# Copyright 1999-2023 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. +# + +com.alibaba.nacos.core.paramcheck.impl.SubscribeServiceRequestParamExtractor +com.alibaba.nacos.core.paramcheck.impl.ServiceQueryRequestParamExtractor +com.alibaba.nacos.core.paramcheck.impl.ServiceListRequestParamExtractor +com.alibaba.nacos.core.paramcheck.impl.InstanceRequestParamExtractor +com.alibaba.nacos.core.paramcheck.impl.ConfigRequestParamExtractor +com.alibaba.nacos.core.paramcheck.impl.ConfigBatchListenRequestParamExtractor +com.alibaba.nacos.core.paramcheck.impl.BatchInstanceRequestParamExtractor \ No newline at end of file diff --git a/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/BatchInstanceRequestParamExtractorTest.java b/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/BatchInstanceRequestParamExtractorTest.java new file mode 100644 index 00000000000..8727ccfe3e1 --- /dev/null +++ b/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/BatchInstanceRequestParamExtractorTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 1999-2023 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.core.paramcheck.impl; + +import com.alibaba.nacos.api.naming.remote.request.BatchInstanceRequest; +import com.alibaba.nacos.core.paramcheck.AbstractRpcParamExtractor; +import com.alibaba.nacos.core.paramcheck.RpcParamExtractorManager; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class BatchInstanceRequestParamExtractorTest { + + private static BatchInstanceRequest req; + + @BeforeClass + public static void initBatchInstanceRequest() { + req = new BatchInstanceRequest(); + } + + /** + * Test extract param and check. + */ + @Test + public void testExtractParamAndCheck() throws Exception { + RpcParamExtractorManager paramExtractorManager = RpcParamExtractorManager.getInstance(); + AbstractRpcParamExtractor extractor = paramExtractorManager.getExtractor(req.getClass().getSimpleName()); + assertEquals(extractor.getClass().getSimpleName(), BatchInstanceRequestParamExtractor.class.getSimpleName()); + extractor.extractParamAndCheck(req); + } +} \ No newline at end of file diff --git a/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/ConfigBatchListenRequestParamExtractorTest.java b/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/ConfigBatchListenRequestParamExtractorTest.java new file mode 100644 index 00000000000..7a3cb9ab479 --- /dev/null +++ b/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/ConfigBatchListenRequestParamExtractorTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 1999-2023 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.core.paramcheck.impl; + +import com.alibaba.nacos.api.config.remote.request.ConfigBatchListenRequest; +import com.alibaba.nacos.core.paramcheck.AbstractRpcParamExtractor; +import com.alibaba.nacos.core.paramcheck.RpcParamExtractorManager; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class ConfigBatchListenRequestParamExtractorTest { + + private static ConfigBatchListenRequest req; + + @BeforeClass + public static void initConfigBatchListenRequest() { + req = new ConfigBatchListenRequest(); + } + + /** + * Test extract param and check. + */ + @Test + public void testExtractParamAndCheck() throws Exception { + RpcParamExtractorManager paramExtractorManager = RpcParamExtractorManager.getInstance(); + AbstractRpcParamExtractor extractor = paramExtractorManager.getExtractor(req.getClass().getSimpleName()); + assertEquals(extractor.getClass().getSimpleName(), ConfigBatchListenRequestParamExtractor.class.getSimpleName()); + extractor.extractParamAndCheck(req); + } +} \ No newline at end of file diff --git a/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/ConfigRequestParamExtractorTest.java b/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/ConfigRequestParamExtractorTest.java new file mode 100644 index 00000000000..eae348c8fbe --- /dev/null +++ b/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/ConfigRequestParamExtractorTest.java @@ -0,0 +1,71 @@ +/* + * Copyright 1999-2023 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.core.paramcheck.impl; + +import com.alibaba.nacos.api.config.remote.request.AbstractConfigRequest; +import com.alibaba.nacos.api.config.remote.request.ConfigPublishRequest; +import com.alibaba.nacos.api.config.remote.request.ConfigQueryRequest; +import com.alibaba.nacos.api.config.remote.request.ConfigRemoveRequest; +import com.alibaba.nacos.api.config.remote.request.cluster.ConfigChangeClusterSyncRequest; +import com.alibaba.nacos.core.paramcheck.AbstractRpcParamExtractor; +import com.alibaba.nacos.core.paramcheck.RpcParamExtractorManager; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class ConfigRequestParamExtractorTest { + + private static AbstractConfigRequest req1; + + private static AbstractConfigRequest req2; + + private static AbstractConfigRequest req3; + + private static AbstractConfigRequest req4; + + @BeforeClass + public static void initAbstractConfigRequest() { + req1 = new ConfigPublishRequest(); + req2 = new ConfigQueryRequest(); + req3 = new ConfigRemoveRequest(); + req4 = new ConfigChangeClusterSyncRequest(); + } + + /** + * Test extract param and check. + */ + @Test + public void testExtractParamAndCheck() throws Exception { + RpcParamExtractorManager paramExtractorManager = RpcParamExtractorManager.getInstance(); + AbstractRpcParamExtractor extractor1 = paramExtractorManager.getExtractor(req1.getClass().getSimpleName()); + assertEquals(extractor1.getClass().getSimpleName(), ConfigRequestParamExtractor.class.getSimpleName()); + extractor1.extractParamAndCheck(req1); + + AbstractRpcParamExtractor extractor2 = paramExtractorManager.getExtractor(req2.getClass().getSimpleName()); + assertEquals(extractor2.getClass().getSimpleName(), ConfigRequestParamExtractor.class.getSimpleName()); + extractor2.extractParamAndCheck(req2); + + AbstractRpcParamExtractor extractor3 = paramExtractorManager.getExtractor(req3.getClass().getSimpleName()); + assertEquals(extractor3.getClass().getSimpleName(), ConfigRequestParamExtractor.class.getSimpleName()); + extractor3.extractParamAndCheck(req3); + + AbstractRpcParamExtractor extractor4 = paramExtractorManager.getExtractor(req4.getClass().getSimpleName()); + assertEquals(extractor4.getClass().getSimpleName(), ConfigRequestParamExtractor.class.getSimpleName()); + extractor4.extractParamAndCheck(req4); + } +} \ No newline at end of file diff --git a/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/InstanceRequestParamExtractorTest.java b/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/InstanceRequestParamExtractorTest.java new file mode 100644 index 00000000000..97d4ac28048 --- /dev/null +++ b/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/InstanceRequestParamExtractorTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 1999-2023 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.core.paramcheck.impl; + +import com.alibaba.nacos.api.naming.remote.request.InstanceRequest; +import com.alibaba.nacos.core.paramcheck.AbstractRpcParamExtractor; +import com.alibaba.nacos.core.paramcheck.RpcParamExtractorManager; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class InstanceRequestParamExtractorTest { + + private static InstanceRequest req; + + @BeforeClass + public static void initInstanceRequest() { + req = new InstanceRequest(); + } + + /** + * Test extract param and check. + */ + @Test + public void testExtractParamAndCheck() throws Exception { + RpcParamExtractorManager paramExtractorManager = RpcParamExtractorManager.getInstance(); + AbstractRpcParamExtractor extractor = paramExtractorManager.getExtractor(req.getClass().getSimpleName()); + assertEquals(extractor.getClass().getSimpleName(), InstanceRequestParamExtractor.class.getSimpleName()); + extractor.extractParamAndCheck(req); + } +} \ No newline at end of file diff --git a/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/ServiceListRequestParamExtractorTest.java b/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/ServiceListRequestParamExtractorTest.java new file mode 100644 index 00000000000..bc8a9d4b514 --- /dev/null +++ b/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/ServiceListRequestParamExtractorTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 1999-2023 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.core.paramcheck.impl; + +import com.alibaba.nacos.api.naming.remote.request.ServiceListRequest; +import com.alibaba.nacos.core.paramcheck.AbstractRpcParamExtractor; +import com.alibaba.nacos.core.paramcheck.RpcParamExtractorManager; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class ServiceListRequestParamExtractorTest { + + private static ServiceListRequest req; + + @BeforeClass + public static void initServiceListRequest() { + req = new ServiceListRequest(); + } + + /** + * Test extract param and check. + */ + @Test + public void testExtractParamAndCheck() throws Exception { + RpcParamExtractorManager paramExtractorManager = RpcParamExtractorManager.getInstance(); + AbstractRpcParamExtractor extractor = paramExtractorManager.getExtractor(req.getClass().getSimpleName()); + assertEquals(extractor.getClass().getSimpleName(), ServiceListRequestParamExtractor.class.getSimpleName()); + extractor.extractParamAndCheck(req); + } +} \ No newline at end of file diff --git a/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/ServiceQueryRequestParamExtractorTest.java b/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/ServiceQueryRequestParamExtractorTest.java new file mode 100644 index 00000000000..93ca14bdc5a --- /dev/null +++ b/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/ServiceQueryRequestParamExtractorTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 1999-2023 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.core.paramcheck.impl; + +import com.alibaba.nacos.api.naming.remote.request.ServiceQueryRequest; +import com.alibaba.nacos.core.paramcheck.AbstractRpcParamExtractor; +import com.alibaba.nacos.core.paramcheck.RpcParamExtractorManager; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class ServiceQueryRequestParamExtractorTest { + + private static ServiceQueryRequest req; + + @BeforeClass + public static void initServiceQueryRequest() { + req = new ServiceQueryRequest(); + } + + /** + * Test extract param and check. + */ + @Test + public void testExtractParamAndCheck() throws Exception { + RpcParamExtractorManager paramExtractorManager = RpcParamExtractorManager.getInstance(); + AbstractRpcParamExtractor extractor = paramExtractorManager.getExtractor(req.getClass().getSimpleName()); + assertEquals(extractor.getClass().getSimpleName(), ServiceQueryRequestParamExtractor.class.getSimpleName()); + extractor.extractParamAndCheck(req); + } +} \ No newline at end of file diff --git a/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/SubscribeServiceRequestParamExtractorTest.java b/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/SubscribeServiceRequestParamExtractorTest.java new file mode 100644 index 00000000000..22d08495e8c --- /dev/null +++ b/core/src/test/java/com/alibaba/nacos/core/paramcheck/impl/SubscribeServiceRequestParamExtractorTest.java @@ -0,0 +1,47 @@ +/* + * Copyright 1999-2023 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.core.paramcheck.impl; + +import com.alibaba.nacos.api.naming.remote.request.SubscribeServiceRequest; +import com.alibaba.nacos.core.paramcheck.AbstractRpcParamExtractor; +import com.alibaba.nacos.core.paramcheck.RpcParamExtractorManager; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class SubscribeServiceRequestParamExtractorTest { + + private static SubscribeServiceRequest req; + + @BeforeClass + public static void initSubscribeServiceRequest() { + req = new SubscribeServiceRequest(); + } + + /** + * Test extract param and check. + */ + @Test + public void testExtractParamAndCheck() throws Exception { + RpcParamExtractorManager paramExtractorManager = RpcParamExtractorManager.getInstance(); + AbstractRpcParamExtractor extractor = paramExtractorManager.getExtractor(req.getClass().getSimpleName()); + assertEquals(extractor.getClass().getSimpleName(), SubscribeServiceRequestParamExtractor.class.getSimpleName()); + extractor.extractParamAndCheck(req); + } + +} \ No newline at end of file diff --git a/test/naming-test/src/test/java/com/alibaba/nacos/test/naming/AbstractInstanceOperate_ITCase.java b/test/naming-test/src/test/java/com/alibaba/nacos/test/naming/AbstractInstanceOperate_ITCase.java index 2687a262391..1a539c68116 100644 --- a/test/naming-test/src/test/java/com/alibaba/nacos/test/naming/AbstractInstanceOperate_ITCase.java +++ b/test/naming-test/src/test/java/com/alibaba/nacos/test/naming/AbstractInstanceOperate_ITCase.java @@ -1,5 +1,5 @@ /* - * Copyright 1999-2020 Alibaba Group Holding Ltd. + * Copyright 1999-2023 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. @@ -349,9 +349,9 @@ public void regServiceWithTTL() throws Exception { */ @Test public void registerEphemeralInstanceWithInvalidClusterName() throws Exception { - expectedException.expect(NacosException.class); + expectedException.expect(Exception.class); expectedException.expectMessage( - "Instance 'clusterName' should be characters with only 0-9a-zA-Z-. (current: cluster1,cluster2)"); + "Param 'cluster' is illegal, Chinese characters and ',' should not appear in the param"); String serviceName = NamingBase.randomDomainName(); Instance instance = new Instance();