Skip to main content

Universal Audit Framework (UAF) — A production-ready, framework-agnostic audit logging SDK for Python.

Project description

Universal Audit Framework (UAF) SDK

PyPI version Python 3.12+ License: MIT

A production-ready audit logging SDK for Python. Auto-captures WHO did WHAT, WHEN — with zero code in your endpoints.


Installation

# With FastAPI middleware + PostgreSQL (most common)
pip install sumanth-audit-sdk[fastapi,postgres]

# Core only (REST transport)
pip install sumanth-audit-sdk

# All transports
pip install sumanth-audit-sdk[all]

FastAPI Middleware Setup (Recommended)

from fastapi import FastAPI
from starlette.middleware.sessions import SessionMiddleware
from audit_sdk.middleware.enhanced import (
    EnhancedAuditMiddleware,
    EnhancedAuditConfig,
)
from audit_sdk.transport.simple_database import SimpleAuditTransport

app = FastAPI()

# 1. Configure audit transport (writes to PostgreSQL)
transport = SimpleAuditTransport.for_postgres(
    host="localhost",
    port=5432,
    database="audit_db",
    username="your_user",
    password="your_password",
)

# 2. Add audit middleware
app.add_middleware(
    EnhancedAuditMiddleware,
    transport=transport,
    config=EnhancedAuditConfig(
        capture_request_body=True,
        excluded_paths=frozenset({"/health", "/docs", "/openapi.json"}),
    ),
)

# 3. Add session middleware (for user tracking)
app.add_middleware(SessionMiddleware, secret_key="your-secret-key")

Done. Every endpoint is now automatically audited. No audit.log() calls needed anywhere.


User Detection

The middleware auto-detects the logged-in user. Provide a UserProvider:

from audit_sdk.middleware.enhanced import EnhancedAuditMiddleware

class SessionUserProvider:
    async def get_user(self, request) -> dict | None:
        user = request.session.get("current_user")
        if user:
            return {
                "user_id": user["id"],
                "username": user["username"],
                "email": user.get("email", ""),
                "roles": user.get("roles", []),
            }
        return None

app.add_middleware(
    EnhancedAuditMiddleware,
    transport=transport,
    user_provider=SessionUserProvider(),
    config=EnhancedAuditConfig(capture_request_body=True),
)

What Gets Captured Automatically

Field Example
performed_by Sumanth
action CREATE, UPDATE, DELETE, LOGIN
action_message Sumanth created Employee John
functionality Employee Management
performed_on Employee #1
performed_at 2025-07-08 10:30:00+00
previous_state {"salary": 100000}
current_state {"salary": 120000}
changed_fields {"salary": {"old": 100000, "new": 120000}}

Transport Options

PostgreSQL (recommended)

from audit_sdk.transport.simple_database import SimpleAuditTransport

transport = SimpleAuditTransport.for_postgres(
    host="localhost", port=5432,
    database="audit_db", username="user", password="pass",
)

SQLite

transport = SimpleAuditTransport.for_sqlite(path="./audit.db")

MongoDB

pip install sumanth-audit-sdk[mongodb]
from audit_sdk.transport.mongodb import MongoDBTransport

transport = MongoDBTransport(uri="mongodb://localhost:27017", database="audit_db")

REST (HTTP collector)

from audit_sdk import AuditClient, AuditSettings

settings = AuditSettings(endpoint="https://audit.example.com/api/v1/events")
audit = AuditClient(settings=settings)

Audit Log Table (auto-created)

CREATE TABLE audit_log (
    id              VARCHAR(36) PRIMARY KEY,
    performed_by    TEXT NOT NULL,
    action          VARCHAR(100) NOT NULL,
    action_message  TEXT,
    functionality   VARCHAR(200) NOT NULL,
    performed_on    TEXT NOT NULL,
    performed_at    TIMESTAMPTZ NOT NULL,
    previous_state  JSONB,
    current_state   JSONB,
    changed_fields  JSONB,
    http_context    JSONB
);

License

MIT - see LICENSE

Copyright (c) 2025 Sumanth

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sumanth_audit_sdk-1.0.1.tar.gz (65.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sumanth_audit_sdk-1.0.1-py3-none-any.whl (84.7 kB view details)

Uploaded Python 3

File details

Details for the file sumanth_audit_sdk-1.0.1.tar.gz.

File metadata

  • Download URL: sumanth_audit_sdk-1.0.1.tar.gz
  • Upload date:
  • Size: 65.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.5

File hashes

Hashes for sumanth_audit_sdk-1.0.1.tar.gz
Algorithm Hash digest
SHA256 bcc26a69e2d82ab5d36a40c041bf93bcade5820b4f82cb9e8964ba189210f144
MD5 c134e0d1a999ee8cf860c3394bf65ce1
BLAKE2b-256 c18d921f9d6e90b63697f9c3e97150f29ec4597f3e253a29a78b15ee2f238e4a

See more details on using hashes here.

File details

Details for the file sumanth_audit_sdk-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sumanth_audit_sdk-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d58051259de1b0e7517021b4161345c106bfba2afc3fbca9127365580ca1000c
MD5 52c451bf70feada9ab83fbfdb8501d46
BLAKE2b-256 6941f9ad3036780a9922bb82b017ba6e45d92fb5a872ac00d89c00305ff4bce7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page