fix: scope provider model keys and pin async credentials
This commit is contained in:
1 parent
58dc904c98
commit
558c0dcdd5
8 files changed
+166
-55
No files matched your search
+2
-1
@@ -103,7 +103,8 @@ class OpenAiCompatibleProviderHttpTest {
|
||||
});
|
||||
server.start();
|
||||
environment.withProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_ROUTE_A", "route-key")
|
||||
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_GPT_5", "model-key");
|
||||
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_PROVIDER_DEEPSEEK_GPT_5", "model-key")
|
||||
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_GPT_5", "wrong-global-key");
|
||||
|
||||
provider.chat("provider-deepseek", "gpt-5", "route-a", textInput(128));
|
||||
provider.chat("provider-deepseek", "gpt-5", "route-b", textInput(128));
|
||||
|
||||
+44
@@ -14,11 +14,13 @@ import com.moyi.controlplane.security.SecretCipher;
|
||||
import com.sun.net.httpserver.HttpServer;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.Map;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.mock.env.MockEnvironment;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
class AsyncProviderClientTest {
|
||||
@@ -139,6 +141,48 @@ class AsyncProviderClientTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void asyncPollingReusesTheSubmittedKeyFingerprint() throws Exception {
|
||||
HttpServer server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0);
|
||||
AtomicReference<String> submitAuthorization = new AtomicReference<>();
|
||||
AtomicReference<String> pollAuthorization = new AtomicReference<>();
|
||||
server.createContext("/videos", exchange -> {
|
||||
String authorization = exchange.getRequestHeaders().getFirst("Authorization");
|
||||
if (exchange.getRequestURI().getPath().equals("/videos")) {
|
||||
submitAuthorization.set(authorization);
|
||||
exchange.getRequestBody().readAllBytes();
|
||||
byte[] response = "{\"id\":\"video-1\",\"status\":\"queued\"}".getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(200, response.length);
|
||||
try (var output = exchange.getResponseBody()) { output.write(response); }
|
||||
} else {
|
||||
pollAuthorization.set(authorization);
|
||||
byte[] response = "{\"id\":\"video-1\",\"status\":\"succeeded\"}".getBytes(StandardCharsets.UTF_8);
|
||||
exchange.sendResponseHeaders(200, response.length);
|
||||
try (var output = exchange.getResponseBody()) { output.write(response); }
|
||||
}
|
||||
});
|
||||
server.start();
|
||||
try {
|
||||
JdbcTemplate jdbc = mock(JdbcTemplate.class);
|
||||
SecretCipher cipher = new SecretCipher("test-encryption");
|
||||
when(jdbc.queryForMap(any(String.class), eq("provider-video"))).thenReturn(Map.of(
|
||||
"code", "relay-video", "protocol", "openai-compatible", "base_url", "http://127.0.0.1:" + server.getAddress().getPort(), "credential_ciphertext", ""
|
||||
));
|
||||
MockEnvironment environment = new MockEnvironment()
|
||||
.withProperty("MOYI_UPSTREAM_API_KEYS_ROUTE_ROUTE_A", "key-a|key-b")
|
||||
.withProperty("MOYI_UPSTREAM_API_KEYS_MODEL_RELAY_VIDEO_SORA_2", "wrong-model-key");
|
||||
AsyncProviderClient client = new AsyncProviderClient(jdbc, cipher, new ObjectMapper(), environment);
|
||||
|
||||
Map<String,Object> submitted = client.submit("provider-video", "sora-2", "route-a", "video", Map.of("prompt", "a cat"));
|
||||
String fingerprint = String.valueOf(submitted.get("_moyiUpstreamKeyFingerprint"));
|
||||
assertTrue(fingerprint.matches("[0-9a-f]{64}"));
|
||||
client.poll("provider-video", "route-a", "sora-2", "video-1", fingerprint);
|
||||
assertEquals(submitAuthorization.get(), pollAuthorization.get());
|
||||
} finally {
|
||||
server.stop(0);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource({"401,UPSTREAM_AUTH_FAILED", "403,UPSTREAM_AUTH_FAILED", "402,UPSTREAM_CREDIT_INSUFFICIENT", "429,UPSTREAM_RATE_LIMITED", "503,UPSTREAM_PROVIDER_ERROR"})
|
||||
void httpErrorsExposeOnlySafeCodes(int status, String code) throws Exception {
|
||||
|
||||
Reference in new issue
Block a user