feat: API-Schlüssel für strata aus Umgebungsvariable
strata verlangt nach dem Start mit --access lan einen Schlüssel (HTTP 401). Der Client liest ihn aus der Umgebungsvariable STRATA_API_KEY (Name in config.toml änderbar) und sendet ihn als Bearer-Token. Der Schlüssel steht weder in der Konfiguration noch im Repo. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
@ -559,6 +559,7 @@ impl ModelClient for OllamaClient {
|
||||
pub struct OpenAiClient {
|
||||
pub url: String,
|
||||
pub model: String,
|
||||
api_key: Option<String>,
|
||||
http: reqwest::blocking::Client,
|
||||
}
|
||||
|
||||
@ -644,13 +645,14 @@ pub fn to_openai_messages(messages: &[Message]) -> Vec<Value> {
|
||||
}
|
||||
|
||||
impl OpenAiClient {
|
||||
pub fn new(url: &str, model: &str) -> Result<OpenAiClient, String> {
|
||||
pub fn new(url: &str, model: &str, api_key: Option<String>) -> Result<OpenAiClient, String> {
|
||||
let http = reqwest::blocking::Client::builder()
|
||||
.build()
|
||||
.map_err(|e| format!("HTTP-Client nicht baubar: {e}"))?;
|
||||
Ok(OpenAiClient {
|
||||
url: url.trim_end_matches('/').into(),
|
||||
model: model.into(),
|
||||
api_key,
|
||||
http,
|
||||
})
|
||||
}
|
||||
@ -667,11 +669,11 @@ impl ModelClient for OpenAiClient {
|
||||
"stream": false,
|
||||
"temperature": 0,
|
||||
});
|
||||
let resp = self
|
||||
.http
|
||||
.post(&self.url)
|
||||
.timeout(timeout)
|
||||
.json(&body)
|
||||
let mut req = self.http.post(&self.url).timeout(timeout).json(&body);
|
||||
if let Some(key) = &self.api_key {
|
||||
req = req.bearer_auth(key);
|
||||
}
|
||||
let resp = req
|
||||
.send()
|
||||
.map_err(|e| {
|
||||
if e.is_timeout() {
|
||||
|
||||
@ -27,6 +27,8 @@ pub struct Config {
|
||||
pub ollama_url: String,
|
||||
/// OpenAI-kompatibler Endpunkt, vollständige URL inkl. `/v1/chat/completions`.
|
||||
pub openai_url: String,
|
||||
/// Name der Umgebungsvariable mit dem API-Schlüssel. Der Schlüssel selbst steht nicht in der Datei.
|
||||
pub openai_api_key_env: String,
|
||||
/// Speicherlimit pro Lauf, Podman-Schreibweise.
|
||||
pub sandbox_memory: String,
|
||||
/// CPU-Anteil pro Lauf.
|
||||
@ -43,6 +45,7 @@ impl Default for Config {
|
||||
backend: "ollama".into(),
|
||||
ollama_url: "http://127.0.0.1:11434".into(),
|
||||
openai_url: "http://192.168.12.4:8082/v1/chat/completions".into(),
|
||||
openai_api_key_env: "STRATA_API_KEY".into(),
|
||||
sandbox_memory: limits.memory,
|
||||
sandbox_cpus: limits.cpus,
|
||||
}
|
||||
|
||||
@ -75,6 +75,7 @@ async fn start_batch(
|
||||
runtime: cfg.container_runtime.clone(),
|
||||
backend: cfg.backend.clone(),
|
||||
endpoint_url: cfg.endpoint_url(),
|
||||
api_key: std::env::var(&cfg.openai_api_key_env).ok().filter(|k| !k.is_empty()),
|
||||
data_dir: paths.0.data_dir.clone(),
|
||||
limits: cfg.sandbox_limits(),
|
||||
};
|
||||
|
||||
@ -36,6 +36,8 @@ pub struct RunConfig {
|
||||
/// `ollama` oder `openai`.
|
||||
pub backend: String,
|
||||
pub endpoint_url: String,
|
||||
/// Schlüssel für den OpenAI-Endpunkt, wird nicht gespeichert.
|
||||
pub api_key: Option<String>,
|
||||
pub data_dir: PathBuf,
|
||||
pub limits: Limits,
|
||||
}
|
||||
@ -257,7 +259,7 @@ enum JobResult {
|
||||
pub fn make_client(cfg: &RunConfig, model: &str) -> Result<Box<dyn ModelClient>, String> {
|
||||
match cfg.backend.as_str() {
|
||||
"ollama" => Ok(Box::new(OllamaClient::new(&cfg.endpoint_url, model)?)),
|
||||
"openai" => Ok(Box::new(OpenAiClient::new(&cfg.endpoint_url, model)?)),
|
||||
"openai" => Ok(Box::new(OpenAiClient::new(&cfg.endpoint_url, model, cfg.api_key.clone())?)),
|
||||
other => Err(format!("Unbekanntes Backend: {other} (erlaubt: ollama, openai)")),
|
||||
}
|
||||
}
|
||||
@ -456,6 +458,7 @@ mod tests {
|
||||
runtime: "podman".into(),
|
||||
backend: "ollama".into(),
|
||||
endpoint_url: url,
|
||||
api_key: None,
|
||||
data_dir: base.join("data"),
|
||||
limits: Limits::default(),
|
||||
};
|
||||
@ -499,6 +502,7 @@ mod tests {
|
||||
runtime: "podman".into(),
|
||||
backend: "ollama".into(),
|
||||
endpoint_url: url,
|
||||
api_key: None,
|
||||
data_dir: data.clone(),
|
||||
limits: Limits::default(),
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user