Skip to content

0xTariq-dev/AccessControlMiddleware

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

9 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Access Control Middleware

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.

Features

  • πŸ” 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

Quick Start

Prerequisites

  • Python 3.12+
  • Docker & Docker Compose (for containerized deployment)
  • PostgreSQL 14+ (or SQLite for development)
  • Optional: HashiCorp Vault for secrets management

Installation

Option 1: Local Development (venv)

# 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

Option 2: Docker Compose (Recommended)

# 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 middleware

Configuration

Create a .env file in the project root using the provided template:

cp .env.example .env

Key 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.

Architecture

High-Level Overview

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚                    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)              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Key Components

  • 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

API Documentation

Once the server is running, access the interactive API documentation:

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}'

Deployment

Production Deployment with Systemd

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.service

View logs:

sudo journalctl -u access-control.service -f

Kubernetes Deployment

Ready for Kubernetes - see deploy/ directory for Helm-ready configurations.

Environment-Specific Configs

  • Development: Use DEVICE_MODE=mock and SQLite
  • Testing: Use DEVICE_MODE=mock and PostgreSQL with test fixtures
  • Production: Use DEVICE_MODE=isapi with Vault, Redis, and PostgreSQL

Development

Project Structure

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

Running Tests

# 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

Database Migrations

# 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

Adding a New Device Vendor

  1. Create a new driver in app/devices/vendors/my_vendor.py
  2. Implement the DeviceDriver base class interface
  3. Add integration tests in tests/test_devices.py
  4. Update device initialization in app/devices/__init__.py
  5. Document in docs/VENDORS.md

See docs/VENDOR_INTEGRATION.md for detailed guidelines.

Code Quality

# Format code with Black
black app/ tests/

# Type checking
mypy app/

# Linting
pylint app/

# Security scanning
bandit -r app/

Security Considerations

  • βœ… 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

Troubleshooting

Connection Refused to Device

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.

Database Migration Failures

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 migrate

Redis/Queue Issues

Error: ConnectionError: Cannot connect to Redis

Solution: Queue Manager automatically falls back to SQLite queue storage. Check Redis connectivity:

redis-cli ping  # Should return PONG

Vault Token Expiration

The middleware monitors Vault token expiration. Action required: Refresh your Vault token or update credentials if tokens expire.

Contributing

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

License

This project is licensed under the MIT License - see LICENSE for details.

Support & Community

  • πŸ“š Documentation: See docs/ directory
  • πŸ› Issue Tracker: GitHub Issues
  • πŸ’¬ Discussions: GitHub Discussions
  • πŸ“§ Email: See CONTRIBUTING.md

Roadmap

  • 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

Acknowledgments

Built with:


Version: 1.0.0
Last Updated: April 5, 2026
Maintainer: Tariq Mohammed

About

Production-grade middleware for HikVision access control devices. Integrates with external authentication systems, processes real-time events, manages multiple devices, and provides REST API control with enterprise audit logging.

Resources

Contributing

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages