Skip to content

Commit

Permalink
[type:fix] fix client register validation (apache#5764)
Browse files Browse the repository at this point in the history
* [type:fix] fix client register validation

* [type:fix] fix client register validation

* [type:fix] fix client register validation

* [type:fix] fix client register validation

* [type:fix] fix client register validation

* [type:fix] fix client register validation

* [type:fix] fix client register validation

* [type:fix] fix sql error

---------

Co-authored-by: liuhy <[email protected]>
  • Loading branch information
moremind and Aias00 authored Nov 8, 2024
1 parent b19af23 commit ab87238
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public abstract class AbstractContextPathRegisterService extends AbstractShenyuC

@Override
public void registerContextPath(final MetaDataRegisterDTO dto) {
this.checkNamespacePluginRel(dto.getNamespaceId(), PluginEnum.CONTEXT_PATH.getName());
String name = PluginEnum.CONTEXT_PATH.getName();
String contextPath = PathUtils.decoratorContextPath(dto.getContextPath());
String key = LOCK_KEY_PREFIX + contextPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.admin.listener.DataChangedEvent;
import org.apache.shenyu.admin.mapper.NamespacePluginRelMapper;
import org.apache.shenyu.admin.mapper.PluginMapper;
import org.apache.shenyu.admin.model.dto.DiscoveryUpstreamDTO;
import org.apache.shenyu.admin.model.dto.RuleConditionDTO;
import org.apache.shenyu.admin.model.dto.RuleDTO;
import org.apache.shenyu.admin.model.entity.PluginDO;
import org.apache.shenyu.admin.model.entity.SelectorDO;
import org.apache.shenyu.admin.model.vo.NamespacePluginVO;
import org.apache.shenyu.admin.service.DiscoveryService;
import org.apache.shenyu.admin.service.DiscoveryUpstreamService;
import org.apache.shenyu.admin.service.MetaDataService;
Expand Down Expand Up @@ -101,6 +105,12 @@ public abstract class AbstractShenyuClientRegisterServiceImpl extends FallbackSh
@Resource
private DiscoveryUpstreamService discoveryUpstreamService;

@Resource
private PluginMapper pluginMapper;

@Resource
private NamespacePluginRelMapper namespacePluginRelMapper;

/**
* Selector handler string.
*
Expand Down Expand Up @@ -141,6 +151,8 @@ public abstract class AbstractShenyuClientRegisterServiceImpl extends FallbackSh
@Override
public String register(final MetaDataRegisterDTO dto) {
String namespaceId = StringUtils.defaultIfEmpty(dto.getNamespaceId(), SYS_DEFAULT_NAMESPACE_ID);
String pluginName = PluginNameAdapter.rpcTypeAdapter(rpcType());
this.checkNamespacePluginRel(namespaceId, pluginName);
dto.setNamespaceId(namespaceId);
//handler plugin selector
String selectorHandler = selectorHandler(dto);
Expand Down Expand Up @@ -183,10 +195,13 @@ public String doRegisterURI(final String selectorName, final List<URIRegisterDTO
if (Objects.isNull(selectorDO)) {
throw new ShenyuException("doRegister Failed to execute, wait to retry.");
}
this.checkNamespacePluginRel(namespaceId, pluginName);
// fetch UPSTREAM_MAP data from db
//upstreamCheckService.fetchUpstreamData();
//update upstream
List<URIRegisterDTO> validUriList = uriList.stream().filter(dto -> Objects.nonNull(dto.getPort()) && StringUtils.isNotBlank(dto.getHost())).collect(Collectors.toList());
List<URIRegisterDTO> validUriList = uriList.stream()
.filter(dto -> Objects.nonNull(dto.getPort()) && StringUtils.isNotBlank(dto.getHost()))
.collect(Collectors.toList());
String handler = buildHandle(validUriList, selectorDO);
if (handler != null) {
selectorDO.setHandle(handler);
Expand All @@ -199,6 +214,16 @@ public String doRegisterURI(final String selectorName, final List<URIRegisterDTO
}
return ShenyuResultMessage.SUCCESS;
}

@Override
public void checkNamespacePluginRel(final String namespaceId, final String pluginName) {
PluginDO pluginDO = pluginMapper.selectByName(pluginName);
NamespacePluginVO namespacePluginRelation = namespacePluginRelMapper.selectByPluginIdAndNamespaceId(pluginDO.getId(), namespaceId);
if (Objects.isNull(namespacePluginRelation)) {
String errorMsg = String.format("%s plugin not enabled for current namespace or plugin not exist for namespaceId: %s", pluginName, namespaceId);
throw new IllegalArgumentException(errorMsg);
}
}

@Override
public String doHeartbeat(final String selectorName, final List<URIRegisterDTO> uriList, final String namespaceId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ public interface ShenyuClientRegisterService {
* @return String
*/
String registerApiDoc(ApiDocRegisterDTO apiDocRegisterDTO);

/**
* check whether the namespace and plugin relation exists.
*
* @param namespaceId namespaceId
* @param pluginName plugin name
*/
void checkNamespacePluginRel(String namespaceId, String pluginName);

/**
* Register uri string.
Expand Down
2 changes: 1 addition & 1 deletion shenyu-admin/src/main/resources/sql-script/h2/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ CREATE TABLE IF NOT EXISTS `namespace_user_rel`
`namespace_id` varchar(50) NOT NULL COMMENT 'namespace_id',
`user_id` varchar(128) NOT NULL COMMENT 'user_id',
`date_created` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'date_created',
`date_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'date_updated'
`date_updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'date_updated',
PRIMARY KEY (`id`)
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@

package org.apache.shenyu.admin.service.register;

import org.apache.shenyu.admin.mapper.NamespacePluginRelMapper;
import org.apache.shenyu.admin.mapper.PluginMapper;
import org.apache.shenyu.admin.model.entity.PluginDO;
import org.apache.shenyu.admin.model.entity.SelectorDO;
import org.apache.shenyu.admin.model.vo.NamespacePluginVO;
import org.apache.shenyu.admin.service.impl.RuleServiceImpl;
import org.apache.shenyu.admin.service.impl.SelectorServiceImpl;
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.enums.PluginEnum;
import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
import org.apache.shenyu.register.common.dto.URIRegisterDTO;
Expand Down Expand Up @@ -60,17 +65,31 @@ class AbstractContextPathRegisterServiceTest {
@Mock
private LockRegistry registry;

@Mock
private NamespacePluginRelMapper namespacePluginRelMapper;

@Mock
private PluginMapper pluginMapper;

@Test
public void testRegisterContextPath() {
MetaDataRegisterDTO dto = MetaDataRegisterDTO.builder().build();
dto.setContextPath("Context_Path");
dto.setAddPrefixed(true);
dto.setNamespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID);
when(selectorService.registerDefault(dto, PluginEnum.CONTEXT_PATH.getName(), "")).thenReturn("Context_Path_Selector_Id");
// org.springframework.integration.jdbc.lock.JdbcLockRegistry.JdbcLock is private and cannot be mocked directly so we mock the LockRegistry and return a mock MockLock
// here mock ReentrantLock cause cpu usage 100% in the jdk 19 20 21 environment
when(registry.obtain(any())).thenReturn(mock(MockLock.class));
when(ruleService.findBySelectorIdAndName("Context_Path_Selector_Id", "Context_Path")).thenReturn(null);
when(ruleService.registerDefault(any())).thenReturn("Context_Path_Rule_Id");
PluginDO pluginDO = new PluginDO();
pluginDO.setId("1");
pluginDO.setName(PluginEnum.CONTEXT_PATH.getName());
when(pluginMapper.selectByName(PluginEnum.CONTEXT_PATH.getName())).thenReturn(pluginDO);
NamespacePluginVO namespacePluginVO = new NamespacePluginVO();
namespacePluginVO.setPluginId("1");
when(namespacePluginRelMapper.selectByPluginIdAndNamespaceId(pluginDO.getId(), Constants.SYS_DEFAULT_NAMESPACE_ID)).thenReturn(namespacePluginVO);
abstractContextPathRegisterService.registerContextPath(dto);
verify(ruleService).registerDefault(any());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
package org.apache.shenyu.admin.service.register;

import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.admin.mapper.NamespacePluginRelMapper;
import org.apache.shenyu.admin.mapper.PluginMapper;
import org.apache.shenyu.admin.model.dto.RuleDTO;
import org.apache.shenyu.admin.model.entity.PluginDO;
import org.apache.shenyu.admin.model.entity.SelectorDO;
import org.apache.shenyu.admin.model.vo.NamespacePluginVO;
import org.apache.shenyu.admin.model.vo.TagVO;
import org.apache.shenyu.admin.service.SelectorService;
import org.apache.shenyu.admin.service.RuleService;
Expand All @@ -29,9 +33,11 @@
import org.apache.shenyu.admin.service.impl.UpstreamCheckService;
import org.apache.shenyu.admin.service.manager.RegisterApiDocService;
import org.apache.shenyu.admin.utils.ShenyuResultMessage;
import org.apache.shenyu.common.constant.Constants;
import org.apache.shenyu.common.dto.DiscoverySyncData;
import org.apache.shenyu.common.dto.SelectorData;
import org.apache.shenyu.common.dto.convert.selector.CommonUpstream;
import org.apache.shenyu.common.enums.PluginEnum;
import org.apache.shenyu.common.exception.ShenyuException;
import org.apache.shenyu.register.common.dto.ApiDocRegisterDTO;
import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
Expand All @@ -42,6 +48,8 @@
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import org.springframework.context.ApplicationEventPublisher;

import java.util.ArrayList;
Expand All @@ -60,6 +68,7 @@
* Test cases for AbstractShenyuClientRegisterServiceImpl.
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
public final class AbstractShenyuClientRegisterServiceImplTest {

@InjectMocks
Expand Down Expand Up @@ -89,11 +98,26 @@ public final class AbstractShenyuClientRegisterServiceImplTest {
@Mock
private RegisterApiDocService registerApiDocService;

@Mock
private NamespacePluginRelMapper namespacePluginRelMapper;

@Mock
private PluginMapper pluginMapper;

@Test
public void testRegister() {
MetaDataRegisterDTO dto = MetaDataRegisterDTO.builder().build();
dto.setContextPath("Context_Path");
dto.setPath("Path");
dto.setNamespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID);
PluginDO pluginDO = new PluginDO();
pluginDO.setId("1");
pluginDO.setName(PluginEnum.GRPC.getName());
when(pluginMapper.selectByName(PluginEnum.GRPC.getName())).thenReturn(pluginDO);
NamespacePluginVO namespacePluginVO = new NamespacePluginVO();
namespacePluginVO.setPluginId("1");
namespacePluginVO.setNamespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID);
when(namespacePluginRelMapper.selectByPluginIdAndNamespaceId(pluginDO.getId(), Constants.SYS_DEFAULT_NAMESPACE_ID)).thenReturn(namespacePluginVO);
when(selectorService.registerDefault(any(), any(), any())).thenReturn("SELECTOR_ID");
assertEquals(ShenyuResultMessage.SUCCESS, abstractShenyuClientRegisterService.register(dto));

Expand Down Expand Up @@ -137,6 +161,14 @@ public void testRegisterApiDoc() {

@Test
public void testDoRegisterURI() {
PluginDO pluginDO = new PluginDO();
pluginDO.setId("1");
pluginDO.setName(PluginEnum.GRPC.getName());
when(pluginMapper.selectByName(PluginEnum.GRPC.getName())).thenReturn(pluginDO);
NamespacePluginVO namespacePluginVO = new NamespacePluginVO();
namespacePluginVO.setPluginId("1");
namespacePluginVO.setNamespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID);
when(namespacePluginRelMapper.selectByPluginIdAndNamespaceId(pluginDO.getId(), Constants.SYS_DEFAULT_NAMESPACE_ID)).thenReturn(namespacePluginVO);
assertEquals(StringUtils.EMPTY, abstractShenyuClientRegisterService.doRegisterURI("Selector_Name", new ArrayList<>(), SYS_DEFAULT_NAMESPACE_ID));

URIRegisterDTO uriRegisterDTO = URIRegisterDTO.builder().build();
Expand All @@ -148,6 +180,14 @@ public void testDoRegisterURI() {
when(selectorService.findByNameAndPluginNameAndNamespaceId(any(), any(), any())).thenReturn(selectorDO);
SelectorData selectorData = new SelectorData();
when(selectorService.buildByNameAndPluginNameAndNamespaceId(any(), any(), any())).thenReturn(selectorData);
PluginDO plugin = new PluginDO();
plugin.setId("1");
plugin.setName(PluginEnum.GRPC.getName());
when(pluginMapper.selectByName(PluginEnum.GRPC.getName())).thenReturn(plugin);
NamespacePluginVO namespacePlugin = new NamespacePluginVO();
namespacePlugin.setPluginId("1");
namespacePlugin.setNamespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID);
when(namespacePluginRelMapper.selectByPluginIdAndNamespaceId(pluginDO.getId(), Constants.SYS_DEFAULT_NAMESPACE_ID)).thenReturn(namespacePlugin);
assertEquals(ShenyuResultMessage.SUCCESS, abstractShenyuClientRegisterService.doRegisterURI("Selector_Name", Collections.singletonList(uriRegisterDTO), SYS_DEFAULT_NAMESPACE_ID));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public String register(final MetaDataRegisterDTO metaDataRegisterDTO) {
public String registerApiDoc(final ApiDocRegisterDTO apiDocRegisterDTO) {
return null;
}

@Override
public void checkNamespacePluginRel(final String namespaceId, final String pluginName) {

}
}

static class MockFallbackShenyuClientRegisterServiceException extends FallbackShenyuClientRegisterService {
Expand Down Expand Up @@ -98,5 +103,10 @@ public String register(final MetaDataRegisterDTO metaDataRegisterDTO) {
public String registerApiDoc(final ApiDocRegisterDTO apiDocRegisterDTO) {
return null;
}

@Override
public void checkNamespacePluginRel(final String namespaceId, final String pluginName) {

}
}
}

0 comments on commit ab87238

Please sign in to comment.