Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(audit): label vault POST as updated when DB row exists

userabd51fc30e1e
diff --git a/litellm/proxy/management_endpoints/config_override_endpoints.py b/litellm/proxy/management_endpoints/config_override_endpoints.py
index 230ed7e425c..04e4c323554 100644
--- a/litellm/proxy/management_endpoints/config_override_endpoints.py
+++ b/litellm/proxy/management_endpoints/config_override_endpoints.py
@@ -340,11 +340,11 @@ async def update_hashicorp_vault_config(
# Mutating the proxy's KMS config affects every secret retrieval going
# forward — emit an audit-log row so the action is traceable even
# though the secret_manager_client itself was just swapped under us.
- # ``existing_decrypted`` is only set when the DB row had a non-null
- # ``config_value`` (the same branch that ran the merge above);
- # otherwise fall back to whatever env vars were in scope.
+ # Action keys off row existence (a row with NULL ``config_value`` is
+ # still an update). ``before_config`` falls back to env vars when the
+ # row was absent or its ``config_value`` was NULL.
before_config = existing_decrypted if existing_decrypted is not None else env_values
- action: AUDIT_ACTIONS = "updated" if existing_decrypted is not None else "created"
+ action: AUDIT_ACTIONS = "updated" if existing_record is not None else "created"
await _emit_hashicorp_vault_audit_log(
action=action,
before_config=before_config,
diff --git a/tests/test_litellm/proxy/management_endpoints/test_config_override_endpoints.py b/tests/test_litellm/proxy/management_endpoints/test_config_override_endpoints.py
index a36ea287130..d90d589c504 100644
--- a/tests/test_litellm/proxy/management_endpoints/test_config_override_endpoints.py
+++ b/tests/test_litellm/proxy/management_endpoints/test_config_override_endpoints.py
@@ -1,6 +1,7 @@
+import asyncio
import json
import os
-from unittest.mock import AsyncMock, MagicMock
+from unittest.mock import AsyncMock, MagicMock, patch
import pytest
from fastapi.testclient import TestClient
@@ -297,8 +298,6 @@ class TestHashicorpVaultAuditLog:
async def capture(request_data):
audit_calls.append(request_data)
- from unittest.mock import patch
-
try:
with patch(
"litellm.proxy.management_helpers.audit_logs.create_audit_log_for_update",
@@ -312,8 +311,6 @@ class TestHashicorpVaultAuditLog:
},
)
assert r.status_code == 200
- import asyncio
-
for _ in range(3):
await asyncio.sleep(0)
@@ -332,9 +329,13 @@ class TestHashicorpVaultAuditLog:
_cleanup()
@pytest.mark.asyncio
- async def test_delete_emits_audit_log_only_when_row_existed(
+ async def test_post_action_is_updated_when_row_exists_with_null_config_value(
self, client, monkeypatch
):
+ """A row can exist in ``litellm_configoverrides`` with a NULL
+ ``config_value`` (e.g. an earlier failed write left a stub).
+ Re-POSTing must label the audit row as ``updated`` — the row
+ already exists — not ``created``."""
monkeypatch.setattr(litellm, "store_audit_logs", True)
mock_prisma, mock_db = _make_mock_db()
mock_cfg = _make_mock_proxy_config()
@@ -342,12 +343,52 @@ class TestHashicorpVaultAuditLog:
monkeypatch.setattr(ps, "proxy_config", mock_cfg)
_set_admin()
+ # Row exists but ``config_value`` is NULL.
+ null_record = MagicMock()
+ null_record.config_value = None
+ mock_db.find_unique = AsyncMock(return_value=null_record)
+
audit_calls = []
async def capture(request_data):
audit_calls.append(request_data)
- from unittest.mock import patch
+ try:
+ with patch(
+ "litellm.proxy.management_helpers.audit_logs.create_audit_log_for_update",
+ new=capture,
+ ):
+ r = client.post(
+ VAULT_URL,
+ json={
+ "vault_addr": "https://vault.example.com",
+ "vault_token": "tok",
+ },
+ )
+ assert r.status_code == 200
+ for _ in range(3):
+ await asyncio.sleep(0)
+
+ assert len(audit_calls) == 1
+ assert audit_calls[0].action == "updated"
+ finally:
+ _cleanup()
+
+ @pytest.mark.asyncio
+ async def test_delete_emits_audit_log_only_when_row_existed(
+ self, client, monkeypatch
+ ):
+ monkeypatch.setattr(litellm, "store_audit_logs", True)
+ mock_prisma, mock_db = _make_mock_db()
+ mock_cfg = _make_mock_proxy_config()
+ monkeypatch.setattr(ps, "prisma_client", mock_prisma)
+ monkeypatch.setattr(ps, "proxy_config", mock_cfg)
+ _set_admin()
+
+ audit_calls = []
+
+ async def capture(request_data):
+ audit_calls.append(request_data)
try:
with patch(
@@ -359,8 +400,6 @@ class TestHashicorpVaultAuditLog:
mock_db.delete = AsyncMock(side_effect=RecordNotFoundError(MagicMock()))
r = client.delete(VAULT_URL)
assert r.status_code == 200
- import asyncio
-
for _ in range(3):
await asyncio.sleep(0)
assert audit_calls == []
@@ -403,8 +442,6 @@ class TestHashicorpVaultAuditLog:
async def capture(request_data):
audit_calls.append(request_data)
- from unittest.mock import patch
-
try:
with patch(
"litellm.proxy.management_helpers.audit_logs.create_audit_log_for_update",