litellm/litellm-rust/crates/secrets/src/resolver.rs
devin-ai-integration[bot] 1a58162630
refactor(http): hand out an owned Client and route all providers through the pool (#43245)
* refactor(messages): take the provider client from the injected HTTP pool

The messages route kept its own process-wide reqwest client, so it ignored
ssl_verify, CA bundles, client certs, proxies and every other setting that
litellm-http resolves. The machine now takes the HttpClientPool and the
call's HttpClientConfig, as OCR does, and the bridge passes its shared pool.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(http): hand out an owned Client and move chat, audio and OIDC onto the pool

HttpClientPool now returns litellm_http::Client, a newtype only crates/http
can build, so every provider client carries the resolved TLS, proxy and
timeout settings. Chat completions and audio transcription drop their
process-wide reqwest clients and take the pool and call config like
messages; their 600s ceiling moves to the request. OidcResolver takes its
client instead of building one, and the bridge hands it the pooled one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(secrets): build Google, Azure and CyberArk manager clients from the pool

The native secret managers built bare reqwest clients, so they ignored the
host's TLS and proxy settings. load_native_manager now takes the pool and
the host config and hands each manager a pooled client.

CyberArk's CYBERARK_SSL_VERIFY and CYBERARK_CLIENT_CERT/KEY become an
override on the host config instead of a hand-built client. To express a
certificate and key in separate files, HttpClientConfig::client_certificate
is now a ClientIdentity that is either one PEM or a split pair.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* chore(clippy): only crates/http may build a reqwest client

Fence reqwest::Client, ClientBuilder and the TLS builder methods with
disallowed-types and disallowed-methods so new code takes a
litellm_http::Client from the pool. crates/http is exempt as the one place
clients are built, and testkit as a dev-only installer. Tests move to
litellm_http::Client::plain_for_test or a pooled client.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(secrets-cyberark): keep verifying certificates when the host disables it

Python hands CyberArk its own ssl_verify, which wins over the global
setting, so CYBERARK_SSL_VERIFY unset or true still verifies even when the
host sets ssl_verify false. The pooled client copied the host's Disabled
and would send the API key unverified; fall back to the built-in roots
instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(python-bridge): treat a missing litellm package as no host HTTP settings

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Yujong Lee <yujong@berri.ai>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-09-25 18:31:15 -07:00

195 lines
6 KiB
Rust

use std::sync::Arc;
use crate::compatibility::python_manager_string;
use litellm_core_utils::{serde_compat::parse_str_bool, settings::Lookup};
use crate::state::{LookupTarget, normalize_secret_name};
use crate::{Error, OidcResolver, Secret, SecretManagerState, SecretValue};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum FailurePolicy {
#[default]
Propagate,
EnvironmentFallback,
}
pub struct SecretResolver {
state: Arc<SecretManagerState>,
environment: Arc<dyn Lookup + Send + Sync>,
oidc: OidcResolver,
failure_policy: FailurePolicy,
python_compatible: bool,
}
impl SecretResolver {
pub fn new(
state: Arc<SecretManagerState>,
environment: Arc<dyn Lookup + Send + Sync>,
oidc: OidcResolver,
) -> Self {
Self {
state,
environment,
oidc,
failure_policy: FailurePolicy::default(),
python_compatible: false,
}
}
pub fn new_python_compatible(
state: Arc<SecretManagerState>,
environment: Arc<dyn Lookup + Send + Sync>,
oidc: OidcResolver,
) -> Self {
Self {
python_compatible: true,
failure_policy: FailurePolicy::EnvironmentFallback,
..Self::new(state, environment, oidc)
}
}
pub fn with_failure_policy(self, failure_policy: FailurePolicy) -> Self {
Self {
failure_policy,
..self
}
}
pub async fn get_secret(
&self,
name: &str,
default_value: Option<Secret>,
) -> Result<Option<Secret>, Error> {
let value = self.read(name, default_value.clone()).await?;
Ok(if self.python_compatible {
value
} else {
value.or(default_value)
})
}
async fn read(
&self,
name: &str,
default_value: Option<Secret>,
) -> Result<Option<Secret>, Error> {
let name = normalize_secret_name(name);
if name.starts_with("oidc/") {
return self
.oidc
.resolve(name, self.environment.as_ref())
.await
.map(|value| value.map(Secret::String));
}
let LookupTarget::Manager { backend, settings } = self.state.lookup_target(name) else {
return Ok(self.environment_value(name));
};
let result = if self.python_compatible {
crate::get_secret_from_python_manager(
backend,
name,
settings,
self.environment.as_ref(),
)
.await
} else {
crate::get_secret_from_manager(backend, name, settings, self.environment.as_ref()).await
};
match result {
Ok(value) => Ok(value.and_then(|value| self.manager_value(value))),
Err(error @ Error::ExternalManager(_)) => Err(error),
Err(error) => match self.failure_policy {
FailurePolicy::Propagate if self.python_compatible => {
default_value.map(Some).ok_or(error)
}
FailurePolicy::Propagate => Err(error),
FailurePolicy::EnvironmentFallback => Ok(self
.environment
.get(name)
.and_then(|value| self.manager_value(Secret::String(SecretValue::new(value))))),
},
}
}
pub async fn get_secret_str(
&self,
name: &str,
default_value: Option<SecretValue>,
) -> Result<Option<SecretValue>, Error> {
match self
.get_secret(name, default_value.map(Secret::String))
.await?
{
Some(Secret::String(value)) => Ok(Some(value)),
None => Ok(None),
Some(Secret::Bool(_) | Secret::Json(_)) if self.python_compatible => Ok(None),
Some(Secret::Bool(_) | Secret::Json(_)) => {
Err(Error::TypeMismatch { expected: "string" })
}
}
}
pub async fn get_secret_bool(
&self,
name: &str,
default_value: Option<bool>,
) -> Result<Option<bool>, Error> {
match self
.get_secret(name, default_value.map(Secret::Bool))
.await?
{
Some(Secret::Bool(value)) => Ok(Some(value)),
Some(Secret::String(value)) => match parse_str_bool(value.expose()) {
Some(value) => Ok(Some(value)),
None if self.python_compatible => Ok(None),
None => Err(Error::TypeMismatch {
expected: "boolean",
}),
},
Some(Secret::Json(_)) if self.python_compatible => Ok(None),
Some(Secret::Json(_)) => Err(Error::TypeMismatch {
expected: "boolean",
}),
None => Ok(None),
}
}
fn environment_value(&self, name: &str) -> Option<Secret> {
let value = self.environment.get(name)?;
if !self.python_compatible {
return Some(Secret::String(SecretValue::new(value)));
}
if self
.state
.settings()
.is_some_and(|settings| settings.access_mode.readable())
{
return Some(python_manager_string(SecretValue::new(value)));
}
Some(
parse_str_bool(&value)
.map_or_else(|| Secret::String(SecretValue::new(value)), Secret::Bool),
)
}
fn manager_value(&self, secret: Secret) -> Option<Secret> {
if !self.python_compatible {
return Some(secret);
}
let Secret::String(value) = secret else {
return None;
};
Some(python_manager_string(value))
}
}
pub fn normalize_nonempty_secret_str(value: Option<&str>) -> Option<&str> {
value
.map(|value| {
value.trim_matches(|character: char| {
character.is_whitespace() || matches!(character, '\u{1c}'..='\u{1f}')
})
})
.filter(|value| !value.is_empty())
}