azure-powershell icon indicating copy to clipboard operation
azure-powershell copied to clipboard

Set-AzureVMOperatingSystem -WinRMHttps option does not enforce WinRMCertificateUrl to be specied. Error is not thrown until New-AzureVM

Open bgelens opened this issue 11 years ago • 10 comments

When deploying a VM using ARM cmdlets, the Set-AzureVMOperatingSystem let's you specify -WinRMHttps switch. When this switch is set, WinRMCertificateUrl parameter should be set as well but it is not enforced. You won't notice this missing until New-AzureVM tells you so.

error

This is a bit confusing as with v1 VMs, a self-signed cert would be generated and WinRM over SSL would be default. Either the previous behavior should be reinstated (e.g. no KeyVault Url means Self-Signed cert) or the switch being true and the Cert Url missing should throw an error during Set-AzureVMOperatingSystem invocation instead of New-AzureVM

bgelens avatar Sep 22 '15 08:09 bgelens

@huangpf please reassign if needed.

hovsepm avatar Oct 23 '15 18:10 hovsepm

Is there an update on this? The approach has certainly got complicated and I wonder if the previous behavior would be reinstated. I keep running into the error:

"New-AzureRmVM : InvalidParameter: Certificate https://kv-testvms.vault.azure.net/secrets/servicecert/db1baf0c860f456ks4nckh06d86e1ec8594 specified in WinRM configuration is not defined in the VM model secrets."

Followed this: https://msdn.microsoft.com/en-us/library/mt163529.aspx

Vijayaraghavanl avatar Dec 09 '15 16:12 Vijayaraghavanl

@Vijayaraghavanl We're working on fixing this bug but I'm also working on an article that explains how to setup winrm. Here's a draft version. Can you follow this and see if it works for you?

Step 1: Create a Key Vault

Learn how to create a Key Vault using PowerShell at the links below

NOTE: You'll need to set the "EnabledForDeployment" switch to enable the Microsoft.Compute resource provider to download your secret from the Key Vault

  • PowerShell - https://azure.microsoft.com/en-us/documentation/articles/key-vault-get-started/#vault

Step 2: Create a self-signed certificate

The self-signed certificate can be generated by following this guide - https://msdn.microsoft.com/en-us/library/ff699202.aspx

Step 3: Upload your self-signed certificate to the Key Vault

Before uploading the certificate to the Key Vault created in step 1, it needs to converted into a format the Microsoft.Compute resource provider will understand. The below PowerShell script will allow you do that

$fileName = "<Path to the .pfx file>"
$fileContentBytes = get-content $fileName -Encoding Byte
$fileContentEncoded = [System.Convert]::ToBase64String($fileContentBytes)

$jsonObject = @"
{
    "data": "$filecontentencoded",
    "dataType" :"pfx",
    "password": "<password>"
}
"@

$jsonObjectBytes = [System.Text.Encoding]::UTF8.GetBytes($jsonObject)
$jsonEncoded = [System.Convert]::ToBase64String($jsonObjectBytes)

$secret = ConvertTo-SecureString -String $jsonEncoded -AsPlainText –Force
Set-AzureKeyVaultSecret -VaultName "<vault name>" -Name "<secret name>" -SecretValue $secret

Step 4: Get the URL for your self-signed certificate in the Key Vault

The Microsoft.Compute resource provider needs a URL to the secret inside the Key Vault while provisioning the VM. This enables the Microsoft.Compute resource provider to download the secret and create the equivalent certificate on the VM.

Templates

You can get the link to the URL in the template using the below code

"certificateUrl": "[reference(resourceId(resourceGroup().name, 'Microsoft.KeyVault/vaults/secrets', '<vault-name>', '<secret-name>'), '2015-06-01').secretUriWithVersion]"

PowerShell

You can get this URL using the below PowerShell command

$secretURL = (Get-AzureKeyVaultSecret -VaultName "<vault name>" -Name "<secret name>").Id

Step 5: Reference your self-signed certificates URL while creating a VM

ARM Templates

While creating a VM through templates, the certificate gets referenced in the secrets section and the winRM section as below

"osProfile": {
      ...
      "secrets": [
        {
          "sourceVault": {
            "id": "<resource id of the Key Vault containing the secret>"
          },
          "vaultCertificates": [
            {
              "certificateUrl": "<URL for the certificate you got in Step 4>",
              "certificateStore": "<Name of the certificate store on the VM>"
            }
          ]
        }
      ],
      "windowsConfiguration": {
        ...
        "winRM": {
          "listeners": [
            {
              "protocol": "http"
            },
            {
              "protocol": "https",
              "certificateUrl": "<URL for the certificate you got in Step 4>"
            }
          ]
        },
        ...
      }
    },

