Compare commits
23
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aee5fe10b4 | ||
|
|
5d6253cbf1 | ||
|
|
bb3baed095 | ||
|
|
4b6bdeeef6 | ||
|
|
f2ac810572 | ||
|
|
fd860a87a2 | ||
|
|
c04b9262c4 | ||
|
|
aef5356c54 | ||
|
|
1a608f8ff8 | ||
|
|
2e53b1d9c8 | ||
|
|
ba5dbc195e | ||
|
|
02c21e5c19 | ||
|
|
32f6f67b9f | ||
|
|
926aa64ba3 | ||
|
|
d994ffb9a5 | ||
|
|
c716ec7478 | ||
|
|
5be06a97c1 | ||
|
|
76265845dc | ||
|
|
5533a483d9 | ||
|
|
7785cda6a6 | ||
|
|
813b406da4 | ||
|
|
af505c5596 | ||
|
|
8a13ef9c02 |
No files matched your search
@@ -0,0 +1,17 @@
|
||||
version: 2
|
||||
updates:
|
||||
- package-ecosystem: maven
|
||||
directory: /control-plane-java
|
||||
schedule:
|
||||
interval: weekly
|
||||
open-pull-requests-limit: 5
|
||||
- package-ecosystem: npm
|
||||
directory: /prototype
|
||||
schedule:
|
||||
interval: weekly
|
||||
open-pull-requests-limit: 5
|
||||
- package-ecosystem: github-actions
|
||||
directory: /
|
||||
schedule:
|
||||
interval: weekly
|
||||
open-pull-requests-limit: 5
|
||||
@@ -0,0 +1,66 @@
|
||||
name: Repository verification
|
||||
on:
|
||||
pull_request:
|
||||
push:
|
||||
branches: [master, main]
|
||||
permissions:
|
||||
contents: read
|
||||
concurrency:
|
||||
group: verify-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
jobs:
|
||||
backend:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 20
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:15.18
|
||||
env:
|
||||
POSTGRES_USER: moyi_role_test
|
||||
POSTGRES_DB: moyi_ci_test
|
||||
POSTGRES_HOST_AUTH_METHOD: trust
|
||||
ports: ['5432:5432']
|
||||
options: >-
|
||||
--health-cmd "pg_isready -U moyi_role_test -d moyi_ci_test"
|
||||
--health-interval 5s --health-timeout 5s --health-retries 10
|
||||
redis:
|
||||
image: redis:7.4-alpine
|
||||
ports: ['6380:6379']
|
||||
options: >-
|
||||
--health-cmd "redis-cli ping"
|
||||
--health-interval 5s --health-timeout 5s --health-retries 10
|
||||
env:
|
||||
MOYI_AUTH_TEST_JDBC_URL: jdbc:postgresql://127.0.0.1:5432/moyi_ci_test
|
||||
MOYI_GATEWAY_TEST_REDIS_PORT: '6380'
|
||||
steps:
|
||||
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
- uses: actions/setup-java@cf277c60eb25467037889841efdb72551f06f6c3 # v4
|
||||
with:
|
||||
distribution: temurin
|
||||
java-version: '21'
|
||||
cache: maven
|
||||
- run: mvn -B -ntp -f control-plane-java/pom.xml clean verify
|
||||
- uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02 # v4
|
||||
if: failure()
|
||||
with:
|
||||
name: backend-test-results
|
||||
path: control-plane-java/target/surefire-reports/
|
||||
frontend:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 10
|
||||
defaults:
|
||||
run:
|
||||
working-directory: prototype
|
||||
steps:
|
||||
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4
|
||||
- uses: pnpm/action-setup@b906affcce14559ad1aafd4ab0e942779e9f58b1 # v4
|
||||
with:
|
||||
version: '9'
|
||||
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4
|
||||
with:
|
||||
node-version: '22'
|
||||
cache: pnpm
|
||||
cache-dependency-path: prototype/pnpm-lock.yaml
|
||||
- run: pnpm install --frozen-lockfile
|
||||
- run: pnpm run build
|
||||
- run: pnpm test
|
||||
@@ -6,6 +6,18 @@
|
||||
>
|
||||
> One Gateway for Every Model You Use.
|
||||
|
||||
## 当前工程状态
|
||||
|
||||
2026-10-28:当前产品为 Java/Vue 自研控制面与多模型网关,已补请求模块化、账务恢复、异步幂等和审计核账,并继续加固认证与 HTTP 边界。参考图下载已补 DNS 地址绑定,注册/重置已补事务原子性与加密异步投递;模型级 FIFO 与执行期间续租已有本地实现,但共享等待预算和部署侧长请求合同仍未完成。音视频结果规范化与媒体有效性检查已补本地回归,**鉴权成品下载、音视频用量计费、真实供应商生成/播放、账号全生命周期和生产安全验收仍未完成**。Codex WebSocket/续接按用户决定暂缓,不存储对话。下文演进目标不等于已实现功能。
|
||||
|
||||
逐项完成状态与剩余验收见 [安全与 CPR 任务追踪](docs/24-security-and-cpr-task-tracker.md),认证边界见 [认证及有界 HTTP 增量](docs/25-auth-and-bounded-transport.md),出站加固与验收阻塞见 [媒体 DNS 绑定与验收状态](docs/26-media-dns-binding-and-acceptance.md),注册/重置原子性见 [验证码与账号事务](docs/27-verified-account-transactions.md)。本地验证通过不代表已部署或全仓库安全审计完成。
|
||||
|
||||
最新 [验证码投递隔离](docs/28-verification-delivery-isolation.md) 需要先迁移 V50,并配置 `CREDENTIAL_ENCRYPTION_KEY` 及邮件/短信服务。2026-10-22 用户要求的每半小时本地提交检查已取消;产品内部的任务与投递 worker 不属于该聊天自动化。
|
||||
|
||||
媒体后续见 [媒体结果交付边界与验收准备](docs/31-media-result-delivery-boundary.md):即时/轮询共用纯媒体结果模块,无主文件不再提示可下载;本地回归不等于真实供应商生成/播放验收。
|
||||
|
||||
本地测试后续见 [Docker 功能验收与并发测试准备](docs/32-local-docker-acceptance-and-concurrency.md):独立 QA 栈已运行在 `http://127.0.0.1:18089`,原本地实例保留;七项只读 API 规格验收通过。并发 skill 因框架没有 `race` 执行器只交付待审计划,没有宣称并发或音视频生成/播放通过。
|
||||
|
||||
## 一、产品简介
|
||||
|
||||
模亦灵枢是由模灵科技与亦城财富等合作方共同推动的多模型智能服务平台。
|
||||
|
||||
@@ -1,14 +1,34 @@
|
||||
# MOYI Java Control Plane
|
||||
|
||||
这是 MOYI Control Plane 的 Java 版本,目标运行时为 Java 21,框架为 Spring Boot。当前已提供健康检查、Flyway 数据库迁移、邀请码认证、用户会话、供应商/模型目录、项目、API Key、模型权限和最小 OpenAI 兼容接口;真实供应商适配、异步任务队列和计费仍按后续节点迁移。
|
||||
这是 MOYI Control Plane 的 Java 版本,目标运行时为 Java 21,框架为 Spring Boot。当前包括健康检查、Flyway 数据库迁移、邀请码认证、用户会话、供应商/模型目录、项目、API Key、模型权限、同步与原生协议网关、异步任务、计费和用量审计。
|
||||
|
||||
目标依赖边界:
|
||||
|
||||
- PostgreSQL:用户、工作区、供应商、模型目录、Key、计费和审计等持久化数据。
|
||||
- Redis:会话、限流、并发计数、幂等键和异步任务队列。
|
||||
- PostgreSQL:用户、会话、供应商、模型目录、Key、任务队列、幂等、上游账号健康、计费和审计等持久化数据。
|
||||
- Redis:请求频率、并发准入租约;当前会话、调用幂等和异步任务队列以 PostgreSQL 持久化为准。
|
||||
- OSS:3D、图片、视频等任务结果和用户上传文件;只保存对象元数据,应用不保存大文件本体。
|
||||
- Provider Adapter:服务端调用 DeepSeek、Kimi、GLM、Seedance、Tripo/VAST 和后续中转渠道。
|
||||
|
||||
## API 请求模块边界
|
||||
|
||||
`gateway.request` 是统一的模型 API 请求应用边界。HTTP/Playground 控制器负责认证、协议解析和规范化;请求模块接收不含凭据、提示词和原始请求体的 `ApiRequestContext`,先执行风险策略,再通过准入租约包住钱包预留、Provider 调用和最终结算。Chat、原生协议和异步任务提交共用该风险策略。
|
||||
|
||||
协议字段映射留在各自 Meter/Adapter;`ApiRequestRiskRule` 只能读取最小化上下文并返回决策,不能调用 Provider 或改账。新增协议时应复用准入、风控和账务边界,而不是在 Controller 中另写一套钱包/重试流程。默认 `GATEWAY_RISK_MODE=shadow` 仅记录规则 ID,不改变调用;只有显式部署规则并设置 `enforce` 才会拒绝或限速。请求来源由服务端路由确定,不能信任 `X-Moyi-Source` 作为风控分类。
|
||||
|
||||
账务不变量:风险拒绝必须先于并发准入和钱包预留;预留使用价格快照;已确认失败的释放和成功结算沿用原子事务;不完整流或无效用量不得估算扣费。
|
||||
|
||||
异步创建支持用户级 `Idempotency-Key`(`V45`),同键同请求返回原任务,同键不同请求返回 409。异步上游受理未知、回执丢失、worker 中断或轮询期限到期时进入 `reconciling`(`V46`),保留冻结与预算占用,不自动重发/退款。已观察的生成结果先持久化再结算,账本失败只重试本地结算。发布时须先停旧 worker,不能混跑旧的未知结果退款逻辑。详见 `../docs/22-async-task-safety.md`。
|
||||
|
||||
`V47` 冻结异步 provider/model 绑定并约束已证明的上游任务身份;`V48` 增加仅追加核账收据。管理端“价格版本与成本对账”提供人工核账,可根据证据恢复只读轮询、确认失败释放原冻结、确认成功并按原价和实际用量结算。操作使用管理员会话、版本检查、UUID 幂等收据和与钱包同事务的审计;worker 和人工操作共用 `TaskSettlementService`,不另写扣费逻辑。历史无法证明的已受理绑定不能猜测补齐。详见 `../docs/23-audited-task-reconciliation.md`;尚无双人审批,不等同于完整财务流程。
|
||||
|
||||
同步和原生调用在结算前单独提交精简用量快照(Flyway `V44`)。快照已持久化而账本事务失败时,接口返回 `SETTLEMENT_PENDING`,冻结额度保留,由后台恢复任务重试结算。该快照不保存提示词、生成内容或密钥;不能将其当作响应重放存储。若数据库在快照写入前已不可用,则尚不能保证结果的持久恢复,必须结合上游记录核对。
|
||||
|
||||
`provider.credentials.UpstreamCredentialResolver` 共享密钥池解析与轮转;`provider.transport.SseEventReader` 专门负责 SSE 解帧、UTF-8 校验和有界读取,不承担风控、路由或计费。完整模块边界、验证命令和后续风险清单见 `../docs/21-api-request-modularization.md`。
|
||||
|
||||
`V49` 增加数据库共享登录窗口;所有新密码统一 8–256 位,改密撤销全部浏览器会话,登录/改密/重置通过用户行锁协调。初次创建管理员必须显式配置非默认密码;已存在管理员不会被启动配置重置。API Key 不随改密自动撤销。
|
||||
|
||||
`provider.transport.BoundedHttpTransport` 已供异步 JSON/multipart/音频/媒体读取复用:接收中限量、总期限覆盖响应体、取消订阅、不自动跟随重定向。它不等于 SSRF 已解决,DNS 预检与实际解析的绑定仍需补齐;同步/SSE 生命周期和 Codex 长请求亦未统一完成。详见 `../docs/25-auth-and-bounded-transport.md` 与 `../docs/24-security-and-cpr-task-tracker.md`。
|
||||
|
||||
Flyway 脚本位于 `src/main/resources/db/migration/`,与现有 TypeScript 服务使用同一套基础表命名,后续迁移数据时可以按表逐步切换。
|
||||
|
||||
本目录的镜像由 `infra/moyi/docker-compose.yml` 构建。切换生产流量前仍需完成真实供应商适配、Redis 限流/队列和完整集成测试;TypeScript Control Plane 继续作为迁移期间的参考实现。
|
||||
本目录的镜像由 `infra/moyi/docker-compose.yml` 构建。生产发布前应验证目标渠道能力、价格快照、Redis 准入和 PostgreSQL 集成测试;本地通过不代表线上部署验收。TypeScript Control Plane 保留为历史参考实现,不与 Java 后端同时承担同一钱包的写入。
|
||||
@@ -17,6 +17,9 @@
|
||||
|
||||
<properties>
|
||||
<java.version>21</java.version>
|
||||
<!-- Keep the new DNS-bound media transport on the matching stable Client/Core pair. -->
|
||||
<httpclient5.version>5.6.5</httpclient5.version>
|
||||
<httpcore5.version>5.4.4</httpcore5.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
@@ -53,6 +56,10 @@
|
||||
<groupId>org.springframework.security</groupId>
|
||||
<artifactId>spring-security-crypto</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.httpcomponents.client5</groupId>
|
||||
<artifactId>httpclient5</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.postgresql</groupId>
|
||||
<artifactId>postgresql</artifactId>
|
||||
|
||||
@@ -28,17 +28,20 @@ public class AuthController {
|
||||
private static final String SESSION_COOKIE = "moyi_session";
|
||||
private final AuthService authService;
|
||||
private final VerificationService verification;
|
||||
private final VerifiedAccountService verifiedAccounts;
|
||||
private final int sessionTtlHours;
|
||||
private final boolean secureCookie;
|
||||
|
||||
public AuthController(
|
||||
AuthService authService,
|
||||
VerificationService verification,
|
||||
VerifiedAccountService verifiedAccounts,
|
||||
@Value("${moyi.auth.session-ttl-hours}") int sessionTtlHours,
|
||||
@Value("${moyi.auth.secure-cookie}") boolean secureCookie
|
||||
) {
|
||||
this.authService = authService;
|
||||
this.verification = verification;
|
||||
this.verifiedAccounts = verifiedAccounts;
|
||||
this.sessionTtlHours = sessionTtlHours;
|
||||
this.secureCookie = secureCookie;
|
||||
}
|
||||
@@ -67,9 +70,8 @@ public class AuthController {
|
||||
|
||||
@PostMapping("/auth/password-reset")
|
||||
public ResponseEntity<Void> resetPassword(@RequestBody PasswordResetRequest input,HttpServletRequest request) {
|
||||
String account = verification.consumeReset(input.account(), input.verificationCode());
|
||||
authService.resetPassword(account, input.newPassword(), meta(request));
|
||||
return ResponseEntity.noContent().build();
|
||||
verifiedAccounts.resetPassword(input.account(), input.verificationCode(), input.newPassword(), meta(request));
|
||||
return ResponseEntity.noContent().header(HttpHeaders.SET_COOKIE, sessionCookie("", Duration.ZERO).toString()).build();
|
||||
}
|
||||
|
||||
public record InvitationRequest(String label, Integer maxUses, OffsetDateTime expiresAt) {}
|
||||
@@ -102,9 +104,8 @@ public class AuthController {
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response
|
||||
) {
|
||||
verification.consume(input.account(),input.verificationCode());
|
||||
AuthService.AuthResult result = authService.register(
|
||||
input.invitationCode(), input.account(), input.password(), input.displayName(), meta(request)
|
||||
AuthService.AuthResult result = verifiedAccounts.register(
|
||||
input.invitationCode(), input.account(), input.password(), input.displayName(), input.verificationCode(), meta(request)
|
||||
);
|
||||
response.addHeader(HttpHeaders.SET_COOKIE, sessionCookie(result.token(), null).toString());
|
||||
return ResponseEntity.status(HttpStatus.CREATED).body(Map.of("user", result.user()));
|
||||
@@ -123,10 +124,12 @@ public class AuthController {
|
||||
}
|
||||
|
||||
@PatchMapping("/auth/account")
|
||||
public Map<String, Object> updateAccount(@RequestBody AccountSettingsRequest input, HttpServletRequest request) {
|
||||
return Map.of("user", authService.updateAccount(
|
||||
public Map<String, Object> updateAccount(@RequestBody AccountSettingsRequest input, HttpServletRequest request, HttpServletResponse response) {
|
||||
var user = authService.updateAccount(
|
||||
sessionToken(request), input.displayName(), input.currentPassword(), input.newPassword(), meta(request)
|
||||
));
|
||||
);
|
||||
if (input.newPassword() != null) response.addHeader(HttpHeaders.SET_COOKIE, sessionCookie("", Duration.ZERO).toString());
|
||||
return Map.of("user", user);
|
||||
}
|
||||
|
||||
@GetMapping("/admin/users")
|
||||
|
||||
@@ -24,6 +24,8 @@ import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
@Service
|
||||
public class AuthService {
|
||||
@@ -34,6 +36,10 @@ public class AuthService {
|
||||
private final ObjectMapper objectMapper;
|
||||
private final int sessionTtlHours;
|
||||
private final SecretCipher secretCipher;
|
||||
private final LoginAttemptLimiter loginAttempts;
|
||||
private final TransactionTemplate loginTransactions;
|
||||
// Valid PBKDF2 shape for comparable unknown-account work; this is not a usable user credential.
|
||||
private static final String UNKNOWN_PASSWORD_HASH = "pbkdf2$210000$AAECAwQFBgcICQoLDA0ODw$_ZW4zVJDiheXQ51WuP_SSr_f8xjYJXBHvjkzLlPtFHo";
|
||||
@Autowired private TotpService totp;
|
||||
|
||||
/** Kept for isolated unit tests that do not exercise encrypted invitation display. */
|
||||
@@ -59,6 +65,8 @@ public class AuthService {
|
||||
this.objectMapper = objectMapper;
|
||||
this.sessionTtlHours = sessionTtlHours;
|
||||
this.secretCipher = secretCipher;
|
||||
this.loginAttempts = new LoginAttemptLimiter(jdbc);
|
||||
this.loginTransactions = jdbc.getDataSource() == null ? null : new TransactionTemplate(new DataSourceTransactionManager(jdbc.getDataSource()));
|
||||
}
|
||||
|
||||
public record UserView(
|
||||
@@ -91,7 +99,7 @@ public class AuthService {
|
||||
@Transactional
|
||||
public void ensureAdmin(String accountInput, String password, String displayName) {
|
||||
String account = normalizeAccount(accountInput);
|
||||
if (account.isBlank() || password == null || password.isBlank()) {
|
||||
if (account.isBlank()) {
|
||||
throw new ApiException("INVALID_ADMIN_CONFIG", "管理员初始配置无效", 500);
|
||||
}
|
||||
// Serialize bootstrap across replicas, including the first creation.
|
||||
@@ -112,6 +120,10 @@ public class AuthService {
|
||||
return;
|
||||
}
|
||||
OffsetDateTime now = now();
|
||||
PasswordPolicy.validate(password);
|
||||
if (java.util.Set.of("admin123", "generate_admin_password", "replace-with-a-strong-initial-password").contains(password.strip().toLowerCase(Locale.ROOT))) {
|
||||
throw new ApiException("INVALID_ADMIN_CONFIG", "首次启动必须配置非默认、非示例占位的管理员密码", 500);
|
||||
}
|
||||
jdbc.update(
|
||||
"INSERT INTO users (id, account, account_kind, password_hash, display_name, role, status, created_at, updated_at) VALUES (?, ?, ?, ?, ?, 'super_admin', 'active', ?, ?)",
|
||||
UUID.randomUUID(), account, accountKind(account), passwordHasher.hash(password),
|
||||
@@ -123,18 +135,36 @@ public class AuthService {
|
||||
return login(accountInput,password,null,meta);
|
||||
}
|
||||
public AuthResult login(String accountInput,String password,String otp,RequestMeta meta) {
|
||||
UserRow user = findUserByAccount(normalizeAccount(accountInput));
|
||||
if (user == null || !passwordHasher.matches(password, user.passwordHash())) {
|
||||
String account = normalizeAccount(accountInput);
|
||||
if (account.length() > 320 || password == null || password.length() > 4096) throw new ApiException("INVALID_CREDENTIALS", "账号或密码错误", 401);
|
||||
loginAttempts.check(account, meta == null ? null : meta.ipAddress());
|
||||
// Acquire the row lock only AFTER the independently committed limiter. Do not hold a pool
|
||||
// connection while asking REQUIRES_NEW for another connection under concurrent login load.
|
||||
if (loginTransactions == null) throw new IllegalStateException("Login requires a transactional data source");
|
||||
LoginOutcome outcome = loginTransactions.execute(status -> authenticateLocked(account, password, otp, meta));
|
||||
if (outcome.error() != null) throw outcome.error();
|
||||
return outcome.result();
|
||||
}
|
||||
|
||||
private record LoginOutcome(AuthResult result, ApiException error) {}
|
||||
|
||||
private LoginOutcome authenticateLocked(String account, String password, String otp, RequestMeta meta) {
|
||||
UserRow user = findUserByAccountLocked(account);
|
||||
boolean matches = passwordHasher.matches(password, user == null ? UNKNOWN_PASSWORD_HASH : user.passwordHash());
|
||||
if (user == null || !matches) {
|
||||
throw new ApiException("INVALID_CREDENTIALS", "账号或密码错误", 401);
|
||||
}
|
||||
if (!"active".equals(user.status())) {
|
||||
throw new ApiException("ACCOUNT_DISABLED", "账号已停用", 403);
|
||||
}
|
||||
if(totp!=null) totp.verifyLogin(user.id(),otp);
|
||||
if(totp!=null) {
|
||||
try { totp.verifyLogin(user.id(),otp); }
|
||||
catch (ApiException exception) { return new LoginOutcome(null, exception); } // Persist MFA failure/replay counters.
|
||||
}
|
||||
OffsetDateTime loginAt = now();
|
||||
jdbc.update("UPDATE users SET last_login_at = ?, updated_at = ? WHERE id = ?", loginAt, loginAt, user.id());
|
||||
audit("user.logged_in", user.id(), meta, Map.of());
|
||||
return new AuthResult(createSession(user.id(), meta), toView(user, loginAt));
|
||||
return new LoginOutcome(new AuthResult(createSession(user.id(), meta), toView(user, loginAt)), null);
|
||||
}
|
||||
public void verifyPassword(UUID userId,String password) {
|
||||
String hash=jdbc.queryForObject("SELECT password_hash FROM users WHERE id=? AND status='active'",String.class,userId);
|
||||
@@ -151,9 +181,7 @@ public class AuthService {
|
||||
) {
|
||||
String account = normalizeAccount(accountInput);
|
||||
validatePublicAccount(account);
|
||||
if (password == null || password.length() < 8) {
|
||||
throw new ApiException("WEAK_PASSWORD", "密码至少需要 8 位");
|
||||
}
|
||||
PasswordPolicy.validate(password);
|
||||
if (invitationCode == null || invitationCode.isBlank()) {
|
||||
throw new ApiException("INVITATION_REQUIRED", "第一版注册需要邀请码");
|
||||
}
|
||||
@@ -177,6 +205,7 @@ public class AuthService {
|
||||
}
|
||||
UUID userId = UUID.randomUUID();
|
||||
String name = displayName == null || displayName.isBlank() ? defaultDisplayName(account) : displayName.trim();
|
||||
if (name.length() > 80) throw new ApiException("INVALID_NAME", "显示名称最多 80 字符", 400);
|
||||
jdbc.update(
|
||||
"INSERT INTO users (id, account, account_kind, password_hash, display_name, role, status, created_at, updated_at) VALUES (?, ?, ?, ?, ?, 'user', 'active', ?, ?)",
|
||||
userId, account, accountKind(account), passwordHasher.hash(password), name, now, now
|
||||
@@ -242,22 +271,23 @@ public class AuthService {
|
||||
String newPassword,
|
||||
RequestMeta meta
|
||||
) {
|
||||
UserRow user = requireUserRow(token);
|
||||
UserRow authenticated = requireUserRow(token);
|
||||
jdbc.queryForList("SELECT id FROM users WHERE id=? FOR UPDATE", authenticated.id());
|
||||
UserRow user = requireUserRow(token); // Recheck after any concurrent password change revoked this session.
|
||||
String displayName = displayNameInput == null ? user.displayName() : displayNameInput.trim();
|
||||
if (displayName.isBlank() || displayName.length() > 80) {
|
||||
throw new ApiException("INVALID_NAME", "显示名称不能为空且最多 80 字符", 400);
|
||||
}
|
||||
boolean changingPassword = newPassword != null && !newPassword.isBlank();
|
||||
boolean changingPassword = newPassword != null;
|
||||
if (changingPassword) {
|
||||
if (currentPassword == null || currentPassword.isBlank() || !passwordHasher.matches(currentPassword, user.passwordHash())) {
|
||||
throw new ApiException("INVALID_CURRENT_PASSWORD", "当前密码错误", 403);
|
||||
}
|
||||
if (newPassword.length() > 256 || (!"super_admin".equals(user.role()) && newPassword.length() < 8)) {
|
||||
throw new ApiException("WEAK_PASSWORD", "普通用户新密码长度为 8 至 256 位", 400);
|
||||
}
|
||||
PasswordPolicy.validate(newPassword);
|
||||
}
|
||||
if (changingPassword) {
|
||||
jdbc.update("UPDATE users SET display_name = ?, password_hash = ?, updated_at = ? WHERE id = ?", displayName, passwordHasher.hash(newPassword), now(), user.id());
|
||||
jdbc.update("UPDATE auth_sessions SET revoked_at=? WHERE user_id=? AND revoked_at IS NULL", now(), user.id());
|
||||
} else {
|
||||
jdbc.update("UPDATE users SET display_name = ?, updated_at = ? WHERE id = ?", displayName, now(), user.id());
|
||||
}
|
||||
@@ -268,14 +298,11 @@ public class AuthService {
|
||||
@Transactional
|
||||
public void resetPassword(String accountInput, String newPassword, RequestMeta meta) {
|
||||
String account = normalizeAccount(accountInput);
|
||||
UserRow user = findUserByAccount(account);
|
||||
UserRow user = findUserByAccountLocked(account);
|
||||
if (user == null || !"active".equals(user.status())) {
|
||||
throw new ApiException("ACCOUNT_NOT_FOUND", "账号不存在或已停用", 404);
|
||||
}
|
||||
if (newPassword == null || newPassword.isBlank() || newPassword.length() > 256
|
||||
|| (!"super_admin".equals(user.role()) && newPassword.length() < 8)) {
|
||||
throw new ApiException("WEAK_PASSWORD", "普通用户新密码长度为 8 至 256 位", 400);
|
||||
}
|
||||
PasswordPolicy.validate(newPassword);
|
||||
OffsetDateTime changedAt = now();
|
||||
jdbc.update("UPDATE users SET password_hash=?, updated_at=? WHERE id=?", passwordHasher.hash(newPassword), changedAt, user.id());
|
||||
jdbc.update("UPDATE auth_sessions SET revoked_at=? WHERE user_id=? AND revoked_at IS NULL", changedAt, user.id());
|
||||
@@ -312,7 +339,7 @@ public class AuthService {
|
||||
UserView actor=requireAdmin(token);
|
||||
String account=normalizeAccount(accountInput);
|
||||
if(account.isBlank() || account.length()>254 || (!EMAIL.matcher(account).matches() && !PHONE.matcher(account).matches() && !account.matches("[a-z0-9_]{3,64}"))) throw new ApiException("INVALID_ACCOUNT","请输入有效邮箱、手机号或 3 至 64 位字母数字下划线用户名");
|
||||
if(password==null || password.length()<8 || password.length()>256) throw new ApiException("WEAK_PASSWORD","普通用户密码长度为 8 至 256 位");
|
||||
PasswordPolicy.validate(password);
|
||||
if(findUserByAccount(account)!=null) throw new ApiException("ACCOUNT_EXISTS","账号已存在",409);
|
||||
String name=displayName==null || displayName.isBlank()?defaultDisplayName(account):displayName.trim();
|
||||
if(name.length()>80) throw new ApiException("INVALID_NAME","显示名称最多 80 字符");
|
||||
@@ -446,6 +473,11 @@ public class AuthService {
|
||||
}
|
||||
}
|
||||
|
||||
private UserRow findUserByAccountLocked(String account) {
|
||||
try { return jdbc.queryForObject("SELECT * FROM users WHERE account=? FOR UPDATE", this::mapUser, account); }
|
||||
catch (EmptyResultDataAccessException exception) { return null; }
|
||||
}
|
||||
|
||||
private UserRow findUserById(UUID id) {
|
||||
return jdbc.queryForObject("SELECT * FROM users WHERE id = ?", this::mapUser, id);
|
||||
}
|
||||
@@ -511,13 +543,13 @@ public class AuthService {
|
||||
}
|
||||
|
||||
private void validatePublicAccount(String account) {
|
||||
if (!EMAIL.matcher(account).matches() && !PHONE.matcher(account).matches()) {
|
||||
if (account.length() > 254 || (!EMAIL.matcher(account).matches() && !PHONE.matcher(account).matches())) {
|
||||
throw new ApiException("INVALID_ACCOUNT", "请输入有效的邮箱或手机号");
|
||||
}
|
||||
}
|
||||
|
||||
private String normalizeAccount(String account) {
|
||||
String normalized = account == null ? "" : account.trim().toLowerCase();
|
||||
String normalized = account == null ? "" : account.trim().toLowerCase(Locale.ROOT);
|
||||
return normalized.contains("@") ? normalized : normalized.replaceAll("[\\s-]", "");
|
||||
}
|
||||
|
||||
|
||||
+8
-4
@@ -26,6 +26,11 @@ public class CloudVerificationSender implements VerificationSender {
|
||||
|
||||
@Override
|
||||
public void send(String channel,String account,String code) {
|
||||
send(channel,account,code,VerificationPurpose.REGISTRATION);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void send(String channel,String account,String code,VerificationPurpose purpose) {
|
||||
if(!available(channel)) throw new ApiException("VERIFICATION_UNAVAILABLE","该验证码发送服务尚未配置,请联系管理员",503);
|
||||
try {
|
||||
if("email".equals(channel)) {
|
||||
@@ -47,8 +52,8 @@ public class CloudVerificationSender implements VerificationSender {
|
||||
SimpleMailMessage message=new SimpleMailMessage();
|
||||
message.setFrom(value("MOYI_SMTP_FROM"));
|
||||
message.setTo(account);
|
||||
message.setSubject("MOYI 注册验证码");
|
||||
message.setText("您的注册验证码为:"+code+",5 分钟内有效。请勿向他人泄露。若非本人操作,请忽略此邮件。");
|
||||
message.setSubject("MOYI "+purpose.label+"验证码");
|
||||
message.setText("您的"+purpose.label+"验证码为:"+code+",5 分钟内有效。请勿向他人泄露。若非本人操作,请忽略此邮件。");
|
||||
mail.send(message);
|
||||
} else {
|
||||
Config config=new Config().setAccessKeyId(value("MOYI_SMS_ACCESS_KEY_ID")).setAccessKeySecret(value("MOYI_SMS_ACCESS_KEY_SECRET")).setEndpoint("dysmsapi.aliyuncs.com").setProtocol("https");
|
||||
@@ -57,8 +62,7 @@ public class CloudVerificationSender implements VerificationSender {
|
||||
.setSignName(value("MOYI_SMS_SIGN_NAME")).setTemplateCode(value("MOYI_SMS_TEMPLATE_CODE")).setTemplateParam("{\"code\":\""+code+"\"}");
|
||||
var response=client.sendSmsWithOptions(request,new RuntimeOptions().setAutoretry(false).setConnectTimeout(5000).setReadTimeout(10000));
|
||||
if(response.getBody()==null || !"OK".equals(response.getBody().getCode())) {
|
||||
String resultCode = response.getBody() == null ? "EMPTY_RESPONSE" : String.valueOf(response.getBody().getCode());
|
||||
log.warn("Aliyun SMS verification rejected: resultCode={}", resultCode);
|
||||
log.warn("Aliyun SMS verification rejected");
|
||||
throw new IllegalStateException("SMS rejected");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.moyi.controlplane.auth;
|
||||
|
||||
import com.moyi.controlplane.gateway.GatewayLimitException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
/** Bounds expensive password work for known AND unknown users; counters survive failed-login rollbacks. */
|
||||
final class LoginAttemptLimiter {
|
||||
private final JdbcTemplate jdbc;
|
||||
private final TransactionTemplate transactions;
|
||||
|
||||
LoginAttemptLimiter(JdbcTemplate jdbc) {
|
||||
this.jdbc = jdbc;
|
||||
if (jdbc.getDataSource() == null) { transactions = null; return; } // Isolated mock-only constructors.
|
||||
transactions = new TransactionTemplate(new DataSourceTransactionManager(jdbc.getDataSource()));
|
||||
transactions.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
|
||||
}
|
||||
|
||||
void check(String normalizedAccount, String sourceIp) {
|
||||
if (transactions == null) throw new IllegalStateException("Login limiter requires a transactional data source");
|
||||
int retry = transactions.execute(status -> {
|
||||
var limits = Map.of(AuthService.hashSecret("login:account:" + normalizedAccount), 10,
|
||||
AuthService.hashSecret("login:source:" + (sourceIp == null || sourceIp.isBlank() ? "unknown" : sourceIp)), 50);
|
||||
int remaining = 0;
|
||||
// Stable ordering prevents cross-account/source deadlocks on concurrent replicas.
|
||||
for (String bucket : limits.keySet().stream().sorted().toList()) {
|
||||
int limit = limits.get(bucket);
|
||||
var row = jdbc.queryForMap("INSERT INTO auth_login_windows(bucket_hash,attempts) VALUES (?,1) ON CONFLICT(bucket_hash) DO UPDATE SET attempts=CASE WHEN auth_login_windows.expires_at<=clock_timestamp() THEN 1 ELSE LEAST(auth_login_windows.attempts+1,?) END,window_started_at=CASE WHEN auth_login_windows.expires_at<=clock_timestamp() THEN clock_timestamp() ELSE auth_login_windows.window_started_at END,expires_at=CASE WHEN auth_login_windows.expires_at<=clock_timestamp() THEN clock_timestamp()+interval '60 seconds' ELSE auth_login_windows.expires_at END RETURNING attempts,GREATEST(1,CEIL(EXTRACT(EPOCH FROM (expires_at-clock_timestamp()))))::integer AS retry", bucket, limit + 1);
|
||||
if (((Number) row.get("attempts")).intValue() > limit) remaining = Math.max(remaining, ((Number) row.get("retry")).intValue());
|
||||
}
|
||||
jdbc.update("DELETE FROM auth_login_windows WHERE bucket_hash IN (SELECT bucket_hash FROM auth_login_windows WHERE expires_at<clock_timestamp()-interval '5 minutes' LIMIT 500)");
|
||||
return remaining;
|
||||
});
|
||||
if (retry > 0) throw new GatewayLimitException("AUTH_RATE_LIMITED", "登录尝试过于频繁,请稍后重试", 429, retry);
|
||||
}
|
||||
}
|
||||
@@ -30,13 +30,17 @@ public class PasswordHasher {
|
||||
|
||||
public boolean matches(String password, String encoded) {
|
||||
try {
|
||||
// Accept existing bounded legacy passwords, but never let malformed hashes choose unbounded KDF work.
|
||||
if (password == null || password.length() > 4096 || encoded == null || encoded.length() > 1024) return false;
|
||||
String[] parts = encoded.split("\\$");
|
||||
if (parts.length != 4 || !"pbkdf2".equals(parts[0])) {
|
||||
return false;
|
||||
}
|
||||
int iterations = Integer.parseInt(parts[1]);
|
||||
if (iterations < 10_000 || iterations > 1_000_000) return false;
|
||||
byte[] salt = Base64.getUrlDecoder().decode(parts[2]);
|
||||
byte[] expected = Base64.getUrlDecoder().decode(parts[3]);
|
||||
if (salt.length < 16 || salt.length > 64 || expected.length != KEY_LENGTH / 8) return false;
|
||||
byte[] actual = derive(password, salt, iterations, expected.length * 8);
|
||||
return MessageDigest.isEqual(expected, actual);
|
||||
} catch (RuntimeException exception) {
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.moyi.controlplane.auth;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
|
||||
/** One issuance policy for every role; do not silently trim or truncate passwords. */
|
||||
public final class PasswordPolicy {
|
||||
private PasswordPolicy() {}
|
||||
public static void validate(String password) {
|
||||
if (password == null || password.isBlank() || password.length() < 8 || password.length() > 256) {
|
||||
throw new ApiException("WEAK_PASSWORD", "所有账号的新密码长度须为 8 至 256 位,不能仅含空白", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
package com.moyi.controlplane.auth;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.security.SecretCipher;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.RejectedExecutionException;
|
||||
import java.util.concurrent.SynchronousQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
/** Durable, encrypted single-attempt delivery. Never hold a DB transaction over a mail/SMS call. */
|
||||
@Service
|
||||
public class VerificationDispatchService {
|
||||
private static final Logger log = LoggerFactory.getLogger(VerificationDispatchService.class);
|
||||
private final JdbcTemplate jdbc;
|
||||
private final VerificationSender sender;
|
||||
private final SecretCipher cipher;
|
||||
private final ObjectMapper json;
|
||||
private final TransactionTemplate transactions;
|
||||
private final ExecutorService workers = new ThreadPoolExecutor(0, 2, 30, TimeUnit.SECONDS, new SynchronousQueue<>(),
|
||||
task -> { var thread=new Thread(task, "verification-delivery"); thread.setDaemon(true); return thread; },
|
||||
new ThreadPoolExecutor.AbortPolicy());
|
||||
|
||||
public VerificationDispatchService(JdbcTemplate jdbc, VerificationSender sender, SecretCipher cipher,
|
||||
ObjectMapper json, PlatformTransactionManager manager) {
|
||||
this.jdbc=jdbc; this.sender=sender; this.cipher=cipher; this.json=json;
|
||||
transactions=new TransactionTemplate(manager);
|
||||
transactions.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
|
||||
}
|
||||
|
||||
public boolean available(String channel) { return cipher.isConfigured() && sender.available(channel); }
|
||||
|
||||
/** Called only after common quotas; code generation and queue insert commit or roll back together. */
|
||||
@Transactional(propagation=Propagation.MANDATORY)
|
||||
public void enqueue(VerificationPurpose purpose, String account, String code, String codeHash) {
|
||||
UUID id=UUID.randomUUID();
|
||||
String payload;
|
||||
try { payload=cipher.encrypt(json.writeValueAsString(Map.of("id",id.toString(),"purpose",purpose.key,"account",account,"code",code))); }
|
||||
catch (ApiException error) { throw error; }
|
||||
catch (Exception error) { throw new ApiException("VERIFICATION_QUEUE_FAILED", "验证码服务暂时不可用", 503); }
|
||||
jdbc.update("INSERT INTO "+purpose.table+"(account,code_hash,expires_at,delivery_id) VALUES (?,?,clock_timestamp()+interval '5 minutes',?) ON CONFLICT(account) DO UPDATE SET code_hash=excluded.code_hash,expires_at=excluded.expires_at,attempts=0,consumed=false,created_at=clock_timestamp(),delivery_id=excluded.delivery_id", account,codeHash,id);
|
||||
jdbc.update("INSERT INTO verification_delivery_outbox(id,purpose,payload_ciphertext) VALUES (?,?,?)",id,purpose.key,payload);
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelayString="${moyi.auth.verification-dispatch-interval-ms:1000}")
|
||||
public void dispatchPending() {
|
||||
// No queue behind these two workers: a slow provider cannot consume unbounded threads/tasks.
|
||||
for (int i=0;i<2;i++) {
|
||||
try { workers.submit(() -> {
|
||||
try { dispatchOne(); }
|
||||
catch (RuntimeException error) { log.warn("Verification delivery persistence failed; no automatic replay"); }
|
||||
}); } catch (RejectedExecutionException busyOrStopping) { break; }
|
||||
}
|
||||
}
|
||||
|
||||
private record Attempt(UUID id, VerificationPurpose purpose, String ciphertext) {
|
||||
@Override public String toString() { return "VerificationAttempt[id="+id+"]"; }
|
||||
}
|
||||
|
||||
/** Package-visible for isolated deterministic database tests; not an HTTP operation. */
|
||||
boolean dispatchOne() {
|
||||
Attempt attempt=transactions.execute(status -> {
|
||||
var rows=jdbc.queryForList("SELECT id,purpose,payload_ciphertext FROM verification_delivery_outbox WHERE status='queued' AND expires_at>clock_timestamp() ORDER BY created_at,id LIMIT 1 FOR UPDATE SKIP LOCKED");
|
||||
if (rows.isEmpty()) return null;
|
||||
var row=rows.getFirst(); UUID id=(UUID)row.get("id");
|
||||
jdbc.update("UPDATE verification_delivery_outbox SET status='sending',started_at=clock_timestamp() WHERE id=?",id);
|
||||
return new Attempt(id,VerificationPurpose.fromKey(String.valueOf(row.get("purpose"))),String.valueOf(row.get("payload_ciphertext")));
|
||||
});
|
||||
if (attempt==null) { cleanupExpired(); return false; }
|
||||
try {
|
||||
var payload=json.readTree(cipher.decrypt(attempt.ciphertext()));
|
||||
if (!attempt.id().toString().equals(payload.path("id").asText())
|
||||
|| !attempt.purpose().key.equals(payload.path("purpose").asText())
|
||||
|| !payload.path("code").asText().matches("[0-9]{6}")) throw new IllegalArgumentException("Invalid delivery envelope");
|
||||
String account=payload.path("account").asText(), code=payload.path("code").asText();
|
||||
if (!isCurrent(attempt,account)) { finish(attempt,"discarded","SUPERSEDED_OR_INACTIVE"); return true; }
|
||||
if (Thread.currentThread().isInterrupted()) { finish(attempt,"failed","INTERRUPTED"); return true; }
|
||||
String channel=account.contains("@")?"email":"phone";
|
||||
// At this point the claim is committed and its connection is released.
|
||||
sender.send(channel,account,code,attempt.purpose());
|
||||
} catch (Exception error) {
|
||||
// Do not attach/log exceptions: providers can echo recipients, OTPs or credentials.
|
||||
finish(attempt,"failed","DELIVERY_FAILED");
|
||||
return true;
|
||||
}
|
||||
// A storage failure here leaves 'sending'. Never re-send an ambiguously accepted message.
|
||||
finish(attempt,"sent",null);
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean isCurrent(Attempt attempt, String account) {
|
||||
String activeUser=attempt.purpose()==VerificationPurpose.PASSWORD_RESET
|
||||
? " AND EXISTS(SELECT 1 FROM users WHERE account=c.account AND status='active')" : "";
|
||||
return Boolean.TRUE.equals(jdbc.queryForObject("SELECT EXISTS(SELECT 1 FROM "+attempt.purpose().table+" c WHERE account=? AND delivery_id=? AND consumed=false AND attempts<5 AND expires_at>clock_timestamp()"+activeUser+")",Boolean.class,account,attempt.id()));
|
||||
}
|
||||
|
||||
private void finish(Attempt attempt, String state, String failureCode) {
|
||||
transactions.executeWithoutResult(status -> {
|
||||
int changed=jdbc.update("UPDATE verification_delivery_outbox SET status=?,payload_ciphertext=NULL,finished_at=clock_timestamp(),failure_code=? WHERE id=? AND status='sending'",state,failureCode,attempt.id());
|
||||
if (changed>0 && !"sent".equals(state)) {
|
||||
jdbc.update("UPDATE "+attempt.purpose().table+" SET consumed=true WHERE delivery_id=?",attempt.id());
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void cleanupExpired() {
|
||||
transactions.executeWithoutResult(status -> {
|
||||
// 'sending' abandoned on restart is never retried. Erase the encrypted payload after expiry.
|
||||
jdbc.update("UPDATE verification_delivery_outbox SET status='discarded',payload_ciphertext=NULL,finished_at=clock_timestamp(),failure_code='EXPIRED' WHERE id IN (SELECT id FROM verification_delivery_outbox WHERE status IN ('queued','sending') AND expires_at<=clock_timestamp() LIMIT 100 FOR UPDATE SKIP LOCKED)");
|
||||
jdbc.update("DELETE FROM verification_delivery_outbox WHERE id IN (SELECT id FROM verification_delivery_outbox WHERE finished_at<clock_timestamp()-interval '2 days' LIMIT 100)");
|
||||
for (var purpose : VerificationPurpose.values()) jdbc.update("DELETE FROM "+purpose.table+" WHERE account IN (SELECT account FROM "+purpose.table+" WHERE expires_at<clock_timestamp()-interval '1 day' LIMIT 100 FOR UPDATE SKIP LOCKED)");
|
||||
});
|
||||
}
|
||||
|
||||
@PreDestroy public void close() {
|
||||
workers.shutdownNow();
|
||||
try { workers.awaitTermination(3,TimeUnit.SECONDS); }
|
||||
catch (InterruptedException error) { Thread.currentThread().interrupt(); }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.moyi.controlplane.auth;
|
||||
|
||||
/** Closed purpose set: SQL identifiers and message intent must never come from a request. */
|
||||
public enum VerificationPurpose {
|
||||
REGISTRATION("registration", "registration_codes", "注册"),
|
||||
PASSWORD_RESET("password-reset", "password_reset_codes", "密码重置");
|
||||
|
||||
final String key, table, label;
|
||||
VerificationPurpose(String key, String table, String label) { this.key=key; this.table=table; this.label=label; }
|
||||
static VerificationPurpose fromKey(String key) {
|
||||
for (var purpose : values()) if (purpose.key.equals(key)) return purpose;
|
||||
throw new IllegalArgumentException("Invalid verification purpose");
|
||||
}
|
||||
}
|
||||
@@ -3,4 +3,5 @@ package com.moyi.controlplane.auth;
|
||||
public interface VerificationSender {
|
||||
boolean available(String channel);
|
||||
void send(String channel, String account, String code);
|
||||
default void send(String channel, String account, String code, VerificationPurpose purpose) { send(channel,account,code); }
|
||||
}
|
||||
+35
-41
@@ -4,7 +4,6 @@ import com.moyi.controlplane.common.ApiException;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.SecureRandom;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
@@ -16,13 +15,13 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
@Service
|
||||
public class VerificationService {
|
||||
private final JdbcTemplate jdbc;
|
||||
private final VerificationSender sender;
|
||||
private final VerificationDispatchService dispatch;
|
||||
private final String pepper;
|
||||
private final SecureRandom random=new SecureRandom();
|
||||
public VerificationService(JdbcTemplate jdbc,VerificationSender sender,@Value("${moyi.auth.password-pepper}") String pepper) {
|
||||
this.jdbc=jdbc; this.sender=sender; this.pepper=pepper;
|
||||
public VerificationService(JdbcTemplate jdbc,VerificationDispatchService dispatch,@Value("${moyi.auth.password-pepper}") String pepper) {
|
||||
this.jdbc=jdbc; this.dispatch=dispatch; this.pepper=pepper;
|
||||
}
|
||||
public Map<String,Boolean> capabilities() { return Map.of("email",sender.available("email"),"phone",sender.available("phone")); }
|
||||
public Map<String,Boolean> capabilities() { return Map.of("email",dispatch.available("email"),"phone",dispatch.available("phone")); }
|
||||
public String normalize(String raw) {
|
||||
String account=raw==null?"":raw.trim().toLowerCase(Locale.ROOT);
|
||||
if(!account.contains("@")) account=account.replaceAll("[\\s-]","");
|
||||
@@ -33,29 +32,33 @@ public class VerificationService {
|
||||
private String registrationDigest(String account,String code) { return digest("registration",account,code); }
|
||||
private String resetDigest(String account,String code) { return digest("password-reset",account,code); }
|
||||
|
||||
@Transactional(noRollbackFor=ApiException.class)
|
||||
@Transactional
|
||||
public Map<String,Object> send(String raw,String invitation,String ip) {
|
||||
String account=normalize(raw), channel=account.contains("@")?"email":"phone";
|
||||
if(!sender.available(channel)) throw new ApiException("VERIFICATION_UNAVAILABLE","该验证码发送服务尚未配置,请联系管理员",503);
|
||||
if(!dispatch.available(channel)) throw new ApiException("VERIFICATION_UNAVAILABLE","该验证码发送服务尚未配置,请联系管理员",503);
|
||||
Integer invites=jdbc.queryForObject("SELECT count(*) FROM invitation_codes WHERE code_hash=? AND disabled_at IS NULL AND used_count<max_uses AND (expires_at IS NULL OR expires_at>now())",Integer.class,AuthService.hashSecret(invitation==null?"":invitation.trim()));
|
||||
if(invites==null || invites==0) throw new ApiException("INVITATION_INVALID","请先填写有效邀请码",403);
|
||||
String source=ip==null?"unknown":ip;
|
||||
// One global lock bounds aggregate sending and serializes account/IP limits across replicas.
|
||||
jdbc.queryForList("SELECT pg_advisory_xact_lock(734092615)");
|
||||
Integer limited=jdbc.queryForObject("SELECT count(*) FROM verification_send_events WHERE account=? AND created_at>now()-interval '60 seconds'",Integer.class,account);
|
||||
Integer daily=jdbc.queryForObject("SELECT count(*) FROM verification_send_events WHERE account=? AND created_at>now()-interval '24 hours'",Integer.class,account);
|
||||
Integer hourly=jdbc.queryForObject("SELECT count(*) FROM verification_send_events WHERE ip_address=? AND created_at>now()-interval '1 hour'",Integer.class,source);
|
||||
Integer total=jdbc.queryForObject("SELECT count(*) FROM verification_send_events WHERE created_at>now()-interval '24 hours'",Integer.class);
|
||||
if(limited>0 || daily>=10 || hourly>=30 || total>=1000) throw new ApiException("VERIFICATION_RATE_LIMIT","发送过于频繁,请稍后重试",429);
|
||||
admitSend(account,ip);
|
||||
String code=String.format(Locale.ROOT,"%06d",random.nextInt(1000000));
|
||||
jdbc.update("INSERT INTO verification_send_events(id,account,ip_address) VALUES (?,?,?)",UUID.randomUUID(),account,source);
|
||||
jdbc.update("INSERT INTO registration_codes(account,code_hash,expires_at) VALUES (?,?,now()+interval '5 minutes') ON CONFLICT(account) DO UPDATE SET code_hash=excluded.code_hash, expires_at=excluded.expires_at, attempts=0, consumed=false, created_at=now()",account,registrationDigest(account,code));
|
||||
try { sender.send(channel,account,code); }
|
||||
catch(ApiException exception) { jdbc.update("UPDATE registration_codes SET consumed=true WHERE account=?",account); throw exception; }
|
||||
jdbc.update("DELETE FROM verification_send_events WHERE created_at<now()-interval '2 days'");
|
||||
return Map.of("sent",true,"retryAfterSeconds",60,"expiresInSeconds",300);
|
||||
dispatch.enqueue(VerificationPurpose.REGISTRATION,account,code,registrationDigest(account,code));
|
||||
return accepted();
|
||||
}
|
||||
|
||||
private void admitSend(String account,String ip) {
|
||||
String source=ip==null || ip.isBlank()?"unknown":ip;
|
||||
// This lock covers only bounded DB work, NEVER the mail/SMS provider.
|
||||
jdbc.queryForObject("SELECT set_config('lock_timeout','2s',true)",String.class);
|
||||
jdbc.queryForList("SELECT pg_advisory_xact_lock(734092615)");
|
||||
jdbc.update("DELETE FROM verification_send_events WHERE id IN (SELECT id FROM verification_send_events WHERE created_at<clock_timestamp()-interval '2 days' LIMIT 500)");
|
||||
Integer limited=jdbc.queryForObject("SELECT count(*) FROM verification_send_events WHERE account=? AND created_at>clock_timestamp()-interval '60 seconds'",Integer.class,account);
|
||||
Integer daily=jdbc.queryForObject("SELECT count(*) FROM verification_send_events WHERE account=? AND created_at>clock_timestamp()-interval '24 hours'",Integer.class,account);
|
||||
Integer hourly=jdbc.queryForObject("SELECT count(*) FROM verification_send_events WHERE ip_address=? AND created_at>clock_timestamp()-interval '1 hour'",Integer.class,source);
|
||||
Integer total=jdbc.queryForObject("SELECT count(*) FROM verification_send_events WHERE created_at>clock_timestamp()-interval '24 hours'",Integer.class);
|
||||
if(limited>0 || daily>=10 || hourly>=30 || total>=1000) throw new ApiException("VERIFICATION_RATE_LIMIT","发送过于频繁,请稍后重试",429);
|
||||
jdbc.update("INSERT INTO verification_send_events(id,account,ip_address,created_at) VALUES (?,?,?,clock_timestamp())",UUID.randomUUID(),account,source);
|
||||
}
|
||||
private Map<String,Object> accepted() { return Map.of("sent",true,"retryAfterSeconds",60,"expiresInSeconds",300); }
|
||||
|
||||
@Transactional(noRollbackFor=ApiException.class)
|
||||
public void consume(String raw,String code) {
|
||||
String account=normalize(raw);
|
||||
@@ -69,39 +72,30 @@ public class VerificationService {
|
||||
jdbc.update("UPDATE registration_codes SET consumed=true WHERE account=?",account);
|
||||
}
|
||||
|
||||
@Transactional(noRollbackFor=ApiException.class)
|
||||
@Transactional
|
||||
public Map<String,Object> sendReset(String raw,String ip) {
|
||||
String account=normalize(raw), channel=account.contains("@")?"email":"phone";
|
||||
if(!sender.available(channel)) throw new ApiException("VERIFICATION_UNAVAILABLE","该验证码发送服务尚未配置,请联系管理员",503);
|
||||
Integer exists=jdbc.queryForObject("SELECT count(*) FROM users WHERE account=? AND status='active'",Integer.class,account);
|
||||
// Do not reveal whether an account exists. The client receives the same response either way.
|
||||
if(exists==null || exists==0) return Map.of("sent",true,"retryAfterSeconds",60,"expiresInSeconds",300);
|
||||
String source=ip==null?"unknown":ip;
|
||||
jdbc.queryForList("SELECT pg_advisory_xact_lock(734092615)");
|
||||
Integer limited=jdbc.queryForObject("SELECT count(*) FROM verification_send_events WHERE account=? AND created_at>now()-interval '60 seconds'",Integer.class,account);
|
||||
Integer daily=jdbc.queryForObject("SELECT count(*) FROM verification_send_events WHERE account=? AND created_at>now()-interval '24 hours'",Integer.class,account);
|
||||
Integer hourly=jdbc.queryForObject("SELECT count(*) FROM verification_send_events WHERE ip_address=? AND created_at>now()-interval '1 hour'",Integer.class,source);
|
||||
Integer total=jdbc.queryForObject("SELECT count(*) FROM verification_send_events WHERE created_at>now()-interval '24 hours'",Integer.class);
|
||||
if(limited>0 || daily>=10 || hourly>=30 || total>=1000) throw new ApiException("VERIFICATION_RATE_LIMIT","发送过于频繁,请稍后重试",429);
|
||||
if(!dispatch.available(channel)) throw new ApiException("VERIFICATION_UNAVAILABLE","该验证码发送服务尚未配置,请联系管理员",503);
|
||||
// Exact same request-side DB/crypto path for active, unknown and disabled accounts.
|
||||
// Existence is checked ONLY in the worker; no network result reaches this receipt.
|
||||
admitSend(account,ip);
|
||||
String code=String.format(Locale.ROOT,"%06d",random.nextInt(1000000));
|
||||
jdbc.update("INSERT INTO verification_send_events(id,account,ip_address) VALUES (?,?,?)",UUID.randomUUID(),account,source);
|
||||
jdbc.update("INSERT INTO password_reset_codes(account,code_hash,expires_at) VALUES (?,?,now()+interval '5 minutes') ON CONFLICT(account) DO UPDATE SET code_hash=excluded.code_hash, expires_at=excluded.expires_at, attempts=0, consumed=false, created_at=now()",account,resetDigest(account,code));
|
||||
try { sender.send(channel,account,code); }
|
||||
catch(ApiException exception) { jdbc.update("UPDATE password_reset_codes SET consumed=true WHERE account=?",account); throw exception; }
|
||||
return Map.of("sent",true,"retryAfterSeconds",60,"expiresInSeconds",300);
|
||||
dispatch.enqueue(VerificationPurpose.PASSWORD_RESET,account,code,resetDigest(account,code));
|
||||
return accepted();
|
||||
}
|
||||
|
||||
@Transactional(noRollbackFor=ApiException.class)
|
||||
public String consumeReset(String raw,String code) {
|
||||
String account=normalize(raw);
|
||||
var rows=jdbc.queryForList("SELECT * FROM password_reset_codes WHERE account=? FOR UPDATE",account);
|
||||
if(rows.isEmpty()) throw new ApiException("VERIFICATION_INVALID","请先获取验证码");
|
||||
if(rows.isEmpty()) throw invalidResetCode();
|
||||
var row=rows.getFirst();
|
||||
java.time.Instant expiry=((java.sql.Timestamp)row.get("expires_at")).toInstant();
|
||||
if(Boolean.TRUE.equals(row.get("consumed")) || expiry.isBefore(java.time.Instant.now()) || ((Number)row.get("attempts")).intValue()>=5) throw new ApiException("VERIFICATION_EXPIRED","验证码已失效,请重新获取");
|
||||
if(Boolean.TRUE.equals(row.get("consumed")) || !expiry.isAfter(java.time.Instant.now()) || ((Number)row.get("attempts")).intValue()>=5) throw invalidResetCode();
|
||||
jdbc.update("UPDATE password_reset_codes SET attempts=attempts+1 WHERE account=?",account);
|
||||
if(code==null || !code.matches("[0-9]{6}") || !MessageDigest.isEqual(String.valueOf(row.get("code_hash")).getBytes(StandardCharsets.UTF_8),resetDigest(account,code).getBytes(StandardCharsets.UTF_8))) throw new ApiException("VERIFICATION_INVALID","验证码错误");
|
||||
if(code==null || !code.matches("[0-9]{6}") || !MessageDigest.isEqual(String.valueOf(row.get("code_hash")).getBytes(StandardCharsets.UTF_8),resetDigest(account,code).getBytes(StandardCharsets.UTF_8))) throw invalidResetCode();
|
||||
jdbc.update("UPDATE password_reset_codes SET consumed=true WHERE account=?",account);
|
||||
return account;
|
||||
}
|
||||
private ApiException invalidResetCode() { return new ApiException("VERIFICATION_INVALID","验证码无效或已失效,请重新获取"); }
|
||||
}
|
||||
+60
@@ -0,0 +1,60 @@
|
||||
package com.moyi.controlplane.auth;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.TransactionDefinition;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
/** Public registration/recovery transaction boundary. Never call these flows inside a business transaction. */
|
||||
@Service
|
||||
public class VerifiedAccountService {
|
||||
private final AuthService auth;
|
||||
private final VerificationService verification;
|
||||
private final TransactionTemplate transactions;
|
||||
|
||||
public VerifiedAccountService(AuthService auth, VerificationService verification, PlatformTransactionManager manager) {
|
||||
this.auth = auth;
|
||||
this.verification = verification;
|
||||
transactions = new TransactionTemplate(manager);
|
||||
// Own the public operation; a caller rollback cannot erase committed incorrect-code counters.
|
||||
transactions.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
|
||||
}
|
||||
|
||||
public AuthService.AuthResult register(String invitation, String account, String password, String displayName,
|
||||
String code, AuthService.RequestMeta meta) {
|
||||
PasswordPolicy.validate(password);
|
||||
return complete(() -> { verification.consume(account, code); return account; },
|
||||
verified -> auth.register(invitation, verified, password, displayName, meta));
|
||||
}
|
||||
|
||||
public void resetPassword(String account, String code, String password, AuthService.RequestMeta meta) {
|
||||
PasswordPolicy.validate(password);
|
||||
complete(() -> verification.consumeReset(account, code), verified -> {
|
||||
auth.resetPassword(verified, password, meta);
|
||||
return Boolean.TRUE;
|
||||
});
|
||||
}
|
||||
|
||||
private record Completion<T>(T result, ApiException rejectedCode) {}
|
||||
|
||||
private <T> T complete(Supplier<String> consume, Function<String, T> changeAccount) {
|
||||
Completion<T> outcome = transactions.execute(status -> {
|
||||
String account;
|
||||
try { account = consume.get(); }
|
||||
catch (ApiException rejected) {
|
||||
// VerificationService intentionally commits wrong-code attempts. Return the rejection,
|
||||
// then throw AFTER commit. No account mutation has run at this point.
|
||||
return new Completion<T>(null, rejected);
|
||||
}
|
||||
// Do not catch business/audit/storage failures here: roll back successful code consumption,
|
||||
// invitation redemption, password/session changes and audit as one operation.
|
||||
return new Completion<>(changeAccount.apply(account), null);
|
||||
});
|
||||
if (outcome == null) throw new IllegalStateException("Verified account transaction returned no outcome");
|
||||
if (outcome.rejectedCode() != null) throw outcome.rejectedCode();
|
||||
return outcome.result();
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -30,7 +30,7 @@ public class CatalogController {
|
||||
@GetMapping("/models")
|
||||
public Map<String, Object> models(jakarta.servlet.http.HttpServletRequest request) {
|
||||
authService.requireUser(AuthController.sessionToken(request));
|
||||
return Map.of("providers", catalogService.listProviders(false), "models", catalogService.listModels(false), "logicalModels", catalogService.listDefinitions(false));
|
||||
return Map.of("providers", catalogService.listPublicProviders(), "models", catalogService.listModels(false), "logicalModels", catalogService.listDefinitions(false));
|
||||
}
|
||||
|
||||
@GetMapping("/overview")
|
||||
|
||||
@@ -149,6 +149,12 @@ public class CatalogService {
|
||||
boolean publishingChannel = !"active".equals(previous.get("status")) && "active".equals(nextStatus);
|
||||
jdbc.update("UPDATE model_providers SET code=?, name=?, protocol=?, source_type=?, base_url=?, description=?, credential_type=?, status=?, updated_at=now() WHERE id=?", code,name,protocol,sourceType,baseUrl,optional(value,"description",""),credentialType,nextStatus,id);
|
||||
String secret=optional(input,"credentialSecret","");
|
||||
// A stored credential is bound to the endpoint it was entered for. Redirecting it elsewhere
|
||||
// would send the decrypted key to the new host, so changing the endpoint requires re-entry.
|
||||
boolean endpointChanged=!java.util.Objects.equals(normalizedEndpoint(previous.get("baseUrl")),normalizedEndpoint(baseUrl));
|
||||
if (endpointChanged && !"none".equals(credentialType) && secret.isBlank() && previous.get("credentialMasked")!=null) {
|
||||
throw new ApiException("CREDENTIAL_REENTRY_REQUIRED","修改渠道地址时须重新填写凭据,已保存的凭据不会发送到新地址");
|
||||
}
|
||||
if ("none".equals(credentialType)) jdbc.update("UPDATE model_providers SET credential_ciphertext=NULL, credential_masked=NULL WHERE id=?",id);
|
||||
else if (!secret.isBlank()) jdbc.update("UPDATE model_providers SET credential_ciphertext=?, credential_masked=? WHERE id=?",secretCipher.encrypt(secret),mask(secret),id);
|
||||
if (publishingChannel) {
|
||||
@@ -157,10 +163,27 @@ public class CatalogService {
|
||||
// an administrator can still disable individual groups afterwards.
|
||||
jdbc.update("UPDATE model_catalog SET status='active', updated_at=now() WHERE provider_id=? AND status='disabled' AND pricing IS NOT NULL AND deleted_at IS NULL", id);
|
||||
}
|
||||
auth.audit("provider.updated",admin.id(),new AuthService.RequestMeta(null,null),Map.of("providerId",id));
|
||||
Map<String,Object> details=new LinkedHashMap<>();
|
||||
details.put("providerId",id);
|
||||
if (endpointChanged) { details.put("previousBaseUrl",String.valueOf(previous.get("baseUrl"))); details.put("baseUrl",String.valueOf(baseUrl)); }
|
||||
if (!secret.isBlank() || "none".equals(credentialType)) details.put("credentialReplaced",true);
|
||||
auth.audit("provider.updated",admin.id(),new AuthService.RequestMeta(null,null),details);
|
||||
return findProvider(id);
|
||||
}
|
||||
|
||||
private static String normalizedEndpoint(Object value) {
|
||||
return value == null || String.valueOf(value).isBlank() ? null : String.valueOf(value).trim().replaceAll("/+$", "").toLowerCase(java.util.Locale.ROOT);
|
||||
}
|
||||
|
||||
/** Ordinary users only need channel identity for display; endpoints and credential hints stay admin-only. */
|
||||
public List<Map<String, Object>> listPublicProviders() {
|
||||
return listProviders(false).stream().map(provider -> {
|
||||
Map<String, Object> visible = new LinkedHashMap<>();
|
||||
for (String field : List.of("id", "name", "status")) if (provider.get(field) != null) visible.put(field, provider.get(field));
|
||||
return visible;
|
||||
}).toList();
|
||||
}
|
||||
|
||||
private void validateBaseUrl(String value) {
|
||||
if (value == null || value.isBlank()) return;
|
||||
try {
|
||||
@@ -383,6 +406,7 @@ public class CatalogService {
|
||||
result.put("tasks", Map.of(
|
||||
"queued", taskCounts.getOrDefault("queued", 0L),
|
||||
"running", taskCounts.getOrDefault("running", 0L),
|
||||
"reconciling", taskCounts.getOrDefault("reconciling", 0L),
|
||||
"succeeded", taskCounts.getOrDefault("succeeded", 0L),
|
||||
"failed", taskCounts.getOrDefault("failed", 0L)
|
||||
));
|
||||
@@ -540,7 +564,9 @@ public class CatalogService {
|
||||
// "异步执行器待接入" when the execution path is already implemented,
|
||||
// while unsupported task types remain visibly pending.
|
||||
boolean asyncExecutor = asyncExecutorReady(
|
||||
resultSet.getString("provider_id"), providerProtocol, resultSet.getString("model_type"));
|
||||
resultSet.getString("provider_id"), providerProtocol, resultSet.getString("model_type"), resultSet.getString("upstream_model"));
|
||||
String upstream=resultSet.getString("upstream_model").toLowerCase(java.util.Locale.ROOT);
|
||||
if(upstream.startsWith("mj_") || upstream.startsWith("suno") || upstream.startsWith("lyria") || upstream.equals("swap_face")) asyncExecutor=false;
|
||||
boolean syncExecutor=List.of("chat","text","code","multimodal","llm","reasoning","vision","embedding").contains(resultSet.getString("model_type")) && ("openai-compatible".equalsIgnoreCase(providerProtocol)||"provider-mock".equals(resultSet.getString("provider_id")));
|
||||
model.put("executionReady", "async".equals(resultSet.getString("call_mode")) ? asyncExecutor : syncExecutor);
|
||||
var protocols=new java.util.ArrayList<String>();
|
||||
@@ -548,10 +574,21 @@ public class CatalogService {
|
||||
for(String protocol:List.of("messages","responses","gemini")) if(com.moyi.controlplane.gateway.NativeProtocolMeter.supports(model,protocol)) protocols.add(protocol);
|
||||
model.put("supportedProtocols",protocols);
|
||||
model.put("billingReady", billingReady(pricing, resultSet.getString("call_mode"), resultSet.getString("provider_id")));
|
||||
if(!Boolean.TRUE.equals(model.get("billingReady"))) {
|
||||
String reason=pricing==null ? "当前型号报价待核实" : "计费规则待核实";
|
||||
if("async".equals(model.get("mode")) && pricing!=null) {
|
||||
try { com.moyi.controlplane.task.AsyncPriceSchedule.validate(pricing); }
|
||||
catch(ApiException e) { reason=e.getMessage(); }
|
||||
}
|
||||
model.put("billingReason",reason);
|
||||
}
|
||||
// Keep the internal route UUID for permission storage and expose a
|
||||
// stable, readable identifier for the user console and third-party
|
||||
// clients. The gateway uses the same value in /v1/models.
|
||||
model.put("gatewayModelId", GatewayModelIds.publicId(model));
|
||||
if(model.get("capabilities") instanceof Map<?,?> capabilities && Boolean.TRUE.equals(capabilities.get("integrationDeferred"))) {
|
||||
model.put("integrationDeferred",true); model.put("executionReady",false); model.put("billingReady",false); model.put("billingReason","暂不接入");
|
||||
}
|
||||
model.put("createdAt", resultSet.getObject("created_at", OffsetDateTime.class));
|
||||
return model;
|
||||
}
|
||||
@@ -567,9 +604,16 @@ public class CatalogService {
|
||||
}
|
||||
|
||||
static boolean asyncExecutorReady(String providerId, String providerProtocol, String modelType) {
|
||||
if (List.of("provider-mock", "provider-vast", "provider-bytedance").contains(providerId)) return true;
|
||||
return asyncExecutorReady(providerId, providerProtocol, modelType, null);
|
||||
}
|
||||
|
||||
static boolean asyncExecutorReady(String providerId, String providerProtocol, String modelType, String upstreamModel) {
|
||||
if ("provider-mock".equals(providerId)) return true;
|
||||
if ("provider-vast".equals(providerId)) return "3d".equalsIgnoreCase(modelType);
|
||||
if ("provider-bytedance".equals(providerId)) return "video".equalsIgnoreCase(modelType);
|
||||
if (!"openai-compatible".equalsIgnoreCase(providerProtocol)) return false;
|
||||
return List.of("image", "video", "audio").contains(String.valueOf(modelType).toLowerCase(java.util.Locale.ROOT));
|
||||
if ("audio".equalsIgnoreCase(modelType)) return com.moyi.controlplane.provider.media.AsyncMediaSupport.supportsSpeech(upstreamModel);
|
||||
return List.of("image", "video").contains(String.valueOf(modelType).toLowerCase(java.util.Locale.ROOT));
|
||||
}
|
||||
|
||||
private boolean billingReady(Map<String,Object> pricing, String mode, String providerId) {
|
||||
@@ -577,10 +621,7 @@ public class CatalogService {
|
||||
if (pricing == null) return false;
|
||||
String unit = String.valueOf(pricing.getOrDefault("unit", "")).trim().toLowerCase(java.util.Locale.ROOT);
|
||||
if ("async".equals(mode)) {
|
||||
if (unit.contains("token")) return false;
|
||||
Object amount = pricing.get("price");
|
||||
if (amount == null) amount = pricing.get("input");
|
||||
return amount != null;
|
||||
return com.moyi.controlplane.task.AsyncPriceSchedule.ready(pricing);
|
||||
}
|
||||
return unit.equals("cny / 1m tokens") && pricing.get("input") != null && pricing.get("output") != null;
|
||||
}
|
||||
|
||||
+53
-30
@@ -4,7 +4,12 @@ import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
||||
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestContext;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestMetadata;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestProtocol;
|
||||
import com.moyi.controlplane.gateway.request.GatewayRequestExecutionService;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.time.Instant;
|
||||
@@ -26,15 +31,16 @@ public class GatewayController {
|
||||
private final CatalogService catalogService;
|
||||
private final OpenAiCompatibleProvider provider;
|
||||
private final GatewayGuard guard;
|
||||
private final ChatBillingService billing;
|
||||
private final GatewayRequestExecutionService requests;
|
||||
private final ObjectMapper json;
|
||||
@org.springframework.beans.factory.annotation.Autowired private GatewayFailover failover;
|
||||
|
||||
public GatewayController(CatalogService catalogService, OpenAiCompatibleProvider provider, GatewayGuard guard, ChatBillingService billing, ObjectMapper json) {
|
||||
public GatewayController(CatalogService catalogService, OpenAiCompatibleProvider provider, GatewayGuard guard,
|
||||
GatewayRequestExecutionService requests, ObjectMapper json) {
|
||||
this.catalogService = catalogService;
|
||||
this.provider = provider;
|
||||
this.guard = guard;
|
||||
this.billing = billing;
|
||||
this.requests = requests;
|
||||
this.json = json;
|
||||
}
|
||||
|
||||
@@ -73,15 +79,31 @@ public class GatewayController {
|
||||
@RequestHeader(value = "Authorization", required = false) String authorization,
|
||||
@RequestBody Map<String, Object> input,
|
||||
@RequestHeader(value = "Idempotency-Key", required = false) String idempotencyKey,
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse servletResponse
|
||||
) {
|
||||
return executeChat(requireKey(authorization), input, idempotencyKey, servletResponse);
|
||||
return executeChat(requireKey(authorization), input, idempotencyKey, servletResponse,
|
||||
ApiRequestMetadata.fromHttp(request.getRemoteAddr()));
|
||||
}
|
||||
|
||||
// Both the public gateway and the authenticated playground share admission and billing.
|
||||
/** Direct-call compatibility for application tests and non-HTTP adapters. */
|
||||
public ResponseEntity<Map<String, Object>> chatCompletions(String authorization,
|
||||
Map<String, Object> input, String idempotencyKey, HttpServletResponse servletResponse) {
|
||||
return executeChat(requireKey(authorization), input, idempotencyKey, servletResponse, ApiRequestMetadata.internal());
|
||||
}
|
||||
|
||||
// Public API and Playground share the same request execution and risk boundary.
|
||||
public ResponseEntity<Map<String, Object>> executeChat(Map<String, Object> key,
|
||||
Map<String, Object> input, String idempotencyKey, HttpServletResponse servletResponse) {
|
||||
return executeChat(key, input, idempotencyKey, servletResponse, ApiRequestMetadata.internal());
|
||||
}
|
||||
|
||||
public ResponseEntity<Map<String, Object>> executeChat(Map<String, Object> key,
|
||||
Map<String, Object> input, String idempotencyKey, HttpServletResponse servletResponse, ApiRequestMetadata metadata) {
|
||||
long startedAt = System.currentTimeMillis();
|
||||
UUID keyId = UUID.fromString(String.valueOf(key.get("id")));
|
||||
// Reject request floods before catalog reads and payload normalization, including malformed requests.
|
||||
guard.checkRate(keyId, false);
|
||||
String modelId = String.valueOf(input.getOrDefault("model", ""));
|
||||
Map<String, Object> model = resolveModel(key, modelId);
|
||||
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线;请使用当前 API Key 的 /v1/models 返回的模型 ID", 404);
|
||||
@@ -92,27 +114,28 @@ public class GatewayController {
|
||||
boolean mock="provider-mock".equals(providerId);
|
||||
if(!mock && !provider.isConfigured(providerId, routeId, String.valueOf(model.get("upstreamModel")))) throw new ApiException("PROVIDER_NOT_CONFIGURED","该供应商尚未配置密钥,不能执行真实调用",503);
|
||||
ChatMeter.Request request=ChatMeter.request(input,json);
|
||||
guard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
|
||||
// Reserve both limits atomically. A queued request never consumes a
|
||||
// model slot while waiting for the API-Key slot (or vice versa).
|
||||
try (GatewayGuard.Admission admission = guard.acquire(model, UUID.fromString(String.valueOf(key.get("id"))), guard.queueWaitMs())) {
|
||||
var ticket=billing.reserve(key,model,request,idempotencyKey);
|
||||
ApiRequestContext context = new ApiRequestContext(
|
||||
UUID.fromString(String.valueOf(key.get("userId"))), keyId, ApiRequestProtocol.CHAT_COMPLETIONS,
|
||||
modelId, routeId, Boolean.TRUE.equals(request.body().get("stream")), request.reservedInputTokens(),
|
||||
request.maxOutputTokens(), metadata, System.nanoTime());
|
||||
return requests.execute(context, key, model, request, idempotencyKey, () -> {
|
||||
// A queued request never consumes one scarce slot while waiting for another.
|
||||
return guard.acquire(model, keyId, guard.queueWaitMs());
|
||||
}, ticket -> {
|
||||
servletResponse.setHeader("X-Request-Id",ticket.id().toString());
|
||||
try {
|
||||
catalogService.touchKey(key);
|
||||
if(Boolean.TRUE.equals(request.body().get("stream"))) {
|
||||
stream(key,model,modelId,routeId,request.body(),ticket,servletResponse,startedAt,mock);
|
||||
return null;
|
||||
}
|
||||
Map<String,Object> body=mock?mockResponse(modelId,input):failover!=null?failover.invoke(key,model,"chat",request.body(),ticket):provider.chat(providerId,String.valueOf(model.get("upstreamModel")),routeId,request.body());
|
||||
body.put("model",modelId);
|
||||
billing.observedUsage(ticket,"chat",body.get("usage"));
|
||||
requireCompletion(body);
|
||||
var cost=billing.settle(ticket,body,elapsed(startedAt),null);
|
||||
return ResponseEntity.ok().header("X-Request-Id",ticket.id().toString()).header("X-Moyi-Cost-CNY",cost.toPlainString()).body(body);
|
||||
} catch(Exception exception) {
|
||||
ApiException error=exception instanceof ApiException e?e:new ApiException("CALL_INTERRUPTED","调用中断,请查看使用记录确认结算状态",502);
|
||||
billing.fail(ticket,error.getCode(),error.getMessage(),elapsed(startedAt));
|
||||
catalogService.touchKey(key);
|
||||
if(Boolean.TRUE.equals(request.body().get("stream"))) {
|
||||
stream(key,model,modelId,routeId,request.body(),ticket,servletResponse,startedAt,mock);
|
||||
return null;
|
||||
}
|
||||
Map<String,Object> body=mock?mockResponse(modelId,input):failover!=null?failover.invoke(key,model,"chat",request.body(),ticket):provider.chat(providerId,String.valueOf(model.get("upstreamModel")),routeId,request.body());
|
||||
body.put("model",modelId);
|
||||
requests.observedUsage(ticket,"chat",body.get("usage"));
|
||||
requireCompletion(body);
|
||||
var cost=requests.settle(ticket,body,elapsed(startedAt),null);
|
||||
return ResponseEntity.ok().header("X-Request-Id",ticket.id().toString()).header("X-Moyi-Cost-CNY",cost.toPlainString()).body(body);
|
||||
}, exception -> exception instanceof ApiException api ? api
|
||||
: new ApiException("CALL_INTERRUPTED","调用中断,请查看使用记录确认结算状态",502), error -> {
|
||||
if(servletResponse.isCommitted()) {
|
||||
try { event(servletResponse,Map.of("error",Map.of("code",error.getCode(),"message",error.getMessage()))); done(servletResponse); } catch(IOException ignored) { }
|
||||
return null;
|
||||
@@ -120,8 +143,7 @@ public class GatewayController {
|
||||
servletResponse.resetBuffer();
|
||||
servletResponse.setContentType("application/json");
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void stream(Map<String,Object> key,Map<String,Object> model, String modelId, String routeId, Map<String,Object> input, ChatBillingService.Ticket ticket, HttpServletResponse response, long started, boolean mock) throws IOException {
|
||||
@@ -131,7 +153,8 @@ public class GatewayController {
|
||||
var choices=(List<?>)body.get("choices");
|
||||
var message=((Map<?,?>)choices.getFirst()).get("message");
|
||||
event(response,Map.of("id",body.get("id"),"object","chat.completion.chunk","model",modelId,"choices",List.of(Map.of("index",0,"delta",message))));
|
||||
billing.settle(ticket,body,elapsed(started),elapsed(started));
|
||||
requests.observedUsage(ticket,"chat",body.get("usage"));
|
||||
requests.settle(ticket,body,elapsed(started),elapsed(started));
|
||||
event(response,Map.of("id",body.get("id"),"object","chat.completion.chunk","model",modelId,"choices",List.of(Map.of("index",0,"delta",Map.of(),"finish_reason","stop"))));
|
||||
event(response,Map.of("choices",List.of(),"usage",body.get("usage"))); done(response); return;
|
||||
}
|
||||
@@ -147,9 +170,9 @@ public class GatewayController {
|
||||
// A closed browser must not turn an otherwise completed provider call into free usage.
|
||||
// Continue within the same hard deadline to obtain the final meter and settle once.
|
||||
if(!disconnected[0]) try { event(response,chunk); } catch(IOException clientClosed) { disconnected[0]=true; }
|
||||
}); } finally {billing.observedUsage(ticket,"chat",collector.observedUsage());}
|
||||
}); } finally {requests.observedUsage(ticket,"chat",collector.observedUsage());}
|
||||
var body=collector.result();
|
||||
billing.settle(ticket,body,elapsed(started),collector.firstTokenMs());
|
||||
requests.settle(ticket,body,elapsed(started),collector.firstTokenMs());
|
||||
if(!disconnected[0]) done(response);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestAdmission;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
@@ -22,6 +23,7 @@ public class GatewayGuard {
|
||||
private static final long SLOT_TTL_MS = 180_000;
|
||||
private static final DefaultRedisScript<Long> RATE = script("redis/rate-limit.lua");
|
||||
private static final DefaultRedisScript<Long> ACQUIRE = script("redis/acquire-slot.lua");
|
||||
private static final DefaultRedisScript<Long> ACQUIRE_TASK = script("redis/acquire-task-slot.lua");
|
||||
private static final DefaultRedisScript<Long> ACQUIRE_PAIR = script("redis/acquire-pair.lua");
|
||||
private static final DefaultRedisScript<List> CAPACITY = new DefaultRedisScript<>("""
|
||||
local time = redis.call('TIME')
|
||||
@@ -32,6 +34,16 @@ public class GatewayGuard {
|
||||
end
|
||||
return counts
|
||||
""", List.class);
|
||||
// Renew only a lease this request still owns; an expired or released token is never recreated.
|
||||
private static final DefaultRedisScript<Long> RENEW = new DefaultRedisScript<>("""
|
||||
local time = redis.call('TIME')
|
||||
local now = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2]) / 1000)
|
||||
local score = redis.call('ZSCORE', KEYS[1], ARGV[1])
|
||||
if not score or tonumber(score) <= now then return 0 end
|
||||
redis.call('ZADD', KEYS[1], 'XX', now + tonumber(ARGV[2]), ARGV[1])
|
||||
if redis.call('PTTL', KEYS[1]) < tonumber(ARGV[2]) then redis.call('PEXPIRE', KEYS[1], ARGV[2]) end
|
||||
return 1
|
||||
""", Long.class);
|
||||
private static final DefaultRedisScript<Long> RELEASE = new DefaultRedisScript<>("return redis.call('ZREM', KEYS[1], ARGV[1])", Long.class);
|
||||
private final StringRedisTemplate redis;
|
||||
private final int requestsPerMinute;
|
||||
@@ -162,9 +174,11 @@ public class GatewayGuard {
|
||||
if(policies==null) return true;
|
||||
var policy=policies.taskLimit(keyId);
|
||||
String bucket=redisPrefix+"gateway:slots:user:"+policy.userId();
|
||||
boolean acquired=execute(ACQUIRE,bucket,Integer.toString(policy.concurrency()),Long.toString(Math.max(180000,ttlMs)),taskId)==1;
|
||||
long result=execute(ACQUIRE_TASK,bucket,Integer.toString(policy.concurrency()),Long.toString(Math.max(180000,ttlMs)),taskId);
|
||||
boolean acquired=result>0;
|
||||
// Redis is outside the database transaction: roll back the lease if claiming the task fails.
|
||||
if(acquired&&org.springframework.transaction.support.TransactionSynchronizationManager.isSynchronizationActive())
|
||||
// Rolling back renewal must not remove a lease that existed before this transaction.
|
||||
if(result==1&&org.springframework.transaction.support.TransactionSynchronizationManager.isSynchronizationActive())
|
||||
org.springframework.transaction.support.TransactionSynchronizationManager.registerSynchronization(new org.springframework.transaction.support.TransactionSynchronization() {@Override public void afterCompletion(int status) {if(status!=STATUS_COMMITTED) releaseQuietly(bucket,taskId);}});
|
||||
return acquired;
|
||||
}
|
||||
@@ -224,6 +238,7 @@ public class GatewayGuard {
|
||||
String modelBucket = redisPrefix + "gateway:slots:model:" + model.get("id");
|
||||
String keyBucket = redisPrefix + "gateway:slots:key:" + keyId;
|
||||
String waitingBucket = redisPrefix + "gateway:waiting:model:" + model.get("id");
|
||||
String orderBucket = redisPrefix + "gateway:order:model:" + model.get("id");
|
||||
String token = UUID.randomUUID().toString();
|
||||
long waitMs = Math.max(0, Math.min(queueWaitMs, requestedWaitMs > 0 ? requestedWaitMs : queueWaitMs));
|
||||
long deadline = System.nanoTime() + java.util.concurrent.TimeUnit.MILLISECONDS.toNanos(waitMs);
|
||||
@@ -245,7 +260,7 @@ public class GatewayGuard {
|
||||
}
|
||||
while (true) {
|
||||
long remaining = Math.max(0, java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(deadline - System.nanoTime()));
|
||||
long acquired = executePair(modelBucket, keyBucket, waitingBucket, Integer.toString(modelLimit), Integer.toString(defaultKeyConcurrency), Long.toString(SLOT_TTL_MS), token, Long.toString(remaining), Long.toString(Math.max(SLOT_TTL_MS, waitMs + 1000)));
|
||||
long acquired = executePair(modelBucket, keyBucket, waitingBucket, orderBucket, Integer.toString(modelLimit), Integer.toString(defaultKeyConcurrency), Long.toString(SLOT_TTL_MS), token, Long.toString(remaining), Long.toString(Math.max(SLOT_TTL_MS, waitMs + 1000)));
|
||||
if (acquired == 1) {
|
||||
admitted = true; // The acquisition script also removed the waiting entry.
|
||||
return new Admission(modelBucket, keyBucket, token);
|
||||
@@ -262,7 +277,11 @@ public class GatewayGuard {
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
if (!admitted && waitMs > 0) releaseQuietly(waitingBucket, token);
|
||||
if (!admitted && waitMs > 0) {
|
||||
// Leaving the queue must also release this caller's FIFO position.
|
||||
releaseQuietly(waitingBucket, token);
|
||||
releaseQuietly(orderBucket, token);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -297,9 +316,9 @@ public class GatewayGuard {
|
||||
throw new GatewayLimitException("GATEWAY_GUARD_UNAVAILABLE", "网关流量控制暂不可用,请稍后重试", 503, 3);
|
||||
}
|
||||
|
||||
private long executePair(String modelKey, String keyKey, String waitingKey, String modelLimit, String keyLimit, String ttl, String token, String waitMs, String waitingTtl) {
|
||||
private long executePair(String modelKey, String keyKey, String waitingKey, String orderKey, String modelLimit, String keyLimit, String ttl, String token, String waitMs, String waitingTtl) {
|
||||
try {
|
||||
Long value = redis.execute(ACQUIRE_PAIR, List.of(modelKey, keyKey, waitingKey), modelLimit, keyLimit, ttl, token, waitMs, waitingTtl);
|
||||
Long value = redis.execute(ACQUIRE_PAIR, List.of(modelKey, keyKey, waitingKey, orderKey), modelLimit, keyLimit, ttl, token, waitMs, waitingTtl);
|
||||
if (value != null) return value;
|
||||
} catch (DataAccessException exception) {
|
||||
log.warn("Gateway pair admission Redis operation failed ({})", exception.getClass().getSimpleName());
|
||||
@@ -315,7 +334,7 @@ public class GatewayGuard {
|
||||
}
|
||||
}
|
||||
|
||||
public final class Admission implements AutoCloseable {
|
||||
public final class Admission implements ApiRequestAdmission {
|
||||
private Slot userSlot;
|
||||
private final Slot modelSlot;
|
||||
private final Slot keySlot;
|
||||
@@ -331,6 +350,15 @@ public class GatewayGuard {
|
||||
this.keySlot = keySlot;
|
||||
}
|
||||
|
||||
/** Extend every lease held by this admission; returns false if any lease was already lost. */
|
||||
@Override
|
||||
public boolean renew(long ttlMs) {
|
||||
if (closed.get()) return false;
|
||||
boolean held = true;
|
||||
for (Slot slot : new Slot[]{modelSlot, keySlot, userSlot}) if (slot != null && !slot.renew(ttlMs)) held = false;
|
||||
return held;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (!closed.compareAndSet(false, true)) return;
|
||||
@@ -347,6 +375,13 @@ public class GatewayGuard {
|
||||
|
||||
private Slot(String key, String token) { this.key = key; this.token = token; }
|
||||
|
||||
boolean renew(long ttlMs) {
|
||||
if (closed.get()) return false;
|
||||
if (key == null || token == null) return true;
|
||||
try { return execute(RENEW, key, token, Long.toString(Math.max(SLOT_TTL_MS, ttlMs))) == 1; }
|
||||
catch (GatewayLimitException exception) { return false; }
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
if (!closed.compareAndSet(false, true)) return;
|
||||
|
||||
@@ -45,7 +45,7 @@ public final class GatewayModelIds {
|
||||
List<Map<String, Object>> exact = authorized.stream()
|
||||
.filter(model -> requested.equals(publicId(model)) || matchesRouteTag(requested, model))
|
||||
.toList();
|
||||
if (exact.size() == 1) return exact.getFirst();
|
||||
if (exact.size() == 1) return requireOnboarding(exact.getFirst());
|
||||
if (exact.size() > 1) throw ambiguous();
|
||||
// Avoid treating a readable public identifier as a legacy database ID
|
||||
// if an administrator happens to create a route with the same text.
|
||||
@@ -54,13 +54,18 @@ public final class GatewayModelIds {
|
||||
if (direct != null) {
|
||||
if (!catalog.keyCanInvoke(key, String.valueOf(direct.get("id"))))
|
||||
throw new ApiException("MODEL_FORBIDDEN", "当前 Key 无权调用该模型", 403);
|
||||
return direct;
|
||||
return requireOnboarding(direct);
|
||||
}
|
||||
List<Map<String, Object>> aliases = authorized.stream()
|
||||
.filter(model -> equalsNonBlank(requested, model.get("modelKey")) || equalsNonBlank(requested, model.get("upstreamModel")))
|
||||
.toList();
|
||||
if (aliases.size() > 1) throw ambiguous();
|
||||
return aliases.isEmpty() ? null : aliases.getFirst();
|
||||
return aliases.isEmpty() ? null : requireOnboarding(aliases.getFirst());
|
||||
}
|
||||
|
||||
private static Map<String,Object> requireOnboarding(Map<String,Object> model) {
|
||||
if(Boolean.TRUE.equals(model.get("integrationDeferred"))) throw new ApiException("MODEL_ONBOARDING_DEFERRED","该模型暂不接入",503);
|
||||
return model;
|
||||
}
|
||||
|
||||
private static boolean matchesRouteTag(String requested, Map<String, Object> model) {
|
||||
|
||||
+42
-33
@@ -4,7 +4,10 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
||||
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestContext;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestMetadata;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestProtocol;
|
||||
import com.moyi.controlplane.gateway.request.GatewayRequestExecutionService;
|
||||
import jakarta.servlet.http.*;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -15,9 +18,9 @@ import org.springframework.web.bind.annotation.*;
|
||||
@RestController
|
||||
public class NativeGatewayController {
|
||||
private final CatalogService catalog; private final GatewayGuard guard;
|
||||
private final ChatBillingService billing; private final OpenAiCompatibleProvider provider; private final ObjectMapper json;
|
||||
private final GatewayRequestExecutionService requests; private final OpenAiCompatibleProvider provider; private final ObjectMapper json;
|
||||
@org.springframework.beans.factory.annotation.Autowired private GatewayFailover failover;
|
||||
public NativeGatewayController(CatalogService catalog,GatewayGuard guard,ChatBillingService billing,OpenAiCompatibleProvider provider,ObjectMapper json) {this.catalog=catalog;this.guard=guard;this.billing=billing;this.provider=provider;this.json=json;}
|
||||
public NativeGatewayController(CatalogService catalog,GatewayGuard guard,GatewayRequestExecutionService requests,OpenAiCompatibleProvider provider,ObjectMapper json) {this.catalog=catalog;this.guard=guard;this.requests=requests;this.provider=provider;this.json=json;}
|
||||
@PostMapping({"/v1/embeddings","/v1/messages","/v1/responses"})
|
||||
public ResponseEntity<Map<String,Object>> invoke(@RequestBody Map<String,Object> input,HttpServletRequest request,HttpServletResponse response) {
|
||||
String protocol=request.getRequestURI().substring(request.getRequestURI().lastIndexOf('/')+1);
|
||||
@@ -41,7 +44,11 @@ public class NativeGatewayController {
|
||||
return executeForKey(protocol,input,streaming,key,request,response);
|
||||
}
|
||||
public ResponseEntity<Map<String,Object>> executeForKey(String protocol,Map<String,Object> input,boolean streaming,Map<String,Object> key,HttpServletRequest request,HttpServletResponse response) {
|
||||
UUID keyId=UUID.fromString(key.get("id").toString());guard.checkRate(keyId,false);
|
||||
return executeForKey(protocol,input,streaming,key,request,response,ApiRequestMetadata.fromHttp(request.getRemoteAddr()));
|
||||
}
|
||||
public ResponseEntity<Map<String,Object>> executeForKey(String protocol,Map<String,Object> input,boolean streaming,Map<String,Object> key,HttpServletRequest request,HttpServletResponse response,ApiRequestMetadata metadata) {
|
||||
UUID keyId=UUID.fromString(key.get("id").toString());
|
||||
guard.checkRate(keyId,false);
|
||||
String requested=String.valueOf(input.getOrDefault("model",""));
|
||||
var model=GatewayModelIds.resolve(catalog,key,requested);
|
||||
if(model==null) throw new ApiException("MODEL_NOT_FOUND","模型不存在或未授权",404);
|
||||
@@ -50,38 +57,40 @@ public class NativeGatewayController {
|
||||
if(!provider.isConfigured(providerId,route,upstream)) throw new ApiException("PROVIDER_NOT_CONFIGURED","渠道凭据未配置",503);
|
||||
var normalized=NativeProtocolMeter.request(protocol,input,json);
|
||||
long start=System.currentTimeMillis();
|
||||
try(var admission=guard.acquire(model,keyId,guard.queueWaitMs())) {
|
||||
var ticket=billing.reserve(key,model,normalized,request.getHeader("Idempotency-Key"));
|
||||
billing.endpoint(ticket.id(),"gemini".equals(protocol)?"/v1beta/models:generateContent":"/v1/"+protocol);
|
||||
ApiRequestProtocol requestProtocol = ApiRequestProtocol.from(protocol);
|
||||
ApiRequestContext context = new ApiRequestContext(UUID.fromString(key.get("userId").toString()), keyId,
|
||||
requestProtocol, requested, route, streaming, normalized.reservedInputTokens(), normalized.maxOutputTokens(),
|
||||
metadata, System.nanoTime());
|
||||
return requests.execute(context, key, model, normalized, request.getHeader("Idempotency-Key"), () -> {
|
||||
return guard.acquire(model,keyId,guard.queueWaitMs());
|
||||
}, ticket -> {
|
||||
requests.endpoint(ticket,"gemini".equals(protocol)?"/v1beta/models:generateContent":"/v1/"+protocol);
|
||||
response.setHeader("X-Request-Id",ticket.id().toString());
|
||||
try {
|
||||
catalog.touchKey(key);
|
||||
if(!streaming) {
|
||||
var result=failover!=null?failover.invoke(key,model,protocol,normalized.body(),ticket):provider.invoke(providerId,upstream,route,normalized.body(),protocol);
|
||||
if(result.containsKey("model")) result.put("model",requested);
|
||||
billing.observedUsage(ticket,protocol,result.get("gemini".equals(protocol)?"usageMetadata":"usage"));
|
||||
var cost=billing.settle(ticket,NativeProtocolMeter.normalize(protocol,result),elapsed(start),null);
|
||||
return ResponseEntity.ok().header("X-Request-Id",ticket.id().toString()).header("X-Moyi-Cost-CNY",cost.toPlainString()).body(result);
|
||||
}
|
||||
try(var stream=failover!=null?failover.stream(key,model,protocol,normalized.body(),ticket):provider.nativeStream(providerId,upstream,route,normalized.body(),protocol)) {
|
||||
var collector=new NativeProtocolMeter.Collector(protocol);
|
||||
response.setContentType("text/event-stream");response.setCharacterEncoding("UTF-8");response.setHeader("Cache-Control","no-cache");response.setHeader("X-Accel-Buffering","no");
|
||||
boolean[] disconnected={false};Integer[] first={null};
|
||||
try {stream.events(chunk->{
|
||||
if(first[0]==null) first[0]=elapsed(start);
|
||||
collector.accept(chunk);
|
||||
if(!disconnected[0]) try {event(response,chunk);} catch(IOException e) {disconnected[0]=true;}
|
||||
},false);} finally {billing.observedUsage(ticket,protocol,collector.observedUsage());}
|
||||
billing.settle(ticket,collector.result(),elapsed(start),first[0]);
|
||||
}
|
||||
return null;
|
||||
} catch(Exception failure) {
|
||||
var error=failure instanceof ApiException api?api:new ApiException("CALL_INTERRUPTED","调用中断,渠道成本待核对",502);
|
||||
billing.fail(ticket,error.getCode(),error.getMessage(),elapsed(start));
|
||||
catalog.touchKey(key);
|
||||
if(!streaming) {
|
||||
var result=failover!=null?failover.invoke(key,model,protocol,normalized.body(),ticket):provider.invoke(providerId,upstream,route,normalized.body(),protocol);
|
||||
if(result.containsKey("model")) result.put("model",requested);
|
||||
requests.observedUsage(ticket,protocol,result.get("gemini".equals(protocol)?"usageMetadata":"usage"));
|
||||
var cost=requests.settle(ticket,NativeProtocolMeter.normalize(protocol,result),elapsed(start),null);
|
||||
return ResponseEntity.ok().header("X-Request-Id",ticket.id().toString()).header("X-Moyi-Cost-CNY",cost.toPlainString()).body(result);
|
||||
}
|
||||
try(var stream=failover!=null?failover.stream(key,model,protocol,normalized.body(),ticket):provider.nativeStream(providerId,upstream,route,normalized.body(),protocol)) {
|
||||
var collector=new NativeProtocolMeter.Collector(protocol);
|
||||
response.setContentType("text/event-stream");response.setCharacterEncoding("UTF-8");response.setHeader("Cache-Control","no-cache");response.setHeader("X-Accel-Buffering","no");
|
||||
boolean[] disconnected={false};Integer[] first={null};
|
||||
try {stream.events(chunk->{
|
||||
if(first[0]==null) first[0]=elapsed(start);
|
||||
collector.accept(chunk);
|
||||
if(!disconnected[0]) try {event(response,chunk);} catch(IOException e) {disconnected[0]=true;}
|
||||
},false);} finally {requests.observedUsage(ticket,protocol,collector.observedUsage());}
|
||||
requests.settle(ticket,collector.result(),elapsed(start),first[0]);
|
||||
}
|
||||
return null;
|
||||
}, failure -> failure instanceof ApiException api ? api
|
||||
: new ApiException("CALL_INTERRUPTED","调用中断,渠道成本待核对",502), error -> {
|
||||
if(response.isCommitted()) {try {event(response,Map.of("type","error","error",Map.of("type",error.getCode(),"message",error.getMessage())));} catch(IOException ignored) {}return null;}
|
||||
response.resetBuffer();response.setContentType("application/json");throw error;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
private int elapsed(long start) {return (int)Math.min(Integer.MAX_VALUE,System.currentTimeMillis()-start);}
|
||||
private void event(HttpServletResponse response,Map<String,Object> chunk) throws IOException {
|
||||
|
||||
+8
-4
@@ -21,7 +21,8 @@ public class PlaygroundController {
|
||||
@org.springframework.beans.factory.annotation.Autowired private NativeGatewayController nativeGateway;
|
||||
@PostMapping(value="/embeddings",consumes="application/json")
|
||||
public ResponseEntity<Map<String,Object>> embeddings(@PathVariable String keyId,@RequestBody Map<String,Object> input,HttpServletRequest request,HttpServletResponse response) {
|
||||
return nativeGateway.executeForKey("embeddings",input,false,requireKey(request,keyId),request,response);
|
||||
return nativeGateway.executeForKey("embeddings",input,false,requireKey(request,keyId),request,response,
|
||||
com.moyi.controlplane.gateway.request.ApiRequestMetadata.playground(request.getRemoteAddr()));
|
||||
}
|
||||
|
||||
public PlaygroundController(AuthService auth, CatalogService catalog, GatewayController gateway, AsyncTaskService tasks) {
|
||||
@@ -36,13 +37,16 @@ public class PlaygroundController {
|
||||
@RequestBody Map<String, Object> input,
|
||||
@RequestHeader(value="Idempotency-Key", required=false) String idempotencyKey,
|
||||
HttpServletRequest request, HttpServletResponse response) {
|
||||
return gateway.executeChat(requireKey(request, keyId), input, idempotencyKey, response);
|
||||
return gateway.executeChat(requireKey(request, keyId), input, idempotencyKey, response,
|
||||
com.moyi.controlplane.gateway.request.ApiRequestMetadata.playground(request.getRemoteAddr()));
|
||||
}
|
||||
|
||||
@PostMapping(value="/{type:3d|video|image|audio}/tasks", consumes="application/json")
|
||||
public ResponseEntity<Map<String, Object>> createTask(@PathVariable String keyId, @PathVariable String type,
|
||||
@RequestBody Map<String, Object> input, HttpServletRequest request) {
|
||||
return ResponseEntity.accepted().body(Map.of("task", tasks.createForKey(requireKey(request, keyId), type, input)));
|
||||
@RequestBody Map<String, Object> input,
|
||||
@RequestHeader(value="Idempotency-Key", required=false) String idempotencyKey, HttpServletRequest request) {
|
||||
return ResponseEntity.accepted().body(Map.of("task", tasks.createForKey(requireKey(request, keyId), type, input, idempotencyKey,
|
||||
com.moyi.controlplane.gateway.request.ApiRequestMetadata.playground(request.getRemoteAddr()))));
|
||||
}
|
||||
|
||||
@GetMapping("/tasks/{taskId}")
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.moyi.controlplane.gateway.request;
|
||||
|
||||
/** One request's releasable admission lease. */
|
||||
public interface ApiRequestAdmission extends AutoCloseable {
|
||||
/** Extend the lease while the request is still running. Returns false if it was already lost. */
|
||||
default boolean renew(long ttlMs) { return true; }
|
||||
|
||||
@Override
|
||||
void close();
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.moyi.controlplane.gateway.request;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.UUID;
|
||||
|
||||
/** Immutable, payload-free facts supplied to admission and risk policies. */
|
||||
public record ApiRequestContext(
|
||||
UUID userId,
|
||||
UUID apiKeyId,
|
||||
ApiRequestProtocol protocol,
|
||||
String requestedModelId,
|
||||
String routeId,
|
||||
boolean streaming,
|
||||
long estimatedInputUnits,
|
||||
int maxOutputUnits,
|
||||
ApiRequestMetadata metadata,
|
||||
long startedAtNanos
|
||||
) {
|
||||
public ApiRequestContext {
|
||||
Objects.requireNonNull(userId, "userId");
|
||||
Objects.requireNonNull(apiKeyId, "apiKeyId");
|
||||
Objects.requireNonNull(protocol, "protocol");
|
||||
requestedModelId = Objects.requireNonNullElse(requestedModelId, "");
|
||||
routeId = Objects.requireNonNullElse(routeId, "");
|
||||
metadata = metadata == null ? ApiRequestMetadata.internal() : metadata;
|
||||
if (estimatedInputUnits < 0 || maxOutputUnits < 0) {
|
||||
throw new IllegalArgumentException("Request estimates cannot be negative");
|
||||
}
|
||||
}
|
||||
|
||||
public int elapsedMillis() {
|
||||
return (int) Math.min(Integer.MAX_VALUE,
|
||||
Math.max(0, java.util.concurrent.TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startedAtNanos)));
|
||||
}
|
||||
|
||||
/** Deliberately omit client metadata so incidental logging cannot expose it. */
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ApiRequestContext[protocol=" + protocol + ", userId=" + userId + ", apiKeyId=" + apiKeyId
|
||||
+ ", requestedModelId=" + requestedModelId + ", routeId=" + routeId + ", streaming=" + streaming + "]";
|
||||
}
|
||||
}
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
package com.moyi.controlplane.gateway.request;
|
||||
|
||||
/** Trusted transport metadata captured by the HTTP adapter; never includes credentials or payloads. */
|
||||
public record ApiRequestMetadata(String clientIp, Source source) {
|
||||
public enum Source {
|
||||
API,
|
||||
PLAYGROUND,
|
||||
INTERNAL
|
||||
}
|
||||
|
||||
public ApiRequestMetadata {
|
||||
source = source == null ? Source.INTERNAL : source;
|
||||
if (clientIp != null && clientIp.length() > 64) clientIp = clientIp.substring(0, 64);
|
||||
}
|
||||
|
||||
/** Public HTTP source is determined by the mapped server endpoint, never a caller-controlled header. */
|
||||
public static ApiRequestMetadata fromHttp(String clientIp) {
|
||||
return new ApiRequestMetadata(clientIp, Source.API);
|
||||
}
|
||||
|
||||
/** Use only after the session-backed Playground controller has authorized its key. */
|
||||
public static ApiRequestMetadata playground(String clientIp) {
|
||||
return new ApiRequestMetadata(clientIp, Source.PLAYGROUND);
|
||||
}
|
||||
|
||||
public static ApiRequestMetadata internal() {
|
||||
return new ApiRequestMetadata(null, Source.INTERNAL);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ApiRequestMetadata[source=" + source + "]";
|
||||
}
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.moyi.controlplane.gateway.request;
|
||||
|
||||
import java.util.List;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
public class ApiRequestModuleConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(ApiRequestRiskEngine.class)
|
||||
ApiRequestRiskEngine apiRequestRiskEngine(
|
||||
List<ApiRequestRiskRule> rules,
|
||||
@Value("${moyi.gateway.risk-mode:shadow}") String mode
|
||||
) {
|
||||
return new CompositeApiRequestRiskEngine(rules, CompositeApiRequestRiskEngine.Mode.parse(mode));
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.moyi.controlplane.gateway.request;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
|
||||
public enum ApiRequestProtocol {
|
||||
CHAT_COMPLETIONS,
|
||||
MESSAGES,
|
||||
RESPONSES,
|
||||
GEMINI,
|
||||
EMBEDDINGS,
|
||||
ASYNC_TASK;
|
||||
|
||||
public static ApiRequestProtocol from(String value) {
|
||||
return switch (value) {
|
||||
case "chat", "chat.completions" -> CHAT_COMPLETIONS;
|
||||
case "messages" -> MESSAGES;
|
||||
case "responses" -> RESPONSES;
|
||||
case "gemini" -> GEMINI;
|
||||
case "embeddings" -> EMBEDDINGS;
|
||||
case "async", "3d", "image", "audio", "video" -> ASYNC_TASK;
|
||||
default -> throw new ApiException("UNSUPPORTED_PROTOCOL", "请求协议未接入", 400);
|
||||
};
|
||||
}
|
||||
}
|
||||
+63
@@ -0,0 +1,63 @@
|
||||
package com.moyi.controlplane.gateway.request;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public record ApiRequestRiskDecision(
|
||||
Action action,
|
||||
List<String> ruleIds,
|
||||
String code,
|
||||
String publicMessage,
|
||||
int retryAfterSeconds
|
||||
) {
|
||||
public enum Action {
|
||||
ALLOW,
|
||||
SHADOW,
|
||||
DENY,
|
||||
THROTTLE,
|
||||
UNAVAILABLE
|
||||
}
|
||||
|
||||
public ApiRequestRiskDecision {
|
||||
Objects.requireNonNull(action, "action");
|
||||
ruleIds = ruleIds == null ? List.of() : List.copyOf(ruleIds);
|
||||
for (String ruleId : ruleIds) {
|
||||
if (ruleId == null || !ruleId.matches("[a-z][a-z0-9._-]{0,63}")) {
|
||||
throw new IllegalArgumentException("Risk decision contains an invalid rule ID");
|
||||
}
|
||||
}
|
||||
if (action == Action.DENY || action == Action.THROTTLE) {
|
||||
if (code == null || !code.matches("[A-Z][A-Z0-9_]{1,63}")) {
|
||||
throw new IllegalArgumentException("Blocking risk decision requires a safe error code");
|
||||
}
|
||||
if (publicMessage == null || publicMessage.isBlank() || publicMessage.length() > 240) {
|
||||
throw new IllegalArgumentException("Blocking risk decision requires a short public message");
|
||||
}
|
||||
if (action == Action.THROTTLE ? retryAfterSeconds < 1 : retryAfterSeconds != 0) {
|
||||
throw new IllegalArgumentException("Risk retry delay does not match its action");
|
||||
}
|
||||
} else if (retryAfterSeconds != 0) {
|
||||
throw new IllegalArgumentException("Non-blocking risk decisions cannot include a retry delay");
|
||||
}
|
||||
}
|
||||
|
||||
public static ApiRequestRiskDecision allow() {
|
||||
return new ApiRequestRiskDecision(Action.ALLOW, List.of(), null, null, 0);
|
||||
}
|
||||
|
||||
public static ApiRequestRiskDecision shadow(List<String> ruleIds) {
|
||||
return new ApiRequestRiskDecision(Action.SHADOW, ruleIds, null, null, 0);
|
||||
}
|
||||
|
||||
public static ApiRequestRiskDecision deny(ApiRequestRiskSignal signal) {
|
||||
return new ApiRequestRiskDecision(Action.DENY, List.of(signal.ruleId()), signal.code(), signal.publicMessage(), 0);
|
||||
}
|
||||
|
||||
public static ApiRequestRiskDecision throttle(ApiRequestRiskSignal signal) {
|
||||
return new ApiRequestRiskDecision(Action.THROTTLE, List.of(signal.ruleId()), signal.code(), signal.publicMessage(), signal.retryAfterSeconds());
|
||||
}
|
||||
|
||||
public static ApiRequestRiskDecision unavailable(String ruleId) {
|
||||
return new ApiRequestRiskDecision(Action.UNAVAILABLE, List.of(ruleId), null, null, 0);
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.moyi.controlplane.gateway.request;
|
||||
|
||||
@FunctionalInterface
|
||||
public interface ApiRequestRiskEngine {
|
||||
ApiRequestRiskDecision evaluate(ApiRequestContext context);
|
||||
|
||||
static ApiRequestRiskEngine allowAll() {
|
||||
return ignored -> ApiRequestRiskDecision.allow();
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
package com.moyi.controlplane.gateway.request;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.gateway.GatewayLimitException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/** Applies risk decisions before admission or wallet reservation. */
|
||||
@Service
|
||||
public class ApiRequestRiskPolicy {
|
||||
private static final Logger log = LoggerFactory.getLogger(ApiRequestRiskPolicy.class);
|
||||
private final ApiRequestRiskEngine engine;
|
||||
|
||||
public ApiRequestRiskPolicy(ApiRequestRiskEngine engine) {
|
||||
this.engine = java.util.Objects.requireNonNull(engine, "engine");
|
||||
}
|
||||
|
||||
public void enforce(ApiRequestContext context) {
|
||||
final ApiRequestRiskDecision decision;
|
||||
try {
|
||||
decision = engine.evaluate(context);
|
||||
} catch (RuntimeException exception) {
|
||||
log.warn("API request risk engine failed ({})", exception.getClass().getSimpleName());
|
||||
throw unavailable();
|
||||
}
|
||||
if (decision == null) throw unavailable();
|
||||
|
||||
switch (decision.action()) {
|
||||
case ALLOW -> { }
|
||||
case SHADOW -> log.info("API request risk shadow signal: rules={} protocol={} user={} key={} route={}",
|
||||
decision.ruleIds(), context.protocol(), context.userId(), context.apiKeyId(), context.routeId());
|
||||
case DENY -> {
|
||||
log.warn("API request blocked by risk policy: rules={} protocol={} user={} key={} route={}",
|
||||
decision.ruleIds(), context.protocol(), context.userId(), context.apiKeyId(), context.routeId());
|
||||
throw new ApiException(decision.code(), decision.publicMessage(), 403);
|
||||
}
|
||||
case THROTTLE -> {
|
||||
log.warn("API request throttled by risk policy: rules={} protocol={} user={} key={} route={}",
|
||||
decision.ruleIds(), context.protocol(), context.userId(), context.apiKeyId(), context.routeId());
|
||||
throw new GatewayLimitException(decision.code(), decision.publicMessage(), 429, decision.retryAfterSeconds());
|
||||
}
|
||||
case UNAVAILABLE -> throw unavailable();
|
||||
}
|
||||
}
|
||||
|
||||
private ApiException unavailable() {
|
||||
return new ApiException("RISK_ENGINE_UNAVAILABLE", "请求安全检查暂不可用,请稍后重试", 503);
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package com.moyi.controlplane.gateway.request;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
/** A deterministic policy over request metadata only. Rules never call providers or mutate billing state. */
|
||||
public interface ApiRequestRiskRule {
|
||||
String id();
|
||||
|
||||
Optional<ApiRequestRiskSignal> evaluate(ApiRequestContext context);
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package com.moyi.controlplane.gateway.request;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/** A rule's stable, public-safe decision; rule implementations must not put prompts or secrets in the message. */
|
||||
public record ApiRequestRiskSignal(Action action, String ruleId, String code, String publicMessage, int retryAfterSeconds) {
|
||||
public enum Action {
|
||||
DENY,
|
||||
THROTTLE
|
||||
}
|
||||
|
||||
public ApiRequestRiskSignal {
|
||||
Objects.requireNonNull(action, "action");
|
||||
if (ruleId == null || !ruleId.matches("[a-z][a-z0-9._-]{0,63}")) {
|
||||
throw new IllegalArgumentException("Risk rule ID is invalid");
|
||||
}
|
||||
if (code == null || !code.matches("[A-Z][A-Z0-9_]{1,63}")) {
|
||||
throw new IllegalArgumentException("Risk error code is invalid");
|
||||
}
|
||||
if (publicMessage == null || publicMessage.isBlank() || publicMessage.length() > 240) {
|
||||
throw new IllegalArgumentException("Risk public message must contain 1 to 240 characters");
|
||||
}
|
||||
if (action == Action.THROTTLE && retryAfterSeconds < 1) {
|
||||
throw new IllegalArgumentException("A throttle decision must include a positive retry delay");
|
||||
}
|
||||
if (action == Action.DENY && retryAfterSeconds != 0) {
|
||||
throw new IllegalArgumentException("A deny decision cannot include a retry delay");
|
||||
}
|
||||
}
|
||||
}
|
||||
+69
@@ -0,0 +1,69 @@
|
||||
package com.moyi.controlplane.gateway.request;
|
||||
|
||||
import java.util.Comparator;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
/** Composes independently testable rules without giving them access to billing or provider ports. */
|
||||
public final class CompositeApiRequestRiskEngine implements ApiRequestRiskEngine {
|
||||
public enum Mode {
|
||||
SHADOW,
|
||||
ENFORCE;
|
||||
|
||||
public static Mode parse(String value) {
|
||||
if (value == null) return SHADOW;
|
||||
return switch (value.trim().toLowerCase(java.util.Locale.ROOT)) {
|
||||
case "shadow" -> SHADOW;
|
||||
case "enforce" -> ENFORCE;
|
||||
default -> throw new IllegalArgumentException("GATEWAY_RISK_MODE must be 'shadow' or 'enforce'");
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
private final List<ApiRequestRiskRule> rules;
|
||||
private final Mode mode;
|
||||
|
||||
public CompositeApiRequestRiskEngine(List<ApiRequestRiskRule> rules, Mode mode) {
|
||||
this.rules = rules == null ? List.of() : rules.stream()
|
||||
.sorted(Comparator.comparing(ApiRequestRiskRule::id))
|
||||
.toList();
|
||||
this.mode = mode == null ? Mode.SHADOW : mode;
|
||||
var ids = new HashSet<String>();
|
||||
for (ApiRequestRiskRule rule : this.rules) {
|
||||
if (rule.id() == null || !rule.id().matches("[a-z][a-z0-9._-]{0,63}") || !ids.add(rule.id())) {
|
||||
throw new IllegalArgumentException("Risk rule IDs must be unique lowercase identifiers");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ApiRequestRiskDecision evaluate(ApiRequestContext context) {
|
||||
var signals = new java.util.ArrayList<ApiRequestRiskSignal>();
|
||||
for (ApiRequestRiskRule rule : rules) {
|
||||
try {
|
||||
Optional<ApiRequestRiskSignal> signal = rule.evaluate(context);
|
||||
if (signal.isPresent()) {
|
||||
if (!rule.id().equals(signal.get().ruleId())) {
|
||||
throw new IllegalStateException("Risk rule returned another rule's ID");
|
||||
}
|
||||
signals.add(signal.get());
|
||||
}
|
||||
} catch (RuntimeException exception) {
|
||||
if (mode == Mode.ENFORCE) return ApiRequestRiskDecision.unavailable(rule.id());
|
||||
signals.add(new ApiRequestRiskSignal(ApiRequestRiskSignal.Action.DENY,
|
||||
rule.id(), "RISK_RULE_UNAVAILABLE", "Risk rule evaluation is unavailable", 0));
|
||||
}
|
||||
}
|
||||
if (signals.isEmpty()) return ApiRequestRiskDecision.allow();
|
||||
if (mode == Mode.SHADOW) return ApiRequestRiskDecision.shadow(signals.stream().map(ApiRequestRiskSignal::ruleId).distinct().toList());
|
||||
|
||||
return signals.stream()
|
||||
.max(Comparator.comparingInt((ApiRequestRiskSignal signal) -> signal.action() == ApiRequestRiskSignal.Action.DENY ? 2 : 1)
|
||||
.thenComparingInt(ApiRequestRiskSignal::retryAfterSeconds))
|
||||
.map(signal -> signal.action() == ApiRequestRiskSignal.Action.DENY
|
||||
? ApiRequestRiskDecision.deny(signal)
|
||||
: ApiRequestRiskDecision.throttle(signal))
|
||||
.orElseGet(ApiRequestRiskDecision::allow);
|
||||
}
|
||||
}
|
||||
+129
@@ -0,0 +1,129 @@
|
||||
package com.moyi.controlplane.gateway.request;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.gateway.ChatMeter;
|
||||
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
/** Shared application boundary for synchronous and native-protocol model requests. */
|
||||
@Service
|
||||
public class GatewayRequestExecutionService {
|
||||
/** Wallet holds and Redis leases are short; a running call renews both on this cadence. */
|
||||
static final long RENEW_INTERVAL_MS = 60_000;
|
||||
static final long WALLET_HOLD_MS = 300_000;
|
||||
static final long LEASE_TTL_MS = 180_000;
|
||||
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(GatewayRequestExecutionService.class);
|
||||
private final ChatBillingService billing;
|
||||
private final ApiRequestRiskPolicy risk;
|
||||
private final long renewIntervalMs;
|
||||
private final java.util.concurrent.ScheduledExecutorService renewals = java.util.concurrent.Executors.newSingleThreadScheduledExecutor(r -> {
|
||||
var thread = new Thread(r, "request-lease-renewal"); thread.setDaemon(true); return thread;
|
||||
});
|
||||
|
||||
@org.springframework.beans.factory.annotation.Autowired
|
||||
public GatewayRequestExecutionService(ChatBillingService billing, ApiRequestRiskPolicy risk) {
|
||||
this(billing, risk, RENEW_INTERVAL_MS);
|
||||
}
|
||||
|
||||
GatewayRequestExecutionService(ChatBillingService billing, ApiRequestRiskPolicy risk, long renewIntervalMs) {
|
||||
this.billing = java.util.Objects.requireNonNull(billing, "billing");
|
||||
this.risk = java.util.Objects.requireNonNull(risk, "risk");
|
||||
if (renewIntervalMs < 1) throw new IllegalArgumentException("Renewal interval must be positive");
|
||||
this.renewIntervalMs = renewIntervalMs;
|
||||
}
|
||||
|
||||
@jakarta.annotation.PreDestroy
|
||||
void shutdown() { renewals.shutdownNow(); }
|
||||
|
||||
public <T> T execute(
|
||||
ApiRequestContext context,
|
||||
Map<String, Object> key,
|
||||
Map<String, Object> model,
|
||||
ChatMeter.Request normalizedRequest,
|
||||
String idempotencyKey,
|
||||
Supplier<? extends ApiRequestAdmission> admission,
|
||||
RequestWork<T> work,
|
||||
Function<Exception, ApiException> classifyFailure,
|
||||
Function<ApiException, T> deliverFailure
|
||||
) {
|
||||
risk.enforce(context);
|
||||
try (ApiRequestAdmission lease = admission.get()) {
|
||||
ChatBillingService.Ticket ticket = billing.reserve(key, model, normalizedRequest, idempotencyKey, context.metadata());
|
||||
// Long calls outlive the initial hold and lease. Keep both alive until the work returns,
|
||||
// so expiry recovery cannot release a running call and other callers cannot take its slot.
|
||||
var renewal = renewals.scheduleWithFixedDelay(() -> renew(ticket, lease), renewIntervalMs, renewIntervalMs, java.util.concurrent.TimeUnit.MILLISECONDS);
|
||||
try {
|
||||
return work.execute(ticket);
|
||||
} catch (Exception failure) {
|
||||
if (failure instanceof SettlementPendingException pending) {
|
||||
// The provider completed but persistence failed. Do not classify that as a provider failure.
|
||||
// Recovery can retry automatically if prepareSettlement committed its normalized meter.
|
||||
return deliverFailure.apply(pending);
|
||||
}
|
||||
ApiException error = classifyFailure.apply(failure);
|
||||
billing.fail(ticket, error.getCode(), error.getMessage(), context.elapsedMillis());
|
||||
return deliverFailure.apply(error);
|
||||
} finally {
|
||||
renewal.cancel(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void renew(ChatBillingService.Ticket ticket, ApiRequestAdmission lease) {
|
||||
try {
|
||||
billing.renew(ticket.id(), WALLET_HOLD_MS);
|
||||
if (!lease.renew(LEASE_TTL_MS)) log.warn("Request concurrency lease could not be renewed; it may be reused before the call ends");
|
||||
} catch (RuntimeException exception) {
|
||||
// A missed renewal is retried on the next tick; the original hold still bounds recovery.
|
||||
log.warn("Request lease renewal failed ({})", exception.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
|
||||
public BigDecimal settle(ChatBillingService.Ticket ticket, Map<String, Object> result, int latencyMs, Integer firstTokenMs) {
|
||||
// Validate and reduce to the only data required to price the call. The response body may contain
|
||||
// prompts, generated content, tool arguments or other sensitive data and is never recovery state.
|
||||
ChatMeter.Tokens tokens = ChatMeter.tokens(result);
|
||||
Map<String,Object> usage = new LinkedHashMap<>();
|
||||
usage.put("prompt_tokens", tokens.prompt());
|
||||
usage.put("completion_tokens", tokens.completion());
|
||||
usage.put("prompt_cache_hit_tokens", tokens.cached());
|
||||
long cacheWrite = tokens.cacheWrite5m() + tokens.cacheWrite1h();
|
||||
if (cacheWrite > 0) {
|
||||
usage.put("cache_creation_input_tokens", cacheWrite);
|
||||
usage.put("cache_creation", Map.of(
|
||||
"ephemeral_5m_input_tokens", tokens.cacheWrite5m(),
|
||||
"ephemeral_1h_input_tokens", tokens.cacheWrite1h()));
|
||||
}
|
||||
Map<String,Object> meter = Map.of("usage", usage);
|
||||
try {
|
||||
billing.prepareSettlement(ticket, meter, latencyMs, firstTokenMs);
|
||||
return billing.settle(ticket, result, latencyMs, firstTokenMs);
|
||||
} catch (org.springframework.dao.DataAccessException | org.springframework.transaction.TransactionException failure) {
|
||||
throw new SettlementPendingException();
|
||||
}
|
||||
}
|
||||
|
||||
public void endpoint(ChatBillingService.Ticket ticket, String endpoint) {
|
||||
billing.endpoint(ticket.id(), endpoint);
|
||||
}
|
||||
|
||||
public void observedUsage(ChatBillingService.Ticket ticket, String protocol, Object usage) {
|
||||
billing.observedUsage(ticket, protocol, usage);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface RequestWork<T> {
|
||||
T execute(ChatBillingService.Ticket ticket) throws Exception;
|
||||
}
|
||||
|
||||
public static final class SettlementPendingException extends ApiException {
|
||||
private SettlementPendingException() {
|
||||
super("SETTLEMENT_PENDING", "调用已完成,账务正在核对;请稍后查看使用记录", 503);
|
||||
}
|
||||
}
|
||||
}
|
||||
+45
-73
@@ -4,6 +4,8 @@ import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.security.SecretCipher;
|
||||
import com.moyi.controlplane.provider.credentials.UpstreamCredentialResolver;
|
||||
import com.moyi.controlplane.provider.transport.SseEventReader;
|
||||
import java.net.URI;
|
||||
import java.net.SocketTimeoutException;
|
||||
import java.net.http.HttpClient;
|
||||
@@ -16,7 +18,6 @@ import java.time.Duration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.ArrayList;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
@@ -29,6 +30,8 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class OpenAiCompatibleProvider {
|
||||
/** Upper bound for the configurable total deadline; leases are renewed at most this long. */
|
||||
public static final int MAX_TIMEOUT_SECONDS = 3600;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final HttpClient httpClient;
|
||||
private final JdbcTemplate jdbc;
|
||||
@@ -40,6 +43,10 @@ public class OpenAiCompatibleProvider {
|
||||
private final ConcurrentMap<String, AtomicInteger> keyCursors = new ConcurrentHashMap<>();
|
||||
private final ScheduledExecutorService deadlines = Executors.newSingleThreadScheduledExecutor(r -> { var t=new Thread(r,"upstream-deadlines"); t.setDaemon(true); return t; });
|
||||
private final int timeoutSeconds;
|
||||
// Long reasoning calls need a larger total deadline without letting a silent upstream hold
|
||||
// the request, its concurrency lease and wallet hold. Both default to the total deadline.
|
||||
private final long firstByteTimeoutMs;
|
||||
private final long idleTimeoutMs;
|
||||
@org.springframework.beans.factory.annotation.Autowired private UpstreamHealth health;
|
||||
|
||||
public OpenAiCompatibleProvider(
|
||||
@@ -53,7 +60,9 @@ public class OpenAiCompatibleProvider {
|
||||
this.jdbc = jdbc;
|
||||
this.secretCipher = secretCipher;
|
||||
this.environment = environment;
|
||||
this.timeoutSeconds = Math.max(1,Math.min(90,environment.getProperty("moyi.gateway.upstream-timeout-seconds",Integer.class,90)));
|
||||
this.timeoutSeconds = Math.max(1,Math.min(MAX_TIMEOUT_SECONDS,environment.getProperty("moyi.gateway.upstream-timeout-seconds",Integer.class,90)));
|
||||
this.firstByteTimeoutMs = 1000L*Math.max(1,Math.min(timeoutSeconds,environment.getProperty("moyi.gateway.upstream-first-byte-timeout-seconds",Integer.class,timeoutSeconds)));
|
||||
this.idleTimeoutMs = 1000L*Math.max(1,Math.min(timeoutSeconds,environment.getProperty("moyi.gateway.upstream-idle-timeout-seconds",Integer.class,timeoutSeconds)));
|
||||
this.providers = Map.of(
|
||||
"provider-deepseek", configured("provider-deepseek", environment.getProperty("moyi.providers.deepseek.api-key", ""), environment.getProperty("moyi.providers.deepseek.base-url", "https://api.deepseek.com"), environment),
|
||||
"provider-kimi", configured("provider-kimi", environment.getProperty("moyi.providers.kimi.api-key", ""), environment.getProperty("moyi.providers.kimi.base-url", "https://api.moonshot.ai/v1"), environment),
|
||||
@@ -156,7 +165,7 @@ public class OpenAiCompatibleProvider {
|
||||
String json = writeJson(body);
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder()
|
||||
.uri(URI.create(base + path))
|
||||
.timeout(Duration.ofMillis(remaining))
|
||||
.timeout(Duration.ofMillis(streaming?Math.min(remaining,firstByteTimeoutMs):remaining))
|
||||
.header("Authorization", "Bearer " + apiKey)
|
||||
.header("Content-Type", "application/json")
|
||||
.POST(HttpRequest.BodyPublishers.ofString(json));
|
||||
@@ -169,7 +178,7 @@ public class OpenAiCompatibleProvider {
|
||||
if(health!=null) health.record(healthRoute,apiKey,response.statusCode(),(int)TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-started));
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||
String details;
|
||||
try(var bounded=new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-overallStarted)))) {details=readErrorBody(bounded.body);}
|
||||
try(var bounded=new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-overallStarted)),false)) {details=readErrorBody(bounded.body);}
|
||||
if (requiresLargerOutputBudget(details)) {
|
||||
throw new ApiException("INVALID_MAX_TOKENS", "上游模型要求 max_tokens 大于 1024,请提高输出 Token 上限后重试", 400);
|
||||
}
|
||||
@@ -189,7 +198,7 @@ public class OpenAiCompatibleProvider {
|
||||
response.body().close();
|
||||
throw new ApiException("UPSTREAM_STREAM_UNSUPPORTED","上游未返回 SSE 流式响应",502);
|
||||
}
|
||||
return new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-overallStarted)));
|
||||
return new StreamResponse(response.body(),Math.max(1,timeoutSeconds*1000L-TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-overallStarted)),streaming);
|
||||
} catch (ApiException exception) {
|
||||
throw exception;
|
||||
} catch (HttpTimeoutException | SocketTimeoutException exception) {
|
||||
@@ -232,47 +241,39 @@ public class OpenAiCompatibleProvider {
|
||||
private final InputStream body;
|
||||
private final AtomicBoolean expired=new AtomicBoolean();
|
||||
private final ScheduledFuture<?> deadline;
|
||||
private final boolean idleWatch;
|
||||
private volatile ScheduledFuture<?> idle;
|
||||
private volatile boolean closed;
|
||||
private Runnable afterClose=()->{};
|
||||
public StreamResponse onClose(Runnable callback) {afterClose=callback;return this;}
|
||||
private StreamResponse(InputStream body,long remainingMs) {
|
||||
this.body=body;
|
||||
this.deadline=deadlines.schedule(() -> { expired.set(true); try { body.close(); } catch(IOException ignored) {} },remainingMs,TimeUnit.MILLISECONDS);
|
||||
private StreamResponse(InputStream raw,long remainingMs,boolean streaming) {
|
||||
Runnable expire=() -> { expired.set(true); try { raw.close(); } catch(IOException ignored) {} };
|
||||
// The idle watchdog only matters when it is shorter than the remaining total deadline.
|
||||
this.idleWatch=streaming&&idleTimeoutMs<remainingMs;
|
||||
this.body=!idleWatch?raw:new FilterInputStream(raw) {
|
||||
@Override public int read() throws IOException { int value=super.read(); if(value!=-1) touch(expire); return value; }
|
||||
@Override public int read(byte[] buffer,int offset,int length) throws IOException { int count=super.read(buffer,offset,length); if(count>0) touch(expire); return count; }
|
||||
};
|
||||
this.deadline=deadlines.schedule(expire,remainingMs,TimeUnit.MILLISECONDS);
|
||||
if(idleWatch) touch(expire);
|
||||
}
|
||||
/** Received bytes prove the upstream is alive; restart the idle window. */
|
||||
private void touch(Runnable expire) {
|
||||
if(closed) return;
|
||||
ScheduledFuture<?> previous=idle;
|
||||
idle=deadlines.schedule(expire,idleTimeoutMs,TimeUnit.MILLISECONDS);
|
||||
if(previous!=null) previous.cancel(false);
|
||||
}
|
||||
public void events(EventConsumer consumer) throws IOException {
|
||||
events(consumer,true);
|
||||
}
|
||||
public void events(EventConsumer consumer,boolean requireDone) throws IOException {
|
||||
var reader=new BufferedReader(new InputStreamReader(body,StandardCharsets.UTF_8));
|
||||
StringBuilder event=new StringBuilder(), line=new StringBuilder();
|
||||
long total=0;
|
||||
int c;
|
||||
try {
|
||||
// Bound both individual frames and the whole response before allocating unbounded strings.
|
||||
while((c=reader.read())!=-1) {
|
||||
if(expired.get()) throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT","上游响应超时,本次未扣费",504);
|
||||
if(++total>4*1024*1024 || line.length()>262144 || event.length()>262144) throw new ApiException("UPSTREAM_RESPONSE_TOO_LARGE","上游响应超过限制",502);
|
||||
if(c=='\n') {
|
||||
String text=line.toString(); line.setLength(0);
|
||||
if(text.endsWith("\r")) text=text.substring(0,text.length()-1);
|
||||
if(text.isEmpty()) {
|
||||
if(!event.isEmpty()) {
|
||||
String data=event.substring(0,event.length()-1); event.setLength(0);
|
||||
if("[DONE]".equals(data)) return;
|
||||
Map<String,Object> chunk=objectMapper.readValue(data,new com.fasterxml.jackson.core.type.TypeReference<>(){});
|
||||
if(chunk.containsKey("error")) throw classifyUpstreamError(502, writeJson(chunk));
|
||||
consumer.accept(chunk);
|
||||
}
|
||||
} else if(text.startsWith("data:")) event.append(text.substring(5).stripLeading()).append('\n');
|
||||
} else line.append((char)c);
|
||||
}
|
||||
} catch(IOException exception) {
|
||||
if(expired.get()) throw new ApiException("UPSTREAM_PROVIDER_TIMEOUT","上游响应超时,本次未扣费",504);
|
||||
throw exception;
|
||||
}
|
||||
if(!requireDone && !expired.get() && event.isEmpty() && line.isEmpty()) return;
|
||||
throw new ApiException(expired.get()?"UPSTREAM_PROVIDER_TIMEOUT":"UPSTREAM_STREAM_INCOMPLETE","上游流式响应中断,本次未扣费",502);
|
||||
new SseEventReader(objectMapper).read(body, expired::get, requireDone, chunk -> {
|
||||
if (chunk.containsKey("error")) throw classifyUpstreamError(502, writeJson(chunk));
|
||||
consumer.accept(chunk);
|
||||
});
|
||||
}
|
||||
@Override public void close() { deadline.cancel(false); try { body.close(); } catch(IOException ignored) {} finally {afterClose.run();afterClose=()->{};} }
|
||||
@Override public void close() { closed=true; deadline.cancel(false); if(idle!=null) idle.cancel(false); try { body.close(); } catch(IOException ignored) {} finally {afterClose.run();afterClose=()->{};} }
|
||||
}
|
||||
@FunctionalInterface public interface EventConsumer { void accept(Map<String,Object> chunk) throws IOException; }
|
||||
@PreDestroy public void close() { deadlines.shutdownNow(); }
|
||||
@@ -351,25 +352,17 @@ public class OpenAiCompatibleProvider {
|
||||
}
|
||||
|
||||
private ProviderConfig configured(String providerId, String providerCode, String fallbackKey, String baseUrl, org.springframework.core.env.Environment environment, AtomicInteger cursor) {
|
||||
List<String> keys = new ArrayList<>();
|
||||
if (fallbackKey != null && !fallbackKey.isBlank()) keys.add(fallbackKey.trim());
|
||||
addEnvironmentKeys(keys, providerId, environment);
|
||||
if (providerCode != null && !providerCode.isBlank() && !providerCode.equals(providerId)) addEnvironmentKeys(keys, providerCode, environment);
|
||||
return new ProviderConfig(providerId, providerCode, keys.stream().distinct().toList(), baseUrl, cursor);
|
||||
}
|
||||
|
||||
private void addEnvironmentKeys(List<String> keys, String alias, org.springframework.core.env.Environment environment) {
|
||||
String envName = "MOYI_UPSTREAM_API_KEYS_" + alias.toUpperCase(java.util.Locale.ROOT).replace('-', '_');
|
||||
String configured = environment == null ? null : environment.getProperty(envName);
|
||||
if (configured != null && !configured.isBlank()) for (String key : configured.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
||||
List<String> keys = new UpstreamCredentialResolver(environment).providerKeys(fallbackKey, providerId, providerCode);
|
||||
return new ProviderConfig(providerId, providerCode, keys, baseUrl, cursor);
|
||||
}
|
||||
|
||||
private record ProviderConfig(String alias, String providerCode, List<String> apiKeys, String baseUrl, AtomicInteger cursor) {
|
||||
@Override public String toString() { return "ProviderConfig[alias=" + alias + ", credentialCount=" + apiKeys.size() + "]"; }
|
||||
private boolean hasKey() { return !apiKeys.isEmpty(); }
|
||||
private boolean hasKey(String routeId, String upstreamModel, org.springframework.core.env.Environment environment) {
|
||||
return !apiKeys.isEmpty() || !routeKeys(routeId, upstreamModel, environment).isEmpty();
|
||||
}
|
||||
private String nextApiKey() { return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size())); }
|
||||
private String nextApiKey() { return UpstreamCredentialResolver.nextKey(apiKeys, cursor); }
|
||||
|
||||
private String nextApiKey(String routeId, String upstreamModel, org.springframework.core.env.Environment environment,
|
||||
ConcurrentMap<String, AtomicInteger> cursors) {
|
||||
@@ -377,32 +370,11 @@ public class OpenAiCompatibleProvider {
|
||||
if (routeKeys.isEmpty()) return nextApiKey();
|
||||
String cursorKey = "route:" + (routeId == null ? "" : routeId);
|
||||
AtomicInteger routeCursor = cursors.computeIfAbsent(cursorKey, ignored -> new AtomicInteger());
|
||||
return routeKeys.get(Math.floorMod(routeCursor.getAndIncrement(), routeKeys.size()));
|
||||
return UpstreamCredentialResolver.nextKey(routeKeys, routeCursor);
|
||||
}
|
||||
|
||||
private List<String> routeKeys(String routeId, String upstreamModel, org.springframework.core.env.Environment environment) {
|
||||
if (environment == null) return List.of();
|
||||
if (routeId != null && !routeId.isBlank()) {
|
||||
String value = environment.getProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_" + normalize(routeId));
|
||||
if (value != null && !value.isBlank()) return parseKeys(value);
|
||||
}
|
||||
if (upstreamModel != null && !upstreamModel.isBlank()) {
|
||||
String scopedName = "MOYI_UPSTREAM_API_KEYS_MODEL_" + normalize(providerCode) + "_" + normalize(upstreamModel);
|
||||
String value = environment.getProperty(scopedName);
|
||||
if (value == null || value.isBlank()) return List.of();
|
||||
return parseKeys(value);
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
|
||||
private static List<String> parseKeys(String value) {
|
||||
List<String> keys = new ArrayList<>();
|
||||
for (String key : value.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
||||
return keys.stream().distinct().toList();
|
||||
}
|
||||
|
||||
private static String normalize(String value) {
|
||||
return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_");
|
||||
return new UpstreamCredentialResolver(environment).scopedKeys(providerCode, routeId, upstreamModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
+68
@@ -0,0 +1,68 @@
|
||||
package com.moyi.controlplane.provider.credentials;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.springframework.core.env.Environment;
|
||||
|
||||
/**
|
||||
* Shared, side-effect-free credential pool resolution for sync and async providers.
|
||||
* Selection state belongs to the caller so resolving a dynamic DB channel never resets rotation.
|
||||
* This component neither logs credentials nor decides retries, health cooldowns or task affinity.
|
||||
*/
|
||||
public final class UpstreamCredentialResolver {
|
||||
private final Environment environment;
|
||||
|
||||
public UpstreamCredentialResolver(Environment environment) {
|
||||
this.environment = environment;
|
||||
}
|
||||
|
||||
/** Preserve the supplied alias ordering; the encrypted/single configured key remains first. */
|
||||
public List<String> providerKeys(String fallbackKey, String... aliases) {
|
||||
var keys = new LinkedHashSet<String>();
|
||||
if (fallbackKey != null && !fallbackKey.isBlank()) keys.add(fallbackKey.trim());
|
||||
for (String alias : aliases) {
|
||||
if (alias == null || alias.isBlank()) continue;
|
||||
// Keep the existing provider alias naming convention for deployed environment variables.
|
||||
keys.addAll(environmentKeys("MOYI_UPSTREAM_API_KEYS_" + alias.toUpperCase(Locale.ROOT).replace('-', '_')));
|
||||
}
|
||||
return List.copyOf(keys);
|
||||
}
|
||||
|
||||
/** Explicit property names are used only for provider-specific official-key fallbacks. */
|
||||
public List<String> environmentKeys(String propertyName) {
|
||||
return parseKeys(environment == null ? null : environment.getProperty(propertyName));
|
||||
}
|
||||
|
||||
/** A route override wins over a provider+model override. Neither is combined with channel keys. */
|
||||
public List<String> scopedKeys(String providerCode, String routeId, String upstreamModel) {
|
||||
if (environment == null) return List.of();
|
||||
if (routeId != null && !routeId.isBlank()) {
|
||||
String value = environment.getProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_" + normalize(routeId));
|
||||
if (value != null && !value.isBlank()) return parseKeys(value);
|
||||
}
|
||||
if (upstreamModel != null && !upstreamModel.isBlank()) {
|
||||
return environmentKeys("MOYI_UPSTREAM_API_KEYS_MODEL_" + normalize(providerCode) + "_" + normalize(upstreamModel));
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
|
||||
/** Stable order, no blank entries or duplicates; immutable so callers cannot corrupt a pool. */
|
||||
public static List<String> parseKeys(String value) {
|
||||
if (value == null || value.isBlank()) return List.of();
|
||||
var keys = new LinkedHashSet<String>();
|
||||
for (String key : value.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
||||
return List.copyOf(keys);
|
||||
}
|
||||
|
||||
public static String nextKey(List<String> keys, AtomicInteger cursor) {
|
||||
if (keys.isEmpty()) throw new ApiException("UPSTREAM_KEY_NOT_AVAILABLE", "渠道没有可用的上游凭据", 503);
|
||||
return keys.get(Math.floorMod(cursor.getAndIncrement(), keys.size()));
|
||||
}
|
||||
|
||||
private static String normalize(String value) {
|
||||
return (value == null ? "" : value).toUpperCase(Locale.ROOT).replaceAll("[^A-Z0-9]", "_");
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.moyi.controlplane.provider.media;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
|
||||
/** Shared catalog/execution contract. Audio model type alone does not imply a speech endpoint. */
|
||||
public final class AsyncMediaSupport {
|
||||
private AsyncMediaSupport() {}
|
||||
private static final Set<String> SPEECH_MODELS = Set.of(
|
||||
"tts-1", "tts-1-1106", "tts-1-hd", "tts-1-hd-1106", "gpt-4o-mini-tts");
|
||||
|
||||
public static boolean supportsSpeech(String upstreamModel) {
|
||||
if (upstreamModel == null) return false;
|
||||
String model = upstreamModel.toLowerCase(Locale.ROOT);
|
||||
return SPEECH_MODELS.contains(model) || model.matches("gpt-4o-mini-tts-[0-9]{4}-[0-9]{2}-[0-9]{2}");
|
||||
}
|
||||
}
|
||||
+142
@@ -0,0 +1,142 @@
|
||||
package com.moyi.controlplane.provider.media;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
|
||||
/** Pure media-result boundary. Does not fetch URLs, resolve credentials or change settlement. */
|
||||
public final class MediaAssetNormalizer {
|
||||
private static final Map<String,String> FORMAT_MIMES = Map.ofEntries(
|
||||
Map.entry("png", "image/png"), Map.entry("jpg", "image/jpeg"), Map.entry("jpeg", "image/jpeg"),
|
||||
Map.entry("webp", "image/webp"), Map.entry("gif", "image/gif"), Map.entry("avif", "image/avif"),
|
||||
Map.entry("mp4", "video/mp4"), Map.entry("webm", "video/webm"), Map.entry("mov", "video/quicktime"),
|
||||
Map.entry("mp3", "audio/mpeg"), Map.entry("wav", "audio/wav"), Map.entry("ogg", "audio/ogg"),
|
||||
Map.entry("opus", "audio/ogg"), Map.entry("aac", "audio/aac"), Map.entry("flac", "audio/flac"),
|
||||
Map.entry("m4a", "audio/mp4"), Map.entry("glb", "model/gltf-binary"), Map.entry("gltf", "model/gltf+json"));
|
||||
|
||||
private MediaAssetNormalizer() {}
|
||||
|
||||
public static Map<String,Object> normalize(Object raw, String taskType) {
|
||||
if (raw instanceof String) {
|
||||
String url = reference(raw, taskType);
|
||||
return url == null ? null : new LinkedHashMap<>(Map.of("url", url));
|
||||
}
|
||||
if (!(raw instanceof Map<?,?> source)) return null;
|
||||
var result = new LinkedHashMap<String,Object>();
|
||||
String[][] aliases = {{"modelUrl","modelUrl"},{"model_url","modelUrl"},
|
||||
{"previewUrl","previewUrl"},{"rendered_image_url","previewUrl"},{"preview_url","previewUrl"},{"last_frame_url","previewUrl"},
|
||||
{"imageUrl","imageUrl"},{"image_url","imageUrl"},{"generated_image_url","imageUrl"},{"image","imageUrl"},
|
||||
{"videoUrl","videoUrl"},{"video_url","videoUrl"},{"video","videoUrl"},
|
||||
{"audioUrl","audioUrl"},{"audio_url","audioUrl"},{"audio","audioUrl"}};
|
||||
for (String[] alias : aliases) {
|
||||
String kind = switch (alias[1]) {
|
||||
case "modelUrl" -> "3d";
|
||||
case "videoUrl" -> "video";
|
||||
case "audioUrl" -> "audio";
|
||||
default -> "image";
|
||||
};
|
||||
String value = reference(source.get(alias[0]), kind);
|
||||
if (value != null) result.putIfAbsent(alias[1], value);
|
||||
}
|
||||
String url = reference(source.get("url"), taskType);
|
||||
String payload = payload(source.get("b64_json"));
|
||||
if (payload == null) payload = payload(source.get("base64"));
|
||||
String mime = mime(source);
|
||||
if (mime == null && payload != null) mime = imageSignatureMime(payload);
|
||||
if (url != null && (mime == null || matchesKind(mime, taskType))) result.put("url", url);
|
||||
if (payload != null && mime != null && matchesKind(mime, taskType)) {
|
||||
result.put("base64", payload);
|
||||
result.put("mimeType", mime);
|
||||
}
|
||||
if (source.get("format") instanceof String format && format.length() <= 40 && (mime == null || matchesKind(mime, taskType))) result.put("format", format.toLowerCase(Locale.ROOT));
|
||||
if (mime != null && matchesKind(mime, taskType)) result.putIfAbsent("mimeType", mime);
|
||||
// A thumbnail or another media lane is not proof that this task's primary file was delivered.
|
||||
List<String> primary = switch (taskType == null ? "" : taskType.toLowerCase(Locale.ROOT)) {
|
||||
case "video" -> List.of("videoUrl");
|
||||
case "audio" -> List.of("audioUrl");
|
||||
case "image" -> List.of("imageUrl");
|
||||
case "3d" -> List.of("modelUrl");
|
||||
default -> List.of("modelUrl", "videoUrl", "audioUrl", "imageUrl");
|
||||
};
|
||||
if (!result.containsKey("url")) {
|
||||
for (String field : primary) if (result.containsKey(field)) { result.put("url", result.get(field)); break; }
|
||||
}
|
||||
if (!result.containsKey("url") && !result.containsKey("base64")) return null;
|
||||
for (String field : List.of("usage", "usageMetadata")) if (source.get(field) instanceof Map<?,?> value) result.put(field, value);
|
||||
Object credits = source.get("creditsConsumed");
|
||||
if (credits == null) credits = source.get("credits_consumed");
|
||||
if (credits instanceof Number || credits instanceof String) result.put("creditsConsumed", credits);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static String reference(Object raw, String kind) {
|
||||
return reference(raw, kind, 0);
|
||||
}
|
||||
|
||||
private static String reference(Object raw, String kind, int depth) {
|
||||
if (depth > 4) return null;
|
||||
if (raw instanceof Map<?,?> object) {
|
||||
String url = reference(object.get("url"), kind, depth + 1);
|
||||
if (url != null) return url;
|
||||
String payload = payload(object.get("b64_json"));
|
||||
if (payload == null) payload = payload(object.get("base64"));
|
||||
String mime = mime(object);
|
||||
if (mime == null && payload != null) mime = imageSignatureMime(payload);
|
||||
return payload != null && mime != null && matchesKind(mime, kind) ? "data:" + mime + ";base64," + payload : null;
|
||||
}
|
||||
if (!(raw instanceof String text)) return null;
|
||||
String value = text.trim();
|
||||
if (value.isEmpty() || value.indexOf('\\') >= 0 || value.chars().anyMatch(Character::isISOControl)) return null;
|
||||
if (value.regionMatches(true, 0, "data:", 0, 5)) {
|
||||
int marker = value.toLowerCase(Locale.ROOT).indexOf(";base64,");
|
||||
if (marker < 0) return null;
|
||||
String mime = validMime(value.substring(5, marker));
|
||||
String payload = payload(value.substring(marker + 8));
|
||||
return mime != null && payload != null && matchesKind(mime, kind) ? "data:" + mime + ";base64," + payload : null;
|
||||
}
|
||||
try {
|
||||
URI uri = URI.create(value);
|
||||
String path = uri.getPath();
|
||||
String suffix = path == null ? "" : path.substring(path.lastIndexOf('.') + 1).toLowerCase(Locale.ROOT);
|
||||
String knownMime = FORMAT_MIMES.get(suffix);
|
||||
return ("https".equalsIgnoreCase(uri.getScheme()) || "http".equalsIgnoreCase(uri.getScheme()))
|
||||
&& uri.getHost() != null && uri.getUserInfo() == null
|
||||
&& (knownMime == null || matchesKind(knownMime, kind)) ? value : null;
|
||||
} catch (IllegalArgumentException error) { return null; }
|
||||
}
|
||||
|
||||
private static String payload(Object raw) {
|
||||
if (!(raw instanceof String text) || text.length() > 12 * 1024 * 1024) return null;
|
||||
String value = text.replaceAll("\\s+", "");
|
||||
return !value.isEmpty() && value.length() % 4 != 1 && value.matches("[a-zA-Z0-9+/]+={0,2}") ? value : null;
|
||||
}
|
||||
|
||||
private static String mime(Map<?,?> source) {
|
||||
String value = validMime(source.get("mimeType"));
|
||||
if (value == null) value = validMime(source.get("mime_type"));
|
||||
if (value == null && source.get("format") instanceof String format) value = FORMAT_MIMES.get(format.toLowerCase(Locale.ROOT));
|
||||
return value;
|
||||
}
|
||||
|
||||
private static String validMime(Object raw) {
|
||||
if (!(raw instanceof String text)) return null;
|
||||
String value = text.trim().toLowerCase(Locale.ROOT);
|
||||
if (List.of("image/png", "image/jpeg", "image/webp", "image/gif", "image/avif", "model/gltf-binary", "model/gltf+json").contains(value)) return value;
|
||||
return value.matches("(audio|video)/[a-z0-9.+-]+") && !value.endsWith("xml") ? value : null;
|
||||
}
|
||||
|
||||
private static boolean matchesKind(String mime, String kind) {
|
||||
if (kind == null || kind.isBlank()) return true;
|
||||
return mime.startsWith(("3d".equalsIgnoreCase(kind) ? "model" : kind.toLowerCase(Locale.ROOT)) + "/");
|
||||
}
|
||||
|
||||
private static String imageSignatureMime(String payload) {
|
||||
// Infer only unambiguous image signatures; never guess the format of arbitrary base64 text.
|
||||
if (payload.startsWith("iVBORw0KGgo")) return "image/png";
|
||||
if (payload.startsWith("/9j/")) return "image/jpeg";
|
||||
if (payload.startsWith("R0lGODdh") || payload.startsWith("R0lGODlh")) return "image/gif";
|
||||
return null;
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.moyi.controlplane.provider.media;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.provider.transport.BoundedHttpTransport;
|
||||
import com.moyi.controlplane.provider.transport.PinnedHttpsTransport;
|
||||
import java.time.Duration;
|
||||
import java.util.Locale;
|
||||
|
||||
/** Public reference-image input only. No provider credentials, routing, retries or billing. */
|
||||
public final class MediaDownloader {
|
||||
private static final int MAX_BYTES = 8 * 1024 * 1024;
|
||||
private final PublicMediaAddressPolicy policy = new PublicMediaAddressPolicy();
|
||||
private final PinnedHttpsTransport transport;
|
||||
private final PinnedHttpsTransport.AddressResolver resolver;
|
||||
|
||||
public MediaDownloader() { this(new PinnedHttpsTransport(), new PublicMediaAddressPolicy()::resolve); }
|
||||
MediaDownloader(PinnedHttpsTransport transport, PinnedHttpsTransport.AddressResolver resolver) {
|
||||
this.transport = transport;
|
||||
this.resolver = resolver;
|
||||
}
|
||||
|
||||
public record MediaBytes(byte[] bytes, String contentType, String filename) {
|
||||
@Override public String toString() { return "MediaBytes[type=" + contentType + ", bytes=" + bytes.length + "]"; }
|
||||
}
|
||||
|
||||
public MediaBytes download(String value) {
|
||||
java.net.URI uri;
|
||||
try { uri = policy.requireUri(value); }
|
||||
catch (IllegalArgumentException error) { throw invalidAddress(); }
|
||||
BoundedHttpTransport.Response response;
|
||||
try { response = transport.get(uri, resolver, Duration.ofSeconds(15), MAX_BYTES); }
|
||||
catch (BoundedHttpTransport.Failure failure) {
|
||||
if (failure.reason() == BoundedHttpTransport.Reason.INVALID_ADDRESS) throw invalidAddress();
|
||||
if (failure.reason() == BoundedHttpTransport.Reason.BODY_TOO_LARGE) throw new ApiException("MEDIA_TOO_LARGE", "媒体输入超过 8 MiB", 400);
|
||||
throw new ApiException("MEDIA_FETCH_FAILED", "无法在期限内读取媒体输入", 400);
|
||||
}
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||
throw new ApiException("MEDIA_FETCH_FAILED", "无法读取媒体输入(HTTP " + response.statusCode() + ")", 400);
|
||||
}
|
||||
if (response.body().length == 0) throw new ApiException("MEDIA_TOO_LARGE", "媒体输入为空或超过 8 MiB", 400);
|
||||
String contentType = response.headers().firstValue("Content-Type").orElse("application/octet-stream")
|
||||
.split(";", 2)[0].trim().toLowerCase(Locale.ROOT);
|
||||
if (!contentType.matches("image/[a-z0-9.+-]{1,64}")) {
|
||||
throw new ApiException("MEDIA_TYPE_UNSUPPORTED", "图像编辑和视频参考图必须是 image/* 文件", 400);
|
||||
}
|
||||
String extension = switch (contentType) {
|
||||
case "image/jpeg" -> ".jpg";
|
||||
case "image/png" -> ".png";
|
||||
case "image/webp" -> ".webp";
|
||||
case "image/gif" -> ".gif";
|
||||
default -> ".bin";
|
||||
};
|
||||
return new MediaBytes(response.body(), contentType, "input" + extension);
|
||||
}
|
||||
|
||||
private ApiException invalidAddress() {
|
||||
return new ApiException("INVALID_MEDIA_URL", "媒体地址必须是公开 HTTPS 地址,不能指向本机、内网或特殊用途地址", 400);
|
||||
}
|
||||
}
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
package com.moyi.controlplane.provider.media;
|
||||
|
||||
import com.moyi.controlplane.provider.transport.PinnedHttpsTransport;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.URI;
|
||||
|
||||
/** Fail closed for non-global and special-purpose targets, including mixed DNS answer sets. */
|
||||
final class PublicMediaAddressPolicy {
|
||||
URI requireUri(String value) {
|
||||
try {
|
||||
URI uri = URI.create(value);
|
||||
if (!"https".equalsIgnoreCase(uri.getScheme()) || uri.getHost() == null || uri.getHost().contains("%")
|
||||
|| uri.getUserInfo() != null || uri.getRawFragment() != null || uri.getPort() == 0 || uri.getPort() > 65535) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
return uri;
|
||||
} catch (RuntimeException error) { throw new IllegalArgumentException("Invalid public HTTPS media URL"); }
|
||||
}
|
||||
|
||||
InetAddress[] resolve(String host) throws IOException {
|
||||
return requirePublic(InetAddress.getAllByName(host));
|
||||
}
|
||||
|
||||
InetAddress[] requirePublic(InetAddress[] addresses) throws PinnedHttpsTransport.AddressRejectedException {
|
||||
if (addresses == null || addresses.length == 0 || addresses.length > 16) throw new PinnedHttpsTransport.AddressRejectedException();
|
||||
for (var address : addresses) if (address == null || !isPublic(address)) throw new PinnedHttpsTransport.AddressRejectedException();
|
||||
return addresses.clone();
|
||||
}
|
||||
|
||||
boolean isPublic(InetAddress address) {
|
||||
byte[] raw = address.getAddress();
|
||||
int a = Byte.toUnsignedInt(raw[0]), b = Byte.toUnsignedInt(raw[1]);
|
||||
if (raw.length == 4) {
|
||||
int c = Byte.toUnsignedInt(raw[2]), d = Byte.toUnsignedInt(raw[3]);
|
||||
return !(a == 0 || a == 10 || a == 127 || a >= 224
|
||||
|| a == 100 && b >= 64 && b <= 127
|
||||
|| a == 169 && b == 254 || a == 172 && b >= 16 && b <= 31
|
||||
|| a == 192 && (b == 168 || b == 0 && (c == 0 || c == 2) || b == 88 && c == 99)
|
||||
|| a == 198 && (b == 18 || b == 19 || b == 51 && c == 100)
|
||||
|| a == 203 && b == 0 && c == 113
|
||||
|| a == 168 && b == 63 && c == 129 && d == 16); // Azure platform virtual address
|
||||
}
|
||||
// Only native global unicast 2000::/3, excluding special/tunnel/documentation prefixes.
|
||||
// This also rejects ULA, link/site local, multicast, compatible/mapped IPv4 and well-known NAT64.
|
||||
if (raw.length != 16 || (a & 0xe0) != 0x20) return false;
|
||||
int c = Byte.toUnsignedInt(raw[2]), d = Byte.toUnsignedInt(raw[3]);
|
||||
return !(a == 0x20 && b == 0x01 && (c <= 1 || c == 0x0d && d == 0xb8)
|
||||
|| a == 0x20 && b == 0x02 // 6to4 can encode a private IPv4 target
|
||||
|| a == 0x3f && b == 0xfe // retired 6bone allocation
|
||||
|| a == 0x3f && b == 0xff && (c & 0xf0) == 0); // 3fff::/20 documentation
|
||||
}
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
package com.moyi.controlplane.provider.transport;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpHeaders;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.CompletionStage;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.Flow;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
/** One bounded HTTP attempt. No credentials, route selection, protocol mapping, retry, risk or billing policy. */
|
||||
public final class BoundedHttpTransport {
|
||||
private static final int ERROR_BODY_LIMIT = 64 * 1024;
|
||||
private final HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10))
|
||||
.followRedirects(HttpClient.Redirect.NEVER).build();
|
||||
|
||||
public record Response(int statusCode, HttpHeaders headers, byte[] body) {
|
||||
@Override public String toString() { return "TransportResponse[status=" + statusCode + ", bytes=" + body.length + "]"; }
|
||||
}
|
||||
public enum Reason { TIMEOUT, INTERRUPTED, BODY_TOO_LARGE, IO, INVALID_ADDRESS, BUSY }
|
||||
public static final class Failure extends IOException {
|
||||
private final Reason reason;
|
||||
private final Integer observedStatus;
|
||||
Failure(Reason reason, Integer status) {
|
||||
super("Bounded HTTP attempt failed: " + reason);
|
||||
this.reason = reason; this.observedStatus = status;
|
||||
}
|
||||
public Reason reason() { return reason; }
|
||||
public Integer observedStatus() { return observedStatus; }
|
||||
}
|
||||
|
||||
public Response send(HttpRequest request, int maxResponseBytes) throws Failure {
|
||||
if (maxResponseBytes < 1 || maxResponseBytes > 32 * 1024 * 1024) throw new IllegalArgumentException("Invalid response limit");
|
||||
Duration timeout = request.timeout().orElseThrow(() -> new IllegalArgumentException("An explicit total HTTP deadline is required"));
|
||||
var status = new AtomicInteger();
|
||||
var subscriber = new AtomicReference<LimitedBody>();
|
||||
CompletableFuture<HttpResponse<byte[]>> attempt = client.sendAsync(request, info -> {
|
||||
status.set(info.statusCode());
|
||||
int limit = info.statusCode() >= 200 && info.statusCode() < 300 ? maxResponseBytes : Math.min(maxResponseBytes, ERROR_BODY_LIMIT);
|
||||
var body = new LimitedBody(limit, info.statusCode(), info.headers().firstValueAsLong("Content-Length").orElse(-1));
|
||||
subscriber.set(body);
|
||||
return body;
|
||||
});
|
||||
try {
|
||||
// Unlike a header-only timeout, this also bounds a server that drips or never finishes the body.
|
||||
HttpResponse<byte[]> response = attempt.get(Math.max(1, timeout.toMillis()), TimeUnit.MILLISECONDS);
|
||||
return new Response(response.statusCode(), response.headers(), response.body());
|
||||
} catch (TimeoutException exception) {
|
||||
throw cancel(attempt, subscriber.get(), Reason.TIMEOUT, status.get());
|
||||
} catch (InterruptedException exception) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw cancel(attempt, subscriber.get(), Reason.INTERRUPTED, status.get());
|
||||
} catch (ExecutionException exception) {
|
||||
Throwable cause = exception.getCause();
|
||||
while (cause instanceof java.util.concurrent.CompletionException && cause.getCause() != null) cause = cause.getCause();
|
||||
if (cause instanceof Failure failure) throw failure;
|
||||
throw cancel(attempt, subscriber.get(), cause instanceof java.net.http.HttpTimeoutException ? Reason.TIMEOUT : Reason.IO, status.get());
|
||||
}
|
||||
}
|
||||
|
||||
private Failure cancel(CompletableFuture<?> attempt, LimitedBody body, Reason reason, int status) {
|
||||
Failure failure = new Failure(reason, status == 0 ? null : status);
|
||||
if (body != null) body.cancel(failure);
|
||||
attempt.cancel(true);
|
||||
return failure;
|
||||
}
|
||||
|
||||
private static final class LimitedBody implements HttpResponse.BodySubscriber<byte[]> {
|
||||
private final CompletableFuture<byte[]> completed = new CompletableFuture<>();
|
||||
private final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
|
||||
private final int limit, status;
|
||||
private final long declaredLength;
|
||||
private Flow.Subscription subscription;
|
||||
private int received;
|
||||
|
||||
LimitedBody(int limit, int status, long declaredLength) { this.limit = limit; this.status = status; this.declaredLength = declaredLength; }
|
||||
@Override public CompletionStage<byte[]> getBody() { return completed; }
|
||||
@Override public void onSubscribe(Flow.Subscription subscription) {
|
||||
this.subscription = subscription;
|
||||
if (completed.isDone()) { subscription.cancel(); return; }
|
||||
if (declaredLength > limit) { cancel(new Failure(Reason.BODY_TOO_LARGE, status)); return; }
|
||||
subscription.request(1);
|
||||
}
|
||||
@Override public void onNext(List<ByteBuffer> buffers) {
|
||||
if (completed.isDone()) { if (subscription != null) subscription.cancel(); return; }
|
||||
long chunkBytes = 0;
|
||||
for (ByteBuffer buffer : buffers) chunkBytes += buffer.remaining();
|
||||
if (chunkBytes > limit - received) { cancel(new Failure(Reason.BODY_TOO_LARGE, status)); return; }
|
||||
received += (int) chunkBytes;
|
||||
byte[] part = new byte[8192];
|
||||
for (ByteBuffer buffer : buffers) while (buffer.hasRemaining()) {
|
||||
int count = Math.min(part.length, buffer.remaining());
|
||||
buffer.get(part, 0, count); bytes.write(part, 0, count);
|
||||
}
|
||||
subscription.request(1);
|
||||
}
|
||||
@Override public void onError(Throwable error) { completed.completeExceptionally(new Failure(Reason.IO, status)); }
|
||||
@Override public void onComplete() { completed.complete(bytes.toByteArray()); }
|
||||
void cancel(Failure failure) {
|
||||
completed.completeExceptionally(failure);
|
||||
if (subscription != null) subscription.cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
+187
@@ -0,0 +1,187 @@
|
||||
package com.moyi.controlplane.provider.transport;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Proxy;
|
||||
import java.net.Socket;
|
||||
import java.net.URI;
|
||||
import java.net.UnknownHostException;
|
||||
import java.net.http.HttpHeaders;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.RejectedExecutionException;
|
||||
import java.util.concurrent.SynchronousQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import org.apache.hc.client5.http.DnsResolver;
|
||||
import org.apache.hc.client5.http.classic.methods.HttpGet;
|
||||
import org.apache.hc.client5.http.config.ConnectionConfig;
|
||||
import org.apache.hc.client5.http.config.RequestConfig;
|
||||
import org.apache.hc.client5.http.config.TlsConfig;
|
||||
import org.apache.hc.client5.http.impl.DefaultSchemePortResolver;
|
||||
import org.apache.hc.client5.http.impl.classic.HttpClients;
|
||||
import org.apache.hc.client5.http.impl.io.BasicHttpClientConnectionManager;
|
||||
import org.apache.hc.client5.http.impl.io.DefaultHttpClientConnectionOperator;
|
||||
import org.apache.hc.client5.http.impl.io.ManagedHttpClientConnectionFactory;
|
||||
import org.apache.hc.client5.http.impl.routing.DefaultRoutePlanner;
|
||||
import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
|
||||
import org.apache.hc.client5.http.ssl.DefaultHostnameVerifier;
|
||||
import org.apache.hc.client5.http.ssl.HostnameVerificationPolicy;
|
||||
import org.apache.hc.core5.http.config.Http1Config;
|
||||
import org.apache.hc.core5.io.CloseMode;
|
||||
import org.apache.hc.core5.ssl.SSLContexts;
|
||||
import org.apache.hc.core5.util.Timeout;
|
||||
import static com.moyi.controlplane.provider.transport.BoundedHttpTransport.Reason;
|
||||
|
||||
/**
|
||||
* A credential-free HTTPS GET with per-attempt DNS binding. The caller supplies address policy;
|
||||
* this module owns only bounded transport. Never rewrite the URI to an IP: TLS/SNI and Host use
|
||||
* the original authority. No proxy, cookies, redirects, retry or connection reuse across attempts.
|
||||
*/
|
||||
public final class PinnedHttpsTransport {
|
||||
private static final ExecutorService WORKERS = new ThreadPoolExecutor(0, 8, 60, TimeUnit.SECONDS,
|
||||
new SynchronousQueue<>(), task -> {
|
||||
var thread = new Thread(task, "moyi-media-fetch");
|
||||
thread.setDaemon(true);
|
||||
return thread;
|
||||
}, new ThreadPoolExecutor.AbortPolicy());
|
||||
private final SSLContext sslContext;
|
||||
private final ExecutorService workers;
|
||||
|
||||
public PinnedHttpsTransport() { this(SSLContexts.createDefault(), WORKERS); }
|
||||
PinnedHttpsTransport(SSLContext sslContext, ExecutorService workers) {
|
||||
this.sslContext = sslContext;
|
||||
this.workers = workers;
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface AddressResolver { InetAddress[] resolve(String host) throws IOException; }
|
||||
public static final class AddressRejectedException extends IOException {
|
||||
public AddressRejectedException() { super("Target address rejected"); }
|
||||
}
|
||||
|
||||
public BoundedHttpTransport.Response get(URI uri, AddressResolver resolver, Duration deadline, int maxBytes)
|
||||
throws BoundedHttpTransport.Failure {
|
||||
if (uri == null || !"https".equalsIgnoreCase(uri.getScheme()) || uri.getHost() == null
|
||||
|| uri.getUserInfo() != null || uri.getRawFragment() != null || uri.getHost().contains("%")
|
||||
|| uri.getPort() == 0 || uri.getPort() > 65535) throw new IllegalArgumentException("Invalid HTTPS target");
|
||||
if (maxBytes < 1 || maxBytes > 32 * 1024 * 1024 || deadline == null || deadline.isNegative()
|
||||
|| deadline.isZero() || deadline.compareTo(Duration.ofMinutes(1)) > 0) throw new IllegalArgumentException("Invalid transport limits");
|
||||
var request = new HttpGet(uri);
|
||||
var observed = new AtomicInteger();
|
||||
Future<BoundedHttpTransport.Response> attempt;
|
||||
try {
|
||||
attempt = workers.submit(() -> execute(uri, resolver, request, deadline, maxBytes, observed));
|
||||
} catch (RejectedExecutionException error) {
|
||||
throw failure(Reason.BUSY, observed);
|
||||
}
|
||||
try {
|
||||
return attempt.get(Math.max(1, deadline.toMillis()), TimeUnit.MILLISECONDS);
|
||||
} catch (TimeoutException error) {
|
||||
cancel(request, attempt);
|
||||
throw failure(Reason.TIMEOUT, observed);
|
||||
} catch (InterruptedException error) {
|
||||
cancel(request, attempt);
|
||||
Thread.currentThread().interrupt();
|
||||
throw failure(Reason.INTERRUPTED, observed);
|
||||
} catch (ExecutionException error) {
|
||||
cancel(request, attempt);
|
||||
if (error.getCause() instanceof BoundedHttpTransport.Failure safe) throw safe;
|
||||
if (error.getCause() instanceof AddressRejectedException) throw failure(Reason.INVALID_ADDRESS, observed);
|
||||
if (error.getCause() instanceof java.net.SocketTimeoutException) throw failure(Reason.TIMEOUT, observed);
|
||||
// Do not attach causes: TLS/DNS/parser exceptions can contain signed URLs or response data.
|
||||
throw failure(Reason.IO, observed);
|
||||
}
|
||||
}
|
||||
|
||||
private BoundedHttpTransport.Response execute(URI uri, AddressResolver resolver, HttpGet request,
|
||||
Duration deadline, int maxBytes, AtomicInteger observed) throws IOException {
|
||||
String host = unbracket(uri.getHost());
|
||||
InetAddress[] addresses = resolver.resolve(host);
|
||||
if (addresses == null || addresses.length == 0 || addresses.length > 16
|
||||
|| java.util.Arrays.stream(addresses).anyMatch(java.util.Objects::isNull)) throw new AddressRejectedException();
|
||||
// DNS lookup may ignore interruption. Once timed out, it must never resume with a late HTTP GET.
|
||||
if (request.isCancelled() || Thread.currentThread().isInterrupted()) throw failure(Reason.INTERRUPTED, observed);
|
||||
InetAddress[] pinned = addresses.clone();
|
||||
var timeout = Timeout.ofMilliseconds(Math.max(1, deadline.toMillis()));
|
||||
var boundDns = new DnsResolver() {
|
||||
@Override public InetAddress[] resolve(String target) throws UnknownHostException {
|
||||
if (!host.equalsIgnoreCase(unbracket(target))) throw new UnknownHostException("Unexpected target");
|
||||
return pinned.clone();
|
||||
}
|
||||
@Override public String resolveCanonicalHostname(String target) throws UnknownHostException {
|
||||
if (!host.equalsIgnoreCase(unbracket(target))) throw new UnknownHostException("Unexpected target");
|
||||
return host;
|
||||
}
|
||||
};
|
||||
var tls = new DefaultClientTlsStrategy(sslContext, HostnameVerificationPolicy.BOTH, new DefaultHostnameVerifier());
|
||||
// A direct HTTP route alone is insufficient: new Socket() can still use the JVM's SOCKS selector.
|
||||
var operator = new DefaultHttpClientConnectionOperator(ignored -> new Socket(Proxy.NO_PROXY),
|
||||
DefaultSchemePortResolver.INSTANCE, boundDns, scheme -> "https".equalsIgnoreCase(scheme) ? tls : null);
|
||||
var manager = new BasicHttpClientConnectionManager(operator, ManagedHttpClientConnectionFactory.builder()
|
||||
.http1Config(Http1Config.custom().setMaxLineLength(8192).setMaxHeaderCount(64).build()).build());
|
||||
manager.setConnectionConfig(ConnectionConfig.custom().setConnectTimeout(timeout).setSocketTimeout(timeout).build());
|
||||
manager.setTlsConfig(TlsConfig.custom().setHandshakeTimeout(timeout).build());
|
||||
request.setConfig(RequestConfig.custom().setResponseTimeout(timeout).setConnectionRequestTimeout(timeout)
|
||||
.setHardCancellationEnabled(true).setAuthenticationEnabled(false).setRedirectsEnabled(false).build());
|
||||
request.setHeader("Accept", "image/*");
|
||||
request.setHeader("Accept-Encoding", "identity");
|
||||
request.setHeader("Connection", "close");
|
||||
try (var client = HttpClients.custom().setConnectionManager(manager)
|
||||
.setRoutePlanner(new DefaultRoutePlanner(DefaultSchemePortResolver.INSTANCE))
|
||||
.disableAutomaticRetries().disableRedirectHandling().disableCookieManagement()
|
||||
.disableAuthCaching().disableContentCompression().disableDefaultUserAgent().build()) {
|
||||
try {
|
||||
var response = client.executeOpen(null, request, null);
|
||||
try {
|
||||
observed.set(response.getCode());
|
||||
var entity = response.getEntity();
|
||||
int limit = response.getCode() >= 200 && response.getCode() < 300 ? maxBytes : Math.min(maxBytes, 64 * 1024);
|
||||
if (entity != null && entity.getContentLength() > limit) throw failure(Reason.BODY_TOO_LARGE, observed);
|
||||
// Compressed images are already encoded; do not accept a decompression bomb or compressed MIME wrapper.
|
||||
if (entity != null && entity.getContentEncoding() != null
|
||||
&& !"identity".equalsIgnoreCase(entity.getContentEncoding())) throw failure(Reason.IO, observed);
|
||||
var body = new ByteArrayOutputStream();
|
||||
if (entity != null) {
|
||||
var input = entity.getContent();
|
||||
byte[] chunk = new byte[8192];
|
||||
int count;
|
||||
while ((count = input.read(chunk)) != -1) {
|
||||
if (request.isCancelled() || Thread.currentThread().isInterrupted()) throw failure(Reason.INTERRUPTED, observed);
|
||||
if (count > limit - body.size()) throw failure(Reason.BODY_TOO_LARGE, observed);
|
||||
body.write(chunk, 0, count);
|
||||
}
|
||||
}
|
||||
var headers = new LinkedHashMap<String, List<String>>();
|
||||
for (var header : response.getHeaders()) headers.computeIfAbsent(header.getName(), ignored -> new ArrayList<>()).add(header.getValue());
|
||||
return new BoundedHttpTransport.Response(response.getCode(), HttpHeaders.of(headers, (key, value) -> true), body.toByteArray());
|
||||
} finally {
|
||||
// Cancel BEFORE closing the entity: close/consume must not drain an oversized/never-ending body.
|
||||
request.cancel();
|
||||
try { response.close(); }
|
||||
catch (IOException expectedAfterCancellation) { /* Never replace the primary outcome with a closed-socket cleanup error. */ }
|
||||
}
|
||||
} finally {
|
||||
request.cancel();
|
||||
client.close(CloseMode.IMMEDIATE);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static String unbracket(String host) {
|
||||
return host.startsWith("[") && host.endsWith("]") ? host.substring(1, host.length() - 1) : host;
|
||||
}
|
||||
private static void cancel(HttpGet request, Future<?> attempt) { request.cancel(); attempt.cancel(true); }
|
||||
private static BoundedHttpTransport.Failure failure(Reason reason, AtomicInteger observed) {
|
||||
return new BoundedHttpTransport.Failure(reason, observed.get() == 0 ? null : observed.get());
|
||||
}
|
||||
}
|
||||
+44
@@ -0,0 +1,44 @@
|
||||
package com.moyi.controlplane.provider.transport;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
|
||||
/** HTTP boundary facts, not a retry policy. Unknown acceptance must never authorize replay/refund. */
|
||||
public final class ProviderRequestException extends ApiException {
|
||||
public enum Acceptance { NOT_SENT, REJECTED, UNKNOWN }
|
||||
|
||||
private final Acceptance acceptance;
|
||||
private final Integer upstreamStatus;
|
||||
|
||||
private ProviderRequestException(String code, String message, int status, Acceptance acceptance, Integer upstreamStatus) {
|
||||
super(code, message, status);
|
||||
this.acceptance = acceptance;
|
||||
this.upstreamStatus = upstreamStatus;
|
||||
}
|
||||
|
||||
public static ProviderRequestException notSent(String code, String message, int status) {
|
||||
return new ProviderRequestException(code, message, status, Acceptance.NOT_SENT, null);
|
||||
}
|
||||
|
||||
public static ProviderRequestException unknown(String code, String message) {
|
||||
return new ProviderRequestException(code, message, 502, Acceptance.UNKNOWN, null);
|
||||
}
|
||||
|
||||
public static ProviderRequestException http(int status, String message) {
|
||||
String code = switch (status) {
|
||||
case 401, 403 -> "UPSTREAM_AUTH_FAILED";
|
||||
case 402 -> "UPSTREAM_CREDIT_INSUFFICIENT";
|
||||
case 429 -> "UPSTREAM_RATE_LIMITED";
|
||||
default -> "UPSTREAM_PROVIDER_ERROR";
|
||||
};
|
||||
// Timeout/conflict/5xx responses do not prove a generation was never accepted.
|
||||
Acceptance acceptance = switch (status) {
|
||||
case 400, 401, 402, 403, 404, 405, 413, 415, 422, 429 -> Acceptance.REJECTED;
|
||||
default -> Acceptance.UNKNOWN;
|
||||
};
|
||||
return new ProviderRequestException(code, message + "(HTTP " + status + ")", 502, acceptance, status);
|
||||
}
|
||||
|
||||
public Acceptance acceptance() { return acceptance; }
|
||||
public Integer upstreamStatus() { return upstreamStatus; }
|
||||
public boolean definitelyNotAccepted() { return acceptance != Acceptance.UNKNOWN; }
|
||||
}
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
package com.moyi.controlplane.provider.transport;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FilterInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.CodingErrorAction;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Map;
|
||||
import java.util.function.BooleanSupplier;
|
||||
|
||||
/** Bounded SSE/JSON framing only; no routing, retry, provider credentials, metering or billing. */
|
||||
public final class SseEventReader {
|
||||
private static final int MAX_RESPONSE_BYTES = 4 * 1024 * 1024;
|
||||
private static final int MAX_FRAME_CHARS = 262_144;
|
||||
private final ObjectMapper json;
|
||||
|
||||
public SseEventReader(ObjectMapper json) {
|
||||
this.json = json;
|
||||
}
|
||||
|
||||
/** The transport owns and closes the stream; the supplied deadline covers blocked reads. */
|
||||
public void read(InputStream body, BooleanSupplier expired, boolean requireDone, EventConsumer consumer) throws IOException {
|
||||
var decoder = StandardCharsets.UTF_8.newDecoder()
|
||||
.onMalformedInput(CodingErrorAction.REPORT).onUnmappableCharacter(CodingErrorAction.REPORT);
|
||||
var reader = new BufferedReader(new InputStreamReader(new BoundedBody(body), decoder));
|
||||
var event = new StringBuilder();
|
||||
var line = new StringBuilder();
|
||||
try {
|
||||
int c;
|
||||
while ((c = reader.read()) != -1) {
|
||||
if (expired.getAsBoolean()) throw timeout();
|
||||
if (c == '\n') {
|
||||
String text = line.toString();
|
||||
line.setLength(0);
|
||||
if (text.endsWith("\r")) text = text.substring(0, text.length() - 1);
|
||||
if (text.isEmpty()) {
|
||||
if (!event.isEmpty()) {
|
||||
String data = event.substring(0, event.length() - 1);
|
||||
event.setLength(0);
|
||||
if ("[DONE]".equals(data)) return;
|
||||
Map<String,Object> chunk = json.readValue(data, new TypeReference<>() {});
|
||||
if (chunk == null) throw new ApiException("UPSTREAM_STREAM_INVALID", "上游流式事件格式无效", 502);
|
||||
consumer.accept(chunk);
|
||||
}
|
||||
} else if (text.startsWith("data:")) {
|
||||
if ((long) event.length() + text.length() - 5 + 1 > MAX_FRAME_CHARS) throw tooLarge();
|
||||
event.append(text.substring(5).stripLeading()).append('\n');
|
||||
}
|
||||
} else {
|
||||
if (line.length() >= MAX_FRAME_CHARS) throw tooLarge();
|
||||
line.append((char) c);
|
||||
}
|
||||
}
|
||||
} catch (IOException exception) {
|
||||
if (expired.getAsBoolean()) throw timeout();
|
||||
throw exception;
|
||||
}
|
||||
if (expired.getAsBoolean()) throw timeout();
|
||||
if (!requireDone && event.isEmpty() && line.isEmpty()) return;
|
||||
throw new ApiException("UPSTREAM_STREAM_INCOMPLETE", "上游流式响应中断,本次未扣费", 502);
|
||||
}
|
||||
|
||||
private static ApiException tooLarge() {
|
||||
return new ApiException("UPSTREAM_RESPONSE_TOO_LARGE", "上游响应超过限制", 502);
|
||||
}
|
||||
|
||||
private static ApiException timeout() {
|
||||
return new ApiException("UPSTREAM_PROVIDER_TIMEOUT", "上游响应超时,本次未扣费", 504);
|
||||
}
|
||||
|
||||
private static final class BoundedBody extends FilterInputStream {
|
||||
private long bytes;
|
||||
|
||||
private BoundedBody(InputStream body) { super(body); }
|
||||
|
||||
@Override
|
||||
public int read() throws IOException {
|
||||
int value = in.read();
|
||||
if (value != -1 && ++bytes > MAX_RESPONSE_BYTES) throw tooLarge();
|
||||
return value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int read(byte[] buffer, int offset, int length) throws IOException {
|
||||
if (length == 0) return 0;
|
||||
int count = in.read(buffer, offset, (int) Math.min(length, MAX_RESPONSE_BYTES - bytes + 1));
|
||||
if (count > 0 && (bytes += count) > MAX_RESPONSE_BYTES) throw tooLarge();
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
public interface EventConsumer {
|
||||
void accept(Map<String,Object> event) throws IOException;
|
||||
}
|
||||
}
|
||||
@@ -40,6 +40,9 @@ public class SecretCipher {
|
||||
}
|
||||
}
|
||||
|
||||
/** Readiness only; never expose key material. */
|
||||
public boolean isConfigured() { return key != null; }
|
||||
|
||||
public String decrypt(String encoded) {
|
||||
if (key == null) throw new ApiException("CREDENTIAL_ENCRYPTION_NOT_CONFIGURED", "请先配置供应商凭证加密密钥", 500);
|
||||
try {
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.moyi.controlplane.task;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.gateway.TokenPriceSchedule;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.util.*;
|
||||
|
||||
/** Task prices are frozen at admission; only provider-reported usage can settle tokens. */
|
||||
public final class AsyncPriceSchedule {
|
||||
private AsyncPriceSchedule() {}
|
||||
public static boolean ready(Map<String,Object> pricing) {
|
||||
try { validate(pricing); return true; } catch (ApiException e) { return false; }
|
||||
}
|
||||
public static void validate(Map<String,Object> pricing) {
|
||||
if (pricing == null) throw unavailable("该分组缺少人民币售价");
|
||||
String unit=String.valueOf(pricing.get("unit"));
|
||||
if ("CNY / 1M tokens".equals(unit)) {
|
||||
if (!List.of("openai-image-tokens","gemini-image-tokens").contains(pricing.getOrDefault("taskBilling","")))
|
||||
throw unavailable("已配置 Token 单价,尚未配置对应的异步用量结算");
|
||||
TokenPriceSchedule.validate(pricing);
|
||||
if(pricing.get("tiers") instanceof List<?> tiers && !tiers.isEmpty()) throw unavailable("图片上下文阶梯尚未接入异步结算");
|
||||
if(pricing.containsKey("cacheWrite5m") || pricing.containsKey("cacheWrite1h")) throw unavailable("图片缓存写入计费尚未接入");
|
||||
if(pricing.containsKey("imageInput")) TokenPriceSchedule.amount(pricing.get("imageInput"));
|
||||
if(pricing.containsKey("cachedImageInput")) TokenPriceSchedule.amount(pricing.get("cachedImageInput"));
|
||||
} else {
|
||||
if (!List.of("CNY / task","CNY / image","CNY / generation").contains(unit))
|
||||
throw unavailable("该计费单位尚未接入异步结算");
|
||||
TokenPriceSchedule.amount(pricing.getOrDefault("price",pricing.get("input")));
|
||||
}
|
||||
}
|
||||
public static BigDecimal reserve(Map<String,Object> pricing, Map<String,Object> input) {
|
||||
validate(pricing);
|
||||
if(input.get("n")!=null) {
|
||||
try { if(new BigDecimal(String.valueOf(input.get("n"))).compareTo(BigDecimal.ONE)!=0) throw new NumberFormatException(); }
|
||||
catch(Exception e) { throw new ApiException("INVALID_TASK_INPUT","每个图片任务仅支持生成 1 张图片"); }
|
||||
}
|
||||
if(!tokens(pricing)) return TokenPriceSchedule.amount(pricing.getOrDefault("price",pricing.get("input")));
|
||||
if(String.valueOf(input.getOrDefault("prompt","")).getBytes(java.nio.charset.StandardCharsets.UTF_8).length>16384)
|
||||
throw new ApiException("INVALID_TASK_INPUT","图片提示词最多 16384 字节");
|
||||
// A conservative admission estimate, not a substituted usage charge.
|
||||
// Final provider usage may exceed this hold, as with synchronous metering.
|
||||
BigDecimal inputRate=TokenPriceSchedule.amount(pricing.get("input"))
|
||||
.max(TokenPriceSchedule.amount(pricing.getOrDefault("imageInput",pricing.get("input"))));
|
||||
return inputRate.multiply(BigDecimal.valueOf(65536))
|
||||
.add(TokenPriceSchedule.amount(pricing.get("output")).multiply(BigDecimal.valueOf(32768)))
|
||||
.divide(new BigDecimal("1000000"),8,RoundingMode.CEILING);
|
||||
}
|
||||
public static boolean tokens(Map<String,Object> pricing) { return pricing!=null && "CNY / 1M tokens".equals(pricing.get("unit")); }
|
||||
public record Bill(BigDecimal amount,long input,long output,long imageInput) {}
|
||||
public static Bill bill(Map<String,Object> pricing,Map<String,Object> result) {
|
||||
validate(pricing);
|
||||
if(!tokens(pricing)) return new Bill(TokenPriceSchedule.amount(pricing.getOrDefault("price",pricing.get("input"))),0,0,0);
|
||||
Map<?,?> usage=map(result==null?null:result.get("usage"));
|
||||
long input,output,image=0,cachedText=0,cachedImage=0;
|
||||
if("gemini-image-tokens".equals(pricing.get("taskBilling"))) {
|
||||
usage=map(result==null?null:result.get("usageMetadata"));
|
||||
input=count(usage.get("promptTokenCount"));
|
||||
output=count(usage.get("candidatesTokenCount"))+optional(usage,"thoughtsTokenCount");
|
||||
if(optional(usage,"cachedContentTokenCount")>0) throw missing();
|
||||
} else {
|
||||
input=count(usage.get("input_tokens")); output=count(usage.get("output_tokens"));
|
||||
var detail=map(usage.get("input_tokens_details"));
|
||||
long cached=optional(detail,"cached_tokens");
|
||||
if(optional(usage,"cached_tokens")>0) cached=count(usage.get("cached_tokens"));
|
||||
if(pricing.containsKey("imageInput")) {
|
||||
image=count(detail.get("image_tokens"));
|
||||
long text=count(detail.get("text_tokens"));
|
||||
if(image+text!=input) throw missing();
|
||||
}
|
||||
if(cached>0) {
|
||||
var cachedDetail=map(detail.get("cached_tokens_details"));
|
||||
cachedImage=count(cachedDetail.get("image_tokens")); cachedText=count(cachedDetail.get("text_tokens"));
|
||||
if(cachedText+cachedImage!=cached || cachedImage>image || cachedText>input-image
|
||||
|| !pricing.containsKey("cachedInput") || !pricing.containsKey("cachedImageInput")) throw missing();
|
||||
}
|
||||
}
|
||||
if(input+output==0 || image>input) throw missing();
|
||||
BigDecimal amount=TokenPriceSchedule.amount(pricing.get("input")).multiply(BigDecimal.valueOf(input-image-cachedText))
|
||||
.add(TokenPriceSchedule.amount(pricing.getOrDefault("imageInput",pricing.get("input"))).multiply(BigDecimal.valueOf(image-cachedImage)))
|
||||
.add(TokenPriceSchedule.amount(pricing.getOrDefault("cachedInput",0)).multiply(BigDecimal.valueOf(cachedText)))
|
||||
.add(TokenPriceSchedule.amount(pricing.getOrDefault("cachedImageInput",0)).multiply(BigDecimal.valueOf(cachedImage)))
|
||||
.add(TokenPriceSchedule.amount(pricing.get("output")).multiply(BigDecimal.valueOf(output)))
|
||||
.divide(new BigDecimal("1000000"),8,RoundingMode.HALF_UP);
|
||||
return new Bill(amount,input,output,image);
|
||||
}
|
||||
private static Map<?,?> map(Object value) { return value instanceof Map<?,?> m ? m : Map.of(); }
|
||||
private static long optional(Map<?,?> value,String field) { return value.get(field)==null?0:count(value.get(field)); }
|
||||
private static long count(Object value) {
|
||||
try { long n=new BigDecimal(String.valueOf(value)).longValueExact(); if(n<0 || n>100_000_000) throw new NumberFormatException(); return n; }
|
||||
catch(Exception e) { throw missing(); }
|
||||
}
|
||||
private static ApiException unavailable(String message) { return new ApiException("ASYNC_BILLING_UNSUPPORTED",message,503); }
|
||||
private static ApiException missing() { return new ApiException("TASK_USAGE_PENDING","生成已完成,但上游未返回完整可核对的用量;暂不扣费,等待核账",502); }
|
||||
}
|
||||
+157
-154
@@ -5,11 +5,14 @@ import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.security.SecretCipher;
|
||||
import com.moyi.controlplane.provider.credentials.UpstreamCredentialResolver;
|
||||
import com.moyi.controlplane.provider.transport.ProviderRequestException;
|
||||
import com.moyi.controlplane.provider.transport.BoundedHttpTransport;
|
||||
import com.moyi.controlplane.provider.media.MediaDownloader;
|
||||
import com.moyi.controlplane.provider.media.AsyncMediaSupport;
|
||||
import com.moyi.controlplane.provider.media.MediaDownloader.MediaBytes;
|
||||
import java.net.URI;
|
||||
import java.net.InetAddress;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.time.Duration;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
@@ -40,7 +43,8 @@ public class AsyncProviderClient {
|
||||
/** Keep rotation state across submit/poll calls; rebuilding a Provider on
|
||||
* every request otherwise selected the first upstream key forever. */
|
||||
private final ConcurrentMap<String, AtomicInteger> keyCursors = new ConcurrentHashMap<>();
|
||||
private final HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build();
|
||||
private final BoundedHttpTransport transport = new BoundedHttpTransport();
|
||||
private final MediaDownloader mediaDownloader = new MediaDownloader();
|
||||
|
||||
public AsyncProviderClient(JdbcTemplate jdbc, SecretCipher cipher, ObjectMapper mapper, Environment environment) {
|
||||
this.jdbc = jdbc; this.cipher = cipher; this.mapper = mapper; this.environment = environment;
|
||||
@@ -59,11 +63,28 @@ public class AsyncProviderClient {
|
||||
}
|
||||
|
||||
public Map<String,Object> submit(String providerId, String upstreamModel, String routeId, String taskType, Map<String,Object> input) {
|
||||
try {
|
||||
return submitConfigured(providerId, upstreamModel, routeId, taskType, input);
|
||||
} catch (ProviderRequestException exception) {
|
||||
throw exception;
|
||||
} catch (ApiException exception) {
|
||||
// Only these local validation/configuration errors are known to precede generation submission.
|
||||
if (List.of("PROVIDER_NOT_CONFIGURED", "UPSTREAM_KEY_NOT_AVAILABLE", "ASYNC_PROVIDER_NOT_IMPLEMENTED",
|
||||
"MEDIA_INPUT_UNSUPPORTED", "INVALID_MEDIA_URL", "MEDIA_FETCH_FAILED", "MEDIA_TOO_LARGE",
|
||||
"MEDIA_TYPE_UNSUPPORTED", "INVALID_TASK_INPUT").contains(exception.getCode())) {
|
||||
throw ProviderRequestException.notSent(exception.getCode(), exception.getMessage(), exception.getStatus());
|
||||
}
|
||||
throw ProviderRequestException.unknown("UPSTREAM_PROVIDER_ERROR", "上游任务提交结果尚未确认");
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String,Object> submitConfigured(String providerId, String upstreamModel, String routeId, String taskType, Map<String,Object> input) {
|
||||
if (input == null) input = Map.of();
|
||||
String prompt = String.valueOf(input.getOrDefault("prompt", ""));
|
||||
Provider provider = provider(providerId, routeId, upstreamModel);
|
||||
String code = provider.code().toLowerCase(java.util.Locale.ROOT);
|
||||
if (isTripo(provider.code(), provider.baseUrl())) {
|
||||
requireTaskType(taskType, "3d");
|
||||
Map<String,Object> body = new LinkedHashMap<>();
|
||||
body.put("model", upstreamModel);
|
||||
String media = mediaUrl(input);
|
||||
@@ -75,6 +96,7 @@ public class AsyncProviderClient {
|
||||
return withKeyFingerprint(request("POST", tripoBase(provider.baseUrl()) + (media == null ? "/generation/text-to-model" : "/generation/image-to-model"), apiKey, body, "上游 3D 任务创建失败"), apiKey);
|
||||
}
|
||||
if (code.contains("byte") || code.contains("seedance") || provider.baseUrl().contains("volces.com")) {
|
||||
requireTaskType(taskType, "video");
|
||||
List<Map<String,Object>> content = new ArrayList<>();
|
||||
if (!prompt.isBlank()) content.add(Map.of("type", "text", "text", prompt));
|
||||
String media = mediaUrl(input);
|
||||
@@ -92,6 +114,17 @@ public class AsyncProviderClient {
|
||||
throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该异步渠道没有可用的执行器", 501);
|
||||
}
|
||||
|
||||
/** Validate a proven polling binding without sending any HTTP request or exposing credential values. */
|
||||
public void requirePollableBinding(String providerId, String routeId, String upstreamModel, String taskType, String fingerprint) {
|
||||
if (fingerprint == null || !fingerprint.matches("[0-9a-f]{64}")) throw new ApiException("INVALID_CREDENTIAL_FINGERPRINT", "需要有效的上游账号指纹", 422);
|
||||
Provider provider = provider(providerId, routeId, upstreamModel);
|
||||
String code = provider.code().toLowerCase(java.util.Locale.ROOT);
|
||||
boolean pollable = isTripo(provider.code(), provider.baseUrl()) ? "3d".equals(taskType)
|
||||
: (code.contains("byte") || code.contains("seedance") || provider.baseUrl().contains("volces.com") || "openai-compatible".equalsIgnoreCase(provider.protocol())) && "video".equals(taskType);
|
||||
if (!pollable) throw new ApiException("TASK_NOT_POLLABLE", "该执行器没有可恢复的只读任务查询接口,请核实终态", 409);
|
||||
provider.keyForFingerprint(routeId, upstreamModel, environment, keyCursors, fingerprint);
|
||||
}
|
||||
|
||||
public Map<String,Object> poll(String providerId, String providerTaskId) {
|
||||
return poll(providerId, null, providerTaskId);
|
||||
}
|
||||
@@ -129,6 +162,7 @@ public class AsyncProviderClient {
|
||||
String media = mediaUrl(input);
|
||||
String base = trim(provider.baseUrl());
|
||||
if ("image".equalsIgnoreCase(taskType)) {
|
||||
if(model.startsWith("gemini-") && model.contains("image")) return submitGeminiImage(provider,routeId,model,input);
|
||||
Map<String,Object> body = new LinkedHashMap<>();
|
||||
body.put("model", model); body.put("prompt", prompt); body.put("n", 1);
|
||||
// DALL-E accepts `url` while GPT-image models require
|
||||
@@ -137,6 +171,7 @@ public class AsyncProviderClient {
|
||||
// choose its documented default and normalize either shape.
|
||||
copyParameter(input, body, "response_format");
|
||||
copyParameter(input, body, "size");
|
||||
copyParameter(input, body, "quality");
|
||||
String apiKey = provider.nextApiKey(routeId, model, environment, keyCursors);
|
||||
Map<String,Object> response = media == null
|
||||
? request("POST", base + "/images/generations", apiKey, body, "上游图片任务创建失败")
|
||||
@@ -165,15 +200,29 @@ public class AsyncProviderClient {
|
||||
}
|
||||
}
|
||||
if ("audio".equalsIgnoreCase(taskType)) {
|
||||
if (!AsyncMediaSupport.supportsSpeech(model)) throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该音频模型不是已接入的文本转语音模型;转写、音乐和实时音频需专用接口", 501);
|
||||
if (media != null) throw new ApiException("MEDIA_INPUT_UNSUPPORTED", "该语音生成接口接收文本;音频转写需使用专用转写接口", 400);
|
||||
if (prompt.isBlank() || prompt.length() > 4096) throw new ApiException("INVALID_TASK_INPUT", "语音文本必须为 1 至 4096 个字符", 400);
|
||||
// OpenAI-compatible TTS returns an audio/* binary body. Keep the
|
||||
// payload bounded before embedding it into the task result so a
|
||||
// provider cannot make the database grow without limit.
|
||||
Map<String,Object> body = new LinkedHashMap<>();
|
||||
body.put("model", model); body.put("input", prompt);
|
||||
body.put("voice", input.getOrDefault("voice", "alloy"));
|
||||
body.put("response_format", input.getOrDefault("response_format", "mp3"));
|
||||
copyParameter(input, body, "speed");
|
||||
Object voice = input.getOrDefault("voice", "alloy");
|
||||
if (!(voice instanceof String name) || name.isBlank() || name.length() > 100) throw new ApiException("INVALID_TASK_INPUT", "请提供有效的语音音色", 400);
|
||||
Object format = input.getOrDefault("response_format", "mp3");
|
||||
if (!(format instanceof String formatName) || !List.of("mp3","wav","opus","aac","flac").contains(formatName)) {
|
||||
throw new ApiException("INVALID_TASK_INPUT", "语音结果支持 mp3、wav、opus、aac、flac;原始 PCM 尚未接入播放", 400);
|
||||
}
|
||||
if (input.containsKey("stream_format") && !"audio".equals(input.get("stream_format"))) throw new ApiException("INVALID_TASK_INPUT", "语音任务暂不支持 SSE 事件格式", 400);
|
||||
body.put("voice", voice); body.put("response_format", format);
|
||||
if (input.containsKey("speed")) {
|
||||
try {
|
||||
var speed = new java.math.BigDecimal(String.valueOf(input.get("speed")));
|
||||
if (speed.compareTo(new java.math.BigDecimal("0.25")) < 0 || speed.compareTo(new java.math.BigDecimal("4")) > 0) throw new NumberFormatException();
|
||||
body.put("speed", speed);
|
||||
} catch (NumberFormatException error) { throw new ApiException("INVALID_TASK_INPUT", "语速必须为 0.25 至 4", 400); }
|
||||
}
|
||||
String apiKey = provider.nextApiKey(routeId, model, environment, keyCursors);
|
||||
return Map.of("_moyiInlineResult", requestAudio("POST", base + "/audio/speech", apiKey, body, "上游语音任务创建失败"), "_moyiUpstreamKeyFingerprint", keyFingerprint(apiKey));
|
||||
}
|
||||
@@ -185,6 +234,53 @@ public class AsyncProviderClient {
|
||||
if (value != null) body.put(name, value);
|
||||
}
|
||||
|
||||
private Map<String,Object> submitGeminiImage(Provider provider,String routeId,String model,Map<String,Object> input) {
|
||||
var parts=new ArrayList<Map<String,Object>>();
|
||||
parts.add(Map.of("text",String.valueOf(input.getOrDefault("prompt",""))));
|
||||
String media=mediaUrl(input);
|
||||
if(media!=null) {
|
||||
try {
|
||||
MediaBytes file=mediaDownloader.download(media);
|
||||
parts.add(Map.of("inlineData",Map.of("mimeType",file.contentType(),"data",Base64.getEncoder().encodeToString(file.bytes()))));
|
||||
} catch(ApiException e) { throw e; }
|
||||
catch(Exception e) { throw new ApiException("MEDIA_FETCH_FAILED","无法读取参考图",400); }
|
||||
}
|
||||
var config=new LinkedHashMap<String,Object>();
|
||||
config.put("responseModalities",List.of("TEXT","IMAGE"));
|
||||
var imageConfig=new LinkedHashMap<String,Object>();
|
||||
Object size=input.getOrDefault("imageSize",input.get("size"));
|
||||
if(size!=null) {
|
||||
if(!List.of("1K","2K","4K").contains(String.valueOf(size))) throw new ApiException("INVALID_TASK_INPUT","Gemini 图片尺寸请使用 1K、2K 或 4K",400);
|
||||
imageConfig.put("imageSize",size);
|
||||
}
|
||||
copyParameter(input,imageConfig,"aspectRatio");
|
||||
if(!imageConfig.isEmpty()) config.put("imageConfig",imageConfig);
|
||||
String base=trim(provider.baseUrl()).replaceFirst("/v1$","");
|
||||
String key=provider.nextApiKey(routeId,model,environment,keyCursors);
|
||||
var response=request("POST",base+"/v1beta/models/"+encode(model)+":generateContent",key,
|
||||
Map.of("contents",List.of(Map.of("role","user","parts",parts)),"generationConfig",config),"上游图片任务创建失败");
|
||||
var result=new LinkedHashMap<String,Object>();
|
||||
if(response.get("candidates") instanceof List<?> candidates && !candidates.isEmpty()
|
||||
&& candidates.getFirst() instanceof Map<?,?> candidate && candidate.get("content") instanceof Map<?,?> content
|
||||
&& content.get("parts") instanceof List<?> returned) {
|
||||
for(Object raw:returned) if(raw instanceof Map<?,?> part) {
|
||||
if(part.get("text") instanceof String text) result.put("text",text);
|
||||
Object inline=part.get("inlineData"); if(inline==null) inline=part.get("inline_data");
|
||||
if(inline instanceof Map<?,?> data && data.get("data") instanceof String b64 && !result.containsKey("base64")) {
|
||||
if(b64.length()>16*1024*1024) throw new ApiException("UPSTREAM_MEDIA_TOO_LARGE","返回图片超过保存限制",502);
|
||||
result.put("base64",b64); result.put("mimeType",data.get("mimeType")==null?"image/png":data.get("mimeType"));
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!result.containsKey("base64")) throw new ApiException("UPSTREAM_INVALID_MEDIA","上游未返回生成图片",502);
|
||||
copyUsage(response,result);
|
||||
return Map.of("_moyiInlineResult",result,"_moyiUpstreamKeyFingerprint",keyFingerprint(key));
|
||||
}
|
||||
|
||||
private void copyUsage(Map<String,Object> response,Map<String,Object> result) {
|
||||
for(String name:List.of("usage","usageMetadata")) if(response.get(name) instanceof Map<?,?> value) result.put(name,value);
|
||||
}
|
||||
|
||||
private String mediaUrl(Map<String,Object> input) {
|
||||
Object raw = input.get("media_url");
|
||||
if (raw == null || String.valueOf(raw).isBlank()) return null;
|
||||
@@ -211,55 +307,41 @@ public class AsyncProviderClient {
|
||||
}
|
||||
|
||||
private boolean isNotFound(ApiException exception) {
|
||||
String message = exception.getMessage();
|
||||
return message != null && (message.contains("HTTP 404") || message.contains("HTTP 405"));
|
||||
return exception instanceof ProviderRequestException failure
|
||||
&& (Integer.valueOf(404).equals(failure.upstreamStatus()) || Integer.valueOf(405).equals(failure.upstreamStatus()));
|
||||
}
|
||||
|
||||
/** Normalize OpenAI image/audio response fields to the task result shape. */
|
||||
private Map<String,Object> compatibleResult(Map<String,Object> response) {
|
||||
Object data = response.get("data");
|
||||
if (data instanceof List<?> list && !list.isEmpty()) {
|
||||
Object first = list.getFirst();
|
||||
if (first instanceof Map<?,?> map) {
|
||||
Map<String,Object> result = new LinkedHashMap<>();
|
||||
map.forEach((key, value) -> result.put(String.valueOf(key), value));
|
||||
Object url = result.get("url");
|
||||
if (url == null) url = result.get("audio_url");
|
||||
if (url != null) result.put("url", url);
|
||||
if (result.get("b64_json") != null) result.put("base64", result.get("b64_json"));
|
||||
return result;
|
||||
}
|
||||
}
|
||||
Object url = response.get("url");
|
||||
if (url != null) return Map.of("url", url);
|
||||
return null;
|
||||
return AsyncTaskResponseNormalizer.result(response, "image");
|
||||
}
|
||||
|
||||
private Map<String,Object> requestAudio(String method, String url, String key, Map<String,Object> body, String message) {
|
||||
boolean sending = false;
|
||||
try {
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder().uri(URI.create(url)).timeout(UPSTREAM_REQUEST_TIMEOUT)
|
||||
.header("Authorization", "Bearer " + key).header("Content-Type", "application/json");
|
||||
HttpResponse<byte[]> response = client.send(builder.POST(HttpRequest.BodyPublishers.ofString(write(body))).build(), HttpResponse.BodyHandlers.ofByteArray());
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||
String code = switch (response.statusCode()) {
|
||||
case 401, 403 -> "UPSTREAM_AUTH_FAILED";
|
||||
case 402 -> "UPSTREAM_CREDIT_INSUFFICIENT";
|
||||
case 429 -> "UPSTREAM_RATE_LIMITED";
|
||||
default -> "UPSTREAM_PROVIDER_ERROR";
|
||||
};
|
||||
throw new ApiException(code, message + "(HTTP " + response.statusCode() + ")", 502);
|
||||
}
|
||||
HttpRequest request = builder.POST(HttpRequest.BodyPublishers.ofString(write(body))).build();
|
||||
sending = true;
|
||||
var response = transport.send(request, 8 * 1024 * 1024);
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) throw upstreamStatus(response.statusCode(), message);
|
||||
byte[] bytes = response.body();
|
||||
if (bytes.length == 0 || bytes.length > 8 * 1024 * 1024) throw new ApiException("UPSTREAM_MEDIA_TOO_LARGE", message + "(返回音频超过 8 MiB 或为空)", 502);
|
||||
String contentType = response.headers().firstValue("Content-Type").orElse("audio/mpeg").split(";", 2)[0].trim().toLowerCase(java.util.Locale.ROOT);
|
||||
String contentType = response.headers().firstValue("Content-Type").orElse("application/octet-stream").split(";", 2)[0].trim().toLowerCase(java.util.Locale.ROOT);
|
||||
if (contentType.equals("application/octet-stream")) contentType = switch (String.valueOf(body.get("response_format"))) {
|
||||
case "wav" -> "audio/wav";
|
||||
case "opus" -> "audio/ogg";
|
||||
case "aac" -> "audio/aac";
|
||||
case "flac" -> "audio/flac";
|
||||
default -> "audio/mpeg";
|
||||
};
|
||||
if (!contentType.startsWith("audio/")) throw new ApiException("UPSTREAM_INVALID_MEDIA", message + "(返回内容不是音频)", 502);
|
||||
Map<String,Object> result = new LinkedHashMap<>();
|
||||
result.put("base64", Base64.getEncoder().encodeToString(bytes));
|
||||
result.put("mimeType", contentType);
|
||||
result.put("format", String.valueOf(body.getOrDefault("response_format", "mp3")));
|
||||
return result;
|
||||
} catch (ApiException exception) { throw exception; }
|
||||
catch (Exception exception) { throw new ApiException("UPSTREAM_PROVIDER_ERROR", message, 502); }
|
||||
} catch (Exception exception) { throw requestFailure(exception, sending, message); }
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -271,6 +353,7 @@ public class AsyncProviderClient {
|
||||
*/
|
||||
private Map<String,Object> requestMultipart(String method, String url, String key, Map<String,Object> fields,
|
||||
String fileField, String media, String message) {
|
||||
boolean sending = false;
|
||||
try {
|
||||
String boundary = "----moyi-" + java.util.UUID.randomUUID();
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
@@ -278,7 +361,7 @@ public class AsyncProviderClient {
|
||||
multipartField(out, boundary, entry.getKey(), String.valueOf(entry.getValue()));
|
||||
}
|
||||
if (fileField != null && media != null) {
|
||||
MediaBytes file = downloadMedia(media);
|
||||
MediaBytes file = mediaDownloader.download(media);
|
||||
multipartFile(out, boundary, fileField, file.filename(), file.contentType(), file.bytes());
|
||||
}
|
||||
out.write(("--" + boundary + "--\r\n").getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
@@ -286,7 +369,8 @@ public class AsyncProviderClient {
|
||||
.header("Authorization", "Bearer " + key)
|
||||
.header("Content-Type", "multipart/form-data; boundary=" + boundary)
|
||||
.POST(HttpRequest.BodyPublishers.ofByteArray(out.toByteArray())).build();
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
sending = true;
|
||||
var response = transport.send(request, 8 * 1024 * 1024);
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) throw upstreamStatus(response.statusCode(), message);
|
||||
Map<String,Object> parsed = mapper.readValue(response.body(), new TypeReference<>() {});
|
||||
Object code = parsed.get("code");
|
||||
@@ -294,18 +378,27 @@ public class AsyncProviderClient {
|
||||
throw new ApiException("UPSTREAM_PROVIDER_ERROR", message, 502);
|
||||
}
|
||||
return parsed;
|
||||
} catch (ApiException exception) { throw exception; }
|
||||
catch (Exception exception) { throw new ApiException("UPSTREAM_PROVIDER_ERROR", message, 502); }
|
||||
} catch (Exception exception) { throw requestFailure(exception, sending, message); }
|
||||
}
|
||||
|
||||
private ApiException upstreamStatus(int status, String message) {
|
||||
String code = switch (status) {
|
||||
case 401, 403 -> "UPSTREAM_AUTH_FAILED";
|
||||
case 402 -> "UPSTREAM_CREDIT_INSUFFICIENT";
|
||||
case 429 -> "UPSTREAM_RATE_LIMITED";
|
||||
default -> "UPSTREAM_PROVIDER_ERROR";
|
||||
};
|
||||
return new ApiException(code, message + "(HTTP " + status + ")", 502);
|
||||
return ProviderRequestException.http(status, message);
|
||||
}
|
||||
|
||||
private void requireTaskType(String actual, String expected) {
|
||||
if (!expected.equalsIgnoreCase(actual)) throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "渠道专用执行接口与任务类型不匹配", 501);
|
||||
}
|
||||
|
||||
private ProviderRequestException requestFailure(Exception exception, boolean sending, String message) {
|
||||
if (exception instanceof InterruptedException) Thread.currentThread().interrupt();
|
||||
if (exception instanceof ProviderRequestException failure) return failure;
|
||||
if (exception instanceof BoundedHttpTransport.Failure failure && failure.observedStatus() != null
|
||||
&& (failure.observedStatus() < 200 || failure.observedStatus() >= 300)) {
|
||||
return ProviderRequestException.http(failure.observedStatus(), message);
|
||||
}
|
||||
if (sending) return ProviderRequestException.unknown("UPSTREAM_PROVIDER_ERROR", message);
|
||||
if (exception instanceof ApiException error) return ProviderRequestException.notSent(error.getCode(), error.getMessage(), error.getStatus());
|
||||
return ProviderRequestException.notSent("UPSTREAM_REQUEST_NOT_SENT", "请求准备失败,尚未提交上游生成", 502);
|
||||
}
|
||||
|
||||
private void multipartField(ByteArrayOutputStream out, String boundary, String name, String value) throws IOException {
|
||||
@@ -321,63 +414,17 @@ public class AsyncProviderClient {
|
||||
out.write("\r\n".getBytes(java.nio.charset.StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
private MediaBytes downloadMedia(String value) throws IOException, InterruptedException {
|
||||
URI uri = URI.create(value);
|
||||
if (!"https".equalsIgnoreCase(uri.getScheme()) || uri.getHost() == null || uri.getUserInfo() != null) {
|
||||
throw new ApiException("INVALID_MEDIA_URL", "媒体地址必须是可公开访问的 HTTPS URL", 400);
|
||||
}
|
||||
for (InetAddress address : InetAddress.getAllByName(uri.getHost())) {
|
||||
if (address.isAnyLocalAddress() || address.isLoopbackAddress() || address.isLinkLocalAddress()
|
||||
|| address.isSiteLocalAddress() || address.isMulticastAddress()) {
|
||||
throw new ApiException("INVALID_MEDIA_URL", "媒体地址不能指向内网或本机地址", 400);
|
||||
}
|
||||
}
|
||||
HttpRequest request = HttpRequest.newBuilder(uri).timeout(Duration.ofSeconds(15)).GET().build();
|
||||
HttpResponse<byte[]> response = client.send(request, HttpResponse.BodyHandlers.ofByteArray());
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||
throw new ApiException("MEDIA_FETCH_FAILED", "无法读取媒体输入(HTTP " + response.statusCode() + ")", 400);
|
||||
}
|
||||
byte[] bytes = response.body();
|
||||
if (bytes.length == 0 || bytes.length > 8 * 1024 * 1024) {
|
||||
throw new ApiException("MEDIA_TOO_LARGE", "媒体输入为空或超过 8 MiB", 400);
|
||||
}
|
||||
String contentType = response.headers().firstValue("Content-Type").orElse("application/octet-stream")
|
||||
.split(";", 2)[0].trim().toLowerCase(java.util.Locale.ROOT);
|
||||
if (!contentType.startsWith("image/")) {
|
||||
throw new ApiException("MEDIA_TYPE_UNSUPPORTED", "图像编辑和视频参考图必须是 image/* 文件", 400);
|
||||
}
|
||||
String filename = "input" + extension(contentType);
|
||||
return new MediaBytes(bytes, contentType, filename);
|
||||
}
|
||||
|
||||
private String extension(String contentType) {
|
||||
return switch (contentType) {
|
||||
case "image/jpeg" -> ".jpg";
|
||||
case "image/png" -> ".png";
|
||||
case "image/webp" -> ".webp";
|
||||
case "image/gif" -> ".gif";
|
||||
default -> ".bin";
|
||||
};
|
||||
}
|
||||
|
||||
private record MediaBytes(byte[] bytes, String contentType, String filename) {}
|
||||
|
||||
private Map<String,Object> request(String method, String url, String key, Map<String,Object> body, String message) {
|
||||
boolean sending = false;
|
||||
try {
|
||||
HttpRequest.Builder builder = HttpRequest.newBuilder().uri(URI.create(url)).timeout(UPSTREAM_REQUEST_TIMEOUT)
|
||||
.header("Authorization", "Bearer " + key).header("Content-Type", "application/json");
|
||||
if ("POST".equals(method)) builder.POST(HttpRequest.BodyPublishers.ofString(write(body)));
|
||||
else builder.GET();
|
||||
HttpResponse<String> response = client.send(builder.build(), HttpResponse.BodyHandlers.ofString());
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) {
|
||||
String code = switch (response.statusCode()) {
|
||||
case 401, 403 -> "UPSTREAM_AUTH_FAILED";
|
||||
case 402 -> "UPSTREAM_CREDIT_INSUFFICIENT";
|
||||
case 429 -> "UPSTREAM_RATE_LIMITED";
|
||||
default -> "UPSTREAM_PROVIDER_ERROR";
|
||||
};
|
||||
throw new ApiException(code, message + "(HTTP " + response.statusCode() + ")", 502);
|
||||
}
|
||||
HttpRequest request = builder.build();
|
||||
sending = true;
|
||||
var response = transport.send(request, 8 * 1024 * 1024);
|
||||
if (response.statusCode() < 200 || response.statusCode() >= 300) throw upstreamStatus(response.statusCode(), message);
|
||||
Map<String,Object> parsed = mapper.readValue(response.body(), new TypeReference<>() {});
|
||||
Object code = parsed.get("code");
|
||||
if (parsed.containsKey("error") || code instanceof Number number && number.intValue() != 0) {
|
||||
@@ -386,8 +433,7 @@ public class AsyncProviderClient {
|
||||
throw new ApiException("UPSTREAM_PROVIDER_ERROR", message, 502);
|
||||
}
|
||||
return parsed;
|
||||
} catch (ApiException exception) { throw exception; }
|
||||
catch (Exception exception) { throw new ApiException("UPSTREAM_PROVIDER_ERROR", message, 502); }
|
||||
} catch (Exception exception) { throw requestFailure(exception, sending, message); }
|
||||
}
|
||||
|
||||
private Provider provider(String id) {
|
||||
@@ -400,17 +446,15 @@ public class AsyncProviderClient {
|
||||
String ciphertext = row.get("credential_ciphertext") == null ? "" : String.valueOf(row.get("credential_ciphertext"));
|
||||
String key = ciphertext.isBlank() ? "" : cipher.decrypt(ciphertext);
|
||||
String code = String.valueOf(row.get("code"));
|
||||
List<String> keys = new ArrayList<>();
|
||||
if (!key.isBlank()) keys.add(key);
|
||||
addEnvironmentKeys(keys, code);
|
||||
addEnvironmentKeys(keys, id);
|
||||
var resolver = new UpstreamCredentialResolver(environment);
|
||||
List<String> keys = new ArrayList<>(resolver.providerKeys(key, code, id));
|
||||
// Tripo's official API key may be supplied through the server secret
|
||||
// environment so it never has to be committed or exposed in SQL.
|
||||
if (key.isBlank() && isTripo(code, String.valueOf(row.get("base_url")))) {
|
||||
addEnvironmentKeys(keys, "TRIPO_API_KEY");
|
||||
addEnvironmentKeys(keys, "MOYI_TRIPO_API_KEY");
|
||||
keys.addAll(resolver.environmentKeys("TRIPO_API_KEY"));
|
||||
keys.addAll(resolver.environmentKeys("MOYI_TRIPO_API_KEY"));
|
||||
}
|
||||
if (keys.isEmpty() && routeKeys(code, id, routeId, upstreamModel).isEmpty()) throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道尚未配置 API Key", 503);
|
||||
if (keys.isEmpty() && resolver.scopedKeys(code, routeId, upstreamModel).isEmpty()) throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道尚未配置 API Key", 503);
|
||||
return new Provider(code, String.valueOf(row.get("protocol")), String.valueOf(row.get("base_url")), keys.stream().distinct().toList(), keyCursors.computeIfAbsent(id, ignored -> new AtomicInteger()));
|
||||
} catch (EmptyResultDataAccessException exception) { throw new ApiException("PROVIDER_NOT_CONFIGURED", "异步渠道不存在或已停用", 503); }
|
||||
}
|
||||
@@ -427,32 +471,6 @@ public class AsyncProviderClient {
|
||||
private String trim(String value) { return value == null ? "" : value.replaceAll("/+$", ""); }
|
||||
private String encode(String value) { return java.net.URLEncoder.encode(value, java.nio.charset.StandardCharsets.UTF_8); }
|
||||
private String write(Object value) throws JsonProcessingException { return mapper.writeValueAsString(value == null ? Map.of() : value); }
|
||||
private void addEnvironmentKeys(List<String> keys, String alias) {
|
||||
String name = alias.endsWith("_API_KEY") ? alias : "MOYI_UPSTREAM_API_KEYS_" + alias.toUpperCase(java.util.Locale.ROOT).replace('-', '_');
|
||||
String configured = environment.getProperty(name);
|
||||
if (configured != null && !configured.isBlank()) for (String value : configured.split("\\|")) if (!value.isBlank()) keys.add(value.trim());
|
||||
}
|
||||
|
||||
private List<String> routeKeys(String providerCode, String providerId, String routeId, String upstreamModel) {
|
||||
if (routeId != null && !routeId.isBlank()) {
|
||||
String configured = environment.getProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_" + normalize(routeId));
|
||||
if (configured != null && !configured.isBlank()) return parseKeys(configured);
|
||||
}
|
||||
if (upstreamModel != null && !upstreamModel.isBlank()) {
|
||||
String scopedName = "MOYI_UPSTREAM_API_KEYS_MODEL_" + normalize(providerCode) + "_" + normalize(upstreamModel);
|
||||
String configured = environment.getProperty(scopedName);
|
||||
if (configured != null && !configured.isBlank()) return parseKeys(configured);
|
||||
}
|
||||
return List.of();
|
||||
}
|
||||
|
||||
private List<String> parseKeys(String configured) {
|
||||
List<String> keys = new ArrayList<>();
|
||||
for (String key : configured.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
||||
return keys.stream().distinct().toList();
|
||||
}
|
||||
|
||||
private String normalize(String value) { return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_"); }
|
||||
private Map<String,Object> withKeyFingerprint(Map<String,Object> response, String apiKey) {
|
||||
Map<String,Object> copy = new LinkedHashMap<>();
|
||||
if (response != null) copy.putAll(response);
|
||||
@@ -478,15 +496,15 @@ public class AsyncProviderClient {
|
||||
return normalizedCode.contains("tripo") || normalizedUrl.contains("tripo3d");
|
||||
}
|
||||
private record Provider(String code, String protocol, String baseUrl, List<String> apiKeys, AtomicInteger cursor) {
|
||||
@Override public String toString() { return "Provider[code=" + code + ", credentialCount=" + apiKeys.size() + "]"; }
|
||||
private String nextApiKey() {
|
||||
if (apiKeys.isEmpty()) throw new ApiException("UPSTREAM_KEY_NOT_AVAILABLE", "异步渠道没有可用的上游凭据", 503);
|
||||
return apiKeys.get(Math.floorMod(cursor.getAndIncrement(), apiKeys.size()));
|
||||
return UpstreamCredentialResolver.nextKey(apiKeys, cursor);
|
||||
}
|
||||
private String nextApiKey(String routeId, String upstreamModel, Environment environment, ConcurrentMap<String, AtomicInteger> cursors) {
|
||||
List<String> routeKeys = routeKeys(routeId, upstreamModel, environment);
|
||||
if (routeKeys.isEmpty()) return nextApiKey();
|
||||
AtomicInteger routeCursor = cursors.computeIfAbsent("route:" + (routeId == null ? upstreamModel : routeId), ignored -> new AtomicInteger());
|
||||
return routeKeys.get(Math.floorMod(routeCursor.getAndIncrement(), routeKeys.size()));
|
||||
return UpstreamCredentialResolver.nextKey(routeKeys, routeCursor);
|
||||
}
|
||||
private String keyForFingerprint(String routeId, String upstreamModel, Environment environment,
|
||||
ConcurrentMap<String, AtomicInteger> cursors, String fingerprint) {
|
||||
@@ -498,22 +516,7 @@ public class AsyncProviderClient {
|
||||
throw new ApiException("UPSTREAM_KEY_NOT_AVAILABLE", "创建任务时使用的上游凭据已从服务器配置中移除,无法安全轮询", 503);
|
||||
}
|
||||
private List<String> routeKeys(String routeId, String upstreamModel, Environment environment) {
|
||||
if (environment == null) return List.of();
|
||||
if (routeId != null && !routeId.isBlank()) {
|
||||
String value = environment.getProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_" + normalize(routeId));
|
||||
if (value != null && !value.isBlank()) return parseKeys(value);
|
||||
}
|
||||
if (upstreamModel != null && !upstreamModel.isBlank()) {
|
||||
String value = environment.getProperty("MOYI_UPSTREAM_API_KEYS_MODEL_" + normalize(code()) + "_" + normalize(upstreamModel));
|
||||
if (value != null && !value.isBlank()) return parseKeys(value);
|
||||
}
|
||||
return List.of();
|
||||
return new UpstreamCredentialResolver(environment).scopedKeys(code, routeId, upstreamModel);
|
||||
}
|
||||
private static List<String> parseKeys(String value) {
|
||||
List<String> keys = new ArrayList<>();
|
||||
for (String key : value.split("\\|")) if (!key.isBlank()) keys.add(key.trim());
|
||||
return keys.stream().distinct().toList();
|
||||
}
|
||||
private static String normalize(String value) { return String.valueOf(value == null ? "" : value).toUpperCase(java.util.Locale.ROOT).replaceAll("[^A-Z0-9]", "_"); }
|
||||
}
|
||||
}
|
||||
+81
@@ -0,0 +1,81 @@
|
||||
package com.moyi.controlplane.task;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import com.moyi.controlplane.provider.media.MediaAssetNormalizer;
|
||||
|
||||
/** Bounded envelope normalization only. No upstream calls, credentials, routing or settlement. */
|
||||
final class AsyncTaskResponseNormalizer {
|
||||
private AsyncTaskResponseNormalizer() {}
|
||||
|
||||
static String status(Map<String,Object> response) {
|
||||
Object raw = response.get("status");
|
||||
if (raw == null) raw = nested(response).get("status");
|
||||
return switch (String.valueOf(raw == null ? "running" : raw).trim().toLowerCase(Locale.ROOT)) {
|
||||
case "success", "succeeded", "completed" -> "succeeded";
|
||||
case "failed", "failure", "error", "banned" -> "failed";
|
||||
case "cancelled", "canceled" -> "cancelled";
|
||||
default -> "running";
|
||||
};
|
||||
}
|
||||
|
||||
static int progress(Map<String,Object> response, String status) {
|
||||
if ("succeeded".equals(status)) return 100;
|
||||
Object raw = response.get("progress");
|
||||
if (raw == null) raw = nested(response).get("progress");
|
||||
try {
|
||||
String value = String.valueOf(raw).trim();
|
||||
if (value.length() > 32) return 0;
|
||||
if (value.endsWith("%")) value = value.substring(0, value.length() - 1);
|
||||
return new BigDecimal(value).max(BigDecimal.ZERO).min(BigDecimal.valueOf(100)).intValue();
|
||||
} catch (NumberFormatException error) { return 0; }
|
||||
}
|
||||
|
||||
static Map<String,Object> result(Map<String,Object> response) {
|
||||
return result(response, null);
|
||||
}
|
||||
|
||||
static Map<String,Object> result(Map<String,Object> response, String taskType) {
|
||||
return findResult(response, taskType, 0);
|
||||
}
|
||||
|
||||
static Map<String,Object> completedResult(Map<String,Object> response, String taskType) {
|
||||
Map<String,Object> result = result(response, taskType);
|
||||
if (result != null) return result;
|
||||
result = new LinkedHashMap<>(Map.of("deliveryStatus", "pending", "deliveryCode", "TASK_RESULT_UNAVAILABLE"));
|
||||
copyMetadata(nested(response), result);
|
||||
copyMetadata(response, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Map<String,Object> findResult(Object raw, String taskType, int depth) {
|
||||
if (depth > 4) return null;
|
||||
if (raw instanceof List<?> items) {
|
||||
for (int i = 0; i < Math.min(items.size(), 16); i++) {
|
||||
var result = findResult(items.get(i), taskType, depth + 1);
|
||||
if (result != null) return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
if (!(raw instanceof Map<?,?> source)) return MediaAssetNormalizer.normalize(raw, taskType);
|
||||
for (String field : List.of("output", "content", "result", "data")) {
|
||||
var result = findResult(source.get(field), taskType, depth + 1);
|
||||
if (result != null) { copyMetadata(source, result); return result; }
|
||||
}
|
||||
return MediaAssetNormalizer.normalize(source, taskType);
|
||||
}
|
||||
|
||||
private static Map<?,?> nested(Map<String,Object> response) {
|
||||
return response.get("data") instanceof Map<?,?> data ? data : Map.of();
|
||||
}
|
||||
|
||||
private static void copyMetadata(Map<?,?> source, Map<String,Object> result) {
|
||||
for (String field : List.of("usage", "usageMetadata")) if (source.get(field) instanceof Map<?,?> value) result.put(field, value);
|
||||
Object credits = source.get("creditsConsumed");
|
||||
if (credits == null) credits = source.get("credits_consumed");
|
||||
if (credits instanceof Number || credits instanceof String) result.putIfAbsent("creditsConsumed", credits);
|
||||
}
|
||||
}
|
||||
+159
-169
@@ -7,14 +7,18 @@ import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.gateway.GatewayGuard;
|
||||
import com.moyi.controlplane.gateway.GatewayModelIds;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestContext;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestMetadata;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestProtocol;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestRiskEngine;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestRiskPolicy;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import com.moyi.controlplane.provider.transport.ProviderRequestException;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -30,6 +34,7 @@ import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class AsyncTaskService {
|
||||
private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(AsyncTaskService.class);
|
||||
private final JdbcTemplate jdbc;
|
||||
private final ObjectMapper objectMapper;
|
||||
private final CatalogService catalogService;
|
||||
@@ -43,11 +48,15 @@ public class AsyncTaskService {
|
||||
private final AsyncProviderClient providerClient;
|
||||
private final TransactionTemplate transactions;
|
||||
private final com.moyi.controlplane.gateway.EnterprisePolicies policies;
|
||||
private final ApiRequestRiskPolicy requestRisk;
|
||||
private final TaskOutcomeJournal outcomes;
|
||||
private final TaskSettlementService settlements;
|
||||
|
||||
/** Compatibility constructor for isolated gateway tests that only exercise mock tasks. */
|
||||
public AsyncTaskService(JdbcTemplate jdbc, ObjectMapper objectMapper, CatalogService catalogService,
|
||||
int maxQueuedPerModel, GatewayGuard gatewayGuard, UsageAuditService usage) {
|
||||
this(jdbc, objectMapper, catalogService, maxQueuedPerModel, 4, 4, 3_600_000, true, gatewayGuard, usage, null, null);
|
||||
this(jdbc, objectMapper, catalogService, maxQueuedPerModel, 4, 4, 3_600_000, true,
|
||||
gatewayGuard, usage, null, null, new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()));
|
||||
}
|
||||
|
||||
/** Compatibility constructor for isolated tests using explicit limits. */
|
||||
@@ -58,7 +67,8 @@ public class AsyncTaskService {
|
||||
AsyncProviderClient providerClient, PlatformTransactionManager transactionManager
|
||||
) {
|
||||
this(jdbc, objectMapper, catalogService, maxQueuedPerModel, defaultModelConcurrency,
|
||||
defaultKeyConcurrency, providerTimeoutMs, true, gatewayGuard, usage, providerClient, transactionManager);
|
||||
defaultKeyConcurrency, providerTimeoutMs, true, gatewayGuard, usage, providerClient,
|
||||
transactionManager, new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()));
|
||||
}
|
||||
|
||||
@org.springframework.beans.factory.annotation.Autowired
|
||||
@@ -74,7 +84,8 @@ public class AsyncTaskService {
|
||||
GatewayGuard gatewayGuard,
|
||||
UsageAuditService usage,
|
||||
AsyncProviderClient providerClient,
|
||||
PlatformTransactionManager transactionManager
|
||||
PlatformTransactionManager transactionManager,
|
||||
ApiRequestRiskPolicy requestRisk
|
||||
) {
|
||||
// A model concurrency of 0 means no MOYI-side model-group cap. The
|
||||
// upstream provider still enforces its own quota, while API-Key
|
||||
@@ -94,36 +105,89 @@ public class AsyncTaskService {
|
||||
this.providerClient = providerClient;
|
||||
this.transactions = transactionManager == null ? null : new TransactionTemplate(transactionManager);
|
||||
this.policies = new com.moyi.controlplane.gateway.EnterprisePolicies(jdbc,objectMapper);
|
||||
this.requestRisk = java.util.Objects.requireNonNull(requestRisk, "requestRisk");
|
||||
this.outcomes = new TaskOutcomeJournal(jdbc, objectMapper, transactions, usage);
|
||||
this.settlements = new TaskSettlementService(jdbc, objectMapper, gatewayGuard, usage, transactionManager);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String, Object> create(String authorization, String taskType, Map<String, Object> input) {
|
||||
return createForKey(requireKey(authorization), taskType, input);
|
||||
return create(authorization, taskType, input, ApiRequestMetadata.internal());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String, Object> create(String authorization, String taskType, Map<String, Object> input, ApiRequestMetadata metadata) {
|
||||
return create(authorization, taskType, input, null, metadata);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String, Object> create(String authorization, String taskType, Map<String, Object> input,
|
||||
String idempotencyKey, ApiRequestMetadata metadata) {
|
||||
return createForKey(requireKey(authorization), taskType, input, idempotencyKey, metadata);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String, Object> createForKey(Map<String, Object> key, String taskType, Map<String, Object> input) {
|
||||
gatewayGuard.checkRate(UUID.fromString(String.valueOf(key.get("id"))), false);
|
||||
return createForKey(key, taskType, input, ApiRequestMetadata.internal());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String, Object> createForKey(Map<String, Object> key, String taskType, Map<String, Object> input, ApiRequestMetadata metadata) {
|
||||
return createForKey(key, taskType, input, null, metadata);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Map<String, Object> createForKey(Map<String, Object> key, String taskType, Map<String, Object> input,
|
||||
String idempotencyKey, ApiRequestMetadata metadata) {
|
||||
// Explicit transaction also protects callers that do not go through the Spring proxy.
|
||||
return inTransaction(() -> createInTransaction(key, taskType, input, idempotencyKey, metadata));
|
||||
}
|
||||
|
||||
private Map<String,Object> createInTransaction(Map<String,Object> key, String taskType, Map<String,Object> input,
|
||||
String idempotencyKey, ApiRequestMetadata metadata) {
|
||||
UUID apiKeyId = UUID.fromString(String.valueOf(key.get("id")));
|
||||
gatewayGuard.checkRate(apiKeyId, false);
|
||||
if (input == null) throw new ApiException("INVALID_REQUEST", "请提供任务请求");
|
||||
TaskCreationIdentity identity = TaskCreationIdentity.of(objectMapper, idempotencyKey, taskType, input);
|
||||
UUID userId = catalogService.keyOwnerId(apiKeyId);
|
||||
if (identity.keyHash() != null) {
|
||||
// Serialize the same user/key before checking capacity, budget or wallet balances.
|
||||
jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended(?,0))",
|
||||
"moyi:task-idempotency:" + userId + ":" + identity.keyHash());
|
||||
var existing = jdbc.queryForList("SELECT id,request_hash FROM model_tasks WHERE user_id=? AND idempotency_hash=?", userId, identity.keyHash());
|
||||
if (!existing.isEmpty()) {
|
||||
if (!identity.requestHash().equals(existing.getFirst().get("request_hash"))) {
|
||||
throw new ApiException("IDEMPOTENCY_CONFLICT", "此 Idempotency-Key 已用于不同的任务请求", 409);
|
||||
}
|
||||
// Authenticated same-user reads remain valid even if the original route was retired.
|
||||
return taskView(String.valueOf(existing.getFirst().get("id")));
|
||||
}
|
||||
}
|
||||
String requestedModelId = String.valueOf(input.getOrDefault("model", ""));
|
||||
Map<String, Object> model = GatewayModelIds.resolve(catalogService, key, requestedModelId);
|
||||
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
|
||||
String modelId = String.valueOf(model.get("id"));
|
||||
if (!taskType.equals(model.get("type")) || !"async".equals(model.get("mode"))) throw new ApiException("NOT_ASYNC_MEDIA_MODEL", "该模型不是对应的异步媒体模型", 400);
|
||||
if (Boolean.FALSE.equals(model.get("executionReady"))) throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "该模型的专用执行接口尚未接入", 503);
|
||||
if (!"provider-mock".equals(model.get("providerId")) && providerClient == null) throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "异步执行器未配置", 501);
|
||||
// Serialize the queue-capacity check with task insertion for this
|
||||
// model. Without the transaction advisory lock concurrent creates
|
||||
// could all observe the same count and exceed maxQueuedPerModel.
|
||||
jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended('moyi:async-queue:' || ?, 0))", modelId);
|
||||
Integer queued = jdbc.queryForObject("SELECT COUNT(*) FROM model_tasks WHERE model_id = ? AND status IN ('queued', 'running')", Integer.class, modelId);
|
||||
if (queued != null && queued >= maxQueuedPerModel) throw new ApiException("TASK_QUEUE_FULL", "该模型当前排队已满,请稍后重试", 429);
|
||||
String prompt = String.valueOf(input.getOrDefault("prompt", "")).trim();
|
||||
Object mediaInput = input.get("media_url");
|
||||
if (prompt.isBlank() && (mediaInput == null || String.valueOf(mediaInput).isBlank())) {
|
||||
throw new ApiException("PROMPT_REQUIRED", "生成提示词或媒体输入不能为空");
|
||||
}
|
||||
UUID apiKeyId = UUID.fromString(String.valueOf(key.get("id")));
|
||||
UUID userId = catalogService.keyOwnerId(apiKeyId);
|
||||
BigDecimal reservedAmount = taskCharge(model);
|
||||
requestRisk.enforce(new ApiRequestContext(userId, apiKeyId, ApiRequestProtocol.ASYNC_TASK,
|
||||
requestedModelId, modelId, false, estimateInputUnits(input), 0, metadata, System.nanoTime()));
|
||||
String upstreamModel = model.get("upstreamModel") instanceof String value ? value : null;
|
||||
if (upstreamModel == null || upstreamModel.isBlank()) throw new ApiException("ASYNC_BINDING_MISSING", "模型缺少可冻结的上游执行标识", 503);
|
||||
// Serialize the queue-capacity check with task insertion for this
|
||||
// model. Without the transaction advisory lock, concurrent creates
|
||||
// could all observe the same count and exceed maxQueuedPerModel.
|
||||
jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended('moyi:async-queue:' || ?, 0))", modelId);
|
||||
Integer queued = jdbc.queryForObject("SELECT COUNT(*) FROM model_tasks WHERE model_id = ? AND status IN ('queued', 'running', 'reconciling')", Integer.class, modelId);
|
||||
if (queued != null && queued >= maxQueuedPerModel) throw new ApiException("TASK_QUEUE_FULL", "该模型当前排队已满,请稍后重试", 429);
|
||||
@SuppressWarnings("unchecked") Map<String,Object> frozenPricing=(Map<String,Object>) model.get("pricing");
|
||||
if(AsyncPriceSchedule.tokens(frozenPricing) && !"image".equals(taskType)) throw new ApiException("ASYNC_BILLING_UNSUPPORTED","该媒体类型的 Token 用量结算尚未接入",503);
|
||||
BigDecimal reservedAmount = "provider-mock".equals(model.get("providerId")) ? BigDecimal.ZERO : AsyncPriceSchedule.reserve(frozenPricing,input);
|
||||
reserveWallet(userId, reservedAmount);
|
||||
// Real async providers are submitted by the worker below. Creating a
|
||||
// task must only reserve the wallet and enqueue the work; submitting
|
||||
@@ -136,14 +200,15 @@ public class AsyncTaskService {
|
||||
OffsetDateTime now = now();
|
||||
try {
|
||||
jdbc.update(
|
||||
"INSERT INTO model_tasks (id, user_id, api_key_id, model_id, task_type, provider_task_id, status, progress, prompt, input, reserved_amount, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, 'queued', 0, ?, CAST(? AS jsonb), ?, ?, ?)",
|
||||
id, userId, apiKeyId, modelId, taskType, providerTaskId, prompt, writeJson(input == null ? Map.of() : input), reservedAmount, now, now
|
||||
"INSERT INTO model_tasks (id, user_id, api_key_id, model_id, task_type, provider_task_id, status, progress, prompt, input, reserved_amount, created_at, updated_at, idempotency_hash, request_hash, provider_id_snapshot, upstream_model_snapshot) VALUES (?, ?, ?, ?, ?, ?, 'queued', 0, ?, CAST(? AS jsonb), ?, ?, ?, ?, ?, ?, ?)",
|
||||
id, userId, apiKeyId, modelId, taskType, providerTaskId, prompt, writeJson(input), reservedAmount, now, now, identity.keyHash(), identity.requestHash(), model.get("providerId"), upstreamModel
|
||||
);
|
||||
} catch (RuntimeException exception) {
|
||||
releaseWallet(userId, reservedAmount);
|
||||
throw exception;
|
||||
}
|
||||
usage.recordTask(userId, apiKeyId, modelId, String.valueOf(model.get("providerId")), id, "queued", input, null, null, null);
|
||||
usage.recordTask(userId, apiKeyId, modelId, String.valueOf(model.get("providerId")), id, "queued", input, null, null, null, metadata);
|
||||
if (!"provider-mock".equals(model.get("providerId"))) jdbc.update("UPDATE model_tasks SET pricing_snapshot=CAST(? AS jsonb),price_version=(SELECT max(id) FROM model_price_history WHERE model_id=?) WHERE id=?",writeJson(frozenPricing),modelId,id);
|
||||
catalogService.touchKey(key);
|
||||
return taskView(id);
|
||||
}
|
||||
@@ -178,6 +243,11 @@ public class AsyncTaskService {
|
||||
|
||||
@Scheduled(fixedDelayString = "${moyi.tasks.worker-interval-ms:1000}")
|
||||
public void completeMockTasks() {
|
||||
// Retry local settlement from durable observations, never the upstream generation request.
|
||||
for (var completion : outcomes.pendingCompletions()) {
|
||||
try { completeObserved(completion); }
|
||||
catch (RuntimeException exception) { log.warn("Async task {} settlement remains pending", completion.taskId()); }
|
||||
}
|
||||
List<String> taskIds = jdbc.query("SELECT id FROM model_tasks WHERE status IN ('queued', 'running') AND provider_task_id = 'mock' AND created_at < now() - INTERVAL '500 milliseconds'", (rs, row) -> rs.getString("id"));
|
||||
jdbc.update(
|
||||
"UPDATE model_tasks SET status = 'succeeded', progress = 100, result = CAST(CASE WHEN task_type = '3d' THEN ? ELSE ? END AS jsonb), updated_at = ?, completed_at = ? WHERE status IN ('queued', 'running') AND provider_task_id = 'mock' AND created_at < now() - INTERVAL '500 milliseconds'",
|
||||
@@ -185,7 +255,7 @@ public class AsyncTaskService {
|
||||
"{\"format\":\"mp4\",\"url\":\"https://mock.moyi.ai/tasks/result.mp4\"}", now(), now()
|
||||
);
|
||||
taskIds.forEach(taskId -> usage.updateTask(taskId, "succeeded", Map.of("format", "result", "url", "https://mock.moyi.ai/tasks/" + taskId + "/result"), null, null));
|
||||
List<Map<String,Object>> pending = jdbc.queryForList("SELECT t.id, t.model_id, t.provider_task_id, t.provider_key_fingerprint, t.provider_deadline_at, t.created_at, m.provider_id, m.upstream_model FROM model_tasks t JOIN model_catalog m ON m.id=t.model_id WHERE t.status IN ('queued','running') AND t.provider_task_id <> 'mock' AND (t.updated_at < now() - INTERVAL '5 seconds' OR t.provider_deadline_at <= now()) ORDER BY t.updated_at LIMIT 50");
|
||||
List<Map<String,Object>> pending = jdbc.queryForList("SELECT t.id, t.model_id, t.provider_task_id, t.provider_key_fingerprint, t.provider_deadline_at, t.created_at, t.provider_id_snapshot AS provider_id, t.upstream_model_snapshot AS upstream_model FROM model_tasks t WHERE t.status IN ('queued','running') AND t.completion_status IS NULL AND t.provider_task_id <> 'mock' AND (t.updated_at < now() - INTERVAL '5 seconds' OR t.provider_deadline_at <= now()) ORDER BY t.updated_at LIMIT 50");
|
||||
for (Map<String,Object> row : pending) pollProviderTask(row);
|
||||
dispatchQueuedTasks();
|
||||
}
|
||||
@@ -198,36 +268,44 @@ public class AsyncTaskService {
|
||||
*/
|
||||
private void dispatchQueuedTasks() {
|
||||
if (providerClient == null) return;
|
||||
// A process can die after claiming a task but before receiving an
|
||||
// upstream task id. Fail those claims after a grace period. Retrying
|
||||
// them blindly could submit the same generation twice when the
|
||||
// upstream accepted the request just before the process died.
|
||||
// A lost submit response is ambiguous, not a failed generation.
|
||||
// Quarantine stale claims without retrying or releasing financial holds.
|
||||
recoverInterruptedClaims();
|
||||
for (int i = 0; i < 50; i++) {
|
||||
Map<String,Object> task = claimQueuedTask();
|
||||
if (task == null) return;
|
||||
String taskId = String.valueOf(task.get("id"));
|
||||
usage.updateTask(taskId, "running", null, null, null);
|
||||
Map<String,Object> upstream;
|
||||
try {
|
||||
Map<String,Object> input = taskInput(task);
|
||||
Map<String,Object> upstream = submitProviderTask(task, input);
|
||||
// Some OpenAI-compatible image/audio endpoints return the
|
||||
// generated media in the submit response rather than creating
|
||||
// a pollable job. Persist that result and settle the task in
|
||||
// the same worker transaction.
|
||||
upstream = submitProviderTask(task, input);
|
||||
} catch (RuntimeException exception) {
|
||||
if (exception instanceof ProviderRequestException failure && failure.definitelyNotAccepted()) {
|
||||
completeObserved(outcomes.observeTerminal(taskId, "failed", null, failure.getCode(), failure.getMessage()));
|
||||
} else {
|
||||
outcomes.requireReconciliation(taskId, "UPSTREAM_TASK_ACCEPTANCE_UNKNOWN", "上游任务是否已受理尚未确认;冻结额度保留,请联系管理员核账,勿重复提交", false);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
TaskOutcomeJournal.Completion completion = null;
|
||||
try {
|
||||
// Journal a received result before touching the ledger. A failed settlement
|
||||
// must only retry locally, never generate again or release its reservation.
|
||||
if (upstream.get("_moyiInlineResult") instanceof Map<?,?> inline) {
|
||||
Map<String,Object> result = new LinkedHashMap<>();
|
||||
inline.forEach((key, value) -> result.put(String.valueOf(key), value));
|
||||
finishTask(taskId, "succeeded", 100, result, null, null);
|
||||
continue;
|
||||
completion = outcomes.observeTerminal(taskId, "succeeded",
|
||||
AsyncTaskResponseNormalizer.completedResult(result, String.valueOf(task.get("task_type"))), null, null);
|
||||
} else {
|
||||
OffsetDateTime submittedAt = now();
|
||||
outcomes.accepted(taskId, providerTaskId(upstream), upstream.get("_moyiUpstreamKeyFingerprint"),
|
||||
submittedAt, submittedAt.plus(java.time.Duration.ofMillis(providerTimeoutMs)));
|
||||
}
|
||||
OffsetDateTime submittedAt = now();
|
||||
jdbc.update("UPDATE model_tasks SET provider_task_id=?, provider_key_fingerprint=?, provider_deadline_at=?, updated_at=? WHERE id=? AND status='running'", providerTaskId(upstream), upstream.get("_moyiUpstreamKeyFingerprint"), submittedAt.plus(java.time.Duration.ofMillis(providerTimeoutMs)), submittedAt, taskId);
|
||||
} catch (RuntimeException exception) {
|
||||
String code = exception instanceof ApiException error ? error.getCode() : "UPSTREAM_TASK_SUBMIT_FAILED";
|
||||
String message = exception instanceof ApiException error ? error.getMessage() : "上游异步任务创建失败";
|
||||
failTask(taskId, code, message);
|
||||
outcomes.requireReconciliation(taskId, "UPSTREAM_TASK_RECEIPT_UNKNOWN", "已调用上游,但任务回执无法确认或保存;冻结额度保留,等待核账,勿重复提交", false);
|
||||
}
|
||||
// Deliberately outside the provider exception handler.
|
||||
completeObserved(completion);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,26 +317,26 @@ public class AsyncTaskService {
|
||||
// workers reliably.
|
||||
jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended('moyi:async-dispatch', 0))");
|
||||
String sql = """
|
||||
SELECT t.id, t.user_id, t.api_key_id, t.model_id, m.provider_id, m.upstream_model, t.task_type, t.prompt, t.input
|
||||
SELECT t.id, t.user_id, t.api_key_id, t.model_id, t.provider_id_snapshot AS provider_id, t.upstream_model_snapshot AS upstream_model, t.task_type, t.prompt, t.input
|
||||
FROM model_tasks t
|
||||
JOIN model_catalog m ON m.id=t.model_id
|
||||
JOIN model_providers p ON p.id=m.provider_id
|
||||
JOIN model_providers p ON p.id=t.provider_id_snapshot
|
||||
WHERE t.status='queued'
|
||||
AND t.provider_task_id IS NULL
|
||||
AND m.status='active' AND m.deleted_at IS NULL
|
||||
AND p.status='active' AND p.deleted_at IS NULL
|
||||
AND (SELECT count(*) FROM model_tasks ru WHERE ru.user_id=t.user_id AND ru.status='running') < coalesce((SELECT max_concurrency FROM gateway_user_policies up WHERE up.user_id=t.user_id),16)
|
||||
AND (SELECT count(*) FROM model_tasks ru WHERE ru.user_id=t.user_id AND ru.status IN ('running','reconciling')) < coalesce((SELECT max_concurrency FROM gateway_user_policies up WHERE up.user_id=t.user_id),16)
|
||||
AND (
|
||||
? <= 0
|
||||
OR COALESCE(m.max_concurrency, ?) <= 0
|
||||
OR (SELECT COUNT(*) FROM model_tasks running_model
|
||||
WHERE running_model.model_id=t.model_id AND running_model.status='running')
|
||||
WHERE running_model.model_id=t.model_id AND running_model.status IN ('running','reconciling'))
|
||||
< COALESCE(m.max_concurrency, ?)
|
||||
)
|
||||
AND (
|
||||
? <= 0
|
||||
OR (SELECT COUNT(*) FROM model_tasks running_key
|
||||
WHERE running_key.api_key_id=t.api_key_id AND running_key.status='running')
|
||||
WHERE running_key.api_key_id=t.api_key_id AND running_key.status IN ('running','reconciling'))
|
||||
< ?
|
||||
)
|
||||
ORDER BY t.created_at
|
||||
@@ -273,38 +351,32 @@ public class AsyncTaskService {
|
||||
if(!gatewayGuard.tryTaskUserSlot((UUID)row.get("api_key_id"),row.get("id").toString(),providerTimeoutMs+180000)) return null;
|
||||
try {jdbc.update("UPDATE model_tasks SET status='running', updated_at=now() WHERE id=? AND status='queued'", row.get("id"));}
|
||||
catch(RuntimeException e) {gatewayGuard.releaseTaskUserSlot((UUID)row.get("user_id"),row.get("id").toString());throw e;}
|
||||
usage.updateTask(row.get("id").toString(), "running", null, null, null);
|
||||
return row;
|
||||
});
|
||||
}
|
||||
|
||||
private void recoverInterruptedClaims() {
|
||||
List<String> taskIds = inTransaction(() -> jdbc.queryForList(
|
||||
"SELECT id FROM model_tasks WHERE status='running' AND provider_task_id IS NULL AND updated_at < now() - INTERVAL '2 minutes' FOR UPDATE SKIP LOCKED",
|
||||
"SELECT id FROM model_tasks WHERE status='running' AND provider_task_id IS NULL AND completion_status IS NULL AND updated_at < now() - INTERVAL '2 minutes' FOR UPDATE SKIP LOCKED",
|
||||
String.class));
|
||||
for (String taskId : taskIds) {
|
||||
failTask(taskId, "UPSTREAM_TASK_SUBMIT_INTERRUPTED", "上游任务提交被中断,请确认上游记录后重新提交");
|
||||
outcomes.requireReconciliation(taskId, "UPSTREAM_TASK_SUBMIT_INTERRUPTED", "上游提交回执丢失或进程中断;冻结额度保留,等待核账,勿重复提交", true);
|
||||
}
|
||||
}
|
||||
|
||||
private void completeObserved(TaskOutcomeJournal.Completion completion) {
|
||||
if (completion == null) return;
|
||||
finishTask(completion.taskId(), completion.status(), "succeeded".equals(completion.status()) ? 100 : 0,
|
||||
completion.result(), completion.code(), completion.message());
|
||||
}
|
||||
|
||||
private void failTask(String taskId, String code, String message) {
|
||||
finishTask(taskId, "failed", 0, null, code, message);
|
||||
}
|
||||
|
||||
private void finishTask(String taskId, String status, int progress, Map<String,Object> result, String code, String message) {
|
||||
// The terminal state and wallet change must commit together. Otherwise a
|
||||
// crash after marking failed/succeeded leaves a hold that polling never
|
||||
// revisits. The conditional update also prevents duplicate settlement.
|
||||
inTransaction(() -> {
|
||||
int changed = jdbc.update(
|
||||
"UPDATE model_tasks SET status=?, progress=?, result=CAST(? AS jsonb), error=CAST(? AS jsonb), updated_at=?, completed_at=? WHERE id=? AND status IN ('queued','running')",
|
||||
status, progress, result == null ? null : writeJson(result),
|
||||
code == null ? null : writeJson(Map.of("code", code, "message", message)), now(), now(), taskId);
|
||||
if (changed == 0) return null;
|
||||
if ("succeeded".equals(status)) settleTaskInTransaction(taskId);
|
||||
else releaseTaskInTransaction(taskId);
|
||||
usage.updateTask(taskId, status, result, code, message);
|
||||
return null;
|
||||
});
|
||||
settlements.finish(taskId, status, progress, result, code, message);
|
||||
}
|
||||
|
||||
private Map<String, Object> requireKey(String authorization) {
|
||||
@@ -321,10 +393,13 @@ public class AsyncTaskService {
|
||||
task.put("id", resultSet.getString("id"));
|
||||
task.put("provider", "upstream");
|
||||
task.put("model", resultSet.getString("model_id"));
|
||||
task.put("taskType", resultSet.getString("task_type"));
|
||||
task.put("status", resultSet.getString("status"));
|
||||
task.put("progress", resultSet.getInt("progress"));
|
||||
task.put("createdAt", resultSet.getObject("created_at", OffsetDateTime.class));
|
||||
task.put("updatedAt", resultSet.getObject("updated_at", OffsetDateTime.class));
|
||||
putIfPresent(task, "completedAt", resultSet, "completed_at");
|
||||
task.put("requiresReconciliation", "reconciling".equals(resultSet.getString("status")));
|
||||
putIfPresent(task, "costAmount", resultSet, "cost_amount");
|
||||
String result = resultSet.getString("result");
|
||||
if (result != null) task.put("result", readMap(result));
|
||||
@@ -368,13 +443,20 @@ public class AsyncTaskService {
|
||||
|
||||
private String providerTaskId(Map<String,Object> response) {
|
||||
Object direct = response.get("id");
|
||||
if (direct == null && response.get("data") instanceof Map<?,?> data) direct = data.get("task_id");
|
||||
if (direct == null) throw new ApiException("UPSTREAM_PROVIDER_ERROR", "上游没有返回异步任务编号", 502);
|
||||
return String.valueOf(direct);
|
||||
if (direct == null) direct = response.get("task_id");
|
||||
if (direct == null && response.get("data") instanceof Map<?,?> data) direct = data.get("task_id") == null ? data.get("id") : data.get("task_id");
|
||||
if (!(direct instanceof String value) || value.isBlank() || value.length() > 160 || value.chars().anyMatch(Character::isISOControl)) {
|
||||
throw new ApiException("UPSTREAM_PROVIDER_ERROR", "上游没有返回有效的异步任务编号", 502);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private void pollProviderTask(Map<String,Object> row) {
|
||||
String taskId = String.valueOf(row.get("id"));
|
||||
if (row.get("provider_id") == null || row.get("upstream_model") == null) {
|
||||
outcomes.requireReconciliation(taskId, "ASYNC_BINDING_MISSING", "历史任务缺少可证明的原始执行路由;冻结额度保留,请根据上游记录核账", false);
|
||||
return;
|
||||
}
|
||||
OffsetDateTime deadline = taskTimestamp(row.get("provider_deadline_at"));
|
||||
if (deadline == null) {
|
||||
// Defensive fallback for pre-migration tasks. The migration also
|
||||
@@ -384,7 +466,7 @@ public class AsyncTaskService {
|
||||
deadline = createdAt.plus(java.time.Duration.ofMillis(providerTimeoutMs));
|
||||
}
|
||||
if (!now().isBefore(deadline)) {
|
||||
failTask(taskId, "UPSTREAM_TASK_TIMEOUT", "异步任务查询已超时,执行结果未知;平台冻结额度已释放。上游可能仍在执行,请先核实后再提交");
|
||||
outcomes.requireReconciliation(taskId, "UPSTREAM_TASK_TIMEOUT", "异步任务查询已超时,执行结果未知;冻结额度保留,等待管理员核账,勿重复提交", false);
|
||||
return;
|
||||
}
|
||||
Map<String,Object> response;
|
||||
@@ -392,7 +474,7 @@ public class AsyncTaskService {
|
||||
response = providerClient.poll(String.valueOf(row.get("provider_id")), String.valueOf(row.get("model_id")), String.valueOf(row.get("upstream_model")), String.valueOf(row.get("provider_task_id")), row.get("provider_key_fingerprint") == null ? null : String.valueOf(row.get("provider_key_fingerprint")));
|
||||
} catch (RuntimeException exception) {
|
||||
if (exception instanceof ApiException error && "UPSTREAM_KEY_NOT_AVAILABLE".equals(error.getCode())) {
|
||||
failTask(taskId, error.getCode(), error.getMessage());
|
||||
outcomes.requireReconciliation(taskId, error.getCode(), "提交任务使用的上游凭据已不可用,执行结果未知;冻结额度保留,等待核账", false);
|
||||
return;
|
||||
}
|
||||
// Even a permanent authentication/configuration error only proves
|
||||
@@ -404,15 +486,20 @@ public class AsyncTaskService {
|
||||
updatePollingState(taskId, null, "UPSTREAM_TASK_POLL_RETRY", message);
|
||||
return;
|
||||
}
|
||||
String status = normalizedStatus(response);
|
||||
int progress = progress(response, status);
|
||||
Map<String,Object> result = result(response);
|
||||
String status = AsyncTaskResponseNormalizer.status(response);
|
||||
int progress = AsyncTaskResponseNormalizer.progress(response, status);
|
||||
String taskType = jdbc.queryForObject("SELECT task_type FROM model_tasks WHERE id=?", String.class, taskId);
|
||||
// File delivery is independent of upstream generation/settlement. Missing or invalid files
|
||||
// never cause a generation retry or an automatic refund, including image and 3D tasks.
|
||||
Map<String,Object> result = "succeeded".equals(status)
|
||||
? AsyncTaskResponseNormalizer.completedResult(response, taskType)
|
||||
: AsyncTaskResponseNormalizer.result(response, taskType);
|
||||
if ("succeeded".equals(status) || "failed".equals(status) || "cancelled".equals(status)) {
|
||||
// Persistence failures must roll back and remain pollable, rather
|
||||
// than being mistaken for upstream failures and refunded.
|
||||
finishTask(taskId, status, progress, result,
|
||||
completeObserved(outcomes.observeTerminal(taskId, status, result,
|
||||
"failed".equals(status) ? "UPSTREAM_TASK_FAILED" : null,
|
||||
"failed".equals(status) ? "上游异步任务失败" : null);
|
||||
"failed".equals(status) ? "上游异步任务失败" : null));
|
||||
} else {
|
||||
updatePollingState(taskId, progress, null, null);
|
||||
}
|
||||
@@ -420,7 +507,7 @@ public class AsyncTaskService {
|
||||
|
||||
private void updatePollingState(String taskId, Integer progress, String code, String message) {
|
||||
inTransaction(() -> {
|
||||
int changed = jdbc.update("UPDATE model_tasks SET status='running', progress=COALESCE(?,progress), error=CAST(? AS jsonb), updated_at=? WHERE id=? AND status IN ('queued','running')",
|
||||
int changed = jdbc.update("UPDATE model_tasks SET status='running', progress=COALESCE(?,progress), error=CAST(? AS jsonb), updated_at=? WHERE id=? AND status IN ('queued','running') AND completion_status IS NULL",
|
||||
progress, code == null ? null : writeJson(Map.of("code", code, "message", message, "retryable", true)), now(), taskId);
|
||||
if (changed > 0) usage.updateTask(taskId, "running", null, code, message);
|
||||
return null;
|
||||
@@ -434,78 +521,11 @@ public class AsyncTaskService {
|
||||
throw new IllegalStateException("Persisted task timestamp is missing");
|
||||
}
|
||||
|
||||
private String normalizedStatus(Map<String,Object> response) {
|
||||
Object raw = response.get("status");
|
||||
if (raw == null && response.get("data") instanceof Map<?,?> data) raw = data.get("status");
|
||||
String value = String.valueOf(raw == null ? "running" : raw).toLowerCase(java.util.Locale.ROOT);
|
||||
if (value.contains("success") || value.equals("completed")) return "succeeded";
|
||||
if (value.contains("fail") || value.equals("error") || value.equals("banned")) return "failed";
|
||||
if (value.contains("cancel")) return "cancelled";
|
||||
return "running";
|
||||
}
|
||||
|
||||
private int progress(Map<String,Object> response, String status) {
|
||||
Object raw = response.get("progress");
|
||||
if (raw == null && response.get("data") instanceof Map<?,?> data) raw = data.get("progress");
|
||||
if (raw instanceof Number number) return Math.max(0, Math.min(100, number.intValue()));
|
||||
return "succeeded".equals(status) ? 100 : 0;
|
||||
}
|
||||
|
||||
private Map<String,Object> result(Map<String,Object> response) {
|
||||
Object raw = response.get("output");
|
||||
Map<?,?> data = response.get("data") instanceof Map<?,?> value ? value : null;
|
||||
if (raw == null && data != null) raw = data.get("output");
|
||||
if (raw instanceof Map<?,?> map) {
|
||||
Map<String,Object> value = new LinkedHashMap<>();
|
||||
map.forEach((k,v)->value.put(String.valueOf(k),v));
|
||||
copyAlias(value, "model_url", "modelUrl");
|
||||
copyAlias(value, "rendered_image_url", "previewUrl");
|
||||
copyAlias(value, "generated_image_url", "imageUrl");
|
||||
copyAlias(value, "video_url", "videoUrl");
|
||||
copyAlias(value, "audio_url", "audioUrl");
|
||||
copyAlias(value, "image_url", "imageUrl");
|
||||
if (value.get("url") == null && value.get("modelUrl") != null) value.put("url", value.get("modelUrl"));
|
||||
if (value.get("url") == null && value.get("imageUrl") != null) value.put("url", value.get("imageUrl"));
|
||||
if (value.get("url") == null && value.get("videoUrl") != null) value.put("url", value.get("videoUrl"));
|
||||
if (value.get("url") == null && value.get("audioUrl") != null) value.put("url", value.get("audioUrl"));
|
||||
if (data != null && data.get("credits_consumed") != null) value.put("creditsConsumed", data.get("credits_consumed"));
|
||||
return value;
|
||||
}
|
||||
if (raw instanceof String value && !value.isBlank()) return Map.of("url", value);
|
||||
// OpenAI-compatible video jobs return the media URL at the top level
|
||||
// (or in a nested video object) rather than under `output`.
|
||||
Object directUrl = response.get("url");
|
||||
if (directUrl == null) directUrl = response.get("video_url");
|
||||
if (directUrl == null) directUrl = response.get("image_url");
|
||||
if (directUrl == null) directUrl = response.get("audio_url");
|
||||
if (directUrl == null && response.get("video") instanceof Map<?,?> video) directUrl = video.get("url");
|
||||
if (directUrl == null && response.get("image") instanceof Map<?,?> image) directUrl = image.get("url");
|
||||
if (directUrl == null && response.get("audio") instanceof Map<?,?> audio) directUrl = audio.get("url");
|
||||
if (directUrl != null && !String.valueOf(directUrl).isBlank()) return Map.of("url", directUrl);
|
||||
return null;
|
||||
}
|
||||
|
||||
private void copyAlias(Map<String,Object> value, String source, String target) {
|
||||
if (value.get(target) == null && value.get(source) != null) value.put(target, value.get(source));
|
||||
}
|
||||
|
||||
private String writeJson(Object value) { try { return objectMapper.writeValueAsString(value); } catch (JsonProcessingException exception) { return "{}"; } }
|
||||
|
||||
private BigDecimal taskCharge(Map<String,Object> model) {
|
||||
if ("provider-mock".equals(model.get("providerId"))) return BigDecimal.ZERO;
|
||||
if (!(model.get("pricing") instanceof Map<?,?> pricing)) throw new ApiException("MODEL_NOT_PRICED", "该异步模型尚未配置按任务计费价格", 503);
|
||||
Object unitValue = pricing.get("unit");
|
||||
String unit = String.valueOf(unitValue == null ? "" : unitValue).toLowerCase(java.util.Locale.ROOT);
|
||||
if (unit.contains("token")) throw new ApiException("ASYNC_BILLING_UNSUPPORTED", "异步任务不能使用 Token 价格,需配置按次、按生成或按媒体单位的固定售价", 503);
|
||||
Object value = pricing.get("price");
|
||||
if (value == null) value = pricing.get("input");
|
||||
try {
|
||||
BigDecimal amount = new BigDecimal(String.valueOf(value));
|
||||
if (amount.signum() < 0 || amount.scale() > 8 || amount.compareTo(new BigDecimal("1000000")) > 0) throw new NumberFormatException();
|
||||
return amount.setScale(8, RoundingMode.CEILING);
|
||||
} catch (Exception exception) {
|
||||
throw new ApiException("MODEL_NOT_PRICED", "该异步模型需配置有效的固定任务售价", 503);
|
||||
}
|
||||
private long estimateInputUnits(Map<String,Object> input) {
|
||||
try { return objectMapper.writeValueAsBytes(input == null ? Map.of() : input).length; }
|
||||
catch (JsonProcessingException exception) { throw new ApiException("INVALID_REQUEST", "任务请求无法编码"); }
|
||||
}
|
||||
|
||||
private void reserveWallet(UUID userId, BigDecimal amount) {
|
||||
@@ -520,40 +540,10 @@ public class AsyncTaskService {
|
||||
if (amount.signum() > 0) jdbc.update("UPDATE user_wallets SET reserved_balance=GREATEST(0,reserved_balance-?),updated_at=now() WHERE user_id=?", amount, userId);
|
||||
}
|
||||
|
||||
private void settleTaskInTransaction(String taskId) {
|
||||
Map<String,Object> task = jdbc.queryForMap("SELECT user_id,reserved_amount,cost_amount FROM model_tasks WHERE id=? FOR UPDATE", taskId);
|
||||
BigDecimal amount = (BigDecimal) task.get("reserved_amount");
|
||||
policies.release(taskId);
|
||||
gatewayGuard.releaseTaskUserSlot((UUID)task.get("user_id"),taskId);
|
||||
if (amount == null || amount.signum() == 0 || task.get("cost_amount") != null) return;
|
||||
UUID userId = (UUID) task.get("user_id");
|
||||
Map<String,Object> wallet = jdbc.queryForMap("SELECT balance,reserved_balance FROM user_wallets WHERE user_id=? FOR UPDATE", userId);
|
||||
BigDecimal before = (BigDecimal) wallet.get("balance");
|
||||
BigDecimal after = before.subtract(amount);
|
||||
jdbc.update("UPDATE user_wallets SET balance=?,reserved_balance=GREATEST(0,reserved_balance-?),updated_at=now() WHERE user_id=?", after, amount, userId);
|
||||
jdbc.update("INSERT INTO wallet_entries(id,user_id,request_id,amount,balance_after,currency,reason,operation,requested_amount,balance_before,actor_user_id) VALUES (?,?,?,?,?,'CNY',?,'usage',?,?,NULL)", UUID.randomUUID(), userId, taskRequestId(taskId), amount.negate(), after, "异步模型任务 · " + taskId, amount, before);
|
||||
jdbc.update("UPDATE model_tasks SET cost_amount=? WHERE id=?", amount, taskId);
|
||||
jdbc.update("UPDATE usage_events SET cost_amount=? WHERE provider_task_id=? AND event_kind='task'", amount, taskId);
|
||||
}
|
||||
|
||||
private void releaseTaskInTransaction(String taskId) {
|
||||
Map<String,Object> task = jdbc.queryForMap("SELECT user_id,reserved_amount,cost_amount FROM model_tasks WHERE id=? FOR UPDATE", taskId);
|
||||
BigDecimal amount = (BigDecimal) task.get("reserved_amount");
|
||||
policies.release(taskId);
|
||||
gatewayGuard.releaseTaskUserSlot((UUID)task.get("user_id"),taskId);
|
||||
if (amount != null && amount.signum() > 0 && task.get("cost_amount") == null) {
|
||||
releaseWallet((UUID) task.get("user_id"), amount);
|
||||
jdbc.update("UPDATE model_tasks SET cost_amount=0 WHERE id=?", taskId);
|
||||
jdbc.update("UPDATE usage_events SET cost_amount=0 WHERE provider_task_id=? AND event_kind='task'", taskId);
|
||||
}
|
||||
}
|
||||
|
||||
private <T> T inTransaction(Supplier<T> work) {
|
||||
return transactions == null ? work.get() : transactions.execute(status -> work.get());
|
||||
}
|
||||
|
||||
private UUID taskRequestId(String taskId) { return UUID.nameUUIDFromBytes(("moyi-async:" + taskId).getBytes(StandardCharsets.UTF_8)); }
|
||||
|
||||
private void putIfPresent(Map<String,Object> target, String name, ResultSet resultSet, String column) throws SQLException {
|
||||
Object value = resultSet.getObject(column);
|
||||
if (value != null) target.put(name, value);
|
||||
|
||||
@@ -11,6 +11,8 @@ import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestHeader;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestMetadata;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/v1")
|
||||
@@ -25,19 +27,25 @@ public class TaskController {
|
||||
public ResponseEntity<Map<String, Object>> create3d(
|
||||
@org.springframework.web.bind.annotation.PathVariable(required = false) String type,
|
||||
@RequestHeader(value = "Authorization", required = false) String authorization,
|
||||
@RequestBody Map<String, Object> input
|
||||
@RequestHeader(value = "Idempotency-Key", required = false) String idempotencyKey,
|
||||
@RequestBody Map<String, Object> input,
|
||||
HttpServletRequest request
|
||||
) {
|
||||
String taskType = type == null ? String.valueOf(input == null ? "3d" : input.getOrDefault("taskType", "3d")) : type;
|
||||
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, taskType, input)));
|
||||
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, taskType, input, idempotencyKey,
|
||||
ApiRequestMetadata.fromHttp(request.getRemoteAddr()))));
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/video/tasks")
|
||||
public ResponseEntity<Map<String, Object>> createVideo(
|
||||
@RequestHeader(value = "Authorization", required = false) String authorization,
|
||||
@RequestBody Map<String, Object> input
|
||||
@RequestHeader(value = "Idempotency-Key", required = false) String idempotencyKey,
|
||||
@RequestBody Map<String, Object> input,
|
||||
HttpServletRequest request
|
||||
) {
|
||||
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, "video", input)));
|
||||
return ResponseEntity.status(HttpStatus.ACCEPTED).body(Map.of("task", tasks.create(authorization, "video", input, idempotencyKey,
|
||||
ApiRequestMetadata.fromHttp(request.getRemoteAddr()))));
|
||||
}
|
||||
|
||||
@GetMapping("/tasks/{id}")
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.moyi.controlplane.task;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.DecimalNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.HexFormat;
|
||||
import java.util.Map;
|
||||
import java.util.TreeSet;
|
||||
|
||||
/** Stable request identity only; never stores or exposes the caller's raw idempotency key. */
|
||||
record TaskCreationIdentity(String keyHash, String requestHash) {
|
||||
static TaskCreationIdentity of(ObjectMapper mapper, String key, String type, Map<String,Object> input) {
|
||||
if (key == null) return new TaskCreationIdentity(null, null);
|
||||
if (!key.matches("[!-~]{1,128}")) {
|
||||
throw new ApiException("INVALID_IDEMPOTENCY_KEY", "Idempotency-Key 须为 1–128 个可见 ASCII 字符");
|
||||
}
|
||||
try {
|
||||
ObjectNode request = mapper.createObjectNode();
|
||||
request.put("taskType", type);
|
||||
request.set("input", canonical(mapper, mapper.valueToTree(input)));
|
||||
return new TaskCreationIdentity(hash(key), hash(mapper.writeValueAsString(request)));
|
||||
} catch (JsonProcessingException | IllegalArgumentException exception) {
|
||||
throw new ApiException("INVALID_REQUEST", "任务请求无法编码");
|
||||
}
|
||||
}
|
||||
|
||||
private static JsonNode canonical(ObjectMapper mapper, JsonNode node) {
|
||||
if (node.isObject()) {
|
||||
ObjectNode result = mapper.createObjectNode();
|
||||
var fields = new TreeSet<String>();
|
||||
node.fieldNames().forEachRemaining(fields::add);
|
||||
for (String field : fields) result.set(field, canonical(mapper, node.get(field)));
|
||||
return result;
|
||||
}
|
||||
if (node.isArray()) {
|
||||
ArrayNode result = mapper.createArrayNode();
|
||||
node.forEach(value -> result.add(canonical(mapper, value)));
|
||||
return result;
|
||||
}
|
||||
if (node.isNumber()) return DecimalNode.valueOf(node.decimalValue().stripTrailingZeros());
|
||||
return node;
|
||||
}
|
||||
|
||||
private static String hash(String value) {
|
||||
try {
|
||||
return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(value.getBytes(StandardCharsets.UTF_8)));
|
||||
} catch (NoSuchAlgorithmException exception) {
|
||||
throw new IllegalStateException("SHA-256 is required by the Java runtime", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.moyi.controlplane.task;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Supplier;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
/** Durable provider observations only. No upstream calls, wallet mutation, retry or refund policy. */
|
||||
final class TaskOutcomeJournal {
|
||||
record Completion(String taskId, String status, Map<String,Object> result, String code, String message) { }
|
||||
|
||||
private final JdbcTemplate jdbc;
|
||||
private final ObjectMapper mapper;
|
||||
private final TransactionTemplate transactions;
|
||||
private final UsageAuditService usage;
|
||||
|
||||
TaskOutcomeJournal(JdbcTemplate jdbc, ObjectMapper mapper, TransactionTemplate transactions, UsageAuditService usage) {
|
||||
this.jdbc = jdbc;
|
||||
this.mapper = mapper;
|
||||
this.transactions = transactions;
|
||||
this.usage = usage;
|
||||
}
|
||||
|
||||
Completion observeTerminal(String taskId, String status, Map<String,Object> result, String code, String message) {
|
||||
if (!List.of("succeeded", "failed", "cancelled").contains(status)) throw new IllegalArgumentException("Terminal outcome required");
|
||||
return transaction(() -> {
|
||||
var row = jdbc.queryForMap("SELECT status,completion_status,result::text AS result,error::text AS error FROM model_tasks WHERE id=? FOR UPDATE", taskId);
|
||||
if (!List.of("queued", "running", "reconciling").contains(row.get("status"))) return null;
|
||||
if (row.get("completion_status") != null) return completion(taskId, row);
|
||||
jdbc.update("UPDATE model_tasks SET completion_status=?,result=CAST(? AS jsonb),error=CAST(? AS jsonb),updated_at=now() WHERE id=?",
|
||||
status, json(result), code == null ? null : json(Map.of("code", code, "message", message)), taskId);
|
||||
return new Completion(taskId, status, result, code, message);
|
||||
});
|
||||
}
|
||||
|
||||
List<Completion> pendingCompletions() {
|
||||
return jdbc.queryForList("SELECT id,completion_status,result::text AS result,error::text AS error FROM model_tasks WHERE status IN ('running','reconciling') AND completion_status IS NOT NULL ORDER BY updated_at LIMIT 50")
|
||||
.stream().map(row -> completion(String.valueOf(row.get("id")), row)).toList();
|
||||
}
|
||||
|
||||
void accepted(String taskId, String providerTaskId, Object keyFingerprint, OffsetDateTime submittedAt, OffsetDateTime deadline) {
|
||||
transaction(() -> {
|
||||
int changed = jdbc.update("UPDATE model_tasks SET status='running',provider_task_id=?,provider_key_fingerprint=?,provider_deadline_at=?,error=NULL,reconciliation_required_at=NULL,updated_at=? WHERE id=? AND status IN ('running','reconciling') AND provider_task_id IS NULL AND completion_status IS NULL",
|
||||
providerTaskId, keyFingerprint, deadline, submittedAt, taskId);
|
||||
if (changed > 0) usage.updateTask(taskId, "running", null, null, null);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
void requireReconciliation(String taskId, String code, String message, boolean interruptedClaim) {
|
||||
transaction(() -> {
|
||||
String claimGuard = interruptedClaim
|
||||
? " AND provider_task_id IS NULL AND updated_at < now()-interval '2 minutes'" : "";
|
||||
int changed = jdbc.update("UPDATE model_tasks SET status='reconciling',error=CAST(? AS jsonb),reconciliation_required_at=now(),updated_at=now() WHERE id=? AND status IN ('queued','running') AND completion_status IS NULL" + claimGuard,
|
||||
json(Map.of("code", code, "message", message, "retryable", false, "requiresReconciliation", true)), taskId);
|
||||
if (changed > 0) {
|
||||
usage.updateTask(taskId, "reconciling", null, code, message);
|
||||
jdbc.update("UPDATE usage_events SET cost_status='pending',cost_amount=NULL WHERE provider_task_id=? AND event_kind='task'", taskId);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
private Completion completion(String taskId, Map<String,Object> row) {
|
||||
Map<String,Object> error = map(row.get("error"));
|
||||
return new Completion(taskId, String.valueOf(row.get("completion_status")), map(row.get("result")),
|
||||
error == null ? null : (String) error.get("code"), error == null ? null : (String) error.get("message"));
|
||||
}
|
||||
|
||||
private Map<String,Object> map(Object value) {
|
||||
if (value == null) return null;
|
||||
try { return mapper.readValue(String.valueOf(value), new TypeReference<>() {}); }
|
||||
catch (JsonProcessingException exception) { throw new IllegalStateException("Invalid persisted provider observation"); }
|
||||
}
|
||||
|
||||
private String json(Object value) {
|
||||
if (value == null) return null;
|
||||
try { return mapper.writeValueAsString(value); }
|
||||
catch (JsonProcessingException exception) { throw new IllegalStateException("Provider observation cannot be encoded"); }
|
||||
}
|
||||
|
||||
private <T> T transaction(Supplier<T> work) {
|
||||
return transactions == null ? work.get() : transactions.execute(status -> work.get());
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
package com.moyi.controlplane.task;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
/** Administrative evidence and optimistic-concurrency preconditions, never a raw provider key. */
|
||||
public record TaskReconciliationCommand(
|
||||
UUID requestId,
|
||||
String action,
|
||||
OffsetDateTime expectedUpdatedAt,
|
||||
String evidenceReference,
|
||||
String note,
|
||||
String providerTaskId,
|
||||
String credentialFingerprint,
|
||||
BigDecimal expectedCost,
|
||||
Map<String,Object> result
|
||||
) {
|
||||
@Override public String toString() { return "TaskReconciliationCommand[requestId=" + requestId + ", action=" + action + "]"; }
|
||||
}
|
||||
+42
@@ -0,0 +1,42 @@
|
||||
package com.moyi.controlplane.task;
|
||||
|
||||
import com.moyi.controlplane.auth.AuthController;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import java.util.Map;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/admin/task-reconciliation")
|
||||
public class TaskReconciliationController {
|
||||
private final TaskReconciliationService reconciliations;
|
||||
public TaskReconciliationController(TaskReconciliationService reconciliations) { this.reconciliations = reconciliations; }
|
||||
|
||||
@GetMapping
|
||||
public Map<String,Object> list(@RequestParam(defaultValue="1") int page, HttpServletRequest request) {
|
||||
return reconciliations.list(AuthController.sessionToken(request), page);
|
||||
}
|
||||
|
||||
@GetMapping("/{taskId}")
|
||||
public Map<String,Object> detail(@PathVariable String taskId, HttpServletRequest request) {
|
||||
return reconciliations.detail(AuthController.sessionToken(request), taskId);
|
||||
}
|
||||
|
||||
@PostMapping(value="/{taskId}", consumes="application/json")
|
||||
public Map<String,Object> reconcile(@PathVariable String taskId, @RequestBody TaskReconciliationCommand command, HttpServletRequest request) {
|
||||
// A custom header + JSON requires CORS preflight; the header is a CSRF barrier, not authentication.
|
||||
if (!"console".equals(request.getHeader("X-Moyi-Source"))) throw new ApiException("CONSOLE_REQUEST_REQUIRED", "请通过管理控制台发起核账", 403);
|
||||
return reconciliations.reconcile(AuthController.sessionToken(request), taskId, command, AuthController.meta(request));
|
||||
}
|
||||
|
||||
@PostMapping(value="/{taskId}/quote", consumes="application/json")
|
||||
public Map<String,Object> quote(@PathVariable String taskId, @RequestBody Map<String,Object> result, HttpServletRequest request) {
|
||||
return reconciliations.quote(AuthController.sessionToken(request), taskId, result);
|
||||
}
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package com.moyi.controlplane.task;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.math.BigDecimal;
|
||||
import java.net.URI;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/** Only normalized assets and numeric usage may enter a manual completion, never raw provider payloads. */
|
||||
final class TaskReconciliationResult {
|
||||
private static final Set<String> ASSETS = Set.of("url", "modelUrl", "videoUrl", "imageUrl", "audioUrl", "previewUrl");
|
||||
private static final Set<String> OPENAI = Set.of("input_tokens", "output_tokens", "total_tokens", "cached_tokens", "input_tokens_details");
|
||||
private static final Set<String> DETAILS = Set.of("text_tokens", "image_tokens", "cached_tokens", "cached_tokens_details");
|
||||
private static final Set<String> CACHED = Set.of("text_tokens", "image_tokens");
|
||||
private static final Set<String> GEMINI = Set.of("promptTokenCount", "candidatesTokenCount", "thoughtsTokenCount", "cachedContentTokenCount", "totalTokenCount");
|
||||
|
||||
static Map<String,Object> normalize(Map<String,Object> input, ObjectMapper json) {
|
||||
if (input == null) throw invalid();
|
||||
try { if (json.writeValueAsBytes(input).length > 32_768) throw invalid(); }
|
||||
catch (JsonProcessingException exception) { throw invalid(); }
|
||||
var result = new LinkedHashMap<String,Object>();
|
||||
for (var entry : input.entrySet()) {
|
||||
String field = entry.getKey();
|
||||
Object value = entry.getValue();
|
||||
if (ASSETS.contains(field)) {
|
||||
if (!(value instanceof String text) || text.length() > 4096 || text.chars().anyMatch(Character::isISOControl)) throw invalid();
|
||||
try {
|
||||
URI uri = URI.create(text);
|
||||
if (!("https".equalsIgnoreCase(uri.getScheme()) || "http".equalsIgnoreCase(uri.getScheme())) || uri.getHost() == null || uri.getUserInfo() != null) throw invalid();
|
||||
} catch (IllegalArgumentException exception) { throw invalid(); }
|
||||
result.put(field, text);
|
||||
} else if (Set.of("format", "mimeType").contains(field)) {
|
||||
if (!(value instanceof String text) || text.isBlank() || text.length() > 128 || text.chars().anyMatch(Character::isISOControl)) throw invalid();
|
||||
result.put(field, text);
|
||||
} else if ("usage".equals(field)) result.put(field, numericMap(value, OPENAI));
|
||||
else if ("usageMetadata".equals(field)) result.put(field, numericMap(value, GEMINI));
|
||||
else throw invalid();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static Map<String,Object> numericMap(Object value, Set<String> allowed) {
|
||||
if (!(value instanceof Map<?,?> map)) throw invalid();
|
||||
var result = new LinkedHashMap<String,Object>();
|
||||
for (var entry : map.entrySet()) {
|
||||
if (!(entry.getKey() instanceof String name) || !allowed.contains(name)) throw invalid();
|
||||
if ("input_tokens_details".equals(name)) result.put(name, numericMap(entry.getValue(), DETAILS));
|
||||
else if ("cached_tokens_details".equals(name)) result.put(name, numericMap(entry.getValue(), CACHED));
|
||||
else {
|
||||
try {
|
||||
long count = new BigDecimal(String.valueOf(entry.getValue())).longValueExact();
|
||||
if (count < 0 || count > 100_000_000) throw invalid();
|
||||
result.put(name, count);
|
||||
} catch (NumberFormatException | ArithmeticException exception) { throw invalid(); }
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private static ApiException invalid() { return new ApiException("INVALID_RECONCILIATION_RESULT", "只接受有界的 HTTP(S) 结果地址和规范化数值用量,不接受原始上游响应或密钥", 422); }
|
||||
}
|
||||
+216
@@ -0,0 +1,216 @@
|
||||
package com.moyi.controlplane.task;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.auth.AuthService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.gateway.GatewayGuard;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import java.math.BigDecimal;
|
||||
import java.math.RoundingMode;
|
||||
import java.time.OffsetDateTime;
|
||||
import java.time.ZoneOffset;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
/** Evidence-backed administrative decisions; no generation, arbitrary balance adjustment or transparent replay. */
|
||||
@Service
|
||||
public class TaskReconciliationService {
|
||||
private final JdbcTemplate jdbc;
|
||||
private final ObjectMapper json;
|
||||
private final AuthService auth;
|
||||
private final TaskSettlementService settlements;
|
||||
private final AsyncProviderClient provider;
|
||||
private final GatewayGuard guard;
|
||||
private final UsageAuditService usage;
|
||||
private final TransactionTemplate transactions;
|
||||
private final long timeoutMs;
|
||||
// List/detail must not load prompts, original inputs or potentially large generated media.
|
||||
private static final String TASK_COLUMNS = "SELECT t.id,t.user_id,t.api_key_id,t.model_id,t.task_type,t.status,t.updated_at,t.reserved_amount,t.cost_amount,t.provider_id_snapshot,t.upstream_model_snapshot,t.provider_task_id,t.provider_key_fingerprint,t.completion_status,t.pricing_snapshot::text AS frozen_pricing,t.error::text AS error FROM model_tasks t";
|
||||
|
||||
public TaskReconciliationService(JdbcTemplate jdbc, ObjectMapper json, AuthService auth,
|
||||
TaskSettlementService settlements, AsyncProviderClient provider, GatewayGuard guard,
|
||||
UsageAuditService usage, PlatformTransactionManager manager,
|
||||
@Value("${moyi.tasks.provider-timeout-ms:3600000}") long timeoutMs) {
|
||||
this.jdbc = jdbc; this.json = json; this.auth = auth; this.settlements = settlements;
|
||||
this.provider = provider; this.guard = guard; this.usage = usage;
|
||||
this.transactions = new TransactionTemplate(manager);
|
||||
if (timeoutMs < 1000) throw new IllegalArgumentException("Polling deadline must be positive");
|
||||
this.timeoutMs = timeoutMs;
|
||||
}
|
||||
|
||||
public Map<String,Object> list(String token, int requestedPage) {
|
||||
auth.requireAdmin(token);
|
||||
int page = Math.max(1, Math.min(100000, requestedPage));
|
||||
return Map.of("records", jdbc.queryForList(TASK_COLUMNS + " WHERE t.status='reconciling' ORDER BY t.reconciliation_required_at NULLS FIRST,t.id LIMIT 50 OFFSET ?", (page - 1) * 50).stream().map(this::view).toList(),
|
||||
"total", jdbc.queryForObject("SELECT count(*) FROM model_tasks WHERE status='reconciling'", Long.class), "page", page);
|
||||
}
|
||||
|
||||
public Map<String,Object> detail(String token, String taskId) {
|
||||
auth.requireAdmin(token);
|
||||
Map<String,Object> task = task(taskId, false);
|
||||
var history = jdbc.queryForList("SELECT request_id,actor_user_id,action,evidence_reference,note,before_state::text AS before_state,after_state::text AS after_state,created_at FROM task_reconciliations WHERE task_id=? ORDER BY created_at DESC LIMIT 50", taskId);
|
||||
var records = history.stream().map(row -> {
|
||||
var item = new LinkedHashMap<String,Object>();
|
||||
for (String field : List.of("request_id", "actor_user_id", "action", "evidence_reference", "note", "created_at")) item.put(field, row.get(field));
|
||||
item.put("before_state", decode(row.get("before_state")));
|
||||
item.put("after_state", decode(row.get("after_state")));
|
||||
return item;
|
||||
}).toList();
|
||||
return Map.of("task", view(task), "history", records);
|
||||
}
|
||||
|
||||
public Map<String,Object> quote(String token, String taskId, Map<String,Object> input) {
|
||||
auth.requireAdmin(token);
|
||||
var task = task(taskId, false);
|
||||
if (!"reconciling".equals(task.get("status")) || task.get("completion_status") != null || task.get("cost_amount") != null) throw conflict("任务不处于可核账状态,请刷新");
|
||||
var result = TaskReconciliationResult.normalize(input, json);
|
||||
return Map.of("expectedCost", confirmedCharge(task, result).toPlainString(),
|
||||
"expectedUpdatedAt", timestamp(task.get("updated_at")).toInstant().toString());
|
||||
}
|
||||
|
||||
public Map<String,Object> reconcile(String token, String taskId, TaskReconciliationCommand command, AuthService.RequestMeta meta) {
|
||||
var actor = auth.requireAdmin(token); // Authorization is required even for replay.
|
||||
validate(command);
|
||||
Map<String,Object> result = "confirm_succeeded".equals(command.action()) ? TaskReconciliationResult.normalize(command.result(), json) : null;
|
||||
String requestHash = requestHash(command, taskId, result);
|
||||
return transactions.execute(status -> {
|
||||
// Keep role/status revocation ordered with this financial decision, not a stale pre-check.
|
||||
if (jdbc.queryForList("SELECT id FROM users WHERE id=? AND status='active' AND role IN ('admin','super_admin') FOR SHARE", actor.id()).isEmpty()) throw new ApiException("FORBIDDEN", "管理员权限已变化,请重新登录", 403);
|
||||
auth.requireAdmin(token); // Recheck session revocation/expiry after entering the transaction.
|
||||
jdbc.queryForList("SELECT pg_advisory_xact_lock(hashtextextended(?,0))", "moyi:task-reconciliation:" + command.requestId());
|
||||
var prior = jdbc.queryForList("SELECT actor_user_id,request_hash,after_state::text AS after_state FROM task_reconciliations WHERE request_id=?", command.requestId());
|
||||
if (!prior.isEmpty()) {
|
||||
var receipt = prior.getFirst();
|
||||
if (!actor.id().equals(receipt.get("actor_user_id")) || !requestHash.equals(receipt.get("request_hash"))) throw conflict("此核账请求编号已用于不同的操作");
|
||||
return Map.of("task", decode(receipt.get("after_state")), "requestId", command.requestId(), "replayed", true);
|
||||
}
|
||||
var task = task(taskId, true);
|
||||
if (!"reconciling".equals(task.get("status")) || task.get("completion_status") != null || task.get("cost_amount") != null) throw conflict("任务不处于可人工核账状态,或已有可信终态;请刷新");
|
||||
if (!timestamp(task.get("updated_at")).toInstant().equals(command.expectedUpdatedAt().toInstant())) throw conflict("任务已被其他操作更新,请刷新后重新核对");
|
||||
var wallets = jdbc.queryForList("SELECT balance,reserved_balance FROM user_wallets WHERE user_id=? FOR UPDATE", task.get("user_id"));
|
||||
if (wallets.isEmpty() || ((BigDecimal) wallets.getFirst().get("reserved_balance")).compareTo((BigDecimal) task.get("reserved_amount")) < 0) throw conflict("任务冻结额度与钱包不一致,请先排查账本,不允许直接覆盖");
|
||||
Map<String,Object> before = view(task);
|
||||
if ("resume_polling".equals(command.action())) resume(task, command);
|
||||
else {
|
||||
if ("confirm_succeeded".equals(command.action())) {
|
||||
BigDecimal charge = confirmedCharge(task, result);
|
||||
if (charge == null || charge.compareTo(command.expectedCost()) != 0) throw conflict("确认金额与任务原价格快照及实际用量不一致");
|
||||
}
|
||||
String target = "confirm_succeeded".equals(command.action()) ? "succeeded" : "failed";
|
||||
String code = "failed".equals(target) ? "TASK_RECONCILED_FAILED" : null;
|
||||
String message = "failed".equals(target) ? "管理员已根据上游记录确认任务失败,冻结额度已释放" : null;
|
||||
// The observed outcome, shared wallet transaction and both audit receipts commit together.
|
||||
jdbc.update("UPDATE model_tasks SET completion_status=? WHERE id=?", target, taskId);
|
||||
settlements.finish(taskId, target, "succeeded".equals(target) ? 100 : 0, result, code, message);
|
||||
}
|
||||
Map<String,Object> after = view(task(taskId, false));
|
||||
jdbc.update("INSERT INTO task_reconciliations(request_id,task_id,actor_user_id,action,request_hash,evidence_reference,note,before_state,after_state) VALUES (?,?,?,?,?,?,?,CAST(? AS jsonb),CAST(? AS jsonb))",
|
||||
command.requestId(), taskId, actor.id(), command.action(), requestHash, command.evidenceReference().trim(), command.note().trim(), encode(before), encode(after));
|
||||
auth.audit("task.reconciled", actor.id(), meta, Map.of("taskId", taskId, "requestId", command.requestId(), "action", command.action(), "evidenceReference", command.evidenceReference().trim()));
|
||||
return Map.of("task", after, "requestId", command.requestId(), "replayed", false);
|
||||
});
|
||||
}
|
||||
|
||||
private BigDecimal confirmedCharge(Map<String,Object> task, Map<String,Object> result) {
|
||||
try { return settlements.confirmedCharge(task, result); }
|
||||
catch (ApiException exception) {
|
||||
if ("TASK_USAGE_PENDING".equals(exception.getCode())) throw new ApiException("TASK_USAGE_REQUIRED", "成功核账必须提供完整可核验用量;任务与冻结额度保持不变", 422);
|
||||
throw exception;
|
||||
}
|
||||
}
|
||||
|
||||
private void resume(Map<String,Object> task, TaskReconciliationCommand command) {
|
||||
String originalId = (String) task.get("provider_task_id");
|
||||
if (originalId != null && !originalId.equals(command.providerTaskId())) throw conflict("不能将已受理任务改绑到另一个上游编号");
|
||||
String fingerprint = (String) task.get("provider_key_fingerprint");
|
||||
if (fingerprint != null && !fingerprint.equals(command.credentialFingerprint())) throw conflict("必须继续使用创建任务时的账号指纹,不能切换账号");
|
||||
if (task.get("provider_id_snapshot") == null || task.get("upstream_model_snapshot") == null) throw conflict("历史任务原执行路由无法证明,禁止猜测后恢复查询;请核实终态");
|
||||
provider.requirePollableBinding(String.valueOf(task.get("provider_id_snapshot")), String.valueOf(task.get("model_id")),
|
||||
String.valueOf(task.get("upstream_model_snapshot")), String.valueOf(task.get("task_type")), command.credentialFingerprint());
|
||||
String id = String.valueOf(task.get("id"));
|
||||
if (!guard.tryTaskUserSlot((java.util.UUID) task.get("api_key_id"), id, timeoutMs + 180000)) throw new ApiException("TASK_USER_BUSY", "用户当前并发已满,尚未恢复轮询,请稍后重试", 429);
|
||||
jdbc.update("UPDATE model_tasks SET status='running',provider_task_id=?,provider_key_fingerprint=?,provider_deadline_at=?,error=NULL,reconciliation_required_at=NULL,updated_at=now() WHERE id=?",
|
||||
command.providerTaskId(), command.credentialFingerprint(), OffsetDateTime.now(ZoneOffset.UTC).plus(java.time.Duration.ofMillis(timeoutMs)), id);
|
||||
usage.updateTask(id, "running", null, null, null);
|
||||
}
|
||||
|
||||
private void validate(TaskReconciliationCommand command) {
|
||||
if (command == null || command.requestId() == null || command.expectedUpdatedAt() == null) throw new ApiException("RECONCILIATION_PRECONDITION_REQUIRED", "必须提供操作编号和任务版本", 422);
|
||||
if (command.action() == null || !List.of("resume_polling", "confirm_failed", "confirm_succeeded").contains(command.action())) throw new ApiException("INVALID_RECONCILIATION_ACTION", "核账操作无效", 422);
|
||||
text(command.evidenceReference(), 3, 500); text(command.note(), 10, 2000);
|
||||
if ("resume_polling".equals(command.action())) {
|
||||
text(command.providerTaskId(), 1, 160);
|
||||
if (!command.providerTaskId().matches("[A-Za-z0-9][A-Za-z0-9._:-]{0,159}")) throw new ApiException("INVALID_PROVIDER_TASK_ID", "上游任务编号不能包含路径、查询串或空白", 422);
|
||||
if (command.credentialFingerprint() == null || !command.credentialFingerprint().matches("[0-9a-f]{64}")) throw new ApiException("INVALID_CREDENTIAL_FINGERPRINT", "必须提供 SHA-256 账号指纹,不能填写 API Key", 422);
|
||||
if (command.result() != null || command.expectedCost() != null) throw conflict("恢复轮询不能同时修改结果或扣费金额");
|
||||
} else {
|
||||
if (command.providerTaskId() != null || command.credentialFingerprint() != null) throw conflict("终态核账不能改绑上游编号或账号");
|
||||
if ("confirm_succeeded".equals(command.action())) {
|
||||
if (command.expectedCost() == null || command.expectedCost().signum() < 0 || command.expectedCost().precision() > 20) throw new ApiException("EXPECTED_COST_REQUIRED", "请确认按原价格快照核算的非负扣费金额", 422);
|
||||
try { command.expectedCost().setScale(8, RoundingMode.UNNECESSARY); }
|
||||
catch (ArithmeticException exception) { throw new ApiException("INVALID_EXPECTED_COST", "确认金额最多 8 位小数", 422); }
|
||||
} else if (command.result() != null || command.expectedCost() != null) throw conflict("确认失败不能附带成功结果或扣费金额");
|
||||
}
|
||||
}
|
||||
|
||||
private void text(String value, int min, int max) {
|
||||
if (value == null || value.trim().length() < min || value.trim().length() > max || value.chars().anyMatch(Character::isISOControl)) throw new ApiException("RECONCILIATION_EVIDENCE_REQUIRED", "必须填写有效的上游证据引用与 10–2000 字核账说明;不得包含控制字符", 422);
|
||||
}
|
||||
|
||||
private String requestHash(TaskReconciliationCommand command, String taskId, Map<String,Object> result) {
|
||||
var body = new LinkedHashMap<String,Object>();
|
||||
body.put("taskId", taskId); body.put("action", command.action()); body.put("expectedUpdatedAt", command.expectedUpdatedAt().toInstant().toString());
|
||||
body.put("evidenceReference", command.evidenceReference().trim()); body.put("note", command.note().trim());
|
||||
body.put("providerTaskId", command.providerTaskId()); body.put("credentialFingerprint", command.credentialFingerprint());
|
||||
body.put("expectedCost", command.expectedCost()); body.put("result", result);
|
||||
return TaskCreationIdentity.of(json, command.requestId().toString(), "task-reconciliation", body).requestHash();
|
||||
}
|
||||
|
||||
private Map<String,Object> task(String taskId, boolean lock) {
|
||||
if (taskId == null || taskId.isBlank() || taskId.length() > 160) throw new ApiException("TASK_NOT_FOUND", "任务不存在", 404);
|
||||
var rows = jdbc.queryForList(TASK_COLUMNS + " WHERE t.id=?" + (lock ? " FOR UPDATE" : ""), taskId);
|
||||
if (rows.isEmpty()) throw new ApiException("TASK_NOT_FOUND", "任务不存在", 404);
|
||||
var task = rows.getFirst();
|
||||
task.put("pricing_snapshot", task.get("frozen_pricing"));
|
||||
return task;
|
||||
}
|
||||
|
||||
private Map<String,Object> view(Map<String,Object> task) {
|
||||
var view = new LinkedHashMap<String,Object>();
|
||||
view.put("id", task.get("id")); view.put("userId", task.get("user_id")); view.put("modelId", task.get("model_id"));
|
||||
view.put("taskType", task.get("task_type")); view.put("status", task.get("status"));
|
||||
view.put("updatedAt", timestamp(task.get("updated_at")).toInstant().toString());
|
||||
view.put("reservedAmount", ((BigDecimal) task.get("reserved_amount")).toPlainString());
|
||||
view.put("costAmount", task.get("cost_amount") == null ? null : ((BigDecimal) task.get("cost_amount")).toPlainString());
|
||||
view.put("providerId", task.get("provider_id_snapshot")); view.put("upstreamModel", task.get("upstream_model_snapshot"));
|
||||
view.put("providerTaskId", task.get("provider_task_id")); view.put("credentialFingerprint", task.get("provider_key_fingerprint"));
|
||||
view.put("completionStatus", task.get("completion_status")); view.put("requiresReconciliation", "reconciling".equals(task.get("status")));
|
||||
view.put("pricing", task.get("frozen_pricing") == null ? null : decode(task.get("frozen_pricing")));
|
||||
view.put("error", task.get("error") == null ? null : decode(task.get("error")));
|
||||
return view;
|
||||
}
|
||||
|
||||
private OffsetDateTime timestamp(Object value) {
|
||||
if (value instanceof OffsetDateTime time) return time;
|
||||
if (value instanceof java.sql.Timestamp time) return time.toInstant().atOffset(ZoneOffset.UTC);
|
||||
throw new IllegalStateException("Persisted task timestamp missing");
|
||||
}
|
||||
|
||||
private Map<String,Object> decode(Object value) {
|
||||
try { return json.readValue(String.valueOf(value), new TypeReference<>() {}); }
|
||||
catch (JsonProcessingException exception) { throw new IllegalStateException("Persisted reconciliation record cannot be read"); }
|
||||
}
|
||||
private String encode(Object value) {
|
||||
try { return json.writeValueAsString(value); }
|
||||
catch (JsonProcessingException exception) { throw new IllegalStateException("Reconciliation record cannot be encoded"); }
|
||||
}
|
||||
private ApiException conflict(String message) { return new ApiException("RECONCILIATION_CONFLICT", message, 409); }
|
||||
}
|
||||
+125
@@ -0,0 +1,125 @@
|
||||
package com.moyi.controlplane.task;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.gateway.EnterprisePolicies;
|
||||
import com.moyi.controlplane.gateway.GatewayGuard;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import java.math.BigDecimal;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.function.Supplier;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
|
||||
/** The one task/wallet/usage settlement transaction, shared by workers and audited reconciliation. */
|
||||
@Service
|
||||
public class TaskSettlementService {
|
||||
private final JdbcTemplate jdbc;
|
||||
private final ObjectMapper json;
|
||||
private final GatewayGuard guard;
|
||||
private final UsageAuditService usage;
|
||||
private final EnterprisePolicies policies;
|
||||
private final TransactionTemplate transactions;
|
||||
|
||||
public TaskSettlementService(JdbcTemplate jdbc, ObjectMapper json, GatewayGuard guard,
|
||||
UsageAuditService usage, PlatformTransactionManager manager) {
|
||||
this.jdbc = jdbc;
|
||||
this.json = json;
|
||||
this.guard = guard;
|
||||
this.usage = usage;
|
||||
this.policies = new EnterprisePolicies(jdbc, json);
|
||||
this.transactions = manager == null ? null : new TransactionTemplate(manager);
|
||||
}
|
||||
|
||||
void finish(String taskId, String status, int progress, Map<String,Object> result, String code, String message) {
|
||||
if (!List.of("succeeded", "failed", "cancelled").contains(status)) throw new IllegalArgumentException("Terminal task state required");
|
||||
transaction(() -> {
|
||||
var task = jdbc.queryForMap("SELECT status,completion_status,pricing_snapshot::text AS pricing_snapshot FROM model_tasks WHERE id=? FOR UPDATE", taskId);
|
||||
if (!List.of("queued", "running", "reconciling").contains(task.get("status"))) return null;
|
||||
if (task.get("completion_status") != null && !status.equals(task.get("completion_status"))) return null;
|
||||
AsyncPriceSchedule.Bill bill = null;
|
||||
String finalCode = code, finalMessage = message;
|
||||
if ("succeeded".equals(status) && task.get("pricing_snapshot") != null) {
|
||||
try { bill = AsyncPriceSchedule.bill(pricing(task.get("pricing_snapshot")), result); }
|
||||
catch (ApiException exception) {
|
||||
finalCode = "TASK_USAGE_PENDING";
|
||||
finalMessage = "生成已完成,但上游计费用量未能核对;暂不扣费,等待核账";
|
||||
}
|
||||
}
|
||||
jdbc.update("UPDATE model_tasks SET status=?,progress=?,result=CAST(? AS jsonb),error=CAST(? AS jsonb),reconciliation_required_at=NULL,updated_at=now(),completed_at=now() WHERE id=?",
|
||||
status, progress, encode(result), finalCode == null ? null : encode(Map.of("code", finalCode, "message", finalMessage)), taskId);
|
||||
if ("TASK_USAGE_PENDING".equals(finalCode)) {
|
||||
release(taskId);
|
||||
jdbc.update("UPDATE model_tasks SET cost_amount=NULL WHERE id=?", taskId);
|
||||
var observed = new LinkedHashMap<String,Object>();
|
||||
if (result != null) for (String field : List.of("usage", "usageMetadata")) if (result.get(field) instanceof Map<?,?> value) observed.put(field, value);
|
||||
jdbc.update("UPDATE usage_events SET cost_amount=NULL,cost_status='pending',observed_usage=CAST(? AS jsonb) WHERE provider_task_id=? AND event_kind='task'", encode(observed), taskId);
|
||||
} else if ("succeeded".equals(status)) settle(taskId, bill);
|
||||
else release(taskId);
|
||||
usage.updateTask(taskId, status, result, finalCode, finalMessage);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
/** Strict calculation for explicit administrator confirmation: missing usage is an error, not a refund. */
|
||||
BigDecimal confirmedCharge(Map<String,Object> task, Map<String,Object> result) {
|
||||
if (task.get("pricing_snapshot") == null) return (BigDecimal) task.get("reserved_amount");
|
||||
return AsyncPriceSchedule.bill(pricing(task.get("pricing_snapshot")), result).amount();
|
||||
}
|
||||
|
||||
private void settle(String taskId, AsyncPriceSchedule.Bill bill) {
|
||||
var task = jdbc.queryForMap("SELECT user_id,reserved_amount,cost_amount FROM model_tasks WHERE id=? FOR UPDATE", taskId);
|
||||
BigDecimal reserved = (BigDecimal) task.get("reserved_amount");
|
||||
BigDecimal amount = bill == null ? reserved : bill.amount();
|
||||
releasePolicies(taskId, task);
|
||||
if (amount == null || task.get("cost_amount") != null) return;
|
||||
UUID userId = (UUID) task.get("user_id");
|
||||
var wallet = jdbc.queryForMap("SELECT balance,reserved_balance FROM user_wallets WHERE user_id=? FOR UPDATE", userId);
|
||||
BigDecimal before = (BigDecimal) wallet.get("balance"), after = before.subtract(amount);
|
||||
jdbc.update("UPDATE user_wallets SET balance=?,reserved_balance=GREATEST(0,reserved_balance-?),updated_at=now() WHERE user_id=?", after, reserved, userId);
|
||||
if (amount.signum() > 0) jdbc.update("INSERT INTO wallet_entries(id,user_id,request_id,amount,balance_after,currency,reason,operation,requested_amount,balance_before,actor_user_id) VALUES (?,?,?,?,?,'CNY',?,'usage',?,?,NULL)",
|
||||
UUID.randomUUID(), userId, taskRequestId(taskId), amount.negate(), after, "异步模型任务 · " + taskId, amount, before);
|
||||
jdbc.update("UPDATE model_tasks SET cost_amount=? WHERE id=?", amount, taskId);
|
||||
jdbc.update("UPDATE usage_events SET cost_amount=? WHERE provider_task_id=? AND event_kind='task'", amount, taskId);
|
||||
if (bill != null) jdbc.update("UPDATE usage_events SET input_units=?,output_units=?,observed_usage=jsonb_build_object('input_tokens',?::bigint,'output_tokens',?::bigint,'image_input_tokens',?::bigint) WHERE provider_task_id=? AND event_kind='task'",
|
||||
bill.input(), bill.output(), bill.input(), bill.output(), bill.imageInput(), taskId);
|
||||
}
|
||||
|
||||
private void release(String taskId) {
|
||||
var task = jdbc.queryForMap("SELECT user_id,reserved_amount,cost_amount FROM model_tasks WHERE id=? FOR UPDATE", taskId);
|
||||
BigDecimal amount = (BigDecimal) task.get("reserved_amount");
|
||||
releasePolicies(taskId, task);
|
||||
if (amount != null && task.get("cost_amount") == null) {
|
||||
if (amount.signum() > 0) jdbc.update("UPDATE user_wallets SET reserved_balance=GREATEST(0,reserved_balance-?),updated_at=now() WHERE user_id=?", amount, task.get("user_id"));
|
||||
jdbc.update("UPDATE model_tasks SET cost_amount=0 WHERE id=?", taskId);
|
||||
jdbc.update("UPDATE usage_events SET cost_amount=0 WHERE provider_task_id=? AND event_kind='task'", taskId);
|
||||
}
|
||||
}
|
||||
|
||||
private void releasePolicies(String taskId, Map<String,Object> task) {
|
||||
policies.release(taskId);
|
||||
guard.releaseTaskUserSlot((UUID) task.get("user_id"), taskId);
|
||||
}
|
||||
|
||||
private Map<String,Object> pricing(Object value) {
|
||||
try { return json.readValue(String.valueOf(value), new TypeReference<>() {}); }
|
||||
catch (JsonProcessingException exception) { throw new ApiException("INVALID_PRICE_SNAPSHOT", "任务价格快照无法核验", 409); }
|
||||
}
|
||||
|
||||
private String encode(Object value) {
|
||||
if (value == null) return null;
|
||||
try { return json.writeValueAsString(value); }
|
||||
catch (JsonProcessingException exception) { throw new IllegalStateException("Task settlement cannot be encoded"); }
|
||||
}
|
||||
|
||||
private UUID taskRequestId(String taskId) { return UUID.nameUUIDFromBytes(("moyi-async:" + taskId).getBytes(StandardCharsets.UTF_8)); }
|
||||
private <T> T transaction(Supplier<T> work) { return transactions == null ? work.get() : transactions.execute(status -> work.get()); }
|
||||
}
|
||||
+55
-13
@@ -8,6 +8,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.moyi.controlplane.auth.AuthService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.gateway.GatewayGuard;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestMetadata;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
@@ -50,14 +51,22 @@ public class UsageAuditService {
|
||||
}
|
||||
|
||||
public void startChat(UUID id, UUID userId, UUID keyId, Map<String,Object> model, Map<String,Object> input) {
|
||||
String agent=null, source="api";
|
||||
startChat(id,userId,keyId,model,input,ApiRequestMetadata.internal());
|
||||
}
|
||||
|
||||
public void startChat(UUID id, UUID userId, UUID keyId, Map<String,Object> model, Map<String,Object> input,
|
||||
ApiRequestMetadata metadata) {
|
||||
String agent=null, source=source(metadata);
|
||||
String ip=metadata == null ? null : metadata.clientIp();
|
||||
var attributes=org.springframework.web.context.request.RequestContextHolder.getRequestAttributes();
|
||||
if(attributes instanceof org.springframework.web.context.request.ServletRequestAttributes servlet) {
|
||||
agent=servlet.getRequest().getHeader("User-Agent");
|
||||
if("playground".equals(servlet.getRequest().getHeader("X-Moyi-Source"))) source="playground";
|
||||
var request=servlet.getRequest();
|
||||
agent=request.getHeader("User-Agent");
|
||||
if(ip==null) ip=request.getRemoteAddr();
|
||||
if(metadata==null || metadata.source()==ApiRequestMetadata.Source.INTERNAL) source=isPlaygroundRequest(request)?"playground":"api";
|
||||
}
|
||||
jdbc.update("INSERT INTO usage_events(id,request_id,user_id,api_key_id,model_id,provider_id,event_kind,status,input_payload,endpoint,call_mode,request_source,user_agent,reasoning_effort) VALUES (?,?,?,?,?,?,'call','running',?::jsonb,'/v1/chat/completions',?,?,?,?)",
|
||||
id,id.toString(),userId,keyId,model.get("id"),model.get("providerId"),json(input),Boolean.TRUE.equals(input.get("stream"))?"stream":"sync",source,
|
||||
jdbc.update("INSERT INTO usage_events(id,request_id,user_id,api_key_id,model_id,provider_id,event_kind,status,input_payload,endpoint,call_mode,request_source,source_ip,user_agent,reasoning_effort) VALUES (?,?,?,?,?,?,'call','running',?::jsonb,'/v1/chat/completions',?,?,CAST(? AS inet),?,?)",
|
||||
id,id.toString(),userId,keyId,model.get("id"),model.get("providerId"),json(input),Boolean.TRUE.equals(input.get("stream"))?"stream":"sync",source,blankToNull(ip),
|
||||
agent==null?null:agent.substring(0,Math.min(1000,agent.length())),input.get("reasoning_effort")==null?null:String.valueOf(input.get("reasoning_effort")).substring(0,Math.min(32,String.valueOf(input.get("reasoning_effort")).length())));
|
||||
}
|
||||
|
||||
@@ -83,9 +92,18 @@ public class UsageAuditService {
|
||||
public void recordTask(
|
||||
UUID userId, UUID apiKeyId, String modelId, String providerId, String taskId,
|
||||
String status, Object input, Object output, String errorCode, String errorMessage
|
||||
) {
|
||||
recordTask(userId,apiKeyId,modelId,providerId,taskId,status,input,output,errorCode,errorMessage,ApiRequestMetadata.internal());
|
||||
}
|
||||
|
||||
public void recordTask(
|
||||
UUID userId, UUID apiKeyId, String modelId, String providerId, String taskId,
|
||||
String status, Object input, Object output, String errorCode, String errorMessage,
|
||||
ApiRequestMetadata metadata
|
||||
) {
|
||||
record(userId, apiKeyId, modelId, providerId, "task", status, null, 0, 0,
|
||||
input, output, taskId, errorCode, errorMessage, null, null);
|
||||
input, output, taskId, errorCode, errorMessage, metadata == null ? null : metadata.clientIp(), null,
|
||||
metadata == null ? null : source(metadata));
|
||||
}
|
||||
|
||||
public void updateTask(String taskId, String status, Object output, String errorCode, String errorMessage) {
|
||||
@@ -139,15 +157,17 @@ public class UsageAuditService {
|
||||
WITH pending AS (
|
||||
SELECT model_id,
|
||||
COUNT(*) FILTER (WHERE status='running') AS running,
|
||||
COUNT(*) FILTER (WHERE status='queued') AS queued
|
||||
FROM model_tasks WHERE status IN ('queued','running') GROUP BY model_id
|
||||
COUNT(*) FILTER (WHERE status='queued') AS queued,
|
||||
COUNT(*) FILTER (WHERE status='reconciling') AS reconciling
|
||||
FROM model_tasks WHERE status IN ('queued','running','reconciling') GROUP BY model_id
|
||||
)
|
||||
SELECT m.id, m.public_name, m.source_label, p.name AS provider_name, m.call_mode,
|
||||
CASE WHEN m.max_concurrency = 0 THEN 0
|
||||
ELSE COALESCE(m.max_concurrency, ?) END AS max_concurrency,
|
||||
(m.status='active' AND m.deleted_at IS NULL AND p.status='active' AND p.deleted_at IS NULL
|
||||
AND d.status='active' AND d.deleted_at IS NULL) AS route_enabled,
|
||||
COALESCE(pending.running, 0) AS running, COALESCE(pending.queued, 0) AS queued
|
||||
COALESCE(pending.running, 0) AS running, COALESCE(pending.queued, 0) AS queued,
|
||||
COALESCE(pending.reconciling, 0) AS reconciling
|
||||
FROM model_catalog m JOIN model_providers p ON p.id=m.provider_id
|
||||
JOIN model_definitions d ON d.id=m.logical_model_id
|
||||
LEFT JOIN pending ON pending.model_id=m.id
|
||||
@@ -164,6 +184,7 @@ public class UsageAuditService {
|
||||
group.put("limit", modelGuardEnabled ? rs.getInt("max_concurrency") : 0);
|
||||
group.put("running", rs.getLong("running"));
|
||||
group.put("queued", rs.getLong("queued"));
|
||||
group.put("reconciling", rs.getLong("reconciling"));
|
||||
return group;
|
||||
}, defaultModelLimit);
|
||||
List<String> synchronousIds = groups.stream().filter(group -> !"async".equals(group.get("mode"))).map(group -> group.get("id").toString()).toList();
|
||||
@@ -175,12 +196,13 @@ public class UsageAuditService {
|
||||
group.put("running", current == null ? null : current.running());
|
||||
group.put("queued", current == null ? null : current.queued());
|
||||
}
|
||||
// Keep disabled groups visible while their queued or running work still exists.
|
||||
// Keep disabled groups visible while any work or unresolved financial hold still exists.
|
||||
groups = groups.stream().filter(group -> Boolean.TRUE.equals(group.get("routeEnabled"))
|
||||
|| positive(group.get("running")) || positive(group.get("queued"))).toList();
|
||||
|| positive(group.get("running")) || positive(group.get("queued")) || positive(group.get("reconciling"))).toList();
|
||||
result.put("capacityGroups", groups);
|
||||
result.put("running", totalCapacity(groups, "running"));
|
||||
result.put("waiting", totalCapacity(groups, "queued"));
|
||||
result.put("reconciling", totalCapacity(groups, "reconciling"));
|
||||
result.put("capacityConfig", Map.of(
|
||||
"queueWaitMs", gatewayGuard == null ? 30000 : gatewayGuard.queueWaitMs(),
|
||||
"defaultModelConcurrency", defaultModelLimit,
|
||||
@@ -345,14 +367,20 @@ public class UsageAuditService {
|
||||
}
|
||||
|
||||
private void record(UUID userId, UUID keyId, String modelId, String providerId, String kind, String status, Integer latency, long inputUnits, long outputUnits, Object input, Object output, String taskId, String errorCode, String errorMessage, String ip, String userAgent) {
|
||||
record(userId,keyId,modelId,providerId,kind,status,latency,inputUnits,outputUnits,input,output,taskId,errorCode,errorMessage,ip,userAgent,null);
|
||||
}
|
||||
|
||||
private void record(UUID userId, UUID keyId, String modelId, String providerId, String kind, String status, Integer latency, long inputUnits, long outputUnits, Object input, Object output, String taskId, String errorCode, String errorMessage, String ip, String userAgent, String explicitSource) {
|
||||
String endpoint=null, source=null;
|
||||
var attributes=org.springframework.web.context.request.RequestContextHolder.getRequestAttributes();
|
||||
if(attributes instanceof org.springframework.web.context.request.ServletRequestAttributes servlet) {
|
||||
var request=servlet.getRequest();
|
||||
endpoint=request.getRequestURI();
|
||||
userAgent=request.getHeader("User-Agent");
|
||||
source="playground".equals(request.getHeader("X-Moyi-Source")) ? "playground" : "api";
|
||||
if(ip==null) ip=request.getRemoteAddr();
|
||||
source=explicitSource == null ? (isPlaygroundRequest(request) ? "playground" : "api") : explicitSource;
|
||||
}
|
||||
if(source==null) source=explicitSource;
|
||||
JsonNode payload=objectMapper.valueToTree(output);
|
||||
Long cached=usageNumber(payload,"/usage/prompt_tokens_details/cached_tokens");
|
||||
if(cached==null) cached=usageNumber(payload,"/usage/prompt_cache_hit_tokens");
|
||||
@@ -370,6 +398,15 @@ public class UsageAuditService {
|
||||
return value.isIntegralNumber() && value.canConvertToLong() && value.longValue()>=0 ? value.longValue() : null;
|
||||
}
|
||||
|
||||
/** Source attribution is based on the matched server route, not a spoofable client header. */
|
||||
private boolean isPlaygroundRequest(jakarta.servlet.http.HttpServletRequest request) {
|
||||
return request.getRequestURI() != null && request.getRequestURI().startsWith("/api/playground/keys/");
|
||||
}
|
||||
|
||||
private String source(ApiRequestMetadata metadata) {
|
||||
return metadata != null && metadata.source()==ApiRequestMetadata.Source.PLAYGROUND ? "playground" : "api";
|
||||
}
|
||||
|
||||
private String json(Object value) {
|
||||
if (value == null) return null;
|
||||
try {
|
||||
@@ -413,7 +450,12 @@ public class UsageAuditService {
|
||||
}
|
||||
|
||||
private String row(Object... values) { List<String> cells = new ArrayList<>(); for (Object value : values) cells.add(csv(value)); return String.join(",", cells); }
|
||||
private String csv(Object value) { String text = value == null ? "" : String.valueOf(value); return "\"" + text.replace("\"", "\"\"").replace("\r", " ").replace("\n", " ") + "\""; }
|
||||
static String csv(Object value) {
|
||||
String text = value == null ? "" : String.valueOf(value).replace("\r", " ").replace("\n", " ");
|
||||
// Prompts, outputs and key names are user-controlled; never let a spreadsheet evaluate them.
|
||||
if (text.stripLeading().matches("^[=+@\\-].*") || text.startsWith("\t")) text = "'" + text;
|
||||
return "\"" + text.replace("\"", "\"\"") + "\"";
|
||||
}
|
||||
private UUID parseUuid(String value) { try { return UUID.fromString(value); } catch (Exception exception) { throw new ApiException("INVALID_ID", "记录 ID 格式无效"); } }
|
||||
private String blankToNull(String value) { return value == null || value.isBlank() ? null : value; }
|
||||
private OffsetDateTime now() { return OffsetDateTime.now(ZoneOffset.UTC); }
|
||||
|
||||
+67
-8
@@ -3,6 +3,7 @@ package com.moyi.controlplane.wallet;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.gateway.ChatMeter;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestMetadata;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import java.math.BigDecimal;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -12,20 +13,50 @@ import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
@Service
|
||||
public class ChatBillingService {
|
||||
private static final Logger log = LoggerFactory.getLogger(ChatBillingService.class);
|
||||
private final JdbcTemplate jdbc;
|
||||
private final ObjectMapper json;
|
||||
private final UsageAuditService usage;
|
||||
private final com.moyi.controlplane.gateway.EnterprisePolicies policies;
|
||||
public ChatBillingService(JdbcTemplate jdbc, ObjectMapper json, UsageAuditService usage) { this.jdbc=jdbc; this.json=json; this.usage=usage; this.policies=new com.moyi.controlplane.gateway.EnterprisePolicies(jdbc,json); }
|
||||
private final TransactionTemplate recoveryTransactions;
|
||||
public ChatBillingService(JdbcTemplate jdbc, ObjectMapper json, UsageAuditService usage) {
|
||||
this.jdbc=jdbc; this.json=json; this.usage=usage; this.policies=new com.moyi.controlplane.gateway.EnterprisePolicies(jdbc,json);
|
||||
this.recoveryTransactions = jdbc.getDataSource() == null ? null
|
||||
: new TransactionTemplate(new DataSourceTransactionManager(jdbc.getDataSource()));
|
||||
}
|
||||
public record Ticket(UUID id, UUID userId, ChatMeter.Rates rates) {}
|
||||
public void endpoint(UUID id,String endpoint) {jdbc.update("UPDATE usage_events SET endpoint=? WHERE id=?",endpoint,id);}
|
||||
public void observedUsage(Ticket ticket,String protocol,Object observed) {
|
||||
if(observed==null) return;
|
||||
usage.observeUsage(ticket.id(),protocol,observed);
|
||||
try { usage.observeUsage(ticket.id(),protocol,observed); }
|
||||
catch (RuntimeException exception) {
|
||||
// Usage diagnostics are best-effort; canonical normalized usage is persisted before settlement.
|
||||
log.warn("Observed provider usage could not be persisted ({})", exception.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
|
||||
/** Persist a minimal, normalized meter before attempting the atomic ledger transaction. */
|
||||
@Transactional
|
||||
public void prepareSettlement(Ticket ticket, Map<String,Object> normalizedUsage, int latencyMs, Integer firstTokenMs) {
|
||||
jdbc.update("UPDATE chat_billing_requests SET settlement_usage=?::jsonb, settlement_latency_ms=?, settlement_first_token_ms=?, expires_at=GREATEST(expires_at,now()+interval '5 minutes') WHERE id=? AND status='pending'",
|
||||
encode(normalizedUsage), Math.max(0,latencyMs), firstTokenMs, ticket.id());
|
||||
}
|
||||
/**
|
||||
* Extend a still-running call's hold so expiry recovery cannot release it mid-call.
|
||||
* Only pending tickets move, and the hold never shrinks below its current expiry.
|
||||
*/
|
||||
public boolean renew(UUID id, long holdMs) {
|
||||
long seconds=Math.max(60,(holdMs+999)/1000);
|
||||
return jdbc.update("UPDATE chat_billing_requests SET expires_at=GREATEST(expires_at,now()+make_interval(secs=>?)) WHERE id=? AND status='pending'",seconds,id)>0;
|
||||
}
|
||||
|
||||
@Transactional public void actualRoute(Ticket ticket,Map<String,Object> route) {
|
||||
var snapshots=jdbc.queryForList("SELECT procurement_pricing FROM model_price_history WHERE model_id=? ORDER BY id DESC LIMIT 1",route.get("id"));
|
||||
Object cost=snapshots.isEmpty()?null:snapshots.getFirst().get("procurement_pricing");
|
||||
@@ -36,6 +67,12 @@ public class ChatBillingService {
|
||||
|
||||
@Transactional
|
||||
public Ticket reserve(Map<String,Object> key, Map<String,Object> model, ChatMeter.Request request, String idempotencyKey) {
|
||||
return reserve(key,model,request,idempotencyKey,ApiRequestMetadata.internal());
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Ticket reserve(Map<String,Object> key, Map<String,Object> model, ChatMeter.Request request,
|
||||
String idempotencyKey, ApiRequestMetadata metadata) {
|
||||
UUID userId=UUID.fromString(key.get("userId").toString());
|
||||
var versions=jdbc.queryForList("SELECT id,pricing,procurement_pricing FROM model_price_history WHERE model_id=? ORDER BY id DESC LIMIT 1",model.get("id"));
|
||||
var version=versions.isEmpty()?Map.<String,Object>of():versions.getFirst();
|
||||
@@ -58,7 +95,7 @@ public class ChatBillingService {
|
||||
if(available.compareTo(hold)<0) throw new ApiException("INSUFFICIENT_BALANCE","可用余额不足以预留本次调用费用,请充值或调低输出 Token 上限",402);
|
||||
UUID id=UUID.randomUUID();
|
||||
policies.reserve(id.toString(),userId,UUID.fromString(key.get("id").toString()),hold);
|
||||
usage.startChat(id,userId,UUID.fromString(key.get("id").toString()),model,request.body());
|
||||
usage.startChat(id,userId,UUID.fromString(key.get("id").toString()),model,request.body(),metadata);
|
||||
// Longer than the upstream hard deadline and the concurrency lease, including cleanup margin.
|
||||
jdbc.update("INSERT INTO chat_billing_requests(id,user_id,idempotency_hash,request_hash,reserved_amount,pricing,expires_at) VALUES (?,?,?,?,?,?::jsonb,now()+interval '5 minutes')",id,userId,idem,requestHash,hold,encode(rates.snapshot()));
|
||||
jdbc.update("UPDATE chat_billing_requests SET procurement_pricing=?::jsonb,price_version=? WHERE id=?",version.get("procurement_pricing")==null?null:version.get("procurement_pricing").toString(),version.get("id"),id);
|
||||
@@ -84,7 +121,7 @@ public class ChatBillingService {
|
||||
BigDecimal procurement=ChatMeter.rates(decode(call.get("procurement_pricing").toString()),false).cost(tokens);
|
||||
jdbc.update("UPDATE usage_events SET procurement_cost=?,profit_amount=?,cost_status='estimated' WHERE id=?",procurement,cost.subtract(procurement),ticket.id());
|
||||
}
|
||||
jdbc.update("UPDATE chat_billing_requests SET status='succeeded',cost_amount=? WHERE id=?",cost,ticket.id());
|
||||
jdbc.update("UPDATE chat_billing_requests SET status='succeeded',cost_amount=?,settlement_usage=NULL,settlement_latency_ms=NULL,settlement_first_token_ms=NULL WHERE id=?",cost,ticket.id());
|
||||
policies.release(ticket.id().toString());
|
||||
return cost;
|
||||
}
|
||||
@@ -96,17 +133,39 @@ public class ChatBillingService {
|
||||
var call=jdbc.queryForMap("SELECT * FROM chat_billing_requests WHERE id=? FOR UPDATE",id);
|
||||
if(!"pending".equals(call.get("status"))) return;
|
||||
jdbc.update("UPDATE user_wallets SET reserved_balance=reserved_balance-?,updated_at=now() WHERE user_id=?",call.get("reserved_amount"),call.get("user_id"));
|
||||
jdbc.update("UPDATE chat_billing_requests SET status='failed',cost_amount=0 WHERE id=?",id);
|
||||
jdbc.update("UPDATE chat_billing_requests SET status='failed',cost_amount=0,settlement_usage=NULL,settlement_latency_ms=NULL,settlement_first_token_ms=NULL WHERE id=?",id);
|
||||
usage.finishChat(id,"failed",new ChatMeter.Tokens(0,0,0),latency,null,BigDecimal.ZERO,null,code,message);
|
||||
jdbc.update("UPDATE usage_events SET cost_status='pending' WHERE id=?",id);
|
||||
policies.release(id.toString());
|
||||
}
|
||||
|
||||
@Scheduled(fixedDelayString="${moyi.billing.recovery-interval-ms:60000}")
|
||||
@Transactional
|
||||
public void recoverExpired() {
|
||||
var ids=jdbc.queryForList("SELECT id FROM chat_billing_requests WHERE status='pending' AND expires_at<now() ORDER BY expires_at LIMIT 100 FOR UPDATE SKIP LOCKED",UUID.class);
|
||||
for(UUID id:ids) release(id,"CALL_INTERRUPTED","调用中断,未取得可结算用量,预留额度已释放",0);
|
||||
var ids=jdbc.queryForList("SELECT id FROM chat_billing_requests WHERE status='pending' AND expires_at<now() ORDER BY expires_at LIMIT 100",UUID.class);
|
||||
for(UUID id:ids) {
|
||||
try {
|
||||
if (recoveryTransactions == null) recoverOne(id);
|
||||
else recoveryTransactions.executeWithoutResult(status -> recoverOne(id));
|
||||
} catch (RuntimeException exception) {
|
||||
// Keep both the pending ticket and its wallet hold. A later scheduler tick retries the durable meter.
|
||||
log.warn("Pending chat settlement recovery failed ({})", exception.getClass().getSimpleName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void recoverOne(UUID id) {
|
||||
var rows=jdbc.queryForList("SELECT user_id,settlement_usage::text AS settlement_usage,settlement_latency_ms,settlement_first_token_ms FROM chat_billing_requests WHERE id=? AND status='pending' AND expires_at<now() FOR UPDATE SKIP LOCKED",id);
|
||||
if(rows.isEmpty()) return;
|
||||
var row=rows.getFirst();
|
||||
Object usageSnapshot=row.get("settlement_usage");
|
||||
if(usageSnapshot!=null) {
|
||||
Object firstToken=row.get("settlement_first_token_ms");
|
||||
settle(new Ticket(id,(UUID)row.get("user_id"),null),decode(usageSnapshot.toString()),
|
||||
row.get("settlement_latency_ms")==null?0:((Number)row.get("settlement_latency_ms")).intValue(),
|
||||
firstToken==null?null:((Number)firstToken).intValue());
|
||||
} else {
|
||||
release(id,"CALL_INTERRUPTED","调用中断,未取得可结算用量,预留额度已释放",0);
|
||||
}
|
||||
}
|
||||
private String encode(Object value) { try { return json.writeValueAsString(value); } catch(Exception e) { throw new ApiException("INVALID_REQUEST","请求无法编码"); } }
|
||||
private Map<String,Object> decode(String value) { try { return json.readValue(value,new com.fasterxml.jackson.core.type.TypeReference<>(){}); } catch(Exception e) { throw new IllegalStateException("Invalid billing snapshot",e); } }
|
||||
|
||||
@@ -43,6 +43,7 @@ management:
|
||||
moyi:
|
||||
gateway:
|
||||
redis-prefix: ${MOYI_REDIS_KEY_PREFIX:moyi:}
|
||||
risk-mode: ${GATEWAY_RISK_MODE:shadow}
|
||||
requests-per-minute: ${GATEWAY_REQUESTS_PER_MINUTE:60}
|
||||
reads-per-minute: ${GATEWAY_READS_PER_MINUTE:300}
|
||||
# 0 disables the MOYI-side model-group cap so channel concurrency can be
|
||||
@@ -55,6 +56,12 @@ moyi:
|
||||
# provider's own response/limit.
|
||||
disable-model-concurrency: ${GATEWAY_DISABLE_MODEL_CONCURRENCY:false}
|
||||
queue-wait-ms: ${GATEWAY_QUEUE_WAIT_MS:30000}
|
||||
# Synchronous/stream upstream deadlines (seconds). Total is capped at 3600; first-byte and idle
|
||||
# default to the total and never exceed it. Running calls renew wallet holds and concurrency
|
||||
# leases, but a reverse proxy in front must also allow the longer read time.
|
||||
upstream-timeout-seconds: ${GATEWAY_UPSTREAM_TIMEOUT_SECONDS:90}
|
||||
upstream-first-byte-timeout-seconds: ${GATEWAY_UPSTREAM_FIRST_BYTE_TIMEOUT_SECONDS:${GATEWAY_UPSTREAM_TIMEOUT_SECONDS:90}}
|
||||
upstream-idle-timeout-seconds: ${GATEWAY_UPSTREAM_IDLE_TIMEOUT_SECONDS:${GATEWAY_UPSTREAM_TIMEOUT_SECONDS:90}}
|
||||
queue-poll-ms: ${GATEWAY_QUEUE_POLL_MS:100}
|
||||
tasks:
|
||||
max-queued-per-model: ${TASK_MAX_QUEUED_PER_MODEL:100}
|
||||
@@ -68,7 +75,7 @@ moyi:
|
||||
secure-cookie: ${AUTH_SECURE_COOKIE:false}
|
||||
bootstrap:
|
||||
admin-account: ${ADMIN_ACCOUNT:adimin}
|
||||
admin-password: ${ADMIN_PASSWORD:admin123}
|
||||
admin-password: ${ADMIN_PASSWORD:}
|
||||
admin-display-name: ${ADMIN_DISPLAY_NAME:平台管理员}
|
||||
credentials:
|
||||
encryption-key: ${CREDENTIAL_ENCRYPTION_KEY:}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
-- NULL snapshots identify legacy fixed-price tasks; preserve their reserved charge.
|
||||
ALTER TABLE model_tasks ADD COLUMN pricing_snapshot jsonb;
|
||||
ALTER TABLE model_tasks ADD COLUMN price_version bigint REFERENCES model_price_history(id);
|
||||
@@ -0,0 +1,22 @@
|
||||
-- Exact old-price guards preserve administrator edits; no enable-state or wallet changes.
|
||||
WITH reviewed(id,upstream_model,old_price,new_price) AS (VALUES
|
||||
('relay-route-6922f2ae-e466-4270-bf98-d952e3b6a69d', 'grok-imagine-image-quality', NULL, '{"unit":"CNY / image","price":0.2016,"discount":5.6,"discountLabel":"5.6折","exchangeRate":7.2,"standardPrice":{"price":0.05,"unit":"USD / image"},"priceSource":"APIPro 模型广场 2026-10-13,单张请求"}'::jsonb),
|
||||
('relay-route-db04c9f4-e7ee-4235-9aed-882cdf9dad47', 'gemini-3-pro-image-preview-c', NULL, '{"unit":"CNY / image","price":0.8064,"discount":5.6,"discountLabel":"5.6折","exchangeRate":7.2,"standardPrice":{"price":0.2,"unit":"USD / image"},"priceSource":"APIPro 模型广场 2026-10-13,单张请求"}'::jsonb),
|
||||
('relay-route-6cf83465-37e8-4368-a08c-16a685cf3670', 'gpt-image-2-c', NULL, '{"unit":"CNY / image","price":0.4032,"discount":5.6,"discountLabel":"5.6折","exchangeRate":7.2,"standardPrice":{"price":0.1,"unit":"USD / image"},"priceSource":"APIPro 模型广场 2026-10-13,单张请求"}'::jsonb),
|
||||
('relay-route-f5455597-e007-416a-8341-420deb6f2ae8', 'gpt-image-2-pro', NULL, '{"unit":"CNY / image","price":0.8064,"discount":5.6,"discountLabel":"5.6折","exchangeRate":7.2,"standardPrice":{"price":0.2,"unit":"USD / image"},"priceSource":"APIPro 模型广场 2026-10-13,单张请求"}'::jsonb),
|
||||
('relay-route-d8589945-6521-452c-9242-052a7a89aac8', 'gemini-2.5-flash-image-c', NULL, '{"unit":"CNY / image","price":0.16128,"discount":5.6,"discountLabel":"5.6折","exchangeRate":7.2,"standardPrice":{"price":0.04,"unit":"USD / image"},"priceSource":"APIPro 模型广场 2026-10-13,单张请求"}'::jsonb),
|
||||
('relay-route-062b0509-5a12-4e60-8257-d6287c9b57ec', 'gemini-3.1-flash-image-preview-c', NULL, '{"unit":"CNY / image","price":0.48384,"discount":5.6,"discountLabel":"5.6折","exchangeRate":7.2,"standardPrice":{"price":0.12,"unit":"USD / image"},"priceSource":"APIPro 模型广场 2026-10-13,单张请求"}'::jsonb),
|
||||
('relay-route-288a8bca-9560-432e-924b-0bd756208d24', 'qwen-image-2.0-pro', NULL, '{"unit":"CNY / image","price":0.3342924,"discount":6.5,"discountLabel":"6.5折","exchangeRate":7.2,"standardPrice":{"price":0.07143,"unit":"USD / image"},"priceSource":"APIPro 模型广场 2026-10-13,单张请求"}'::jsonb),
|
||||
('relay-route-26d35841-51ef-4c99-96d0-e2cff9b773d1', 'qwen-image-max', NULL, '{"unit":"CNY / image","price":0.3342924,"discount":6.5,"discountLabel":"6.5折","exchangeRate":7.2,"standardPrice":{"price":0.07143,"unit":"USD / image"},"priceSource":"APIPro 模型广场 2026-10-13,单张请求"}'::jsonb),
|
||||
('relay-route-d4a9d44c-b5a2-407f-92d4-5ae50067b87a', 'gemini-2.5-flash-image', '{"unit":"CNY / 1M tokens","input":1.2096,"output":120.96,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.3,"output":30},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":1.2096,"output":120.96,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.3,"output":30},"discountLabel":"5.6折","taskBilling":"gemini-image-tokens","usageSource":"provider-reported-only"}'::jsonb),
|
||||
('relay-route-2f0c9dfe-07a2-4751-a08d-6cdfb6b96d34', 'gemini-3-pro-image-preview', '{"unit":"CNY / 1M tokens","input":8.064,"output":483.84,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":2,"output":120},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":8.064,"output":483.84,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":2,"output":120},"discountLabel":"5.6折","taskBilling":"gemini-image-tokens","usageSource":"provider-reported-only"}'::jsonb),
|
||||
('relay-route-8ccecb3f-595f-4bb0-9147-5548303a6cda', 'gemini-3.1-flash-image', '{"unit":"CNY / 1M tokens","input":2.016,"output":241.92,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.5,"output":60},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":2.016,"output":241.92,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.5,"output":60},"discountLabel":"5.6折","taskBilling":"gemini-image-tokens","usageSource":"provider-reported-only"}'::jsonb),
|
||||
('relay-route-916cd7b1-cc65-40ec-bc40-541b2bd0c33d', 'gemini-3.1-flash-image-preview', '{"unit":"CNY / 1M tokens","input":2.016,"output":241.92,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.5,"output":60},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":2.016,"output":241.92,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.5,"output":60},"discountLabel":"5.6折","taskBilling":"gemini-image-tokens","usageSource":"provider-reported-only"}'::jsonb),
|
||||
('relay-route-5e15dbed-0d9e-44df-a491-d121c9bdd635', 'gemini-3.1-flash-lite-image', '{"unit":"CNY / 1M tokens","input":1.008,"output":120.96,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.25,"output":30},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":1.008,"output":120.96,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":0.25,"output":30},"discountLabel":"5.6折","taskBilling":"gemini-image-tokens","usageSource":"provider-reported-only"}'::jsonb),
|
||||
('relay-route-4886de15-b83c-4616-a74f-dfe52e0c99ba', 'gpt-image-2', '{"unit":"CNY / 1M tokens","input":20.16,"output":120.96,"discount":5.6,"cachedInput":8.064,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":5,"output":30,"cachedInput":2},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":20.16,"output":120.96,"discount":5.6,"cachedInput":8.064,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":5,"output":30,"cachedInput":2,"imageInput":8.0},"discountLabel":"5.6折","taskBilling":"openai-image-tokens","imageInput":32.256,"usageSource":"provider-reported-only"}'::jsonb),
|
||||
('relay-route-12d63ec3-e2e5-4a5d-98c9-ac1f7628c4b2', 'gpt-image-2.5-flare', '{"unit":"CNY / 1M tokens","input":20.16,"output":120.96,"discount":5.6,"cachedInput":8.064,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":5,"output":30,"cachedInput":2},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":20.16,"output":120.96,"discount":5.6,"cachedInput":8.064,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":5,"output":30,"cachedInput":2,"imageInput":8.0},"discountLabel":"5.6折","taskBilling":"openai-image-tokens","imageInput":32.256,"usageSource":"provider-reported-only"}'::jsonb),
|
||||
('relay-route-30eacdea-b609-47f5-80f9-0140154ddf45', 'gpt-image-2.5-sunburst', '{"unit":"CNY / 1M tokens","input":20.16,"output":120.96,"discount":5.6,"cachedInput":8.064,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":5,"output":30,"cachedInput":2},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":20.16,"output":120.96,"discount":5.6,"cachedInput":8.064,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":5,"output":30,"cachedInput":2,"imageInput":8.0},"discountLabel":"5.6折","taskBilling":"openai-image-tokens","imageInput":32.256,"usageSource":"provider-reported-only"}'::jsonb),
|
||||
('relay-route-106ba3ca-c6a4-4d24-9f45-44fc6158b212', 'gemini-3-pro-image', '{"unit":"CNY / 1M tokens","input":8.064,"output":483.84,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":2,"output":120},"discountLabel":"5.6折"}'::jsonb, '{"unit":"CNY / 1M tokens","input":8.064,"output":483.84,"discount":5.6,"priceSource":"APIPro 模型广场","exchangeRate":7.2,"listPriceUsd":{"input":2,"output":120},"discountLabel":"5.6折","taskBilling":"gemini-image-tokens","usageSource":"provider-reported-only"}'::jsonb))
|
||||
UPDATE model_catalog m SET pricing=r.new_price,updated_at=now() FROM reviewed r,model_providers p
|
||||
WHERE m.id=r.id AND m.upstream_model=r.upstream_model AND m.provider_id=p.id AND p.code='apipro'
|
||||
AND m.deleted_at IS NULL AND m.model_type='image' AND m.call_mode='async' AND m.pricing IS NOT DISTINCT FROM r.old_price;
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
-- User confirmed on 2026-10-13: these unpriced families are outside current onboarding.
|
||||
-- Keep catalog rows, grants and enable-state intact; admission checks the explicit flag.
|
||||
UPDATE model_catalog m
|
||||
SET capabilities=coalesce(m.capabilities,'{}'::jsonb)||'{"integrationDeferred":true}'::jsonb,updated_at=now()
|
||||
FROM model_providers p
|
||||
WHERE m.provider_id=p.id AND p.code='apipro' AND m.deleted_at IS NULL AND m.pricing IS NULL
|
||||
AND (lower(m.upstream_model) ~ '^(sparkdesk|step[-/]|stepfun/|mimo-|mj_|midjourney|suno_|veo-|lyria-|wanx|wan[0-9])'
|
||||
OR m.upstream_model='swap_face');
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
-- Persist only the normalized meter needed to retry settlement after a transient ledger failure.
|
||||
-- Prompts, generated content and provider credentials are deliberately excluded.
|
||||
ALTER TABLE chat_billing_requests ADD COLUMN settlement_usage jsonb;
|
||||
ALTER TABLE chat_billing_requests ADD COLUMN settlement_latency_ms integer;
|
||||
ALTER TABLE chat_billing_requests ADD COLUMN settlement_first_token_ms integer;
|
||||
@@ -0,0 +1,9 @@
|
||||
-- User-scoped durable identities survive completion; raw Idempotency-Key values are never persisted.
|
||||
ALTER TABLE model_tasks ADD COLUMN idempotency_hash varchar(64);
|
||||
ALTER TABLE model_tasks ADD COLUMN request_hash varchar(64);
|
||||
ALTER TABLE model_tasks ADD CONSTRAINT model_tasks_request_identity_check
|
||||
CHECK ((idempotency_hash IS NULL AND request_hash IS NULL)
|
||||
OR (idempotency_hash ~ '^[0-9a-f]{64}$' AND request_hash ~ '^[0-9a-f]{64}$'
|
||||
AND idempotency_hash IS NOT NULL AND request_hash IS NOT NULL));
|
||||
CREATE UNIQUE INDEX model_tasks_user_idempotency_idx ON model_tasks(user_id, idempotency_hash)
|
||||
WHERE idempotency_hash IS NOT NULL;
|
||||
@@ -0,0 +1,16 @@
|
||||
-- Unknown provider acceptance is not a failed generation and must retain its financial holds.
|
||||
ALTER TABLE model_tasks DROP CONSTRAINT model_tasks_status_check;
|
||||
ALTER TABLE model_tasks ADD CONSTRAINT model_tasks_status_check
|
||||
CHECK (status IN ('queued','running','reconciling','succeeded','failed','cancelled'));
|
||||
ALTER TABLE usage_events DROP CONSTRAINT usage_events_status_check;
|
||||
ALTER TABLE usage_events ADD CONSTRAINT usage_events_status_check
|
||||
CHECK (status IN ('queued','running','reconciling','succeeded','failed','cancelled'));
|
||||
ALTER TABLE model_tasks ADD COLUMN reconciliation_required_at timestamptz;
|
||||
-- A normalized observed terminal outcome survives a crash or transient ledger/audit write failure.
|
||||
-- The result itself uses the existing result column; no second copy of generated media is stored.
|
||||
ALTER TABLE model_tasks ADD COLUMN completion_status varchar(16)
|
||||
CHECK (completion_status IN ('succeeded','failed','cancelled'));
|
||||
CREATE INDEX model_tasks_pending_completion_idx ON model_tasks(updated_at)
|
||||
WHERE completion_status IS NOT NULL AND status IN ('running','reconciling');
|
||||
CREATE INDEX model_tasks_reconciliation_idx ON model_tasks(reconciliation_required_at)
|
||||
WHERE status='reconciling';
|
||||
@@ -0,0 +1,23 @@
|
||||
-- Freeze route identity without copying credentials or mutable provider configuration.
|
||||
ALTER TABLE model_tasks ADD COLUMN provider_id_snapshot varchar(64) REFERENCES model_providers(id);
|
||||
ALTER TABLE model_tasks ADD COLUMN upstream_model_snapshot varchar(160);
|
||||
ALTER TABLE model_tasks ADD CONSTRAINT model_tasks_execution_binding_check CHECK (
|
||||
(provider_id_snapshot IS NULL AND upstream_model_snapshot IS NULL)
|
||||
OR (provider_id_snapshot IS NOT NULL AND upstream_model_snapshot IS NOT NULL
|
||||
AND length(trim(provider_id_snapshot)) > 0 AND length(trim(upstream_model_snapshot)) > 0)
|
||||
);
|
||||
|
||||
-- Never guess the original route of an already-submitted legacy task.
|
||||
-- Unsubmitted queued work can safely freeze its route at migration time.
|
||||
UPDATE model_tasks t SET provider_id_snapshot=m.provider_id, upstream_model_snapshot=m.upstream_model
|
||||
FROM model_catalog m
|
||||
WHERE t.model_id=m.id AND t.status='queued' AND t.provider_task_id IS NULL;
|
||||
|
||||
COMMENT ON COLUMN model_tasks.provider_id_snapshot IS 'Provider identity at admission; NULL for ambiguous legacy accepted tasks';
|
||||
|
||||
-- New proven bindings cannot associate the same provider/account task with two local wallets.
|
||||
-- Legacy accepted rows are deliberately not guessed/backfilled, so this index does not rewrite their history.
|
||||
CREATE UNIQUE INDEX model_tasks_proven_upstream_identity_idx
|
||||
ON model_tasks(provider_id_snapshot,provider_key_fingerprint,provider_task_id)
|
||||
WHERE provider_id_snapshot IS NOT NULL AND provider_key_fingerprint IS NOT NULL
|
||||
AND provider_task_id IS NOT NULL AND provider_task_id <> 'mock';
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
-- Append-only operation receipts; no prompts, generated media or raw credentials.
|
||||
CREATE TABLE task_reconciliations (
|
||||
request_id uuid PRIMARY KEY,
|
||||
task_id varchar(160) NOT NULL REFERENCES model_tasks(id),
|
||||
actor_user_id uuid NOT NULL REFERENCES users(id),
|
||||
action varchar(24) NOT NULL CHECK (action IN ('resume_polling','confirm_failed','confirm_succeeded')),
|
||||
request_hash varchar(64) NOT NULL CHECK (request_hash ~ '^[0-9a-f]{64}$'),
|
||||
evidence_reference varchar(500) NOT NULL CHECK (length(trim(evidence_reference)) BETWEEN 3 AND 500),
|
||||
note varchar(2000) NOT NULL CHECK (length(trim(note)) BETWEEN 10 AND 2000),
|
||||
before_state jsonb NOT NULL,
|
||||
after_state jsonb NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
CREATE INDEX task_reconciliations_task_idx ON task_reconciliations(task_id,created_at DESC);
|
||||
CREATE FUNCTION protect_task_reconciliation_history() RETURNS trigger LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
RAISE EXCEPTION 'Task reconciliation history is append-only';
|
||||
END $$;
|
||||
CREATE TRIGGER task_reconciliation_history_immutable BEFORE UPDATE OR DELETE ON task_reconciliations
|
||||
FOR EACH ROW EXECUTE FUNCTION protect_task_reconciliation_history();
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
-- Shared across replicas. Raw accounts, IPs and passwords are never written here.
|
||||
CREATE TABLE auth_login_windows (
|
||||
bucket_hash varchar(64) PRIMARY KEY CHECK (bucket_hash ~ '^[0-9a-f]{64}$'),
|
||||
window_started_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
expires_at timestamptz NOT NULL DEFAULT (clock_timestamp()+interval '60 seconds'),
|
||||
attempts integer NOT NULL CHECK (attempts BETWEEN 1 AND 1000)
|
||||
);
|
||||
CREATE INDEX auth_login_windows_expiry_idx ON auth_login_windows(expires_at);
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
-- One immutable generation per delivery. No plaintext recipient or OTP is stored in this queue.
|
||||
ALTER TABLE registration_codes ADD COLUMN delivery_id uuid;
|
||||
ALTER TABLE password_reset_codes ADD COLUMN delivery_id uuid;
|
||||
CREATE INDEX registration_codes_expiry_idx ON registration_codes(expires_at);
|
||||
|
||||
CREATE TABLE verification_delivery_outbox (
|
||||
id uuid PRIMARY KEY,
|
||||
purpose varchar(24) NOT NULL CHECK (purpose IN ('registration','password-reset')),
|
||||
payload_ciphertext text,
|
||||
status varchar(16) NOT NULL DEFAULT 'queued' CHECK (status IN ('queued','sending','sent','failed','discarded')),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
expires_at timestamptz NOT NULL DEFAULT clock_timestamp()+interval '5 minutes',
|
||||
started_at timestamptz,
|
||||
finished_at timestamptz,
|
||||
failure_code varchar(32),
|
||||
CHECK ((status IN ('queued','sending') AND payload_ciphertext IS NOT NULL)
|
||||
OR (status IN ('sent','failed','discarded') AND payload_ciphertext IS NULL))
|
||||
);
|
||||
CREATE INDEX verification_delivery_queue_idx ON verification_delivery_outbox(created_at,id) WHERE status='queued';
|
||||
CREATE INDEX verification_delivery_expiry_idx ON verification_delivery_outbox(expires_at) WHERE status IN ('queued','sending');
|
||||
CREATE INDEX verification_delivery_finished_idx ON verification_delivery_outbox(finished_at) WHERE status IN ('sent','failed','discarded');
|
||||
@@ -1,19 +1,40 @@
|
||||
-- KEYS: model slots, key slots, model waiting (score = wait expiry), model arrival order (score = first arrival).
|
||||
-- ARGV: model limit, key limit, slot ttl, token, remaining wait ms, waiting ttl.
|
||||
-- Returns 1 admitted, 0 model busy or behind earlier waiters, 2 own API Key busy.
|
||||
local time = redis.call('TIME')
|
||||
local now = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2]) / 1000)
|
||||
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', now)
|
||||
redis.call('ZREMRANGEBYSCORE', KEYS[2], '-inf', now)
|
||||
-- Expired waiters (crashed or timed-out callers) must also leave the arrival order.
|
||||
local expired = redis.call('ZRANGEBYSCORE', KEYS[3], '-inf', now)
|
||||
for _, member in ipairs(expired) do redis.call('ZREM', KEYS[4], member) end
|
||||
redis.call('ZREMRANGEBYSCORE', KEYS[3], '-inf', now)
|
||||
local busy = nil
|
||||
if redis.call('ZCARD', KEYS[1]) >= tonumber(ARGV[1]) then busy = 0
|
||||
elseif redis.call('ZCARD', KEYS[2]) >= tonumber(ARGV[2]) then busy = 2 end
|
||||
if busy ~= nil then
|
||||
if tonumber(ARGV[5]) > 0 then
|
||||
redis.call('ZADD', KEYS[3], now + tonumber(ARGV[5]), ARGV[4])
|
||||
local wait = tonumber(ARGV[5])
|
||||
local function remember_waiting()
|
||||
if wait > 0 then
|
||||
redis.call('ZADD', KEYS[3], now + wait, ARGV[4])
|
||||
redis.call('PEXPIRE', KEYS[3], ARGV[6])
|
||||
end
|
||||
return busy
|
||||
end
|
||||
-- A waiter blocked only by its own API Key limit does not hold a place ahead of other keys.
|
||||
if redis.call('ZCARD', KEYS[2]) >= tonumber(ARGV[2]) then
|
||||
redis.call('ZREM', KEYS[4], ARGV[4])
|
||||
remember_waiting()
|
||||
return 2
|
||||
end
|
||||
local ahead = redis.call('ZRANK', KEYS[4], ARGV[4])
|
||||
if not ahead then ahead = redis.call('ZCARD', KEYS[4]) end
|
||||
-- FIFO: a free model slot belongs to earlier model waiters before a newer request.
|
||||
if redis.call('ZCARD', KEYS[1]) + ahead >= tonumber(ARGV[1]) then
|
||||
if wait > 0 then
|
||||
redis.call('ZADD', KEYS[4], 'NX', now, ARGV[4])
|
||||
redis.call('PEXPIRE', KEYS[4], ARGV[6])
|
||||
end
|
||||
remember_waiting()
|
||||
return 0
|
||||
end
|
||||
redis.call('ZREM', KEYS[3], ARGV[4])
|
||||
redis.call('ZREM', KEYS[4], ARGV[4])
|
||||
redis.call('ZADD', KEYS[1], now + tonumber(ARGV[3]), ARGV[4])
|
||||
redis.call('ZADD', KEYS[2], now + tonumber(ARGV[3]), ARGV[4])
|
||||
redis.call('PEXPIRE', KEYS[1], ARGV[3])
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
-- A resumed task renews its own lease instead of consuming a second slot.
|
||||
local time = redis.call('TIME')
|
||||
local now = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2]) / 1000)
|
||||
redis.call('ZREMRANGEBYSCORE', KEYS[1], '-inf', now)
|
||||
local existing = redis.call('ZSCORE', KEYS[1], ARGV[3])
|
||||
if not existing and redis.call('ZCARD', KEYS[1]) >= tonumber(ARGV[1]) then
|
||||
return 0
|
||||
end
|
||||
redis.call('ZADD', KEYS[1], now + tonumber(ARGV[2]), ARGV[3])
|
||||
if redis.call('PTTL', KEYS[1]) < tonumber(ARGV[2]) then
|
||||
redis.call('PEXPIRE', KEYS[1], ARGV[2])
|
||||
end
|
||||
if existing then return 2 end
|
||||
return 1
|
||||
+105
-7
@@ -54,6 +54,7 @@ class PlatformAdminPostgresTest {
|
||||
private CatalogService catalog;
|
||||
private WalletService wallet;
|
||||
private VerificationService verification;
|
||||
private VerificationDispatchService dispatch;
|
||||
private TestSender sender;
|
||||
|
||||
@BeforeEach void setup() throws Exception {
|
||||
@@ -87,12 +88,14 @@ class PlatformAdminPostgresTest {
|
||||
context.registerBean(WalletService.class, () -> new WalletService(jdbc, context.getBean(AuthService.class)));
|
||||
context.registerBean(UsageAuditService.class, () -> new UsageAuditService(jdbc,json));
|
||||
context.registerBean(ChatBillingService.class, () -> new ChatBillingService(jdbc,json,context.getBean(UsageAuditService.class)));
|
||||
context.registerBean(VerificationService.class, () -> new VerificationService(jdbc, sender, "test-pepper"));
|
||||
context.registerBean(VerificationDispatchService.class, () -> new VerificationDispatchService(jdbc,sender,new SecretCipher("test-delivery-encryption"),json,context.getBean(DataSourceTransactionManager.class)));
|
||||
context.registerBean(VerificationService.class, () -> new VerificationService(jdbc, context.getBean(VerificationDispatchService.class), "test-pepper"));
|
||||
context.refresh();
|
||||
auth = context.getBean(AuthService.class);
|
||||
catalog = context.getBean(CatalogService.class);
|
||||
wallet = context.getBean(WalletService.class);
|
||||
verification = context.getBean(VerificationService.class);
|
||||
dispatch = context.getBean(VerificationDispatchService.class);
|
||||
auth.ensureAdmin("test_owner", "local-password", "Test Owner");
|
||||
ownerToken = auth.login("test_owner", "local-password", meta).token();
|
||||
memberId = auth.createUser(ownerToken, "test_member", "local-password", "Test Member", meta).id();
|
||||
@@ -122,10 +125,16 @@ class PlatformAdminPostgresTest {
|
||||
assertFalse(catalog.keyCanInvoke(key,secondId));
|
||||
assertFalse(catalog.keyCanInvoke(key,modelId));
|
||||
assertThrows(ApiException.class, () -> catalog.updateKeyModels(ownerToken,keyId,List.of(secondId),auth));
|
||||
var edited = catalog.updateProvider(channelId,Map.of("name","Relay Updated","baseUrl","https://example.test/v2","credentialSecret",""),ownerToken,auth);
|
||||
// The stored key must never follow a changed endpoint without being re-entered.
|
||||
assertEquals("CREDENTIAL_REENTRY_REQUIRED", assertThrows(ApiException.class, () -> catalog.updateProvider(channelId,Map.of("baseUrl","https://attacker.test/v1","credentialSecret",""),ownerToken,auth)).getCode());
|
||||
assertEquals("https://example.test/v1",jdbc.queryForObject("SELECT base_url FROM model_providers WHERE id=?",String.class,channelId));
|
||||
var edited = catalog.updateProvider(channelId,Map.of("name","Relay Updated","baseUrl","https://example.test/v1/","credentialSecret",""),ownerToken,auth);
|
||||
assertEquals("Relay Updated",edited.get("name"));
|
||||
assertFalse(edited.containsKey("credentialSecret"));
|
||||
assertEquals(cipher,jdbc.queryForObject("SELECT credential_ciphertext FROM model_providers WHERE id=?",String.class,channelId));
|
||||
catalog.updateProvider(channelId,Map.of("baseUrl","https://example.test/v2","credentialSecret","replacement-test-secret"),ownerToken,auth);
|
||||
assertNotEquals(cipher,jdbc.queryForObject("SELECT credential_ciphertext FROM model_providers WHERE id=?",String.class,channelId));
|
||||
assertTrue(jdbc.queryForObject("SELECT string_agg(details::text,' ') FROM auth_audit_log WHERE event_type='provider.updated'",String.class).contains("previousBaseUrl"));
|
||||
catalog.saveDefinition(modelId,Map.of("publicName","Renamed"),ownerToken,auth);
|
||||
assertEquals("Renamed",catalog.activeModel(firstId).get("publicName"));
|
||||
catalog.updateModel(firstId,Map.of("sourceLabel","Edited Group","upstreamModel","upstream-c","maxConcurrency",2),ownerToken,auth);
|
||||
@@ -157,6 +166,10 @@ class PlatformAdminPostgresTest {
|
||||
var cookie = new Cookie("moyi_session",memberToken);
|
||||
var response = mvc.perform(get("/api/models").cookie(cookie)).andExpect(status().isOk()).andReturn();
|
||||
var payload = json.readTree(response.getResponse().getContentAsString());
|
||||
// Members see channel identity only, never endpoints or credential hints.
|
||||
assertTrue(payload.get("providers").size() > 0);
|
||||
assertTrue(payload.get("providers").findValues("baseUrl").isEmpty());
|
||||
assertTrue(payload.get("providers").findValues("credentialMasked").isEmpty());
|
||||
assertEquals(catalog.listDefinitions(false).size(),payload.get("logicalModels").size());
|
||||
assertTrue(payload.get("logicalModels").size() > 250);
|
||||
assertTrue(payload.get("logicalModels").findValuesAsText("id").contains(pending.get("id")));
|
||||
@@ -308,7 +321,7 @@ class PlatformAdminPostgresTest {
|
||||
} finally { org.springframework.web.context.request.RequestContextHolder.resetRequestAttributes(); }
|
||||
var saved=jdbc.queryForMap("SELECT * FROM usage_events WHERE user_id=?",memberId);
|
||||
assertEquals("/v1/chat/completions",saved.get("endpoint"));
|
||||
assertEquals("playground",saved.get("request_source"));
|
||||
assertEquals("api",saved.get("request_source"),"a caller-supplied source header must not reclassify a public API request");
|
||||
assertEquals("Local test browser",saved.get("user_agent"));
|
||||
assertEquals(80L,saved.get("cached_input_units"));
|
||||
assertEquals(5L,saved.get("cache_write_units"));
|
||||
@@ -323,7 +336,8 @@ class PlatformAdminPostgresTest {
|
||||
}
|
||||
|
||||
@Test void directCreationCannotEscalateAndOtpIsRequiredOnBothChannels() throws Exception {
|
||||
MockMvc http = MockMvcBuilders.standaloneSetup(new AuthController(auth,verification,168,false)).setControllerAdvice(new ApiExceptionHandler()).build();
|
||||
var verifiedAccounts = new VerifiedAccountService(auth, verification, context.getBean(DataSourceTransactionManager.class));
|
||||
MockMvc http = MockMvcBuilders.standaloneSetup(new AuthController(auth,verification,verifiedAccounts,168,false)).setControllerAdvice(new ApiExceptionHandler()).build();
|
||||
http.perform(post("/api/admin/users").cookie(new Cookie("moyi_session",ownerToken)).contentType("application/json").content("{\"account\":\"created_user\",\"password\":\"test-password\",\"role\":\"super_admin\"}"))
|
||||
.andExpect(status().isCreated()).andExpect(jsonPath("$.user.role").value("user")).andExpect(jsonPath("$.user.password").doesNotExist());
|
||||
http.perform(post("/api/admin/users").cookie(new Cookie("moyi_session",memberToken)).contentType("application/json").content("{\"account\":\"bad_user\",\"password\":\"test-password\"}"))
|
||||
@@ -334,6 +348,7 @@ class PlatformAdminPostgresTest {
|
||||
http.perform(post("/api/auth/register").contentType("application/json").content(json.writeValueAsBytes(input))).andExpect(status().isBadRequest());
|
||||
http.perform(post("/api/auth/verification").contentType("application/json").content(json.writeValueAsBytes(input)))
|
||||
.andExpect(status().isOk()).andExpect(jsonPath("$.code").doesNotExist()).andExpect(jsonPath("$.sent").value(true));
|
||||
assertTrue(VerificationDeliveryFixture.deliverOne(dispatch));
|
||||
String code = sender.codes.get(account);
|
||||
assertNotEquals(code,jdbc.queryForObject("SELECT code_hash FROM registration_codes WHERE account=?",String.class,account));
|
||||
input.put("verificationCode",code);
|
||||
@@ -349,16 +364,19 @@ class PlatformAdminPostgresTest {
|
||||
assertThrows(ApiException.class, () -> verification.send(account,"bad-invite","127.0.0.1"));
|
||||
verification.send(account,invite,"127.0.0.1");
|
||||
assertThrows(ApiException.class, () -> verification.send(account,invite,"127.0.0.1"));
|
||||
assertTrue(VerificationDeliveryFixture.deliverOne(dispatch));
|
||||
String correct=sender.codes.get(account);
|
||||
String wrong=correct.equals("000000")?"111111":"000000";
|
||||
for(int i=0;i<5;i++) assertThrows(ApiException.class, () -> verification.consume(account,wrong));
|
||||
assertEquals(5,jdbc.queryForObject("SELECT attempts FROM registration_codes WHERE account=?",Integer.class,account));
|
||||
assertThrows(ApiException.class, () -> verification.consume(account,correct));
|
||||
verification.send("expired@example.test",invite,"127.0.0.1");
|
||||
assertTrue(VerificationDeliveryFixture.deliverOne(dispatch));
|
||||
jdbc.update("UPDATE registration_codes SET expires_at=now()-interval '1 second' WHERE account='expired@example.test'");
|
||||
assertThrows(ApiException.class, () -> verification.consume("expired@example.test",sender.codes.get("expired@example.test")));
|
||||
sender.fail=true;
|
||||
assertThrows(ApiException.class, () -> verification.send("failed@example.test",invite,"127.0.0.1"));
|
||||
assertEquals(true,verification.send("failed@example.test",invite,"127.0.0.1").get("sent"));
|
||||
assertTrue(VerificationDeliveryFixture.deliverOne(dispatch));
|
||||
assertTrue(jdbc.queryForObject("SELECT consumed FROM registration_codes WHERE account='failed@example.test'",Boolean.class));
|
||||
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM verification_send_events WHERE account='failed@example.test'",Integer.class));
|
||||
sender.enabled=false;
|
||||
@@ -432,6 +450,22 @@ class PlatformAdminPostgresTest {
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE operation='usage'",Integer.class));
|
||||
assertEquals(2,jdbc.queryForObject("SELECT count(*) FROM usage_events WHERE status='failed' AND cost_amount=0",Integer.class));
|
||||
}
|
||||
@Test void renewedRunningCallIsNotReleasedByExpiryRecovery() {
|
||||
var f=chatFixture();
|
||||
wallet.grant(ownerToken,memberId,UUID.randomUUID(),"1","renewal test",meta);
|
||||
var running=f.reserve(null);
|
||||
jdbc.update("UPDATE chat_billing_requests SET expires_at=now()+interval '5 seconds' WHERE id=?",running.id());
|
||||
assertTrue(f.billing().renew(running.id(),1_800_000));
|
||||
assertTrue(jdbc.queryForObject("SELECT expires_at>now()+interval '29 minutes' FROM chat_billing_requests WHERE id=?",Boolean.class,running.id()));
|
||||
// Renewal never shortens an existing hold.
|
||||
assertTrue(f.billing().renew(running.id(),60_000));
|
||||
assertTrue(jdbc.queryForObject("SELECT expires_at>now()+interval '29 minutes' FROM chat_billing_requests WHERE id=?",Boolean.class,running.id()));
|
||||
f.billing().recoverExpired();
|
||||
assertEquals("pending",jdbc.queryForObject("SELECT status FROM chat_billing_requests WHERE id=?",String.class,running.id()));
|
||||
assertTrue(jdbc.queryForObject("SELECT reserved_balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId).signum()>0);
|
||||
f.billing().fail(running,"TEST_DONE","done",1);
|
||||
assertFalse(f.billing().renew(running.id(),1_800_000));
|
||||
}
|
||||
@Test void unexpectedProviderOverrunIsRecordedAsDebtAndBlocksNewCalls() {
|
||||
var f=chatFixture();
|
||||
wallet.grant(ownerToken,memberId,UUID.randomUUID(),"1","billing test",meta);
|
||||
@@ -482,7 +516,13 @@ class PlatformAdminPostgresTest {
|
||||
.withProperty("moyi.providers.deepseek.base-url","http://127.0.0.1:"+upstream.getAddress().getPort()+"/v1");
|
||||
var provider=new com.moyi.controlplane.provider.OpenAiCompatibleProvider(json,environment,jdbc,new SecretCipher("local-test"));
|
||||
try {
|
||||
var controller=new com.moyi.controlplane.gateway.GatewayController(catalog,provider,org.mockito.Mockito.mock(com.moyi.controlplane.gateway.GatewayGuard.class),f.billing(),json);
|
||||
var guard=org.mockito.Mockito.mock(com.moyi.controlplane.gateway.GatewayGuard.class);
|
||||
org.mockito.Mockito.doReturn(org.mockito.Mockito.mock(com.moyi.controlplane.gateway.GatewayGuard.Admission.class))
|
||||
.when(guard).acquire(org.mockito.ArgumentMatchers.anyMap(),org.mockito.ArgumentMatchers.any(java.util.UUID.class),org.mockito.ArgumentMatchers.anyLong());
|
||||
var requestExecution=new com.moyi.controlplane.gateway.request.GatewayRequestExecutionService(
|
||||
f.billing(),new com.moyi.controlplane.gateway.request.ApiRequestRiskPolicy(
|
||||
com.moyi.controlplane.gateway.request.ApiRequestRiskEngine.allowAll()));
|
||||
var controller=new com.moyi.controlplane.gateway.GatewayController(catalog,provider,guard,requestExecution,json);
|
||||
var http=MockMvcBuilders.standaloneSetup(controller).setControllerAdvice(new ApiExceptionHandler()).build();
|
||||
var input=new LinkedHashMap<>(f.request().body());
|
||||
http.perform(post("/v1/chat/completions").header("Authorization","Bearer "+key).header("Idempotency-Key","retry-test").contentType("application/json").content(json.writeValueAsBytes(input)))
|
||||
@@ -494,7 +534,7 @@ class PlatformAdminPostgresTest {
|
||||
.andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith("text/event-stream")).andReturn().getResponse().getContentAsString();
|
||||
assertTrue(streamed.endsWith("data: [DONE]\n\n")); assertFalse(streamed.contains("private-alias"));
|
||||
assertEquals(new BigDecimal("0.99932000"),jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId));
|
||||
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM usage_events WHERE call_mode='stream' AND request_source='playground' AND cost_amount=0.00034 AND first_token_ms IS NOT NULL",Integer.class));
|
||||
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM usage_events WHERE call_mode='stream' AND request_source='api' AND cost_amount=0.00034 AND first_token_ms IS NOT NULL",Integer.class));
|
||||
mode.set("no-usage");
|
||||
var failed=http.perform(post("/v1/chat/completions").header("Authorization","Bearer "+key).contentType("application/json").content(json.writeValueAsBytes(input))).andReturn().getResponse().getContentAsString();
|
||||
assertTrue(failed.contains("UPSTREAM_USAGE_MISSING"));
|
||||
@@ -530,4 +570,62 @@ class PlatformAdminPostgresTest {
|
||||
f.billing().fail(ticket,"SETTLEMENT_FAILED","Test rollback",5);
|
||||
assertEquals(0,jdbc.queryForObject("SELECT reserved_balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId).signum());
|
||||
}
|
||||
|
||||
@Test void explicitRequestMetadataPersistsWithoutThreadLocalHttpContext() {
|
||||
var f=chatFixture(); wallet.grant(ownerToken,memberId,UUID.randomUUID(),"1","metadata test",meta);
|
||||
var metadata=com.moyi.controlplane.gateway.request.ApiRequestMetadata.playground("198.51.100.7");
|
||||
var ticket=f.billing().reserve(f.key(),f.model(),f.request(),"metadata",metadata);
|
||||
var saved=jdbc.queryForMap("SELECT request_source,host(source_ip) AS client_ip FROM usage_events WHERE id=?",ticket.id());
|
||||
assertEquals("playground",saved.get("request_source"));
|
||||
assertEquals("198.51.100.7",saved.get("client_ip"));
|
||||
f.billing().fail(ticket,"TEST_COMPLETE","metadata fixture",1);
|
||||
|
||||
var audit=context.getBean(UsageAuditService.class);
|
||||
audit.recordTask(memberId,UUID.fromString(String.valueOf(f.key().get("id"))),String.valueOf(f.model().get("id")),
|
||||
String.valueOf(f.model().get("providerId")),"metadata-task","queued",Map.of(),null,null,null,
|
||||
com.moyi.controlplane.gateway.request.ApiRequestMetadata.fromHttp("203.0.113.8"));
|
||||
saved=jdbc.queryForMap("SELECT request_source,host(source_ip) AS client_ip FROM usage_events WHERE provider_task_id='metadata-task'");
|
||||
assertEquals("api",saved.get("request_source"));
|
||||
assertEquals("203.0.113.8",saved.get("client_ip"));
|
||||
}
|
||||
|
||||
@Test void gatewaySettlementFailureKeepsDurableMeterAndRecoversExactlyOnce() {
|
||||
var f=chatFixture(); wallet.grant(ownerToken,memberId,UUID.randomUUID(),"1","recoverable settlement test",meta);
|
||||
var billing=f.billing();
|
||||
var execution=new com.moyi.controlplane.gateway.request.GatewayRequestExecutionService(billing,
|
||||
new com.moyi.controlplane.gateway.request.ApiRequestRiskPolicy(
|
||||
com.moyi.controlplane.gateway.request.ApiRequestRiskEngine.allowAll()));
|
||||
var requestContext=new com.moyi.controlplane.gateway.request.ApiRequestContext(memberId,
|
||||
UUID.fromString(String.valueOf(f.key().get("id"))),
|
||||
com.moyi.controlplane.gateway.request.ApiRequestProtocol.CHAT_COMPLETIONS,
|
||||
String.valueOf(f.model().get("id")),String.valueOf(f.model().get("id")),false,
|
||||
f.request().reservedInputTokens(),f.request().maxOutputTokens(),
|
||||
com.moyi.controlplane.gateway.request.ApiRequestMetadata.internal(),System.nanoTime());
|
||||
jdbc.execute("ALTER TABLE usage_events ADD CONSTRAINT test_reject_recoverable_cost CHECK (cost_amount IS NULL OR cost_amount=0)");
|
||||
try {
|
||||
String result=execution.execute(requestContext,f.key(),f.model(),f.request(),null,
|
||||
() -> (com.moyi.controlplane.gateway.request.ApiRequestAdmission)()->{},
|
||||
ticket -> { execution.settle(ticket,meteredResult(),5,null); return "unexpected"; },
|
||||
failure -> failure instanceof ApiException api ? api : new ApiException("CALL_INTERRUPTED","interrupted",502),
|
||||
ApiException::getCode);
|
||||
assertEquals("SETTLEMENT_PENDING",result);
|
||||
UUID ticketId=jdbc.queryForObject("SELECT id FROM chat_billing_requests WHERE user_id=? ORDER BY created_at DESC LIMIT 1",UUID.class,memberId);
|
||||
assertEquals("pending",jdbc.queryForObject("SELECT status FROM chat_billing_requests WHERE id=?",String.class,ticketId));
|
||||
assertNotNull(jdbc.queryForObject("SELECT settlement_usage FROM chat_billing_requests WHERE id=?",Object.class,ticketId));
|
||||
assertTrue(jdbc.queryForObject("SELECT reserved_balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId).signum()>0);
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE request_id=?",Integer.class,ticketId));
|
||||
|
||||
jdbc.execute("ALTER TABLE usage_events DROP CONSTRAINT test_reject_recoverable_cost");
|
||||
jdbc.update("UPDATE chat_billing_requests SET expires_at=now()-interval '1 second' WHERE id=?",ticketId);
|
||||
billing.recoverExpired();
|
||||
billing.recoverExpired();
|
||||
|
||||
assertEquals("succeeded",jdbc.queryForObject("SELECT status FROM chat_billing_requests WHERE id=?",String.class,ticketId));
|
||||
assertEquals(new BigDecimal("0.99966000"),jdbc.queryForObject("SELECT balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId));
|
||||
assertEquals(0,jdbc.queryForObject("SELECT reserved_balance FROM user_wallets WHERE user_id=?",BigDecimal.class,memberId).signum());
|
||||
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM wallet_entries WHERE request_id=?",Integer.class,ticketId));
|
||||
} finally {
|
||||
jdbc.execute("ALTER TABLE usage_events DROP CONSTRAINT IF EXISTS test_reject_recoverable_cost");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -65,7 +65,9 @@ class AuthPostgresTest {
|
||||
assertThrows(DataIntegrityViolationException.class, () -> jdbc.update("UPDATE users SET role = 'super_admin' WHERE id = ?", operatorId));
|
||||
assertThrows(IllegalStateException.class, () -> auth.ensureAdmin("operator", "test-password", "Other"));
|
||||
|
||||
MockMvc http = MockMvcBuilders.standaloneSetup(new AuthController(auth, org.mockito.Mockito.mock(VerificationService.class), 168, true))
|
||||
var verification = org.mockito.Mockito.mock(VerificationService.class);
|
||||
var verifiedAccounts = new VerifiedAccountService(auth, verification, context.getBean(DataSourceTransactionManager.class));
|
||||
MockMvc http = MockMvcBuilders.standaloneSetup(new AuthController(auth, verification, verifiedAccounts, 168, true))
|
||||
.setControllerAdvice(new ApiExceptionHandler()).build();
|
||||
var ownerCookie = login(http, "adimin", "super_admin");
|
||||
var operatorCookie = login(http, "operator", "admin");
|
||||
|
||||
@@ -32,6 +32,15 @@ class AuthRolesTest {
|
||||
verify(hasher).hash("test-password");
|
||||
}
|
||||
|
||||
@Test
|
||||
void firstBootstrapCannotUsePublishedDefaultsOrExamplePlaceholders() {
|
||||
for (String password : List.of("admin123", "GENERATE_ADMIN_PASSWORD", "replace-with-a-strong-initial-password")) {
|
||||
assertEquals("INVALID_ADMIN_CONFIG", assertThrows(ApiException.class, () -> auth.ensureAdmin("adimin", password, "Owner")).getCode());
|
||||
}
|
||||
verify(jdbc, never()).update(anyString(), any(Object[].class));
|
||||
verifyNoInteractions(hasher);
|
||||
}
|
||||
|
||||
@Test
|
||||
void bootstrapPromotesLegacyAdminWithoutReplacingPassword() throws Exception {
|
||||
lookupUser(ownerId, "admin");
|
||||
|
||||
+180
@@ -0,0 +1,180 @@
|
||||
package com.moyi.controlplane.auth;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.common.ApiExceptionHandler;
|
||||
import com.moyi.controlplane.security.SecretCipher;
|
||||
import jakarta.servlet.http.Cookie;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.sql.DataSource;
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
@EnabledIfEnvironmentVariable(named="MOYI_AUTH_TEST_JDBC_URL", matches="jdbc:postgresql://127\\.0\\.0\\.1:[0-9]+/.+")
|
||||
class AuthSecurityPostgresTest {
|
||||
@Configuration @EnableTransactionManagement(proxyTargetClass=true) static class Transactions {}
|
||||
private final UUID userId = UUID.randomUUID();
|
||||
private final ObjectMapper json = new ObjectMapper().findAndRegisterModules();
|
||||
private final AuthService.RequestMeta meta = new AuthService.RequestMeta("127.0.0.1", "isolated-security-test");
|
||||
private DriverManagerDataSource base, source;
|
||||
private String schema;
|
||||
private JdbcTemplate jdbc;
|
||||
private AnnotationConfigApplicationContext context;
|
||||
private AuthService auth;
|
||||
private PasswordHasher hasher;
|
||||
private MockMvc http;
|
||||
|
||||
@BeforeEach void setup() {
|
||||
String url = System.getenv("MOYI_AUTH_TEST_JDBC_URL");
|
||||
schema = "auth_security_" + UUID.randomUUID().toString().replace("-", "");
|
||||
base = new DriverManagerDataSource(url, "moyi_role_test", "");
|
||||
source = new DriverManagerDataSource(url + "?currentSchema=" + schema, "moyi_role_test", "");
|
||||
Flyway.configure().dataSource(base).schemas(schema).defaultSchema(schema).load().migrate();
|
||||
jdbc = new JdbcTemplate(source); hasher = spy(new PasswordHasher("isolated-pepper"));
|
||||
jdbc.update("INSERT INTO users(id,account,account_kind,password_hash,display_name,role) VALUES (?,'security_member','username',?,'Test','super_admin')", userId, hasher.hash("original-password"));
|
||||
context = new AnnotationConfigApplicationContext();
|
||||
context.register(Transactions.class);
|
||||
context.registerBean(DataSource.class, () -> source);
|
||||
context.registerBean(DataSourceTransactionManager.class, () -> new DataSourceTransactionManager(source));
|
||||
context.registerBean(AuthService.class, () -> new AuthService(jdbc, hasher, json, 168));
|
||||
context.registerBean(TotpService.class, () -> new TotpService(jdbc, new SecretCipher("isolated-mfa-key")));
|
||||
context.refresh(); auth = context.getBean(AuthService.class);
|
||||
var verification = mock(VerificationService.class);
|
||||
var verifiedAccounts = new VerifiedAccountService(auth, verification, context.getBean(DataSourceTransactionManager.class));
|
||||
http = MockMvcBuilders.standaloneSetup(new AuthController(auth, verification, verifiedAccounts, 168, true)).setControllerAdvice(new ApiExceptionHandler()).build();
|
||||
}
|
||||
|
||||
@AfterEach void cleanup() {
|
||||
if (context != null) context.close();
|
||||
if (base != null && schema != null) new JdbcTemplate(base).execute("DROP SCHEMA " + schema + " CASCADE");
|
||||
}
|
||||
private String session() { return auth.login("security_member", "original-password", meta).token(); }
|
||||
|
||||
@Test void passwordChangeRevokesEverySessionAndClearsTheBrowserCookie() throws Exception {
|
||||
String first = session(), second = session();
|
||||
http.perform(patch("/api/auth/account").cookie(new Cookie("moyi_session", first)).contentType("application/json")
|
||||
.content("{\"displayName\":\"Test\",\"currentPassword\":\"original-password\",\"newPassword\":\"replacement-password\"}"))
|
||||
.andExpect(status().isOk()).andExpect(cookie().maxAge("moyi_session", 0));
|
||||
assertEquals(401, assertThrows(ApiException.class, () -> auth.requireUser(first)).getStatus());
|
||||
assertEquals(401, assertThrows(ApiException.class, () -> auth.requireUser(second)).getStatus());
|
||||
assertEquals(401, assertThrows(ApiException.class, () -> session()).getStatus());
|
||||
assertNotNull(auth.login("security_member", "replacement-password", meta).token());
|
||||
}
|
||||
|
||||
@Test void allRolesHaveTheSamePasswordBoundsAndProfileOnlySaveKeepsSessions() {
|
||||
String token = session();
|
||||
assertEquals("WEAK_PASSWORD", assertThrows(ApiException.class, () -> auth.updateAccount(token, "Test", "original-password", "x", meta)).getCode());
|
||||
assertEquals("WEAK_PASSWORD", assertThrows(ApiException.class, () -> auth.resetPassword("security_member", "short", meta)).getCode());
|
||||
assertEquals("WEAK_PASSWORD", assertThrows(ApiException.class, () -> auth.register("unused", "new@example.test", "x".repeat(257), "New", meta)).getCode());
|
||||
auth.updateAccount(token, "Renamed", null, null, meta);
|
||||
assertEquals("Renamed", auth.requireUser(token).displayName());
|
||||
assertEquals(0, jdbc.queryForObject("SELECT count(*) FROM auth_sessions WHERE revoked_at IS NOT NULL", Integer.class));
|
||||
}
|
||||
|
||||
@Test void failedLoginBudgetIsCommittedAndHttpReturnsRetryAfter() throws Exception {
|
||||
for (int i = 0; i < 10; i++) assertEquals(401, assertThrows(ApiException.class, () -> auth.login("missing_member", "wrong-password", meta)).getStatus());
|
||||
http.perform(post("/api/auth/login").contentType("application/json").content("{\"account\":\"missing_member\",\"password\":\"wrong-password\"}"))
|
||||
.andExpect(status().isTooManyRequests()).andExpect(header().exists("Retry-After"));
|
||||
assertEquals(2, jdbc.queryForObject("SELECT count(*) FROM auth_login_windows", Integer.class));
|
||||
assertFalse(jdbc.queryForObject("SELECT string_agg(bucket_hash,',') FROM auth_login_windows", String.class).contains("missing_member"));
|
||||
jdbc.update("UPDATE auth_login_windows SET expires_at=clock_timestamp()-interval '1 second'");
|
||||
assertEquals(401, assertThrows(ApiException.class, () -> auth.login("missing_member", "wrong-password", meta)).getStatus());
|
||||
}
|
||||
|
||||
@Test void concurrentLoginBudgetCannotExceedTheAccountLimit() throws Exception {
|
||||
var start = new CountDownLatch(1);
|
||||
try (var executor = Executors.newFixedThreadPool(12)) {
|
||||
var results = new ArrayList<java.util.concurrent.Future<Integer>>();
|
||||
for (int i = 0; i < 20; i++) results.add(executor.submit(() -> {
|
||||
assertTrue(start.await(10, TimeUnit.SECONDS));
|
||||
try { auth.login("missing_member", "wrong-password", meta); return 200; }
|
||||
catch (ApiException error) { return error.getStatus(); }
|
||||
}));
|
||||
start.countDown();
|
||||
var codes = new ArrayList<Integer>();
|
||||
for (var result : results) codes.add(result.get(30, TimeUnit.SECONDS));
|
||||
assertEquals(10, codes.stream().filter(code -> code == 401).count());
|
||||
assertEquals(10, codes.stream().filter(code -> code == 429).count());
|
||||
}
|
||||
}
|
||||
|
||||
@Test void sourceBudgetAlsoLimitsChangingUnknownAccounts() {
|
||||
var limiter = new LoginAttemptLimiter(jdbc);
|
||||
for (int i = 0; i < 50; i++) limiter.check("different-" + i, "127.0.0.1");
|
||||
assertEquals(429, assertThrows(ApiException.class, () -> limiter.check("yet-another", "127.0.0.1")).getStatus());
|
||||
assertDoesNotThrow(() -> limiter.check("yet-another", "127.0.0.2"));
|
||||
}
|
||||
|
||||
@Test void passwordResetWinningTheUserLockPreventsOldPasswordSessionIssuance() throws Exception {
|
||||
try (var executor = Executors.newSingleThreadExecutor()) {
|
||||
var started = new CountDownLatch(1);
|
||||
var future = new java.util.concurrent.atomic.AtomicReference<java.util.concurrent.Future<Integer>>();
|
||||
new TransactionTemplate(new DataSourceTransactionManager(source)).execute(status -> {
|
||||
jdbc.queryForList("SELECT id FROM users WHERE id=? FOR UPDATE", userId);
|
||||
future.set(executor.submit(() -> {
|
||||
started.countDown();
|
||||
try { session(); return 200; } catch (ApiException error) { return error.getStatus(); }
|
||||
}));
|
||||
try { assertTrue(started.await(10, TimeUnit.SECONDS)); } catch (InterruptedException error) { throw new AssertionError(error); }
|
||||
auth.resetPassword("security_member", "replacement-password", meta);
|
||||
return null;
|
||||
});
|
||||
assertEquals(401, future.get().get(20, TimeUnit.SECONDS));
|
||||
assertEquals(0, jdbc.queryForObject("SELECT count(*) FROM auth_sessions WHERE revoked_at IS NULL", Integer.class));
|
||||
}
|
||||
}
|
||||
|
||||
@Test void loginWinningTheLockStillGetsRevokedByFollowingPasswordReset() throws Exception {
|
||||
var verified = new CountDownLatch(1); var allow = new CountDownLatch(1);
|
||||
doAnswer(call -> {
|
||||
verified.countDown(); assertTrue(allow.await(15, TimeUnit.SECONDS)); return call.callRealMethod();
|
||||
}).when(hasher).matches(eq("original-password"), anyString());
|
||||
try (var executor = Executors.newFixedThreadPool(2)) {
|
||||
var login = executor.submit(this::session);
|
||||
assertTrue(verified.await(10, TimeUnit.SECONDS));
|
||||
var reset = executor.submit(() -> auth.resetPassword("security_member", "replacement-password", meta));
|
||||
allow.countDown();
|
||||
String token = login.get(20, TimeUnit.SECONDS); reset.get(20, TimeUnit.SECONDS);
|
||||
assertEquals(401, assertThrows(ApiException.class, () -> auth.requireUser(token)).getStatus());
|
||||
} finally { allow.countDown(); }
|
||||
}
|
||||
|
||||
@Test void mfaFailuresRemainCommittedInsideTheLoginTransaction() {
|
||||
var cipher = new SecretCipher("isolated-mfa-key");
|
||||
jdbc.update("INSERT INTO user_totp(user_id,secret_ciphertext,enabled) VALUES (?,?,true)", userId, cipher.encrypt("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"));
|
||||
for (int i = 0; i < 5; i++) assertEquals("INVALID_MFA_CODE", assertThrows(ApiException.class, () -> auth.login("security_member", "original-password", "not-a-code", meta)).getCode());
|
||||
assertEquals("MFA_RATE_LIMITED", assertThrows(ApiException.class, () -> auth.login("security_member", "original-password", "not-a-code", meta)).getCode());
|
||||
assertEquals(5, jdbc.queryForObject("SELECT attempts FROM user_totp WHERE user_id=?", Integer.class, userId));
|
||||
assertEquals(0, jdbc.queryForObject("SELECT count(*) FROM auth_sessions", Integer.class));
|
||||
}
|
||||
|
||||
@Test void auditFailureRollsBackPasswordAndRevocationsTogether() {
|
||||
String token = session();
|
||||
jdbc.execute("CREATE FUNCTION reject_password_audit() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NEW.event_type='user.account_updated' THEN RAISE EXCEPTION 'isolated audit failure'; END IF; RETURN NEW; END $$");
|
||||
jdbc.execute("CREATE TRIGGER reject_password_audit BEFORE INSERT ON auth_audit_log FOR EACH ROW EXECUTE FUNCTION reject_password_audit()");
|
||||
assertThrows(org.springframework.dao.DataAccessException.class, () -> auth.updateAccount(token, "Test", "original-password", "replacement-password", meta));
|
||||
assertNotNull(auth.requireUser(token));
|
||||
assertNotNull(session());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.moyi.controlplane.auth;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PasswordPolicyTest {
|
||||
@Test void everyNewPasswordHasTheSameBoundsWithoutTrimming() {
|
||||
for (String value : new String[]{null, "", " ".repeat(12), "short", "x".repeat(257)}) assertThrows(ApiException.class, () -> PasswordPolicy.validate(value));
|
||||
assertDoesNotThrow(() -> PasswordPolicy.validate("a".repeat(8)));
|
||||
assertDoesNotThrow(() -> PasswordPolicy.validate("a".repeat(256)));
|
||||
}
|
||||
@Test void malformedHashesCannotSelectUnboundedWorkAndNullIsNeverAPassword() {
|
||||
var hasher = new PasswordHasher("pepper");
|
||||
for (String encoded : new String[]{null, "", "pbkdf2$2147483647$AAECAwQFBgcICQoLDA0ODw$" + "A".repeat(43), "pbkdf2$210000$AA$AA"}) assertFalse(hasher.matches("test-password", encoded));
|
||||
assertFalse(hasher.matches(null, hasher.hash("null")));
|
||||
}
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.moyi.controlplane.auth;
|
||||
|
||||
/** Deterministic test-only access; no public production dispatch endpoint. */
|
||||
public final class VerificationDeliveryFixture {
|
||||
private VerificationDeliveryFixture() {}
|
||||
public static boolean deliverOne(VerificationDispatchService dispatch) { return dispatch.dispatchOne(); }
|
||||
}
|
||||
+343
@@ -0,0 +1,343 @@
|
||||
package com.moyi.controlplane.auth;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.common.ApiExceptionHandler;
|
||||
import com.moyi.controlplane.security.SecretCipher;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import javax.sql.DataSource;
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.transaction.IllegalTransactionStateException;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
@EnabledIfEnvironmentVariable(named="MOYI_AUTH_TEST_JDBC_URL",matches="jdbc:postgresql://127[.]0[.]0[.]1:[0-9]+/.+")
|
||||
class VerificationDispatchPostgresTest {
|
||||
@Configuration @EnableTransactionManagement(proxyTargetClass=true) static class Transactions {}
|
||||
private static final String ACTIVE="active@example.test", UNKNOWN="unknown@example.test", DISABLED="disabled@example.test";
|
||||
private final ObjectMapper json=new ObjectMapper().findAndRegisterModules();
|
||||
private final SecretCipher cipher=new SecretCipher("isolated-delivery-encryption");
|
||||
private DriverManagerDataSource base;
|
||||
private String schema, invitation;
|
||||
private JdbcTemplate jdbc;
|
||||
private AnnotationConfigApplicationContext context;
|
||||
private VerificationDispatchService dispatch;
|
||||
private VerificationService verification;
|
||||
private MockMvc http;
|
||||
private final TestSender sender=new TestSender();
|
||||
|
||||
static class TestSender implements VerificationSender {
|
||||
final Map<String,String> codes=new ConcurrentHashMap<>();
|
||||
final AtomicInteger calls=new AtomicInteger();
|
||||
volatile boolean available=true, fail=false;
|
||||
volatile CountDownLatch arrived, release;
|
||||
volatile VerificationPurpose lastPurpose;
|
||||
public boolean available(String channel) { return available; }
|
||||
public void send(String channel,String account,String code) { send(channel,account,code,VerificationPurpose.REGISTRATION); }
|
||||
public void send(String channel,String account,String code,VerificationPurpose purpose) {
|
||||
assertFalse(TransactionSynchronizationManager.isActualTransactionActive(),"Network delivery must not hold a DB transaction");
|
||||
calls.incrementAndGet(); lastPurpose=purpose;
|
||||
if (arrived!=null) arrived.countDown();
|
||||
if (release!=null) try { assertTrue(release.await(10,TimeUnit.SECONDS)); }
|
||||
catch (InterruptedException error) { Thread.currentThread().interrupt(); throw new IllegalStateException("Interrupted fixture"); }
|
||||
if (fail) throw new ApiException("VERIFICATION_SEND_FAILED","Synthetic provider detail must stay private",502);
|
||||
codes.put(account,code);
|
||||
}
|
||||
}
|
||||
|
||||
@BeforeEach void setup() {
|
||||
String url=System.getenv("MOYI_AUTH_TEST_JDBC_URL");
|
||||
schema="verification_dispatch_"+UUID.randomUUID().toString().replace("-","");
|
||||
base=new DriverManagerDataSource(url,"moyi_role_test","");
|
||||
var source=new DriverManagerDataSource(url+"?currentSchema="+schema,"moyi_role_test","");
|
||||
Flyway.configure().dataSource(base).schemas(schema).defaultSchema(schema).load().migrate();
|
||||
jdbc=new JdbcTemplate(source); UUID creator=UUID.randomUUID(); invitation="synthetic-invite-"+UUID.randomUUID();
|
||||
jdbc.update("INSERT INTO users(id,account,account_kind,password_hash,display_name,status) VALUES (?,?,'email','not-a-login-hash','Synthetic','active'),(?,?,'email','not-a-login-hash','Synthetic','disabled')",creator,ACTIVE,UUID.randomUUID(),DISABLED);
|
||||
jdbc.update("INSERT INTO invitation_codes(id,code_hash,code_prefix,label,max_uses,created_by) VALUES (?,?,?,'Synthetic',100,?)",UUID.randomUUID(),AuthService.hashSecret(invitation),"synthetic",creator);
|
||||
context=new AnnotationConfigApplicationContext(); context.register(Transactions.class);
|
||||
context.registerBean(DataSource.class,()->source);
|
||||
context.registerBean(DataSourceTransactionManager.class,()->new DataSourceTransactionManager(source));
|
||||
context.registerBean(VerificationDispatchService.class,()->new VerificationDispatchService(jdbc,sender,cipher,json,context.getBean(DataSourceTransactionManager.class)));
|
||||
context.registerBean(VerificationService.class,()->new VerificationService(jdbc,context.getBean(VerificationDispatchService.class),"isolated-pepper"));
|
||||
context.refresh(); dispatch=context.getBean(VerificationDispatchService.class); verification=context.getBean(VerificationService.class);
|
||||
http=MockMvcBuilders.standaloneSetup(new AuthController(mock(AuthService.class),verification,mock(VerifiedAccountService.class),168,true))
|
||||
.setControllerAdvice(new ApiExceptionHandler()).build();
|
||||
}
|
||||
@AfterEach void cleanup() {
|
||||
if (sender.release!=null) sender.release.countDown();
|
||||
if (context!=null) context.close();
|
||||
if (base!=null && schema!=null) new JdbcTemplate(base).execute("DROP SCHEMA "+schema+" CASCADE");
|
||||
}
|
||||
private UUID delivery(String account,VerificationPurpose purpose) {
|
||||
return jdbc.queryForObject("SELECT delivery_id FROM "+purpose.table+" WHERE account=?",UUID.class,account);
|
||||
}
|
||||
private void releaseQuota(String account) { jdbc.update("UPDATE verification_send_events SET created_at=clock_timestamp()-interval '61 seconds' WHERE account=?",account); }
|
||||
private void assertTerminal(UUID id,String state) {
|
||||
var row=jdbc.queryForMap("SELECT status,payload_ciphertext,finished_at FROM verification_delivery_outbox WHERE id=?",id);
|
||||
assertEquals(state,row.get("status")); assertNull(row.get("payload_ciphertext")); assertNotNull(row.get("finished_at"));
|
||||
}
|
||||
|
||||
@Test void resetRequestsHaveIdenticalReceiptsAndNoRequestSideDeliveryForAllAccountStates() throws Exception {
|
||||
String reference=null;
|
||||
for (String account : java.util.List.of(ACTIVE,UNKNOWN,DISABLED)) {
|
||||
var response=http.perform(post("/api/auth/password-reset/code").contentType("application/json").content(json.writeValueAsBytes(Map.of("account",account))))
|
||||
.andExpect(status().isOk()).andExpect(jsonPath("$.sent").value(true)).andExpect(jsonPath("$.code").doesNotExist())
|
||||
.andExpect(jsonPath("$.token").doesNotExist()).andExpect(cookie().doesNotExist("moyi_session")).andReturn().getResponse().getContentAsString();
|
||||
if (reference==null) reference=response; else assertEquals(json.readTree(reference),json.readTree(response));
|
||||
}
|
||||
assertEquals(0,sender.calls.get());
|
||||
assertEquals(3,jdbc.queryForObject("SELECT count(*) FROM verification_send_events",Integer.class));
|
||||
assertEquals(3,jdbc.queryForObject("SELECT count(*) FROM verification_delivery_outbox WHERE status='queued'",Integer.class));
|
||||
assertEquals(3,jdbc.queryForObject("SELECT count(*) FROM password_reset_codes",Integer.class));
|
||||
}
|
||||
|
||||
@Test void knownAndUnknownRepeatedRequestsExposeTheSameLimit() throws Exception {
|
||||
for (String account : java.util.List.of(ACTIVE,UNKNOWN,DISABLED)) {
|
||||
verification.sendReset(account,"127.0.0.1");
|
||||
http.perform(post("/api/auth/password-reset/code").contentType("application/json").content(json.writeValueAsBytes(Map.of("account",account))))
|
||||
.andExpect(status().isTooManyRequests()).andExpect(jsonPath("$.error.code").value("VERIFICATION_RATE_LIMIT"));
|
||||
}
|
||||
assertEquals(3,jdbc.queryForObject("SELECT count(*) FROM verification_send_events",Integer.class));
|
||||
}
|
||||
|
||||
@Test void workerDeliversOnlyToActiveUsersAndPurgesAllFinishedPayloads() {
|
||||
for (String account : java.util.List.of(ACTIVE,UNKNOWN,DISABLED)) verification.sendReset(account,"127.0.0.1");
|
||||
for (int i=0;i<3;i++) assertTrue(dispatch.dispatchOne());
|
||||
assertEquals(1,sender.calls.get()); assertEquals(VerificationPurpose.PASSWORD_RESET,sender.lastPurpose);
|
||||
assertTrue(sender.codes.containsKey(ACTIVE)); assertFalse(sender.codes.containsKey(UNKNOWN)); assertFalse(sender.codes.containsKey(DISABLED));
|
||||
assertTerminal(delivery(ACTIVE,VerificationPurpose.PASSWORD_RESET),"sent");
|
||||
assertTerminal(delivery(UNKNOWN,VerificationPurpose.PASSWORD_RESET),"discarded");
|
||||
assertTerminal(delivery(DISABLED,VerificationPurpose.PASSWORD_RESET),"discarded");
|
||||
}
|
||||
|
||||
@Test void payloadContainsNoPlaintextAccountOrOtpAndIsBoundToItsGeneration() throws Exception {
|
||||
verification.sendReset(ACTIVE,"127.0.0.1"); UUID id=delivery(ACTIVE,VerificationPurpose.PASSWORD_RESET);
|
||||
String stored=jdbc.queryForObject("SELECT payload_ciphertext FROM verification_delivery_outbox WHERE id=?",String.class,id);
|
||||
var decoded=json.readTree(cipher.decrypt(stored));
|
||||
assertTrue(stored.startsWith("v1$")); assertFalse(stored.contains(ACTIVE)); assertFalse(stored.contains("account"));
|
||||
assertEquals(id.toString(),decoded.path("id").asText()); assertEquals("password-reset",decoded.path("purpose").asText());
|
||||
assertTrue(decoded.path("code").asText().matches("[0-9]{6}"));
|
||||
assertNotEquals(decoded.path("code").asText(),jdbc.queryForObject("SELECT code_hash FROM password_reset_codes WHERE account=?",String.class,ACTIVE));
|
||||
}
|
||||
|
||||
@Test void providerFailureStaysOutOfTheReceiptAndIsNotRetried() {
|
||||
sender.fail=true;
|
||||
assertEquals(true,verification.sendReset(ACTIVE,"127.0.0.1").get("sent"));
|
||||
UUID id=delivery(ACTIVE,VerificationPurpose.PASSWORD_RESET); assertTrue(dispatch.dispatchOne());
|
||||
assertTerminal(id,"failed"); assertEquals(1,sender.calls.get()); assertFalse(dispatch.dispatchOne()); assertEquals(1,sender.calls.get());
|
||||
assertTrue(jdbc.queryForObject("SELECT consumed FROM password_reset_codes WHERE delivery_id=?",Boolean.class,id));
|
||||
assertEquals("DELIVERY_FAILED",jdbc.queryForObject("SELECT failure_code FROM verification_delivery_outbox WHERE id=?",String.class,id));
|
||||
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM verification_send_events",Integer.class));
|
||||
}
|
||||
|
||||
@Test void sendingDoesNotHoldTheGlobalQuotaLockOrADatabaseTransaction() throws Exception {
|
||||
verification.sendReset(ACTIVE,"127.0.0.1");
|
||||
sender.arrived=new CountDownLatch(1); sender.release=new CountDownLatch(1);
|
||||
try (var executor=Executors.newFixedThreadPool(2)) {
|
||||
var worker=executor.submit(dispatch::dispatchOne); assertTrue(sender.arrived.await(3,TimeUnit.SECONDS));
|
||||
var request=executor.submit(()->verification.sendReset(UNKNOWN,"127.0.0.2"));
|
||||
assertEquals(true,request.get(3,TimeUnit.SECONDS).get("sent"));
|
||||
assertEquals("sending",jdbc.queryForObject("SELECT status FROM verification_delivery_outbox WHERE id=?",String.class,delivery(ACTIVE,VerificationPurpose.PASSWORD_RESET)));
|
||||
sender.release.countDown(); assertTrue(worker.get(3,TimeUnit.SECONDS));
|
||||
}
|
||||
}
|
||||
|
||||
@Test void supersededQueueEntriesCannotDeliverOldCodesOrInvalidateNewOnes() {
|
||||
verification.sendReset(ACTIVE,"127.0.0.1"); UUID old=delivery(ACTIVE,VerificationPurpose.PASSWORD_RESET);
|
||||
releaseQuota(ACTIVE); verification.sendReset(ACTIVE,"127.0.0.1"); UUID current=delivery(ACTIVE,VerificationPurpose.PASSWORD_RESET);
|
||||
assertNotEquals(old,current); assertTrue(dispatch.dispatchOne()); assertTerminal(old,"discarded"); assertEquals(0,sender.calls.get());
|
||||
assertFalse(jdbc.queryForObject("SELECT consumed FROM password_reset_codes WHERE delivery_id=?",Boolean.class,current));
|
||||
assertTrue(dispatch.dispatchOne()); assertTerminal(current,"sent"); assertEquals(1,sender.calls.get());
|
||||
assertEquals(ACTIVE,verification.consumeReset(ACTIVE,sender.codes.get(ACTIVE)));
|
||||
}
|
||||
|
||||
@Test void oldInFlightFailureDoesNotConsumeTheReplacementCode() throws Exception {
|
||||
verification.sendReset(ACTIVE,"127.0.0.1"); UUID old=delivery(ACTIVE,VerificationPurpose.PASSWORD_RESET);
|
||||
sender.fail=true; sender.arrived=new CountDownLatch(1); sender.release=new CountDownLatch(1);
|
||||
try (var executor=Executors.newSingleThreadExecutor()) {
|
||||
var worker=executor.submit(dispatch::dispatchOne); assertTrue(sender.arrived.await(3,TimeUnit.SECONDS));
|
||||
releaseQuota(ACTIVE); verification.sendReset(ACTIVE,"127.0.0.2"); UUID replacement=delivery(ACTIVE,VerificationPurpose.PASSWORD_RESET);
|
||||
sender.release.countDown(); assertTrue(worker.get(3,TimeUnit.SECONDS)); assertTerminal(old,"failed");
|
||||
assertFalse(jdbc.queryForObject("SELECT consumed FROM password_reset_codes WHERE delivery_id=?",Boolean.class,replacement));
|
||||
sender.fail=false; sender.arrived=null; sender.release=null; assertTrue(dispatch.dispatchOne()); assertTerminal(replacement,"sent");
|
||||
}
|
||||
}
|
||||
|
||||
@Test void expiredAndAmbiguousSendingRowsAreErasedWithoutAutomaticReplay() {
|
||||
verification.sendReset(ACTIVE,"127.0.0.1"); UUID id=delivery(ACTIVE,VerificationPurpose.PASSWORD_RESET);
|
||||
jdbc.update("UPDATE verification_delivery_outbox SET status='sending',started_at=clock_timestamp() WHERE id=?",id);
|
||||
assertFalse(dispatch.dispatchOne()); assertEquals(0,sender.calls.get());
|
||||
jdbc.update("UPDATE verification_delivery_outbox SET expires_at=clock_timestamp()-interval '1 second' WHERE id=?",id);
|
||||
assertFalse(dispatch.dispatchOne()); assertTerminal(id,"discarded"); assertEquals(0,sender.calls.get());
|
||||
jdbc.update("UPDATE verification_delivery_outbox SET finished_at=clock_timestamp()-interval '3 days' WHERE id=?",id);
|
||||
assertFalse(dispatch.dispatchOne()); assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM verification_delivery_outbox",Integer.class));
|
||||
}
|
||||
|
||||
@Test void disabledAfterEnqueueAndAlreadyConsumedCodesAreNotSent() {
|
||||
verification.sendReset(ACTIVE,"127.0.0.1"); UUID id=delivery(ACTIVE,VerificationPurpose.PASSWORD_RESET);
|
||||
jdbc.update("UPDATE users SET status='disabled' WHERE account=?",ACTIVE); assertTrue(dispatch.dispatchOne()); assertTerminal(id,"discarded");
|
||||
jdbc.update("UPDATE users SET status='active' WHERE account=?",ACTIVE); releaseQuota(ACTIVE); verification.sendReset(ACTIVE,"127.0.0.1");
|
||||
id=delivery(ACTIVE,VerificationPurpose.PASSWORD_RESET); jdbc.update("UPDATE password_reset_codes SET consumed=true WHERE delivery_id=?",id);
|
||||
assertTrue(dispatch.dispatchOne()); assertTerminal(id,"discarded"); assertEquals(0,sender.calls.get());
|
||||
}
|
||||
|
||||
@Test void missingEncryptionOrDisabledSenderIsAccountIndependentAndCreatesNoWork() {
|
||||
var missingKey=new VerificationDispatchService(jdbc,sender,new SecretCipher(""),json,context.getBean(DataSourceTransactionManager.class));
|
||||
try { assertFalse(missingKey.available("email")); assertFalse(missingKey.available("phone")); } finally { missingKey.close(); }
|
||||
sender.available=false;
|
||||
for (String account : java.util.List.of(ACTIVE,UNKNOWN,DISABLED)) {
|
||||
assertEquals("VERIFICATION_UNAVAILABLE",assertThrows(ApiException.class,()->verification.sendReset(account,"127.0.0.1")).getCode());
|
||||
}
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM verification_delivery_outbox",Integer.class));
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM verification_send_events",Integer.class));
|
||||
}
|
||||
|
||||
@Test void queueFailureRollsBackTheCodeAndQuotaRecordTogether() {
|
||||
jdbc.execute("ALTER TABLE verification_delivery_outbox ADD CONSTRAINT injected_failure CHECK (purpose <> 'password-reset') NOT VALID");
|
||||
assertThrows(DataIntegrityViolationException.class,()->verification.sendReset(ACTIVE,"127.0.0.1"));
|
||||
for (String table : java.util.List.of("verification_delivery_outbox","verification_send_events","password_reset_codes")) {
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM "+table,Integer.class));
|
||||
}
|
||||
jdbc.execute("ALTER TABLE verification_delivery_outbox DROP CONSTRAINT injected_failure");
|
||||
assertEquals(true,verification.sendReset(ACTIVE,"127.0.0.1").get("sent"));
|
||||
}
|
||||
|
||||
@Test void concurrentRequestsAcrossConnectionsCannotDuplicateTheSameAccountAdmission() throws Exception {
|
||||
var start=new CountDownLatch(1); var results=new ArrayList<String>();
|
||||
try (var executor=Executors.newFixedThreadPool(2)) {
|
||||
var requests=new ArrayList<java.util.concurrent.Future<String>>();
|
||||
for (int i=0;i<2;i++) requests.add(executor.submit(()->{
|
||||
assertTrue(start.await(3,TimeUnit.SECONDS));
|
||||
try { verification.sendReset(UNKNOWN,"127.0.0.1"); return "accepted"; } catch (ApiException error) { return error.getCode(); }
|
||||
}));
|
||||
start.countDown(); for (var request : requests) results.add(request.get(5,TimeUnit.SECONDS));
|
||||
}
|
||||
assertEquals(1,results.stream().filter("accepted"::equals).count()); assertEquals(1,results.stream().filter("VERIFICATION_RATE_LIMIT"::equals).count());
|
||||
assertEquals(1,jdbc.queryForObject("SELECT count(*) FROM verification_delivery_outbox",Integer.class));
|
||||
}
|
||||
|
||||
@Test void concurrentWorkerClaimsSendAGenerationAtMostOnce() throws Exception {
|
||||
verification.sendReset(ACTIVE,"127.0.0.1"); var start=new CountDownLatch(1);
|
||||
try (var executor=Executors.newFixedThreadPool(2)) {
|
||||
var first=executor.submit(()->{start.await(); return dispatch.dispatchOne();});
|
||||
var second=executor.submit(()->{start.await(); return dispatch.dispatchOne();}); start.countDown();
|
||||
assertNotEquals(first.get(5,TimeUnit.SECONDS),second.get(5,TimeUnit.SECONDS));
|
||||
}
|
||||
assertEquals(1,sender.calls.get()); assertTerminal(delivery(ACTIVE,VerificationPurpose.PASSWORD_RESET),"sent");
|
||||
}
|
||||
|
||||
@Test void accountDailySourceHourlyAndGlobalBudgetsIncludeUnknownAccounts() {
|
||||
for (int i=0;i<10;i++) {
|
||||
verification.sendReset(UNKNOWN,"127.0.0.1"); releaseQuota(UNKNOWN);
|
||||
}
|
||||
assertEquals("VERIFICATION_RATE_LIMIT",assertThrows(ApiException.class,()->verification.sendReset(UNKNOWN,"127.0.0.2")).getCode());
|
||||
jdbc.update("DELETE FROM verification_send_events");
|
||||
jdbc.execute("INSERT INTO verification_send_events(id,account,ip_address) SELECT md5('source-'||g)::uuid,'synthetic-'||g||'@example.test','127.0.0.9' FROM generate_series(1,30) g");
|
||||
assertEquals("VERIFICATION_RATE_LIMIT",assertThrows(ApiException.class,()->verification.sendReset(ACTIVE,"127.0.0.9")).getCode());
|
||||
assertEquals("VERIFICATION_RATE_LIMIT",assertThrows(ApiException.class,()->verification.sendReset("other@example.test","127.0.0.9")).getCode());
|
||||
jdbc.update("DELETE FROM verification_send_events");
|
||||
jdbc.execute("INSERT INTO verification_send_events(id,account,ip_address) SELECT md5('global-'||g)::uuid,'synthetic-'||g||'@example.test','192.0.2.1' FROM generate_series(1,1000) g");
|
||||
assertEquals("VERIFICATION_RATE_LIMIT",assertThrows(ApiException.class,()->verification.sendReset(ACTIVE,"127.0.0.1")).getCode());
|
||||
assertEquals("VERIFICATION_RATE_LIMIT",assertThrows(ApiException.class,()->verification.sendReset("another@example.test","127.0.0.2")).getCode());
|
||||
}
|
||||
|
||||
@Test void crossPurposeQuotaIsSharedWhileCodesRemainPurposeBound() {
|
||||
verification.send(UNKNOWN,invitation,"127.0.0.1");
|
||||
assertEquals("VERIFICATION_RATE_LIMIT",assertThrows(ApiException.class,()->verification.sendReset(UNKNOWN,"127.0.0.1")).getCode());
|
||||
assertTrue(dispatch.dispatchOne()); String code=sender.codes.get(UNKNOWN);
|
||||
assertEquals(VerificationPurpose.REGISTRATION,sender.lastPurpose); verification.consume(UNKNOWN,code);
|
||||
assertEquals("VERIFICATION_INVALID",assertThrows(ApiException.class,()->verification.consumeReset(UNKNOWN,code)).getCode());
|
||||
}
|
||||
|
||||
@Test void allInvalidResetCodeStatesHaveTheSamePublicError() {
|
||||
String expected=assertThrows(ApiException.class,()->verification.consumeReset(UNKNOWN,"000000")).getMessage();
|
||||
verification.sendReset(ACTIVE,"127.0.0.1"); assertTrue(dispatch.dispatchOne());
|
||||
String correct=sender.codes.get(ACTIVE), wrong="000000".equals(correct)?"111111":"000000";
|
||||
assertEquals(expected,assertThrows(ApiException.class,()->verification.consumeReset(ACTIVE,wrong)).getMessage());
|
||||
jdbc.update("UPDATE password_reset_codes SET attempts=5 WHERE account=?",ACTIVE);
|
||||
assertEquals(expected,assertThrows(ApiException.class,()->verification.consumeReset(ACTIVE,correct)).getMessage());
|
||||
jdbc.update("UPDATE password_reset_codes SET attempts=0,expires_at=clock_timestamp()-interval '1 second' WHERE account=?",ACTIVE);
|
||||
assertEquals(expected,assertThrows(ApiException.class,()->verification.consumeReset(ACTIVE,correct)).getMessage());
|
||||
}
|
||||
|
||||
@Test void ciphertextTamperingAndEnvelopeSwappingNeverReachTheSender() {
|
||||
verification.sendReset(ACTIVE,"127.0.0.1"); UUID first=delivery(ACTIVE,VerificationPurpose.PASSWORD_RESET);
|
||||
jdbc.update("UPDATE verification_delivery_outbox SET payload_ciphertext='v1$invalid$ciphertext' WHERE id=?",first);
|
||||
assertTrue(dispatch.dispatchOne()); assertTerminal(first,"failed"); assertEquals(0,sender.calls.get());
|
||||
releaseQuota(ACTIVE); verification.sendReset(ACTIVE,"127.0.0.1");
|
||||
UUID second=delivery(ACTIVE,VerificationPurpose.PASSWORD_RESET); String valid=jdbc.queryForObject("SELECT payload_ciphertext FROM verification_delivery_outbox WHERE id=?",String.class,second);
|
||||
verification.sendReset(UNKNOWN,"127.0.0.2"); UUID third=delivery(UNKNOWN,VerificationPurpose.PASSWORD_RESET);
|
||||
jdbc.update("UPDATE verification_delivery_outbox SET payload_ciphertext=? WHERE id=?",valid,third);
|
||||
assertTrue(dispatch.dispatchOne()); assertTerminal(second,"sent"); assertTrue(dispatch.dispatchOne()); assertTerminal(third,"failed");
|
||||
assertEquals(1,sender.calls.get());
|
||||
}
|
||||
|
||||
@Test void enqueuingRequiresTheCallingTransaction() {
|
||||
assertThrows(IllegalTransactionStateException.class,()->dispatch.enqueue(VerificationPurpose.REGISTRATION,UNKNOWN,"123456","a".repeat(64)));
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM verification_delivery_outbox",Integer.class));
|
||||
}
|
||||
|
||||
@Test void failedReceiptPersistenceNeverResendsTheMessage() {
|
||||
verification.sendReset(ACTIVE,"127.0.0.1"); UUID id=delivery(ACTIVE,VerificationPurpose.PASSWORD_RESET);
|
||||
jdbc.execute("ALTER TABLE verification_delivery_outbox ADD CONSTRAINT injected_receipt_failure CHECK (status <> 'sent') NOT VALID");
|
||||
assertThrows(DataIntegrityViolationException.class,dispatch::dispatchOne); assertEquals(1,sender.calls.get());
|
||||
assertEquals("sending",jdbc.queryForObject("SELECT status FROM verification_delivery_outbox WHERE id=?",String.class,id));
|
||||
jdbc.execute("ALTER TABLE verification_delivery_outbox DROP CONSTRAINT injected_receipt_failure");
|
||||
assertFalse(dispatch.dispatchOne()); assertEquals(1,sender.calls.get());
|
||||
}
|
||||
|
||||
@Test void scheduledDeliveryHasOnlyTwoWorkersAndNoUnboundedWaitingQueue() throws Exception {
|
||||
for (int i=0;i<6;i++) verification.send("queued-"+i+"@example.test",invitation,"127.0.0.1");
|
||||
sender.arrived=new CountDownLatch(2); sender.release=new CountDownLatch(1);
|
||||
try {
|
||||
dispatch.dispatchPending(); assertTrue(sender.arrived.await(3,TimeUnit.SECONDS));
|
||||
for (int i=0;i<10;i++) dispatch.dispatchPending();
|
||||
assertEquals(2,sender.calls.get());
|
||||
assertEquals(2,jdbc.queryForObject("SELECT count(*) FROM verification_delivery_outbox WHERE status='sending'",Integer.class));
|
||||
assertEquals(4,jdbc.queryForObject("SELECT count(*) FROM verification_delivery_outbox WHERE status='queued'",Integer.class));
|
||||
} finally { sender.release.countDown(); dispatch.close(); }
|
||||
}
|
||||
|
||||
@Test void blockedQuotaLockTimesOutAndDoesNotLeavePartialAdmission() throws Exception {
|
||||
var locked=new CountDownLatch(1); var unlock=new CountDownLatch(1);
|
||||
try (var executor=Executors.newSingleThreadExecutor()) {
|
||||
var blocker=executor.submit(()->new TransactionTemplate(context.getBean(DataSourceTransactionManager.class)).executeWithoutResult(status->{
|
||||
jdbc.queryForList("SELECT pg_advisory_xact_lock(734092615)"); locked.countDown();
|
||||
try { assertTrue(unlock.await(8,TimeUnit.SECONDS)); } catch (InterruptedException error) { Thread.currentThread().interrupt(); throw new IllegalStateException(error); }
|
||||
}));
|
||||
assertTrue(locked.await(3,TimeUnit.SECONDS)); long start=System.nanoTime();
|
||||
try {
|
||||
assertThrows(org.springframework.dao.DataAccessException.class,()->verification.sendReset(UNKNOWN,"127.0.0.1"));
|
||||
assertTrue(TimeUnit.NANOSECONDS.toMillis(System.nanoTime()-start)<5000);
|
||||
} finally { unlock.countDown(); }
|
||||
blocker.get(3,TimeUnit.SECONDS);
|
||||
}
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM verification_send_events",Integer.class));
|
||||
assertEquals(0,jdbc.queryForObject("SELECT count(*) FROM verification_delivery_outbox",Integer.class));
|
||||
assertEquals(true,verification.sendReset(UNKNOWN,"127.0.0.1").get("sent"));
|
||||
}
|
||||
}
|
||||
+227
@@ -0,0 +1,227 @@
|
||||
package com.moyi.controlplane.auth;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.common.ApiExceptionHandler;
|
||||
import com.moyi.controlplane.security.SecretCipher;
|
||||
import jakarta.servlet.http.Cookie;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import javax.sql.DataSource;
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.jdbc.datasource.DriverManagerDataSource;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
import org.springframework.transaction.support.TransactionTemplate;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
|
||||
|
||||
@EnabledIfEnvironmentVariable(named="MOYI_AUTH_TEST_JDBC_URL", matches="jdbc:postgresql://127[.]0[.]0[.]1:[0-9]+/.+")
|
||||
class VerifiedAccountPostgresTest {
|
||||
@Configuration @EnableTransactionManagement(proxyTargetClass=true) static class Transactions {}
|
||||
private static final String ACCOUNT = "verified@example.test", CODE = "123456", PASSWORD = "synthetic-original-password";
|
||||
private final UUID invitationCreator = UUID.randomUUID();
|
||||
private final AuthService.RequestMeta meta = new AuthService.RequestMeta("127.0.0.1", "isolated-verified-workflow");
|
||||
private final ObjectMapper json = new ObjectMapper().findAndRegisterModules();
|
||||
private DriverManagerDataSource base;
|
||||
private String schema;
|
||||
private JdbcTemplate jdbc;
|
||||
private AnnotationConfigApplicationContext context;
|
||||
private AuthService auth;
|
||||
private VerifiedAccountService verified;
|
||||
private VerificationService verification;
|
||||
private PasswordHasher hasher;
|
||||
private MockMvc http;
|
||||
|
||||
@BeforeEach void setup() {
|
||||
String url = System.getenv("MOYI_AUTH_TEST_JDBC_URL");
|
||||
schema = "verified_account_" + UUID.randomUUID().toString().replace("-", "");
|
||||
base = new DriverManagerDataSource(url, "moyi_role_test", "");
|
||||
var source = new DriverManagerDataSource(url + "?currentSchema=" + schema, "moyi_role_test", "");
|
||||
Flyway.configure().dataSource(base).schemas(schema).defaultSchema(schema).load().migrate();
|
||||
jdbc = new JdbcTemplate(source); hasher = new PasswordHasher("isolated-verified-pepper");
|
||||
jdbc.update("INSERT INTO users(id,account,account_kind,password_hash,display_name,role) VALUES (?,'invitation_creator','username','not-a-login-hash','Synthetic Creator','admin')", invitationCreator);
|
||||
context = new AnnotationConfigApplicationContext(); context.register(Transactions.class);
|
||||
context.registerBean(DataSource.class, () -> source);
|
||||
context.registerBean(DataSourceTransactionManager.class, () -> new DataSourceTransactionManager(source));
|
||||
context.registerBean(AuthService.class, () -> new AuthService(jdbc, hasher, json, 168));
|
||||
context.registerBean(TotpService.class, () -> new TotpService(jdbc, new SecretCipher("isolated-totp")));
|
||||
context.registerBean(VerificationService.class, () -> new VerificationService(jdbc, mock(VerificationDispatchService.class), "isolated-verified-pepper"));
|
||||
context.registerBean(VerifiedAccountService.class); context.refresh();
|
||||
auth = context.getBean(AuthService.class); verification = context.getBean(VerificationService.class); verified = context.getBean(VerifiedAccountService.class);
|
||||
http = MockMvcBuilders.standaloneSetup(new AuthController(auth, verification, verified, 168, true))
|
||||
.setControllerAdvice(new ApiExceptionHandler()).build();
|
||||
}
|
||||
@AfterEach void cleanup() {
|
||||
if (context != null) context.close();
|
||||
if (base != null && schema != null) new JdbcTemplate(base).execute("DROP SCHEMA " + schema + " CASCADE");
|
||||
}
|
||||
private String invitation() {
|
||||
String value = "synthetic-invite-" + UUID.randomUUID();
|
||||
jdbc.update("INSERT INTO invitation_codes(id,code_hash,code_prefix,label,max_uses,used_count,created_by) VALUES (?,?,?,'test',1,0,?)", UUID.randomUUID(), AuthService.hashSecret(value), "synthetic", invitationCreator);
|
||||
return value;
|
||||
}
|
||||
private UUID user() {
|
||||
UUID id = UUID.randomUUID();
|
||||
jdbc.update("INSERT INTO users(id,account,account_kind,password_hash,display_name) VALUES (?,?,'email',?,'Test')", id, ACCOUNT, hasher.hash(PASSWORD));
|
||||
return id;
|
||||
}
|
||||
private void code(boolean reset) {
|
||||
String table = reset ? "password_reset_codes" : "registration_codes";
|
||||
String purpose = reset ? "password-reset" : "registration";
|
||||
jdbc.update("INSERT INTO " + table + "(account,code_hash,expires_at) VALUES (?,?,clock_timestamp()+interval '5 minutes')",
|
||||
ACCOUNT, AuthService.hashSecret("isolated-verified-pepper:" + purpose + ":" + ACCOUNT + ":" + CODE));
|
||||
}
|
||||
private void assertUnconsumed(boolean reset, int attempts) {
|
||||
var row = jdbc.queryForMap("SELECT consumed,attempts FROM " + (reset ? "password_reset_codes" : "registration_codes") + " WHERE account=?", ACCOUNT);
|
||||
assertEquals(false, row.get("consumed")); assertEquals(attempts, ((Number)row.get("attempts")).intValue());
|
||||
}
|
||||
private void rejectAudit(String event) {
|
||||
// Constraint is isolated to this random test schema; only synthetic event types are supplied.
|
||||
jdbc.execute("ALTER TABLE auth_audit_log ADD CONSTRAINT injected_audit_failure CHECK (event_type <> '" + event + "') NOT VALID");
|
||||
}
|
||||
|
||||
@Test void invalidInvitationKeepsTheCodeUsableForTheSameAccount() {
|
||||
code(false); String invite = invitation();
|
||||
assertEquals("INVITATION_INVALID", assertThrows(ApiException.class, () -> verified.register("invalid", ACCOUNT, PASSWORD, "Test", CODE, meta)).getCode());
|
||||
assertUnconsumed(false, 0);
|
||||
assertEquals(0, jdbc.queryForObject("SELECT count(*) FROM users WHERE account=?", Integer.class, ACCOUNT));
|
||||
assertEquals(0, jdbc.queryForObject("SELECT sum(used_count) FROM invitation_codes", Integer.class));
|
||||
var result = verified.register(invite, ACCOUNT, PASSWORD, "Test", CODE, meta);
|
||||
assertEquals(ACCOUNT, result.user().account()); assertNotNull(auth.requireUser(result.token()));
|
||||
assertEquals(1, jdbc.queryForObject("SELECT count(*) FROM invitation_redemptions", Integer.class));
|
||||
assertEquals("VERIFICATION_EXPIRED", assertThrows(ApiException.class, () -> verified.register(invite, ACCOUNT, PASSWORD, "Test", CODE, meta)).getCode());
|
||||
}
|
||||
|
||||
@Test void invalidDisplayNameAndDuplicateAccountDoNotBurnAValidCode() {
|
||||
code(false); String invite = invitation();
|
||||
assertEquals("INVALID_NAME", assertThrows(ApiException.class, () -> verified.register(invite, ACCOUNT, PASSWORD, "x".repeat(81), CODE, meta)).getCode());
|
||||
assertUnconsumed(false, 0);
|
||||
user();
|
||||
assertEquals("ACCOUNT_EXISTS", assertThrows(ApiException.class, () -> verified.register(invite, ACCOUNT, PASSWORD, "Test", CODE, meta)).getCode());
|
||||
assertUnconsumed(false, 0);
|
||||
assertEquals(0, jdbc.queryForObject("SELECT count(*) FROM invitation_redemptions", Integer.class));
|
||||
}
|
||||
|
||||
@Test void registrationAuditFailureRollsBackCodeInvitationUserAndSessionTogether() {
|
||||
code(false); String invite = invitation(); rejectAudit("user.registered");
|
||||
assertThrows(DataIntegrityViolationException.class, () -> verified.register(invite, ACCOUNT, PASSWORD, "Test", CODE, meta));
|
||||
assertUnconsumed(false, 0);
|
||||
assertEquals(0, jdbc.queryForObject("SELECT count(*) FROM users WHERE account=?", Integer.class, ACCOUNT));
|
||||
for (String table : java.util.List.of("auth_sessions", "invitation_redemptions", "auth_audit_log")) {
|
||||
assertEquals(0, jdbc.queryForObject("SELECT count(*) FROM " + table, Integer.class), table);
|
||||
}
|
||||
assertEquals(0, jdbc.queryForObject("SELECT sum(used_count) FROM invitation_codes", Integer.class));
|
||||
jdbc.execute("ALTER TABLE auth_audit_log DROP CONSTRAINT injected_audit_failure");
|
||||
assertNotNull(verified.register(invite, ACCOUNT, PASSWORD, "Test", CODE, meta).token());
|
||||
}
|
||||
|
||||
@Test void resetAuditFailureRollsBackCodeHashAndSessionRevocations() {
|
||||
UUID id = user(); code(true);
|
||||
String first = auth.login(ACCOUNT, PASSWORD, meta).token(), second = auth.login(ACCOUNT, PASSWORD, meta).token();
|
||||
String original = jdbc.queryForObject("SELECT password_hash FROM users WHERE id=?", String.class, id);
|
||||
rejectAudit("user.password_reset");
|
||||
assertThrows(DataIntegrityViolationException.class, () -> verified.resetPassword(ACCOUNT, CODE, "replacement-password", meta));
|
||||
assertUnconsumed(true, 0);
|
||||
assertEquals(original, jdbc.queryForObject("SELECT password_hash FROM users WHERE id=?", String.class, id));
|
||||
assertNotNull(auth.requireUser(first)); assertNotNull(auth.requireUser(second));
|
||||
jdbc.execute("ALTER TABLE auth_audit_log DROP CONSTRAINT injected_audit_failure");
|
||||
verified.resetPassword(ACCOUNT, CODE, "replacement-password", meta);
|
||||
assertEquals(401, assertThrows(ApiException.class, () -> auth.requireUser(first)).getStatus());
|
||||
assertEquals(401, assertThrows(ApiException.class, () -> auth.requireUser(second)).getStatus());
|
||||
assertNotNull(auth.login(ACCOUNT, "replacement-password", meta).token());
|
||||
assertEquals(1, jdbc.queryForObject("SELECT count(*) FROM auth_audit_log WHERE event_type='user.password_reset'", Integer.class));
|
||||
}
|
||||
|
||||
@ParameterizedTest @ValueSource(booleans = {false, true})
|
||||
void wrongCodeAttemptsCommitEvenWhenAnOuterCallerRollsBack(boolean reset) {
|
||||
if (reset) user(); code(reset); String invite = invitation();
|
||||
var outer = new TransactionTemplate(context.getBean(DataSourceTransactionManager.class));
|
||||
outer.executeWithoutResult(status -> {
|
||||
for (int i = 0; i < 5; i++) {
|
||||
var failure = assertThrows(ApiException.class, () -> {
|
||||
if (reset) verified.resetPassword(ACCOUNT, "000000", "replacement-password", meta);
|
||||
else verified.register(invite, ACCOUNT, PASSWORD, "Test", "000000", meta);
|
||||
});
|
||||
assertEquals("VERIFICATION_INVALID", failure.getCode());
|
||||
}
|
||||
status.setRollbackOnly();
|
||||
});
|
||||
assertUnconsumed(reset, 5);
|
||||
assertEquals(reset ? "VERIFICATION_INVALID" : "VERIFICATION_EXPIRED", assertThrows(ApiException.class, () -> {
|
||||
if (reset) verified.resetPassword(ACCOUNT, CODE, "replacement-password", meta);
|
||||
else verified.register(invite, ACCOUNT, PASSWORD, "Test", CODE, meta);
|
||||
}).getCode());
|
||||
}
|
||||
|
||||
@ParameterizedTest @ValueSource(booleans = {false, true})
|
||||
void weakNewPasswordsNeverConsumeCodes(boolean reset) {
|
||||
code(reset); String invite = invitation();
|
||||
assertEquals("WEAK_PASSWORD", assertThrows(ApiException.class, () -> {
|
||||
if (reset) verified.resetPassword(ACCOUNT, CODE, "short", meta);
|
||||
else verified.register(invite, ACCOUNT, "short", "Test", CODE, meta);
|
||||
}).getCode());
|
||||
assertUnconsumed(reset, 0);
|
||||
}
|
||||
|
||||
@Test void disabledResetAccountLeavesTheValidCodeUnconsumed() {
|
||||
UUID id = user(); code(true); jdbc.update("UPDATE users SET status='disabled' WHERE id=?", id);
|
||||
assertEquals("ACCOUNT_NOT_FOUND", assertThrows(ApiException.class, () -> verified.resetPassword(ACCOUNT, CODE, "replacement-password", meta)).getCode());
|
||||
assertUnconsumed(true, 0);
|
||||
}
|
||||
|
||||
@ParameterizedTest @ValueSource(booleans = {false, true})
|
||||
void concurrentCodeUseCompletesExactlyOneAccountOperation(boolean reset) throws Exception {
|
||||
if (reset) user(); code(reset); String invite = invitation();
|
||||
var start = new CountDownLatch(1); var results = new ArrayList<String>();
|
||||
try (var executor = Executors.newFixedThreadPool(2)) {
|
||||
var futures = new ArrayList<java.util.concurrent.Future<String>>();
|
||||
for (int i = 0; i < 2; i++) futures.add(executor.submit(() -> {
|
||||
assertTrue(start.await(3, TimeUnit.SECONDS));
|
||||
try {
|
||||
if (reset) verified.resetPassword(ACCOUNT, CODE, "replacement-password", meta);
|
||||
else verified.register(invite, ACCOUNT, PASSWORD, "Test", CODE, meta);
|
||||
return "completed";
|
||||
} catch (ApiException error) { return error.getCode(); }
|
||||
}));
|
||||
start.countDown();
|
||||
for (var future : futures) results.add(future.get(15, TimeUnit.SECONDS));
|
||||
}
|
||||
assertEquals(1, results.stream().filter("completed"::equals).count());
|
||||
assertEquals(1, results.stream().filter((reset ? "VERIFICATION_INVALID" : "VERIFICATION_EXPIRED")::equals).count());
|
||||
assertEquals(1, jdbc.queryForObject("SELECT count(*) FROM users WHERE account=?", Integer.class, ACCOUNT));
|
||||
assertEquals(1, jdbc.queryForObject("SELECT count(*) FROM auth_audit_log WHERE event_type=?", Integer.class, reset ? "user.password_reset" : "user.registered"));
|
||||
if (!reset) assertEquals(1, jdbc.queryForObject("SELECT count(*) FROM invitation_redemptions", Integer.class));
|
||||
}
|
||||
|
||||
@Test void publicResetClearsTheCookieAndRevokesTheServerSessionOnlyAfterSuccess() throws Exception {
|
||||
user(); code(true); String token = auth.login(ACCOUNT, PASSWORD, meta).token();
|
||||
http.perform(post("/api/auth/password-reset").cookie(new Cookie("moyi_session", token)).contentType("application/json")
|
||||
.content(json.writeValueAsBytes(Map.of("account", ACCOUNT, "verificationCode", "000000", "newPassword", "replacement-password"))))
|
||||
.andExpect(status().isBadRequest()).andExpect(cookie().doesNotExist("moyi_session"));
|
||||
assertNotNull(auth.requireUser(token)); assertUnconsumed(true, 1);
|
||||
http.perform(post("/api/auth/password-reset").cookie(new Cookie("moyi_session", token)).contentType("application/json")
|
||||
.content(json.writeValueAsBytes(Map.of("account", ACCOUNT, "verificationCode", CODE, "newPassword", "replacement-password"))))
|
||||
.andExpect(status().isNoContent()).andExpect(cookie().maxAge("moyi_session", 0))
|
||||
.andExpect(cookie().httpOnly("moyi_session", true)).andExpect(cookie().secure("moyi_session", true));
|
||||
assertEquals(401, assertThrows(ApiException.class, () -> auth.requireUser(token)).getStatus());
|
||||
}
|
||||
}
|
||||
+9
-1
@@ -25,7 +25,15 @@ class CatalogServiceTest {
|
||||
assertTrue(CatalogService.asyncExecutorReady("provider-bytedance", "http", "video"));
|
||||
assertTrue(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "image"));
|
||||
assertTrue(CatalogService.asyncExecutorReady("relay-provider", "OPENAI-COMPATIBLE", "video"));
|
||||
assertTrue(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "audio"));
|
||||
// Audio readiness depends on the upstream endpoint, not on the model type alone.
|
||||
assertTrue(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "audio", "tts-1"));
|
||||
assertTrue(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "audio", "gpt-4o-mini-tts-2025-03-20"));
|
||||
assertFalse(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "audio"));
|
||||
assertFalse(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "audio", "whisper-1"));
|
||||
assertFalse(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "audio", "gpt-4o-realtime-preview"));
|
||||
// Dedicated adapters only execute the task type they implement.
|
||||
assertFalse(CatalogService.asyncExecutorReady("provider-vast", "http", "video"));
|
||||
assertFalse(CatalogService.asyncExecutorReady("provider-bytedance", "http", "3d"));
|
||||
assertFalse(CatalogService.asyncExecutorReady("relay-provider", "openai-compatible", "3d"));
|
||||
assertFalse(CatalogService.asyncExecutorReady("relay-provider", "http", "video"));
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class DeferredModelTest {
|
||||
@Test void deferredRouteIsRejectedByPublicIdLegacyIdAndAliasEvenAfterPricing() {
|
||||
var catalog=mock(CatalogService.class);
|
||||
var key=Map.<String,Object>of("id","test");
|
||||
var model=Map.<String,Object>of("id","deferred","upstreamModel","SparkDesk","integrationDeferred",true,"pricing",Map.of("input",1,"output",1,"unit","CNY / 1M tokens"));
|
||||
when(catalog.listModels(false)).thenReturn(List.of(model));
|
||||
when(catalog.keyCanInvoke(key,"deferred")).thenReturn(true);
|
||||
when(catalog.activeModel(anyString())).thenReturn(null);
|
||||
when(catalog.activeModel("deferred")).thenReturn(model);
|
||||
for(String id:List.of("deferred","SparkDesk",GatewayModelIds.publicId(model))) assertEquals("MODEL_ONBOARDING_DEFERRED",assertThrows(ApiException.class,()->GatewayModelIds.resolve(catalog,key,id)).getCode());
|
||||
}
|
||||
}
|
||||
+95
@@ -0,0 +1,95 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@EnabledIfEnvironmentVariable(named="MOYI_GATEWAY_TEST_REDIS_PORT", matches="[0-9]{2,5}")
|
||||
class FairAdmissionRedisTest {
|
||||
private LettuceConnectionFactory factory;
|
||||
private StringRedisTemplate redis;
|
||||
private final String prefix = "moyi:test:" + UUID.randomUUID() + ":";
|
||||
private final Map<String,Object> model = Map.of("id", "fair-model", "maxConcurrency", 1);
|
||||
|
||||
@BeforeEach void setup() {
|
||||
factory = new LettuceConnectionFactory("127.0.0.1", Integer.parseInt(System.getenv("MOYI_GATEWAY_TEST_REDIS_PORT")));
|
||||
factory.afterPropertiesSet(); factory.start();
|
||||
redis = new StringRedisTemplate(factory); redis.afterPropertiesSet();
|
||||
}
|
||||
|
||||
@AfterEach void cleanup() {
|
||||
if (redis != null) redis.delete(redis.keys(prefix + "*"));
|
||||
if (factory != null) factory.destroy();
|
||||
}
|
||||
|
||||
private GatewayGuard guard(long waitMs) { return new GatewayGuard(redis, 60, 300, 1, 4, waitMs, 10, prefix); }
|
||||
|
||||
@Test void newRequestCannotOvertakeAnEarlierWaiter() throws Exception {
|
||||
var guard = guard(5000);
|
||||
var holder = guard.acquire(model, UUID.randomUUID(), 5000);
|
||||
var earlier = CompletableFuture.supplyAsync(() -> guard.acquire(model, UUID.randomUUID(), 5000));
|
||||
// The earlier waiter is queued before the slot is released.
|
||||
long until = System.nanoTime() + TimeUnit.SECONDS.toNanos(2);
|
||||
while (zcard("order") == 0 && System.nanoTime() < until) Thread.sleep(5);
|
||||
assertEquals(1L, zcard("order"));
|
||||
holder.close();
|
||||
// A newcomer that does not wait finds the free slot reserved for the earlier waiter.
|
||||
var newcomer = assertThrows(GatewayLimitException.class, () -> guard(0).acquire(model, UUID.randomUUID(), 0));
|
||||
assertEquals("MODEL_BUSY", newcomer.getCode());
|
||||
try (var admitted = earlier.get(3, TimeUnit.SECONDS)) {
|
||||
assertEquals(0L, zcard("order"));
|
||||
assertEquals(0L, zcard("waiting"));
|
||||
}
|
||||
}
|
||||
|
||||
@Test void timedOutWaiterLeavesTheQueueAndDoesNotBlockLaterRequests() throws Exception {
|
||||
var holder = guard(5000).acquire(model, UUID.randomUUID(), 5000);
|
||||
assertThrows(GatewayLimitException.class, () -> guard(30).acquire(model, UUID.randomUUID(), 30));
|
||||
assertEquals(0L, zcard("order"));
|
||||
assertEquals(0L, zcard("waiting"));
|
||||
holder.close();
|
||||
try (var next = guard(0).acquire(model, UUID.randomUUID(), 0)) { assertNotNull(next); }
|
||||
}
|
||||
|
||||
@Test void waiterBlockedByItsOwnKeyDoesNotHoldTheModelQueue() throws Exception {
|
||||
UUID busyKey = UUID.randomUUID();
|
||||
var keyGuard = new GatewayGuard(redis, 60, 300, 2, 1, 5000, 10, prefix);
|
||||
var unlimitedModel = Map.<String,Object>of("id", "fair-model", "maxConcurrency", 2);
|
||||
var keyHolder = keyGuard.acquire(unlimitedModel, busyKey, 5000);
|
||||
var blockedByKey = CompletableFuture.supplyAsync(() -> keyGuard.acquire(unlimitedModel, busyKey, 200));
|
||||
Thread.sleep(50);
|
||||
// Another key may use the remaining model slot while the first key is at its own limit.
|
||||
try (var other = keyGuard.acquire(unlimitedModel, UUID.randomUUID(), 0)) { assertNotNull(other); }
|
||||
var failure = assertThrows(java.util.concurrent.ExecutionException.class, () -> blockedByKey.get(3, TimeUnit.SECONDS));
|
||||
assertEquals("KEY_BUSY", ((GatewayLimitException) failure.getCause()).getCode());
|
||||
keyHolder.close();
|
||||
}
|
||||
|
||||
@Test void renewalExtendsOnlyLeasesStillHeld() throws Exception {
|
||||
var admission = guard(0).acquire(model, UUID.randomUUID(), 0);
|
||||
String slots = prefix + "gateway:slots:model:fair-model";
|
||||
String token = redis.opsForZSet().range(slots, 0, 0).iterator().next();
|
||||
double before = redis.opsForZSet().score(slots, token);
|
||||
assertTrue(admission.renew(1_800_000));
|
||||
assertTrue(redis.opsForZSet().score(slots, token) > before + 1_000_000);
|
||||
redis.opsForZSet().remove(slots, token);
|
||||
// A lost lease is reported, never silently recreated on top of another caller's capacity.
|
||||
assertFalse(admission.renew(1_800_000));
|
||||
assertNull(redis.opsForZSet().score(slots, token));
|
||||
admission.close();
|
||||
assertFalse(admission.renew(1_800_000));
|
||||
}
|
||||
|
||||
private long zcard(String kind) {
|
||||
Long value = redis.opsForZSet().zCard(prefix + "gateway:" + kind + ":model:fair-model");
|
||||
return value == null ? 0 : value;
|
||||
}
|
||||
}
|
||||
+19
-9
@@ -7,6 +7,9 @@ import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
||||
import com.moyi.controlplane.task.AsyncTaskService;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestRiskEngine;
|
||||
import com.moyi.controlplane.gateway.request.ApiRequestRiskPolicy;
|
||||
import com.moyi.controlplane.gateway.request.GatewayRequestExecutionService;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
@@ -20,6 +23,13 @@ import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class GatewayDeploymentTest {
|
||||
private static GatewayController controller(CatalogService catalog, OpenAiCompatibleProvider provider,
|
||||
GatewayGuard guard, ChatBillingService billing, ObjectMapper json) {
|
||||
doReturn(mock(GatewayGuard.Admission.class)).when(guard).acquire(anyMap(), any(UUID.class), anyLong());
|
||||
var requests = new GatewayRequestExecutionService(billing,
|
||||
new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()));
|
||||
return new GatewayController(catalog, provider, guard, requests, json);
|
||||
}
|
||||
@Test void failedTaskClaimReleasesRedisLeaseAfterDatabaseRollback() {
|
||||
var redis=mock(StringRedisTemplate.class);doReturn(1L).when(redis).execute(any(RedisScript.class),anyList(),any(Object[].class));
|
||||
var policies=mock(EnterprisePolicies.class);UUID key=UUID.randomUUID(),user=UUID.randomUUID();
|
||||
@@ -124,7 +134,7 @@ class GatewayDeploymentTest {
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.listModels(false)).thenReturn(List.of(route));
|
||||
when(catalog.keyCanInvoke(key, "route-1")).thenReturn(true);
|
||||
GatewayController controller = new GatewayController(catalog, mock(OpenAiCompatibleProvider.class), guard, mock(ChatBillingService.class), new ObjectMapper());
|
||||
GatewayController controller = controller(catalog, mock(OpenAiCompatibleProvider.class), guard, mock(ChatBillingService.class), new ObjectMapper());
|
||||
|
||||
Map<String, Object> response = controller.models("Bearer test-key");
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -163,7 +173,7 @@ class GatewayDeploymentTest {
|
||||
when(catalog.activeModel("glm-5--apihub")).thenReturn(null);
|
||||
when(catalog.listModels(false)).thenReturn(List.of(route));
|
||||
when(catalog.keyCanInvoke(key, "route-readable")).thenReturn(true);
|
||||
GatewayController controller = new GatewayController(catalog, mock(OpenAiCompatibleProvider.class), guard, mock(ChatBillingService.class), new ObjectMapper());
|
||||
GatewayController controller = controller(catalog, mock(OpenAiCompatibleProvider.class), guard, mock(ChatBillingService.class), new ObjectMapper());
|
||||
assertSame(route, controller.resolveModel(key, "glm-5--apihub--" + tag("route-readable")));
|
||||
verify(catalog).keyCanInvoke(key, "route-readable");
|
||||
}
|
||||
@@ -236,7 +246,7 @@ class GatewayDeploymentTest {
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.activeModel("model-real")).thenReturn(Map.of("id", "model-real", "providerId", "provider-deepseek", "mode", "sync", "type", "text"));
|
||||
when(catalog.keyCanInvoke(key, "model-real")).thenReturn(true);
|
||||
GatewayController controller = new GatewayController(catalog, provider, guard, mock(ChatBillingService.class),new ObjectMapper());
|
||||
GatewayController controller = controller(catalog, provider, guard, mock(ChatBillingService.class),new ObjectMapper());
|
||||
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "model-real"),null,new MockHttpServletResponse()));
|
||||
assertEquals("PROVIDER_NOT_CONFIGURED", error.getCode());
|
||||
assertEquals(503, error.getStatus());
|
||||
@@ -251,9 +261,9 @@ class GatewayDeploymentTest {
|
||||
when(catalog.activeModel("mock")).thenReturn(Map.of("id", "mock", "providerId", "provider-mock", "mode", "sync", "type", "text"));
|
||||
when(catalog.keyCanInvoke(key, "mock")).thenReturn(true);
|
||||
var billing=mock(ChatBillingService.class);
|
||||
when(billing.reserve(anyMap(),anyMap(),any(),any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(),UUID.fromString(key.get("userId").toString()),ChatMeter.rates(null,true)));
|
||||
when(billing.reserve(anyMap(),anyMap(),any(),any(),any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(),UUID.fromString(key.get("userId").toString()),ChatMeter.rates(null,true)));
|
||||
when(billing.settle(any(),anyMap(),anyInt(),any())).thenReturn(java.math.BigDecimal.ZERO);
|
||||
GatewayController controller = new GatewayController(catalog, mock(OpenAiCompatibleProvider.class), mock(GatewayGuard.class), billing,new ObjectMapper());
|
||||
GatewayController controller = controller(catalog, mock(OpenAiCompatibleProvider.class), mock(GatewayGuard.class), billing,new ObjectMapper());
|
||||
assertEquals(200, controller.chatCompletions("Bearer test-key", Map.of("model", "mock","messages",List.of(Map.of("role","user","content","test"))),null,new MockHttpServletResponse()).getStatusCode().value());
|
||||
}
|
||||
|
||||
@@ -276,10 +286,10 @@ class GatewayDeploymentTest {
|
||||
"usage", Map.of("prompt_tokens", 2, "completion_tokens", 16, "total_tokens", 18)
|
||||
));
|
||||
when(provider.chat(eq("provider-relay"), eq("reasoning-model"), eq("reasoning-route"), anyMap())).thenReturn(upstreamResponse);
|
||||
when(billing.reserve(anyMap(), anyMap(), any(), isNull())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
|
||||
when(billing.reserve(anyMap(), anyMap(), any(), isNull(), any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
|
||||
when(billing.settle(any(), anyMap(), anyInt(), isNull())).thenReturn(java.math.BigDecimal.ZERO);
|
||||
|
||||
GatewayController controller = new GatewayController(catalog, provider, guard, billing, new ObjectMapper());
|
||||
GatewayController controller = controller(catalog, provider, guard, billing, new ObjectMapper());
|
||||
var response = controller.chatCompletions("Bearer test-key", Map.of("model", "reasoning-route", "messages", List.of(Map.of("role", "user", "content", "hello"))), null, new MockHttpServletResponse());
|
||||
assertEquals(200, response.getStatusCode().value());
|
||||
Map<?, ?> choice = (Map<?, ?>)((List<?>)response.getBody().get("choices")).getFirst();
|
||||
@@ -307,8 +317,8 @@ class GatewayDeploymentTest {
|
||||
"choices", List.of(Map.of("index", 0, "message", Map.of("role", "assistant", "content", ""), "finish_reason", "stop"))
|
||||
));
|
||||
when(provider.chat(eq("provider-relay"), eq("empty-model"), eq("empty-route"), anyMap())).thenReturn(upstreamResponse);
|
||||
when(billing.reserve(anyMap(), anyMap(), any(), isNull())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
|
||||
GatewayController controller = new GatewayController(catalog, provider, guard, billing, new ObjectMapper());
|
||||
when(billing.reserve(anyMap(), anyMap(), any(), isNull(), any())).thenReturn(new ChatBillingService.Ticket(UUID.randomUUID(), UUID.fromString(userId), ChatMeter.rates(null, true)));
|
||||
GatewayController controller = controller(catalog, provider, guard, billing, new ObjectMapper());
|
||||
|
||||
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "empty-route", "messages", List.of(Map.of("role", "user", "content", "hello"))), null, new MockHttpServletResponse()));
|
||||
assertEquals("UPSTREAM_RESPONSE_INVALID", error.getCode());
|
||||
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
|
||||
import java.util.UUID;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
|
||||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.transaction.support.TransactionSynchronization;
|
||||
import org.springframework.transaction.support.TransactionSynchronizationManager;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
@EnabledIfEnvironmentVariable(named="MOYI_GATEWAY_TEST_REDIS_PORT", matches="[0-9]{2,5}")
|
||||
class TaskLeaseRedisTest {
|
||||
private LettuceConnectionFactory factory;
|
||||
private StringRedisTemplate redis;
|
||||
private GatewayGuard guard;
|
||||
private final UUID userId = UUID.randomUUID(), keyId = UUID.randomUUID();
|
||||
private final String prefix = "moyi:test:" + UUID.randomUUID() + ":";
|
||||
private String bucket() { return prefix + "gateway:slots:user:" + userId; }
|
||||
|
||||
@BeforeEach void setup() {
|
||||
factory = new LettuceConnectionFactory("127.0.0.1", Integer.parseInt(System.getenv("MOYI_GATEWAY_TEST_REDIS_PORT")));
|
||||
factory.afterPropertiesSet(); factory.start();
|
||||
redis = new StringRedisTemplate(factory); redis.afterPropertiesSet();
|
||||
guard = new GatewayGuard(redis, 60, 300, 4, prefix);
|
||||
var policies = mock(EnterprisePolicies.class);
|
||||
when(policies.taskLimit(keyId)).thenReturn(new EnterprisePolicies.UserLimit(userId, 120, 1));
|
||||
guard.setEnterprisePolicies(policies);
|
||||
}
|
||||
|
||||
@AfterEach void cleanup() {
|
||||
if (TransactionSynchronizationManager.isSynchronizationActive()) TransactionSynchronizationManager.clearSynchronization();
|
||||
if (redis != null) redis.delete(bucket());
|
||||
if (factory != null) factory.destroy();
|
||||
}
|
||||
|
||||
@Test void renewalDoesNotConsumeAnotherSlotAndDifferentTaskIsBlocked() {
|
||||
assertTrue(guard.tryTaskUserSlot(keyId, "original-task", 180000));
|
||||
assertTrue(guard.tryTaskUserSlot(keyId, "original-task", 300000));
|
||||
assertEquals(1L, redis.opsForZSet().zCard(bucket()));
|
||||
assertFalse(guard.tryTaskUserSlot(keyId, "different-task", 180000));
|
||||
assertTrue(redis.getExpire(bucket(), java.util.concurrent.TimeUnit.MILLISECONDS) > 250000);
|
||||
}
|
||||
|
||||
@Test void rolledBackRenewalKeepsThePreexistingLease() {
|
||||
assertTrue(guard.tryTaskUserSlot(keyId, "original-task", 180000));
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
assertTrue(guard.tryTaskUserSlot(keyId, "original-task", 300000));
|
||||
for (var synchronization : TransactionSynchronizationManager.getSynchronizations()) synchronization.afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
|
||||
TransactionSynchronizationManager.clearSynchronization();
|
||||
assertNotNull(redis.opsForZSet().score(bucket(), "original-task"));
|
||||
}
|
||||
|
||||
@Test void rolledBackNewClaimRemovesOnlyItsOwnLease() {
|
||||
TransactionSynchronizationManager.initSynchronization();
|
||||
assertTrue(guard.tryTaskUserSlot(keyId, "new-task", 180000));
|
||||
for (var synchronization : TransactionSynchronizationManager.getSynchronizations()) synchronization.afterCompletion(TransactionSynchronization.STATUS_ROLLED_BACK);
|
||||
TransactionSynchronizationManager.clearSynchronization();
|
||||
assertNull(redis.opsForZSet().score(bucket(), "new-task"));
|
||||
}
|
||||
}
|
||||
+96
@@ -0,0 +1,96 @@
|
||||
package com.moyi.controlplane.gateway.request;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.gateway.GatewayController;
|
||||
import com.moyi.controlplane.gateway.GatewayGuard;
|
||||
import com.moyi.controlplane.gateway.NativeGatewayController;
|
||||
import com.moyi.controlplane.provider.OpenAiCompatibleProvider;
|
||||
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class ApiRequestEntryPointsTest {
|
||||
private final UUID userId=UUID.randomUUID(), keyId=UUID.randomUUID();
|
||||
private final Map<String,Object> key=Map.of("id",keyId.toString(),"userId",userId.toString());
|
||||
private final CatalogService catalog=mock(CatalogService.class);
|
||||
private final GatewayGuard guard=mock(GatewayGuard.class);
|
||||
private final ChatBillingService billing=mock(ChatBillingService.class);
|
||||
private final OpenAiCompatibleProvider provider=mock(OpenAiCompatibleProvider.class);
|
||||
private final AtomicReference<ApiRequestContext> observed=new AtomicReference<>();
|
||||
private final GatewayRequestExecutionService requests=new GatewayRequestExecutionService(billing,
|
||||
new ApiRequestRiskPolicy(context -> {
|
||||
observed.set(context);
|
||||
return ApiRequestRiskDecision.deny(new ApiRequestRiskSignal(ApiRequestRiskSignal.Action.DENY,
|
||||
"test.entry-block","RISK_BLOCKED","Blocked",0));
|
||||
}));
|
||||
|
||||
@Test void publicChatIgnoresSpoofedPlaygroundHeaderAndDenialNeverAcquiresOrReserves() {
|
||||
Map<String,Object> model=Map.of("id","chat-route","providerId","provider-mock","type","text","mode","sync");
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.activeModel("chat-route")).thenReturn(model);
|
||||
when(catalog.keyCanInvoke(key,"chat-route")).thenReturn(true);
|
||||
var controller=new GatewayController(catalog,provider,guard,requests,new ObjectMapper());
|
||||
var request=http("/v1/chat/completions");
|
||||
|
||||
assertEquals("RISK_BLOCKED",assertThrows(ApiException.class,() -> controller.chatCompletions("Bearer test-key",
|
||||
Map.of("model","chat-route","messages",List.of(Map.of("role","user","content","hello"))),null,
|
||||
request,new MockHttpServletResponse())).getCode());
|
||||
|
||||
assertEquals(ApiRequestMetadata.Source.API,observed.get().metadata().source());
|
||||
assertEquals("198.51.100.7",observed.get().metadata().clientIp());
|
||||
verify(guard).checkRate(keyId,false);
|
||||
verify(guard,never()).acquire(anyMap(),any(UUID.class),anyLong());
|
||||
verifyNoInteractions(billing,provider);
|
||||
}
|
||||
|
||||
@Test void nativePublicAndPlaygroundBothEnterRiskBoundaryWithEndpointOwnedSource() {
|
||||
Map<String,Object> model=Map.of("id","embedding-route","providerId","provider-a","upstreamModel","embedding-model","type","embedding","mode","sync");
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.activeModel("embedding-route")).thenReturn(model);
|
||||
when(catalog.keyCanInvoke(key,"embedding-route")).thenReturn(true);
|
||||
when(provider.isConfigured("provider-a","embedding-route","embedding-model")).thenReturn(true);
|
||||
var controller=new NativeGatewayController(catalog,guard,requests,provider,new ObjectMapper());
|
||||
var request=http("/v1/embeddings");
|
||||
request.addHeader("Authorization","Bearer test-key");
|
||||
Map<String,Object> input=Map.of("model","embedding-route","input","hello");
|
||||
|
||||
assertEquals("RISK_BLOCKED",assertThrows(ApiException.class,
|
||||
() -> controller.invoke(input,request,new MockHttpServletResponse())).getCode());
|
||||
assertEquals(ApiRequestProtocol.EMBEDDINGS,observed.get().protocol());
|
||||
assertEquals(ApiRequestMetadata.Source.API,observed.get().metadata().source());
|
||||
|
||||
assertEquals("RISK_BLOCKED",assertThrows(ApiException.class,
|
||||
() -> controller.executeForKey("embeddings",input,false,key,request,new MockHttpServletResponse(),
|
||||
ApiRequestMetadata.playground(request.getRemoteAddr()))).getCode());
|
||||
assertEquals(ApiRequestMetadata.Source.PLAYGROUND,observed.get().metadata().source());
|
||||
verify(guard,never()).acquire(anyMap(),any(UUID.class),anyLong());
|
||||
verify(provider,never()).invoke(anyString(),anyString(),anyString(),anyMap(),anyString());
|
||||
verifyNoInteractions(billing);
|
||||
}
|
||||
|
||||
@Test void malformedRequestsStillConsumeTheirEarlyRateBudget() {
|
||||
var controller=new NativeGatewayController(catalog,guard,requests,provider,new ObjectMapper());
|
||||
assertThrows(ApiException.class,() -> controller.executeForKey("embeddings",Map.of(),false,key,
|
||||
http("/v1/embeddings"),new MockHttpServletResponse()));
|
||||
verify(guard).checkRate(keyId,false);
|
||||
verifyNoInteractions(billing);
|
||||
}
|
||||
|
||||
private MockHttpServletRequest http(String path) {
|
||||
var request=new MockHttpServletRequest("POST",path);
|
||||
request.setRemoteAddr("198.51.100.7");
|
||||
request.addHeader("X-Moyi-Source","playground");
|
||||
request.addHeader("X-Forwarded-For","203.0.113.99"); // Adapter never parses this untrusted header itself.
|
||||
return request;
|
||||
}
|
||||
}
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
package com.moyi.controlplane.gateway.request;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.Optional;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ApiRequestModuleConfigurationTest {
|
||||
private final ApplicationContextRunner runner = new ApplicationContextRunner()
|
||||
.withUserConfiguration(ApiRequestModuleConfiguration.class);
|
||||
private final ApiRequestContext request = new ApiRequestContext(UUID.randomUUID(), UUID.randomUUID(),
|
||||
ApiRequestProtocol.EMBEDDINGS, "public", "route", false, 12, 0,
|
||||
ApiRequestMetadata.fromHttp("198.51.100.7"), System.nanoTime());
|
||||
|
||||
@Test void noRulesStartsSafelyAndDefaultsToShadow() {
|
||||
runner.run(context -> {
|
||||
assertNull(context.getStartupFailure());
|
||||
assertEquals(ApiRequestRiskDecision.Action.ALLOW, context.getBean(ApiRequestRiskEngine.class).evaluate(request).action());
|
||||
});
|
||||
}
|
||||
|
||||
@Test void installedRuleIsShadowedByDefaultAndEnforcedOnlyWhenConfigured() {
|
||||
var configured = runner.withBean("blockedRule", ApiRequestRiskRule.class, () -> rule("test.block", 0));
|
||||
configured.run(context -> assertEquals(ApiRequestRiskDecision.Action.SHADOW,
|
||||
context.getBean(ApiRequestRiskEngine.class).evaluate(request).action()));
|
||||
configured.withPropertyValues("moyi.gateway.risk-mode=enforce").run(context -> assertEquals(
|
||||
ApiRequestRiskDecision.Action.DENY, context.getBean(ApiRequestRiskEngine.class).evaluate(request).action()));
|
||||
}
|
||||
|
||||
@Test void throttleAggregationHonorsTheLongestRetryAndRuleFailureClosesOnlyInEnforce() {
|
||||
var engine = new CompositeApiRequestRiskEngine(java.util.List.of(rule("test.short", 2), rule("test.long", 30)),
|
||||
CompositeApiRequestRiskEngine.Mode.ENFORCE);
|
||||
assertEquals(30, engine.evaluate(request).retryAfterSeconds());
|
||||
ApiRequestRiskRule broken = new ApiRequestRiskRule() {
|
||||
public String id() { return "test.broken"; }
|
||||
public Optional<ApiRequestRiskSignal> evaluate(ApiRequestContext context) { throw new IllegalStateException(); }
|
||||
};
|
||||
assertEquals(ApiRequestRiskDecision.Action.SHADOW, new CompositeApiRequestRiskEngine(java.util.List.of(broken),
|
||||
CompositeApiRequestRiskEngine.Mode.SHADOW).evaluate(request).action());
|
||||
assertEquals(ApiRequestRiskDecision.Action.UNAVAILABLE, new CompositeApiRequestRiskEngine(java.util.List.of(broken),
|
||||
CompositeApiRequestRiskEngine.Mode.ENFORCE).evaluate(request).action());
|
||||
assertFalse(request.metadata().toString().contains("198.51.100.7"));
|
||||
}
|
||||
|
||||
@Test void invalidModeDoesNotSilentlyDisableRisk() {
|
||||
runner.withPropertyValues("moyi.gateway.risk-mode=typo").run(context -> assertNotNull(context.getStartupFailure()));
|
||||
}
|
||||
|
||||
private ApiRequestRiskRule rule(String id, int retry) {
|
||||
return new ApiRequestRiskRule() {
|
||||
public String id() { return id; }
|
||||
public Optional<ApiRequestRiskSignal> evaluate(ApiRequestContext context) {
|
||||
return Optional.of(new ApiRequestRiskSignal(retry == 0 ? ApiRequestRiskSignal.Action.DENY : ApiRequestRiskSignal.Action.THROTTLE,
|
||||
id, "RISK_BLOCKED", "Blocked", retry));
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
+217
@@ -0,0 +1,217 @@
|
||||
package com.moyi.controlplane.gateway.request;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.gateway.ChatMeter;
|
||||
import com.moyi.controlplane.gateway.GatewayGuard;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.task.AsyncTaskService;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import com.moyi.controlplane.wallet.ChatBillingService;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
class GatewayRequestExecutionServiceTest {
|
||||
private final UUID userId = UUID.randomUUID();
|
||||
private final UUID keyId = UUID.randomUUID();
|
||||
private final ApiRequestContext context = new ApiRequestContext(userId, keyId,
|
||||
ApiRequestProtocol.CHAT_COMPLETIONS, "public-model", "route-1", false,
|
||||
256, 128, ApiRequestMetadata.fromHttp("127.0.0.1"), System.nanoTime());
|
||||
private final Map<String, Object> key = Map.of("id", keyId.toString(), "userId", userId.toString());
|
||||
private final Map<String, Object> model = Map.of("id", "route-1");
|
||||
private final ChatMeter.Request normalized = new ChatMeter.Request(Map.of("model", "public-model"), 256, 128);
|
||||
|
||||
@Test
|
||||
void denyHappensBeforeAdmissionAndWalletReservation() {
|
||||
ChatBillingService billing = mock(ChatBillingService.class);
|
||||
var service = new GatewayRequestExecutionService(billing,
|
||||
new ApiRequestRiskPolicy(ignored -> ApiRequestRiskDecision.deny(
|
||||
new ApiRequestRiskSignal(ApiRequestRiskSignal.Action.DENY, "test.block", "RISK_BLOCKED", "Blocked", 0))));
|
||||
AtomicBoolean admitted = new AtomicBoolean();
|
||||
|
||||
ApiException error = assertThrows(ApiException.class, () -> service.execute(context, key, model, normalized,
|
||||
null, () -> { admitted.set(true); return mock(GatewayGuard.Admission.class); },
|
||||
ticket -> "unreachable", failure -> new ApiException("CALL_FAILED", "failed", 502),
|
||||
ApiException::getCode));
|
||||
|
||||
assertEquals(403, error.getStatus());
|
||||
assertFalse(admitted.get());
|
||||
verifyNoInteractions(billing);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shadowSignalDoesNotChangeRequestExecution() {
|
||||
ChatBillingService billing = mock(ChatBillingService.class);
|
||||
var ticket = new ChatBillingService.Ticket(UUID.randomUUID(), userId, null);
|
||||
when(billing.reserve(key, model, normalized, null, context.metadata())).thenReturn(ticket);
|
||||
var service = new GatewayRequestExecutionService(billing,
|
||||
new ApiRequestRiskPolicy(ignored -> ApiRequestRiskDecision.shadow(List.of("test.observe"))));
|
||||
ApiRequestAdmission admission = mock(ApiRequestAdmission.class);
|
||||
|
||||
String result = service.execute(context, key, model, normalized, null, () -> admission,
|
||||
ignored -> "executed", failure -> new ApiException("CALL_FAILED", "failed", 502), ApiException::getCode);
|
||||
|
||||
assertEquals("executed", result);
|
||||
verify(billing).reserve(key, model, normalized, null, context.metadata());
|
||||
verify(admission).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void enforceModeFailsClosedWhenRiskEngineThrowsBeforeAdmission() {
|
||||
ChatBillingService billing = mock(ChatBillingService.class);
|
||||
var service = new GatewayRequestExecutionService(billing,
|
||||
new ApiRequestRiskPolicy(ignored -> { throw new IllegalStateException("private detail"); }));
|
||||
AtomicBoolean admitted = new AtomicBoolean();
|
||||
|
||||
ApiException error = assertThrows(ApiException.class, () -> service.execute(context, key, model, normalized,
|
||||
null, () -> { admitted.set(true); return mock(GatewayGuard.Admission.class); },
|
||||
ticket -> "unreachable", failure -> new ApiException("CALL_FAILED", "failed", 502),
|
||||
ApiException::getCode));
|
||||
|
||||
assertEquals("RISK_ENGINE_UNAVAILABLE", error.getCode());
|
||||
assertEquals(503, error.getStatus());
|
||||
assertFalse(admitted.get());
|
||||
assertFalse(error.getMessage().contains("private detail"));
|
||||
verifyNoInteractions(billing);
|
||||
}
|
||||
|
||||
@Test
|
||||
void admissionLeaseClosesWhenReservationFails() {
|
||||
ChatBillingService billing = mock(ChatBillingService.class);
|
||||
when(billing.reserve(key, model, normalized, null, context.metadata())).thenThrow(new ApiException("INSUFFICIENT_BALANCE", "low", 402));
|
||||
var service = new GatewayRequestExecutionService(billing, new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()));
|
||||
ApiRequestAdmission admission = mock(ApiRequestAdmission.class);
|
||||
|
||||
assertEquals("INSUFFICIENT_BALANCE", assertThrows(ApiException.class, () -> service.execute(context, key, model,
|
||||
normalized, null, () -> admission, ticket -> "unreachable",
|
||||
failure -> new ApiException("CALL_FAILED", "failed", 502), ApiException::getCode)).getCode());
|
||||
|
||||
verify(admission).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void runningCallRenewsItsHoldAndLeaseUntilTheWorkReturns() throws Exception {
|
||||
ChatBillingService billing = mock(ChatBillingService.class);
|
||||
var ticket = new ChatBillingService.Ticket(UUID.randomUUID(), userId, null);
|
||||
when(billing.reserve(key, model, normalized, null, context.metadata())).thenReturn(ticket);
|
||||
var service = new GatewayRequestExecutionService(billing, new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()), 20);
|
||||
ApiRequestAdmission admission = mock(ApiRequestAdmission.class);
|
||||
when(admission.renew(anyLong())).thenReturn(true);
|
||||
try {
|
||||
assertEquals("done", service.execute(context, key, model, normalized, null, () -> admission,
|
||||
ignored -> { Thread.sleep(150); return "done"; }, failure -> new ApiException("CALL_FAILED", "failed", 502), ApiException::getCode));
|
||||
verify(billing, atLeast(2)).renew(eq(ticket.id()), anyLong());
|
||||
verify(admission, atLeast(2)).renew(anyLong());
|
||||
// Renewal stops with the call; a finished request must not keep its hold alive.
|
||||
clearInvocations(billing);
|
||||
Thread.sleep(100);
|
||||
verify(billing, never()).renew(any(), anyLong());
|
||||
} finally { service.shutdown(); }
|
||||
}
|
||||
|
||||
@Test
|
||||
void workFailureIsRecordedAndAdmissionLeaseCloses() {
|
||||
ChatBillingService billing = mock(ChatBillingService.class);
|
||||
var ticket = new ChatBillingService.Ticket(UUID.randomUUID(), userId, null);
|
||||
when(billing.reserve(key, model, normalized, null, context.metadata())).thenReturn(ticket);
|
||||
var service = new GatewayRequestExecutionService(billing, new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()));
|
||||
ApiRequestAdmission admission = mock(ApiRequestAdmission.class);
|
||||
|
||||
assertEquals("CALL_INTERRUPTED", service.execute(context, key, model, normalized, null, () -> admission,
|
||||
ignored -> { throw new IllegalStateException("failure"); },
|
||||
failure -> new ApiException("CALL_INTERRUPTED", "failed", 502), ApiException::getCode));
|
||||
|
||||
verify(billing).fail(eq(ticket), eq("CALL_INTERRUPTED"), eq("failed"), anyInt());
|
||||
verify(admission).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
void asyncTaskRiskDenialPrecedesQueueAndWalletDatabaseWork() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
CatalogService catalog = mock(CatalogService.class);
|
||||
GatewayGuard guard = mock(GatewayGuard.class);
|
||||
UsageAuditService usage = mock(UsageAuditService.class);
|
||||
var route = new java.util.LinkedHashMap<String,Object>();
|
||||
route.put("id", "async-route");
|
||||
route.put("providerId", "provider-mock");
|
||||
route.put("type", "3d");
|
||||
route.put("mode", "async");
|
||||
route.put("executionReady", true);
|
||||
var apiKey = Map.<String,Object>of("id", keyId.toString(), "userId", userId.toString());
|
||||
when(catalog.listModels(false)).thenReturn(List.<Map<String,Object>>of(route));
|
||||
when(catalog.activeModel("async-route")).thenReturn(route);
|
||||
when(catalog.keyCanInvoke(apiKey, "async-route")).thenReturn(true);
|
||||
when(catalog.keyOwnerId(keyId)).thenReturn(userId);
|
||||
var risk = new ApiRequestRiskPolicy(ignored -> ApiRequestRiskDecision.deny(
|
||||
new ApiRequestRiskSignal(ApiRequestRiskSignal.Action.DENY, "test.async-block", "RISK_BLOCKED", "Blocked", 0)));
|
||||
var tasks = new AsyncTaskService(jdbc, new ObjectMapper(), catalog, 100, 4, 4, 3_600_000,
|
||||
false, guard, usage, null, null, risk);
|
||||
|
||||
ApiException error = assertThrows(ApiException.class,
|
||||
() -> tasks.createForKey(apiKey, "3d", Map.of("model", "async-route", "prompt", "generate"), ApiRequestMetadata.playground("127.0.0.1")));
|
||||
|
||||
assertEquals("RISK_BLOCKED", error.getCode());
|
||||
verify(guard).checkRate(keyId,false);
|
||||
verifyNoMoreInteractions(guard);
|
||||
verifyNoInteractions(jdbc, usage);
|
||||
}
|
||||
|
||||
@Test
|
||||
void publicMetadataCannotBePromotedToPlaygroundByRequestHeaderAndContextStringOmitsIp() {
|
||||
var publicMetadata = ApiRequestMetadata.fromHttp("198.51.100.7");
|
||||
var playgroundMetadata = ApiRequestMetadata.playground("198.51.100.7");
|
||||
|
||||
assertEquals(ApiRequestMetadata.Source.API, publicMetadata.source());
|
||||
assertEquals(ApiRequestMetadata.Source.PLAYGROUND, playgroundMetadata.source());
|
||||
assertFalse(context.toString().contains("127.0.0.1"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void compositeRiskModeReportsSignalsInShadowAndAppliesThemOnlyInEnforce() {
|
||||
ApiRequestRiskRule rule = new ApiRequestRiskRule() {
|
||||
@Override public String id() { return "test.rule"; }
|
||||
@Override public java.util.Optional<ApiRequestRiskSignal> evaluate(ApiRequestContext ignored) {
|
||||
return java.util.Optional.of(new ApiRequestRiskSignal(ApiRequestRiskSignal.Action.DENY,
|
||||
id(), "RISK_BLOCKED", "Blocked", 0));
|
||||
}
|
||||
};
|
||||
|
||||
var shadow = new CompositeApiRequestRiskEngine(List.of(rule), CompositeApiRequestRiskEngine.Mode.SHADOW)
|
||||
.evaluate(context);
|
||||
var enforce = new CompositeApiRequestRiskEngine(List.of(rule), CompositeApiRequestRiskEngine.Mode.ENFORCE)
|
||||
.evaluate(context);
|
||||
|
||||
assertEquals(ApiRequestRiskDecision.Action.SHADOW, shadow.action());
|
||||
assertEquals(List.of("test.rule"), shadow.ruleIds());
|
||||
assertEquals(ApiRequestRiskDecision.Action.DENY, enforce.action());
|
||||
assertEquals("RISK_BLOCKED", enforce.code());
|
||||
}
|
||||
|
||||
@Test
|
||||
void settlementPersistenceFailureDoesNotMarkCompletedProviderWorkAsFailed() {
|
||||
ChatBillingService billing = mock(ChatBillingService.class);
|
||||
var ticket = new ChatBillingService.Ticket(UUID.randomUUID(), userId, null);
|
||||
when(billing.reserve(key, model, normalized, null, context.metadata())).thenReturn(ticket);
|
||||
doThrow(new org.springframework.dao.DataIntegrityViolationException("ledger unavailable"))
|
||||
.when(billing).settle(eq(ticket), anyMap(), anyInt(), any());
|
||||
var service = new GatewayRequestExecutionService(billing, new ApiRequestRiskPolicy(ApiRequestRiskEngine.allowAll()));
|
||||
ApiRequestAdmission admission = mock(ApiRequestAdmission.class);
|
||||
|
||||
String result = service.execute(context, key, model, normalized, null, () -> admission,
|
||||
current -> {
|
||||
service.settle(current, Map.of("usage", Map.of("prompt_tokens", 1, "completion_tokens", 1)), 2, null);
|
||||
return "unexpected";
|
||||
}, failure -> new ApiException("CALL_INTERRUPTED", "failed", 502), ApiException::getCode);
|
||||
|
||||
assertEquals("SETTLEMENT_PENDING", result);
|
||||
verify(billing, never()).fail(any(), anyString(), anyString(), anyInt());
|
||||
verify(admission).close();
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package com.moyi.controlplane.gateway.request;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
/** Lightweight source dependency gates, not a substitute for design review or a sandbox. */
|
||||
class ModuleDependencyTest {
|
||||
private final Path root = Path.of(System.getProperty("basedir", "."), "src/main/java/com/moyi/controlplane");
|
||||
|
||||
@Test void requestApplicationBoundaryDoesNotOwnHttpDatabaseOrProviderCredentials() throws Exception {
|
||||
disallow(root.resolve("gateway/request"), List.of("java.net.http", "org.springframework.jdbc", ".security.SecretCipher", ".provider.credentials", ".task.AsyncProviderClient"));
|
||||
}
|
||||
@Test void transportDoesNotOwnRiskRoutingOrTheWallet() throws Exception {
|
||||
disallow(root.resolve("provider/transport"), List.of(".gateway.", ".wallet.", ".auth.", ".task.", "org.springframework.jdbc", ".security.SecretCipher"));
|
||||
}
|
||||
@Test void taskSettlementDoesNotCallAnUpstream() throws Exception {
|
||||
assertTrue(Files.exists(root.resolve("task/TaskSettlementService.java")));
|
||||
disallow(root.resolve("task/TaskSettlementService.java"), List.of("java.net.http", "AsyncProviderClient", "OpenAiCompatibleProvider"));
|
||||
}
|
||||
@Test void mediaInputDoesNotOwnCredentialsRoutingOrBilling() throws Exception {
|
||||
disallow(root.resolve("provider/media"), List.of(".gateway.", ".wallet.", ".auth.", ".task.",
|
||||
".provider.credentials", "org.springframework.jdbc", ".security.SecretCipher", "java.net.http.HttpClient"));
|
||||
assertFalse(Files.readString(root.resolve("task/AsyncProviderClient.java")).contains("InetAddress"), "DNS policy belongs to the media boundary");
|
||||
}
|
||||
private void disallow(Path path, List<String> forbidden) throws Exception {
|
||||
assertTrue(Files.exists(path), "Expected source boundary: " + path);
|
||||
try (var paths = Files.isDirectory(path) ? Files.walk(path) : java.util.stream.Stream.of(path)) {
|
||||
for (Path file : paths.filter(p -> p.toString().endsWith(".java")).toList()) {
|
||||
String source = Files.readString(file);
|
||||
for (String dependency : forbidden) assertFalse(source.contains(dependency), file + " crosses boundary via " + dependency);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+25
@@ -112,6 +112,31 @@ class OpenAiCompatibleProviderHttpTest {
|
||||
assertEquals(List.of("Bearer route-key", "Bearer model-key"), authorization);
|
||||
}
|
||||
|
||||
@Test
|
||||
void dynamicDatabaseChannelKeepsRotationAcrossRepeatedResolution() throws Exception {
|
||||
var authorization = new ArrayList<String>();
|
||||
server.createContext("/v1/chat/completions", exchange -> {
|
||||
authorization.add(exchange.getRequestHeaders().getFirst("Authorization"));
|
||||
exchange.getRequestBody().readAllBytes();
|
||||
write(exchange, 200, "{\"usage\":{\"prompt_tokens\":1,\"completion_tokens\":1}}");
|
||||
});
|
||||
server.start();
|
||||
var jdbc = org.mockito.Mockito.mock(JdbcTemplate.class);
|
||||
var cipher = new SecretCipher("local-test-only");
|
||||
org.mockito.Mockito.when(jdbc.queryForMap(org.mockito.ArgumentMatchers.anyString(), org.mockito.ArgumentMatchers.eq("dynamic-provider")))
|
||||
.thenReturn(Map.of("code", "dynamic-code", "base_url", "http://127.0.0.1:" + server.getAddress().getPort() + "/v1",
|
||||
"credential_ciphertext", cipher.encrypt("first-key")));
|
||||
environment.withProperty("MOYI_UPSTREAM_API_KEYS_DYNAMIC_CODE", "second-key");
|
||||
var dynamic = new OpenAiCompatibleProvider(json, environment, jdbc, cipher);
|
||||
try {
|
||||
dynamic.chat("dynamic-provider", "test-model", "route-no-override", textInput(128));
|
||||
dynamic.chat("dynamic-provider", "test-model", "route-no-override", textInput(128));
|
||||
dynamic.chat("dynamic-provider", "test-model", "route-no-override", textInput(128));
|
||||
} finally { dynamic.close(); }
|
||||
|
||||
assertEquals(List.of("Bearer first-key", "Bearer second-key", "Bearer first-key"), authorization);
|
||||
}
|
||||
|
||||
private Map<String, Object> textInput(int maxTokens) {
|
||||
var input = new LinkedHashMap<String, Object>();
|
||||
input.put("messages", List.of(Map.of("role", "user", "content", "Reply with OK.")));
|
||||
|
||||
+85
@@ -0,0 +1,85 @@
|
||||
package com.moyi.controlplane.provider;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.security.SecretCipher;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/** Long streams survive while bytes flow; a silent upstream ends at the idle window, not the total deadline. */
|
||||
class StreamDeadlineTest {
|
||||
private OpenAiCompatibleProvider provider(HttpServer server, int total, int idle) {
|
||||
var env = new MockEnvironment()
|
||||
.withProperty("moyi.providers.deepseek.api-key", "local-fake-key")
|
||||
.withProperty("moyi.providers.deepseek.base-url", "http://127.0.0.1:" + server.getAddress().getPort() + "/v1")
|
||||
.withProperty("moyi.gateway.upstream-timeout-seconds", Integer.toString(total))
|
||||
.withProperty("moyi.gateway.upstream-idle-timeout-seconds", Integer.toString(idle));
|
||||
return new OpenAiCompatibleProvider(new ObjectMapper(), env, mock(JdbcTemplate.class), new SecretCipher("local-test-only"));
|
||||
}
|
||||
|
||||
private HttpServer sse(long gapMs, int events, boolean finish) throws Exception {
|
||||
var server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.createContext("/v1/chat/completions", exchange -> {
|
||||
exchange.getResponseHeaders().set("Content-Type", "text/event-stream");
|
||||
exchange.sendResponseHeaders(200, 0);
|
||||
try (var out = exchange.getResponseBody()) {
|
||||
for (int i = 0; i < events; i++) {
|
||||
out.write(("data: {\"i\":" + i + "}\n\n").getBytes(StandardCharsets.UTF_8));
|
||||
out.flush();
|
||||
Thread.sleep(gapMs);
|
||||
}
|
||||
if (finish) out.write("data: [DONE]\n\n".getBytes(StandardCharsets.UTF_8));
|
||||
else Thread.sleep(5_000);
|
||||
} catch (Exception ignored) { }
|
||||
});
|
||||
server.start();
|
||||
return server;
|
||||
}
|
||||
|
||||
@Test void streamLongerThanTheIdleWindowSucceedsWhileEventsKeepArriving() throws Exception {
|
||||
var server = sse(400, 8, true);
|
||||
var provider = provider(server, 10, 1);
|
||||
try (var stream = provider.stream("provider-deepseek", "model", Map.of("messages", List.of()))) {
|
||||
var chunks = new ArrayList<Map<String,Object>>();
|
||||
long started = System.nanoTime();
|
||||
stream.events(chunks::add);
|
||||
assertEquals(8, chunks.size());
|
||||
assertTrue(System.nanoTime() - started > 2_000_000_000L, "the stream outlived the 1s idle window");
|
||||
} finally { provider.close(); server.stop(0); }
|
||||
}
|
||||
|
||||
@Test void silentUpstreamTimesOutAtTheIdleWindow() throws Exception {
|
||||
var server = sse(0, 1, false);
|
||||
var provider = provider(server, 10, 1);
|
||||
try (var stream = provider.stream("provider-deepseek", "model", Map.of("messages", List.of()))) {
|
||||
long started = System.nanoTime();
|
||||
var error = assertThrows(ApiException.class, () -> stream.events(chunk -> { }));
|
||||
assertEquals("UPSTREAM_PROVIDER_TIMEOUT", error.getCode());
|
||||
long elapsedMs = (System.nanoTime() - started) / 1_000_000;
|
||||
assertTrue(elapsedMs < 4_000, "ended by the idle window, not the 10s total: " + elapsedMs);
|
||||
} finally { provider.close(); server.stop(0); }
|
||||
}
|
||||
|
||||
@Test void totalDeadlineIsCappedAndIdleNeverExceedsIt() throws Exception {
|
||||
var server = sse(0, 1, true);
|
||||
var env = new MockEnvironment()
|
||||
.withProperty("moyi.providers.deepseek.api-key", "local-fake-key")
|
||||
.withProperty("moyi.providers.deepseek.base-url", "http://127.0.0.1:" + server.getAddress().getPort() + "/v1")
|
||||
.withProperty("moyi.gateway.upstream-timeout-seconds", "999999")
|
||||
.withProperty("moyi.gateway.upstream-idle-timeout-seconds", "999999");
|
||||
var provider = new OpenAiCompatibleProvider(new ObjectMapper(), env, mock(JdbcTemplate.class), new SecretCipher("local-test-only"));
|
||||
try {
|
||||
assertEquals(OpenAiCompatibleProvider.MAX_TIMEOUT_SECONDS, org.springframework.test.util.ReflectionTestUtils.getField(provider, "timeoutSeconds"));
|
||||
assertEquals(1000L * OpenAiCompatibleProvider.MAX_TIMEOUT_SECONDS, org.springframework.test.util.ReflectionTestUtils.getField(provider, "idleTimeoutMs"));
|
||||
} finally { provider.close(); server.stop(0); }
|
||||
}
|
||||
}
|
||||
+64
@@ -0,0 +1,64 @@
|
||||
package com.moyi.controlplane.provider.credentials;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class UpstreamCredentialResolverTest {
|
||||
@Test
|
||||
void scopedPoolUsesRouteThenProviderModelAndNeverGlobalModelKeys() {
|
||||
var env = new MockEnvironment()
|
||||
.withProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_ROUTE_A", "route-first | route-second | route-first")
|
||||
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_RELAY_CODE_MODEL_1", "model-first|model-second")
|
||||
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_MODEL_1", "unscoped-key");
|
||||
var resolver = new UpstreamCredentialResolver(env);
|
||||
|
||||
assertEquals(List.of("route-first", "route-second"), resolver.scopedKeys("relay-code", "route-a", "model-1"));
|
||||
assertEquals(List.of("model-first", "model-second"), resolver.scopedKeys("relay-code", "route-b", "model-1"));
|
||||
assertTrue(resolver.scopedKeys("other-provider", "route-b", "model-1").isEmpty());
|
||||
assertTrue(resolver.scopedKeys("relay-code", null, null).isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void channelPoolPreservesFallbackAndExplicitAliasOrderAndDeduplicates() {
|
||||
var resolver = new UpstreamCredentialResolver(new MockEnvironment()
|
||||
.withProperty("MOYI_UPSTREAM_API_KEYS_PROVIDER_A", "a|shared")
|
||||
.withProperty("MOYI_UPSTREAM_API_KEYS_CHANNEL_CODE", "b|shared"));
|
||||
|
||||
assertEquals(List.of("fallback", "a", "shared", "b"),
|
||||
resolver.providerKeys(" fallback ", "provider-a", "channel-code"));
|
||||
assertEquals(List.of("fallback", "b", "shared", "a"),
|
||||
resolver.providerKeys("fallback", "channel-code", "provider-a"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void parsingHandlesBlankEntriesAndReturnsImmutablePools() {
|
||||
assertEquals(List.of("a", "b"), UpstreamCredentialResolver.parseKeys(" a | |b|a| "));
|
||||
assertTrue(UpstreamCredentialResolver.parseKeys(null).isEmpty());
|
||||
assertTrue(UpstreamCredentialResolver.parseKeys(" || ").isEmpty());
|
||||
assertThrows(UnsupportedOperationException.class, () -> UpstreamCredentialResolver.parseKeys("a").add("b"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rotationUsesPersistentCallerCursorAndSurvivesIntegerOverflow() {
|
||||
var cursor = new AtomicInteger(Integer.MAX_VALUE);
|
||||
var keys = List.of("a", "b", "c");
|
||||
|
||||
assertEquals(keys.get(Math.floorMod(Integer.MAX_VALUE, keys.size())), UpstreamCredentialResolver.nextKey(keys, cursor));
|
||||
assertEquals(keys.get(Math.floorMod(Integer.MIN_VALUE, keys.size())), UpstreamCredentialResolver.nextKey(keys, cursor));
|
||||
assertEquals(Integer.MIN_VALUE + 1, cursor.get());
|
||||
assertEquals("UPSTREAM_KEY_NOT_AVAILABLE", assertThrows(ApiException.class,
|
||||
() -> UpstreamCredentialResolver.nextKey(List.of(), cursor)).getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void officialFallbackKeysAreReadOnlyWhenRequestedExplicitly() {
|
||||
var resolver = new UpstreamCredentialResolver(new MockEnvironment().withProperty("TRIPO_API_KEY", "tripo-a|tripo-b"));
|
||||
|
||||
assertTrue(resolver.providerKeys("", "provider-tripo").isEmpty());
|
||||
assertEquals(List.of("tripo-a", "tripo-b"), resolver.environmentKeys("TRIPO_API_KEY"));
|
||||
}
|
||||
}
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
package com.moyi.controlplane.provider.media;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
|
||||
class MediaAssetNormalizerTest {
|
||||
@Test void imageSignaturesSupplyMissingMimeWithoutGuessingArbitraryBase64() {
|
||||
assertEquals("image/png", MediaAssetNormalizer.normalize(Map.of("b64_json", "iVBORw0KGgo="), "image").get("mimeType"));
|
||||
assertEquals("image/jpeg", MediaAssetNormalizer.normalize(Map.of("base64", "/9j/4AAQSkZJRg=="), "image").get("mimeType"));
|
||||
assertNull(MediaAssetNormalizer.normalize(Map.of("base64", "bm90IGFuIGltYWdl"), "image"));
|
||||
assertNull(MediaAssetNormalizer.normalize(Map.of("base64", "%%%", "format", "png"), "image"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings={"javascript:alert(1)", "file:///etc/passwd", "https://user:password@cdn.example.test/a.mp4",
|
||||
"data:image/svg+xml;base64,PHN2Zz48L3N2Zz4=", "data:text/html;base64,PGh0bWw+", "https:missing-host",
|
||||
"https://cdn.example.test/\\result", "generation complete"})
|
||||
void invalidReferencesAreNotDeliveredFiles(String value) {
|
||||
assertNull(MediaAssetNormalizer.normalize(Map.of("url", value), null));
|
||||
assertNull(MediaAssetNormalizer.normalize(value, null));
|
||||
}
|
||||
|
||||
@Test void primaryMediaMustBelongToTheTaskRatherThanItsThumbnail() {
|
||||
assertNull(MediaAssetNormalizer.normalize(Map.of("image_url", "https://cdn.example.test/frame.png"), "video"));
|
||||
assertNull(MediaAssetNormalizer.normalize(Map.of("url", "data:image/png;base64,iVBORw0KGgo="), "video"));
|
||||
assertNull(MediaAssetNormalizer.normalize(Map.of("url", "https://cdn.example.test/file", "mimeType", "image/png"), "video"));
|
||||
assertNull(MediaAssetNormalizer.normalize(Map.of("url", "https://cdn.example.test/frame.png?sig=1"), "video"));
|
||||
assertNull(MediaAssetNormalizer.normalize(Map.of("video_url", "https://cdn.example.test/frame.png"), "video"));
|
||||
var video = MediaAssetNormalizer.normalize(Map.of("video", Map.of("url", "https://cdn.example.test/file?sig=ok"),
|
||||
"last_frame_url", "https://cdn.example.test/frame.png"), "video");
|
||||
assertEquals("https://cdn.example.test/file?sig=ok", video.get("url"));
|
||||
assertEquals("https://cdn.example.test/frame.png", video.get("previewUrl"));
|
||||
}
|
||||
|
||||
@Test void metadataIsWhitelistedAndNestedAudioRetainsItsMime() {
|
||||
var result = MediaAssetNormalizer.normalize(Map.of("audio_url", Map.of("base64", "ZmFrZQ==", "format", "opus"),
|
||||
"credentials", "secret", "error", "private detail", "usage", Map.of("output_tokens", 12), "credits_consumed", 2), "audio");
|
||||
assertEquals("data:audio/ogg;base64,ZmFrZQ==", result.get("url"));
|
||||
assertEquals(Map.of("output_tokens", 12), result.get("usage"));
|
||||
assertEquals(2, result.get("creditsConsumed"));
|
||||
assertFalse(result.containsKey("credentials"));
|
||||
assertFalse(result.containsKey("error"));
|
||||
}
|
||||
|
||||
@Test void nestedReferencesAreDepthBounded() {
|
||||
Map<String,Object> nested = Map.of("url", "https://cdn.example.test/result.mp4");
|
||||
for (int i = 0; i < 20; i++) nested = Map.of("url", nested);
|
||||
assertNull(MediaAssetNormalizer.normalize(nested, "video"));
|
||||
}
|
||||
}
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
package com.moyi.controlplane.provider.media;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import com.moyi.controlplane.provider.transport.BoundedHttpTransport;
|
||||
import com.moyi.controlplane.provider.transport.PinnedHttpsTransport;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpHeaders;
|
||||
import java.time.Duration;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class MediaDownloaderTest {
|
||||
private final PinnedHttpsTransport transport = mock(PinnedHttpsTransport.class);
|
||||
private final MediaDownloader downloader = new MediaDownloader(transport, host -> { throw new AssertionError("Unexpected network"); });
|
||||
|
||||
@Test void normalizesImagesAndPreservesBoundedTransportContract() throws Exception {
|
||||
byte[] bytes = {1, 2, 3};
|
||||
when(transport.get(any(), any(), any(), anyInt())).thenReturn(response(200, "IMAGE/PNG; charset=utf-8", bytes));
|
||||
var image = downloader.download("https://media.test/a?signature=synthetic");
|
||||
assertArrayEquals(bytes, image.bytes()); assertEquals("image/png", image.contentType()); assertEquals("input.png", image.filename());
|
||||
verify(transport).get(eq(URI.create("https://media.test/a?signature=synthetic")), any(), eq(Duration.ofSeconds(15)), eq(8 * 1024 * 1024));
|
||||
assertFalse(image.toString().contains("synthetic"));
|
||||
}
|
||||
|
||||
@Test void invalidUrlsFailBeforeTransport() {
|
||||
assertEquals("INVALID_MEDIA_URL", assertThrows(ApiException.class, () -> downloader.download("https://user:secret@media.test/a")).getCode());
|
||||
verifyNoInteractions(transport);
|
||||
}
|
||||
|
||||
@ParameterizedTest @CsvSource({"BODY_TOO_LARGE,MEDIA_TOO_LARGE", "INVALID_ADDRESS,INVALID_MEDIA_URL", "TIMEOUT,MEDIA_FETCH_FAILED", "INTERRUPTED,MEDIA_FETCH_FAILED", "BUSY,MEDIA_FETCH_FAILED", "IO,MEDIA_FETCH_FAILED"})
|
||||
void transportFailuresExposeOnlySafeDomainCodes(BoundedHttpTransport.Reason reason, String code) throws Exception {
|
||||
var failure = mock(BoundedHttpTransport.Failure.class); when(failure.reason()).thenReturn(reason);
|
||||
when(transport.get(any(), any(), any(), anyInt())).thenThrow(failure);
|
||||
var error = assertThrows(ApiException.class, () -> downloader.download("https://media.test/a?signature=private"));
|
||||
assertEquals(code, error.getCode()); assertFalse(error.getMessage().contains("private")); assertNull(error.getCause());
|
||||
}
|
||||
|
||||
@ParameterizedTest @CsvSource({"302,image/png,MEDIA_FETCH_FAILED", "404,image/png,MEDIA_FETCH_FAILED", "200,text/html,MEDIA_TYPE_UNSUPPORTED", "200,image/invalid:type,MEDIA_TYPE_UNSUPPORTED"})
|
||||
void redirectsErrorsAndUnsupportedTypesAreRejected(int status, String type, String code) throws Exception {
|
||||
when(transport.get(any(), any(), any(), anyInt())).thenReturn(response(status, type, new byte[] {1}));
|
||||
assertEquals(code, assertThrows(ApiException.class, () -> downloader.download("https://media.test/a")).getCode());
|
||||
}
|
||||
|
||||
@Test void emptyBodyIsRejected() throws Exception {
|
||||
when(transport.get(any(), any(), any(), anyInt())).thenReturn(response(200, "image/png", new byte[0]));
|
||||
assertEquals("MEDIA_TOO_LARGE", assertThrows(ApiException.class, () -> downloader.download("https://media.test/a")).getCode());
|
||||
}
|
||||
|
||||
private BoundedHttpTransport.Response response(int status, String type, byte[] body) {
|
||||
return new BoundedHttpTransport.Response(status, HttpHeaders.of(Map.of("Content-Type", List.of(type)), (key, value) -> true), body);
|
||||
}
|
||||
}
|
||||
+65
@@ -0,0 +1,65 @@
|
||||
package com.moyi.controlplane.provider.media;
|
||||
|
||||
import com.moyi.controlplane.provider.transport.PinnedHttpsTransport;
|
||||
import java.net.InetAddress;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PublicMediaAddressPolicyTest {
|
||||
private final PublicMediaAddressPolicy policy = new PublicMediaAddressPolicy();
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {
|
||||
"0.0.0.0", "0.255.255.255", "10.0.0.1", "127.0.0.1", "127.255.255.254",
|
||||
"100.64.0.1", "100.127.255.254", "169.254.169.254", "172.16.0.1", "172.31.255.254",
|
||||
"192.0.0.1", "192.0.0.9", "192.0.2.1", "192.88.99.1", "192.168.1.1",
|
||||
"198.18.0.1", "198.19.255.254", "198.51.100.1", "203.0.113.1", "224.0.0.1",
|
||||
"239.255.255.255", "240.0.0.1", "255.255.255.255", "168.63.129.16",
|
||||
"::", "::1", "::127.0.0.1", "::ffff:127.0.0.1", "::ffff:169.254.169.254",
|
||||
"fc00::1", "fd00::1", "fe80::1", "fec0::1", "ff02::1", "64:ff9b::a00:1", "64:ff9b:1::1",
|
||||
"100::1", "2001::1", "2001:1ff:ffff::1", "2001:2::1", "2001:20::1", "2001:db8::1",
|
||||
"2002:7f00:1::1", "2002:0808:0808::1", "3ffe::1", "3fff::1", "3fff:fff:ffff::1", "4000::1"
|
||||
})
|
||||
void rejectsNonGlobalSpecialAndTranslationTargets(String literal) throws Exception {
|
||||
InetAddress address = InetAddress.getByName(literal);
|
||||
assertFalse(policy.isPublic(address), literal);
|
||||
assertThrows(PinnedHttpsTransport.AddressRejectedException.class, () -> policy.requirePublic(new InetAddress[] {address}));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"1.1.1.1", "8.8.8.8", "100.63.255.254", "100.128.0.1", "172.15.255.254", "172.32.0.1",
|
||||
"192.0.1.1", "198.17.255.254", "198.20.0.1", "223.255.255.254", "2606:4700:4700::1111", "2001:4860:4860::8888", "3fff:1000::1"})
|
||||
void permitsOrdinaryGlobalAddressesWithoutMakingInternetRequests(String literal) throws Exception {
|
||||
var address = InetAddress.getByName(literal);
|
||||
assertTrue(policy.isPublic(address), literal);
|
||||
assertArrayEquals(new InetAddress[] {address}, policy.requirePublic(new InetAddress[] {address}));
|
||||
}
|
||||
|
||||
@Test void rejectsTheWholeMixedAnswerSetInEitherOrder() throws Exception {
|
||||
var external = InetAddress.getByName("8.8.8.8");
|
||||
var internal = InetAddress.getByName("fd00::1");
|
||||
assertThrows(PinnedHttpsTransport.AddressRejectedException.class, () -> policy.requirePublic(new InetAddress[] {external, internal}));
|
||||
assertThrows(PinnedHttpsTransport.AddressRejectedException.class, () -> policy.requirePublic(new InetAddress[] {internal, external}));
|
||||
assertThrows(PinnedHttpsTransport.AddressRejectedException.class, () -> policy.requirePublic(new InetAddress[0]));
|
||||
assertThrows(PinnedHttpsTransport.AddressRejectedException.class, () -> policy.requirePublic(new InetAddress[] {null}));
|
||||
assertThrows(PinnedHttpsTransport.AddressRejectedException.class, () -> policy.requirePublic(new InetAddress[17]));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings = {"http://example.test/a", "file:///etc/passwd", "data:image/png,a", "//example.test/a",
|
||||
"https://user:secret@example.test/a", "https://example.test/a#fragment", "https://example.test:0/a", "https://example.test:65536/a",
|
||||
"https://[fe80::1%25eth0]/a", "https://example.test\\@127.0.0.1/a", "https://example.test/%0d%0a/x\r\n", "https:///a", ""})
|
||||
void rejectsAmbiguousOrNonHttpsUris(String value) {
|
||||
var failure = assertThrows(IllegalArgumentException.class, () -> policy.requireUri(value));
|
||||
assertFalse(failure.getMessage().contains("secret"));
|
||||
assertNull(failure.getCause());
|
||||
}
|
||||
|
||||
@Test void permitsSignedHttpsUrlsAndExplicitHttpsPortsWithoutRewritingAuthority() {
|
||||
String value = "https://media.example.test:8443/a.png?signature=synthetic";
|
||||
assertEquals(value, policy.requireUri(value).toString());
|
||||
assertEquals("[2606:4700:4700::1111]", policy.requireUri("https://[2606:4700:4700::1111]/a").getHost());
|
||||
}
|
||||
}
|
||||
+113
@@ -0,0 +1,113 @@
|
||||
package com.moyi.controlplane.provider.transport;
|
||||
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class BoundedHttpTransportTest {
|
||||
private HttpServer server;
|
||||
private final BoundedHttpTransport transport = new BoundedHttpTransport();
|
||||
private final CountDownLatch release = new CountDownLatch(1);
|
||||
@BeforeEach void setup() throws Exception { server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); }
|
||||
@AfterEach void cleanup() { release.countDown(); server.stop(0); }
|
||||
private HttpRequest request(String path, long timeoutMs) { return HttpRequest.newBuilder(URI.create("http://127.0.0.1:" + server.getAddress().getPort() + path)).timeout(Duration.ofMillis(timeoutMs)).GET().build(); }
|
||||
|
||||
@Test void returnsBoundedBytesWithoutExposingBodyInDiagnostics() throws Exception {
|
||||
server.createContext("/", exchange -> {
|
||||
byte[] bytes = "private-provider-result".getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(200, bytes.length);
|
||||
try (var output = exchange.getResponseBody()) { output.write(bytes); }
|
||||
}); server.start();
|
||||
var response = transport.send(request("/", 2000), 1024);
|
||||
assertEquals("private-provider-result", new String(response.body(), StandardCharsets.UTF_8));
|
||||
assertFalse(response.toString().contains("private-provider-result"));
|
||||
}
|
||||
|
||||
@Test void oversizedChunkedResponseIsCancelledBeforeUnboundedBuffering() {
|
||||
var calls = new AtomicInteger();
|
||||
server.createContext("/", exchange -> {
|
||||
calls.incrementAndGet(); exchange.sendResponseHeaders(200, 0);
|
||||
try (var output = exchange.getResponseBody()) { for (int i = 0; i < 100; i++) output.write(new byte[4096]); }
|
||||
catch (java.io.IOException expectedAfterCancellation) { }
|
||||
}); server.start();
|
||||
var failure = assertThrows(BoundedHttpTransport.Failure.class, () -> transport.send(request("/", 2000), 8192));
|
||||
assertEquals(BoundedHttpTransport.Reason.BODY_TOO_LARGE, failure.reason());
|
||||
assertEquals(200, failure.observedStatus()); assertEquals(1, calls.get());
|
||||
}
|
||||
|
||||
@Test void declaredOversizeIsRejectedWithoutWaitingForTheBody() {
|
||||
server.createContext("/", exchange -> {
|
||||
exchange.sendResponseHeaders(200, 1_000_000);
|
||||
try { release.await(3, TimeUnit.SECONDS); } catch (InterruptedException error) { Thread.currentThread().interrupt(); }
|
||||
exchange.close();
|
||||
}); server.start();
|
||||
var failure = assertThrows(BoundedHttpTransport.Failure.class, () -> transport.send(request("/", 1000), 8192));
|
||||
assertEquals(BoundedHttpTransport.Reason.BODY_TOO_LARGE, failure.reason());
|
||||
}
|
||||
|
||||
@Test void totalDeadlineIncludesBodyReadsAfterHeaders() {
|
||||
var calls = new AtomicInteger();
|
||||
server.createContext("/", exchange -> {
|
||||
calls.incrementAndGet(); exchange.sendResponseHeaders(200, 0);
|
||||
try (var output = exchange.getResponseBody()) {
|
||||
output.write('x'); output.flush();
|
||||
try { release.await(3, TimeUnit.SECONDS); } catch (InterruptedException error) { Thread.currentThread().interrupt(); }
|
||||
} catch (java.io.IOException expectedAfterCancellation) { }
|
||||
}); server.start();
|
||||
long start = System.nanoTime();
|
||||
var failure = assertThrows(BoundedHttpTransport.Failure.class, () -> transport.send(request("/", 200), 8192));
|
||||
assertEquals(BoundedHttpTransport.Reason.TIMEOUT, failure.reason());
|
||||
assertTrue(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start) < 2000);
|
||||
assertEquals(1, calls.get());
|
||||
}
|
||||
|
||||
@Test void errorBodiesUseASmallerLimitAndRetainOnlyObservedStatus() {
|
||||
server.createContext("/", exchange -> { exchange.sendResponseHeaders(401, 1_000_000); exchange.close(); }); server.start();
|
||||
var failure = assertThrows(BoundedHttpTransport.Failure.class, () -> transport.send(request("/?private-query", 1000), 8 * 1024 * 1024));
|
||||
assertEquals(BoundedHttpTransport.Reason.BODY_TOO_LARGE, failure.reason());
|
||||
assertEquals(401, failure.observedStatus());
|
||||
assertFalse(failure.toString().contains("private-query"));
|
||||
}
|
||||
|
||||
@Test void redirectsAreReturnedAndNeverFollowed() throws Exception {
|
||||
var redirected = new AtomicInteger();
|
||||
server.createContext("/start", exchange -> { exchange.getResponseHeaders().set("Location", "/internal"); exchange.sendResponseHeaders(302, -1); exchange.close(); });
|
||||
server.createContext("/internal", exchange -> { redirected.incrementAndGet(); exchange.sendResponseHeaders(200, -1); exchange.close(); }); server.start();
|
||||
assertEquals(302, transport.send(request("/start", 2000), 1024).statusCode());
|
||||
assertEquals(0, redirected.get());
|
||||
}
|
||||
|
||||
@Test void interruptionCancelsTheAttemptAndRestoresInterruptFlag() throws Exception {
|
||||
var arrived = new CountDownLatch(1); var result = new AtomicReference<BoundedHttpTransport.Reason>(); var interrupted = new AtomicReference<Boolean>();
|
||||
server.createContext("/", exchange -> {
|
||||
exchange.sendResponseHeaders(200, 0); arrived.countDown();
|
||||
try { release.await(5, TimeUnit.SECONDS); } catch (InterruptedException error) { Thread.currentThread().interrupt(); }
|
||||
exchange.close();
|
||||
}); server.start();
|
||||
var worker = new Thread(() -> {
|
||||
try { transport.send(request("/", 5000), 1024); }
|
||||
catch (BoundedHttpTransport.Failure failure) { result.set(failure.reason()); interrupted.set(Thread.currentThread().isInterrupted()); }
|
||||
}); worker.setDaemon(true); worker.start();
|
||||
try {
|
||||
assertTrue(arrived.await(2, TimeUnit.SECONDS)); worker.interrupt(); worker.join(2000);
|
||||
assertFalse(worker.isAlive()); assertEquals(BoundedHttpTransport.Reason.INTERRUPTED, result.get()); assertEquals(true, interrupted.get());
|
||||
} finally { release.countDown(); worker.interrupt(); }
|
||||
}
|
||||
|
||||
@Test void callersCannotOmitDeadlinesOrRequestUnboundedResponses() {
|
||||
var request = HttpRequest.newBuilder(URI.create("http://127.0.0.1:1/")).GET().build();
|
||||
assertThrows(IllegalArgumentException.class, () -> transport.send(request, 1024));
|
||||
assertThrows(IllegalArgumentException.class, () -> transport.send(request, Integer.MAX_VALUE));
|
||||
}
|
||||
}
|
||||
+279
@@ -0,0 +1,279 @@
|
||||
package com.moyi.controlplane.provider.transport;
|
||||
|
||||
import com.sun.net.httpserver.HttpsConfigurator;
|
||||
import com.sun.net.httpserver.HttpsServer;
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.KeyStore;
|
||||
import java.time.Duration;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.SynchronousQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import javax.net.ssl.ExtendedSSLSession;
|
||||
import javax.net.ssl.KeyManagerFactory;
|
||||
import javax.net.ssl.SNIHostName;
|
||||
import javax.net.ssl.SSLContext;
|
||||
import javax.net.ssl.TrustManagerFactory;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class PinnedHttpsTransportTest {
|
||||
@TempDir static Path temporary;
|
||||
private static SSLContext serverTls, trustedTls;
|
||||
private HttpsServer server;
|
||||
private ExecutorService workers;
|
||||
private PinnedHttpsTransport transport;
|
||||
private final CountDownLatch release = new CountDownLatch(1);
|
||||
|
||||
@BeforeAll static void createEphemeralTestCertificate() throws Exception {
|
||||
// No committed key, no trust-all TLS: mint a .test certificate and trust only this test key.
|
||||
String password = java.util.UUID.randomUUID().toString();
|
||||
Path store = temporary.resolve("media-test.p12");
|
||||
var process = new ProcessBuilder(Path.of(System.getProperty("java.home"), "bin", "keytool").toString(),
|
||||
"-genkeypair", "-alias", "test", "-keyalg", "RSA", "-keysize", "2048", "-validity", "2",
|
||||
"-dname", "CN=media.test", "-ext", "SAN=dns:media.test", "-storetype", "PKCS12",
|
||||
"-keystore", store.toString(), "-storepass", password, "-keypass", password, "-noprompt")
|
||||
.redirectErrorStream(true).start();
|
||||
assertTrue(process.waitFor(15, TimeUnit.SECONDS));
|
||||
assertEquals(0, process.exitValue(), () -> "Unable to create ephemeral TLS fixture");
|
||||
var keys = KeyStore.getInstance("PKCS12");
|
||||
try (var input = Files.newInputStream(store)) { keys.load(input, password.toCharArray()); }
|
||||
var kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
|
||||
kmf.init(keys, password.toCharArray());
|
||||
serverTls = SSLContext.getInstance("TLS");
|
||||
serverTls.init(kmf.getKeyManagers(), null, null);
|
||||
var trust = KeyStore.getInstance(KeyStore.getDefaultType()); trust.load(null, null);
|
||||
trust.setCertificateEntry("test", keys.getCertificate("test"));
|
||||
var tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm()); tmf.init(trust);
|
||||
trustedTls = SSLContext.getInstance("TLS"); trustedTls.init(null, tmf.getTrustManagers(), null);
|
||||
}
|
||||
|
||||
@BeforeEach void setup() throws Exception {
|
||||
server = HttpsServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
server.setHttpsConfigurator(new HttpsConfigurator(serverTls));
|
||||
workers = new ThreadPoolExecutor(0, 2, 5, TimeUnit.SECONDS, new SynchronousQueue<>(),
|
||||
runnable -> { var thread = new Thread(runnable, "test-pinned-get"); thread.setDaemon(true); return thread; });
|
||||
transport = new PinnedHttpsTransport(trustedTls, workers);
|
||||
}
|
||||
@AfterEach void cleanup() throws Exception {
|
||||
release.countDown(); workers.shutdownNow(); server.stop(0);
|
||||
assertTrue(workers.awaitTermination(3, TimeUnit.SECONDS));
|
||||
}
|
||||
private URI uri(String path) { return URI.create("https://media.test:" + server.getAddress().getPort() + path); }
|
||||
private InetAddress[] loopback(String host) throws java.io.IOException { return new InetAddress[] {InetAddress.getByName("127.0.0.1")}; }
|
||||
private BoundedHttpTransport.Response get(String path, long timeoutMs, int maxBytes) throws Exception {
|
||||
return transport.get(uri(path), this::loopback, Duration.ofMillis(timeoutMs), maxBytes);
|
||||
}
|
||||
|
||||
@Test void bindsDnsOnceWhilePreservingHostSniAndCredentialIsolation() throws Exception {
|
||||
var lookups = new AtomicInteger(); var host = new AtomicReference<String>(); var sni = new AtomicReference<String>();
|
||||
server.createContext("/", exchange -> {
|
||||
host.set(exchange.getRequestHeaders().getFirst("Host"));
|
||||
var session = (ExtendedSSLSession) ((com.sun.net.httpserver.HttpsExchange) exchange).getSSLSession();
|
||||
sni.set(((SNIHostName) session.getRequestedServerNames().getFirst()).getAsciiName());
|
||||
assertNull(exchange.getRequestHeaders().getFirst("Authorization"));
|
||||
assertNull(exchange.getRequestHeaders().getFirst("Cookie"));
|
||||
assertNull(exchange.getRequestHeaders().getFirst("Proxy-Authorization"));
|
||||
assertEquals("identity", exchange.getRequestHeaders().getFirst("Accept-Encoding"));
|
||||
exchange.getResponseHeaders().set("Content-Type", "image/png");
|
||||
byte[] bytes = "synthetic-image".getBytes(StandardCharsets.UTF_8); exchange.sendResponseHeaders(200, bytes.length);
|
||||
try (var output = exchange.getResponseBody()) { output.write(bytes); }
|
||||
}); server.start();
|
||||
var response = transport.get(uri("/?signature=synthetic-private-query"), target -> {
|
||||
assertEquals("media.test", target);
|
||||
if (lookups.incrementAndGet() != 1) throw new AssertionError("DNS was resolved twice");
|
||||
return loopback(target);
|
||||
}, Duration.ofSeconds(3), 1024);
|
||||
assertEquals(200, response.statusCode()); assertEquals(1, lookups.get());
|
||||
assertEquals("media.test:" + server.getAddress().getPort(), host.get()); assertEquals("media.test", sni.get());
|
||||
assertEquals("synthetic-image", new String(response.body(), StandardCharsets.UTF_8));
|
||||
assertFalse(response.toString().contains("synthetic"));
|
||||
}
|
||||
|
||||
@Test void aTrustedCertificateForAnotherHostnameStillFails() throws Exception {
|
||||
var calls = new AtomicInteger();
|
||||
server.createContext("/", exchange -> { calls.incrementAndGet(); exchange.sendResponseHeaders(200, -1); exchange.close(); }); server.start();
|
||||
URI wrong = URI.create("https://wrong.test:" + server.getAddress().getPort() + "/?signature=private");
|
||||
var failure = assertThrows(BoundedHttpTransport.Failure.class, () -> transport.get(wrong, this::loopback, Duration.ofSeconds(3), 1024));
|
||||
assertEquals(BoundedHttpTransport.Reason.IO, failure.reason()); assertEquals(0, calls.get());
|
||||
assertNull(failure.getCause()); assertFalse(failure.toString().contains("private"));
|
||||
}
|
||||
|
||||
@Test void theDefaultTrustStoreDoesNotAcceptOurSelfSignedTestCertificate() throws Exception {
|
||||
server.createContext("/", exchange -> { exchange.sendResponseHeaders(200, -1); exchange.close(); }); server.start();
|
||||
var strict = new PinnedHttpsTransport(org.apache.hc.core5.ssl.SSLContexts.createDefault(), workers);
|
||||
assertEquals(BoundedHttpTransport.Reason.IO, assertThrows(BoundedHttpTransport.Failure.class,
|
||||
() -> strict.get(uri("/"), this::loopback, Duration.ofSeconds(3), 1024)).reason());
|
||||
}
|
||||
|
||||
@Test void redirectsAreNeverFollowed() throws Exception {
|
||||
var calls = new AtomicInteger();
|
||||
server.createContext("/", exchange -> {
|
||||
calls.incrementAndGet(); exchange.getResponseHeaders().set("Location", "https://127.0.0.1/private");
|
||||
exchange.sendResponseHeaders(302, -1); exchange.close();
|
||||
}); server.start();
|
||||
assertEquals(302, get("/", 3000, 1024).statusCode()); assertEquals(1, calls.get());
|
||||
}
|
||||
|
||||
@Test void declaredOversizeDoesNotWaitForTheBodyOrDrainItOnClose() {
|
||||
server.createContext("/", exchange -> {
|
||||
exchange.sendResponseHeaders(200, 1_000_000);
|
||||
try { release.await(3, TimeUnit.SECONDS); } catch (InterruptedException error) { Thread.currentThread().interrupt(); }
|
||||
exchange.close();
|
||||
}); server.start();
|
||||
var failure = assertThrows(BoundedHttpTransport.Failure.class, () -> get("/", 2000, 8192));
|
||||
assertEquals(BoundedHttpTransport.Reason.BODY_TOO_LARGE, failure.reason()); assertEquals(200, failure.observedStatus());
|
||||
}
|
||||
|
||||
@Test void chunkedOversizeIsStoppedDuringRead() {
|
||||
server.createContext("/", exchange -> {
|
||||
exchange.sendResponseHeaders(200, 0);
|
||||
try (var output = exchange.getResponseBody()) { for (int i = 0; i < 100; i++) output.write(new byte[4096]); }
|
||||
catch (java.io.IOException expectedAfterCancellation) { }
|
||||
}); server.start();
|
||||
assertEquals(BoundedHttpTransport.Reason.BODY_TOO_LARGE, assertThrows(BoundedHttpTransport.Failure.class, () -> get("/", 3000, 8192)).reason());
|
||||
}
|
||||
|
||||
@Test void deadlineIncludesSlowResponseBodyAndRestoresWorkerCapacity() throws Exception {
|
||||
var arrived = new CountDownLatch(1);
|
||||
server.createContext("/", exchange -> {
|
||||
exchange.sendResponseHeaders(200, 0);
|
||||
try (var output = exchange.getResponseBody()) {
|
||||
output.write('x'); output.flush(); arrived.countDown();
|
||||
try { release.await(5, TimeUnit.SECONDS); } catch (InterruptedException error) { Thread.currentThread().interrupt(); }
|
||||
} catch (java.io.IOException expectedAfterCancellation) { }
|
||||
}); server.start();
|
||||
long start = System.nanoTime();
|
||||
var failure = assertThrows(BoundedHttpTransport.Failure.class, () -> get("/", 1000, 8192));
|
||||
assertEquals(BoundedHttpTransport.Reason.TIMEOUT, failure.reason()); assertEquals(200, failure.observedStatus());
|
||||
assertEquals(0, arrived.getCount()); assertTrue(TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start) < 2500);
|
||||
workers.shutdown(); assertTrue(workers.awaitTermination(2, TimeUnit.SECONDS));
|
||||
}
|
||||
|
||||
@Test void lateDnsResolutionCannotStartHttpAfterTheDeadline() throws Exception {
|
||||
var resolving = new CountDownLatch(1); var finished = new CountDownLatch(1); var calls = new AtomicInteger();
|
||||
server.createContext("/", exchange -> { calls.incrementAndGet(); exchange.sendResponseHeaders(200, -1); exchange.close(); }); server.start();
|
||||
var failure = assertThrows(BoundedHttpTransport.Failure.class, () -> transport.get(uri("/"), host -> {
|
||||
resolving.countDown();
|
||||
boolean interrupted = false;
|
||||
while (release.getCount() != 0) try { release.await(1, TimeUnit.SECONDS); }
|
||||
catch (InterruptedException ignoredForSimulatedNativeResolver) { interrupted = true; }
|
||||
if (interrupted) Thread.currentThread().interrupt();
|
||||
finished.countDown(); return loopback(host);
|
||||
}, Duration.ofMillis(200), 1024));
|
||||
assertEquals(BoundedHttpTransport.Reason.TIMEOUT, failure.reason()); assertEquals(0, resolving.getCount());
|
||||
release.countDown(); assertTrue(finished.await(2, TimeUnit.SECONDS));
|
||||
workers.shutdown(); assertTrue(workers.awaitTermination(2, TimeUnit.SECONDS)); assertEquals(0, calls.get());
|
||||
}
|
||||
|
||||
@Test void interruptionCancelsSocketAndRestoresCallerInterruptFlag() throws Exception {
|
||||
var arrived = new CountDownLatch(1); var observed = new AtomicReference<BoundedHttpTransport.Reason>(); var flag = new AtomicReference<Boolean>();
|
||||
server.createContext("/", exchange -> {
|
||||
exchange.sendResponseHeaders(200, 0);
|
||||
try (var output = exchange.getResponseBody()) {
|
||||
output.write('x'); output.flush(); arrived.countDown();
|
||||
try { release.await(5, TimeUnit.SECONDS); } catch (InterruptedException error) { Thread.currentThread().interrupt(); }
|
||||
} catch (java.io.IOException expectedAfterCancellation) { }
|
||||
}); server.start();
|
||||
var caller = new Thread(() -> {
|
||||
try { get("/", 5000, 1024); }
|
||||
catch (BoundedHttpTransport.Failure failure) { observed.set(failure.reason()); flag.set(Thread.currentThread().isInterrupted()); }
|
||||
catch (Exception unexpected) { throw new AssertionError(unexpected); }
|
||||
}); caller.setDaemon(true); caller.start();
|
||||
try {
|
||||
assertTrue(arrived.await(3, TimeUnit.SECONDS)); caller.interrupt(); caller.join(2000);
|
||||
assertFalse(caller.isAlive()); assertEquals(BoundedHttpTransport.Reason.INTERRUPTED, observed.get()); assertEquals(true, flag.get());
|
||||
} finally { release.countDown(); caller.interrupt(); }
|
||||
}
|
||||
|
||||
@Test void refusedOrDroppedRequestsAreNotAutomaticallyRetried() {
|
||||
var calls = new AtomicInteger();
|
||||
server.createContext("/", exchange -> { calls.incrementAndGet(); exchange.close(); }); server.start();
|
||||
assertThrows(BoundedHttpTransport.Failure.class, () -> get("/", 3000, 1024)); assertEquals(1, calls.get());
|
||||
}
|
||||
|
||||
@Test void compressedBodiesAreNotDecompressedOrTreatedAsImages() {
|
||||
server.createContext("/", exchange -> {
|
||||
exchange.getResponseHeaders().set("Content-Encoding", "gzip"); exchange.sendResponseHeaders(200, 0);
|
||||
try (var output = exchange.getResponseBody()) { output.write(new byte[16]); }
|
||||
catch (java.io.IOException expectedAfterCancellation) { }
|
||||
}); server.start();
|
||||
assertEquals(BoundedHttpTransport.Reason.IO, assertThrows(BoundedHttpTransport.Failure.class, () -> get("/", 3000, 1024)).reason());
|
||||
}
|
||||
|
||||
@Test void busyPoolRejectsImmediatelyRatherThanQueueingUnlimitedDnsRequests() throws Exception {
|
||||
var occupied = new CountDownLatch(1);
|
||||
var oneWorker = new ThreadPoolExecutor(0, 1, 5, TimeUnit.SECONDS, new SynchronousQueue<>());
|
||||
try {
|
||||
oneWorker.submit(() -> {
|
||||
occupied.countDown();
|
||||
try { release.await(5, TimeUnit.SECONDS); } catch (InterruptedException error) { Thread.currentThread().interrupt(); }
|
||||
});
|
||||
assertTrue(occupied.await(2, TimeUnit.SECONDS));
|
||||
var rejected = new PinnedHttpsTransport(trustedTls, oneWorker);
|
||||
var failure = assertThrows(BoundedHttpTransport.Failure.class, () -> rejected.get(uri("/"), this::loopback, Duration.ofSeconds(3), 1024));
|
||||
assertEquals(BoundedHttpTransport.Reason.BUSY, failure.reason()); assertNull(failure.observedStatus());
|
||||
assertEquals(0, oneWorker.getQueue().size());
|
||||
} finally { release.countDown(); oneWorker.shutdownNow(); assertTrue(oneWorker.awaitTermination(2, TimeUnit.SECONDS)); }
|
||||
}
|
||||
|
||||
@Test void defaultSystemProxySelectorCannotBypassDnsBinding() throws Exception {
|
||||
var original = java.net.ProxySelector.getDefault();
|
||||
var selections = new AtomicInteger();
|
||||
server.createContext("/", exchange -> { exchange.sendResponseHeaders(200, 1); exchange.getResponseBody().write('x'); exchange.close(); }); server.start();
|
||||
try {
|
||||
java.net.ProxySelector.setDefault(new java.net.ProxySelector() {
|
||||
@Override public java.util.List<java.net.Proxy> select(URI target) {
|
||||
selections.incrementAndGet();
|
||||
return java.util.List.of(new java.net.Proxy(java.net.Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 1)));
|
||||
}
|
||||
@Override public void connectFailed(URI target, java.net.SocketAddress address, java.io.IOException failure) { }
|
||||
});
|
||||
assertEquals(200, get("/", 3000, 1024).statusCode()); assertEquals(0, selections.get());
|
||||
} finally { java.net.ProxySelector.setDefault(original); }
|
||||
}
|
||||
|
||||
@Test void independentAttemptsRevalidateDnsAndNeverReuseCookiesOrThePreviousConnection() throws Exception {
|
||||
var calls = new AtomicInteger(); var lookups = new AtomicInteger();
|
||||
server.createContext("/", exchange -> {
|
||||
calls.incrementAndGet(); assertNull(exchange.getRequestHeaders().getFirst("Cookie"));
|
||||
exchange.getResponseHeaders().set("Set-Cookie", "synthetic=value; Secure");
|
||||
exchange.sendResponseHeaders(200, 1); exchange.getResponseBody().write('x'); exchange.close();
|
||||
}); server.start();
|
||||
PinnedHttpsTransport.AddressResolver resolver = host -> {
|
||||
if (lookups.incrementAndGet() == 3) throw new PinnedHttpsTransport.AddressRejectedException();
|
||||
return loopback(host);
|
||||
};
|
||||
assertEquals(200, transport.get(uri("/"), resolver, Duration.ofSeconds(3), 1024).statusCode());
|
||||
assertEquals(200, transport.get(uri("/"), resolver, Duration.ofSeconds(3), 1024).statusCode());
|
||||
assertEquals(BoundedHttpTransport.Reason.INVALID_ADDRESS, assertThrows(BoundedHttpTransport.Failure.class,
|
||||
() -> transport.get(uri("/"), resolver, Duration.ofSeconds(3), 1024)).reason());
|
||||
assertEquals(3, lookups.get()); assertEquals(2, calls.get());
|
||||
}
|
||||
|
||||
@Test void errorBodyLimitRetainsStatusAndIsSmallerThanTheImageLimit() {
|
||||
server.createContext("/", exchange -> { exchange.sendResponseHeaders(401, 1_000_000); exchange.close(); }); server.start();
|
||||
var failure = assertThrows(BoundedHttpTransport.Failure.class, () -> get("/", 3000, 8 * 1024 * 1024));
|
||||
assertEquals(BoundedHttpTransport.Reason.BODY_TOO_LARGE, failure.reason()); assertEquals(401, failure.observedStatus());
|
||||
}
|
||||
|
||||
@Test void oversizedResponseHeadersAreNotBufferedWithoutLimits() {
|
||||
server.createContext("/", exchange -> {
|
||||
exchange.getResponseHeaders().set("X-Large", "x".repeat(16 * 1024));
|
||||
exchange.sendResponseHeaders(200, 1); exchange.getResponseBody().write('x'); exchange.close();
|
||||
}); server.start();
|
||||
assertEquals(BoundedHttpTransport.Reason.IO, assertThrows(BoundedHttpTransport.Failure.class, () -> get("/", 3000, 1024)).reason());
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
package com.moyi.controlplane.provider.transport;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.FilterInputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class SseEventReaderTest {
|
||||
private final SseEventReader reader = new SseEventReader(new ObjectMapper());
|
||||
|
||||
@Test
|
||||
void fragmentedUtf8CrLfMultilineDataAndCommentsHaveOneEvent() throws Exception {
|
||||
String data = ": heartbeat\r\nevent: result\r\ndata: {\"content\":\r\ndata: \"你好\"}\r\n\r\ndata: [DONE]\r\n\r\n";
|
||||
var input = new FilterInputStream(bytes(data)) {
|
||||
@Override public int read(byte[] value, int offset, int length) throws IOException {
|
||||
return super.read(value, offset, Math.min(1, length));
|
||||
}
|
||||
};
|
||||
var events = new ArrayList<Map<String,Object>>();
|
||||
|
||||
reader.read(input, () -> false, true, events::add);
|
||||
|
||||
assertEquals(1, events.size());
|
||||
assertEquals("你好", events.getFirst().get("content"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void chatRequiresDoneAndNativeRequiresWholeFramesEvenWithoutDone() throws Exception {
|
||||
String event = "data: {\"type\":\"message_stop\"}\n\n";
|
||||
|
||||
assertEquals("UPSTREAM_STREAM_INCOMPLETE", assertThrows(ApiException.class,
|
||||
() -> reader.read(bytes(event), () -> false, true, ignored -> {})).getCode());
|
||||
assertDoesNotThrow(() -> reader.read(bytes(event), () -> false, false, ignored -> {}));
|
||||
assertEquals("UPSTREAM_STREAM_INCOMPLETE", assertThrows(ApiException.class,
|
||||
() -> reader.read(bytes("data: {\"type\":\"message_stop\"}"), () -> false, false, ignored -> {})).getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void malformedJsonAndUtf8NeverReachConsumer() {
|
||||
var delivered = new AtomicBoolean();
|
||||
assertThrows(IOException.class, () -> reader.read(bytes("data: {not json}\n\n"), () -> false, true,
|
||||
ignored -> delivered.set(true)));
|
||||
assertThrows(IOException.class, () -> reader.read(new ByteArrayInputStream(new byte[]{(byte) 0xc3, 0x28}),
|
||||
() -> false, true, ignored -> delivered.set(true)));
|
||||
assertFalse(delivered.get());
|
||||
}
|
||||
|
||||
@Test
|
||||
void frameAndActualUtf8ByteBudgetsAreBothBounded() {
|
||||
assertEquals("UPSTREAM_RESPONSE_TOO_LARGE", assertThrows(ApiException.class,
|
||||
() -> reader.read(bytes("data: " + "x".repeat(262_145)), () -> false, true, ignored -> {})).getCode());
|
||||
// Fewer than 2M characters, but more than 4MiB on the wire; comments cannot bypass the budget.
|
||||
String multibyte = (": " + "中".repeat(1024) + "\n\n").repeat(1400);
|
||||
assertEquals("UPSTREAM_RESPONSE_TOO_LARGE", assertThrows(ApiException.class,
|
||||
() -> reader.read(bytes(multibyte), () -> false, true, ignored -> {})).getCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
void transportDeadlineMapsReadFailureAndTransportRetainsStreamOwnership() throws Exception {
|
||||
var closed = new AtomicBoolean();
|
||||
var input = new ByteArrayInputStream("data: [DONE]\n\n".getBytes(StandardCharsets.UTF_8)) {
|
||||
@Override public void close() { closed.set(true); }
|
||||
};
|
||||
reader.read(input, () -> false, true, ignored -> {});
|
||||
assertFalse(closed.get());
|
||||
assertEquals(504, assertThrows(ApiException.class,
|
||||
() -> reader.read(bytes("data: {}\n\n"), () -> true, true, ignored -> {})).getStatus());
|
||||
}
|
||||
|
||||
private ByteArrayInputStream bytes(String text) {
|
||||
return new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.moyi.controlplane.task;
|
||||
|
||||
import com.moyi.controlplane.common.ApiException;
|
||||
import java.math.BigDecimal;
|
||||
import java.util.*;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class AsyncPriceScheduleTest {
|
||||
private Map<String,Object> image() { return new LinkedHashMap<>(Map.of("unit","CNY / 1M tokens","input",20.16,"imageInput",32.256,"output",120.96,"taskBilling","openai-image-tokens")); }
|
||||
@Test void inputModalitiesUseDifferentRatesAndOnlyActualUsageIsCharged() {
|
||||
var pricing=image();
|
||||
var result=Map.<String,Object>of("usage",Map.of("input_tokens",50,"output_tokens",50,"input_tokens_details",Map.of("text_tokens",10,"image_tokens",40)));
|
||||
assertEquals(new BigDecimal("0.00753984"),AsyncPriceSchedule.bill(pricing,result).amount());
|
||||
assertTrue(AsyncPriceSchedule.reserve(pricing,Map.of("prompt","draw")).compareTo(AsyncPriceSchedule.bill(pricing,result).amount())>0);
|
||||
}
|
||||
@Test void missingOrInvalidUsageCannotBecomeAnEstimatedCharge() {
|
||||
assertThrows(ApiException.class,()->AsyncPriceSchedule.bill(image(),Map.of("base64","png")));
|
||||
for(Object invalid:List.of(-1,0.5,"NaN",100_000_001)) assertThrows(ApiException.class,()->AsyncPriceSchedule.bill(image(),Map.of("usage",Map.of("input_tokens",invalid,"output_tokens",50))));
|
||||
assertThrows(ApiException.class,()->AsyncPriceSchedule.bill(image(),Map.of("usage",Map.of("input_tokens",50,"output_tokens",50,"input_tokens_details",Map.of("text_tokens",50,"image_tokens",1)))));
|
||||
}
|
||||
@Test void ambiguousCachedModalitiesAreNotDoubleCounted() {
|
||||
var price=image(); price.put("cachedInput",8.064);
|
||||
var usage=Map.<String,Object>of("input_tokens",50,"output_tokens",50,"input_tokens_details",Map.of("text_tokens",10,"image_tokens",40,"cached_tokens",10));
|
||||
assertThrows(ApiException.class,()->AsyncPriceSchedule.bill(price,Map.of("usage",usage)));
|
||||
}
|
||||
@Test void geminiUsageIncludesThinkingAndRequiresPromptAndCandidateCounts() {
|
||||
var price=Map.<String,Object>of("unit","CNY / 1M tokens","input",8.064,"output",483.84,"taskBilling","gemini-image-tokens");
|
||||
var bill=AsyncPriceSchedule.bill(price,Map.of("usageMetadata",Map.of("promptTokenCount",100,"candidatesTokenCount",1000,"thoughtsTokenCount",20)));
|
||||
assertEquals(new BigDecimal("0.49432320"),bill.amount());
|
||||
assertThrows(ApiException.class,()->AsyncPriceSchedule.bill(price,Map.of("usageMetadata",Map.of("totalTokenCount",1120))));
|
||||
}
|
||||
@Test void fixedCnyPricesAreSingleImageAndUnsupportedUnitsAreRejected() {
|
||||
var price=Map.<String,Object>of("unit","CNY / image","price",0.4032);
|
||||
assertEquals(new BigDecimal("0.4032"),AsyncPriceSchedule.reserve(price,Map.of("n",1)));
|
||||
assertThrows(ApiException.class,()->AsyncPriceSchedule.reserve(price,Map.of("n",2)));
|
||||
for(String unit:List.of("USD / task","CNY / second","CNY / 1M tokens")) assertFalse(AsyncPriceSchedule.ready(Map.of("unit",unit,"input",1,"output",1,"price",1)));
|
||||
}
|
||||
}
|
||||
Loaded 100 of 156 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user