Skip to content

API Reference parrot.security.audit_ledger

github-actions[bot] edited this page Dec 2, 2026 · 4 revisions

parrot.security.audit_ledger

View source

Append-only, KMS-signed credential-invocation ledger (FEAT-260 / TASK-1642).

Every credentialed tool invocation over the A2A bridge records an :class:AuditLedgerEntry that carries a key_fingerprint (a SHA-256 hash of the credential material) but never the raw credential. Each entry is signed by a :class:AbstractKMSSigner so the record cannot be silently tampered with after the fact.

Two signers are provided:

  • :class:LocalHMACSigner — HMAC-SHA256 with a caller-supplied secret; suitable for local development, tests, and environments without a managed KMS.
  • A production KMS backend can be injected by implementing :class:AbstractKMSSigner and passing the instance to :class:AuditLedger.

Append-only semantics are enforced by the public API: :meth:AuditLedger.append and :meth:AuditLedger.verify are the only entry-points; there is no update or delete.

Class AbstractKMSSigner

Injectable signing/verification backend for :class:AuditLedger.

Implementations must be async to allow non-blocking calls to managed KMS services (AWS KMS, GCP Cloud KMS, Azure Key Vault, etc.).

Method AbstractKMSSigner.sign

Return a hex-encoded signature over data.

sign(data: bytes) -> str

Parameter Type Default Description
data bytes Canonical bytes to sign (from :meth:AuditLedgerEntry.canonical_bytes).

Returns: str — A hex-encoded signature string.

Method AbstractKMSSigner.verify

Verify that signature was produced by sign(data).

verify(data: bytes, signature: str) -> bool

Parameter Type Default Description
data bytes The canonical bytes that were signed.
signature str Hex-encoded signature to verify.

Returns: bool — True if the signature is valid, False otherwise.

Class AuditLedger

Append-only, KMS-signed credential-invocation ledger.

Entries are stored in-memory by default. An optional storage backend can be supplied (a callable coroutine that accepts a serialised JSON string) for durable persistence (DocumentDB, PostgreSQL, etc.).

Append-only semantics: only :meth:append and :meth:verify are exposed; there is no update, delete, or get_all mutation surface.

Example::

ledger = AuditLedger()
entry = await ledger.append(
    user_id="alice@example.com",
    channel="a2a:copilot",
    tool="jira_create_issue",
    provider="jira",
    credential_material=jira_token,
)
assert await ledger.verify(entry.entry_id)
Parameter Type Default Description
signer Optional[AbstractKMSSigner] None A :class:AbstractKMSSigner implementation. Defaults to :class:LocalHMACSigner (HMAC-SHA256 with a random in-process key — suitable for tests).
storage Optional[Any] None Optional async callable (json_str: str) -> None that persists each entry to a durable backend. None (default) retains entries in memory only.

Attributes:

Attribute Type Description
entry_count int Return the number of entries in the in-memory store.

Method AuditLedger.append

Create, sign, and persist a new ledger entry.

The key_fingerprint is derived by :func:derive_key_fingerprint — the raw credential is never stored or logged.

append(*, user_id: str, channel: str, tool: str, provider: str, credential_material: Any) -> AuditLedgerEntry

Parameter Type Default Description
user_id str Canonical per-user identity (email).
channel str Invocation channel (e.g. "a2a:copilot").
tool str Name of the tool being invoked.
provider str Credential provider key (e.g. "jira").
credential_material Any The resolved credential value used to derive the fingerprint. This value is hashed and then discarded; it is not stored in the entry.

Returns: AuditLedgerEntry — The signed :class:AuditLedgerEntry that was persisted.

Raises:

  • RuntimeError — If the storage backend raises an unrecoverable error.

Method AuditLedger.verify

Re-check the KMS signature on a previously appended entry.

Loads the entry from the in-memory store and verifies that the stored signature matches the canonical bytes re-derived from the entry fields.

verify(entry_id: str) -> bool

Parameter Type Default Description
entry_id str The UUID of the entry to verify.

Returns: bool — True if the signature is intact, False if the entry is not found or the signature does not match.

Method AuditLedger.get_entry

Return the entry for entry_id, or None if not found.

Read-only access for inspection. Does not expose a mutable reference.

get_entry(entry_id: str) -> Optional[AuditLedgerEntry]

