feat: encrypt provider credentials
This commit is contained in:
1 parent
31a34757be
commit
7949f29f1a
6 files changed
+97
-7
No files matched your search
+26
-3
@@ -3,6 +3,7 @@ package com.moyi.controlplane.provider;
|
||||
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 java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
@@ -11,20 +12,28 @@ import java.time.Duration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class OpenAiCompatibleProvider {
|
||||
private final ObjectMapper objectMapper;
|
||||
private final HttpClient httpClient;
|
||||
private final JdbcTemplate jdbc;
|
||||
private final SecretCipher secretCipher;
|
||||
private final Map<String, ProviderConfig> providers;
|
||||
|
||||
public OpenAiCompatibleProvider(
|
||||
ObjectMapper objectMapper,
|
||||
org.springframework.core.env.Environment environment
|
||||
org.springframework.core.env.Environment environment,
|
||||
JdbcTemplate jdbc,
|
||||
SecretCipher secretCipher
|
||||
) {
|
||||
this.objectMapper = objectMapper;
|
||||
this.httpClient = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build();
|
||||
this.jdbc = jdbc;
|
||||
this.secretCipher = secretCipher;
|
||||
this.providers = Map.of(
|
||||
"provider-deepseek", new ProviderConfig(environment.getProperty("moyi.providers.deepseek.api-key", ""), environment.getProperty("moyi.providers.deepseek.base-url", "https://api.deepseek.com")),
|
||||
"provider-kimi", new ProviderConfig(environment.getProperty("moyi.providers.kimi.api-key", ""), environment.getProperty("moyi.providers.kimi.base-url", "https://api.moonshot.ai/v1")),
|
||||
@@ -33,12 +42,12 @@ public class OpenAiCompatibleProvider {
|
||||
}
|
||||
|
||||
public boolean isConfigured(String providerId) {
|
||||
ProviderConfig provider = providers.get(providerId);
|
||||
ProviderConfig provider = resolve(providerId);
|
||||
return provider != null && !provider.apiKey().isBlank();
|
||||
}
|
||||
|
||||
public Map<String, Object> chat(String providerId, String upstreamModel, List<?> messages) {
|
||||
ProviderConfig provider = providers.get(providerId);
|
||||
ProviderConfig provider = resolve(providerId);
|
||||
if (provider == null || provider.apiKey().isBlank()) {
|
||||
throw new ApiException("PROVIDER_NOT_CONFIGURED", "该模型的供应商尚未配置服务端 API Key", 503);
|
||||
}
|
||||
@@ -74,6 +83,20 @@ public class OpenAiCompatibleProvider {
|
||||
return value == null ? "" : value.replaceAll("/+$", "");
|
||||
}
|
||||
|
||||
private ProviderConfig resolve(String providerId) {
|
||||
ProviderConfig fixed = providers.get(providerId);
|
||||
if (fixed != null && !fixed.apiKey().isBlank()) return fixed;
|
||||
try {
|
||||
Map<String, Object> row = jdbc.queryForMap("SELECT base_url, credential_ciphertext FROM model_providers WHERE id = ? AND status = 'active'", providerId);
|
||||
String ciphertext = row.get("credential_ciphertext") == null ? "" : String.valueOf(row.get("credential_ciphertext"));
|
||||
String apiKey = ciphertext.isBlank() ? "" : secretCipher.decrypt(ciphertext);
|
||||
String baseUrl = row.get("base_url") == null ? "" : String.valueOf(row.get("base_url"));
|
||||
return new ProviderConfig(apiKey, baseUrl);
|
||||
} catch (EmptyResultDataAccessException exception) {
|
||||
return fixed;
|
||||
}
|
||||
}
|
||||
|
||||
private String writeJson(Object value) {
|
||||
try {
|
||||
return objectMapper.writeValueAsString(value);
|
||||
|
||||
Reference in new issue
Block a user