Skip to content

About

A production-ready secrets management API built with FastAPI, PostgreSQL, and Fernet encryption. Designed for enterprise deployments with comprehensive DevOps tooling, CI/CD integration, and security-first architecture.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

39 Commits

Folders and files

Repository files navigation

πŸ” SecureVault

A production-ready secrets management API built with FastAPI, PostgreSQL, and Fernet encryption. Designed for enterprise deployments with comprehensive DevOps tooling, CI/CD integration, and security-first architecture.

Python FastAPI PostgreSQL Tests License


πŸ“– Table of Contents


🧭 Overview

SecureVault is a self-hosted secrets management service for teams and applications. It provides a secure REST API to store, retrieve, rotate, and audit sensitive credentials (API keys, database passwords, tokens, certificates, etc.) with:

  • End-to-end encryption using Fernet (symmetric encryption)
  • JWT-based authentication with configurable token expiry
  • Audit logging for all secret operations
  • Secret versioning & rotation capabilities
  • TTL support for time-limited secrets
  • Rate limiting to prevent abuse
  • Container-ready with Docker & Docker Compose

Perfect for microservices, CI/CD pipelines, and distributed systems where secrets need centralized, auditable management.


✨ Key Features

Feature Details
πŸ” Encryption at Rest Fernet symmetric encryption (AES-128 in CBC mode)
πŸ”‘ JWT Authentication HS256 signed tokens with configurable expiry
πŸ—„οΈ Database PostgreSQL with SQLAlchemy ORM
πŸ”„ Migrations Alembic for schema versioning
πŸ“Š Audit Logging Complete action trails for compliance
πŸ” Secret Rotation Built-in versioning and rotation support
⏱️ TTL Support Time-to-live for temporary secrets
🚦 Rate Limiting Configurable request throttling (slowapi)
πŸ“‘ Monitoring Sentry integration ready, structured JSON logging
🐳 Containerization Docker & Docker Compose configs included
βœ… Test Coverage Pytest suite with unit & integration tests
πŸ”„ CI/CD Ready GitHub Actions pipeline configured

πŸ› οΈ Tech Stack

Component Technology Version
Framework FastAPI 0.104.1
Web Server Uvicorn 0.24.0
Database PostgreSQL 13+
ORM SQLAlchemy 2.0.23
Migrations Alembic 1.12.1
Encryption cryptography (Fernet) 41.0.7
Authentication PyJWT / bcrypt 2.9.0 / 4.1.1
Rate Limiting slowapi 0.1.9
Testing pytest 7.4.3
Code Quality black, flake8, mypy, bandit Latest
Container Docker / Docker Compose Latest
Logging python-json-logger 2.0.7
Monitoring Sentry SDK 1.38.0

πŸ“‚ Project Structure

SecureVault/
β”œβ”€β”€ .github/workflows/          # GitHub Actions CI/CD pipelines
β”‚   └── ci.yml                  # (default) Test, lint, build workflow
β”‚
β”œβ”€β”€ alembic/                    # Database migration scripts
β”‚   β”œβ”€β”€ versions/               # Individual migration files
β”‚   β”œβ”€β”€ env.py                  # Alembic environment configuration
β”‚   └── script.py.mako          # Migration template
β”‚
β”œβ”€β”€ app/                        # Application source code
β”‚   β”œβ”€β”€ __init__.py
β”‚   β”œβ”€β”€ main.py                 # FastAPI app initialization
β”‚   β”œβ”€β”€ models.py               # SQLAlchemy ORM models
β”‚   β”œβ”€β”€ schemas.py              # Pydantic request/response schemas
β”‚   β”œβ”€β”€ routes.py               # API endpoint definitions
β”‚   β”œβ”€β”€ database.py             # Database connection & session management
β”‚   β”œβ”€β”€ auth.py                 # JWT authentication logic
β”‚   β”œβ”€β”€ config.py               # Settings from environment
β”‚   └── utils.py                # Helper utilities
β”‚
β”œβ”€β”€ docker/                     # Docker configurations
β”‚   β”œβ”€β”€ Dockerfile              # Production image
β”‚   β”œβ”€β”€ Dockerfile.dev          # Development image
β”‚   └── docker-compose.yml      # Multi-container orchestration
β”‚
β”œβ”€β”€ docs/                       # Documentation
β”‚   β”œβ”€β”€ API.md                  # API reference
β”‚   β”œβ”€β”€ DEPLOYMENT.md           # Deployment guides
β”‚   └── ARCHITECTURE.md         # System design
β”‚
β”œβ”€β”€ tests/                      # Test suite
β”‚   β”œβ”€β”€ conftest.py             # Pytest fixtures & configuration
β”‚   β”œβ”€β”€ test_auth.py            # Authentication tests
β”‚   β”œβ”€β”€ test_secrets.py         # Secret management tests
β”‚   └── test_api.py             # API endpoint tests
β”‚
β”œβ”€β”€ .env.example                # Template environment file
β”œβ”€β”€ .env                        # Actual config (git-ignored)
β”œβ”€β”€ .gitignore                  # Git exclusions
β”œβ”€β”€ alembic.ini                 # Alembic settings
β”œβ”€β”€ pytest.ini                  # Pytest configuration
β”œβ”€β”€ requirements.txt            # Python dependencies
β”œβ”€β”€ secrets_manager.py          # Core secrets encryption & storage logic
β”œβ”€β”€ __init__.py                 # Package metadata (v0.1.0)
└── README.md                   # This file

πŸš€ Getting Started

Prerequisites

  • Python 3.10+ (3.11+ recommended)
  • PostgreSQL 13+ (locally or Docker)
  • Docker & Docker Compose (optional, but recommended)
  • git

Local Development Setup

# Clone repository
git clone https://github.com/Jawahir01/SecureVault.git
cd SecureVault

# Create Python virtual environment
python3 -m venv venv
source venv/bin/activate  # On Windows: venv\Scripts\activate

# Install dependencies
pip install --upgrade pip
pip install -r requirements.txt

# Install pre-commit hooks (optional but recommended)
pre-commit install

Environment Configuration

Copy the example .env file and customize for your environment:

cp .env.example .env

Key variables to configure:

# Database (default: Docker Compose service)
DATABASE_URL=postgresql://securevault_user:securevault_pass@postgres:5432/securevault_db
DATABASE_ECHO=False           # Set True for SQL debug logging

# Security - CHANGE THESE IN PRODUCTION!
SECRET_KEY=your-super-secret-key-change-this-in-production
ENCRYPTION_KEY=your-encryption-key-change-this-in-production
ALGORITHM=HS256
ACCESS_TOKEN_EXPIRE_MINUTES=30
REFRESH_TOKEN_EXPIRE_DAYS=7

# Environment
ENVIRONMENT=development      # development | production | staging
DEBUG=True                   # False in production

# API Configuration
API_TITLE=SecureVault
API_VERSION=1.0.0
ALLOWED_ORIGINS=["http://localhost:3000", "http://localhost:8000"]

# Rate Limiting
RATE_LIMIT_ENABLED=True
RATE_LIMIT_REQUESTS=5        # Requests per period
RATE_LIMIT_PERIOD=60         # Seconds

# Monitoring (optional)
LOG_LEVEL=INFO               # DEBUG | INFO | WARNING | ERROR
SENTRY_DSN=                  # Leave empty to disable Sentry

⚠️ Production Security Notes:

  • Generate strong SECRET_KEY and ENCRYPTION_KEY: openssl rand -hex 32
  • Use environment variables or secrets manager (AWS Secrets Manager, HashiCorp Vault, etc.)
  • Never commit .env files to git (already in .gitignore)

Database Setup

Initialize the PostgreSQL database with Alembic migrations:

# Create initial migration (if needed)
alembic revision --autogenerate -m "Initial schema"

# Apply migrations to database
alembic upgrade head

# Verify migration status
alembic current
alembic history

For rollback:

alembic downgrade -1  # Rollback one migration
alembic downgrade base  # Rollback all migrations

Docker Deployment

Quickstart with Docker Compose:

docker-compose up --build

This starts:

  • FastAPI app on http://localhost:8000
  • PostgreSQL database on localhost:5432
  • Interactive API docs at http://localhost:8000/docs

Check logs:

docker-compose logs -f app
docker-compose logs -f postgres

Stop services:

docker-compose down
docker-compose down -v  # Also remove volumes/data

Production Docker build:

docker build -f docker/Dockerfile -t securevault:latest .
docker run -d \
  --name securevault \
  -p 8000:8000 \
  --env-file .env.production \
  securevault:latest

πŸ§ͺ Testing

The project includes a comprehensive test suite (all passing βœ…).

# Run all tests
pytest

# Run with verbose output
pytest -v

# Run specific test file
pytest tests/test_secrets.py

# Run specific test function
pytest tests/test_secrets.py::test_encrypt_decrypt

# Run with coverage report
pytest --cov=app --cov-report=html tests/

# Run only unit tests
pytest -m unit

# Run only integration tests
pytest -m integration

# Run tests in parallel (faster)
pytest -n auto

Test markers (defined in pytest.ini):

@pytest.mark.unit          # Fast, isolated tests
@pytest.mark.integration   # Tests requiring DB/external services

Output location:

  • Coverage report: htmlcov/index.html
  • Pytest cache: .pytest_cache/

πŸ”„ CI/CD Pipeline

The project includes a GitHub Actions workflow (.github/workflows/ci.yml) that runs on every push and pull request:

Workflow Steps

  1. Dependency Installation β€” Install requirements.txt
  2. Linting & Code Quality β€” Run flake8, black, isort, mypy, bandit
  3. Unit Tests β€” pytest with coverage
  4. Integration Tests β€” Database-dependent tests
  5. Security Scanning β€” bandit for security issues, pip-audit for dependencies
  6. Build Docker Image β€” Build and tag container image
  7. Push to Registry (Optional) β€” Push to Docker Hub / ECR on main branch

Status Badge:

![CI](https://github.com/Jawahir01/SecureVault/actions/workflows/ci.yml/badge.svg)

View pipeline results: https://github.com/Jawahir01/SecureVault/actions


πŸ“‘ API Endpoints

Full interactive API documentation available at http://localhost:8000/docs (Swagger UI)

Core Secret Operations

POST   /api/secrets              # Store a new secret
GET    /api/secrets/:key         # Retrieve a secret
PUT    /api/secrets/:key         # Update/rotate secret
DELETE /api/secrets/:key         # Delete a secret
GET    /api/secrets              # List all secret keys

Authentication

POST   /api/auth/login           # Get JWT token (username + password)
POST   /api/auth/refresh         # Refresh expired token
POST   /api/auth/logout          # Invalidate token

Audit & Monitoring

GET    /api/secrets/:key/audit   # Get audit log for secret
GET    /api/secrets/:key/history # Get version history
GET    /api/health               # Health check

Example: Store a secret

curl -X POST "http://localhost:8000/api/secrets" \
  -H "Authorization: Bearer <JWT_TOKEN>" \
  -H "Content-Type: application/json" \
  -d '{
    "key": "db_password",
    "value": "super-secret-pw",
    "ttl": 86400,
    "metadata": {"environment": "production"}
  }'

Example: Retrieve a secret

curl -X GET "http://localhost:8000/api/secrets/db_password" \
  -H "Authorization: Bearer <JWT_TOKEN>"

πŸ”’ Security Considerations

Encryption

  • Fernet (symmetric) β€” AES-128 in CBC mode with HMAC authentication
  • At-rest encryption β€” All secrets encrypted before database storage
  • In-transit encryption β€” Use HTTPS in production (reverse proxy recommended)

Authentication & Authorization

  • JWT tokens β€” HS256 signed, configurable expiry
  • Access tokens β€” Default 30 minutes (set via ACCESS_TOKEN_EXPIRE_MINUTES)
  • Refresh tokens β€” Default 7 days (set via REFRESH_TOKEN_EXPIRE_DAYS)
  • Password hashing β€” bcrypt with salt rounds

Defense Layers

  1. Secret Masking β€” Sensitive values never logged
  2. Rate Limiting β€” Prevent brute-force attacks (configurable)
  3. CORS β€” Restrict origin access (configured in .env)
  4. Audit Logging β€” Complete access trail for compliance
  5. Security Scanning β€” CI/CD includes bandit & pip-audit

Best Practices

  • βœ… Rotate SECRET_KEY and ENCRYPTION_KEY quarterly
  • βœ… Use strong, randomly-generated keys (32+ bytes)
  • βœ… Deploy behind HTTPS-enabled reverse proxy (nginx, Traefik)
  • βœ… Run in isolated network namespace (Kubernetes NetworkPolicy)
  • βœ… Implement secrets backup & disaster recovery
  • βœ… Monitor with Sentry for error tracking
  • βœ… Use strong database passwords (alphanumeric + special chars)
  • βœ… Enable database SSL connections in production
  • ❌ Never hardcode secrets
  • ❌ Never commit .env to git

πŸ“Š Monitoring & Logging

Structured Logging

Logs are output as JSON for easy parsing by log aggregators:

{
  "timestamp": "2024-01-15T10:30:45.123Z",
  "level": "INFO",
  "action": "secret_retrieved",
  "secret_key": "db_password",
  "user": "app_service",
  "duration_ms": 42
}

Sentry Integration

Enable error tracking by setting SENTRY_DSN:

SENTRY_DSN=https://key@sentry.io/project-id

Log Levels

Set via LOG_LEVEL environment variable:

DEBUG   β€” Detailed diagnostic info
INFO    β€” General informational messages
WARNING β€” Warning messages (default for production)
ERROR   β€” Error conditions

Metrics to Monitor

  • API response times
  • Database connection pool utilization
  • Cache hit rate (if caching layer added)
  • Failed authentication attempts
  • Rate limit rejections
  • Secret rotation frequency

🀝 Contributing

We welcome contributions! Follow these steps:

  1. Fork the repository
  2. Create feature branch β€” git checkout -b feature/awesome-feature
  3. Code with test coverage
  4. Run linting & tests β€” pytest, black, flake8, mypy
  5. Commit clearly β€” git commit -m "Add awesome feature"
  6. Push β€” git push origin feature/awesome-feature
  7. Open Pull Request with description

Development Guidelines

  • Follow PEP 8 (enforced by black/flake8)
  • Write tests for all new features
  • Update docs for API changes
  • Run full test suite before pushing: pytest && black . && flake8 app/

πŸ†˜ Support & Issues


πŸ” Build secure, scalable secret management with SecureVault
Enterprise-grade secrets vault for DevSecOps teams

About

A production-ready secrets management API built with FastAPI, PostgreSQL, and Fernet encryption. Designed for enterprise deployments with comprehensive DevOps tooling, CI/CD integration, and security-first architecture.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages