Production-grade, open-source middleware for multi-vendor access control devices.
Integrates with external authentication systems, processes real-time security events, manages multiple devices simultaneously, and provides a comprehensive REST API with enterprise-grade audit logging.
- π Multi-Vendor Support: Native integration with Hikvision ISAPI and extensible architecture for other vendors
- π‘ Real-Time Event Processing: Stream security events from devices with reliable message queuing
- π Flexible Authentication: Supports external identity providers and OAuth 2.0
- π Enterprise Audit Logging: Complete audit trails for compliance and security investigations
- π³ Container-Ready: Pre-configured for Docker and Kubernetes deployments
- π Secrets Management: HashiCorp Vault integration for secure credential handling
- π§ͺ Comprehensive Testing: Unit tests, integration tests, and mocking support
- π Scalable Architecture: Async/await with FastAPI, connection pooling, and horizontal scaling ready
- Python 3.12+
- Docker & Docker Compose (for containerized deployment)
- PostgreSQL 14+ (or SQLite for development)
- Optional: HashiCorp Vault for secrets management
# Clone the repository
git clone https://github.com/yourusername/access-control-middleware.git
cd AccessControlMiddleware
# Create virtual environment
python -m venv .venv
# Activate virtual environment
# On Linux/macOS:
source .venv/bin/activate
# On Windows:
.venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
# Create .env file from template
cp .env.example .env
# Run migrations
python -m peewee_migrate create init
python -m peewee_migrate migrate
# Start the server
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000# Clone and navigate to project
git clone https://github.com/yourusername/access-control-middleware.git
cd AccessControlMiddleware
# Create environment file
cp .env.example .env
# Start all services (FastAPI, PostgreSQL, Redis, Vault)
docker-compose up -d
# Verify services are running
docker-compose ps
# View logs
docker-compose logs -f middlewareCreate a .env file in the project root using the provided template:
cp .env.example .envKey environment variables:
| Variable | Description | Example |
|---|---|---|
DATABASE_URL |
PostgreSQL connection string | postgresql://user:pass@localhost/acm_db |
DEVICE_MODE |
Device integration mode | mock (development), isapi (production) |
VAULT_URL |
HashiCorp Vault address | http://localhost:8200 |
REDIS_URL |
Redis connection string | redis://localhost:6379/0 |
LOG_LEVEL |
Application logging level | INFO, DEBUG, ERROR |
See .env.example for complete configuration options.
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Access Control Middleware β
β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Nginx Reverse Proxy β β
β ββββββββββ¬βββββββββββββββββββββββββ¬βββββββββββββββββββββββββ β
β β β β
β ββββββββββΌβββββββββ ββββββββββββΌββββββββ ββββββββββββββ β
β β REST API Loop β β Event Stream β β Distributedβ β
β β (FastAPI) β β Processor β β Locking β β
β β β β (FastAPI Tasks) β β (Redis) β β
β ββββββββββ¬βββββββββ ββββββββββββ¬ββββββββ ββββββββββββββ€ β
β β β β² β
β ββββββββββΌββββββββββββββββββββββββΌβββββββββ β β
β β Peewee ORM Layer β β β
β β - Models, Schemas, Relationships β β β
β ββββββββββ¬βββββββββββββββββββββββ¬βββββββββββ β β
β β β β β
β ββββββββββΌββββββββββββ βββββββββΌβββββββ ββββββββββΌββββββ β
β β PostgreSQL DB β β SQLite β β Redis β β
β β (Production) β β (Dev/Test) β β (Locking, β β
β β β β β β Fallback) β β
β ββββββββββββββββββββββ ββββββββββββββββ ββββββββββββββββ β
β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β Device Integrations & Controllers β β
β β - Hikvision ISAPI (vendored, configurable) β β
β β - Mock Device Controller (dev/demo) β β
β β - Extensible vendor architecture β β
β βββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β² β
β β β
β ββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββββββ β
β β Vault Secret Management (Credentials) β β
β β - Device auth tokens β β
β β - Database credentials β β
β β - API keys β β
β ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ β
β β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β Queue Management for Resilience β
β - Redis Primary (fast, distributed) β
β - SQLite Fallback Queue (on-disk persistence) β
β - Queue Manager (automatic failover & recovery) β
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
- REST API (
app/routers/): Device control, event querying, status endpoints - Event Processing (
app/models.py,app/schemas.py): Standardized event structures via Peewee ORM - Device Drivers (
app/devices/): Vendor-specific ISAPI implementations with mock support - Database (
migrations/,app/models.py): Peewee-backed PostgreSQL/SQLite persistent storage - Queue Manager (
app/queue_manager.py): In-memory + persistent SQLite queue with Redis fallback for event processing resilience - Redis Helper (
app/redis_helper.py): Distributed locking, connection pooling, async queue operations, and health checks - Secrets Management (
app/vault_helper.py,app/crypto.py): HashiCorp Vault integration for credential handling and encryption utilities - Reverse Proxy (
deploy/nginx/): Nginx configuration for TLS termination and load balancing
Once the server is running, access the interactive API documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
Example: Turn off a device relay
curl -X POST http://localhost:8000/api/v1/devices/{device_id}/actions/unlock \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{"action": "unlock", "duration": 5}'Copy the systemd service file to your system:
sudo cp deploy/services/access-control.service /etc/systemd/system/
sudo systemctl daemon-reload
sudo systemctl enable access-control.service
sudo systemctl start access-control.serviceView logs:
sudo journalctl -u access-control.service -fReady for Kubernetes - see deploy/ directory for Helm-ready configurations.
- Development: Use
DEVICE_MODE=mockand SQLite - Testing: Use
DEVICE_MODE=mockand PostgreSQL with test fixtures - Production: Use
DEVICE_MODE=isapiwith Vault, Redis, and PostgreSQL
AccessControlMiddleware/
βββ app/ # Main application code
β βββ __init__.py
β βββ main.py # FastAPI app initialization
β βββ config.py # Configuration management
β βββ crypto.py # Encryption utilities
β βββ database.py # Database connections & Peewee setup
β βββ models.py # Peewee ORM models
β βββ schemas.py # Pydantic schemas
β βββ vault_helper.py # Vault integration
β βββ queue_manager.py # Event queue with Redis/SQLite fallback
β βββ redis_helper.py # Redis client, locking, health checks
β βββ devices/ # Device vendor implementations
β β βββ __init__.py
β β βββ base.py # Base DeviceDriver interface
β β βββ factory.py # Device controller factory
β β βββ vendors/ # Vendor-specific implementations
β β βββ hikvision.py # Hikvision ISAPI driver
β β βββ mock.py # Mock device driver (testing/demo)
β βββ routers/ # API endpoint groups
β βββ __init__.py
β βββ devices.py # Device management endpoints
β βββ events.py # Event processing endpoints
β βββ health.py # Health check endpoints
βββ deploy/ # Deployment configurations
β βββ services/ # Systemd service & timer files
β β βββ access-control.service
β β βββ access-control.timer
β βββ nginx/ # Reverse proxy configuration
β β βββ nginx.conf # Production nginx setup
β βββ vault/ # Vault configuration
β βββ vault-config.hcl # Vault server setup
βββ migrations/ # Database migrations
β βββ 001_initial_schema.py
βββ tests/ # Test suite
β βββ __init__.py
βββ docs/ # Documentation
βββ docker-compose.yml # Local development setup
βββ Dockerfile # Container image
βββ requirements.txt # Python dependencies
βββ pytest.ini # Testing configuration
βββ .env.example # Environment template
βββ README.md # This file
# Install dependencies (includes test packages)
pip install -r requirements.txt
# Run all tests with pytest
pytest
# Run with coverage report
pytest --cov=app --cov-report=html
# Run specific test file
pytest tests/test_devices.py -v
# Run async tests
pytest tests/test_async.py -v -s# Create a new migration
python -m peewee_migrate create migration_name
# Apply pending migrations
python -m peewee_migrate migrate
# Show migration status
python -m peewee_migrate status
# Rollback last migration
python -m peewee_migrate rollback- Create a new driver in
app/devices/vendors/my_vendor.py - Implement the
DeviceDriverbase class interface - Add integration tests in
tests/test_devices.py - Update device initialization in
app/devices/__init__.py - Document in
docs/VENDORS.md
See docs/VENDOR_INTEGRATION.md for detailed guidelines.
# Format code with Black
black app/ tests/
# Type checking
mypy app/
# Linting
pylint app/
# Security scanning
bandit -r app/- β All credentials stored in HashiCorp Vault (not in .env for production)
- β Device credentials never stored locally; fetched from Vault on-demand
- β Database passwords encrypted with industry standards
- β HTTPS enforced via Nginx reverse proxy with TLS termination
- β Rate limiting on sensitive endpoints
- β Complete audit trail of all device access and configuration changes
- β Input validation on all API endpoints with Pydantic schemas
- β SQL injection protection via Peewee ORM parameterized queries
- β Distributed locking via Redis prevents race conditions
- β Event queue fallback ensures no message loss during Redis outages
Error: ConnectionError: Unable to connect to device at 192.168.1.100
Solution: Verify device IP, network connectivity, and that device credentials are correct in Vault.
Error: peewee_migrate.MigrateException: ...
Solution:
# Check migration status
python -m peewee_migrate status
# Rollback and re-apply migrations (development only)
python -m peewee_migrate rollback
python -m peewee_migrate migrateError: ConnectionError: Cannot connect to Redis
Solution: Queue Manager automatically falls back to SQLite queue storage. Check Redis connectivity:
redis-cli ping # Should return PONGThe middleware monitors Vault token expiration. Action required: Refresh your Vault token or update credentials if tokens expire.
We welcome contributions! Please see CONTRIBUTING.md for:
- Code style guidelines (PEP 8, type hints required)
- Testing requirements (>80% coverage)
- Commit message conventions
- Pull request process
- Issue reporting guidelines
This project is licensed under the MIT License - see LICENSE for details.
- π Documentation: See docs/ directory
- π Issue Tracker: GitHub Issues
- π¬ Discussions: GitHub Discussions
- π§ Email: See CONTRIBUTING.md
- LDAP/Active Directory authentication provider
- Biometric device support (fingerprint readers)
- SMS/Email notifications for access events
- Advanced analytics dashboard
- Multi-tenancy support
- GraphQL API option
Built with:
- FastAPI - Web framework
- Peewee - ORM with multi-database support
- peewee-migrate - Database migrations
- Pydantic - Data validation
- Redis - Distributed locking and message queues
- HashiCorp Vault - Secrets management
- Docker - Containerization
Version: 1.0.0
Last Updated: April 5, 2026
Maintainer: Tariq Mohammed