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 struct OpenAiClient {
|
||||||
pub url: String,
|
pub url: String,
|
||||||
pub model: String,
|
pub model: String,
|
||||||
|
api_key: Option<String>,
|
||||||
http: reqwest::blocking::Client,
|
http: reqwest::blocking::Client,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -644,13 +645,14 @@ pub fn to_openai_messages(messages: &[Message]) -> Vec<Value> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl OpenAiClient {
|
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()
|
let http = reqwest::blocking::Client::builder()
|
||||||
.build()
|
.build()
|
||||||
.map_err(|e| format!("HTTP-Client nicht baubar: {e}"))?;
|
.map_err(|e| format!("HTTP-Client nicht baubar: {e}"))?;
|
||||||
Ok(OpenAiClient {
|
Ok(OpenAiClient {
|
||||||
url: url.trim_end_matches('/').into(),
|
url: url.trim_end_matches('/').into(),
|
||||||
model: model.into(),
|
model: model.into(),
|
||||||
|
api_key,
|
||||||
http,
|
http,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -667,11 +669,11 @@ impl ModelClient for OpenAiClient {
|
|||||||
"stream": false,
|
"stream": false,
|
||||||
"temperature": 0,
|
"temperature": 0,
|
||||||
});
|
});
|
||||||
let resp = self
|
let mut req = self.http.post(&self.url).timeout(timeout).json(&body);
|
||||||
.http
|
if let Some(key) = &self.api_key {
|
||||||
.post(&self.url)
|
req = req.bearer_auth(key);
|
||||||
.timeout(timeout)
|
}
|
||||||
.json(&body)
|
let resp = req
|
||||||
.send()
|
.send()
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
if e.is_timeout() {
|
if e.is_timeout() {
|
||||||
|
|||||||
@ -27,6 +27,8 @@ pub struct Config {
|
|||||||
pub ollama_url: String,
|
pub ollama_url: String,
|
||||||
/// OpenAI-kompatibler Endpunkt, vollständige URL inkl. `/v1/chat/completions`.
|
/// OpenAI-kompatibler Endpunkt, vollständige URL inkl. `/v1/chat/completions`.
|
||||||
pub openai_url: String,
|
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.
|
/// Speicherlimit pro Lauf, Podman-Schreibweise.
|
||||||
pub sandbox_memory: String,
|
pub sandbox_memory: String,
|
||||||
/// CPU-Anteil pro Lauf.
|
/// CPU-Anteil pro Lauf.
|
||||||
@ -43,6 +45,7 @@ impl Default for Config {
|
|||||||
backend: "ollama".into(),
|
backend: "ollama".into(),
|
||||||
ollama_url: "http://127.0.0.1:11434".into(),
|
ollama_url: "http://127.0.0.1:11434".into(),
|
||||||
openai_url: "http://192.168.12.4:8082/v1/chat/completions".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_memory: limits.memory,
|
||||||
sandbox_cpus: limits.cpus,
|
sandbox_cpus: limits.cpus,
|
||||||
}
|
}
|
||||||
|
|||||||
@ -75,6 +75,7 @@ async fn start_batch(
|
|||||||
runtime: cfg.container_runtime.clone(),
|
runtime: cfg.container_runtime.clone(),
|
||||||
backend: cfg.backend.clone(),
|
backend: cfg.backend.clone(),
|
||||||
endpoint_url: cfg.endpoint_url(),
|
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(),
|
data_dir: paths.0.data_dir.clone(),
|
||||||
limits: cfg.sandbox_limits(),
|
limits: cfg.sandbox_limits(),
|
||||||
};
|
};
|
||||||
|
|||||||
@ -36,6 +36,8 @@ pub struct RunConfig {
|
|||||||
/// `ollama` oder `openai`.
|
/// `ollama` oder `openai`.
|
||||||
pub backend: String,
|
pub backend: String,
|
||||||
pub endpoint_url: 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 data_dir: PathBuf,
|
||||||
pub limits: Limits,
|
pub limits: Limits,
|
||||||
}
|
}
|
||||||
@ -257,7 +259,7 @@ enum JobResult {
|
|||||||
pub fn make_client(cfg: &RunConfig, model: &str) -> Result<Box<dyn ModelClient>, String> {
|
pub fn make_client(cfg: &RunConfig, model: &str) -> Result<Box<dyn ModelClient>, String> {
|
||||||
match cfg.backend.as_str() {
|
match cfg.backend.as_str() {
|
||||||
"ollama" => Ok(Box::new(OllamaClient::new(&cfg.endpoint_url, model)?)),
|
"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)")),
|
other => Err(format!("Unbekanntes Backend: {other} (erlaubt: ollama, openai)")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -456,6 +458,7 @@ mod tests {
|
|||||||
runtime: "podman".into(),
|
runtime: "podman".into(),
|
||||||
backend: "ollama".into(),
|
backend: "ollama".into(),
|
||||||
endpoint_url: url,
|
endpoint_url: url,
|
||||||
|
api_key: None,
|
||||||
data_dir: base.join("data"),
|
data_dir: base.join("data"),
|
||||||
limits: Limits::default(),
|
limits: Limits::default(),
|
||||||
};
|
};
|
||||||
@ -499,6 +502,7 @@ mod tests {
|
|||||||
runtime: "podman".into(),
|
runtime: "podman".into(),
|
||||||
backend: "ollama".into(),
|
backend: "ollama".into(),
|
||||||
endpoint_url: url,
|
endpoint_url: url,
|
||||||
|
api_key: None,
|
||||||
data_dir: data.clone(),
|
data_dir: data.clone(),
|
||||||
limits: Limits::default(),
|
limits: Limits::default(),
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user