Skip to main content

Azure Vault Secrets

A Python client library for securely accessing and managing secrets stored in Azure Key Vault.

Features

  • 🔐 Secure secret retrieval from Azure Key Vault
  • ⚡ Built-in caching to reduce API calls
  • 🔄 Support for secret CRUD operations
  • 🛡️ Automatic authentication using Azure credentials
  • 🧪 Comprehensive test coverage
  • 📝 Type hints for better IDE support

Installation

Prerequisites

  • Python 3.9+
  • Azure subscription
  • Azure Key Vault instance

From PyPI

pip install azure-vault-secrets

From Source

git clone https://github.com/PioManojDatt/azure-vault-secrets.git
cd azure-vault-secrets
pip install -e .

Quick Start

Basic Usage

from azure_vault_secrets import SecretClient

# Initialize the client
client = SecretClient(vault_url="https://<vault-name>.vault.azure.net/")

# Retrieve a secret
secret = client.get_secret("my-secret-name")
print(secret)

# Store a secret
client.set_secret("new-secret", "secret-value")

# List all secrets
secrets = client.list_secrets()
print(secrets)

# Delete a secret
client.delete_secret("secret-to-delete")

With Custom Cache TTL

from azure_vault_secrets import SecretClient

# Initialize with 10-minute cache TTL
client = SecretClient(
    vault_url="https://<vault-name>.vault.azure.net/",
    cache_ttl_seconds=600,
    enable_cache=True
)

# Use the client...
secret = client.get_secret("my-secret")

Disable Caching

client = SecretClient(
    vault_url="https://<vault-name>.vault.azure.net/",
    enable_cache=False
)

Authentication

The client uses Azure's DefaultAzureCredential for authentication, which supports multiple authentication methods:

  1. Environment Variables - AZURE_TENANT_ID, AZURE_CLIENT_ID, AZURE_CLIENT_SECRET
  2. Azure CLI - Automatically uses credentials from az login
  3. Managed Identity - In Azure services (App Service, Functions, etc.)
  4. Visual Studio Code - Uses VS Code Azure extension authentication

Setting Up Authentication

Using Azure CLI

az login

Using Environment Variables

export AZURE_TENANT_ID="<your-tenant-id>"
export AZURE_CLIENT_ID="<your-client-id>"
export AZURE_CLIENT_SECRET="<your-client-secret>"

Error Handling

from azure_vault_secrets import SecretClient, SecretNotFoundError, AuthenticationError

client = SecretClient(vault_url="https://<vault-name>.vault.azure.net/")

try:
    secret = client.get_secret("my-secret")
except SecretNotFoundError:
    print("Secret not found in vault")
except AuthenticationError:
    print("Failed to authenticate with Azure")
except Exception as e:
    print(f"An error occurred: {e}")

API Reference

SecretClient

Methods

  • get_secret(secret_name: str) -> str - Retrieve a secret
  • set_secret(secret_name: str, secret_value: str) -> None - Store a secret
  • delete_secret(secret_name: str) -> None - Delete a secret
  • list_secrets() -> list - List all secrets
  • clear_cache() -> None - Clear the secret cache
  • close() -> None - Close the client connection

SecretCache

Methods

  • get(key: str) -> Optional[str] - Get a cached value
  • set(key: str, value: str) -> None - Cache a value
  • clear() -> None - Clear all cached values
  • remove(key: str) -> None - Remove a specific cached value
  • is_expired(key: str) -> bool - Check if a cached value has expired

Testing

Run the test suite:

pytest tests/

With coverage:

pytest --cov=azure_vault_secrets tests/

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

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

Support

For issues, questions, or suggestions, please open an issue on the GitHub repository.

Changelog

0.1.0 (Initial Release)

  • Initial release
  • Basic secret CRUD operations
  • Caching support
  • Azure Key Vault integration

Security Considerations

  • Never hardcode secrets in your code
  • Always use Azure Key Vault for secret storage
  • Regularly rotate your secrets
  • Use Managed Identity when running in Azure services
  • Keep the Azure SDK up to date

Performance Tips

  • Use caching for frequently accessed secrets
  • Adjust cache TTL based on your security requirements
  • List secrets sparingly (this operation can be expensive)
  • Consider using Managed Identity for better performance in Azure

Resources

Metadata

Release files for azure-vault-secrets 1.0.0

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

Source distribution (sdist)

Source distribution for azure-vault-secrets 1.0.0
File Size Uploaded Provenance
azure_vault_secrets-1.0.0.tar.gz 8.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for azure-vault-secrets 1.0.0
File Interpreter ABI Platform
azure_vault_secrets-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 16.7 kB

Release files / azure_vault_secrets-1.0.0.tar.gz

Download URL azure_vault_secrets-1.0.0.tar.gz
Size 8.9 kB
Tags Source
SHA-256 checksum
How to use checksums
bc7210e2b1799c8d34e31e1883596deb1ee964c171e3ce0cdd7431b76c940154
BLAKE2b-256 checksum
How to use checksums
57f2791a41a0da40898bbf577c7819feb43f309df1c4a24f1c18e87219a2f517
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Release files / azure_vault_secrets-1.0.0-py3-none-any.whl

Download URL azure_vault_secrets-1.0.0-py3-none-any.whl
Size 7.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
274e3ca35c687eee009cbafa45f32094fabada53189c13dd9d9d52626ffef9e2
BLAKE2b-256 checksum
How to use checksums
4457c0b27e7baae4a29c7c67ec2ae542aa15a62cc35d057b87836e1f91361cb4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.9

Security for azure-vault-secrets 1.0.0

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0.0 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