Quickstart: Een Azure sleutelkluis en een geheim maken met behulp van Bicep

Azure Key Vault is een cloudservice die een beveiligd archief biedt voor geheimen, zoals sleutels, wachtwoorden, certificaten en andere geheimen. Deze quickstart richt zich op het implementeren van een Bicep-bestand voor het aanmaken van een Key Vault en een secret.

Bicep is een domeinspecifieke taal (DSL) die declaratieve syntaxis gebruikt om Azure resources te implementeren. Het biedt beknopte syntaxis, betrouwbare typeveiligheid en ondersteuning voor het hergebruik van code. Bicep biedt de beste ontwerpervaring voor uw infrastructuur als code-oplossingen in Azure.

Vereiste voorwaarden

  • Als u geen Azure-abonnement hebt, maakt u een vrij account voordat u begint.

  • Uw Microsoft Entra gebruikersobject-id is nodig voor de sjabloon om machtigingen te configureren. Met de volgende procedure wordt de object-id (GUID) opgehaald.

    1. Voer de volgende opdracht Azure PowerShell of Azure CLI uit door Try it te selecteren en plak het script in het shell-deelvenster. Plak het script door met de rechtermuisknop op de shell te klikken en Plakken te selecteren.

      echo "Enter your email address that is used to sign in to Azure:" &&
      read upn &&
      az ad user show --id $upn --query "id" &&
      echo "Press [ENTER] to continue ..."
      
    2. Noteer de object-id. U hebt deze nodig in de volgende sectie van deze quickstart.

Het Bicep-bestand bekijken

Belangrijk

In deze quickstart wordt een externe sjabloon gebruikt waarmee een kluis met verouderd toegangsbeleid wordt gemaakt. Voor productie-implementaties gebruikt u in plaats daarvan Azure RBAC-autorisatie. Zie Maak een Azure key vault en een sleutel met behulp van Bicep voor een Bicep-sjabloon die gebruikmaakt van enableRbacAuthorization: true of zie Beveilig uw Azure Key Vault voor uitgebreide beveiligingsrichtlijnen.

De sjabloon die in deze quickstart wordt gebruikt, is afkomstig uit Azure Quickstart-sjablonen.

@description('Specifies the name of the key vault.')
param keyVaultName string

@description('Specifies the Azure location where the key vault should be created.')
param location string = resourceGroup().location

@description('Specifies whether Azure Virtual Machines are permitted to retrieve certificates stored as secrets from the key vault.')
param enabledForDeployment bool = false

@description('Specifies whether Azure Disk Encryption is permitted to retrieve secrets from the vault and unwrap keys.')
param enabledForDiskEncryption bool = false

@description('Specifies whether Azure Resource Manager is permitted to retrieve secrets from the key vault.')
param enabledForTemplateDeployment bool = false

@description('Specifies the Azure Active Directory tenant ID that should be used for authenticating requests to the key vault. Get it by using Get-AzSubscription cmdlet.')
param tenantId string = subscription().tenantId

@description('Specifies the object ID of a user, service principal or security group in the Azure Active Directory tenant for the vault. The object ID must be unique for the list of access policies. Get it by using Get-AzADUser or Get-AzADServicePrincipal cmdlets.')
param objectId string

@description('Specifies the permissions to keys in the vault. Valid values are: all, encrypt, decrypt, wrapKey, unwrapKey, sign, verify, get, list, create, update, import, delete, backup, restore, recover, and purge.')
param keysPermissions array = [
  'list'
]

@description('Specifies the permissions to secrets in the vault. Valid values are: all, get, list, set, delete, backup, restore, recover, and purge.')
param secretsPermissions array = [
  'list'
]

@description('Specifies whether the key vault is a standard vault or a premium vault.')
@allowed([
  'standard'
  'premium'
])
param skuName string = 'standard'

@description('Specifies the name of the secret that you want to create.')
param secretName string

@description('Specifies the value of the secret that you want to create.')
@secure()
param secretValue string

resource kv 'Microsoft.KeyVault/vaults@2023-07-01' = {
  name: keyVaultName
  location: location
  properties: {
    enabledForDeployment: enabledForDeployment
    enabledForDiskEncryption: enabledForDiskEncryption
    enabledForTemplateDeployment: enabledForTemplateDeployment
    tenantId: tenantId
    enableSoftDelete: true
    softDeleteRetentionInDays: 90
    accessPolicies: [
      {
        objectId: objectId
        tenantId: tenantId
        permissions: {
          keys: keysPermissions
          secrets: secretsPermissions
        }
      }
    ]
    sku: {
      name: skuName
      family: 'A'
    }
    networkAcls: {
      defaultAction: 'Allow'
      bypass: 'AzureServices'
    }
  }
}

resource secret 'Microsoft.KeyVault/vaults/secrets@2023-07-01' = {
  parent: kv
  name: secretName
  properties: {
    value: secretValue
  }
}

output location string = location
output name string = kv.name
output resourceGroupName string = resourceGroup().name
output resourceId string = kv.id

Er worden twee Azure resources gedefinieerd in het Bicep-bestand:

Het Bicep-bestand implementeren

  1. Sla het bestand Bicep op als main.bicep naar uw lokale computer.

  2. Implementeer het Bicep bestand met behulp van Azure CLI of Azure PowerShell.

    az group create --name myResourceGroup --location eastus
    az deployment group create --resource-group myResourceGroup --template-file main.bicep --parameters keyVaultName=<vault-name> objectId=<object-id>
    

    Opmerking

    Vervang door <vault-name> de naam van de sleutelkluis. Vervang <object-id> door de object-id van een gebruiker, service-principalaccount of beveiligingsgroep in de Microsoft Entra-tenant voor de Key Vault. De object-id moet uniek zijn voor de lijst met toegangsbeleidsregels. Haal deze op met behulp van de cmdlets Get-AzADUser of Get-AzADServicePrincipal.

    Wanneer de implementatie is voltooid, ziet u een bericht waarin wordt aangegeven dat de implementatie is voltooid.

Geïmplementeerde middelen beoordelen

U kunt de Azure-portal gebruiken om de sleutelkluis en het geheim te controleren, of het volgende Azure CLI of Azure PowerShell script gebruiken om het gemaakte geheim weer te geven.

echo "Enter your key vault name:" &&
read keyVaultName &&
az keyvault secret list --vault-name $keyVaultName &&
echo "Press [ENTER] to continue ..."

De hulpbronnen opschonen

Gebruik de Azure-portal, Azure CLI of Azure PowerShell om de resourcegroep en de bijbehorende resources te verwijderen wanneer u deze niet meer nodig hebt.

az group delete --name myResourceGroup

Volgende stappen

In deze quickstart hebt u een sleutelkluis en een geheim gemaakt met behulp van Bicep en vervolgens de implementatie gevalideerd. Ga verder met de onderstaande artikelen voor meer informatie over Key Vault en Bicep.