Develop code that uses keys, secrets, and certificates stored in Azure Key Vault
Implement Secure Authentication and Authorization for Key Vault Access
Azure Key Vault provides a secure cloud service for storing and managing sensitive information like keys, secrets, and certificates. Applications that need to access these resources must authenticate properly and receive authorization before retrieving or modifying any stored data. This approach follows the principle of least privilege, which means granting only the minimum permissions necessary for each application to function.
Managed identities allow Azure resources to authenticate to Key Vault without storing credentials in code. When you enable a system-assigned or user-assigned managed identity, Azure automatically handles the authentication process through Microsoft Entra ID. This eliminates the risk of exposing secrets in application configurations and reduces the potential for human error. The same authentication mechanism works whether the application runs in Azure, on-premises, or during local development.
Role-Based Access Control enforces granular permissions for Key Vault access. Instead of granting broad access to everything in the vault, administrators assign specific roles such as Key Vault Secrets User or Key Vault Crypto Officer to managed identities. Regularly reviewing and updating these roles ensures they align with current requirements and helps maintain compliance with security policies. Applications should receive only the specific permissions they need, such as get and list operations for secrets.
Network isolation adds another layer of protection by configuring firewalls and virtual networks to restrict access to trusted applications. Key Vault uses envelope encryption, where data encryption keys are protected by key encryption keys stored within the vault, adding extra security for sensitive operations. Always use HTTPS for communications to protect data during transmission.
Implement Retrieval and Management of Secrets
Applications retrieve secrets securely using Azure SDKs along with the Azure Identity library. The typical flow involves obtaining a token via DefaultAzureCredential or a managed identity, creating a SecretClient instance with that credential, and calling GetSecretAsync to fetch the secret value. This approach abstracts the authentication details, so the code works the same in any environment without modification.
Managing secrets involves more than just retrieval. Developers should rotate secrets on a scheduled or as-needed basis to reduce the risk of compromised credentials. Never hardcode values in code; instead, reference Key Vault at runtime to keep sensitive information out of source code repositories. Using long, high-entropy secrets and strong encryption algorithms provides extra protection against attacks.
Access control to Key Vault is enforced through RBAC or access policies. Developers should assign the least privilege necessary, granting only the get and list permissions for secrets when needed. When applications run in Azure, using managed identities simplifies authentication and removes the need to manage client secrets entirely. Secure network access through private endpoints or service endpoints limits exposure to untrusted networks.
Execute Cryptographic Operations Using Key Vault Keys
Azure Key Vault stores cryptographic keys and uses Hardware Security Modules that meet strict security standards like FIPS 140. Developers use the Azure Key Vault SDK, which includes KeyClient for managing keys within the vault and CryptographyClient for performing encryption, decryption, or digital signing tasks. The SDK uses DefaultAzureCredential to authenticate without hardcoding credentials directly into application code.
For client-side encryption, applications generate a temporary Content Encryption Key to encrypt the actual data. This Content Encryption Key is then wrapped or encrypted using a Key Encryption Key stored securely inside the Key Vault. This envelope encryption process ensures that the master key never leaves the vault, providing high-level security for sensitive operations.
Access to cryptographic keys is controlled through RBAC, with specific roles granted to users and applications. The Key Vault Crypto Officer role allows performing cryptographic operations, while Key Vault Secrets User permits reading secret values. Following the principle of least privilege means granting only the specific permissions needed for each task.
Manage Digital Certificates and Secure Communication
SSL/TLS protocols protect data while it moves across the internet by encrypting information in transit. Azure Key Vault serves as a central location to store the digital certificates needed for this protection, avoiding the dangerous practice of hard-coding sensitive information into application code. By centralizing certificate storage, teams can manage all certificates in one place.
Azure Key Vault handles the certificate lifecycle by managing creation, storage, and renewal automatically. When a certificate updates in the vault, services like Azure App Service or Azure Application Gateway can automatically sync the new version. Developers use a Secret Identifier URL to reference the latest version of a certificate without manually updating code each time a change occurs.
Applications retrieve certificates by proving their identity through Managed Identities, which provide automatically managed authentication in Microsoft Entra ID. This allows the application to authenticate to Key Vault securely without storing secrets in source control. Authorization is handled through RBAC, ensuring the app only has the specific permissions it needs.
Certificates are commonly used for SSL/TLS termination, where the encrypted connection ends at a gateway before reaching backend servers. Azure Application Gateway and Azure Front Door integrate with Key Vault to manage these secure endpoints efficiently. This setup confirms that the server the user is communicating with is legitimate, while keeping private keys in the vault where developers never handle them directly.
Manage Cryptographic Operations Using Key Vault Keys
The Azure Key Vault client library allows developers to interact programmatically with keys stored in the vault. The KeyClient class creates and manages keys, while the CryptographyClient performs encryption, decryption, signing, and verification operations. Authentication is typically handled through DefaultAzureCredential, which allows applications to connect securely without storing manual passwords.
Developers can perform several essential operations using stored keys to protect application data. Encryption converts plain text into a secure, unreadable format, while decryption reverses this process to recover the original data. Signing creates a digital signature to ensure data has not been changed, and verification checks a signature to confirm the identity of the sender. These operations keep data secure and authentic throughout its entire lifecycle.
Key rotation regularly updates keys to reduce the risk of compromise. Azure Key Vault supports key versioning, allowing applications to specify a particular version or automatically use the latest one. Features like soft delete and purge protection prevent the accidental or malicious deletion of critical keys, allowing administrators to recover deleted items within a certain timeframe.
Access to cryptographic keys uses Azure RBAC to grant specific permissions following the principle of least privilege. The Key Vault Crypto Officer role grants permissions to create and manage keys. Managed Identities enhance security by allowing Azure services to authenticate to Key Vault automatically, removing the need for developers to handle credentials within their code.