Skip to content

Commit

Permalink
[ISSUE #10734] Implement grpc server interceptor and grpc param extra…
Browse files Browse the repository at this point in the history
…ctors (#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
  • Loading branch information
Sunrisea authored Jul 10, 2023
1 parent e7fbbc1 commit cae21e2
Show file tree
Hide file tree
Showing 20 changed files with 829 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Instance> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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<ConfigBatchListenRequest.ConfigListenContext> 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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
}
Loading

0 comments on commit cae21e2

Please sign in to comment.