对应 docs/operation-audit-halo-compliance.md 评审的两项不符合与第 5 节修复建议: 1. API 客户端生成(清单第 2 条):build.gradle 配置 haloPlugin.openApi 管线; 查询/导出/测试端点补 tag、参数声明与类型化 response(生成契约输入); bootstrap OpenAPI 规范 JSON(与 Springdoc 声明一一对手工对齐)驱动 devtools generateApiClient 产出 TS 客户端(ui/src/api/generated,openapi-generator 7.7.0); UI 经 ui/src/api 封装层复用宿主 axiosInstance,替换手写类型与裸 fetch; @halo-dev/api-client 由死依赖转为 axiosInstance 实际来源。 2. 出站安全与凭据(清单第 4 条):新增 OutboundUrlGuard——URI 解析 + scheme 白名单 + 禁止用户名/密码形态 + 全量 DNS 解析并拒绝环回/私网/链路本地/组播/未指定/ 云元数据(169.254.169.254、100.100.100.200、fd00:ec2::254)/CGNAT/ULA/Teredo/ 6to4/文档与保留段;PinnedAddressResolverGroup 钉定校验地址建连(DNS rebinding 防护,TLS SNI/Host 仍取原主机名);重定向显式关闭、三层超时 10 秒。 Webhook 地址凭据迁移 Halo Secret(settings.yaml 改 secret 输入存引用,服务端经 ReactiveExtensionClient 解析键 webhookUrl,fail closed);日志只记主机名, 不再记录可能内嵌 access_token 的完整地址。 测试:后端 244 用例(新增守卫地址段枚举/Secret 解析 fail closed/建连钉定证明等 25 例)+ UI vitest 54 用例 + vue-tsc + ./gradlew build 全部通过。
146 lines
5.7 KiB
Java
146 lines
5.7 KiB
Java
package run.halo.operationaudit.alert;
|
||
|
||
import static org.assertj.core.api.Assertions.assertThat;
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||
import static org.mockito.ArgumentMatchers.any;
|
||
import static org.mockito.ArgumentMatchers.eq;
|
||
import static org.mockito.Mockito.mock;
|
||
import static org.mockito.Mockito.times;
|
||
import static org.mockito.Mockito.verify;
|
||
import static org.mockito.Mockito.when;
|
||
|
||
import java.util.Map;
|
||
import java.util.concurrent.atomic.AtomicInteger;
|
||
import org.junit.jupiter.api.Test;
|
||
import reactor.core.publisher.Mono;
|
||
import run.halo.app.extension.ReactiveExtensionClient;
|
||
import run.halo.app.extension.Secret;
|
||
import run.halo.app.plugin.ReactiveSettingFetcher;
|
||
|
||
/**
|
||
* 告警设置 TTL 缓存测试(与审计设置同款模式):命中缓存、失败/缺省回退默认(告警关闭);
|
||
* Webhook 地址 Secret 解析:未配置引用 / Secret 不存在 / 缺字段 / 空白值均 fail closed
|
||
* (错误信号,绝不发送),字段值去除首尾空白。
|
||
*/
|
||
class AlertSettingProviderTest {
|
||
|
||
private final ReactiveSettingFetcher fetcher = mock(ReactiveSettingFetcher.class);
|
||
private final ReactiveExtensionClient client = mock(ReactiveExtensionClient.class);
|
||
private final AlertSettingProvider provider = new AlertSettingProvider(fetcher, client);
|
||
|
||
private static AlertSettings settings(String secretName) {
|
||
return new AlertSettings(true, secretName, 5, 5);
|
||
}
|
||
|
||
private static Secret secret(String url) {
|
||
var secret = new Secret();
|
||
secret.setStringData(Map.of(AlertSettingProvider.WEBHOOK_URL_FIELD, url));
|
||
return secret;
|
||
}
|
||
|
||
@Test
|
||
void shouldCacheWithinTtl() {
|
||
var fetchCount = new AtomicInteger(0);
|
||
when(fetcher.fetch(eq(AlertSettings.GROUP), eq(AlertSettings.class)))
|
||
.thenAnswer(inv -> {
|
||
fetchCount.incrementAndGet();
|
||
return Mono.just(new AlertSettings(true, "webhook-secret", 3, 2));
|
||
});
|
||
|
||
var first = provider.get().block();
|
||
var second = provider.get().block();
|
||
|
||
assertThat(first).extracting(AlertSettings::enabled, AlertSettings::webhookSecretName,
|
||
AlertSettings::thresholdCount, AlertSettings::windowMinutes)
|
||
.containsExactly(true, "webhook-secret", 3, 2);
|
||
assertThat(second).isSameAs(first);
|
||
assertThat(fetchCount.get()).isEqualTo(1);
|
||
}
|
||
|
||
@Test
|
||
void shouldFallbackToDefaultsWhenFetchFails() {
|
||
when(fetcher.fetch(eq(AlertSettings.GROUP), eq(AlertSettings.class)))
|
||
.thenReturn(Mono.error(new IllegalStateException("configmap missing")));
|
||
|
||
var settings = provider.get().block();
|
||
|
||
assertThat(settings).isEqualTo(AlertSettings.defaults());
|
||
assertThat(settings.enabledOrDefault()).isFalse();
|
||
assertThat(settings.webhookSecretNameOrDefault()).isNull();
|
||
}
|
||
|
||
@Test
|
||
void shouldFallbackToDefaultsWhenConfigMissing() {
|
||
when(fetcher.fetch(eq(AlertSettings.GROUP), eq(AlertSettings.class)))
|
||
.thenReturn(Mono.empty());
|
||
|
||
assertThat(provider.get().block()).isEqualTo(AlertSettings.defaults());
|
||
}
|
||
|
||
@Test
|
||
void shouldRefetchAfterExpiry() {
|
||
when(fetcher.fetch(eq(AlertSettings.GROUP), eq(AlertSettings.class)))
|
||
.thenReturn(Mono.just(AlertSettings.defaults()));
|
||
|
||
provider.get().block();
|
||
|
||
// 手动使缓存过期(TTL 为 60s,等待必然不现实,直接构造过期条目)
|
||
provider.cache = new AlertSettingProvider.CacheEntry(AlertSettings.defaults(), 0);
|
||
|
||
provider.get().block();
|
||
verify(fetcher, times(2)).fetch(any(), any());
|
||
}
|
||
|
||
// ===== Webhook 地址 Secret 解析(fail closed) =====
|
||
|
||
@Test
|
||
void shouldResolveWebhookUrlFromSecret() {
|
||
when(client.fetch(eq(Secret.class), eq("webhook-secret")))
|
||
.thenReturn(Mono.just(secret(" https://hooks.example.com/x ")));
|
||
|
||
assertThat(provider.webhookUrl(settings("webhook-secret")).block())
|
||
.isEqualTo("https://hooks.example.com/x");
|
||
// 便捷方法:先读设置再解析
|
||
when(fetcher.fetch(eq(AlertSettings.GROUP), eq(AlertSettings.class)))
|
||
.thenReturn(Mono.just(settings("webhook-secret")));
|
||
assertThat(provider.webhookUrl().block()).isEqualTo("https://hooks.example.com/x");
|
||
}
|
||
|
||
@Test
|
||
void shouldFailClosedWhenSecretRefMissing() {
|
||
assertThatThrownBy(() -> provider.webhookUrl(settings(" ")).block())
|
||
.isInstanceOf(IllegalStateException.class)
|
||
.hasMessageContaining("未配置");
|
||
}
|
||
|
||
@Test
|
||
void shouldFailClosedWhenSecretMissing() {
|
||
when(client.fetch(eq(Secret.class), eq("gone"))).thenReturn(Mono.empty());
|
||
|
||
assertThatThrownBy(() -> provider.webhookUrl(settings("gone")).block())
|
||
.isInstanceOf(IllegalStateException.class)
|
||
.hasMessageContaining("不存在");
|
||
}
|
||
|
||
@Test
|
||
void shouldFailClosedWhenSecretFieldMissing() {
|
||
var secret = new Secret();
|
||
secret.setStringData(Map.of("other", "value"));
|
||
when(client.fetch(eq(Secret.class), eq("webhook-secret"))).thenReturn(Mono.just(secret));
|
||
|
||
assertThatThrownBy(() -> provider.webhookUrl(settings("webhook-secret")).block())
|
||
.isInstanceOf(IllegalStateException.class)
|
||
.hasMessageContaining(AlertSettingProvider.WEBHOOK_URL_FIELD);
|
||
}
|
||
|
||
@Test
|
||
void shouldFailClosedWhenSecretFieldBlank() {
|
||
when(client.fetch(eq(Secret.class), eq("webhook-secret")))
|
||
.thenReturn(Mono.just(secret(" ")));
|
||
|
||
assertThatThrownBy(() -> provider.webhookUrl(settings("webhook-secret")).block())
|
||
.isInstanceOf(IllegalStateException.class)
|
||
.hasMessageContaining("缺少");
|
||
}
|
||
}
|