refactor(secrets): move Vault Python policy into bridge

This commit is contained in:
Yujong Lee 2026-09-22 22:02:17 -07:00
parent 0e05943203
commit 2b19b62f03
9 changed files with 373 additions and 339 deletions

View file

@ -3175,6 +3175,7 @@ dependencies = [
"tokio",
"tokio-tungstenite",
"url",
"veil",
"wiremock",
]

View file

@ -55,6 +55,7 @@ pyo3-async-runtimes.workspace = true
reqwest.workspace = true
redis = { version = "1.7.0", features = ["tls-rustls"] }
serde_json.workspace = true
veil.workspace = true
thiserror.workspace = true
tokio = { workspace = true, features = ["rt", "sync"] }
url.workspace = true

View file

@ -63,7 +63,7 @@ pub(super) async fn read_python_provider(
#[derive(Debug)]
pub(super) enum PythonMutationError {
Unsupported,
Vault(Box<litellm_secrets::hashicorp::PythonFailure>),
Vault(Box<super::vault::Failure>),
CyberarkWrite {
name: String,
failure: Box<litellm_secrets::cyberark::WriteFailure>,
@ -187,19 +187,19 @@ pub(super) async fn write_python_provider_with_context(
if let (SecretManager::HashicorpVault(client), SecretOperationContext::Hashicorp(operation)) =
(manager, &context.operation)
{
return client
.write_for_python(
name,
value,
&litellm_secrets_types::SecretWriteContext {
description: context.description.clone(),
tags: context.tags.clone(),
operation: operation.clone(),
},
)
.await
.map(PythonMutationResponse::Json)
.map_err(|failure| PythonMutationError::Vault(Box::new(failure)));
return super::vault::write(
client,
name,
value,
&litellm_secrets_types::SecretWriteContext {
description: context.description.clone(),
tags: context.tags.clone(),
operation: operation.clone(),
},
)
.await
.map(PythonMutationResponse::Json)
.map_err(|failure| PythonMutationError::Vault(Box::new(failure)));
}
write_python_provider(manager, name, value)
.await
@ -214,8 +214,7 @@ pub(super) async fn delete_python_provider_with_context(
if let (SecretManager::HashicorpVault(client), SecretOperationContext::Hashicorp(context)) =
(manager, context)
{
client
.delete_for_python(name, context)
super::vault::delete(client, name, context)
.await
.map_err(|failure| PythonMutationError::Vault(Box::new(failure)))?;
return Ok(PythonMutationResponse::Value(serde_json::json!({
@ -237,8 +236,7 @@ pub(super) async fn rotate_python_provider_with_context(
if let (SecretManager::HashicorpVault(client), SecretOperationContext::Hashicorp(context)) =
(manager, context)
{
return client
.rotate_for_python(current_name, new_name, value, context)
return super::vault::rotate(client, current_name, new_name, value, context)
.await
.map(PythonMutationResponse::Json)
.map_err(|failure| PythonMutationError::Vault(Box::new(failure)));

View file

@ -1,37 +1,41 @@
use litellm_secrets::hashicorp::{Error, PythonFailure, PythonFailureKind, PythonFailureStage};
mod operation;
pub(super) use operation::{Failure, FailureKind, FailureStage, delete, rotate, write};
use litellm_secrets::hashicorp::{Error, RawOperationError};
use pyo3::prelude::*;
use super::mutation::{error_value, http_message, json_value};
pub(super) fn failure_value(
py: Python<'_>,
failure: PythonFailure,
failure: Failure,
context: &ErrorContext,
) -> PyResult<Py<PyAny>> {
let message = match *failure.kind {
PythonFailureKind::Http {
FailureKind::Native(RawOperationError::Http {
method,
url,
status,
body,
} => match failure.stage {
PythonFailureStage::Current(name) if status == 404 => {
}) => match failure.stage {
FailureStage::Current(name) if status == 404 => {
format!("Current secret {name} not found")
}
PythonFailureStage::Replacement(name) if status == 404 => {
FailureStage::Replacement(name) if status == 404 => {
format!("Failed to verify new secret {name}")
}
PythonFailureStage::Current(_) => format!(
FailureStage::Current(_) => format!(
"HTTP error occurred while checking current secret: {}",
response_text(py, &body)?
),
PythonFailureStage::Replacement(_) => format!(
FailureStage::Replacement(_) => format!(
"HTTP error occurred while verifying new secret: {}",
response_text(py, &body)?
),
PythonFailureStage::Mutation => http_message(py, &method, &url, status)?,
FailureStage::Mutation => http_message(py, &method, &url, status)?,
},
PythonFailureKind::ValueMismatch { expected, actual } => {
FailureKind::ValueMismatch { expected, actual } => {
let actual = json_value(py, &actual)?;
format!(
"New secret value mismatch. Expected: {}, Got: {}",
@ -42,30 +46,26 @@ pub(super) fn failure_value(
kind => {
let message = cause_message(py, kind, context)?;
match failure.stage {
PythonFailureStage::Current(_) => {
FailureStage::Current(_) => {
format!("Error checking current secret: {message}")
}
PythonFailureStage::Replacement(_) => {
FailureStage::Replacement(_) => {
format!("Error verifying new secret: {message}")
}
PythonFailureStage::Mutation => message,
FailureStage::Mutation => message,
}
}
};
error_value(py, message)
}
fn cause_message(
py: Python<'_>,
kind: PythonFailureKind,
context: &ErrorContext,
) -> PyResult<String> {
fn cause_message(py: Python<'_>, kind: FailureKind, context: &ErrorContext) -> PyResult<String> {
Ok(match kind {
PythonFailureKind::Local(error) => error.to_string(),
PythonFailureKind::UnsafeName(name) => {
FailureKind::Native(RawOperationError::Local(error)) => error.to_string(),
FailureKind::UnsafeName(name) => {
format!("Invalid secret_name {}", name.into_pyobject(py)?.repr()?)
}
PythonFailureKind::Timeout { method, elapsed } => {
FailureKind::Native(RawOperationError::Timeout { method, elapsed }) => {
if method == "POST" {
let elapsed = py
.import("builtins")?
@ -93,7 +93,7 @@ fn cause_message(
String::new()
}
}
PythonFailureKind::Transport(source) => {
FailureKind::Native(RawOperationError::Transport(source)) => {
if let Some(error) = request_error(&source) {
if error.is_timeout() {
String::new()
@ -106,22 +106,22 @@ fn cause_message(
"HashiCorp Vault request failed".to_owned()
}
}
PythonFailureKind::MissingGet(value) => {
FailureKind::MissingGet(value) => {
let value = json_value(py, &value)?;
match value.bind(py).getattr("get") {
Err(error) => error.value(py).str()?.extract()?,
Ok(_) => "HashiCorp Vault response payload is malformed".to_owned(),
}
}
PythonFailureKind::Json(body) => match json_value(py, &body) {
FailureKind::Json(body) => match json_value(py, &body) {
Err(error) => error.value(py).str()?.extract()?,
Ok(_) => "HashiCorp Vault response payload is malformed".to_owned(),
},
PythonFailureKind::Authentication {
FailureKind::Native(RawOperationError::Authentication {
source,
url,
certificate,
} => {
}) => {
let message = match source {
Error::LoginStatus { status } => http_message(py, "POST", &url, status)?,
error => error.to_string(),
@ -129,13 +129,13 @@ fn cause_message(
let mechanism = if certificate { "TLS cert" } else { "AppRole" };
format!("Could not authenticate to Vault via {mechanism}: {message}")
}
PythonFailureKind::Http {
FailureKind::Native(RawOperationError::Http {
method,
url,
status,
..
} => http_message(py, &method, &url, status)?,
PythonFailureKind::ValueMismatch { .. } => "New secret value mismatch".to_owned(),
}) => http_message(py, &method, &url, status)?,
FailureKind::ValueMismatch { .. } => "New secret value mismatch".to_owned(),
})
}

View file

@ -0,0 +1,168 @@
use std::collections::HashMap;
use litellm_secrets::{
SecretValue,
hashicorp::{Error, HashicorpVault, RawOperationError},
};
use litellm_secrets_types::{HashicorpOperationContext, SecretWriteContext};
use serde_json::value::RawValue;
#[derive(Debug)]
pub(crate) enum FailureStage {
Mutation,
Current(String),
Replacement(String),
}
#[derive(veil::Redact)]
pub(crate) enum FailureKind {
Native(RawOperationError),
UnsafeName(#[redact] String),
Json(#[redact] Vec<u8>),
MissingGet(#[redact] Vec<u8>),
ValueMismatch {
expected: SecretValue,
#[redact]
actual: Vec<u8>,
},
}
#[derive(Debug)]
pub(crate) struct Failure {
pub kind: Box<FailureKind>,
pub stage: FailureStage,
}
impl From<FailureKind> for Failure {
fn from(kind: FailureKind) -> Self {
Self {
kind: Box::new(kind),
stage: FailureStage::Mutation,
}
}
}
impl Failure {
fn during(self, stage: FailureStage) -> Self {
if matches!(*self.kind, FailureKind::UnsafeName(_)) {
self
} else {
Self { stage, ..self }
}
}
}
fn native_failure(name: &str, error: RawOperationError) -> Failure {
match error {
RawOperationError::Local(Error::InvalidSecretName(_)) => {
FailureKind::UnsafeName(name.to_owned()).into()
}
error => FailureKind::Native(error).into(),
}
}
pub(crate) async fn write(
client: &HashicorpVault,
name: &str,
value: &SecretValue,
context: &SecretWriteContext<HashicorpOperationContext>,
) -> Result<Vec<u8>, Failure> {
client
.write_raw(name, value, context)
.await
.map_err(|error| native_failure(name, error))
}
pub(crate) async fn delete(
client: &HashicorpVault,
name: &str,
context: &HashicorpOperationContext,
) -> Result<(), Failure> {
client
.delete_raw(name, context)
.await
.map_err(|error| native_failure(name, error))
}
pub(crate) async fn rotate(
client: &HashicorpVault,
current_name: &str,
new_name: &str,
value: &SecretValue,
context: &HashicorpOperationContext,
) -> Result<Vec<u8>, Failure> {
client
.read_raw(current_name, context)
.await
.map_err(|error| {
native_failure(current_name, error)
.during(FailureStage::Current(current_name.to_owned()))
})?;
let response = write(
client,
new_name,
value,
&SecretWriteContext {
description: Some(format!("Rotated from {current_name}")),
operation: context.clone(),
..SecretWriteContext::default()
},
)
.await?;
let parsed: &RawValue =
serde_json::from_slice(&response).map_err(|_| FailureKind::Json(response.clone()))?;
let status = raw_object_field(parsed, "status")
.ok()
.flatten()
.and_then(|value| serde_json::from_str::<String>(value.get()).ok());
if status.as_deref() == Some("error") {
return Ok(response);
}
let verification = client.read_raw(new_name, context).await.map_err(|error| {
native_failure(new_name, error).during(FailureStage::Replacement(new_name.to_owned()))
})?;
let parsed: &RawValue = serde_json::from_slice(&verification).map_err(|_| {
Failure::from(FailureKind::Json(verification.clone()))
.during(FailureStage::Replacement(new_name.to_owned()))
})?;
let data_key = context
.data_key
.as_deref()
.map(str::trim)
.filter(|key| !key.is_empty())
.unwrap_or("key");
let actual = verification_value(parsed, data_key).map_err(|failure| {
Failure::from(failure).during(FailureStage::Replacement(new_name.to_owned()))
})?;
let actual_string = serde_json::from_str::<String>(actual.get()).ok();
if actual_string.as_deref() != Some(value.expose()) {
return Err(FailureKind::ValueMismatch {
expected: value.clone(),
actual: actual.get().as_bytes().to_vec(),
}
.into());
}
if current_name != new_name {
let _ = delete(client, current_name, context).await;
}
Ok(response)
}
fn verification_value<'a>(document: &'a RawValue, key: &str) -> Result<&'a RawValue, FailureKind> {
let Some(outer) = raw_object_field(document, "data")? else {
return Ok(RawValue::NULL);
};
let Some(inner) = raw_object_field(outer, "data")? else {
return Ok(RawValue::NULL);
};
Ok(raw_object_field(inner, key)?.unwrap_or(RawValue::NULL))
}
fn raw_object_field<'a>(
document: &'a RawValue,
key: &str,
) -> Result<Option<&'a RawValue>, FailureKind> {
let object: HashMap<String, &RawValue> = serde_json::from_str(document.get())
.map_err(|_| FailureKind::MissingGet(document.get().as_bytes().to_vec()))?;
Ok(object.get(key).copied())
}

View file

@ -7,6 +7,4 @@ pub mod secret_manager;
pub use config::{AppRoleAuth, HashicorpVaultConfig, TlsCertAuth};
pub use error::Error;
pub use secret_manager::{
HashicorpVault, PythonFailure, PythonFailureKind, PythonFailureStage, SecretLocation,
};
pub use secret_manager::{HashicorpVault, RawOperationError, SecretLocation};

View file

@ -1,5 +1,5 @@
mod client;
mod python;
mod raw;
mod read;
mod write;
@ -31,7 +31,7 @@ use vaultrs::{
use crate::{Error, HashicorpVaultConfig, TlsCertAuth, cert_login::CertLoginRequest};
pub use python::{PythonFailure, PythonFailureKind, PythonFailureStage};
pub use raw::RawOperationError;
const CACHE_CAPACITY: u64 = 200;

View file

@ -1,287 +0,0 @@
use super::*;
use rustify::{client::Client as _, endpoint::Endpoint};
use serde_json::value::RawValue;
use vaultrs::api::kv2::requests::{
DeleteLatestSecretVersionRequest, ReadSecretRequest, SetSecretRequest,
};
#[derive(Debug)]
pub enum PythonFailureStage {
Mutation,
Current(String),
Replacement(String),
}
#[derive(veil::Redact)]
pub enum PythonFailureKind {
Local(Error),
UnsafeName(#[redact] String),
Authentication {
source: Error,
url: String,
certificate: bool,
},
Http {
method: String,
url: String,
status: u16,
#[redact]
body: Vec<u8>,
},
Transport(#[redact] RustifyClientError),
Timeout {
method: String,
elapsed: Duration,
},
Json(#[redact] Vec<u8>),
MissingGet(#[redact] Vec<u8>),
ValueMismatch {
expected: SecretValue,
#[redact]
actual: Vec<u8>,
},
}
#[derive(Debug)]
pub struct PythonFailure {
pub kind: Box<PythonFailureKind>,
pub stage: PythonFailureStage,
}
impl From<PythonFailureKind> for PythonFailure {
fn from(kind: PythonFailureKind) -> Self {
Self {
kind: Box::new(kind),
stage: PythonFailureStage::Mutation,
}
}
}
impl PythonFailure {
fn during(self, stage: PythonFailureStage) -> Self {
Self { stage, ..self }
}
}
impl HashicorpVault {
pub async fn write_for_python(
&self,
name: &str,
value: &SecretValue,
context: &SecretWriteContext<HashicorpOperationContext>,
) -> Result<Vec<u8>, PythonFailure> {
let location = self.python_location(name, &context.operation)?;
let data = super::write::write_data(value, context).map_err(PythonFailureKind::Local)?;
let response = self
.python_request(
&location,
SetSecretRequest {
mount: location.mount.clone(),
path: location.path.clone(),
data,
options: None,
},
&context.operation,
)
.await?;
self.cache
.invalidate_where(move |key| key.location == location);
Ok(response)
}
pub async fn delete_for_python(
&self,
name: &str,
context: &HashicorpOperationContext,
) -> Result<(), PythonFailure> {
let location = self.python_location(name, context)?;
self.python_request(
&location,
DeleteLatestSecretVersionRequest {
mount: location.mount.clone(),
path: location.path.clone(),
},
context,
)
.await?;
self.cache
.invalidate_where(move |key| key.location == location);
Ok(())
}
pub async fn rotate_for_python(
&self,
current_name: &str,
new_name: &str,
value: &SecretValue,
context: &HashicorpOperationContext,
) -> Result<Vec<u8>, PythonFailure> {
let current_location = self.python_location(current_name, context)?;
self.python_read_response(&current_location, context)
.await
.map_err(|failure| {
failure.during(PythonFailureStage::Current(current_name.to_owned()))
})?;
let response = self
.write_for_python(
new_name,
value,
&SecretWriteContext {
description: Some(format!("Rotated from {current_name}")),
operation: context.clone(),
..SecretWriteContext::default()
},
)
.await?;
let parsed: &RawValue = serde_json::from_slice(&response)
.map_err(|_| PythonFailureKind::Json(response.clone()))?;
let status = raw_object_field(parsed, "status")
.ok()
.flatten()
.and_then(|value| serde_json::from_str::<String>(value.get()).ok());
if status.as_deref() == Some("error") {
return Ok(response);
}
let new_location = self.python_location(new_name, context)?;
let verification = self
.python_read_response(&new_location, context)
.await
.map_err(|failure| {
failure.during(PythonFailureStage::Replacement(new_name.to_owned()))
})?;
let parsed: &RawValue = serde_json::from_slice(&verification).map_err(|_| {
PythonFailure::from(PythonFailureKind::Json(verification.clone()))
.during(PythonFailureStage::Replacement(new_name.to_owned()))
})?;
let actual = verification_value(parsed, &data_key(context)).map_err(|failure| {
PythonFailure::from(failure)
.during(PythonFailureStage::Replacement(new_name.to_owned()))
})?;
let actual_string = serde_json::from_str::<String>(actual.get()).ok();
if actual_string.as_deref() != Some(value.expose()) {
return Err(PythonFailureKind::ValueMismatch {
expected: value.clone(),
actual: actual.get().as_bytes().to_vec(),
}
.into());
}
if current_name != new_name {
let _ = self.delete_for_python(current_name, context).await;
}
self.cache
.invalidate_where(move |key| key.location == new_location);
Ok(response)
}
fn python_location(
&self,
name: &str,
context: &HashicorpOperationContext,
) -> Result<SecretLocation, PythonFailure> {
self.secret_location_with_context(name, context)
.map_err(|error| {
match error {
Error::InvalidSecretName(_) => PythonFailureKind::UnsafeName(name.to_owned()),
error => PythonFailureKind::Local(error),
}
.into()
})
}
async fn python_read_response(
&self,
location: &SecretLocation,
context: &HashicorpOperationContext,
) -> Result<Vec<u8>, PythonFailure> {
self.python_request(
location,
ReadSecretRequest {
mount: location.mount.clone(),
path: location.path.clone(),
version: None,
},
context,
)
.await
}
async fn python_request(
&self,
location: &SecretLocation,
endpoint: impl Endpoint,
context: &HashicorpOperationContext,
) -> Result<Vec<u8>, PythonFailure> {
let client = self.client_for_location(location).await.map_err(|source| {
let certificate = self.config.approle.is_none();
let mount = self
.config
.approle
.as_ref()
.map_or("cert", |auth| auth.mount_path.as_str());
PythonFailureKind::Authentication {
source,
certificate,
url: format!("{}/v1/auth/{mount}/login", self.config.address),
}
})?;
let request = endpoint
.with_middleware(&client.middle)
.request(client.http.base())
.map_err(PythonFailureKind::Transport)?;
let method = request.method().to_string();
let url = format!(
"{}/v1/{}{}/data/{}",
self.config.address,
location
.namespace
.as_ref()
.map(|ns| format!("{ns}/"))
.unwrap_or_default(),
location.mount,
location.path
);
let started = Instant::now();
let response = match context.timeout {
Some(timeout) => tokio::time::timeout(timeout, client.http.send(request))
.await
.map_err(|_| PythonFailureKind::Timeout {
method: method.clone(),
elapsed: started.elapsed(),
})?,
None => client.http.send(request).await,
}
.map_err(PythonFailureKind::Transport)?;
if !response.status().is_success() {
return Err(PythonFailureKind::Http {
method,
url,
status: response.status().as_u16(),
body: response.into_body(),
}
.into());
}
Ok(response.into_body())
}
}
fn verification_value<'a>(
document: &'a RawValue,
key: &str,
) -> Result<&'a RawValue, PythonFailureKind> {
let Some(outer) = raw_object_field(document, "data")? else {
return Ok(RawValue::NULL);
};
let Some(inner) = raw_object_field(outer, "data")? else {
return Ok(RawValue::NULL);
};
Ok(raw_object_field(inner, key)?.unwrap_or(RawValue::NULL))
}
fn raw_object_field<'a>(
document: &'a RawValue,
key: &str,
) -> Result<Option<&'a RawValue>, PythonFailureKind> {
let object: HashMap<String, &RawValue> = serde_json::from_str(document.get())
.map_err(|_| PythonFailureKind::MissingGet(document.get().as_bytes().to_vec()))?;
Ok(object.get(key).copied())
}

View file

@ -0,0 +1,155 @@
use super::*;
use rustify::{client::Client as _, endpoint::Endpoint};
use vaultrs::api::kv2::requests::{
DeleteLatestSecretVersionRequest, ReadSecretRequest, SetSecretRequest,
};
#[derive(veil::Redact)]
pub enum RawOperationError {
Local(Error),
Authentication {
source: Error,
url: String,
certificate: bool,
},
Http {
method: String,
url: String,
status: u16,
#[redact]
body: Vec<u8>,
},
Transport(#[redact] RustifyClientError),
Timeout {
method: String,
elapsed: Duration,
},
}
impl HashicorpVault {
pub async fn write_raw(
&self,
name: &str,
value: &SecretValue,
context: &SecretWriteContext<HashicorpOperationContext>,
) -> Result<Vec<u8>, RawOperationError> {
let location = self
.secret_location_with_context(name, &context.operation)
.map_err(RawOperationError::Local)?;
let data = super::write::write_data(value, context).map_err(RawOperationError::Local)?;
let response = self
.raw_request(
&location,
SetSecretRequest {
mount: location.mount.clone(),
path: location.path.clone(),
data,
options: None,
},
&context.operation,
)
.await?;
self.cache
.invalidate_where(move |key| key.location == location);
Ok(response)
}
pub async fn delete_raw(
&self,
name: &str,
context: &HashicorpOperationContext,
) -> Result<(), RawOperationError> {
let location = self
.secret_location_with_context(name, context)
.map_err(RawOperationError::Local)?;
self.raw_request(
&location,
DeleteLatestSecretVersionRequest {
mount: location.mount.clone(),
path: location.path.clone(),
},
context,
)
.await?;
self.cache
.invalidate_where(move |key| key.location == location);
Ok(())
}
pub async fn read_raw(
&self,
name: &str,
context: &HashicorpOperationContext,
) -> Result<Vec<u8>, RawOperationError> {
let location = self
.secret_location_with_context(name, context)
.map_err(RawOperationError::Local)?;
self.raw_request(
&location,
ReadSecretRequest {
mount: location.mount.clone(),
path: location.path.clone(),
version: None,
},
context,
)
.await
}
async fn raw_request(
&self,
location: &SecretLocation,
endpoint: impl Endpoint,
context: &HashicorpOperationContext,
) -> Result<Vec<u8>, RawOperationError> {
let client = self.client_for_location(location).await.map_err(|source| {
let certificate = self.config.approle.is_none();
let mount = self
.config
.approle
.as_ref()
.map_or("cert", |auth| auth.mount_path.as_str());
RawOperationError::Authentication {
source,
certificate,
url: format!("{}/v1/auth/{mount}/login", self.config.address),
}
})?;
let request = endpoint
.with_middleware(&client.middle)
.request(client.http.base())
.map_err(RawOperationError::Transport)?;
let method = request.method().to_string();
let url = format!(
"{}/v1/{}{}/data/{}",
self.config.address,
location
.namespace
.as_ref()
.map(|ns| format!("{ns}/"))
.unwrap_or_default(),
location.mount,
location.path
);
let started = Instant::now();
let response = match context.timeout {
Some(timeout) => tokio::time::timeout(timeout, client.http.send(request))
.await
.map_err(|_| RawOperationError::Timeout {
method: method.clone(),
elapsed: started.elapsed(),
})?,
None => client.http.send(request).await,
}
.map_err(RawOperationError::Transport)?;
if !response.status().is_success() {
return Err(RawOperationError::Http {
method,
url,
status: response.status().as_u16(),
body: response.into_body(),
});
}
Ok(response.into_body())
}
}