Skip to main content

Fast, tamper-evident event auditing (hash chain) for Python apps, with a Rust core.

Project description

rust-py-audit

PyPI Python License GitHub

๐ŸŒ rust-py-audit.vercel.app

Event audit logging library for Python applications, with a Rust core.

Records audit events (who did what, when, on which resource) in a fast, structured way, and chains each event to the previous one with SHA-256 โ€” any later edit, deletion, or reordering of the log file is detectable with verify().


Features

  • AuditLogger โ€” simple API: log(...), verify(), last_hash()
  • Hash chain (SHA-256) โ€” each event embeds the hash of the previous event; altering any recorded event breaks the chain in a detectable way
  • JSONL storage โ€” one event per line, append-only, no database required
  • Free-form metadata โ€” any JSON-serializable dict (IP, reason, request_id, etc.)
  • FastAPI middleware โ€” automatically logs state-changing requests (POST/PUT/PATCH/DELETE)
  • Django middleware โ€” same idea, supports WSGI and ASGI
  • ImmutableLog integration โ€” local/remote/hybrid modes, automatic retry, pending queue, and flush_pending() (see dedicated section)
  • Rust core โ€” hash generation, serialization, I/O, and the ImmutableLog HTTP client all run in Rust via PyO3; the Python API stays simple

Requirements

  • Python 3.10+
  • No required runtime dependencies

Optional, installed separately:

  • fastapi + starlette โ€” for rust_py_audit.fastapi.AuditMiddleware
  • django โ€” for rust_py_audit.django.AuditMiddleware

Installation

pip install rust-py-audit

With optional extras:

pip install "rust-py-audit[fastapi]"
pip install "rust-py-audit[django]"

Quick Start

from rust_py_audit import AuditLogger

audit = AuditLogger(app_name="billing-api", file_path="./audit.jsonl")

event = audit.log(
    actor_id="user_123",
    action="DELETE_INVOICE",
    resource="invoice",
    resource_id="inv_987",
    metadata={"ip": "192.168.0.10", "reason": "duplicate invoice"},
)

print(event["id"])     # uuid v4
print(event["hash"])   # sha256, 64 hex characters

print(audit.last_hash())  # hash of the last recorded event

result = audit.verify()
print(result)
# {"valid": True, "total_events": 1, "last_hash": "..."}

Chain integrity

Each event records the hash of the previous event (previous_hash) and its own hash (hash), computed from the event's content + previous_hash. The first event in the chain has previous_hash = null.

{"id":"evt_123","timestamp":"2026-06-17T10:00:00Z","app_name":"billing-api","actor_id":"user_123","action":"DELETE_INVOICE","resource":"invoice","resource_id":"inv_987","metadata":{"ip":"192.168.0.10"},"previous_hash":null,"hash":"abc123..."}

verify() re-reads the file from scratch and recomputes everything โ€” it never trusts any in-memory cache:

result = audit.verify()

If the chain is intact:

{"valid": True, "total_events": 10, "last_hash": "..."}

If any event was edited, removed, or reordered:

{"valid": False, "total_events": 10, "error_index": 4, "reason": "hash_mismatch"}
# or "reason": "broken_chain" (removed/reordered/forged event)

FastAPI

from fastapi import FastAPI
from rust_py_audit.fastapi import AuditMiddleware

app = FastAPI()
app.add_middleware(AuditMiddleware, app_name="billing-api", file_path="./audit.jsonl")


@app.delete("/invoices/{invoice_id}")
async def delete_invoice(invoice_id: str):
    return {"deleted": invoice_id}

By default, only POST/PUT/PATCH/DELETE requests are logged. actor_id comes from the X-User-Id header (adjustable via actor_header=); falls back to "anonymous" if absent.

See the full example in examples/fastapi_app.py.


Django

# settings.py
MIDDLEWARE = [
    "rust_py_audit.django.AuditMiddleware",
    # ... other middlewares ...
]

# Optional:
RUST_PY_AUDIT_APP_NAME = "my-django-app"
RUST_PY_AUDIT_FILE_PATH = "./audit.jsonl"
RUST_PY_AUDIT_METHODS = {"POST", "PUT", "PATCH", "DELETE"}

