Key Lifecycle Management in Azure

Coding + Data Security + Azure maurotommasi todayJanuary 15, 2024

Background
share close

Key Lifecycle Management (KLM) is a crucial aspect of cryptographic key management, ensuring the secure generation, distribution, usage, and retirement of cryptographic keys. In the context of Azure, a cloud computing platform by Microsoft, Key Vault provides a robust solution for managing cryptographic keys and secrets, offering advanced features for key lifecycle management.

Understanding KLM

Generation

In Azure Key Vault, keys can be generated using various algorithms and key sizes based on your security requirements.

# Powershell

$key = New-AzKeyVaultKey -VaultName 'YourKeyVault' -Name 'KeyName' -KeyType 'RSA' -KeySize 2048
# Python

key_client = KeyClient(vault_url='https://YourKeyVault.vault.azure.net/', credential=credential)
key = key_client.create_rsa_key('KeyName', size=2048)

Storage

Keys need to be securely stored to prevent unauthorized access. Azure Key Vault encrypts keys at rest and allows the configuration of access policies.

Set-AzKeyVaultAccessPolicy -VaultName 'YourKeyVault' -ResourceGroupName 'YourResourceGroup' -ObjectId 'YourUserOrAppObjectId' -PermissionsToKeys all
  1. -VaultName 'YourKeyVault':
    • Specifies the name of the Azure Key Vault where the access policy is being set.
  2. -ResourceGroupName 'YourResourceGroup':
    • Specifies the name of the resource group containing the Azure Key Vault.
    • The resource group is a logical container for resources deployed in Azure.
  3. -ObjectId 'YourUserOrAppObjectId':
    • Specifies the Object ID of the user, group, or application for which the access policy is applied.
    • The Object ID uniquely identifies the user, group, or application within Azure Active Directory (Azure AD).
  4. -PermissionsToKeys all:
    • Specifies the permissions granted for keys.
    • In this example, the access policy grants all permissions for keys (all).
ParameterDescription
-VaultNameName of the Azure Key Vault where the policy is applied.
-ResourceGroupNameName of the resource group containing the Key Vault.
-ObjectIdObject ID of the user, group, or application.
-PermissionsToKeysPermissions granted for keys (e.g., ‘all’, ‘get’, ‘create’).
Parameters
Permissions ValueDescription
allGrants all permissions for keys.
getGrants permission to get keys.
createGrants permission to create keys.
deleteGrants permission to delete keys.
listGrants permission to list keys.
importGrants permission to import keys.
updateGrants permission to update keys.
backupGrants permission to backup keys.
restoreGrants permission to restore keys.
recoverGrants permission to recover keys.
PermissionToKeys
from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient

# Key Vault configuration
vault_url = "https://YourKeyVaultName.vault.azure.net/"
resource_group = "YourResourceGroup"
object_id = "YourUserOrAppObjectId"
permissions_to_keys = ["all"]

# Authenticate to Azure Key Vault
credential = DefaultAzureCredential()
secret_client = SecretClient(vault_url=vault_url, credential=credential)

# Set Access Policy
secret_client.set_access_control(
    {
        "object_id": object_id,
        "permissions": {"keys": permissions_to_keys}
    }
)
  1. vault_url = "https://YourKeyVaultName.vault.azure.net/":
    • Specifies the URL of the Azure Key Vault where the access policy is being set.
  2. resource_group = "YourResourceGroup":
    • Specifies the name of the resource group containing the Azure Key Vault.
    • The resource group is a logical container for resources deployed in Azure.
  3. object_id = "YourUserOrAppObjectId":
    • Specifies the Object ID of the user, group, or application for which the access policy is applied.
    • The Object ID uniquely identifies the user, group, or application within Azure Active Directory (Azure AD).
  4. permissions_to_keys = ["all"]:
    • Specifies the permissions granted for keys.
    • In this example, the access policy grants all permissions for keys (all).
  5. Authenticate to Azure Key Vault:
    • The DefaultAzureCredential is used to authenticate to Azure Key Vault using the application’s identity.
  6. Set Access Policy:
    • The set_access_control method is used to set the access policy for the specified user, group, or application.
    • The access control dictionary includes the object_id and permissions for keys.

Permissions Granted by all:

The permissions granted by setting ["all"] for keys in this example are similar to the PowerShell example:

  • get
  • create
  • delete
  • list
  • update
  • backup
  • restore
  • recover
  • purge
  • import

Usage

Keys are used for encryption, decryption, signing, and verification. Azure Key Vault allows applications to retrieve keys for cryptographic operations securely.

Here an example in Python: https://prodata.engineering/connect-a-python-app-to-azure-key-vault-and-cosmodb/

Rotation

Regularly rotating keys enhances security. Azure Key Vault supports automated key rotation.

# Powershell

$key = Get-AzKeyVaultKey -VaultName 'YourKeyVault' -KeyName 'KeyName'
Update-AzKeyVaultKey -VaultName 'YourKeyVault' -ResourceGroupName 'YourResourceGroup' -KeyVersion $key.Key.Kid
# Python

key_client.rotate_key('KeyName')

Retirement/Deletion

Retiring or deleting keys is essential when they are no longer needed. Azure Key Vault supports soft-delete, allowing recovery within a retention period.

#Powershell

Remove-AzKeyVaultKey -VaultName 'YourKeyVault' -KeyName 'KeyName'
# Python

key_client.begin_delete_key('KeyName')

Audit/Logging

Key Vault provides comprehensive audit logs, enabling organizations to monitor key access and operations. For example we can use Aure Portal to monitor Key Valuts.

To Recap (Powershell, Python, C++)

Key Generation:

OperationPowerShellPythonC++
Create KeyNew-AzKeyVaultKey -VaultName 'YourKeyVault' -Name 'KeyName' -KeyType 'RSA' -KeySize 2048key_client.create_rsa_key('KeyName', size=2048)auto keyClient = Azure::KeyVault::Keys::KeyClient(vaultUrl, credential); auto key = keyClient.CreateRsaKey('KeyName', 2048);

Key Storage:

OperationPowerShellPythonC++
Set Access PolicySet-AzKeyVaultAccessPolicy -VaultName 'YourKeyVault' -ResourceGroupName 'YourResourceGroup' -ObjectId 'YourUserOrAppObjectId' -PermissionsToKeys allkey_client.set_access_control(entry.acl)keyClient.SetAccessPolicy('YourUserOrAppObjectId', KeyOperations::All);

Key Usage:

OperationPowerShellPythonC++
Retrieve Key$key = Get-AzKeyVaultKey -VaultName 'YourKeyVault' -KeyName 'KeyName'key = key_client.get_key('KeyName')auto key = keyClient.GetKey('KeyName');
Encrypt/Decrypt Data$encryptedData = Encrypt-Data -Key $key -PlainText 'SensitiveData'encrypted_data = key_client.encrypt('RSA1_5', plaintext)auto result = cryptoClient.Encrypt(KeyId, EncryptParameters);

Key Rotation:

OperationPowerShellPythonC++
Get Key Version$key = Get-AzKeyVaultKey -VaultName 'YourKeyVault' -KeyName 'KeyName'key = key_client.get_key('KeyName')auto key = keyClient.GetKey('KeyName');
Rotate KeyUpdate-AzKeyVaultKey -VaultName 'YourKeyVault' -ResourceGroupName 'YourResourceGroup' -KeyVersion $key.Key.Kidkey_client.rotate_key('KeyName')auto result = keyClient.RotateKey('KeyName');

Key Retirement/Deletion:

OperationPowerShellPythonC++
Remove KeyRemove-AzKeyVaultKey -VaultName 'YourKeyVault' -KeyName 'KeyName'key_client.begin_delete_key('KeyName')keyClient.DeleteKey('KeyName');
Purge Key (Soft Delete)Remove-AzKeyVaultKey -VaultName 'YourKeyVault' -KeyName 'KeyName' -InRemovedState -Purgekey_client.purge_deleted_key('KeyName')keyClient.PurgeDeletedKey('KeyName');

Written by: maurotommasi

Rate it
Previous post

Similar posts

Post comments (0)

Leave a reply

Your email address will not be published. Required fields are marked *


LOGO

  • help@firwl.com
  • info@firwl.com


Products


Company


Contacts

Support

en_USEnglish