添加ip白名单功能
This commit is contained in:
1 parent
c12b777a42
commit
4d656ef63c
20 files changed
+436
-6
No files matched your search
@@ -0,0 +1,29 @@
|
||||
package com.ruoyi.system.rental;
|
||||
|
||||
import org.junit.Test;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static com.ruoyi.system.rental.RentalValues.map;
|
||||
|
||||
/** 白名单保存必须依据持久化应用归属鉴权,且只写归一化后的配置(复用 admin 测试依赖)。 */
|
||||
public class RentalAppIpWhitelistTest {
|
||||
@Test
|
||||
public void ownerCanSaveNormalizedRules() {
|
||||
RentalStore store = mock(RentalStore.class);
|
||||
when(store.get("app", 7L, true)).thenReturn(map("user_id", 9L));
|
||||
new RentalProvisioning(store, mock(RentalOrders.class))
|
||||
.updateAppIpWhitelist(7L, "192.168.*.* , 203.0.113.1", 9L);
|
||||
verify(store).update("app", 7L, map("ip_whitelist", "192.168.*.*\n203.0.113.1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void otherOwnerCannotChangeWhitelist() {
|
||||
RentalStore store = mock(RentalStore.class);
|
||||
when(store.get("app", 7L, true)).thenReturn(map("user_id", 9L));
|
||||
try {
|
||||
new RentalProvisioning(store, mock(RentalOrders.class)).updateAppIpWhitelist(7L, "*", 10L);
|
||||
org.junit.Assert.fail("其他商户不得修改白名单");
|
||||
} catch (IllegalArgumentException expected) {
|
||||
verify(store, never()).update(anyString(), anyLong(), anyMap());
|
||||
}
|
||||
}
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
package com.ruoyi.web.controller.system;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.ruoyi.common.exception.CustomException;
|
||||
import com.ruoyi.system.domain.AppApiKeysPlus;
|
||||
import com.ruoyi.system.domain.Apps;
|
||||
import com.ruoyi.system.domain.dto.EncryptedRequest;
|
||||
import com.ruoyi.system.mapper.AppsMapper;
|
||||
import com.ruoyi.system.rental.*;
|
||||
import com.ruoyi.system.service.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.test.util.ReflectionTestUtils;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
/** 使用可控凭证和请求,验证两套真实API入口不会因伪造转发头而绕过白名单。 */
|
||||
public class AppIpWhitelistSecurityTest {
|
||||
@After
|
||||
public void clearRequest() { RequestContextHolder.resetRequestAttributes(); }
|
||||
|
||||
@Test
|
||||
public void paymentRejectsNonWhitelistedPeerBeforeDecrypting() {
|
||||
ApiSecurityService service = new ApiSecurityService();
|
||||
IAppApiKeysService keys = mock(IAppApiKeysService.class);
|
||||
AppsMapper apps = mock(AppsMapper.class);
|
||||
AppPrivateKeyCipher cipher = mock(AppPrivateKeyCipher.class);
|
||||
AppApiKeysPlus credential = new AppApiKeysPlus();
|
||||
credential.setAppId(7L); credential.setUserId(8L);
|
||||
Apps app = new Apps();
|
||||
app.setId(7L); app.setUserId(8L); app.setStatus(1); app.setIpWhitelist("203.0.113.*");
|
||||
when(keys.getOne(any())).thenReturn(credential);
|
||||
when(apps.selectAppsById(7L)).thenReturn(app);
|
||||
ReflectionTestUtils.setField(service, "apiKeysService", keys);
|
||||
ReflectionTestUtils.setField(service, "appsMapper", apps);
|
||||
ReflectionTestUtils.setField(service, "privateKeyCipher", cipher);
|
||||
MockHttpServletRequest request = request("198.51.100.1");
|
||||
request.addHeader("X-Forwarded-For", "203.0.113.10");
|
||||
request.addHeader("X-Real-IP", "203.0.113.10");
|
||||
RequestContextHolder.setRequestAttributes(new ServletRequestAttributes(request));
|
||||
EncryptedRequest encrypted = new EncryptedRequest();
|
||||
encrypted.setApiKey("key"); encrypted.setTimestamp(System.currentTimeMillis()); encrypted.setNonce("nonce");
|
||||
encrypted.setEncryptedKey("key"); encrypted.setIv("iv"); encrypted.setData("data"); encrypted.setHmac("hmac");
|
||||
denied(() -> service.decryptAndVerify(encrypted));
|
||||
app.setIpWhitelist("");
|
||||
denied(() -> service.decryptAndVerify(encrypted));
|
||||
verifyNoInteractions(cipher);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void rentalEnforcesApplicationWhitelistAcrossOperations() throws Exception {
|
||||
RentalIdentity identity = mock(RentalIdentity.class);
|
||||
RentalStore store = mock(RentalStore.class);
|
||||
RentalOrders orders = mock(RentalOrders.class);
|
||||
Map<String, Object> credential = new HashMap<>(); credential.put("owner_id", 7L);
|
||||
Map<String, Object> app = new HashMap<>(); app.put("status", "ACTIVE"); app.put("user_id", 8L);
|
||||
app.put("ip_whitelist", "203.0.113.*");
|
||||
when(identity.authenticate(anyString(), eq("APP"), eq("API"), anyLong(), anyString(), eq("POST"), anyString(), anyString(), anyString())).thenReturn(credential);
|
||||
when(store.get("app", 7L, false)).thenReturn(app);
|
||||
RentalApiController controller = new RentalApiController(identity, store, orders, new ObjectMapper());
|
||||
for (String operation : Arrays.asList("create", "query", "quote", "packages")) {
|
||||
MockHttpServletRequest request = request("198.51.100.1");
|
||||
request.addHeader("X-Forwarded-For", "203.0.113.10");
|
||||
try {
|
||||
controller.execute(operation, request);
|
||||
fail("非白名单来源不能调用租赁API");
|
||||
} catch (CustomException expected) { assertEquals(Integer.valueOf(403), expected.getCode()); }
|
||||
}
|
||||
controller.execute("packages", request("203.0.113.10"));
|
||||
try {
|
||||
controller.executeOnchain("quote", request("198.51.100.1"));
|
||||
fail("地址上链入口同样需要白名单");
|
||||
} catch (CustomException expected) { assertEquals(Integer.valueOf(403), expected.getCode()); }
|
||||
verifyNoInteractions(orders);
|
||||
}
|
||||
|
||||
private MockHttpServletRequest request(String ip) {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("POST", "/api/rental/v1/packages");
|
||||
request.setRemoteAddr(ip);
|
||||
request.setContent("{\"network\":\"NILE\"}".getBytes(StandardCharsets.UTF_8));
|
||||
request.addHeader("X-Rent-Key", "key"); request.addHeader("X-Rent-Timestamp", "1");
|
||||
request.addHeader("X-Rent-Nonce", "nonce"); request.addHeader("X-Rent-Signature", "signature");
|
||||
return request;
|
||||
}
|
||||
|
||||
private void denied(Runnable operation) {
|
||||
try { operation.run(); fail("必须拒绝白名单以外的IP"); }
|
||||
catch (CustomException expected) { assertEquals(Integer.valueOf(403), expected.getCode()); }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user