actor_id comes from request.user.pk when there's an authenticated user (via django.contrib.auth); falls back to "anonymous" otherwise. The middleware supports both WSGI and ASGI applications automatically.

See the full example in examples/django_example/.


ImmutableLog Integration

rust_py_audit can send each audit event to ImmutableLog (documentation), in addition to โ€” or instead of โ€” writing locally.

Operating modes

mode Writes local JSONL Sends to ImmutableLog Typical use
"local" (default) โœ… โŒ The library's original behavior, no external dependency
"remote" โŒ (except for pending entries, see below) โœ… ImmutableLog is the single source of truth; a delivery failure raises an exception
"hybrid" โœ… โœ… Local chain + remote receipt; a delivery failure NEVER raises โ€” it becomes delivery_status="pending"

mode="local" is the default โ€” existing code calling AuditLogger(app_name, file_path) keeps working unchanged.

Basic example

from rust_py_audit import AuditLogger

audit = AuditLogger(
    app_name="billing-api",
    file_path="./audit.jsonl",
    mode="hybrid",
    immutablelog_url="https://api.immutablelog.com",
    immutablelog_api_key="iml_live_xxxxx",
    timeout_ms=500,
    retry_enabled=True,
    max_retries=3,
)

event = audit.log(
    actor_id="user_123",
    action="DELETE_INVOICE",
    resource="invoice",
    resource_id="inv_987",
    metadata={"ip": "192.168.0.10", "reason": "duplicate invoice"},
)

print(event["immutablelog"])
# {"status": "delivered", "tx_id": "tx_...", "payload_hash": "...", ...}
# or {"status": "pending", "tx_id": None, ...} if delivery failed (hybrid mode)

# Retries delivery of every event still marked "pending":
print(audit.flush_pending())
# {"flushed": 1, "still_pending": 0, "total": 1}

Environment variables

mode, immutablelog_url, and immutablelog_api_key accept None (the default) to fall back to an environment variable โ€” handy for not hardcoding credentials:

export RUST_PY_AUDIT_MODE=hybrid
export IMMUTABLELOG_URL=https://api.immutablelog.com
export IMMUTABLELOG_API_KEY=iml_live_xxxxx
# Without passing mode/immutablelog_url/immutablelog_api_key explicitly,
# they come from the environment variables above:
audit = AuditLogger(app_name="billing-api", file_path="./audit.jsonl")

An explicit parameter always takes priority over the environment variable. mode="remote"/"hybrid" without immutablelog_url/immutablelog_api_key (neither as a parameter nor as an env var) raises ValueError when the AuditLogger is created โ€” failing fast instead of only on the first log() call.

Severity, immutable_trail, and env

audit.log(...) accepts two optional parameters that only affect what gets sent to ImmutableLog (they never enter the hash):

event = audit.log(
    actor_id="user_123",
    action="DELETE_INVOICE",
    resource="invoice",
    resource_id="inv_987",
    severity="error",                    # meta.type โ€” defaults to "info" if omitted
    immutable_trail="order-2026-00441",  # meta.immutable_trail โ€” groups related events
)
  • severity must be one of "error", "warning", "info", "success" โ€” any other value raises ValueError, in any mode (even "local", where severity is just stored without being used).
  • immutable_trail is sanitized automatically (trimmed, : replaced with -, truncated at 256 chars); if it ends up empty after that, the field is omitted instead of being sent broken.
  • Both are preserved locally (without affecting hash) precisely so that flush_pending() can resend later with the same original classification.
  • immutablelog_env (on the AuditLogger constructor, falling back to the IMMUTABLELOG_ENV env var) sets meta.env โ€” useful for telling staging/production apart in ImmutableLog.

FastAPI

from fastapi import FastAPI
from rust_py_audit.fastapi import AuditMiddleware

app = FastAPI()
app.add_middleware(
    AuditMiddleware,
    app_name="billing-api",
    file_path="./audit.jsonl",
    mode="hybrid",
    immutablelog_url="https://api.immutablelog.com",
    immutablelog_api_key="iml_live_xxxxx",
    immutablelog_env="production",
    trail_header="X-Audit-Trail",  # default โ€” read from the request, becomes meta.immutable_trail
)

