Skip to main content

🔐 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

  1. Managed Identity: Your app gets a system-assigned managed identity (like a special Azure account for your app)
  2. Key Vault: Secrets are stored in Azure Key Vault (like a secure safe)
  3. References: Your app settings point to Key Vault, not the actual secret
  4. 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:

  1. Creates a secret in Key Vault named API-KEY with value super-secret-123
  2. Updates your app setting to: API_KEY=@Microsoft.KeyVault(SecretUri=https://your-vault.vault.azure.net/secrets/API-KEY)
  3. 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 Administrator or Key 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!

Metadata

Release files for az-secure-env 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for az-secure-env 1.0.1
File Size Uploaded Provenance
az_secure_env-1.0.1.tar.gz 18.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for az-secure-env 1.0.1
File Interpreter ABI Platform
az_secure_env-1.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 38.0 kB

Release files / az_secure_env-1.0.1.tar.gz

Download URL az_secure_env-1.0.1.tar.gz
Size 18.5 kB
Tags Source
SHA-256 checksum
How to use checksums
a5d6da651977a3e9fab5a4379c005d8dc928e55a634ff4f37623e8c8ec9582cd
BLAKE2b-256 checksum
How to use checksums
907aee8944845b3710fa1d19e248af9192ca1f8eba6d6529efc1f65c8529cc88
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / az_secure_env-1.0.1-py3-none-any.whl

Download URL az_secure_env-1.0.1-py3-none-any.whl
Size 19.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2abb23e3c0448b7cd78fcb8be82892903414a57847dd38db0bb451284bcccb16
BLAKE2b-256 checksum
How to use checksums
759965691a71781ea19d47591fabac6067eb2a9d92cc134244832d329fcf0460
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for az-secure-env 1.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

1.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page