Back to Blog

Protecting Secrets with Azure Key Vault

Complete tutorial about az keyvault create in Azure CLI. Learn key vault, secrets, certificates.

Protecting Secrets with Azure Key Vault

Protecting Secrets with Azure Key Vault

Introduction

In today’s digital landscape, safeguarding sensitive information such as passwords, API keys, and certificates is paramount. Azure Key Vault is a cloud service designed to securely store and manage secrets, keys, and certificates. With the az keyvault create command, developers can establish a secure vault for their applications, allowing for the centralized management of sensitive data.

Azure Key Vault not only enhances security by protecting sensitive information but also provides auditing and access control features that help organizations comply with regulations. Organizations can efficiently manage access to secrets using Azure Active Directory (AAD), ensuring that only authorized users and applications can retrieve or modify the secrets. In this tutorial, we will explore how to use the az keyvault create command, along with practical examples to help you effectively manage secrets in Azure Key Vault.

Prerequisites

Before you begin, ensure you have the following:

  • Azure CLI installed on your machine. (You can download it here).
  • An active Azure subscription. You can create a free account if you don’t have one here.
  • Sufficient permissions to create resources in your Azure subscription.
  • Authentication to Azure CLI using az login.

Fundamental Concepts

  • Key Vault: A secure storage solution for managing sensitive data such as secrets, encryption keys, and certificates.
  • Secrets: Any sensitive information that you want to securely store, such as passwords or connection strings.
  • Certificates: SSL/TLS certificates or other types used for secure communications.
  • Access Policies: Rules that define which users or applications can access the secrets stored in the Key Vault.

Command Syntax

The syntax for the az keyvault create command is as follows:

az keyvault create --name <keyvault-name> --resource-group <resource-group-name> --location <location> [--sku <sku>] [--enabled-for-deployment] [--enabled-for-template-deployment] [--enabled-for-disk-encryption]

Parameters Table

Parameter Description
--name -n The name of the Key Vault. Must be globally unique.
--resource-group -g The name of the resource group.
--location -l The Azure region where the Key Vault will be created.
--sku The SKU of the Key Vault (e.g., standard, premium).
--enabled-for-deployment Allow the Key Vault to be used for deployment.
--enabled-for-template-deployment Allow the Key Vault to be used for template deployments.
--enabled-for-disk-encryption Allow the Key Vault to be used for disk encryption.

Practical Examples

Example 1: Create a Simple Key Vault

Create a basic Key Vault in a specified resource group and location:

az keyvault create --name myKeyVault --resource-group myResourceGroup --location eastus

This command creates a Key Vault named myKeyVault in the myResourceGroup resource group located in the East US region.

Example 2: Create a Key Vault with Premium SKU

Create a Key Vault with a premium SKU for enhanced features:

az keyvault create --name myPremiumKeyVault --resource-group myResourceGroup --location westus --sku premium

This sets up a premium Key Vault, which provides additional capabilities such as support for hardware security modules (HSMs).

Example 3: Enable Key Vault for Deployment

Create a Key Vault that allows deployment from Azure Resource Manager:

az keyvault create --name myDeployableKeyVault --resource-group myResourceGroup --location centralus --enabled-for-deployment true

This enables the Key Vault to be used in deployments, making it easier to manage secrets for Azure resources.

Example 4: Create a Key Vault with Access Policies

Create a Key Vault and set an access policy for a user or application:

az keyvault create --name myAccessPolicyKeyVault --resource-group myResourceGroup --location westeurope --enabled-for-template-deployment true

az keyvault set-policy --name myAccessPolicyKeyVault --upn user@example.com --secret-permissions get list set delete

This creates a Key Vault and assigns permissions for a specific user to manage secrets.

Example 5: Store a Secret in the Key Vault

After creating the Key Vault, you can store a secret:

az keyvault secret set --vault-name myKeyVault --name MySecret --value "MySecretValue"

This command stores a secret named MySecret with the value MySecretValue in the Key Vault.

Example 6: Retrieve a Secret from the Key Vault

Retrieve the secret that was just stored:

az keyvault secret show --vault-name myKeyVault --name MySecret