Parameter Type Default Description
entry_id str

Returns: Optional[AuditLedgerEntry]

Class AuditLedgerEntry

Append-only, KMS-signed record of a credentialed tool invocation.

Fields:

Field Type Default Description
entry_id str Unique ledger record identifier (UUIDv4).
user_id str Canonical per-user identity (email), consistent with TeamsHumanChannel and A2AServer._extract_identity.
channel str Invocation channel, e.g. "a2a:copilot".
tool str Name of the tool that was invoked.
provider str Credential provider, e.g. "jira", "o365", "work-iq", "fireflies", or "stub".
key_fingerprint str SHA-256 hex digest of the credential material. Never the raw credential.
signature str '' KMS signature over the canonical entry bytes (see :meth:canonical_bytes).
created_at datetime UTC timestamp recorded at entry creation.

Method AuditLedgerEntry.canonical_bytes

Return the canonical byte representation used for signing/verification.

All fields EXCEPT signature are included, serialised to JSON with sorted keys for determinism.

canonical_bytes() -> bytes

Returns: bytes — UTF-8 encoded canonical JSON bytes.

Class AzureKeyVaultSigner

Azure Key Vault backed KMS signer for production environments.

Uses the azure-keyvault-keys SDK to sign and verify entry bytes with an asymmetric key stored in Azure Key Vault (RSA-PSS / EC).

.. note:: This class requires the azure-keyvault-keys and azure-identity packages. Import is guarded — if the packages are not installed a clear :class:ImportError is raised at instantiation time (not at module import time), so environments that use only :class:LocalHMACSigner are unaffected.

Parameter Type Default Description
vault_url str Azure Key Vault URL (e.g. "https://myvault.vault.azure.net/").
key_name str Name of the key in the vault.
key_version Optional[str] None Optional key version. Defaults to the latest version.
credential Optional[Any] None An azure.identity credential object. When None, :class:azure.identity.DefaultAzureCredential is used.
algorithm str 'RS256' Signing algorithm. Defaults to "RS256".

Method AzureKeyVaultSigner.sign

Sign data using the Azure Key Vault key.

sign(data: bytes) -> str

Parameter Type Default Description
data bytes Canonical bytes to sign.

Returns: str — Hex-encoded signature string.

Method AzureKeyVaultSigner.verify

Verify signature against data using the Azure Key Vault key.

verify(data: bytes, signature: str) -> bool

Parameter Type Default Description
data bytes Canonical bytes that were signed.
signature str Hex-encoded signature to verify.

Returns: bool — True if the signature is valid, False otherwise.

Class LocalHMACSigner

HMAC-SHA256 signer for local development and testing.

This signer uses Python's built-in :mod:hmac module with a caller-supplied secret key. It is cryptographically sound for low-threat environments but does not provide the tamper-evidence guarantees of a true managed KMS (key rotation, HSM backing, audit trail of key usage, etc.).

Parameter Type Default Description
secret Optional[bytes] None The HMAC secret. Defaults to a random 32-byte secret if not provided (suitable for unit tests where verification happens in the same process). In production use, supply a secret from the vault.

Method LocalHMACSigner.sign

Return the HMAC-SHA256 hex digest of data under the secret key.

sign(data: bytes) -> str

Parameter Type Default Description
data bytes

Returns: str

Method LocalHMACSigner.verify

Return True iff signature matches the HMAC-SHA256 of data.

verify(data: bytes, signature: str) -> bool

Parameter Type Default Description
data bytes
signature str

Returns: bool

Function derive_key_fingerprint

Return the SHA-256 hex digest of credential_material.

The fingerprint uniquely identifies a credential without exposing any secret bytes.

derive_key_fingerprint(credential_material: Any) -> str

Parameter Type Default Description
credential_material Any Any credential value — token string, dict with access_token / token, or arbitrary bytes. Dicts and other objects are serialised to JSON before hashing.

Returns: str — A 64-character lowercase hex string (SHA-256).

Architecture

Clients

Bots & Agents

Memory

Knowledge

Tools & Toolkits

Flows

Experts

Skills

Stores & Loaders

MCP & A2A

Server & APIs

Integrations

Outputs & A2UI

Prompts

Security

Operations

Migration

SDD Tooling

LLM Wiki

Deployment

Decisions

API Reference

Clone this wiki locally