In mode="remote"/"hybrid", the middleware computes severity automatically from the response's status_code (>=400 โ†’ "error", 300-399 โ†’ "info", 200-299 โ†’ "success"). In mode="remote", if delivery fails the middleware logs a logging.warning(...) and moves on โ€” an audit failure never takes down the actual response already computed by the application.

Django

# settings.py
MIDDLEWARE = [
    "rust_py_audit.django.AuditMiddleware",
    # ... other middlewares ...
]

RUST_PY_AUDIT_MODE = "hybrid"
RUST_PY_AUDIT_FILE_PATH = "./audit.jsonl"
RUST_PY_AUDIT_IMMUTABLELOG_URL = "https://api.immutablelog.com"
RUST_PY_AUDIT_IMMUTABLELOG_API_KEY = "iml_live_xxxxx"
RUST_PY_AUDIT_IMMUTABLELOG_ENV = "production"
RUST_PY_AUDIT_TRAIL_HEADER = "X-Audit-Trail"  # default โ€” read from the request, becomes meta.immutable_trail

Same behavior as FastAPI: severity computed from status_code, and delivery failures logged via logging.warning(...) without affecting the response.

Retry and idempotency

  • retry_enabled/max_retries control how many times a retryable failure is retried (the same Idempotency-Key is used on every attempt โ€” never creates duplicate events on ImmutableLog).
  • Retryable: 5xx and timeouts.
  • Permanent (never retried): 400, 401, 403, 429, and any other client error.
  • In mode="remote", exhausting retries (or a permanent error) raises RuntimeError.
  • In mode="hybrid", the same scenario marks the event as delivery_status="pending", writes it to audit.pending.jsonl, and never raises โ€” call audit.flush_pending() (manually, or from a cron/worker) to retry later.

Integrity guarantee

The local hash (event["hash"]) is computed before any delivery attempt and never includes the immutablelog field โ€” the remote receipt is operational metadata, attached afterward, and never invalidates verify():

audit.log(...)        # hash computed, event already recorded/chained
audit.flush_pending()  # only updates event["immutablelog"]; event["hash"] doesn't change
audit.verify()         # still valid, even after flush_pending()

API Reference

AuditLogger(app_name, file_path="./audit.jsonl", mode=None, immutablelog_url=None, immutablelog_api_key=None, timeout_ms=500, retry_enabled=True, max_retries=3, immutablelog_env=None)

Parameter Type Description
app_name str Application name, recorded on every event
file_path str Path to the JSONL file. If it already exists, the chain resumes from the last recorded hash
mode str | None "local" (default) / "remote" / "hybrid". None falls back to RUST_PY_AUDIT_MODE, and finally to "local"
immutablelog_url str | None ImmutableLog base URL. None falls back to IMMUTABLELOG_URL. Required (one way or another) in mode="remote"/"hybrid"
immutablelog_api_key str | None API key (Bearer). None falls back to IMMUTABLELOG_API_KEY. Same requirement as immutablelog_url
timeout_ms int HTTP request timeout to ImmutableLog, in milliseconds
retry_enabled bool If True, retries retryable errors (5xx, timeout) up to max_retries times
max_retries int Maximum number of retries (in addition to the initial attempt)
immutablelog_env str | None Logical environment (meta.env, e.g. "production"). None falls back to IMMUTABLELOG_ENV; if neither is set, the field is omitted

See ImmutableLog Integration for details on each mode.


audit.log(actor_id, action, resource, resource_id, metadata=None, severity=None, immutable_trail=None) โ†’ dict

Records an event and returns the full event (already with id, timestamp, hash, etc.) as a dict. severity/immutable_trail are optional and only affect delivery to ImmutableLog โ€” see Severity, immutable_trail, and env.

