This project implements a Terraform state management system using Cloudflare Workers, D1 database, and R2 storage. It provides a secure and scalable solution for managing Terraform state files across multiple projects.
- Secure storage of Terraform state files
- Locking mechanism to prevent concurrent modifications
- User authentication and authorization
- Project-based access control
- State file versioning and backups
- Configuration management
- RESTful API for Terraform operations
The system is built using the following components:
- Cloudflare Workers: Serverless compute platform for handling API requests
- D1 Database: SQL database for storing metadata, locks, and user information
- R2 Storage: Object storage for Terraform state files and backups
- Hono: Lightweight web framework for building the API
GET /api/v1/states
: List all Terraform statesGET /api/v1/states/:projectName/*
: Retrieve a state filePOST /api/v1/states/:projectName/*
: Update a state fileDELETE /api/v1/states/:projectName/*
: Delete a state filePOST /api/v1/lock/:projectName/*
: Acquire a lockDELETE /api/v1/lock/:projectName/*
: Release a lockGET /api/v1/lock/:projectName/*
: Get lock informationLOCK /api/v1/lock/:projectName/*
: Acquire a lock (alternative method)UNLOCK /api/v1/lock/:projectName/*
: Release a lock (alternative method)
GET /api/v1/users
: List all usersPOST /api/v1/users
: Add a new userPUT /api/v1/users/:username
: Update an existing userDELETE /api/v1/users/:username
: Delete a user
GET /api/v1/config
: Retrieve current configurationPOST /api/v1/config
: Update configuration
POST /config/init
: Initialize admin user (requires Bearer token authentication)
Note: All routes except /config/init
require Basic authentication and project-based authorization.
GET /api/v1/backup/states
: Create and download a backup of all state filesGET /api/v1/backup/users
: Create and download a backup of user information
Note: Health check and version endpoints are not implemented in the current version.
- Bearer token authentication for admin operations
- Basic authentication for Terraform operations
- Project-based authorization
- Password hashing using SHA-256
- Secure storage of sensitive information in environment variables
- CORS (Cross-Origin Resource Sharing) enabled for API endpoints
-
Clone this repository
-
Install dependencies:
npm install
-
Configure your Cloudflare account and create a D1 database and R2 bucket
-
Update
wrangler.toml
with your Cloudflare account details and environment bindings -
Add the AUTH_TOKEN secret to your worker:
wrangler secret put AUTH_TOKEN
You will be prompted to enter the secret value. This should be a secure, randomly generated string.
-
Deploy the worker:
wrangler deploy
-
Install cf_tsm:
pip install cf_tsm
-
Set up environment variables:
TSM_AUTH_TOKEN
: This is the authentication token for admin operations. Set it in your environment:export TSM_AUTH_TOKEN=your_secret_token_here
TSM_BASE_URL
: The URL of your deployed Terraform State Management system:export TSM_BASE_URL=https://your-tsm-instance.workers.dev
-
Configure user passwords:
- Users are managed through the CLI. To add a new user:
tsm-admin user add --username john_doe --project my_project --role developer
- You will be prompted to enter a password for the user.
- Users are managed through the CLI. To add a new user:
The tsm-admin
CLI provides several commands for managing the Terraform State Management system:
-
User Management:
- Add a user:
tsm-admin user add --username <username> --project <project> --role <role>
- Update a user:
tsm-admin user update --username <username> --project <new_project> --role <new_role>
- Delete a user:
tsm-admin user delete --username <username>
- List users:
tsm-admin user list
- Add a user:
-
Configuration Management:
- Get current configuration:
tsm-admin config get
- Set configuration:
tsm-admin config set --max-backups <number>
- Get current configuration:
-
State Management:
- List states:
tsm-admin state list [--username <username>] [--project <project>]
- Download all states:
tsm-admin state download-all
- List states:
-
Debugging:
- Most commands support a
--debug
flag for verbose output.
- Most commands support a
Update your Terraform configuration to use the HTTP backend, pointing to your deployed TSM instance:
terraform {
backend "http" {
address = "https://your-tsm-instance.workers.dev/api/v1/states/your_project_name/your_state_name"
lock_address = "https://your-tsm-instance.workers.dev/api/v1/lock/your_project_name/your_state_name"
unlock_address = "https://your-tsm-instance.workers.dev/api/v1/lock/your_project_name/your_state_name"
}
}
Terraform will prompt for username and password. Use the credentials you set up with the tsm-admin user add
command.
To run the project locally:
- Install dependencies:
npm install
- Create a
.dev.vars
file in the project root and define yourAUTH_TOKEN
:AUTH_TOKEN=your_secret_token_here
- Start the development server:
wrangler dev
The .dev.vars
file allows you to set environment variables for local development without exposing sensitive information in your code or version control.
For local development of cf_tsm (tsm-admin):
- Clone the cf_tsm repository
- Install dependencies:
pip install -e .
- Set up environment variables as described in the Configuration section
- Run commands with
python -m cf_tsm.cli
instead oftsm-admin
Run tests using the command: npm test
Contributions are welcome! Please feel free to submit a Pull Request.