Skip to main content

Pure security primitives for the Cogno stack — AES-256-GCM encryption at rest, constant-time HMAC/secret verification, and a neutral input-size + crisis screen. Host-injected, fail-loud, deps-minimal.

Project description

cogno-aegis

Pure security primitives for the Cogno stack — authenticated encryption at rest, constant-time HMAC/secret verification, and a neutral input-size + crisis screen. Infra-agnostic, host-injected, fail-loud, deps-minimal.

aegis (αἰγίς) — the shield. The pure, reusable security core extracted from the parent Cogno SaaS (core/{crypto,auth,input_guard}.py). RBAC, credential storage and budget guards (CoreDB-coupled) are intentionally out of this slice.

Why

A security helper must never fail open. The parent's crypto module silently returned plaintext when the cipher backend was missing or a decrypt failed, and read its key straight from environment variables. cogno-aegis inverts both:

  • Host injects everything — keys, secrets, limits, crisis data. The library reads no environment and holds no global state.
  • Fail loud — a missing backend or a bad key/ciphertext raises (CryptoUnavailableError / DecryptionError); it never degrades to plaintext.
  • Core signals, host decides — the input guard returns a structured GuardResult, never user-facing text. The host renders the message.

Install

pip install cogno-aegis            # hmac + input-guard (stdlib only, zero deps)
pip install "cogno-aegis[crypto]"  # + AES-256-GCM (pulls `cryptography`)

Three primitives

1. crypto — encryption at rest (AES-256-GCM)

from cogno_aegis import Cipher

cipher = Cipher.from_passphrase("…high-entropy…", salt=b"my-deployment-salt")
# or: Cipher(key=os.urandom(32))

token = cipher.encrypt("oauth-refresh-token")   # "ENC:v1:…"
cipher.decrypt(token)                            # "oauth-refresh-token"
cipher.decrypt("not-encrypted")                  # pass-through (pre-encryption data)

Envelope: ENC:v1: base64url(nonce:12 ‖ ciphertext ‖ tag:16) — a fresh random nonce per call, authenticated (tamper → DecryptionError). Wire-compatible with the parent's format.

2. hmac_auth — channel-neutral signature & secret verification

from cogno_aegis import hmac_sign, hmac_verify, compare_secret, generate_secret

secret = generate_secret()                       # CSPRNG, URL-safe
sig = hmac_sign(secret, request_body)            # "sha256=…" (X-Hub-Signature-256 style)
hmac_verify(secret, request_body, header_sig)    # constant-time bool
compare_secret(header_api_key, expected_key)     # constant-time, bytes-safe

These are the primitives the per-channel webhook verifiers (cogno-gateway) build on — aegis owns the cryptography, the edge libs keep their channel glue.

3. input_guard — size cap + optional crisis screen

from cogno_aegis import InputGuard, GuardCategory
from cogno_aegis.safety import DEFAULT_CRISIS_RULES, DEFAULT_CRISIS_MESSAGES

guard = InputGuard(max_chars=1000, crisis_rules=DEFAULT_CRISIS_RULES)

verdict = guard.check_text(user_text)
if not verdict:
    if verdict.category is GuardCategory.CRISIS:
        reply = DEFAULT_CRISIS_MESSAGES[verdict.meta["locale"]]   # host renders
    else:  # TOO_LONG
        reply = f"Limit is {verdict.meta['limit']} chars."

The engine is neutral — crisis patterns and messages are opt-in data in cogno_aegis.safety (a heuristic Layer-1 screen, not a clinical tool; localize before production). Bring your own CrisisRules to extend or replace them.

Design

Principle How
Infra-agnostic no env reading, no globals; host injects keys/secrets/limits
Fail-loud raises on missing backend / bad key / tamper — never fails open
Deps-minimal stdlib-only core; cryptography is an optional [crypto] extra
Core signals, host decides GuardResult verdict, not rendered text
Constant-time every secret comparison via hmac.compare_digest

See docs/HOST_INTEGRATION.md and LOGGING.md.

The Cogno ecosystem

cogno-aegis is one organ of Cogno — a family of small, composable, Apache-2.0 libraries that together form a complete conversational-agent platform. Each library owns a single concern and stays infra-agnostic; a host assembles them into a running agent:

The Cogno ecosystem

The open-source libraries are the organs; the host is the body that joins them. Our reference host — cogno-host, with its cogno-ui dashboard — is the private product layer, but it holds no special powers: everything it does rides on the public seams documented in each library's docs/HOST_INTEGRATION.md, so you can assemble a body of your own.

Development

pip install -e ".[dev]"
ruff check cogno_aegis tests examples
mypy cogno_aegis
pytest tests/unit -q --cov=cogno_aegis

License

Apache-2.0

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

cogno_aegis-0.1.0.tar.gz (18.8 kB view details)

Uploaded Source

Built Distribution

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

cogno_aegis-0.1.0-py3-none-any.whl (19.2 kB view details)

Uploaded Python 3

File details

Details for the file cogno_aegis-0.1.0.tar.gz.

File metadata

  • Download URL: cogno_aegis-0.1.0.tar.gz
  • Upload date:
  • Size: 18.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for cogno_aegis-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d8a9b691aef622c22a8ce82734107ac3553e42591a37186df694498ea8ffab33
MD5 bc59ecd6fc3c4f363b76a4e48e500869
BLAKE2b-256 155608e8f0eedd49ddb5184f243a8743b002aaaee84be3dee4ad2ee4f611aba9

See more details on using hashes here.

File details

Details for the file cogno_aegis-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: cogno_aegis-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.12

File hashes

Hashes for cogno_aegis-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cd5422ddbfe7c3f24a828c70c75f590f979e26375c9aca954588b758b474f99e
MD5 1df6f2fd7c4df0bb0c17ba177d1edccc
BLAKE2b-256 f7039a1e79a73be284d9a6db4d6ce01c4a09280dddc9691e641fd795096264f8

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