Skip to main content

Asherah application-layer encryption for Python with automatic key rotation, powered by the native Rust implementation.

Project description

asherah

Python bindings for the Asherah envelope encryption and key rotation library.

Native Rust implementation via PyO3/maturin. Prebuilt wheels are published to PyPI for Linux (x86_64 and aarch64, both glibc and musl), macOS (x86_64 and arm64), and Windows (x86_64 and arm64).

Installation

pip install asherah

Requires Python ≥ 3.8.

Documentation

Task-oriented walkthroughs under docs/:

Guide When to read
Getting started First-time install through round-trip encrypt/decrypt.
Framework integration FastAPI, Flask, Django, AWS Lambda, Celery.
AWS production setup KMS keys, DynamoDB, IAM policy, region routing.
Testing pytest fixtures, Testcontainers, mocking patterns, asyncio test patterns.
Troubleshooting Common errors with what to check first.

Choosing an API style

Two API styles are exposed; both are fully supported and produce the same wire format. New code should prefer the Factory / Session API.

Style When to use
Static / module-level (asherah.setup, asherah.encrypt_bytes, …) Drop-in compatibility with the canonical godaddy/asherah-python package. Simplest call surface. Singleton lifecycle (setup() once, shutdown() once).
Factory / Session (asherah.SessionFactory, factory.get_session(...)) Recommended for new code. Explicit lifecycle, no hidden singleton, multi-tenant isolation is obvious in code. Context-manager friendly.

A complete runnable example exercising both styles plus async, log hook, and metrics hook is in samples/python/sample.py.

Quick start (static API)

import os
import asherah

os.environ["STATIC_MASTER_KEY_HEX"] = "22" * 32  # testing only

asherah.setup({
    "ServiceName": "my-service",
    "ProductID":   "my-product",
    "Metastore":   "memory",   # testing only — use "rdbms" or "dynamodb" in production
    "KMS":         "static",   # testing only — use "aws" in production
})

ct = asherah.encrypt_string("user-42", "secret")
pt = asherah.decrypt_string("user-42", ct)
assert pt == "secret"

asherah.shutdown()

Quick start (factory / session API)

import asherah

with asherah.SessionFactory() as factory:
    with factory.get_session("user-42") as session:
        ct = session.encrypt_text("secret")
        pt = session.decrypt_text(ct)
        assert pt == "secret"

SessionFactory reads its config from environment variables. Set them with asherah.setenv({...}) or via os.environ before constructing the factory.

Async API

There are two flavors of async to choose from depending on your call pattern:

  • Module-level async (encrypt_string_async, decrypt_string_async, setup_async, shutdown_async) — wraps the sync calls with loop.run_in_executor. Lowest setup, but the sync work runs on the default thread pool executor.

  • Session-level async (session.encrypt_bytes_async, session.decrypt_bytes_async) — true async PyO3 coroutines that run on the Rust tokio runtime. The asyncio event loop is not blocked, and there is no thread pool overhead.

import asyncio
import asherah

async def main():
    # Module-level
    await asherah.setup_async({...})
    ct = await asherah.encrypt_string_async("user-42", "secret")
    pt = await asherah.decrypt_string_async("user-42", ct)
    await asherah.shutdown_async()

    # Session-level (true async)
    with asherah.SessionFactory() as factory:
        session = factory.get_session("user-42")
        ct = await session.encrypt_bytes_async(b"secret")
        pt = await session.decrypt_bytes_async(ct)

asyncio.run(main())

Observability hooks

Log hook

Receive every log event from the Rust core (encrypt/decrypt path, metastore drivers, KMS clients).

def on_log(event):
    # event = {"level": "trace"|"debug"|"info"|"warn"|"error",
    #          "message": str, "target": str}
    if event["level"] in ("warn", "error"):
        print(f"[asherah {event['level']}] {event['message']}")

asherah.set_log_hook(on_log)

# later, to deregister:
asherah.set_log_hook(None)

The callback may fire from any thread (Rust tokio worker threads, DB driver threads). PyO3 acquires the GIL before invoking the callback, so the callback runs single-threaded from Python's perspective.

Metrics hook

Receive timing events for encrypt/decrypt/store/load and counter events for cache hit/miss/stale.

def on_metric(event):
    if event["type"] in ("encrypt", "decrypt", "store", "load"):
        # event = {"type": ..., "duration_ns": int}
        my_histogram.observe(event["type"], event["duration_ns"] / 1e6)
    else:
        # event = {"type": "cache_hit"|"cache_miss"|"cache_stale", "name": str}
        my_counter.inc(result=event["type"], cache=event["name"])

asherah.set_metrics_hook(on_metric)

# later:
asherah.set_metrics_hook(None)

Metrics collection is enabled automatically when a hook is installed and disabled when cleared.

Input contract

Partition ID (None, ""): always rejected as programming errors with TypeError (None) or ValueError/Exception ("partition id cannot be empty"). No row is ever written to the metastore under a degenerate partition ID.

Plaintext to encrypt:

  • NoneTypeError from PyO3 type conversion before any native call.
  • Empty str ("") and empty bytes (b"") are valid plaintexts. encrypt_string / encrypt_bytes produce a real DataRowRecord envelope; decrypt_string / decrypt_bytes return exactly "" or b"".

Ciphertext to decrypt:

  • NoneTypeError.
  • Empty str / bytes → exception from JSON parse (not valid DataRowRecord).

Do not short-circuit empty plaintext encryption in caller code — empty data is real data, encrypting it produces a genuine envelope, and skipping encryption leaks the fact that the value was empty. See docs/input-contract.md for the full rationale.

Configuration

setup() accepts a dict (or any JSON-serializable object) using PascalCase keys to match the canonical Go/Java/.NET API:

Key Type Required Description
ServiceName str yes Service identifier for the key hierarchy.
ProductID str yes Product identifier for the key hierarchy.
Metastore str yes "memory", "rdbms", or "dynamodb". "memory" is testing-only.
KMS str "static" (default; testing) or "aws".
ConnectionString str SQL connection string for rdbms.
SQLMetastoreDBType str "mysql" or "postgres" (paired with Metastore: "rdbms").
EnableSessionCaching bool Cache Session objects by partition ID. Default True.
SessionCacheMaxSize int Max cached sessions. Default 1000.
SessionCacheDuration int Session cache TTL in seconds.
RegionMap dict[str,str] AWS KMS multi-region key-ARN map.
PreferredRegion str Preferred region from RegionMap.
AwsProfileName str AWS shared-credentials profile name for KMS, DynamoDB, and Secrets Manager clients.
EnableRegionSuffix bool Append AWS region suffix to key IDs.
ExpireAfter int Intermediate-key expiration in seconds. Default 90 days.
CheckInterval int Revoke-check interval in seconds. Default 60 minutes.
DynamoDBEndpoint str DynamoDB endpoint URL (for local DynamoDB).
DynamoDBRegion str AWS region for DynamoDB.
DynamoDBTableName str DynamoDB table name. Default EncryptionKey.
ReplicaReadConsistency str DynamoDB consistency.
Verbose bool Emit verbose log events (use a log hook to consume).
EnableCanaries bool Enable in-memory canary buffers around plaintexts.

Both PascalCase and snake_case keys are accepted; PascalCase is canonical.

Environment variables

Variable Effect
STATIC_MASTER_KEY_HEX 64 hex chars (32 bytes) for static KMS. Testing only.
SERVICE_NAME / PRODUCT_ID / Metastore / KMS Read by SessionFactory() (no-config constructor).

AWS KMS example

asherah.setup({
    "ServiceName": "payments-api",
    "ProductID": "acme-corp",
    "Metastore": "rdbms",
    "ConnectionString": "mysql://user:pass@host:3306/asherah",
    "SQLMetastoreDBType": "mysql",
    "KMS": "aws",
    "RegionMap": {"us-west-2": "arn:aws:kms:us-west-2:000:key/abc"},
    "PreferredRegion": "us-west-2",
    "EnableSessionCaching": True,
    "SessionCacheMaxSize": 1000,
})

Performance

Native Rust implementation. Typical latencies on Apple M4 Max (in-memory metastore, session caching enabled, 64-byte payload):

Operation Sync Async (session-level, true async)
Encrypt ~1 µs ~37 µs
Decrypt ~1.2 µs ~37 µs

Async overhead is from the asyncio event loop dispatch + GIL handoff. Use sync for CPU-bound batches; use async when you need non-blocking behavior in an asyncio application.

API Reference

Full docstrings live in asherah/_asherah.pyi and asherah/__init__.py and surface in your IDE on hover. The tables below summarize each API; the type stubs are the source of truth.

Static / module-level API (legacy compatibility)