Event field Type Description
id str UUID v4
timestamp str RFC3339 / UTC, e.g.: 2026-06-17T10:00:00Z
app_name str Comes from the AuditLogger
actor_id str Who performed the action
action str E.g.: DELETE_INVOICE
resource str E.g.: invoice
resource_id str E.g.: inv_987
metadata dict Free-form โ€” any JSON-serializable value
previous_hash str | None Hash of the previous event in the chain
hash str SHA-256 (64 hex chars) of the event + previous_hash
severity str | absent Only present if passed to log(). Becomes meta.type on ImmutableLog
immutable_trail str | absent Only present if passed to log() (and not empty after sanitization). Becomes meta.immutable_trail
immutablelog dict | absent Only present in mode="remote"/"hybrid". status is "delivered" or "pending"; other fields (tx_id, payload_hash, duplicate, request_id, ...) come from the ImmutableLog response

In mode="remote", a permanent failure or exhausted retries raise RuntimeError instead of returning the dict.


audit.verify() โ†’ dict

Re-reads the file and revalidates the entire chain from scratch. See Chain integrity. Unaffected by the immutablelog field โ€” only the hashed fields matter (see Integrity guarantee).


audit.last_hash() โ†’ str | None

Hash of the last recorded event (in-memory cache, O(1)) โ€” None if no event has been recorded yet.


audit.flush_pending() โ†’ dict

Attempts to redeliver to ImmutableLog every event marked as pending (recorded in audit.pending.jsonl, derived from file_path). Only relevant in mode="hybrid" โ€” other modes never populate this queue.

{"flushed": 1, "still_pending": 0, "total": 1}

On success, updates event["immutablelog"] in audit.jsonl (without changing hash) and removes the event from the queue. Events that fail again stay in the queue for the next call.


Building from Source

Requires Rust and maturin.

git clone https://github.com/robertolima-dev/rust-py-audit
cd rust-py-audit

python3 -m venv .venv
source .venv/bin/activate
pip install maturin

# Development build (installs into the current Python environment)
maturin develop

# Release wheel
maturin build --release

Running tests

# Rust unit tests
cargo test --no-default-features

# Python integration tests
pip install -e ".[dev]"
pytest tests/

Architecture

