Skip to content

Commit a87a361

Browse files
authored
fix(admin): enforce metadata operation permissions (#8197)
1 parent 4fc4d8e commit a87a361

2 files changed

Lines changed: 111 additions & 1 deletion

File tree

‎shenyu-admin/src/main/java/org/apache/shenyu/admin/service/impl/MetaDataServiceImpl.java‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.apache.shenyu.common.utils.ListUtil;
4343
import org.apache.shenyu.common.utils.UUIDUtils;
4444
import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
45+
import org.apache.shiro.SecurityUtils;
4546
import org.slf4j.Logger;
4647
import org.slf4j.LoggerFactory;
4748
import org.springframework.context.ApplicationEventPublisher;
@@ -66,6 +67,10 @@ public class MetaDataServiceImpl implements MetaDataService {
6667

6768
private static final Logger LOG = LoggerFactory.getLogger(MetaDataServiceImpl.class);
6869

70+
private static final String CREATE_PERMISSION = "system:meta:add";
71+
72+
private static final String UPDATE_PERMISSION = "system:meta:edit";
73+
6974
private final MetaDataMapper metaDataMapper;
7075

7176
private final ApplicationEventPublisher eventPublisher;
@@ -104,7 +109,9 @@ public void saveOrUpdateMetaData(final MetaDataDO exist, final MetaDataRegisterD
104109
@Override
105110
@Transactional(rollbackFor = Exception.class)
106111
public String createOrUpdate(final MetaDataDTO metaDataDTO) {
107-
if (StringUtils.isBlank(metaDataDTO.getId())) {
112+
final boolean create = StringUtils.isBlank(metaDataDTO.getId());
113+
SecurityUtils.getSubject().checkPermission(create ? CREATE_PERMISSION : UPDATE_PERMISSION);
114+
if (create) {
108115
create(metaDataDTO);
109116
return ShenyuResultMessage.CREATE_SUCCESS;
110117
}

‎shenyu-admin/src/test/java/org/apache/shenyu/admin/service/MetaDataServiceTest.java‎

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,14 @@
3535
import org.apache.shenyu.common.constant.AdminConstants;
3636
import org.apache.shenyu.common.dto.MetaData;
3737
import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
38+
import org.apache.shiro.authz.UnauthorizedException;
39+
import org.apache.shiro.subject.Subject;
40+
import org.apache.shiro.util.ThreadContext;
3841
import org.junit.jupiter.api.AfterAll;
42+
import org.junit.jupiter.api.AfterEach;
3943
import org.junit.jupiter.api.Assertions;
4044
import org.junit.jupiter.api.BeforeAll;
45+
import org.junit.jupiter.api.BeforeEach;
4146
import org.junit.jupiter.api.Test;
4247
import org.junit.jupiter.api.extension.ExtendWith;
4348
import org.mockito.ArgumentCaptor;
@@ -58,11 +63,13 @@
5863

5964
import static org.apache.shenyu.common.constant.Constants.SYS_DEFAULT_NAMESPACE_ID;
6065
import static org.junit.jupiter.api.Assertions.assertNotNull;
66+
import static org.junit.jupiter.api.Assertions.assertThrows;
6167
import static org.mockito.ArgumentMatchers.any;
6268
import static org.mockito.ArgumentMatchers.anyString;
6369
import static org.mockito.BDDMockito.given;
6470
import static org.mockito.Mockito.CALLS_REAL_METHODS;
6571
import static org.mockito.Mockito.doNothing;
72+
import static org.mockito.Mockito.doThrow;
6673
import static org.mockito.Mockito.mockStatic;
6774
import static org.mockito.Mockito.never;
6875
import static org.mockito.Mockito.spy;
@@ -102,6 +109,9 @@ public final class MetaDataServiceTest {
102109
@Mock
103110
private NamespaceMapper namespaceMapper;
104111

112+
@Mock
113+
private Subject subject;
114+
105115
@BeforeAll
106116
public static void beforeClass() {
107117
loggerSpy = spy(LoggerFactory.getLogger(MetaDataServiceImpl.class));
@@ -114,6 +124,16 @@ public static void afterClass() {
114124
loggerFactoryMockedStatic.close();
115125
}
116126

127+
@BeforeEach
128+
public void setUp() {
129+
ThreadContext.bind(subject);
130+
}
131+
132+
@AfterEach
133+
public void tearDown() {
134+
ThreadContext.unbindSubject();
135+
}
136+
117137
/**
118138
* Test case for saveOrUpdateMetaData().
119139
*/
@@ -133,6 +153,89 @@ public void testCreateOrUpdate() {
133153
testCreateOrUpdateForUpdate();
134154
}
135155

156+
/**
157+
* Test case for an add-only subject creating metadata with a blank id.
158+
*/
159+
@Test
160+
public void testAddOnlyPermissionAllowsCreateWithBlankId() {
161+
MetaDataDTO dto = new MetaDataDTO();
162+
dto.setId(" ");
163+
dto.setPath("path");
164+
dto.setNamespaceId(SYS_DEFAULT_NAMESPACE_ID);
165+
when(metaDataMapper.pathExisted("path", SYS_DEFAULT_NAMESPACE_ID)).thenReturn(null);
166+
when(metaDataMapper.insert(any())).thenReturn(1);
167+
168+
assertEquals(ShenyuResultMessage.CREATE_SUCCESS, metaDataService.createOrUpdate(dto));
169+
170+
verify(subject).checkPermission("system:meta:add");
171+
verify(subject, never()).checkPermission("system:meta:edit");
172+
verify(metaDataMapper).insert(any());
173+
}
174+
175+
/**
176+
* Test case for an edit-only subject updating existing metadata.
177+
*/
178+
@Test
179+
public void testEditOnlyPermissionAllowsUpdate() {
180+
MetaDataDTO dto = new MetaDataDTO();
181+
dto.setId("id");
182+
dto.setPath("path");
183+
dto.setNamespaceId(SYS_DEFAULT_NAMESPACE_ID);
184+
MetaDataDO current = MetaDataDO.builder().id("id").enabled(true).build();
185+
when(metaDataMapper.pathExistedExclude("path", SYS_DEFAULT_NAMESPACE_ID, Collections.singletonList("id"))).thenReturn(null);
186+
when(metaDataMapper.selectById("id")).thenReturn(current);
187+
when(metaDataMapper.update(any())).thenReturn(1);
188+
189+
assertEquals(ShenyuResultMessage.UPDATE_SUCCESS, metaDataService.createOrUpdate(dto));
190+
191+
verify(subject).checkPermission("system:meta:edit");
192+
verify(subject, never()).checkPermission("system:meta:add");
193+
verify(metaDataMapper).update(any());
194+
}
195+
196+
/**
197+
* Test case for an edit-only subject attempting to create metadata.
198+
*/
199+
@Test
200+
public void testEditOnlyPermissionCannotCreate() {
201+
MetaDataDTO dto = new MetaDataDTO();
202+
doThrow(new UnauthorizedException()).when(subject).checkPermission("system:meta:add");
203+
204+
assertThrows(UnauthorizedException.class, () -> metaDataService.createOrUpdate(dto));
205+
206+
verifyNoInteractions(metaDataMapper, eventPublisher, publisher);
207+
}
208+
209+
/**
210+
* Test case for an add-only subject attempting to update a missing metadata id.
211+
*/
212+
@Test
213+
public void testAddOnlyPermissionCannotUpdateMissingMetadata() {
214+
MetaDataDTO dto = new MetaDataDTO();
215+
dto.setId("missing");
216+
doThrow(new UnauthorizedException()).when(subject).checkPermission("system:meta:edit");
217+
218+
assertThrows(UnauthorizedException.class, () -> metaDataService.createOrUpdate(dto));
219+
220+
verifyNoInteractions(metaDataMapper, eventPublisher, publisher);
221+
}
222+
223+
/**
224+
* Test case for a subject without either metadata write permission.
225+
*/
226+
@Test
227+
public void testNoPermissionRejectsCreateAndUpdate() {
228+
doThrow(new UnauthorizedException()).when(subject).checkPermission(anyString());
229+
MetaDataDTO create = new MetaDataDTO();
230+
MetaDataDTO update = new MetaDataDTO();
231+
update.setId("id");
232+
233+
assertThrows(UnauthorizedException.class, () -> metaDataService.createOrUpdate(create));
234+
assertThrows(UnauthorizedException.class, () -> metaDataService.createOrUpdate(update));
235+
236+
verifyNoInteractions(metaDataMapper, eventPublisher, publisher);
237+
}
238+
136239
/**
137240
* Test case for update publishes sibling methods whose rpc_ext changed.
138241
*/

0 commit comments

Comments
 (0)