Lifecycle

Function Description
setup(config: dict) Initialize the global instance. Raises if already configured.
setup_async(config: dict) Async wrapper. Returns a coroutine.
shutdown() Tear down the global instance. Idempotent.
shutdown_async() Async wrapper.
get_setup_status() -> bool True iff setup() has been called and shutdown() has not.
setenv(env: dict) Apply env vars before setup(). Values may be None to delete.
version() -> str Package version string.

Encrypt / decrypt

Function Param 1 Param 2 Returns
encrypt_bytes(partition_id, data) str (non-empty) bytes (empty OK) str (DRR JSON)
encrypt_string(partition_id, text) str str (empty OK) str (DRR JSON)
decrypt_bytes(partition_id, drr) str str bytes
decrypt_string(partition_id, drr) str str str
encrypt_bytes_async(partition_id, data) str bytes Awaitable[str]
decrypt_bytes_async(partition_id, drr) str str or bytes Awaitable[bytes]
encrypt_string_async(partition_id, text) str str Awaitable[str]
decrypt_string_async(partition_id, drr) str str Awaitable[str]

Hooks

Function Description
set_log_hook(callback) Register a (event_dict) -> None log callback. Pass None to deregister.
set_metrics_hook(callback) Register a (event_dict) -> None metrics callback. Pass None to deregister.

Factory / Session API (recommended)

class SessionFactory

Member Description
SessionFactory() Construct from environment variables.
SessionFactory.from_env() Same as SessionFactory() — provided for SDK parity.
factory.get_session(partition_id) Get a per-partition Session. Raises on null/empty partition.
factory.close() Release native resources.
with SessionFactory() as factory: Context manager — close() runs on exit.

class Session

Member Description
session.encrypt_bytes(data) bytes → DRR JSON str. Empty bytes is valid.
session.encrypt_text(text) str → DRR JSON str. Empty string is valid.
session.decrypt_bytes(drr) DRR JSON strbytes.
session.decrypt_text(drr) DRR JSON strstr.
session.encrypt_bytes_async(data) Awaitable[str] — true async on tokio.
session.decrypt_bytes_async(drr) Awaitable[bytes] — true async on tokio.
session.close() Release native resources.
with session as ...: Context manager — close() runs on exit.

Event dict shapes

LogEvent = {
    "level": "trace" | "debug" | "info" | "warn" | "error",
    "message": str,
    "target": str,
}

# Metrics event for timing measurements:
TimingEvent = {
    "type": "encrypt" | "decrypt" | "store" | "load",
    "duration_ns": int,
}

# Metrics event for cache lifecycle:
CacheEvent = {
    "type": "cache_hit" | "cache_miss" | "cache_stale",
    "name": str,  # cache name, e.g. "session", "intermediate-key"
}

Cross-language compatibility

Wire-format compatible with all other Asherah implementations:

  • canonical godaddy/asherah (Go core via cobhan)
  • canonical godaddy/asherah-csharp
  • canonical godaddy/asherah-java
  • this repo's other bindings: Node, .NET, Java, Ruby, Go

A DataRowRecord written by any of these can be decrypted by any other, provided they share the same metastore and KMS configuration.

License

Licensed under the Apache License, Version 2.0.

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

asherah-0.5.41.tar.gz (311.9 kB view details)

Uploaded Source

Built Distributions

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

asherah-0.5.41-cp38-abi3-win_arm64.whl (7.1 MB view details)

Uploaded CPython 3.8+Windows ARM64

asherah-0.5.41-cp38-abi3-win_amd64.whl (7.6 MB view details)

Uploaded CPython 3.8+Windows x86-64

asherah-0.5.41-cp38-abi3-musllinux_1_2_x86_64.whl (12.1 MB view details)

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

asherah-0.5.41-cp38-abi3-musllinux_1_2_aarch64.whl (12.3 MB view details)

Uploaded CPython 3.8+musllinux: musl 1.2+ ARM64