This fetches the details of the secret MySecret stored in the Key Vault.

Example 7: List All Secrets in the Key Vault

List all secrets stored in a specified Key Vault:

az keyvault secret list --vault-name myKeyVault

This command lists all secrets stored in myKeyVault, helping you to manage your secrets effectively.

Example 8: Delete a Secret from the Key Vault

Delete a specific secret from the Key Vault:

az keyvault secret delete --vault-name myKeyVault --name MySecret

This command removes the secret MySecret from the Key Vault.

Real-World Use Cases

Scenario 1: Secure API Keys

A development team can use Azure Key Vault to store API keys for third-party services. By centralizing the management of these secrets, developers can easily retrieve them without hardcoding them in their applications, thus enhancing security.

Scenario 2: Managing SSL Certificates

An organization can use Azure Key Vault to manage SSL/TLS certificates for their web applications. This centralized management simplifies renewal processes and ensures that certificates are stored securely.

Scenario 3: Protecting Sensitive Configuration Data

Applications often require sensitive configuration data that should not be exposed in code repositories. By using Azure Key Vault, teams can store connection strings and other sensitive information securely while allowing their applications to retrieve this information as needed.

Best Practices

  1. Use Access Policies: Implement access policies to control who can access secrets, keys, and certificates stored in the Key Vault.
  2. Regularly Rotate Secrets: Establish a policy for rotating secrets regularly to minimize the risk of exposure.
  3. Enable Logging: Enable logging and monitoring for your Key Vault to track access and changes, ensuring compliance and security.
  4. Use Managed Identities: Leverage Azure Managed Identities to access Key Vault securely without embedding credentials in your applications.
  5. Apply Network Security: Configure network security settings to restrict access to Key Vault resources based on IP address ranges or virtual network service endpoints.

Common Errors

  1. Error: "Key Vault name already exists."

    • Cause: The specified Key Vault name is not unique across Azure.
    • Solution: Choose a different name for your Key Vault that adheres to naming conventions.
  2. Error: "Resource group not found."

    • Cause: The specified resource group does not exist.
    • Solution: Create the resource group using az group create.
  3. Error: "Access denied."

    • Cause: Insufficient permissions to create the Key Vault.
    • Solution: Ensure that your Azure account has the necessary permissions to create resources in the specified resource group.
  4. Error: "Invalid SKU specified."

    • Cause: The specified SKU is not valid for the Key Vault.
    • Solution: Verify the SKU options and select one of the available SKUs: standard or premium.

Related Commands

Command Description
az keyvault secret set Store a new secret in the Key Vault.
az keyvault secret show Retrieve a specific secret from the Key Vault.
az keyvault certificate create Create a new certificate in the Key Vault.
az keyvault key create Create a new key in the Key Vault.

Automation Script

Here’s a simple bash script to automate the creation of a Key Vault and storage of a secret:

#!/bin/bash

# Variables
KEYVAULT_NAME="myAutomatedKeyVault"
RESOURCE_GROUP="myResourceGroup"
LOCATION="eastus"
SECRET_NAME="MySecret"
SECRET_VALUE="MySecretValue"

# Create Resource Group
az group create --name $RESOURCE_GROUP --location $LOCATION

# Create Key Vault
az keyvault create --name $KEYVAULT_NAME --resource-group $RESOURCE_GROUP --location $LOCATION

# Store a Secret
az keyvault secret set --vault-name $KEYVAULT_NAME --name $SECRET_NAME --value $SECRET_VALUE

echo "Key Vault $KEYVAULT_NAME created and secret $SECRET_NAME stored successfully!"

Conclusion

Azure Key Vault is a powerful service for managing and protecting sensitive information in the cloud. Using the az keyvault create command, organizations can easily create a secure vault and manage secrets, keys, and certificates effectively. By following best practices and utilizing the features provided by Azure Key Vault, teams can enhance their security posture and protect important data.

Next Steps

  1. Explore integrating Azure Key Vault with your applications to securely retrieve secrets at runtime.
  2. Experiment with managing certificates, including creation, renewal, and deployment.
  3. Monitor and audit access to your Key Vault to ensure compliance and security.

References