Distribuire una macchina virtuale con un certificato archiviato in modo sicuro nell'hub Azure StackDeploy a VM with a securely stored certificate on Azure Stack Hub
Questo articolo descrive come distribuire una macchina virtuale (VM) Hub Azure Stack con un certificato di Key Vault installato.This article describes how to deploy an Azure Stack Hub virtual machine (VM) with a Key Vault certificate installed.
PanoramicaOverview
I certificati vengono usati in molti scenari, ad esempio l'autenticazione per Active Directory o la crittografia del traffico Web.Certificates are used in many scenarios, such as authenticating to Active Directory, or encrypting web traffic. È possibile archiviare in modo sicuro i certificati come segreti in un insieme di credenziali delle chiavi dell'hub Azure Stack.You can securely store certificates as secrets in an Azure Stack Hub key vault. I vantaggi derivanti dall'uso di Azure Stack Hub Key Vault sono:The benefits of using Azure Stack Hub Key Vault are:
- I certificati non vengono esposti in uno script, in una cronologia della riga di comando o in un modello.Certificates are not exposed in a script, command-line history, or template.
- Il processo di gestione dei certificati è semplificato.The certificate management process is streamlined.
- Si ha il controllo delle chiavi che accedono ai certificati.You have control of the keys that access certificates.
Descrizione processoProcess description
Nei passaggi seguenti viene descritto il processo necessario per eseguire il push di un certificato nella macchina virtuale:The following steps describe the process required to push a certificate to the VM:
- Creare un segreto di Key Vault.Create a key vault secret.
- Aggiornare il azuredeploy.parameters.jssu file.Update the azuredeploy.parameters.json file.
- Distribuire il modello.Deploy the template.
Nota
È possibile usare questi passaggi dalla Azure Stack Development Kit (Gabriele) o da un client esterno se si è connessi tramite VPN.You can use these steps from the Azure Stack Development Kit (ASDK), or from an external client if you're connected through VPN.
PrerequisitiPrerequisites
- È necessario sottoscrivere un'offerta che includa il servizio Key Vault.You must subscribe to an offer that includes the Key Vault service.
- Installare PowerShell per l'Hub Azure stack.Install PowerShell for Azure Stack Hub.
- Configurare l'ambiente PowerShell dell'utente dell'Hub Azure stack.Configure the Azure Stack Hub user's PowerShell environment.
Crea un segreto dell'insieme di credenziali delle chiaviCreate a key vault secret
Lo script seguente crea un certificato nel formato pfx, crea un insieme di credenziali delle chiavi e archivia il certificato nell'insieme di credenziali delle chiavi come segreto.The following script creates a certificate in the .pfx format, creates a key vault, and stores the certificate in the key vault as a secret. Il contentType
del segreto deve essere impostato su pfx
.The contentType
of the secret must be set to pfx
.
Importante
È necessario usare il -EnabledForDeployment
parametro durante la creazione dell'insieme di credenziali delle chiavi.You must use the -EnabledForDeployment
parameter when creating the key vault. Questo parametro garantisce che sia possibile fare riferimento all'insieme di credenziali delle chiavi da Azure Resource Manager modelli.This parameter ensures that the key vault can be referenced from Azure Resource Manager templates.
# Create a certificate in the .pfx format
New-SelfSignedCertificate `
-certstorelocation cert:\LocalMachine\My `
-dnsname contoso.microsoft.com
$pwd = ConvertTo-SecureString `
-String "<Password used to export the certificate>" `
-Force `
-AsPlainText
Export-PfxCertificate `
-cert "cert:\localMachine\my\<certificate thumbprint that was created in the previous step>" `
-FilePath "<Fully qualified path to where the exported certificate can be stored>" `
-Password $pwd
# Create a key vault and upload the certificate into the key vault as a secret
$vaultName = "contosovault"
$resourceGroup = "contosovaultrg"
$location = "local"
$secretName = "servicecert"
$fileName = "<Fully qualified path to where the exported certificate can be stored>"
$certPassword = "<Password used to export the certificate>"
$fileContentBytes = get-content $fileName `
-Encoding Byte
$fileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)
$jsonObject = @"
{
"data": "$filecontentencoded",
"dataType" :"pfx",
"password": "$certPassword"
}
"@
$jsonObjectBytes = [System.Text.Encoding]::UTF8.GetBytes($jsonObject)
$jsonEncoded = [System.Convert]::ToBase64String($jsonObjectBytes)
New-AzResourceGroup `
-Name $resourceGroup `
-Location $location
New-AzKeyVault `
-VaultName $vaultName `
-ResourceGroupName $resourceGroup `
-Location $location `
-sku standard `
-EnabledForDeployment
$secret = ConvertTo-SecureString `
-String $jsonEncoded `
-AsPlainText -Force
Set-AzureKeyVaultSecret `
-VaultName $vaultName `
-Name $secretName `
-SecretValue $secret
Quando si esegue questo script, l'output include l'URI del segreto.When you run this script, the output includes the secret URI. Prendere nota di questo URI, perché è necessario farvi riferimento nel modello push certificate to Windows Gestione risorse.Make a note of this URI, as you must reference it in the Push certificate to Windows Resource Manager template. Scaricare la cartella VM-push-Certificate-Windows template nel computer di sviluppo.Download the vm-push-certificate-windows template folder to your development computer. Questa cartella contiene il azuredeploy.js e azuredeploy.parameters.jssui file, necessari nei passaggi seguenti.This folder contains the azuredeploy.json and azuredeploy.parameters.json files, which you need in the following steps.
Modificare il azuredeploy.parameters.jsnel file in base ai valori dell'ambiente.Modify the azuredeploy.parameters.json file according to your environment values. I parametri importanti sono il nome dell'insieme di credenziali, il gruppo di risorse dell'insieme di credenziali e l'URI del segreto (generato dallo script precedente).The important parameters are the vault name, the vault resource group, and the secret URI (as generated by the previous script). Nella sezione seguente viene illustrato un esempio di file di parametri.The following section shows an example of a parameter file.
Aggiorna il azuredeploy.parameters.jsnel fileUpdate the azuredeploy.parameters.json file
Aggiornare il azuredeploy.parameters.jsnel file con l' vaultName
URI del segreto,, VmName
e altri parametri in base all'ambiente in uso.Update the azuredeploy.parameters.json file with the vaultName
, secret URI, VmName
, and other parameters as per your environment. Il file JSON seguente mostra un esempio del file dei parametri del modello:The following JSON file shows an example of the template parameters file:
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"newStorageAccountName": {
"value": "kvstorage01"
},
"vmName": {
"value": "VM1"
},
"vmSize": {
"value": "Standard_D1_v2"
},
"adminUserName": {
"value": "demouser"
},
"adminPassword": {
"value": "demouser@123"
},
"vaultName": {
"value": "contosovault"
},
"vaultResourceGroup": {
"value": "contosovaultrg"
},
"secretUrlWithVersion": {
"value": "https://testkv001.vault.local.azurestack.external/secrets/testcert002/82afeeb84f4442329ce06593502e7840"
}
}
}
Distribuire il modelloDeploy the template
Distribuire il modello usando il seguente script di PowerShell:Deploy the template by using the following PowerShell script:
# Deploy a Resource Manager template to create a VM and push the secret to it
New-AzResourceGroupDeployment `
-Name KVDeployment `
-ResourceGroupName $resourceGroup `
-TemplateFile "<Fully qualified path to the azuredeploy.json file>" `
-TemplateParameterFile "<Fully qualified path to the azuredeploy.parameters.json file>"
Quando il modello viene distribuito correttamente, viene visualizzato l'output seguente:When the template is deployed successfully, it displays the following output:
Azure Stack Hub inserisce il certificato nella macchina virtuale durante la distribuzione.Azure Stack Hub pushes the certificate to the VM during deployment. Il percorso del certificato dipende dal sistema operativo della macchina virtuale:The certificate location depends on the operating system of the VM:
- In Windows, il certificato viene aggiunto al percorso del certificato LocalMachine , con l'archivio certificati fornito dall'utente.In Windows, the certificate is added to the LocalMachine certificate location, with the certificate store that the user provided.
- In Linux il certificato viene inserito nella directory /var/lib/waagent con il nome file UppercaseThumbprint. CRT per il file di certificato X509 e UppercaseThumbprint. prv per la chiave privata.In Linux, the certificate is placed under the /var/lib/waagent directory, with the file name UppercaseThumbprint.crt for the X509 certificate file and UppercaseThumbprint.prv for the private key.
Ritira certificatiRetire certificates
Il ritiro dei certificati fa parte del processo di gestione dei certificati.Retiring certificates is part of the certificate management process. Non è possibile eliminare la versione precedente di un certificato, ma è possibile disabilitarla usando il Set-AzureKeyVaultSecretAttribute
cmdlet.You can't delete the older version of a certificate, but you can disable it by using the Set-AzureKeyVaultSecretAttribute
cmdlet.
Nell'esempio seguente viene illustrato come disabilitare un certificato.The following example shows how to disable a certificate. Usare valori personalizzati per i VaultName
parametri, Name
e Version
.Use your own values for the VaultName
, Name
, and Version
parameters.
Set-AzureKeyVaultSecretAttribute -VaultName contosovault -Name servicecert -Version e3391a126b65414f93f6f9806743a1f7 -Enable 0