Unified Authentication & Authorization for Aperion Ecosystem
Project description
Aperion Gatekeeper
The Immune System — Unified Authentication & Authorization for the Aperion Ecosystem
Overview
Aperion Gatekeeper consolidates fragmented security logic (HMAC, Bearer, RBAC) into a single Source of Truth for identity and access control. It enforces Constitution B (Safety & Security) and provides a unified enforce(subject, action, resource) API.
Core Principles
- Zero Trust: Never trust, always verify. Every request must be authenticated.
- Default Deny: If no explicit rule grants access, deny.
- Fail Closed: On any error, deny access.
- Key Rotation: Support multiple active keys for zero-downtime rotation.
Installation
pip install -e .
# With development dependencies
pip install -e ".[dev]"
Quick Start
1. Configure Key Manager
from aperion_gatekeeper.core.credentials import KeyManager
# Load keys from environment
key_manager = KeyManager()
key_manager.load_hmac_from_env("APERION_HMAC_KEY")
key_manager.load_bearer_from_env("FSAL_TOKEN")
2. Authenticate Requests
from aperion_gatekeeper.engines.authentication import AuthenticationEngine
engine = AuthenticationEngine(key_manager)
# Authenticate an incoming request
result = engine.authenticate(
authorization=request.headers.get("Authorization"),
method=request.method,
path=request.path,
)
if result.success:
subject = result.subject
print(f"Authenticated: {subject.principal_id}")
else:
print(f"Auth failed: {result.error_message}")
3. Enforce Authorization
from aperion_gatekeeper.engines.policy import PolicyEngine, Permission, ResourcePolicy
policy = PolicyEngine()
# Add resource-specific policies
policy.add_policy(ResourcePolicy(
resource_pattern="/api/admin/*",
allowed_roles=frozenset({"admin"}),
allowed_permissions=frozenset(Permission),
))
# Enforce access
if policy.enforce(subject, "delete", "/api/admin/users/123"):
# Allowed
pass
else:
# Denied
raise Forbidden()
4. FastAPI Integration
from fastapi import Depends, FastAPI
from aperion_gatekeeper.middleware.fastapi import (
configure_gatekeeper,
get_current_subject,
require_permission,
GatekeeperConfig,
)
from aperion_gatekeeper.core.credentials import KeyManager
app = FastAPI()
# Configure at startup
@app.on_event("startup")
def setup():
km = KeyManager()
km.load_hmac_from_env("APERION_HMAC_KEY")
configure_gatekeeper(GatekeeperConfig(key_manager=km))
# Protected endpoint
@app.get("/api/data")
async def get_data(subject = Depends(get_current_subject)):
return {"user": subject.principal_id}
# Permission-protected endpoint
@app.delete("/api/data/{id}")
async def delete_data(id: str, subject = Depends(require_permission("delete"))):
return {"deleted": id}
Architecture
gatekeeper/
├── core/
│ ├── identity.py # Subject, User, Agent models
│ ├── credentials.py # KeyManager, HMAC/Token credentials
│ └── encryption.py # Crypto utilities
├── engines/
│ ├── authentication.py # "Who are you?" (HMAC/Bearer router)
│ └── policy.py # "Can you do this?" (RBAC engine)
├── middleware/
│ └── fastapi.py # Drop-in FastAPI dependencies
└── audit.py # Structured security logging
Migration Plan
Phase 1A: Core Models (COMPLETE)
- Unified identity models (Subject protocol, User, Agent)
- KeyManager with rotation support
- Policy engine with default deny
Phase 1B: Integration
- Migrate FSAL to use
get_current_subject - Migrate Cortex to use
get_current_subject - Replace direct HMAC validation with
AuthenticationEngine
Phase 2: Enhanced Security
- Add rate limiting per subject
- Add session management
- Add MFA support for User subjects
- Integrate with external identity providers
Phase 3: Observability
- Prometheus metrics for auth success/failure rates
- OpenTelemetry tracing for auth flows
- Alert rules for security anomalies
Source Files Migrated
| Original | Gatekeeper | Status |
|---|---|---|
stack/aperion_zip5/hmac_auth.py |
engines/authentication.py |
✅ Ported |
stack/aperion/fsal/auth.py |
middleware/fastapi.py |
✅ Ported |
stack/aperion/security/rbac.py |
engines/policy.py |
✅ Ported |
stack/aperion/security/audit_logging.py |
audit.py |
✅ Ported |
API Reference
Subject Protocol
Every authenticated entity implements the Subject protocol:
class Subject(Protocol):
principal_id: str # Unique identifier
subject_type: SubjectType # user, agent, service, system
roles: frozenset[str] # Assigned roles
is_authenticated: bool # Auth status
AuthResult
Authentication results contain:
@dataclass
class AuthResult:
success: bool
subject: Subject
method: AuthMethod # hmac, bearer, none
error_code: AuthErrorCode | None
error_message: str | None
PolicyDecision
Policy evaluation results contain:
@dataclass
class PolicyDecision:
allowed: bool
reason: str
policy_matched: str | None
Security Considerations
Key Management
- Keys MUST be loaded from environment variables (Constitution B1)
- Default keys trigger warnings and should NEVER be used in production
- Use key rotation with legacy keys for zero-downtime updates
Audit Logging
All authentication and authorization events are logged:
from aperion_gatekeeper.audit import SecurityAuditor
auditor = SecurityAuditor(log_path=Path("security.jsonl"))
auditor.log_auth_success(subject, method="hmac", path="/api/data")
Development
# Install dev dependencies
pip install -e ".[dev]"
# Run tests
pytest
# Type check
mypy src/
# Lint
ruff check src/
License
MIT License - See LICENSE file.
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aperion_gatekeeper-1.2.1.tar.gz.
File metadata
- Download URL: aperion_gatekeeper-1.2.1.tar.gz
- Upload date:
- Size: 76.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2c222af04e2e8d8873cfc274c3ebd3560c067f299edbc2bcc95e80e897b39d0e
|
|
| MD5 |
22c9fb4433e9261438add971391680ac
|
|
| BLAKE2b-256 |
cb55cda7fa58f01b0d6bf8df8b6b49c3b5eb8bb863d2a8d05d407e786d465d4a
|
Provenance
The following attestation bundles were made for aperion_gatekeeper-1.2.1.tar.gz:
Publisher:
release.yml on invictustitan2/aperion-gatekeeper
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aperion_gatekeeper-1.2.1.tar.gz -
Subject digest:
2c222af04e2e8d8873cfc274c3ebd3560c067f299edbc2bcc95e80e897b39d0e - Sigstore transparency entry: 941995352
- Sigstore integration time:
-
Permalink:
invictustitan2/aperion-gatekeeper@3a5f62d9307834055976a8ca88393433fa96e847 -
Branch / Tag:
refs/tags/v1.2.1 - Owner: https://github.com/invictustitan2
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@3a5f62d9307834055976a8ca88393433fa96e847 -
Trigger Event:
push
-
Statement type:
File details
Details for the file aperion_gatekeeper-1.2.1-py3-none-any.whl.
File metadata
- Download URL: aperion_gatekeeper-1.2.1-py3-none-any.whl
- Upload date:
- Size: 52.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e7ccf95549bf9ea0e76d1faa987672a27ae9ee522bdf51ecef40e9a29f424ce2
|
|
| MD5 |
3ea9e218946afaafed839d0ee7723741
|
|
| BLAKE2b-256 |
be98b3319ead2743ff13054dfdb76cbf79b2fc366ab8eeb65af405711e326cf1
|
Provenance
The following attestation bundles were made for aperion_gatekeeper-1.2.1-py3-none-any.whl:
Publisher:
release.yml on invictustitan2/aperion-gatekeeper
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
aperion_gatekeeper-1.2.1-py3-none-any.whl -
Subject digest:
e7ccf95549bf9ea0e76d1faa987672a27ae9ee522bdf51ecef40e9a29f424ce2 - Sigstore transparency entry: 941995404
- Sigstore integration time:
-
Permalink:
invictustitan2/aperion-gatekeeper@3a5f62d9307834055976a8ca88393433fa96e847 -
Branch / Tag:
refs/tags/v1.2.1 - Owner: https://github.com/invictustitan2
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@3a5f62d9307834055976a8ca88393433fa96e847 -
Trigger Event:
push
-
Statement type: