Skip to content

unamatasanatarai/python-hvault-template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Python Vault Template

A production-ready template for Python projects that use HashiCorp Vault for secrets management.

Python Code style: ruff Type checked: mypy Security: bandit License: MIT


πŸ“‹ Table of Contents


🎯 Overview

This template provides a complete, production-ready foundation for Python applications that require secure secrets management using HashiCorp Vault. It includes automated testing, code quality checks, and a streamlined development workflow with git hooks.

Key Benefits

  • πŸ” Secure: Vault integration for secrets management (no hardcoded credentials)
  • πŸš€ Production-Ready: Pinned dependencies, security scanning, automated quality checks
  • βœ… Quality-First: Automated linting, type checking, formatting, and testing via pre-commit hooks
  • πŸ“¦ Template-Based: Easy to customize and adapt for new projects
  • πŸ› οΈ Developer-Friendly: Interactive setup, comprehensive documentation, Makefile commands

✨ Features

Security & Secrets Management

  • βœ… HashiCorp Vault integration for secure secrets storage
  • βœ… Automatic secret fetching and environment variable injection
  • βœ… Security vulnerability scanning with Bandit
  • βœ… No hardcoded credentials in codebase

Development Experience

  • βœ… Interactive setup script with guided configuration
  • βœ… Git pre-commit hooks for automated quality checks
  • βœ… Makefile for common development tasks
  • βœ… Virtual environment management
  • βœ… Hot-reload development workflow

Code Quality

  • βœ… Ruff - Fast Python linter and formatter
  • βœ… mypy - Static type checking
  • βœ… pytest - Comprehensive testing framework
  • βœ… Bandit - Security issue detection

Deployment

  • βœ… Automated secret injection via bin/run script
  • βœ… Production-ready configuration
  • βœ… Pinned dependencies for reproducible builds

πŸš€ Quick Start

Prerequisites

Installation

  1. Clone or copy this template to your project directory:

    git clone <repository-url> my-project
    cd my-project
  2. Run the interactive setup script:

    chmod +x bin/setup
    ./bin/setup

    The installer will:

    • Initialize git repository (if needed)
    • Configure git user settings (interactive)
    • Create Python virtual environment
    • Install all dependencies
    • Set up pre-commit hooks
    • Configure Vault settings (interactive)
  3. Run your application:

    ./bin/run python main.py

    Or use the Makefile:

    make run

πŸ“ Project Structure

python-vault/
β”œβ”€β”€ .agent/                    # Antigravity AI assistant configuration
β”‚   β”œβ”€β”€ prompts/              # Custom prompts for code generation
β”‚   β”‚   β”œβ”€β”€ README.md
β”‚   β”‚   β”œβ”€β”€ readme-blueprint-generator.prompt.md
β”‚   β”‚   └── vault-secret-documenter.prompt.md
β”‚   └── workflows/            # Development workflows
β”œβ”€β”€ bin/                       # Executable scripts
β”‚   β”œβ”€β”€ setup                 # Interactive project setup
β”‚   └── run                   # Vault-integrated command runner
β”œβ”€β”€ config/                    # Configuration files
β”‚   └── vault.conf            # Vault mount point and secret name
β”œβ”€β”€ hooks/                     # Git hooks
β”‚   └── pre-commit            # Automated quality checks
β”œβ”€β”€ tests/                     # Test files (pytest)
β”‚   β”œβ”€β”€ __init__.py
β”‚   └── test_main.py
β”œβ”€β”€ .bandit                   # Bandit security scanner config
β”œβ”€β”€ .gitignore                # Git ignore patterns
β”œβ”€β”€ main.py                   # Main application entry point
β”œβ”€β”€ Makefile                  # Common development commands
β”œβ”€β”€ requirements.txt          # Production dependencies (pinned)
β”œβ”€β”€ requirements-dev.txt      # Development dependencies
β”œβ”€β”€ LICENSE                   # MIT License
└── README.md                 # This file

πŸ› οΈ Technology Stack

Core Technologies

  • Python 3.13+ - Primary programming language
  • HashiCorp Vault - Secrets management and secure storage
  • Bash - Automation scripts and tooling

Dependencies

Production

  • requests (β‰₯2.32.0, <3.0.0) - HTTP library for API calls

Development

  • ruff (β‰₯0.14.0, <1.0.0) - Fast Python linter and formatter
  • mypy (β‰₯1.19.0, <2.0.0) - Static type checker
  • pytest (β‰₯9.0.0, <10.0.0) - Testing framework
  • bandit (β‰₯1.9.0, <2.0.0) - Security vulnerability scanner

Infrastructure

  • Git - Version control with automated hooks
  • Make - Build automation and task runner

πŸ’» Development Workflow

Daily Development

  1. Write code in main.py (or create additional .py files as needed)
  2. Write tests in tests/ following the test_*.py naming convention
  3. Run your code with Vault integration:
    ./bin/run python main.py
    # or
    make run
  4. Run tests:
    ./bin/run python -m pytest
    # or
    make test
  5. Commit changes - pre-commit hook runs automatically:
    • Linting with auto-fix (ruff)
    • Code formatting (ruff)
    • Type checking (mypy)
    • Security scanning (bandit)
    • All tests (pytest)

Available Make Commands

make help         # Show all available commands
make setup        # Run initial project setup
make run          # Run the application (with Vault)
make test         # Run all tests
make lint         # Run linting checks
make format       # Format code
make clean        # Remove venv, caches, and temporary files

Adding New Dependencies

