A simple CLI tool to secure Azure App Services and Function Apps by managing environment variables safely with Azure Key Vault
Project description
🔐 az-secure-env
A simple and powerful CLI tool to secure your Azure App Services and Function Apps by managing environment variables safely with Azure Key Vault.
🎯 What Does This Tool Do?
If you're using Azure App Services or Function Apps, you might be storing sensitive information like API keys, database passwords, and connection strings directly in your app's environment variables. This is not secure!
This tool helps you:
- Scan your apps to find insecure plain-text settings
- Migrate sensitive settings to Azure Key Vault (a secure storage for secrets)
- Automatically fix permission issues so your app can read from Key Vault
- Add new secure environment variables directly to Key Vault
✨ Key Features
- 🔍 Smart Scanning - Identifies plain-text secrets and Key Vault references
- 🚀 One-Click Migration - Move secrets to Key Vault with a single command
- 🔧 Auto-Fix Permissions - Automatically configures managed identity and Key Vault access
- ➕ Easy Secret Management - Add new secrets through an interactive menu
- 🎨 Beautiful Interface - Clean, colorful output that's easy to understand
- ⚡ No Manual Setup - Handles identity creation, permissions, and app restarts
📦 Installation
Install from PyPI (Recommended)
pip install az-secure-env
🚀 Quick Start
Prerequisites
- Python 3.8 or higher
- Azure account with active subscription
- You must be logged in to Azure CLI:
az login
Basic Usage
1. Scan Your App for Security Issues
az-secure-env scan \
--subscription "your-subscription-id" \
--resource-group "your-rg-name" \
--app-name "your-app-name"
This shows you:
- App information (name, type, location, identity status)
- All environment variables and their security status
- Which settings are secure (using Key Vault) vs plain-text
2. Scan + Auto-Fix Permissions
az-secure-env scan \
--subscription "your-subscription-id" \
--resource-group "your-rg-name" \
--app-name "your-app-name" \
--fix
The --fix flag automatically:
- Enables managed identity if not present
- Grants Key Vault access permissions
- Syncs Key Vault references
- Restarts your app to apply changes
3. Migrate Secrets to Key Vault
az-secure-env migrate \
--subscription "your-subscription-id" \
--resource-group "your-rg-name" \
--app-name "your-app-name" \
--vault-name "your-keyvault-name"
This interactive command:
- Shows all plain-text environment variables
- Lets you select which ones to migrate
- Creates secrets in Key Vault
- Updates app settings to use Key Vault references
- Handles all permissions automatically
4. Add New Secret to Key Vault
az-secure-env add-env \
--subscription "your-subscription-id" \
--resource-group "your-rg-name" \
--app-name "your-app-name"
Interactive menu to add a new environment variable securely:
- Choose to add to existing Key Vault or create new one
- Enter variable name and value
- Automatically creates Key Vault reference in your app
- Handles all setup and permissions
📖 Detailed Examples
Example 1: Complete Security Audit and Fix
# First, scan to see what needs fixing
az-secure-env scan --subscription "abc123" --resource-group "my-rg" --app-name "my-webapp"
# Then auto-fix any Key Vault permission issues
az-secure-env scan --subscription "abc123" --resource-group "my-rg" --app-name "my-webapp" --fix
Example 2: Migrate All Secrets at Once
az-secure-env migrate \
--subscription "abc123" \
--resource-group "my-rg" \
--app-name "my-webapp" \
--vault-name "my-keyvault"
# When prompted, type "all" to migrate all plain-text settings
Example 3: Selective Migration
az-secure-env migrate \
--subscription "abc123" \
--resource-group "my-rg" \
--app-name "my-webapp" \
--vault-name "my-keyvault"
# When prompted, type "1,3,5" to migrate only settings #1, #3, and #5
🔐 How It Works
Security Model
- Managed Identity: Your app gets a system-assigned managed identity (like a special Azure account for your app)
- Key Vault: Secrets are stored in Azure Key Vault (like a secure safe)
- References: Your app settings point to Key Vault, not the actual secret
- Automatic Access: The tool grants your app's identity permission to read from Key Vault
What Happens Behind the Scenes
When you migrate a setting like API_KEY=super-secret-123:
- Creates a secret in Key Vault named
API-KEYwith valuesuper-secret-123 - Updates your app setting to:
API_KEY=@Microsoft.KeyVault(SecretUri=https://your-vault.vault.azure.net/secrets/API-KEY) - Your app automatically reads the real value from Key Vault at runtime
🛠️ Commands Reference
scan
Scan an app for settings and security status
Options:
--subscription(required) - Azure subscription ID--resource-group(required) - Resource group name--app-name(required) - App Service or Function App name--fix(optional) - Automatically fix Key Vault permissions
migrate
Migrate plain-text settings to Key Vault
Options:
--subscription(required) - Azure subscription ID--resource-group(required) - Resource group name--app-name(required) - App Service or Function App name--vault-name(required) - Key Vault name
add-env
Add a new environment variable securely
Options:
--subscription(required) - Azure subscription ID--resource-group(required) - Resource group name--app-name(required) - App Service or Function App name
⚙️ Configuration
Azure Authentication
The tool uses Azure CLI authentication. Make sure you're logged in:
az login
To use a specific account:
az account set --subscription "your-subscription-id"
Required Azure Permissions
Your Azure account needs these permissions:
- Read/Write access to App Service/Function App
- Ability to grant Key Vault access policies
- Create/Read/Write access to Key Vault secrets
Typically, you need the Contributor role or these specific roles:
Website Contributor(for App Services)Key Vault AdministratororKey Vault Secrets Officer(for Key Vault)
🤔 FAQ
Q: Will this break my app?
A: No! The tool creates backups and only modifies settings you select. Your app is restarted automatically to apply changes.
Q: What if I don't have a Key Vault?
A: The add-env command can create one for you, or you can create one manually first.
Q: Can I use this with Azure Functions?
A: Yes! It works with both App Services and Function Apps.
Q: What happens to my old plain-text settings?
A: They are replaced with Key Vault references. The actual values are stored securely in Key Vault.
Q: Can I migrate back to plain-text?
A: Yes, you can manually update the app settings in Azure Portal, but we don't recommend it for security reasons.
🐛 Troubleshooting
"No module named 'az_secure_env'"
Make sure you've installed the package: pip install az-secure-env
"Authentication failed"
Run az login to authenticate with Azure.
"Key Vault not found"
Ensure the Key Vault name is correct and exists in your subscription.
"Permission denied"
Your Azure account needs sufficient permissions. Contact your Azure administrator.
👨💻 Author
Created by Aakash Shah to make Azure security easier for everyone.
🔗 Useful Links
⭐ If this tool helped you, please consider giving it a star on GitHub!
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file az_secure_env-1.0.1.tar.gz.
File metadata
- Download URL: az_secure_env-1.0.1.tar.gz
- Upload date:
- Size: 18.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5d6da651977a3e9fab5a4379c005d8dc928e55a634ff4f37623e8c8ec9582cd
|
|
| MD5 |
a02a70b6a84bdda94732e4458f62fd26
|
|
| BLAKE2b-256 |
907aee8944845b3710fa1d19e248af9192ca1f8eba6d6529efc1f65c8529cc88
|
File details
Details for the file az_secure_env-1.0.1-py3-none-any.whl.
File metadata
- Download URL: az_secure_env-1.0.1-py3-none-any.whl
- Upload date:
- Size: 19.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2abb23e3c0448b7cd78fcb8be82892903414a57847dd38db0bb451284bcccb16
|
|
| MD5 |
0b50f9ad892dcf3e3da5ce27914672e7
|
|
| BLAKE2b-256 |
759965691a71781ea19d47591fabac6067eb2a9d92cc134244832d329fcf0460
|