Dev #105
+50
-2
@@ -25,6 +25,7 @@ import com.xiaowang.jayucloud.salon.core.modules.system.vo.SecureIntegrationConf
|
||||
import com.xiaowang.jayucloud.salon.core.modules.user.entity.UserDO;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -33,6 +34,7 @@ import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
@@ -41,6 +43,8 @@ import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
@Slf4j
|
||||
@Service
|
||||
@@ -51,6 +55,11 @@ public class IntegrationProbeServiceImpl implements IntegrationProbeService {
|
||||
private static final String STORAGE = "OBJECT_STORAGE";
|
||||
private static final String SMS = "SMS";
|
||||
private static final String SUBSCRIBE = "SUBSCRIBE_MESSAGE";
|
||||
private static final Pattern URL_IN_MESSAGE = Pattern.compile("https?://[^\\s\"'<>]+");
|
||||
private static final Pattern URL_HOST = Pattern.compile("^(https?://[^/?#]+)");
|
||||
private static final Pattern SENSITIVE_PARAM = Pattern.compile(
|
||||
"(?i)(signature|token|access[-_]?key(?:[-_]?id|[-_]?secret)?|secret[-_]?key)=([^&\\s\"']+)");
|
||||
private static final int FAILURE_REASON_MAX_LENGTH = 120;
|
||||
|
||||
private final IntegrationProbeResultMapper resultMapper;
|
||||
private final ObjectMapper objectMapper;
|
||||
@@ -386,17 +395,26 @@ public class IntegrationProbeServiceImpl implements IntegrationProbeService {
|
||||
}
|
||||
enabled++;
|
||||
boolean cleanupFailed = false;
|
||||
String stage = "上传";
|
||||
try {
|
||||
provider.uploadStream(new java.io.ByteArrayInputStream(bytes), bytes.length, "text/plain", objectName);
|
||||
stage = "服务端读取";
|
||||
try (InputStream input = provider.downloadFile(objectName)) {
|
||||
boolean same = java.util.Arrays.equals(bytes, input.readAllBytes());
|
||||
stage = "生成签名地址";
|
||||
String signed = provider.getPresignedUrl(objectName, 5);
|
||||
byte[] signedBytes = StringUtils.hasText(signed) ? restTemplate.getForObject(signed, byte[].class) : null;
|
||||
stage = "签名访问";
|
||||
// 传 URI 而不是字符串:字符串重载会经 DefaultUriBuilderFactory 的 URI_COMPONENT 展开,
|
||||
// 把签名参数里的 %2B/%2F/%3D 二次编码成 %252B/%252F/%253D,导致对象存储验签失败。
|
||||
byte[] signedBytes = StringUtils.hasText(signed)
|
||||
? restTemplate.exchange(URI.create(signed), HttpMethod.GET, null, byte[].class).getBody()
|
||||
: null;
|
||||
stage = "内容比对";
|
||||
boolean signedRead = java.util.Arrays.equals(bytes, signedBytes);
|
||||
checks.add(new IntegrationProbeCheckVO(provider.providerCode(), same && signedRead ? "PASS" : "FAIL", same && signedRead ? "上传、读取和签名访问成功" : "对象内容或签名读取不一致"));
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
checks.add(new IntegrationProbeCheckVO(provider.providerCode(), "FAIL", "上传或读取失败"));
|
||||
checks.add(new IntegrationProbeCheckVO(provider.providerCode(), "FAIL", storageFailureDetail(stage, exception)));
|
||||
} finally {
|
||||
try { provider.deleteFile(objectName); }
|
||||
catch (Exception ignored) { cleanupFailed = true; log.warn("清理对象存储测试文件失败,provider={}", provider.providerCode()); }
|
||||
@@ -410,6 +428,36 @@ public class IntegrationProbeServiceImpl implements IntegrationProbeService {
|
||||
}
|
||||
}
|
||||
|
||||
private String storageFailureDetail(String stage, Exception exception) {
|
||||
return stage + "失败(" + exception.getClass().getSimpleName() + ":" + describeStorageFailure(exception) + ")";
|
||||
}
|
||||
|
||||
private String describeStorageFailure(Exception exception) {
|
||||
String message = exception.getMessage();
|
||||
if (!StringUtils.hasText(message)) {
|
||||
return "无错误消息";
|
||||
}
|
||||
Matcher urlMatcher = URL_IN_MESSAGE.matcher(message);
|
||||
StringBuffer masked = new StringBuffer();
|
||||
while (urlMatcher.find()) {
|
||||
urlMatcher.appendReplacement(masked, Matcher.quoteReplacement(maskUrl(urlMatcher.group())));
|
||||
}
|
||||
urlMatcher.appendTail(masked);
|
||||
String sanitized = SENSITIVE_PARAM.matcher(masked.toString()).replaceAll("$1=***");
|
||||
sanitized = sanitized.replaceAll("\\s+", " ").trim();
|
||||
return sanitized.length() > FAILURE_REASON_MAX_LENGTH
|
||||
? sanitized.substring(0, FAILURE_REASON_MAX_LENGTH) + "…"
|
||||
: sanitized;
|
||||
}
|
||||
|
||||
private String maskUrl(String url) {
|
||||
Matcher hostMatcher = URL_HOST.matcher(url);
|
||||
if (hostMatcher.find()) {
|
||||
return hostMatcher.group(1) + "/…";
|
||||
}
|
||||
return "(地址已隐藏)";
|
||||
}
|
||||
|
||||
private IntegrationProbeResultVO probeSms(UserDO currentUser, String operation, String mode, String phone) {
|
||||
List<IntegrationProbeCheckVO> checks = new ArrayList<>();
|
||||
List<com.xiaowang.jayucloud.salon.core.modules.integration.vo.MessageHubPushConfigVO> configs = messageHubPushConfigService.listConfigs(currentUser);
|
||||
|
||||
+71
@@ -18,15 +18,24 @@ import com.xiaowang.jayucloud.salon.core.modules.user.entity.UserDO;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.net.URI;
|
||||
import java.net.UnknownHostException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.anyLong;
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
@@ -159,6 +168,68 @@ class IntegrationProbeServiceImplTest {
|
||||
assertEquals("微信审核请求参数格式无效,请核对接口字段", result.reason());
|
||||
}
|
||||
|
||||
@Test
|
||||
void signedReadPassesRawPresignedUrlAsUriWithoutReencoding() throws Exception {
|
||||
String signed = "https://jayucloud-salon-sit.oss-cn-chengdu.aliyuncs.com/integration-probe/probe.txt"
|
||||
+ "?Expires=1762600000&OSSAccessKeyId=LTAI-test&Signature=abc%2Bdef%2Fghi%3D";
|
||||
byte[] payload = "salon-integration-probe".getBytes(StandardCharsets.UTF_8);
|
||||
when(storageProvider.providerCode()).thenReturn("ALIYUN_OSS");
|
||||
when(storageProvider.isEnabled()).thenReturn(true);
|
||||
when(storageProvider.downloadFile(anyString())).thenReturn(new ByteArrayInputStream(payload));
|
||||
when(storageProvider.getPresignedUrl(anyString(), any())).thenReturn(signed);
|
||||
when(restTemplate.exchange(any(URI.class), eq(HttpMethod.GET), any(), eq(byte[].class)))
|
||||
.thenReturn(new ResponseEntity<>(payload, HttpStatus.OK));
|
||||
when(results.selectOne(any())).thenReturn(null);
|
||||
|
||||
var result = service.probe(admin(), request("OBJECT_STORAGE", "ROUND_TRIP", "DIAGNOSTIC", false));
|
||||
|
||||
ArgumentCaptor<URI> uriCaptor = ArgumentCaptor.forClass(URI.class);
|
||||
verify(restTemplate).exchange(uriCaptor.capture(), eq(HttpMethod.GET), any(), eq(byte[].class));
|
||||
assertEquals(signed, uriCaptor.getValue().toString(), "签名地址必须以 URI 原样传入,不能再被 URI_COMPONENT 编码");
|
||||
assertTrue(uriCaptor.getValue().toString().contains("%2B"));
|
||||
verify(restTemplate, never()).getForObject(anyString(), eq(byte[].class));
|
||||
assertEquals("PASS", result.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
void storageFailureDetailCarriesStageAndExceptionType() throws Exception {
|
||||
when(storageProvider.providerCode()).thenReturn("MINIO");
|
||||
when(storageProvider.isEnabled()).thenReturn(true);
|
||||
when(storageProvider.downloadFile(anyString())).thenThrow(new UnknownHostException("sit.minio.yunail.art"));
|
||||
|
||||
var result = service.probe(admin(), request("OBJECT_STORAGE", "ROUND_TRIP", "DIAGNOSTIC", false));
|
||||
|
||||
String detail = result.getChecks().get(0).getDetail();
|
||||
assertEquals("FAIL", result.getStatus());
|
||||
assertTrue(detail.contains("服务端读取失败"), detail);
|
||||
assertTrue(detail.contains("UnknownHostException"), detail);
|
||||
assertTrue(detail.contains("sit.minio.yunail.art"), detail);
|
||||
}
|
||||
|
||||
@Test
|
||||
void storageFailureDetailMasksSignedUrlInsteadOfLeakingSignature() throws Exception {
|
||||
String signed = "https://jayucloud-salon-sit.oss-cn-chengdu.aliyuncs.com/integration-probe/probe.txt"
|
||||
+ "?Expires=1762600000&OSSAccessKeyId=LTAI-test&Signature=abc%2Bdef%3D";
|
||||
byte[] payload = "salon-integration-probe".getBytes(StandardCharsets.UTF_8);
|
||||
when(storageProvider.providerCode()).thenReturn("ALIYUN_OSS");
|
||||
when(storageProvider.isEnabled()).thenReturn(true);
|
||||
when(storageProvider.downloadFile(anyString())).thenReturn(new ByteArrayInputStream(payload));
|
||||
when(storageProvider.getPresignedUrl(anyString(), any())).thenReturn(signed);
|
||||
when(restTemplate.exchange(any(URI.class), eq(HttpMethod.GET), any(), eq(byte[].class)))
|
||||
.thenThrow(new IllegalStateException("403 SignatureDoesNotMatch for URL: " + signed));
|
||||
when(results.selectOne(any())).thenReturn(null);
|
||||
|
||||
var result = service.probe(admin(), request("OBJECT_STORAGE", "ROUND_TRIP", "DIAGNOSTIC", false));
|
||||
|
||||
String detail = result.getChecks().get(0).getDetail();
|
||||
assertTrue(detail.contains("签名访问失败"), detail);
|
||||
assertTrue(detail.contains("IllegalStateException"), detail);
|
||||
assertTrue(detail.contains("jayucloud-salon-sit.oss-cn-chengdu.aliyuncs.com"), detail);
|
||||
assertFalse(detail.contains("Signature="), detail);
|
||||
assertFalse(detail.contains("OSSAccessKeyId"), detail);
|
||||
assertFalse(detail.contains("%2B"), detail);
|
||||
}
|
||||
|
||||
private UserDO admin() {
|
||||
UserDO user = new UserDO();
|
||||
user.setId(1L);
|
||||
|
||||
@@ -122,9 +122,9 @@
|
||||
<p class="settings-group__desc">配置保存后再测试;点击状态芯片可展开检查项明细,真实发送会产生外部副作用。</p>
|
||||
</div>
|
||||
<div class="settings-group__actions">
|
||||
<a-button size="small" :loading="refreshingProbeResults" :disabled="refreshingProbeResults" title="重新查询最近探测结果,不会重新发起外部探测" @click="refreshProbeResults">刷新</a-button>
|
||||
<span v-if="probeFailCount" class="pill pill--fail">{{ probeFailCount }} 项失败</span>
|
||||
<span v-if="probeWarnCount" class="pill pill--warn">{{ probeWarnCount }} 项需关注</span>
|
||||
<a-button size="small" :loading="refreshingProbeResults" @click="refreshProbeResults">刷新最近结果</a-button>
|
||||
</div>
|
||||
</header>
|
||||
<div class="settings-group__body">
|
||||
@@ -163,7 +163,7 @@
|
||||
<div v-for="check in operation.result?.checks || []" :key="`${check.name}-${check.status}`" class="check-row">
|
||||
<span>{{ check.name }}</span>
|
||||
<em :class="`check-row__status--${check.status.toLowerCase()}`">{{ probeStatusLabel(check.status) }}</em>
|
||||
<small>{{ check.detail }}</small>
|
||||
<small v-if="check.detail && check.detail !== operation.result?.summary">{{ check.detail }}</small>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
Reference in New Issue
Block a user