refactor(api): move audit-logs endpoint queries into MemoryEngine#1925
Merged
Conversation
The /audit-logs and /audit-logs/stats handlers ran raw SQL directly in the HTTP layer instead of going through a MemoryEngine method, violating the API-layer data-access standard (queries belong in the engine; auth/ tenancy enforced there). Mirrors the llm-requests pattern from #1922. - Add list_audit_logs / audit_log_stats engine methods. Both call get_bank_profile(create_if_missing=False) first, which runs _authenticate_tenant before any query, so the SQL is gated behind the same tenant auth every other op uses and scoped to the tenant schema. - Move the audit response models into engine/audit.py so the engine can build and return them; HTTP handlers now just delegate. - Add tenant-auth regression tests for both reads (invalid API key). OpenAPI spec unchanged (model names/fields identical). Closes #1923
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
The
/audit-logsand/audit-logs/statshandlers ran raw SQL directly in the HTTP layer (acquire_with_retry+fq_table) instead of going through aMemoryEnginemethod. This violated the API-layer data-access standard (queries belong in the engine; auth/tenancy enforced there). This PR refactors them to follow the same pattern as thellm-requestsendpoints in #1922.Closes #1923.
Changes
engine/memory_engine.py— addlist_audit_logs(...)andaudit_log_stats(...). Both start withget_bank_profile(..., create_if_missing=False), which calls_authenticate_tenant(request_context)before any query runs — so the SQL is now gated behind the same tenant authentication every other op uses and scoped to the authenticated tenant's schema. They returnNonefor a missing bank, which the HTTP layer maps to 404.engine/audit.py— move the audit response models (AuditLogEntry,AuditLogListResponse,AuditLogStatsBucket,AuditLogStatsResponse) here so the engine can build and return them directly (previously inline inhttp.py).api/http.py— the two handlers now just delegate to the engine methods; all raw SQL / connection handling removed.tests/test_extensions.py— add tenant-auth regression tests (test_list_audit_logs_fails_with_invalid_api_key,test_audit_log_stats_fails_with_invalid_api_key) verifying the reads reject an invalid API key like other ops.Verification
_authenticate_tenantinsideget_bank_profile, now reached before the query, and covered by the two new tests../scripts/hooks/lint.sh),ruff, andtyall pass.