A sample template for the above can be found here - https://azure.microsoft.com/en-us/documentation/templates/201-winrm-windows-vm Source code for this template can be found in Github - https://github.com/Azure/azure-quickstart-templates/tree/master/201-winrm-windows-vm

PowerShell

$vm = New-AzureRmVMConfig -VMName "<VM name>" -VMSize "<VM Size>"
$credential = Get-Credential
$secretURL = (Get-AzureKeyVaultSecret -VaultName "<vault name>" -Name "<secret name>").Id
$vm = Set-AzureRmVMOperatingSystem -VM $vm  -Windows -ComputerName "<Computer Name>" -Credential $credential -WinRMHttps -WinRMCertificateUrl $secretURL
$sourceVaultId = (Get-AzureRmKeyVault -ResourceGroupName "<Resource Group name>" -VaultName "<Vault Name>").ResourceId
$CertificateStore = "My"
$vm = Add-AzureRmVMSecret -VM $vm -SourceVaultId $sourceVaultId -CertificateStore $CertificateStore -CertificateUrl $secretURL

singhkays avatar Dec 09 '15 19:12 singhkays

@singhkay is it even possible to use a self-signed certificate to connect to WinRM this way? wirm quickconfig says it's not:

PS C:\Users\georgiosd> winrm quickconfig -transport:https
WinRM service is already running on this machine.
WSManFault
    Message
        ProviderFault
            WSManFault
                Message = Cannot create a WinRM listener on HTTPS because this machine does not have an appropriate certificate. To be used for SSL, a certificate must have a CN matching the hostname, be appropriate for Server Authentication, and not be expired, revoked, or self-signed.

Error number:  -2144108267 0x80338115
Cannot create a WinRM listener on HTTPS because this machine does not have an appropriate certificate. To be used for SSL, a certificate must have a CN matching the hostname, be appropriate for Server Authentication, and not be expired, revoked, or self-signed.

georgiosd avatar Dec 21 '15 00:12 georgiosd

@georgiosd Since I posted this I found out you can create a self-cert using the following PS script

$certificateName = "somename"
$thumbprint = (New-SelfSignedCertificate -DnsName "$certificateName" -CertStoreLocation Cert:\CurrentUser\My -KeySpec KeyExchange).Thumbprint
$cert = (Get-ChildItem -Path cert:\CurrentUser\My\$thumbprint)
$password = Read-Host -Prompt "Please enter the certificate password." -AsSecureString
Export-PfxCertificate -Cert $cert -FilePath ".\$certificateName.pfx" -Password $password

Once I did that, I had to Enable-PSRemoting -Force

then Enter-PSSession -ConnectionUri https://<public-ip-dns-of-the-vm>:5986 -Credential $cred -SessionOption (New-PSSessionOption -SkipCACheck -SkipCNCheck -SkipRevocationCheck) -Authentication Negotiate

to WinRM to the VM

singhkays avatar Dec 21 '15 03:12 singhkays

Closing this as the original issue is fixed in 1.0.2 release https://github.com/Azure/azure-powershell/releases/tag/v1.0.2-December2015

singhkays avatar Dec 29 '15 22:12 singhkays

The script given above to upload cert to KeyVault and use that url is not working and hrows the same "specified in WinRM configuration is not defined in the VM model secrets.". Has the format changed?

RamjotSingh avatar May 28 '16 19:05 RamjotSingh

I'm getting the same error as RanjotSingh above. New-AzureRmVM : Certificate https://[keyvaultname].vault.azure.net/secrets/WinRMKeyUpload/[KeyID] specified in WinRM configuration is not defined in the VM model secrets. ErrorCode: InvalidParameter

Any suggestions?

calep avatar Jul 18 '17 01:07 calep

Same here.. new issues.. this is closed, so ms wont even glance at it 👎

thepixelabs avatar Apr 03 '18 12:04 thepixelabs

For those getting the "Certificate specified in WinRM configuration is not defined in the VM model secrets", when I hit this issue, I was missing the Add-AzureRMVMSecret cmdlet. Adding that to the VM config (eg. after the call to Set-AzureRMVMOperatingSystem) fixed it for me.

sjlouder avatar Jun 25 '18 15:06 sjlouder