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

New-AzBlueprintAssignment using SecureStringParameter fails

Open apraamsm opened this issue 7 years ago • 5 comments

Description

When i use a hashtable with keyvault secret to pass to the -SecureStringParameter of the New-AzBlueprintAssignment (or Set-AzBlueprintAssignment) cmdlet the cmdlet fails. Without the SecureStringParameter my code works fine and i can use everything i want in the Blueprint i have. But at this point i am going to roll out VMs with the Blueprint and needed a securestring parameter for the localadmin password of those VMs

This is how my hashtable for the keyvault secret is generated;

$keyvaultId = (Get-AzKeyVault -VaultName "xxxxxxVault-xxx-kv" -ResourceGroupName "xxxxxxx-xxx-rg").ResourceId
$keyVaultSecret = Get-AzKeyVaultSecret -VaultName "xxxxxxVault-xxx-kv" -Name "localadmin"
$vmLocalAdminPassword = @{
    keyVaultId = $keyvaultId;
    secretName = $keyVaultSecret.Name;
    secretVersion = $keyVaultSecret.Version
}

Steps to reproduce

When i run this code to assign a blueprint that has changed after a deploy i get the error

foreach($bluePrint in $bluePrints)
{
    $blueprintName = ($bluePrint).replace("envId","$environmentId")
    $myBlueprint = Get-AzBlueprint -managementGroupId $managementGroupId -Name $blueprintName -LatestPublished
    $assignmentName = "assignment-$environmentId-$blueprintName"
    $location = "westeurope"

    $oldAssignment = Get-AzBlueprintAssignment -SubscriptionId $subscriptionId -Name $assignmentName -ErrorAction silentlycontinue
    if ($oldAssignment)
    {
        Write-Host "Updating existing assignment..."
        Set-AzBlueprintAssignment -Blueprint $myBlueprint -Location $location -SubscriptionId $subscriptionId -ResourceGroupParameter $rgArray `
        -Parameter $generalParams -Name $assignmentName -UserAssignedIdentity $userAssignedPrincipalId -SecureStringParameter $vmLocalAdminPassword
    }
    else
    {
        Write-Host "Creating new assignment..."
        New-AzBlueprintAssignment -Blueprint $myBlueprint -Location $location -SubscriptionId $subscriptionId -ResourceGroupParameter $rgArray `
        -Parameter $generalParams -Name $assignmentName -UserAssignedIdentity $userAssignedPrincipalId -SecureStringParameter $vmLocalAdminPassword
    }

}

Environment data

                                                                                  
PSVersion                      5.1.17134.765                                                                                                                                
PSEdition                      Desktop                                                                                                                                      
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                                                                      
BuildVersion                   10.0.17134.765                                                                                                                               
CLRVersion                     4.0.30319.42000                                                                                                                              
WSManStackVersion              3.0                                                                                                                                          
PSRemotingProtocolVersion      2.3                                                                                                                                          
SerializationVersion           1.1.0.1  

Module versions

Script 1.1.2      AxAzureBlueprint                         
    Script 1.1.0      AxAzureBlueprint                         
    Script 1.5.2      Az.Accounts                              
    Script 1.5.1      Az.Accounts                              
    Script 1.5.0      Az.Accounts                              
    Script 1.3.0      Az.Accounts                              
    Script 1.2.1      Az.Accounts                              
    Script 1.0.0      Az.AnalysisServices                      
    Script 1.0.0      Az.Automation                            
    Script 0.1.1      Az.Blueprint                             
    Script 1.0.0      Az.Cdn                                   
    Script 1.1.0      Az.Compute                               
    Script 1.0.0      Az.ContainerRegistry                     
    Script 1.0.0      Az.DataFactory                           
    Script 1.0.1      Az.DataLakeStore                         
    Script 1.0.0      Az.EventHub                              
    Script 1.0.0      Az.KeyVault                              
    Script 1.1.0      Az.LogicApp                              
    Script 0.7.1      Az.ManagedServiceIdentity                
    Script 1.0.0      Az.Monitor                               
    Script 1.0.0      Az.Network                               
    Script 1.0.0      Az.OperationalInsights                   
    Script 1.1.0      Az.Resources                             
    Script 0.7.4      Az.Security                              
    Script 1.0.0      Az.ServiceFabric                         
    Script 1.0.1      Az.Sql                                   
    Script 1.0.1      Az.Storage                               
    Script 1.0.0      Az.TrafficManager                        
    Script 1.0.1      Az.Websites                              
    Script 3.12.0     AzSK                                     
    Script 3.10.0     AzSK                                     
    Script 0.9.5      AzSK.AzureDevOps   

Debug output

Creating new assignment...
DEBUG: 5:50:27 PM - NewAzureRmBlueprintAssignment begin processing with ParameterSet 'CreateBlueprintAssignmentBySystemAssigned'.
DEBUG: 5:50:27 PM - using account id '--removed--'...
New-AzBlueprintAssignment : Object reference not set to an instance of an object.
At C:\Data\_play\blueprints\scripts\xxxxx\assign-blueprints.ps1:44 char:9
+         New-AzBlueprintAssignment -Blueprint $myBlueprint -Location $ ...
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [New-AzBlueprintAssignment], NullReferenceException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Blueprint.Cmdlets.NewAzureRmBlueprintAssignment
 