Production dependencies:

echo "package-name>=1.0.0,<2.0.0" >> requirements.txt
./bin/run pip install -r requirements.txt

Development dependencies:

echo "dev-package>=1.0.0,<2.0.0" >> requirements-dev.txt
./bin/run pip install -r requirements-dev.txt

βš™οΈ Configuration

Vault Configuration

The project uses config/vault.conf to specify Vault settings:

VAULT_MOUNT="python-vault"
VAULT_SECRET_NAME="demo"

Configuration Methods

Option 1: Interactive (Recommended)

./bin/setup

Follow the prompts to configure your Vault mount point and secret name.

Option 2: Manual

# Edit config/vault.conf directly
vim config/vault.conf

Environment Variables

The bin/run script uses these environment variables:

  • VAULT_ADDR - Vault server address (default: http://127.0.0.1:8200)
  • VAULT_TOKEN - Authentication token (default: root for dev)

Managing Secrets

Add secrets to Vault:

vault kv put python-vault/demo \
  api_key="your-api-key" \
  db_pass="your-password" \
  region="us-west-2"

Access secrets in Python:

import os

api_key = os.environ.get("api_key")
db_pass = os.environ.get("db_pass")
region = os.environ.get("region")

The bin/run script automatically:

  1. Starts Vault dev server (if not running)
  2. Enables the KV mount point
  3. Fetches all secrets from the configured path
  4. Exports them as environment variables
  5. Runs your command with secrets available

πŸ§ͺ Testing

Running Tests

# Run all tests
./bin/run python -m pytest

# Run specific test file
./bin/run python -m pytest tests/test_main.py

# Run with verbose output
./bin/run python -m pytest -v

# Run with coverage
./bin/run python -m pytest --cov=src

Or use the Makefile:

make test

Writing Tests

Create test files in tests/ following the test_*.py naming convention:

# tests/test_my_feature.py
from main import main

def test_main():
    """Test the main function."""
    # Your test code here
    assert True

Test Structure

tests/
β”œβ”€β”€ __init__.py
β”œβ”€β”€ test_main.py           # Tests for main.py
β”œβ”€β”€ test_integration.py    # Integration tests
└── fixtures/              # Test fixtures and data
    └── sample_data.json

🎨 Code Quality

Automated Checks

The pre-commit hook automatically runs:

  1. Ruff Linting - Fast Python linter with auto-fix
  2. Ruff Formatting - Consistent code style
  3. mypy - Static type checking
  4. Bandit - Security vulnerability scanning
  5. pytest - All tests must pass

Manual Quality Checks

# Linting
./bin/run python -m ruff check .
./bin/run python -m ruff check --fix .  # Auto-fix issues

# Formatting
./bin/run python -m ruff format .

# Type checking
./bin/run python -m mypy .

# Security scanning
./bin/run python -m bandit -r .

# All tests
./bin/run python -m pytest

Or use Makefile shortcuts:

make lint     # Run linting
make format   # Format code

Coding Standards

  • Type hints - Use type annotations for all functions
  • Docstrings - Document all public functions and classes
  • Error handling - Use proper exception handling
  • Security - Never hardcode secrets or credentials
  • Testing - Write tests for all new features

πŸ”§ Troubleshooting

Common Issues

"vault CLI not found"

Solution: Install Vault from https://www.vaultproject.io/downloads

# macOS
brew install vault

# Linux
wget https://releases.hashicorp.com/vault/1.15.0/vault_1.15.0_linux_amd64.zip
unzip vault_1.15.0_linux_amd64.zip
sudo mv vault /usr/local/bin/

"Git user.name not set"

Solution: Configure git:

git config user.name "Your Name"
git config user.email "[email protected]"

"Failed to retrieve secrets"

Solution: Ensure:

  1. Vault is running: vault status
  2. Mount point exists: vault secrets list
  3. Secret exists: vault kv get python-vault/demo
  4. Token has read permissions

Debug steps:

# Check Vault status
vault status

# List secret engines
vault secrets list

# Read secret directly
vault kv get python-vault/demo

# Check token capabilities
vault token capabilities python-vault/demo

Pre-commit hook fails

Solution: Run checks manually to see detailed errors:

./bin/run python -m pytest
./bin/run python -m ruff check .
./bin/run python -m mypy .
./bin/run python -m bandit -r .

Virtual environment issues

Solution: Recreate the virtual environment:

rm -rf venv/
python3 -m venv venv
./bin/run pip install -r requirements-dev.txt

🀝 Contributing

Getting Started

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/amazing-feature
  3. Make your changes
  4. Run quality checks: make lint && make test
  5. Commit your changes: git commit -m 'Add amazing feature'
  6. Push to the branch: git push origin feature/amazing-feature
  7. Open a Pull Request

Development Guidelines

  • Follow existing code style (enforced by ruff)
  • Add tests for new features
  • Update documentation as needed
  • Ensure all quality checks pass
  • Keep commits atomic and well-described

Code Review Process

All submissions require review. We use GitHub pull requests for this purpose:

  1. Automated checks must pass (linting, tests, security)
  2. Code review by at least one maintainer
  3. Documentation must be updated if needed
  4. Tests must cover new functionality

πŸ“„ License

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


πŸ™ Acknowledgments


πŸ“š Additional Resources


⬆ back to top

Made with πŸ’ͺ by Unamata Sanatarai

About

A robust Python scaffold for secure secrets management using HashiCorp Vault.

Resources

License

Stars

Watchers

Forks

Contributors