Session Metadata / Auditing API in mssql-python - API Spec & Discussion #624
Replies: 1 comment 1 reply
-
|
@kapilsamant First of all, I think this is a very useful feature. Some recommendations/notes/questions/comments.
Can we use
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Issue #621
Session Metadata / Auditing API Specification
Overview
The
Connection.set_audit_context()/Connection.get_audit_context()methods provide session-level auditing and tracing metadata on SQL Server connections. Values are stored viasp_set_session_contextand are visible toSESSION_CONTEXT()in T-SQL, Extended Events,sys.dm_exec_sessions, and audit specifications.Is your feature request related to a problem?
There is no built-in way to attach session-level auditing or tracing metadata to a
Connectionin mssql-python. Applications that need to propagate end-user identity, module names, or action context to SQL Server for auditing or Extended Events tracing must manually execute rawsp_set_session_contextcalls, handle input validation themselves, and track which keys have been set. This is error-prone, verbose, and inconsistent across codebases.Applications that need to comply with regulations (GDPR, PCI-DSS, etc.) must propagate end-user identity, module context, and action tracking to SQL Server for audit trail generation and compliance reporting.
API Specification
Connection.set_audit_context()Connection.get_audit_context()Returns a copy of the locally cached session context dictionary. Does not round-trip to the server.
Parameters Reference
Well-Known Parameters
applicationapplication_namemodulemodule_nameactionaction_nameuser_iduser_idBehavior Parameters
read_onlyboolFalseTrue, keys become immutable for the remainder of the SQL Server session**extrastrValidation Rules
strsp_set_session_contextrequiressysnamekeyssysnamelimitstronlyProgrammingErrorsql_varianthas an 8,000-byte limit; driver binds strings as NVARCHAR (2 bytes/char), so 4,000 chars = 8,000 bytesException Types
ProgrammingErrorInterfaceErrorDatabaseErrorsp_set_session_contextexecution failure (e.g., writing to aread_onlykey)Behavior
""(empty string) — the driver sendsNULLtosp_set_session_contextand removes the key from the local cache.read_only=True: Makes the keys immutable for the remainder of the SQL Server session. Subsequent attempts to change them will raiseDatabaseErrorfrom the server.sp_set_session_contextcalls use parameterized queries to prevent SQL injection.get_audit_context()returns a copy of the internal dict to prevent accidental mutation.Usage Example
Values are now readable in T-SQL:
Visibility
Values set through this API are visible to:
SESSION_CONTEXT(N'<key>')sys.dm_exec_sessionsapplication_nameAlternatives Considered
Raw cursor execution —
cursor.execute("EXEC sp_set_session_context @key=?, @value=?", ...). Works but requires boilerplate, duplicates validation logic, and lacks discoverability.Connection string properties — Encoding audit metadata into the connection string (e.g.,
Application Name). Only covers a single field, is immutable after connect, and doesn't flow toSESSION_CONTEXT().Beta Was this translation helpful? Give feedback.
All reactions