Python API (rust_py_audit)
    โ”œโ”€โ”€ AuditLogger(...)         โ”€โ”€โ–บ src/audit_logger.rs (PyO3 #[pyclass])
    โ”‚       โ”œโ”€โ”€ log()            โ”€โ”€โ–บ src/event.rs               (AuditEvent)
    โ”‚       โ”‚                    โ”€โ”€โ–บ src/hash.rs                (deterministic SHA-256)
    โ”‚       โ”‚                    โ”€โ”€โ–บ src/storage.rs             (append/update in JSONL)
    โ”‚       โ”‚                    โ”€โ”€โ–บ src/immutablelog_client.rs (POST /v1/events, via reqwest)
    โ”‚       โ”‚                    โ”€โ”€โ–บ src/retry.rs               (retry with Idempotency-Key)
    โ”‚       โ”œโ”€โ”€ verify()         โ”€โ”€โ–บ src/verifier.rs (revalidates the local chain)
    โ”‚       โ”œโ”€โ”€ flush_pending()  โ”€โ”€โ–บ redelivers audit.pending.jsonl
    โ”‚       โ””โ”€โ”€ last_hash()      โ”€โ”€โ–บ in-memory cache
    โ”‚
    โ”œโ”€โ”€ fastapi.AuditMiddleware โ”€โ”€โ–บ audit.log() on every mutating request
    โ””โ”€โ”€ django.AuditMiddleware  โ”€โ”€โ–บ same idea, WSGI/ASGI

src/immutablelog_config.rs holds AuditMode/ImmutableLogConfig; src/immutablelog_receipt.rs defines the ImmutableLogReceipt attached to each event.

The core is compiled into a native extension (.so/.pyd) by maturin and PyO3. The Python layer is thin โ€” it just routes calls and provides the framework adapters.


License

MIT โ€” see LICENSE.

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

rust_py_audit-0.2.3.tar.gz (78.3 kB view details)

Uploaded Source

Built Distributions

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

rust_py_audit-0.2.3-cp310-abi3-win_amd64.whl (1.7 MB view details)

Uploaded CPython 3.10+Windows x86-64

rust_py_audit-0.2.3-cp310-abi3-musllinux_1_2_x86_64.whl (2.3 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ x86-64

rust_py_audit-0.2.3-cp310-abi3-musllinux_1_2_aarch64.whl (2.3 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

rust_py_audit-0.2.3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.1 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

rust_py_audit-0.2.3-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.1 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

rust_py_audit-0.2.3-cp310-abi3-macosx_11_0_arm64.whl (1.9 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

rust_py_audit-0.2.3-cp310-abi3-macosx_10_12_x86_64.whl (1.9 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file rust_py_audit-0.2.3.tar.gz.

File metadata

  • Download URL: rust_py_audit-0.2.3.tar.gz
  • Upload date:
  • Size: 78.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.14.1

File hashes

Hashes for rust_py_audit-0.2.3.tar.gz
Algorithm Hash digest
SHA256 00f0c5ba3ec0a92fe2f6e69821056cbe6afa05fa596f22374d96a13c697edb9f
MD5 f4be36ef8e116bbec951ba512348eb20
BLAKE2b-256 ca07e83c448929b6a6eeab8aa475897b47bc6436b3f25bd09d2e8db759e8ad36

See more details on using hashes here.

File details

Details for the file rust_py_audit-0.2.3-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for rust_py_audit-0.2.3-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 e9f5fa703707eca01f299d80fc23feaecc66ad6a0dc31270231b536d1a37b600
MD5 2415882c89f19a66bceb6300f427e996
BLAKE2b-256 2d5a7d2bd8c8455327a56410fab7e68fe3974d3a0728eb22a39e06b063240f21

See more details on using hashes here.

File details

Details for the file rust_py_audit-0.2.3-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for rust_py_audit-0.2.3-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 b173e5e7b8ff81e1f245554e06dd43288504e60383cc5ba18bcd76fcaa78271d
MD5 dd1f93be14161da46c313bfd524f7fcb
BLAKE2b-256 e58a51ec17f181ecf03ba8f5149621265c05aa7a3bc5d85147c0dbed17467287

See more details on using hashes here.

File details

Details for the file rust_py_audit-0.2.3-cp310-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for rust_py_audit-0.2.3-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 2cd87b0368394bb7ce2a16f06e4eff05cb49f153d77ece2c7d03a46f67ae70fb
MD5 72bc62679e14de9eb3d551cc826f2d59
BLAKE2b-256 0d3547a1c3473f5665c6e47f6954536574805593173bdb539e9fe9964edba248

See more details on using hashes here.

File details

Details for the file rust_py_audit-0.2.3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for rust_py_audit-0.2.3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 4788d4e3f7fa2a679ca66931efd2295161dc6aba645554c33a9e8f128b12894b
MD5 74d04deda9ab349780338e080a6b0ebd
BLAKE2b-256 a6db2d90a7790fe39bd362a857556d4373e36e52c5c71de15f3b0e0380476248

See more details on using hashes here.

File details

Details for the file rust_py_audit-0.2.3-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for rust_py_audit-0.2.3-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 c4f44e4e4fdaa59321301f55a5578ebcb95853c0c6cc56790adccab201a961c0
MD5 487c0eea8acdc389b4a8596731d2c16c
BLAKE2b-256 9994c715cd1518b8cfd61c0e66beeef14ddd4f381b974a625cae62ecb2bce693

See more details on using hashes here.

File details

Details for the file rust_py_audit-0.2.3-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rust_py_audit-0.2.3-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 3a7b5e3426034a9e17e07ff0cdcadfba73a78cd866fd72e5569dc3a95029e1bb
MD5 4da09d20f29af0c9654269acccb0f339
BLAKE2b-256 6bb630c42174fa53f8a755ff14cd46c4aea1a7e1a2db12547bac304c6a9a844e

See more details on using hashes here.

File details

Details for the file rust_py_audit-0.2.3-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for rust_py_audit-0.2.3-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 d7a5857cb097addb9ccdfb8837557717c88dbeffb54045c09ba5d11e55074f2b
MD5 7e31a80476fae7b89706786587e2a9ac
BLAKE2b-256 8eed43cbef47f2b58b990deae6802097543e3973aa6a0659f2d8bdb3b4fc5bc3

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