mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-16 09:22:22 +00:00
feat(vertex_ai): add typed core authentication
This commit is contained in:
parent
92e76228cc
commit
26ba34dbba
6 changed files with 414 additions and 1 deletions
138
litellm-rust/Cargo.lock
generated
138
litellm-rust/Cargo.lock
generated
|
|
@ -26,6 +26,15 @@ dependencies = [
|
|||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "android_system_properties"
|
||||
version = "0.1.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ae221649c9976a6f6c56ae1facf410f3ddb33cc661c4b7b61020a912d4237fbc"
|
||||
dependencies = [
|
||||
"libc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "anes"
|
||||
version = "0.1.6"
|
||||
|
|
@ -664,6 +673,20 @@ dependencies = [
|
|||
"rand_core 0.10.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.45"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327"
|
||||
dependencies = [
|
||||
"iana-time-zone",
|
||||
"js-sys",
|
||||
"num-traits",
|
||||
"serde",
|
||||
"wasm-bindgen",
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ciborium"
|
||||
version = "0.2.2"
|
||||
|
|
@ -1156,6 +1179,33 @@ dependencies = [
|
|||
"slab",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "gcp_auth"
|
||||
version = "0.12.7"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "26d27dbcc645b60b8e7f6e2868a9d7102ece97d1bb49c1288b5321fcc67f7260"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"base64 0.22.1",
|
||||
"bytes",
|
||||
"chrono",
|
||||
"http 1.4.2",
|
||||
"http-body-util",
|
||||
"hyper 1.10.1",
|
||||
"hyper-rustls 0.27.9",
|
||||
"hyper-util",
|
||||
"ring",
|
||||
"rustls 0.23.42",
|
||||
"rustls-pki-types",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror 2.0.19",
|
||||
"tokio",
|
||||
"tracing",
|
||||
"tracing-futures",
|
||||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "generic-array"
|
||||
version = "0.14.7"
|
||||
|
|
@ -1452,6 +1502,30 @@ dependencies = [
|
|||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "iana-time-zone"
|
||||
version = "0.1.65"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e31bc9ad994ba00e440a8aa5c9ef0ec67d5cb5e5cb0cc7f8b744a35b389cc470"
|
||||
dependencies = [
|
||||
"android_system_properties",
|
||||
"core-foundation-sys",
|
||||
"iana-time-zone-haiku",
|
||||
"js-sys",
|
||||
"log",
|
||||
"wasm-bindgen",
|
||||
"windows-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "iana-time-zone-haiku"
|
||||
version = "0.1.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
|
||||
dependencies = [
|
||||
"cc",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "icu_collections"
|
||||
version = "2.2.0"
|
||||
|
|
@ -1715,6 +1789,7 @@ dependencies = [
|
|||
"azure_identity",
|
||||
"base64 0.22.1",
|
||||
"data-url",
|
||||
"gcp_auth",
|
||||
"moka",
|
||||
"rand 0.8.7",
|
||||
"reqwest 0.12.28",
|
||||
|
|
@ -3226,6 +3301,16 @@ dependencies = [
|
|||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-futures"
|
||||
version = "0.2.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "97d095ae15e245a057c8e8451bab9b3ee1e1f68e9ba2b4fbc18d0ac5237835f2"
|
||||
dependencies = [
|
||||
"pin-project",
|
||||
"tracing",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tracing-subscriber"
|
||||
version = "0.3.23"
|
||||
|
|
@ -3587,12 +3672,65 @@ version = "0.4.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
|
||||
|
||||
[[package]]
|
||||
name = "windows-core"
|
||||
version = "0.62.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b8e83a14d34d0623b51dce9581199302a221863196a1dde71a7663a4c2be9deb"
|
||||
dependencies = [
|
||||
"windows-implement",
|
||||
"windows-interface",
|
||||
"windows-link",
|
||||
"windows-result",
|
||||
"windows-strings",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-implement"
|
||||
version = "0.60.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.119",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-interface"
|
||||
version = "0.59.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.119",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-link"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
|
||||
|
||||
[[package]]
|
||||
name = "windows-result"
|
||||
version = "0.4.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7781fa89eaf60850ac3d2da7af8e5242a5ea78d1a11c49bf2910bb5a73853eb5"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-strings"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7837d08f69c77cf6b07689544538e017c1bfcf57e34b4c0ff58e6c2cd3b37091"
|
||||
dependencies = [
|
||||
"windows-link",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.52.0"
|
||||
|
|
|
|||
|
|
@ -39,6 +39,7 @@ tokio = { version = "1", features = ["rt-multi-thread", "macros", "time", "net"]
|
|||
tokio-tungstenite = { version = "0.24", default-features = false, features = ["connect", "rustls-tls-native-roots"] }
|
||||
futures-util = { version = "0.3", default-features = false, features = ["sink", "std"] }
|
||||
base64 = "0.22"
|
||||
gcp_auth = "0.12.7"
|
||||
azure_core = "1.0.0"
|
||||
azure_identity = { version = "1.0.0", features = ["tokio"] }
|
||||
moka = { version = "0.12.16", features = ["future"] }
|
||||
|
|
|
|||
|
|
@ -15,6 +15,7 @@ azure_core.workspace = true
|
|||
azure_identity.workspace = true
|
||||
base64.workspace = true
|
||||
data-url = "0.3.2"
|
||||
gcp_auth.workspace = true
|
||||
moka.workspace = true
|
||||
rand.workspace = true
|
||||
reqwest.workspace = true
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ pub enum AuthError {
|
|||
Configuration(#[from] AuthConfigurationError),
|
||||
#[error("credential acquisition failed: {0}")]
|
||||
AzureTokenAcquisition(String),
|
||||
#[error("credential acquisition failed: Vertex AI credentials: {0}")]
|
||||
VertexTokenAcquisition(String),
|
||||
#[error("credential acquisition failed: {}", .0.iter().map(ToString::to_string).collect::<Vec<_>>().join("; "))]
|
||||
CredentialChain(Vec<AuthError>),
|
||||
#[error("credential caller failed: credential caller returned an empty credential")]
|
||||
|
|
|
|||
271
litellm-rust/crates/core/src/providers/vertex_ai/auth.rs
Normal file
271
litellm-rust/crates/core/src/providers/vertex_ai/auth.rs
Normal file
|
|
@ -0,0 +1,271 @@
|
|||
use std::path::Path;
|
||||
use std::sync::Arc;
|
||||
|
||||
use gcp_auth::{CustomServiceAccount, TokenProvider};
|
||||
use serde_json::{Map, Value};
|
||||
|
||||
use crate::auth::error::AuthConfigurationError;
|
||||
use crate::auth::http::apply_credential;
|
||||
use crate::auth::{AuthError, CredentialPlacement, SecretValue};
|
||||
|
||||
const CLOUD_PLATFORM_SCOPE: &str = "https://www.googleapis.com/auth/cloud-platform";
|
||||
const GOOGLE_APPLICATION_CREDENTIALS_ENV: &str = "GOOGLE_APPLICATION_CREDENTIALS";
|
||||
const VERTEX_AI_API_KEY_ENV: &str = "VERTEX_AI_API_KEY";
|
||||
const VERTEXAI_API_KEY_ENV: &str = "VERTEXAI_API_KEY";
|
||||
const VERTEXAI_CREDENTIALS_ENV: &str = "VERTEXAI_CREDENTIALS";
|
||||
const VERTEXAI_PROJECT_ENV: &str = "VERTEXAI_PROJECT";
|
||||
const VERTEXAI_LOCATION_ENV: &str = "VERTEXAI_LOCATION";
|
||||
const VERTEX_LOCATION_ENV: &str = "VERTEX_LOCATION";
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct VertexAuthInputs {
|
||||
credentials: Option<SecretValue>,
|
||||
project_id: Option<String>,
|
||||
location: Option<String>,
|
||||
}
|
||||
|
||||
impl VertexAuthInputs {
|
||||
pub fn from_optional_params(params: &Map<String, Value>) -> Result<Self, AuthError> {
|
||||
let credentials = params
|
||||
.get("vertex_credentials")
|
||||
.or_else(|| params.get("vertex_ai_credentials"));
|
||||
let credentials = match credentials {
|
||||
None | Some(Value::Null) => None,
|
||||
Some(Value::String(value)) => Some(SecretValue::new(value)),
|
||||
Some(Value::Object(value)) => Some(SecretValue::new(
|
||||
serde_json::to_string(value).map_err(|error| {
|
||||
AuthError::Configuration(AuthConfigurationError::InvalidFieldType(format!(
|
||||
"vertex_credentials: {error}"
|
||||
)))
|
||||
})?,
|
||||
)),
|
||||
Some(_) => {
|
||||
return Err(AuthError::Configuration(
|
||||
AuthConfigurationError::InvalidFieldType("vertex_credentials".to_string()),
|
||||
));
|
||||
}
|
||||
};
|
||||
Ok(Self {
|
||||
credentials,
|
||||
project_id: optional_string(params, &["vertex_project", "vertex_ai_project"])?,
|
||||
location: optional_string(params, &["vertex_location", "vertex_ai_location"])?,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn project_id(&self) -> Option<&str> {
|
||||
self.project_id.as_deref()
|
||||
}
|
||||
|
||||
pub fn location(&self) -> Option<&str> {
|
||||
self.location.as_deref()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct VertexAuthentication {
|
||||
pub headers: Vec<(String, String)>,
|
||||
pub project_id: String,
|
||||
}
|
||||
|
||||
pub fn resolve_project_id(
|
||||
auth_inputs: &VertexAuthInputs,
|
||||
env_lookup: &dyn Fn(&str) -> Option<String>,
|
||||
) -> Option<String> {
|
||||
auth_inputs
|
||||
.project_id()
|
||||
.map(str::to_string)
|
||||
.or_else(|| non_empty_env(env_lookup, VERTEXAI_PROJECT_ENV))
|
||||
}
|
||||
|
||||
pub fn resolve_location(
|
||||
auth_inputs: &VertexAuthInputs,
|
||||
env_lookup: &dyn Fn(&str) -> Option<String>,
|
||||
) -> Option<String> {
|
||||
auth_inputs
|
||||
.location()
|
||||
.map(str::to_string)
|
||||
.or_else(|| non_empty_env(env_lookup, VERTEXAI_LOCATION_ENV))
|
||||
.or_else(|| non_empty_env(env_lookup, VERTEX_LOCATION_ENV))
|
||||
}
|
||||
|
||||
pub async fn authenticate(
|
||||
headers: Vec<(String, String)>,
|
||||
api_key: Option<&str>,
|
||||
auth_inputs: &VertexAuthInputs,
|
||||
project_id: Option<String>,
|
||||
env_lookup: &(dyn Fn(&str) -> Option<String> + Sync),
|
||||
) -> Result<VertexAuthentication, AuthError> {
|
||||
let has_authorization = headers
|
||||
.iter()
|
||||
.any(|(name, _)| name.eq_ignore_ascii_case("Authorization"));
|
||||
let static_token = api_key
|
||||
.map(str::trim)
|
||||
.filter(|value| !value.is_empty())
|
||||
.map(str::to_string)
|
||||
.or_else(|| non_empty_env(env_lookup, VERTEX_AI_API_KEY_ENV))
|
||||
.or_else(|| non_empty_env(env_lookup, VERTEXAI_API_KEY_ENV));
|
||||
let project_id = project_id.or_else(|| resolve_project_id(auth_inputs, env_lookup));
|
||||
let needs_provider = !has_authorization && static_token.is_none() || project_id.is_none();
|
||||
let provider = if needs_provider {
|
||||
Some(resolve_provider(auth_inputs, env_lookup).await?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let project_id = match project_id {
|
||||
Some(project_id) => project_id,
|
||||
None => required_provider(provider.as_ref())?
|
||||
.project_id()
|
||||
.await
|
||||
.map_err(auth_acquisition_error)?
|
||||
.to_string(),
|
||||
};
|
||||
let headers = if has_authorization {
|
||||
headers
|
||||
} else if let Some(token) = static_token {
|
||||
apply_credential(headers, &token, CredentialPlacement::Bearer)?
|
||||
} else {
|
||||
let token = required_provider(provider.as_ref())?
|
||||
.token(&[CLOUD_PLATFORM_SCOPE])
|
||||
.await
|
||||
.map_err(auth_acquisition_error)?;
|
||||
apply_credential(headers, token.as_str(), CredentialPlacement::Bearer)?
|
||||
};
|
||||
|
||||
Ok(VertexAuthentication {
|
||||
headers,
|
||||
project_id,
|
||||
})
|
||||
}
|
||||
|
||||
fn optional_string(
|
||||
params: &Map<String, Value>,
|
||||
names: &[&str],
|
||||
) -> Result<Option<String>, AuthError> {
|
||||
let value = names.iter().find_map(|name| params.get(*name));
|
||||
match value {
|
||||
None | Some(Value::Null) => Ok(None),
|
||||
Some(Value::String(value)) if !value.trim().is_empty() => Ok(Some(value.clone())),
|
||||
Some(Value::String(_)) => Ok(None),
|
||||
Some(_) => Err(AuthError::Configuration(
|
||||
AuthConfigurationError::InvalidFieldType(names[0].to_string()),
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
fn required_provider(
|
||||
provider: Option<&Arc<dyn TokenProvider>>,
|
||||
) -> Result<&Arc<dyn TokenProvider>, AuthError> {
|
||||
provider.ok_or(AuthError::MissingApiKey {
|
||||
provider: "Vertex AI",
|
||||
})
|
||||
}
|
||||
|
||||
async fn resolve_provider(
|
||||
auth_inputs: &VertexAuthInputs,
|
||||
env_lookup: &(dyn Fn(&str) -> Option<String> + Sync),
|
||||
) -> Result<Arc<dyn TokenProvider>, AuthError> {
|
||||
let configured = auth_inputs
|
||||
.credentials
|
||||
.as_ref()
|
||||
.map(SecretValue::expose)
|
||||
.map(str::to_string)
|
||||
.or_else(|| non_empty_env(env_lookup, VERTEXAI_CREDENTIALS_ENV));
|
||||
if let Some(configured) = configured {
|
||||
let service_account = if Path::new(&configured).is_file() {
|
||||
CustomServiceAccount::from_file(&configured)
|
||||
} else {
|
||||
CustomServiceAccount::from_json(&configured)
|
||||
}
|
||||
.map_err(auth_acquisition_error)?;
|
||||
return Ok(Arc::new(service_account));
|
||||
}
|
||||
|
||||
if let Some(path) = non_empty_env(env_lookup, GOOGLE_APPLICATION_CREDENTIALS_ENV) {
|
||||
return CustomServiceAccount::from_file(path)
|
||||
.map(|provider| Arc::new(provider) as Arc<dyn TokenProvider>)
|
||||
.map_err(auth_acquisition_error);
|
||||
}
|
||||
|
||||
gcp_auth::provider().await.map_err(auth_acquisition_error)
|
||||
}
|
||||
|
||||
fn non_empty_env(env_lookup: &dyn Fn(&str) -> Option<String>, name: &str) -> Option<String> {
|
||||
env_lookup(name).filter(|value| !value.trim().is_empty())
|
||||
}
|
||||
|
||||
fn auth_acquisition_error(error: gcp_auth::Error) -> AuthError {
|
||||
AuthError::VertexTokenAcquisition(error.to_string())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use serde_json::json;
|
||||
|
||||
use super::{VertexAuthInputs, authenticate};
|
||||
|
||||
#[test]
|
||||
fn credentials_accept_json_objects_without_exposing_secrets() {
|
||||
let inputs = VertexAuthInputs::from_optional_params(
|
||||
json!({"vertex_credentials": {"private_key": "secret-key"}})
|
||||
.as_object()
|
||||
.expect("object"),
|
||||
)
|
||||
.expect("credentials parse");
|
||||
|
||||
assert!(!format!("{inputs:?}").contains("secret-key"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn credentials_reject_non_json_values() {
|
||||
let error = VertexAuthInputs::from_optional_params(
|
||||
json!({"vertex_credentials": true})
|
||||
.as_object()
|
||||
.expect("object"),
|
||||
)
|
||||
.expect_err("boolean credentials are rejected");
|
||||
|
||||
assert!(error.to_string().contains("vertex_credentials"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn routing_inputs_are_typed_separately_from_model_params() {
|
||||
let inputs = VertexAuthInputs::from_optional_params(
|
||||
json!({
|
||||
"vertex_project": "project-1",
|
||||
"vertex_location": "europe-west4"
|
||||
})
|
||||
.as_object()
|
||||
.expect("object"),
|
||||
)
|
||||
.expect("inputs parse");
|
||||
|
||||
assert_eq!(inputs.project_id(), Some("project-1"));
|
||||
assert_eq!(inputs.location(), Some("europe-west4"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn explicit_access_token_does_not_require_adc() {
|
||||
let authentication = authenticate(
|
||||
Vec::new(),
|
||||
Some("access-token"),
|
||||
&VertexAuthInputs::from_optional_params(
|
||||
json!({"vertex_project": "project-1"})
|
||||
.as_object()
|
||||
.expect("object"),
|
||||
)
|
||||
.expect("inputs parse"),
|
||||
None,
|
||||
&|_| None,
|
||||
)
|
||||
.await
|
||||
.expect("static token authenticates");
|
||||
|
||||
assert_eq!(authentication.project_id, "project-1");
|
||||
assert_eq!(
|
||||
authentication.headers,
|
||||
vec![(
|
||||
"Authorization".to_string(),
|
||||
"Bearer access-token".to_string()
|
||||
)]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
@ -1 +1 @@
|
|||
|
||||
pub mod auth;
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue