diff --git a/config/src/main/java/com/alibaba/nacos/config/server/controller/ClientMetricsController.java b/config/src/main/java/com/alibaba/nacos/config/server/controller/ClientMetricsController.java index 08f4e945ef7..24f425a5a34 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/controller/ClientMetricsController.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/controller/ClientMetricsController.java @@ -18,6 +18,7 @@ import com.alibaba.nacos.api.config.remote.request.ClientConfigMetricRequest; import com.alibaba.nacos.api.config.remote.response.ClientConfigMetricResponse; +import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.auth.annotation.Secured; import com.alibaba.nacos.common.http.Callback; import com.alibaba.nacos.common.http.HttpClientBeanHolder; @@ -30,6 +31,7 @@ import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.paramcheck.ConfigDefaultHttpParamExtractor; import com.alibaba.nacos.config.server.utils.GroupKey2; +import com.alibaba.nacos.config.server.utils.ParamUtils; import com.alibaba.nacos.core.cluster.Member; import com.alibaba.nacos.core.cluster.ServerMemberManager; import com.alibaba.nacos.core.paramcheck.ExtractorManager; @@ -86,7 +88,11 @@ public ClientMetricsController(ServerMemberManager serverMemberManager, Connecti public ResponseEntity metric(@RequestParam("ip") String ip, @RequestParam(value = "dataId", required = false) String dataId, @RequestParam(value = "group", required = false) String group, - @RequestParam(value = "tenant", required = false) String tenant) { + @RequestParam(value = "tenant", required = false) String tenant) throws NacosException { + + ParamUtils.checkTenant(tenant); + ParamUtils.checkParam(dataId, group, "default", "default"); + Loggers.CORE.info("Get cluster config metrics received, ip={},dataId={},group={},tenant={}", ip, dataId, group, tenant); Map responseMap = new HashMap<>(3); @@ -168,7 +174,11 @@ public void onCancel() { public Map getClientMetrics(@RequestParam("ip") String ip, @RequestParam(value = "dataId", required = false) String dataId, @RequestParam(value = "group", required = false) String group, - @RequestParam(value = "tenant", required = false) String tenant) { + @RequestParam(value = "tenant", required = false) String tenant) throws NacosException { + + ParamUtils.checkTenant(tenant); + ParamUtils.checkParam(dataId, group, "default", "default"); + Map metrics = new HashMap<>(16); List connectionsByIp = connectionManager.getConnectionByIp(ip); for (Connection connectionByIp : connectionsByIp) { diff --git a/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigChangeClusterSyncRequestHandler.java b/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigChangeClusterSyncRequestHandler.java index a806ee25621..f354cd759b6 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigChangeClusterSyncRequestHandler.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/remote/ConfigChangeClusterSyncRequestHandler.java @@ -19,6 +19,7 @@ import com.alibaba.nacos.api.config.remote.request.cluster.ConfigChangeClusterSyncRequest; import com.alibaba.nacos.api.config.remote.response.cluster.ConfigChangeClusterSyncResponse; import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.remote.RemoteConstants; import com.alibaba.nacos.api.remote.request.RequestMeta; import com.alibaba.nacos.config.server.service.dump.DumpRequest; import com.alibaba.nacos.config.server.service.dump.DumpService; @@ -27,6 +28,7 @@ import com.alibaba.nacos.core.paramcheck.impl.ConfigRequestParamExtractor; import com.alibaba.nacos.core.remote.RequestHandler; import com.alibaba.nacos.core.control.TpsControl; +import com.alibaba.nacos.core.remote.grpc.InvokeSource; import org.springframework.stereotype.Component; /** @@ -36,6 +38,7 @@ * @version $Id: ConfigChangeClusterSyncRequestHandler.java, v 0.1 2020年08月11日 4:35 PM liuzunfei Exp $ */ @Component +@InvokeSource(source = {RemoteConstants.LABEL_SOURCE_CLUSTER}) public class ConfigChangeClusterSyncRequestHandler extends RequestHandler { diff --git a/core/src/main/java/com/alibaba/nacos/core/cluster/remote/MemberReportHandler.java b/core/src/main/java/com/alibaba/nacos/core/cluster/remote/MemberReportHandler.java index 8a78cb380de..a2e9372c568 100644 --- a/core/src/main/java/com/alibaba/nacos/core/cluster/remote/MemberReportHandler.java +++ b/core/src/main/java/com/alibaba/nacos/core/cluster/remote/MemberReportHandler.java @@ -19,6 +19,7 @@ package com.alibaba.nacos.core.cluster.remote; import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.remote.RemoteConstants; import com.alibaba.nacos.api.remote.request.RequestMeta; import com.alibaba.nacos.common.utils.LoggerUtils; import com.alibaba.nacos.core.cluster.Member; @@ -27,6 +28,7 @@ import com.alibaba.nacos.core.cluster.remote.request.MemberReportRequest; import com.alibaba.nacos.core.cluster.remote.response.MemberReportResponse; import com.alibaba.nacos.core.remote.RequestHandler; +import com.alibaba.nacos.core.remote.grpc.InvokeSource; import com.alibaba.nacos.core.utils.Loggers; import org.springframework.stereotype.Component; @@ -36,6 +38,7 @@ * @author : huangtianhui */ @Component +@InvokeSource(source = {RemoteConstants.LABEL_SOURCE_CLUSTER}) public class MemberReportHandler extends RequestHandler { private final ServerMemberManager memberManager; diff --git a/core/src/main/java/com/alibaba/nacos/core/remote/RequestHandlerRegistry.java b/core/src/main/java/com/alibaba/nacos/core/remote/RequestHandlerRegistry.java index 04d90ec7eaf..77fc6e51776 100644 --- a/core/src/main/java/com/alibaba/nacos/core/remote/RequestHandlerRegistry.java +++ b/core/src/main/java/com/alibaba/nacos/core/remote/RequestHandlerRegistry.java @@ -20,7 +20,9 @@ import com.alibaba.nacos.api.remote.request.RequestMeta; import com.alibaba.nacos.core.control.TpsControl; import com.alibaba.nacos.core.control.TpsControlConfig; +import com.alibaba.nacos.core.remote.grpc.InvokeSource; import com.alibaba.nacos.plugin.control.ControlManagerCenter; +import com.google.common.collect.Sets; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Service; @@ -30,6 +32,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.Set; /** * RequestHandlerRegistry. @@ -43,6 +46,8 @@ public class RequestHandlerRegistry implements ApplicationListener registryHandlers = new HashMap<>(); + Map> sourceRegistry = new HashMap<>(); + /** * Get Request Handler By request Type. * @@ -53,6 +58,20 @@ public RequestHandler getByRequestType(String requestType) { return registryHandlers.get(requestType); } + /** + * check source invoke allowed. + * + * @param type type. + * @param source source. + * @return + */ + public boolean checkSourceInvokeAllowed(String type, String source) { + if (sourceRegistry.containsKey(type) && !sourceRegistry.get(type).contains(source)) { + return false; + } + return true; + } + @Override public void onApplicationEvent(ContextRefreshedEvent event) { Map beansOfType = event.getApplicationContext().getBeansOfType(RequestHandler.class); @@ -71,7 +90,7 @@ public void onApplicationEvent(ContextRefreshedEvent event) { if (skip) { continue; } - + //register tps control. try { Method method = clazz.getMethod("handle", Request.class, RequestMeta.class); if (method.isAnnotationPresent(TpsControl.class) && TpsControlConfig.isTpsControlEnabled()) { @@ -82,7 +101,22 @@ public void onApplicationEvent(ContextRefreshedEvent event) { } catch (Exception e) { //ignore. } + Class tClass = (Class) ((ParameterizedType) clazz.getGenericSuperclass()).getActualTypeArguments()[0]; + + //register invoke source. + try { + if (clazz.isAnnotationPresent(InvokeSource.class)) { + InvokeSource tpsControl = clazz.getAnnotation(InvokeSource.class); + String[] sources = tpsControl.source(); + if (sources != null && sources.length > 0) { + sourceRegistry.put(tClass.getSimpleName(), Sets.newHashSet(sources)); + } + } + } catch (Exception e) { + //ignore. + } + registryHandlers.putIfAbsent(tClass.getSimpleName(), requestHandler); } } diff --git a/core/src/main/java/com/alibaba/nacos/core/remote/core/ServerLoaderInfoRequestHandler.java b/core/src/main/java/com/alibaba/nacos/core/remote/core/ServerLoaderInfoRequestHandler.java index 84b12d31662..0cf208e0384 100644 --- a/core/src/main/java/com/alibaba/nacos/core/remote/core/ServerLoaderInfoRequestHandler.java +++ b/core/src/main/java/com/alibaba/nacos/core/remote/core/ServerLoaderInfoRequestHandler.java @@ -23,6 +23,7 @@ import com.alibaba.nacos.api.remote.response.ServerLoaderInfoResponse; import com.alibaba.nacos.core.remote.ConnectionManager; import com.alibaba.nacos.core.remote.RequestHandler; +import com.alibaba.nacos.core.remote.grpc.InvokeSource; import com.alibaba.nacos.sys.env.EnvUtil; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -37,6 +38,7 @@ * @version $Id: ServerLoaderInfoRequestHandler.java, v 0.1 2020年09月03日 2:51 PM liuzunfei Exp $ */ @Component +@InvokeSource(source = {RemoteConstants.LABEL_SOURCE_CLUSTER}) public class ServerLoaderInfoRequestHandler extends RequestHandler { @Autowired diff --git a/core/src/main/java/com/alibaba/nacos/core/remote/core/ServerReloaderRequestHandler.java b/core/src/main/java/com/alibaba/nacos/core/remote/core/ServerReloaderRequestHandler.java index a1c62567e4d..a0a95536c5a 100644 --- a/core/src/main/java/com/alibaba/nacos/core/remote/core/ServerReloaderRequestHandler.java +++ b/core/src/main/java/com/alibaba/nacos/core/remote/core/ServerReloaderRequestHandler.java @@ -23,6 +23,7 @@ import com.alibaba.nacos.api.remote.response.ServerReloadResponse; import com.alibaba.nacos.core.remote.ConnectionManager; import com.alibaba.nacos.core.remote.RequestHandler; +import com.alibaba.nacos.core.remote.grpc.InvokeSource; import com.alibaba.nacos.core.utils.Loggers; import com.alibaba.nacos.core.utils.RemoteUtils; import org.springframework.beans.factory.annotation.Autowired; @@ -37,6 +38,7 @@ * @author liuzunfei * @version $Id: ServerReloaderRequestHandler.java, v 0.1 2020年11月09日 4:38 PM liuzunfei Exp $ */ +@InvokeSource(source = {RemoteConstants.LABEL_SOURCE_CLUSTER}) @Component public class ServerReloaderRequestHandler extends RequestHandler { 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 837924191ea..7f5761f8147 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 @@ -16,10 +16,15 @@ package com.alibaba.nacos.core.remote.grpc; +import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.grpc.auto.Payload; +import com.alibaba.nacos.api.remote.response.ErrorResponse; import com.alibaba.nacos.common.remote.ConnectionType; +import com.alibaba.nacos.common.remote.client.grpc.GrpcUtils; +import com.alibaba.nacos.core.monitor.MetricsMonitor; import com.alibaba.nacos.core.remote.BaseRpcServer; import com.alibaba.nacos.core.remote.ConnectionManager; +import com.alibaba.nacos.core.remote.RequestHandlerRegistry; import com.alibaba.nacos.core.remote.grpc.negotiator.NacosGrpcProtocolNegotiator; import com.alibaba.nacos.core.utils.Loggers; import com.alibaba.nacos.sys.env.EnvUtil; @@ -36,6 +41,7 @@ import io.grpc.netty.shaded.io.grpc.netty.NettyServerBuilder; import io.grpc.protobuf.ProtoUtils; import io.grpc.stub.ServerCalls; +import io.grpc.stub.StreamObserver; import io.grpc.util.MutableHandlerRegistry; import org.springframework.beans.factory.annotation.Autowired; @@ -70,6 +76,9 @@ public abstract class BaseGrpcServer extends BaseRpcServer { @Autowired private ConnectionManager connectionManager; + @Autowired + private RequestHandlerRegistry requestHandlerRegistry; + @Override public ConnectionType getConnectionType() { return ConnectionType.GRPC; @@ -163,6 +172,32 @@ protected List getServerTransportFilters() { return Collections.singletonList(new AddressTransportFilter(connectionManager)); } + /** + * get source for the request. + * + * @return + */ + protected abstract String getSource(); + + private boolean invokeSourceAllowCheck(Payload grpcRequest) { + return requestHandlerRegistry.checkSourceInvokeAllowed(grpcRequest.getMetadata().getType(), getSource()); + } + + protected void handleCommonRequest(Payload grpcRequest, StreamObserver responseObserver) { + if (!invokeSourceAllowCheck(grpcRequest)) { + Payload payloadResponse = GrpcUtils.convert(ErrorResponse.build(NacosException.BAD_GATEWAY, + String.format(" invoke %s from %s is forbidden", grpcRequest.getMetadata().getType(), + this.getSource()))); + responseObserver.onNext(payloadResponse); + + responseObserver.onCompleted(); + MetricsMonitor.recordGrpcRequestEvent(grpcRequest.getMetadata().getType(), false, + NacosException.BAD_GATEWAY, null, null, 0); + } else { + grpcCommonRequestAcceptor.request(grpcRequest, responseObserver); + } + } + private void addServices(MutableHandlerRegistry handlerRegistry, ServerInterceptor... serverInterceptor) { // unary common call register. @@ -174,7 +209,9 @@ private void addServices(MutableHandlerRegistry handlerRegistry, ServerIntercept .setResponseMarshaller(ProtoUtils.marshaller(Payload.getDefaultInstance())).build(); final ServerCallHandler payloadHandler = ServerCalls.asyncUnaryCall( - (request, responseObserver) -> grpcCommonRequestAcceptor.request(request, responseObserver)); + (request, responseObserver) -> { + handleCommonRequest(request, responseObserver); + }); final ServerServiceDefinition serviceDefOfUnaryPayload = ServerServiceDefinition.builder( GrpcServerConstants.REQUEST_SERVICE_NAME).addMethod(unaryPayloadMethod, payloadHandler).build(); diff --git a/core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcClusterServer.java b/core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcClusterServer.java index df50375a61a..9988eabc8d9 100644 --- a/core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcClusterServer.java +++ b/core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcClusterServer.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.core.remote.grpc; import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.api.remote.RemoteConstants; import com.alibaba.nacos.core.remote.grpc.filter.NacosGrpcServerTransportFilter; import com.alibaba.nacos.core.remote.grpc.filter.NacosGrpcServerTransportFilterServiceLoader; import com.alibaba.nacos.core.remote.grpc.interceptor.NacosGrpcServerInterceptor; @@ -126,4 +127,9 @@ protected List getServerTransportFilters() { NacosGrpcServerTransportFilter.CLUSTER_FILTER)); return result; } + + @Override + protected String getSource() { + return RemoteConstants.LABEL_SOURCE_CLUSTER; + } } diff --git a/core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcSdkServer.java b/core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcSdkServer.java index b99364e4c87..22e3fbc009a 100644 --- a/core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcSdkServer.java +++ b/core/src/main/java/com/alibaba/nacos/core/remote/grpc/GrpcSdkServer.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.core.remote.grpc; import com.alibaba.nacos.api.common.Constants; +import com.alibaba.nacos.api.remote.RemoteConstants; import com.alibaba.nacos.core.remote.grpc.filter.NacosGrpcServerTransportFilter; import com.alibaba.nacos.core.remote.grpc.filter.NacosGrpcServerTransportFilterServiceLoader; import com.alibaba.nacos.core.remote.grpc.interceptor.NacosGrpcServerInterceptor; @@ -125,4 +126,8 @@ protected List getServerTransportFilters() { return result; } + @Override + protected String getSource() { + return RemoteConstants.LABEL_SOURCE_SDK; + } } diff --git a/core/src/main/java/com/alibaba/nacos/core/remote/grpc/InvokeSource.java b/core/src/main/java/com/alibaba/nacos/core/remote/grpc/InvokeSource.java new file mode 100644 index 00000000000..f6c50cb966e --- /dev/null +++ b/core/src/main/java/com/alibaba/nacos/core/remote/grpc/InvokeSource.java @@ -0,0 +1,34 @@ +/* + * Copyright 1999-2020 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 java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +/** + * annotation for invoke source. + */ +@Retention(RetentionPolicy.RUNTIME) +public @interface InvokeSource { + + + /** + * allowed sources. + * @return + */ + String[] source(); +} diff --git a/core/src/test/java/com/alibaba/nacos/core/remote/RequestHandlerRegistryTest.java b/core/src/test/java/com/alibaba/nacos/core/remote/RequestHandlerRegistryTest.java index 8ce0a734d05..78b8aa0f0c9 100644 --- a/core/src/test/java/com/alibaba/nacos/core/remote/RequestHandlerRegistryTest.java +++ b/core/src/test/java/com/alibaba/nacos/core/remote/RequestHandlerRegistryTest.java @@ -17,12 +17,19 @@ package com.alibaba.nacos.core.remote; +import com.alibaba.nacos.api.remote.RemoteConstants; import com.alibaba.nacos.api.remote.request.HealthCheckRequest; +import com.alibaba.nacos.api.remote.request.ServerReloadRequest; +import com.alibaba.nacos.core.remote.core.ServerReloaderRequestHandler; +import com.alibaba.nacos.plugin.control.ControlManagerCenter; +import com.alibaba.nacos.plugin.control.tps.TpsControlManager; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; import org.mockito.Mock; +import org.mockito.MockedStatic; import org.mockito.Mockito; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.context.annotation.AnnotationConfigApplicationContext; @@ -31,7 +38,9 @@ import java.util.HashMap; import java.util.Map; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.when; /** * {@link RequestHandlerRegistry} unit test. @@ -51,17 +60,51 @@ class RequestHandlerRegistryTest { @Mock private AnnotationConfigApplicationContext applicationContext; + MockedStatic controlManagerCenterMockedStatic; + + @Mock + private ControlManagerCenter controlManagerCenter; + + @Mock + private TpsControlManager tpsControlManager; + @BeforeEach void setUp() { + controlManagerCenterMockedStatic = Mockito.mockStatic(ControlManagerCenter.class); + controlManagerCenterMockedStatic.when(() -> ControlManagerCenter.getInstance()) + .thenReturn(controlManagerCenter); + when(controlManagerCenter.getTpsControlManager()).thenReturn(tpsControlManager); + Map handlerMap = new HashMap<>(); handlerMap.put(HealthCheckRequestHandler.class.getSimpleName(), new HealthCheckRequestHandler()); Mockito.when(applicationContext.getBeansOfType(Mockito.any())).thenReturn(handlerMap); registry.onApplicationEvent(contextRefreshedEvent); + + } + + @AfterEach + public void after() { + controlManagerCenterMockedStatic.close(); } @Test void testGetByRequestType() { assertNotNull(registry.getByRequestType(HealthCheckRequest.class.getSimpleName())); } + + @Test + public void testSourceInvokeAllowed() { + Map handlerMap = new HashMap<>(); + handlerMap.put(ServerReloadRequest.class.getSimpleName(), new ServerReloaderRequestHandler()); + Mockito.when(applicationContext.getBeansOfType(Mockito.any())).thenReturn(handlerMap); + + registry.onApplicationEvent(contextRefreshedEvent); + assertNotNull(registry.sourceRegistry.get(ServerReloadRequest.class.getSimpleName()) + .contains(RemoteConstants.LABEL_SOURCE_CLUSTER)); + + assertFalse(registry.checkSourceInvokeAllowed(ServerReloadRequest.class.getSimpleName(), + RemoteConstants.LABEL_SOURCE_SDK)); + + } } diff --git a/core/src/test/java/com/alibaba/nacos/core/remote/grpc/GrpcServerTest.java b/core/src/test/java/com/alibaba/nacos/core/remote/grpc/GrpcServerTest.java index b4a9eb49191..51cdf9326c4 100644 --- a/core/src/test/java/com/alibaba/nacos/core/remote/grpc/GrpcServerTest.java +++ b/core/src/test/java/com/alibaba/nacos/core/remote/grpc/GrpcServerTest.java @@ -17,9 +17,14 @@ package com.alibaba.nacos.core.remote.grpc; +import com.alibaba.nacos.api.config.remote.request.cluster.ConfigChangeClusterSyncRequest; +import com.alibaba.nacos.api.grpc.auto.Payload; import com.alibaba.nacos.common.remote.ConnectionType; +import com.alibaba.nacos.common.remote.client.grpc.GrpcUtils; +import com.alibaba.nacos.core.remote.RequestHandlerRegistry; import com.alibaba.nacos.sys.env.EnvUtil; import com.alibaba.nacos.sys.utils.ApplicationUtils; +import io.grpc.stub.StreamObserver; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; @@ -31,8 +36,12 @@ import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; import org.springframework.mock.env.MockEnvironment; +import org.springframework.util.ReflectionUtils; + +import java.lang.reflect.Field; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.eq; /** * {@link GrpcSdkServer} and {@link GrpcClusterServer} unit test. @@ -82,4 +91,31 @@ void testGrpcClusterServer() throws Exception { assertEquals(1001, grpcSdkServer.rpcPortOffset()); grpcSdkServer.stopServer(); } + + @Test + public void testGrpcSdkServerSourceCheck() throws Exception { + grpcSdkServer = new GrpcSdkServer(); + RequestHandlerRegistry mock = Mockito.mock(RequestHandlerRegistry.class); + Field field = ReflectionUtils.findField(GrpcSdkServer.class, "requestHandlerRegistry"); + field.setAccessible(true); + ReflectionUtils.setField(field, grpcSdkServer, mock); + GrpcRequestAcceptor mockAcceptor = Mockito.mock(GrpcRequestAcceptor.class); + Field fieldGrpcAcceptor = ReflectionUtils.findField(GrpcSdkServer.class, "grpcCommonRequestAcceptor"); + fieldGrpcAcceptor.setAccessible(true); + ReflectionUtils.setField(fieldGrpcAcceptor, grpcSdkServer, mockAcceptor); + + StreamObserver streamObserverMock = Mockito.mock(StreamObserver.class); + Payload convert = GrpcUtils.convert(new ConfigChangeClusterSyncRequest()); + //verify not allowed + Mockito.when(mock.checkSourceInvokeAllowed(ConfigChangeClusterSyncRequest.class.getSimpleName(), + grpcSdkServer.getSource())).thenReturn(false); + grpcSdkServer.handleCommonRequest(convert, streamObserverMock); + Mockito.verify(streamObserverMock, Mockito.times(1)).onCompleted(); + //verify allowed + Mockito.when(mock.checkSourceInvokeAllowed(ConfigChangeClusterSyncRequest.class.getSimpleName(), + grpcSdkServer.getSource())).thenReturn(true); + grpcSdkServer.handleCommonRequest(convert, streamObserverMock); + Mockito.verify(mockAcceptor, Mockito.times(1)).request(eq(convert), eq(streamObserverMock)); + + } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/remote/rpc/handler/DistroDataRequestHandler.java b/naming/src/main/java/com/alibaba/nacos/naming/remote/rpc/handler/DistroDataRequestHandler.java index 35836ee6940..765b06b8049 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/remote/rpc/handler/DistroDataRequestHandler.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/remote/rpc/handler/DistroDataRequestHandler.java @@ -17,12 +17,14 @@ package com.alibaba.nacos.naming.remote.rpc.handler; import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.remote.RemoteConstants; import com.alibaba.nacos.api.remote.request.RequestMeta; import com.alibaba.nacos.api.remote.response.ResponseCode; import com.alibaba.nacos.core.distributed.distro.DistroProtocol; import com.alibaba.nacos.core.distributed.distro.entity.DistroData; import com.alibaba.nacos.core.distributed.distro.entity.DistroKey; import com.alibaba.nacos.core.remote.RequestHandler; +import com.alibaba.nacos.core.remote.grpc.InvokeSource; import com.alibaba.nacos.naming.cluster.remote.request.DistroDataRequest; import com.alibaba.nacos.naming.cluster.remote.response.DistroDataResponse; import com.alibaba.nacos.naming.consistency.ephemeral.distro.v2.DistroClientDataProcessor; @@ -34,6 +36,7 @@ * * @author xiweng.yy */ +@InvokeSource(source = {RemoteConstants.LABEL_SOURCE_CLUSTER}) @Component public class DistroDataRequestHandler extends RequestHandler {