asherah-0.5.41-cp38-abi3-manylinux_2_28_x86_64.whl (11.2 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.28+ x86-64

asherah-0.5.41-cp38-abi3-manylinux_2_28_aarch64.whl (11.8 MB view details)

Uploaded CPython 3.8+manylinux: glibc 2.28+ ARM64

asherah-0.5.41-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl (17.2 MB view details)

Uploaded CPython 3.8+macOS 10.12+ universal2 (ARM64, x86-64)macOS 10.12+ x86-64macOS 11.0+ ARM64

File details

Details for the file asherah-0.5.41.tar.gz.

File metadata

  • Download URL: asherah-0.5.41.tar.gz
  • Upload date:
  • Size: 311.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for asherah-0.5.41.tar.gz
Algorithm Hash digest
SHA256 7739b930cb5322d1962774f65711d4460f61b9f0a59be86ab5c9ae6ed959ffd5
MD5 6abe8be7f9a5073dd885028cc609f593
BLAKE2b-256 b60dc23c6b7a7ae56b909e85f796a50e592c1d0c5e485c6b16510e2ff84dd130

See more details on using hashes here.

File details

Details for the file asherah-0.5.41-cp38-abi3-win_arm64.whl.

File metadata

  • Download URL: asherah-0.5.41-cp38-abi3-win_arm64.whl
  • Upload date:
  • Size: 7.1 MB
  • Tags: CPython 3.8+, Windows ARM64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for asherah-0.5.41-cp38-abi3-win_arm64.whl
Algorithm Hash digest
SHA256 fd9ce8d4d49cfcb4b5fd646b09e830be57a8f928729f1d2b33aec06e00059666
MD5 a167c0d04ccddd763f1cbb8f347d092a
BLAKE2b-256 233d99da02ee5f929d66029a0bb7a51d2479fcceb47c388c4574b2c0ae447a08

See more details on using hashes here.

File details

Details for the file asherah-0.5.41-cp38-abi3-win_amd64.whl.

File metadata

  • Download URL: asherah-0.5.41-cp38-abi3-win_amd64.whl
  • Upload date:
  • Size: 7.6 MB
  • Tags: CPython 3.8+, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for asherah-0.5.41-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 bfc5ba15b2590e7f3543a45ee8ea15ae75a243592ed8a10290ff76df96703bc5
MD5 00d3b2266b00daa39b25a71070b78e21
BLAKE2b-256 d9edcbe833ecc564d63e83f11c34f407bd48bdbfbbad2f6ba2201de28314c6a8

See more details on using hashes here.

File details

Details for the file asherah-0.5.41-cp38-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for asherah-0.5.41-cp38-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 0d984be25f9199c70a87d1d0af71cbb6e8dca3da7ab0b76af8bf58b58d13fc75
MD5 10dcdec08a6e0d427d50c5f8cea2e276
BLAKE2b-256 6c78d54c3a7d154568758989af4c13736926d32cf1d6499b894ce90575db6da6

See more details on using hashes here.

File details

Details for the file asherah-0.5.41-cp38-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for asherah-0.5.41-cp38-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 bf1ac332648b1de21b524a43be27cfe0e8943c090cec8f0111649df74de04482
MD5 e464e3801ee09cddac99187504e1aa66
BLAKE2b-256 00c29423d8641a87ad2b8048976532a6bad909c6a22a2827b11767667dbc9aba

See more details on using hashes here.

File details

Details for the file asherah-0.5.41-cp38-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for asherah-0.5.41-cp38-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3d9937edb57fec54c4a6aa61ecbd5ab430b8974bb491c587de4ceeb75965d24e
MD5 8fd93e2a3946b80c3765e90317576046
BLAKE2b-256 3e5c287298eed346ce8f91b5d11d583151b0bd25877115260a77a0b008de506e

See more details on using hashes here.

File details

Details for the file asherah-0.5.41-cp38-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for asherah-0.5.41-cp38-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 4c67519de2e59d5e2cd296acc7fd288f723b61b79dbf172a64c15b5d5db0f2a2
MD5 7ecacebeaba0e55fb9f44794caec55f1
BLAKE2b-256 db2f225099e9a4de1d506a7b3c71277a29c0f98226aef9da9900a6d311989497

See more details on using hashes here.

File details

Details for the file asherah-0.5.41-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl.

File metadata

File hashes

Hashes for asherah-0.5.41-cp38-abi3-macosx_10_12_x86_64.macosx_11_0_arm64.macosx_10_12_universal2.whl
Algorithm Hash digest
SHA256 3bb757007184db9a21ee3f29e72dc4339acd5e8f792de2cde00028fc38374bd1
MD5 3d4a146b20ddffe3ec406766e793012b
BLAKE2b-256 22f0883aefcc812e99764a51f42ecf3ab2aefedc0ac468d9749be9dd95d5fd1f

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