feat: package Beijing test deployment with HTTPS and preflight checks
This commit is contained in:
1 parent
ae749a1fe9
commit
c7278c00a6
33 files changed
+1153
-54
No files matched your search
@@ -147,7 +147,7 @@ public class CatalogService {
|
||||
OffsetDateTime now = now();
|
||||
jdbc.update(
|
||||
"INSERT INTO platform_api_keys (id, user_id, project_id, name, key_prefix, key_hash, scopes, model_ids, status, created_at) VALUES (?, ?, ?, ?, ?, ?, CAST(? AS jsonb), CAST(? AS jsonb), 'active', ?)",
|
||||
id, user.id(), projectId, name, plaintext.substring(0, 16), sha256(plaintext), writeJson(scopes), writeJson(modelIds), now
|
||||
UUID.fromString(id), user.id(), projectId, name, plaintext.substring(0, 16), sha256(plaintext), writeJson(scopes), writeJson(modelIds), now
|
||||
);
|
||||
Map<String, Object> result = new LinkedHashMap<>();
|
||||
result.put("key", plaintext);
|
||||
|
||||
@@ -86,6 +86,9 @@ public class GatewayController {
|
||||
catalogService.touchKey(key);
|
||||
return ResponseEntity.ok(upstream);
|
||||
}
|
||||
if (!"provider-mock".equals(providerId)) {
|
||||
throw new ApiException("PROVIDER_NOT_CONFIGURED", "该供应商尚未配置密钥,不能执行真实调用", 503);
|
||||
}
|
||||
String prompt = extractPrompt(input.get("messages"));
|
||||
String content = "MOYI Java Mock 已收到:" + (prompt.isBlank() ? "(空消息)" : prompt);
|
||||
catalogService.touchKey(key);
|
||||
|
||||
@@ -25,6 +25,7 @@ public class GatewayGuard {
|
||||
private final int requestsPerMinute;
|
||||
private final int readsPerMinute;
|
||||
private final int defaultModelConcurrency;
|
||||
private final String redisPrefix;
|
||||
|
||||
private static DefaultRedisScript<Long> script(String location) {
|
||||
DefaultRedisScript<Long> script = new DefaultRedisScript<>();
|
||||
@@ -37,7 +38,8 @@ public class GatewayGuard {
|
||||
StringRedisTemplate redis,
|
||||
@Value("${moyi.gateway.requests-per-minute:60}") int requestsPerMinute,
|
||||
@Value("${moyi.gateway.reads-per-minute:300}") int readsPerMinute,
|
||||
@Value("${moyi.gateway.default-model-concurrency:4}") int defaultModelConcurrency
|
||||
@Value("${moyi.gateway.default-model-concurrency:4}") int defaultModelConcurrency,
|
||||
@Value("${moyi.gateway.redis-prefix:moyi:}") String redisPrefix
|
||||
) {
|
||||
if (requestsPerMinute < 1 || readsPerMinute < 1 || defaultModelConcurrency < 1) {
|
||||
throw new IllegalArgumentException("Gateway limits must be positive");
|
||||
@@ -46,10 +48,14 @@ public class GatewayGuard {
|
||||
this.requestsPerMinute = requestsPerMinute;
|
||||
this.readsPerMinute = readsPerMinute;
|
||||
this.defaultModelConcurrency = defaultModelConcurrency;
|
||||
if (redisPrefix == null || !redisPrefix.matches("[A-Za-z0-9:_-]{2,80}:")) {
|
||||
throw new IllegalArgumentException("Redis prefix must end in ':' and contain only letters, numbers, ':', '_' or '-'");
|
||||
}
|
||||
this.redisPrefix = redisPrefix;
|
||||
}
|
||||
|
||||
public void checkRate(UUID keyId, boolean readOnly) {
|
||||
String bucket = "moyi:gateway:rate:" + (readOnly ? "read:" : "invoke:") + keyId;
|
||||
String bucket = redisPrefix + "gateway:rate:" + (readOnly ? "read:" : "invoke:") + keyId;
|
||||
long retryMs = execute(RATE, bucket, Integer.toString(readOnly ? readsPerMinute : requestsPerMinute), "60000");
|
||||
if (retryMs > 0) {
|
||||
throw new GatewayLimitException("RATE_LIMITED", "请求过于频繁,请稍后重试", 429, (int) ((retryMs + 999) / 1000));
|
||||
@@ -62,7 +68,7 @@ public class GatewayGuard {
|
||||
}
|
||||
|
||||
public Slot acquire(Map<String, Object> model) {
|
||||
String bucket = "moyi:gateway:slots:" + model.get("id");
|
||||
String bucket = redisPrefix + "gateway:slots:" + model.get("id");
|
||||
String token = UUID.randomUUID().toString();
|
||||
long acquired = execute(ACQUIRE, bucket, Integer.toString(modelConcurrency(model)), Long.toString(SLOT_TTL_MS), token);
|
||||
if (acquired != 1) throw new GatewayLimitException("MODEL_BUSY", "当前模型并发已满,请稍后重试", 429, 1);
|
||||
@@ -71,7 +77,7 @@ public class GatewayGuard {
|
||||
|
||||
private long execute(DefaultRedisScript<Long> script, String key, String... args) {
|
||||
try {
|
||||
Long value = redis.execute(script, List.of(key), args);
|
||||
Long value = redis.execute(script, List.of(key), (Object[]) args);
|
||||
if (value != null) return value;
|
||||
} catch (DataAccessException exception) {
|
||||
log.warn("Gateway guard Redis operation failed ({})", exception.getClass().getSimpleName());
|
||||
|
||||
@@ -56,6 +56,9 @@ public class AsyncTaskService {
|
||||
if (model == null) throw new ApiException("MODEL_NOT_FOUND", "模型不存在或已下线", 404);
|
||||
if (!catalogService.keyCanInvoke(key, modelId)) throw new ApiException("MODEL_FORBIDDEN", "当前 Key 无权调用该模型", 403);
|
||||
if (!taskType.equals(model.get("type")) || !"async".equals(model.get("mode"))) throw new ApiException("NOT_ASYNC_MEDIA_MODEL", "该模型不是对应的异步媒体模型", 400);
|
||||
if (!"provider-mock".equals(model.get("providerId"))) {
|
||||
throw new ApiException("ASYNC_PROVIDER_NOT_IMPLEMENTED", "Java 版尚未接入该供应商的异步执行器", 501);
|
||||
}
|
||||
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();
|
||||
|
||||
@@ -223,7 +223,13 @@ public class UsageAuditService {
|
||||
try {
|
||||
JsonNode sanitized = sanitize(objectMapper.valueToTree(value));
|
||||
String text = objectMapper.writeValueAsString(sanitized);
|
||||
return text.length() > MAX_PAYLOAD_CHARS ? text.substring(0, MAX_PAYLOAD_CHARS) + "…" : text;
|
||||
if (text.length() <= MAX_PAYLOAD_CHARS) return text;
|
||||
// Preserve valid JSON for PostgreSQL jsonb even when payloads are large.
|
||||
ObjectNode summary = objectMapper.createObjectNode();
|
||||
summary.put("_truncated", true);
|
||||
summary.put("_originalChars", text.length());
|
||||
summary.put("preview", text.substring(0, 8000));
|
||||
return objectMapper.writeValueAsString(summary);
|
||||
} catch (JsonProcessingException exception) { return "{\"_unserializable\":true}"; }
|
||||
}
|
||||
|
||||
|
||||
@@ -10,12 +10,18 @@ spring:
|
||||
minimum-idle: ${DB_POOL_MIN_IDLE:2}
|
||||
data:
|
||||
redis:
|
||||
url: ${SPRING_DATA_REDIS_URL:redis://localhost:6379}
|
||||
host: ${MOYI_REDIS_HOST:localhost}
|
||||
port: ${MOYI_REDIS_PORT:6379}
|
||||
username: ${MOYI_REDIS_USERNAME:}
|
||||
password: ${MOYI_REDIS_PASSWORD:}
|
||||
database: ${MOYI_REDIS_DATABASE:0}
|
||||
ssl:
|
||||
enabled: ${MOYI_REDIS_SSL:false}
|
||||
connect-timeout: 2s
|
||||
timeout: 2s
|
||||
flyway:
|
||||
enabled: ${FLYWAY_ENABLED:true}
|
||||
baseline-on-migrate: true
|
||||
baseline-on-migrate: ${FLYWAY_BASELINE_ON_MIGRATE:false}
|
||||
|
||||
server:
|
||||
address: ${CONTROL_PLANE_HOST:0.0.0.0}
|
||||
@@ -34,6 +40,7 @@ management:
|
||||
|
||||
moyi:
|
||||
gateway:
|
||||
redis-prefix: ${MOYI_REDIS_KEY_PREFIX:moyi:}
|
||||
requests-per-minute: ${GATEWAY_REQUESTS_PER_MINUTE:60}
|
||||
reads-per-minute: ${GATEWAY_READS_PER_MINUTE:300}
|
||||
default-model-concurrency: ${GATEWAY_DEFAULT_MODEL_CONCURRENCY:4}
|
||||
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
|
||||
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.task.AsyncTaskService;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.data.redis.core.script.RedisScript;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class GatewayDeploymentTest {
|
||||
@Test
|
||||
void redisRateKeysAreEnvironmentScoped() {
|
||||
StringRedisTemplate redis = mock(StringRedisTemplate.class);
|
||||
doReturn(0L).when(redis).execute(any(RedisScript.class), anyList(), any(Object[].class));
|
||||
UUID keyId = UUID.randomUUID();
|
||||
new GatewayGuard(redis, 60, 300, 4, "moyi:test:").checkRate(keyId, false);
|
||||
verify(redis).execute(any(RedisScript.class), eq(List.of("moyi:test:gateway:rate:invoke:" + keyId)), any(Object[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidRedisPrefixIsRejected() {
|
||||
assertThrows(IllegalArgumentException.class, () -> new GatewayGuard(mock(StringRedisTemplate.class), 60, 300, 4, ""));
|
||||
}
|
||||
|
||||
@Test
|
||||
void unconfiguredRealProviderNeverReturnsMockSuccess() {
|
||||
CatalogService catalog = mock(CatalogService.class);
|
||||
GatewayGuard guard = mock(GatewayGuard.class);
|
||||
OpenAiCompatibleProvider provider = mock(OpenAiCompatibleProvider.class);
|
||||
UsageAuditService usage = mock(UsageAuditService.class);
|
||||
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", UUID.randomUUID().toString());
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.activeModel("model-real")).thenReturn(Map.of("id", "model-real", "providerId", "provider-deepseek", "mode", "sync"));
|
||||
when(catalog.keyCanInvoke(key, "model-real")).thenReturn(true);
|
||||
GatewayController controller = new GatewayController(catalog, provider, guard, usage);
|
||||
ApiException error = assertThrows(ApiException.class, () -> controller.chatCompletions("Bearer test-key", Map.of("model", "model-real")));
|
||||
assertEquals("PROVIDER_NOT_CONFIGURED", error.getCode());
|
||||
assertEquals(503, error.getStatus());
|
||||
verify(provider, never()).chat(anyString(), anyString(), anyList());
|
||||
}
|
||||
|
||||
@Test
|
||||
void explicitMockProviderStillWorks() {
|
||||
CatalogService catalog = mock(CatalogService.class);
|
||||
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString(), "userId", UUID.randomUUID().toString());
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.activeModel("mock")).thenReturn(Map.of("id", "mock", "providerId", "provider-mock", "mode", "sync"));
|
||||
when(catalog.keyCanInvoke(key, "mock")).thenReturn(true);
|
||||
GatewayController controller = new GatewayController(catalog, mock(OpenAiCompatibleProvider.class), mock(GatewayGuard.class), mock(UsageAuditService.class));
|
||||
assertEquals(200, controller.chatCompletions("Bearer test-key", Map.of("model", "mock")).getStatusCode().value());
|
||||
}
|
||||
|
||||
@Test
|
||||
void unimplementedMediaProviderDoesNotCreateFakeTask() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
CatalogService catalog = mock(CatalogService.class);
|
||||
Map<String, Object> key = Map.of("id", UUID.randomUUID().toString());
|
||||
when(catalog.authenticateKey("test-key")).thenReturn(key);
|
||||
when(catalog.activeModel("tripo")).thenReturn(Map.of("id", "tripo", "providerId", "provider-vast", "type", "3d", "mode", "async"));
|
||||
when(catalog.keyCanInvoke(key, "tripo")).thenReturn(true);
|
||||
AsyncTaskService tasks = new AsyncTaskService(jdbc, new ObjectMapper(), catalog, 100, mock(GatewayGuard.class), mock(UsageAuditService.class));
|
||||
ApiException error = assertThrows(ApiException.class, () -> tasks.create("Bearer test-key", "3d", Map.of("model", "tripo", "prompt", "test")));
|
||||
assertEquals(501, error.getStatus());
|
||||
verifyNoInteractions(jdbc);
|
||||
}
|
||||
}
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
package com.moyi.controlplane.gateway;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.moyi.controlplane.auth.AuthService;
|
||||
import com.moyi.controlplane.catalog.CatalogService;
|
||||
import com.moyi.controlplane.security.SecretCipher;
|
||||
import com.moyi.controlplane.usage.UsageAuditService;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class PostgresBindingTest {
|
||||
@Test
|
||||
void keyIdUsesNativeUuidBinding() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
AuthService auth = mock(AuthService.class);
|
||||
when(auth.requireUser("session")).thenReturn(new AuthService.UserView(UUID.randomUUID(), "admin", "username", "Admin", "admin", "active", null, null));
|
||||
doAnswer(call -> {
|
||||
Object[] bindings = (Object[]) call.getRawArguments()[1];
|
||||
assertInstanceOf(UUID.class, bindings[0]);
|
||||
return 1;
|
||||
}).when(jdbc).update(startsWith("INSERT INTO platform_api_keys"), any(Object[].class));
|
||||
doReturn(Map.of("name", "test")).when(jdbc).queryForObject(anyString(), any(RowMapper.class), any(Object[].class));
|
||||
new CatalogService(jdbc, new ObjectMapper(), mock(SecretCipher.class)).createKey("session", Map.of("name", "test"), auth);
|
||||
verify(jdbc).update(startsWith("INSERT INTO platform_api_keys"), any(Object[].class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void oversizedAuditPayloadIsStillValidJsonb() {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
doAnswer(call -> {
|
||||
Object[] bindings = (Object[]) call.getRawArguments()[1];
|
||||
String payload = (String) bindings[11];
|
||||
assertTrue(payload.length() <= 32000);
|
||||
assertTrue(mapper.readTree(payload).get("_truncated").asBoolean());
|
||||
return 1;
|
||||
}).when(jdbc).update(startsWith("INSERT INTO usage_events"), any(Object[].class));
|
||||
new UsageAuditService(jdbc, mapper).recordCall(UUID.randomUUID(), UUID.randomUUID(), "model", "provider", "succeeded", 5, 0, 0,
|
||||
List.of("x".repeat(8000), "y".repeat(8000), "z".repeat(8000), "w".repeat(8000), "v".repeat(8000)), null, null, null);
|
||||
verify(jdbc).update(startsWith("INSERT INTO usage_events"), any(Object[].class));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user