DEBUG: AzureQoSEvent: CommandName - New-AzBlueprintAssignment; IsSuccess - False; Duration - 00:00:00.1568781; Exception - System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Azure.Commands.Blueprint.Cmdlets.BlueprintCmdletBase.CreateAssignmentObject(String identityType, String userAssignedIdentity, String bpLocation, String blueprintId, Nullable`1 lockMode, Hashtable Par
ameters, Hashtable ResourceGroups, Hashtable SecureStringParameters)
   at Microsoft.Azure.Commands.Blueprint.Cmdlets.NewAzureRmBlueprintAssignment.ExecuteCmdlet();
DEBUG: Finish sending metric.
DEBUG: 5:50:28 PM - NewAzureRmBlueprintAssignment end processing.
DEBUG: 5:50:28 PM - NewAzureRmBlueprintAssignment end processing.

Error output

   HistoryId: 7


Message        : Object reference not set to an instance of an object.
StackTrace     :    at Microsoft.Azure.Commands.Blueprint.Cmdlets.BlueprintCmdletBase.CreateAssignmentObject(String identityType, String userAssignedIdentity, String bpLocation, String blueprintId, Nullable`1 
                 lockMode, Hashtable Parameters, Hashtable ResourceGroups, Hashtable SecureStringParameters)
                    at Microsoft.Azure.Commands.Blueprint.Cmdlets.NewAzureRmBlueprintAssignment.ExecuteCmdlet()
Exception      : System.NullReferenceException
InvocationInfo : {New-AzBlueprintAssignment}
Line           :         New-AzBlueprintAssignment -Blueprint $myBlueprint -Location $location -SubscriptionId $subscriptionId -ResourceGroupParameter $rgArray `
                 
Position       : At C:\Data\_play\blueprints\scripts\xxxxx\assign-blueprints.ps1:44 char:9
                 +         New-AzBlueprintAssignment -Blueprint $myBlueprint -Location $ ...
                 +         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HistoryId      : 7

RequestId      : 4878d896-1653-419c-83fc-772c28a51c88
Message        : Assignment 'assignment-dev-blueprint-dev-bp' could not be found in subscription 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx'.
ServerMessage  : AssignmentNotFound: Assignment 'assignment-dev-blueprint-dev-bp' could not be found in subscription 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx'. 
                 (System.Collections.Generic.List`1[Microsoft.Rest.Azure.CloudError])
ServerResponse : {NotFound}
RequestMessage : {GET 
                 https://management.azure.com//subscriptions/xxxxxx-c71f-xxxx-xxxx-ab0c203d2215/providers/Microsoft.Blueprint/blueprintAssignments/assignment-dev-blueprint-dev-bp?api-version=2018-11-01-preview}
InvocationInfo : {Get-AzBlueprintAssignment}
Line           :     $oldAssignment = Get-AzBlueprintAssignment -SubscriptionId $subscriptionId -Name $assignmentName -ErrorAction silentlycontinue
                 
Position       : At C:\Data\_play\blueprints\scripts\xxxxx\assign-blueprints.ps1:34 char:22
                 + ... ssignment = Get-AzBlueprintAssignment -SubscriptionId $subscriptionId ...
                 +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
StackTrace     :    at Microsoft.Azure.Management.Blueprint.AssignmentsOperations.<GetWithHttpMessagesAsync>d__6.MoveNext()
                 --- End of stack trace from previous location where exception was thrown ---
                    at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
                    at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
                    at Microsoft.Azure.Commands.Blueprint.Common.BlueprintClient.GetBlueprintAssignment(String subscriptionId, String blueprintAssignmentName)
                    at Microsoft.Azure.Commands.Blueprint.Cmdlets.GetAzureRmBlueprintAssignment.ExecuteCmdlet()
HistoryId      : 7

DEBUG: AzureQoSEvent: CommandName - Resolve-AzError; IsSuccess - True; Duration - 00:00:00.1373810; Exception - ;
DEBUG: Finish sending metric.
DEBUG: 5:54:27 PM - ResolveError end processing.

apraamsm avatar May 25 '19 16:05 apraamsm

@filizt Hi, could you take a look at this?

niander avatar May 28 '19 21:05 niander

@apraamsm - the correct format for passing the secure strings is as follows: @{mySecureStringParam=@{keyVaultId='/subscriptions/00000000-1111-0000-1111-000000000000/rsourcegroups/myResourceGroup/providers/Microsoft.Keyvault/Vaults/myKeyVault';secretName='mySecret';secretVersion='1.0'}}

Looks like the format in the New-AzBlueprintAssignment example in help documents is wrong. We'll update that.

filizt avatar May 28 '19 23:05 filizt

@apraamsm Did the above format for the secure string work for you?

filizt avatar Jun 03 '19 16:06 filizt

thanks for the reply, i will try this asap and get back to you

apraamsm avatar Jun 03 '19 22:06 apraamsm

Thanks again for your help :-) Works like a charm

apraamsm avatar Jun 04 '19 10:06 apraamsm