There is no doubt that Azure ARM templates are now the best way to deploy and manage resources within Azure. Recently, I found myself creating an ARM template that deployed some Windows Server 2016 virtual machines with PowerShell DSC extension to configure them. This is typically very simple, define the virtual machine include the extension, however, this time I also needed to include sensitive pieces of information; API keys and credentials, and then I wondered, well, what is the best way to protect these?
If you are familiar with DSC, you can either leave these assets in plaintext or encrypt them with a certificate. Now most documentation and posts found online, will direct for the plaintext approach when using the DSC extension, but there is a significantly better approach, which allows for sensitive information to be encrypted throughout the deployment process. Even better, it is really simple to implement.
Whilst I was working through setting this up, I found there are some quirks in how you need to put the ARM template together, and thought it would be a good topic for a post. I am going to talk about some of the challenges that I found, as well as provide some working examples.
Understand the apiVersion and typeHandlerVersion
When you define the PowerShell DSC extension for an Azure virtual machine, there are two fields that control the version and functionality that the extension will provide, apiVersion and typeHandlerVersion.
In terms of ARM Templates, the
apiVersion specifies how the deployment process talks to Azure, that is the layout and content of calls that are made to the underlying API, as such, it controls what we can place into out template files. Microsoft uses the
apiVersion to provide backward compatibility. When breaking changes are introduced, typically as part of introducing new features, Microsoft will define a new version. This allows for older scripts and templates to continue to function long after they have been written. For the PowerShell DSC extension, the
apiVersion will impact what fields are valid in your template definition.
typeHandlerVersion, is extremely critical, it defines what version of the extension will be deployed to the virtual machine and then executed. Whilst apiVersion controls talking to API, the
typeHandlerVersion controls what is happening on the virtual machine itself. This extension regularly receives new features and bug fixes, from support for new operating systems, privacy settings, to WMF versions, and a tonne of issues fixed in between.
Now for the catch, where the devil enters the details. Depending upon what versions you specify, your template syntax may need to change. I found out about these quirks the hard way, this tiny detail can cost you quite a bit of time troubleshooting syntax issues. A mismatch between the
typeHandlerVersion and your template’s syntax could leave to errors during template validation, DSC compilation or DSC application.
So, what happens if you use older version or mix versions and syntax? Well, nothing bad might happen, or you might end up with either template validation errors, DSC complication errors, or even errors during DSC processing (that is, as the DSC is applied to the server).
But what about autoUpgradeMinorVersion?
According to the documentation, the extensions have a Boolean attribute,
autoUpgradeMinorVersion, that allows a user to pick simply the major version, and have Azure install the latest version to their virtual machine during provisioning time. It should be highlighted that this only happens during the provisioning of the extension, extensions will not be upgraded unless the user explicitly removes and then re-provisions the extension. Hot fixes (that is, those of the format of 2.9.x.x), are automatically selected, you don’t get any control. The problem is, this only upgrades the extension, it isn’t going to impact your ARM template syntax.
Be Wary of Visual Studio Created Templates
If you are developing your ARM templates within Visual Studio, you are probably using the “Add New Resource” window to include the PowerShell DSC Extension into your virtual machine. It so happens, that, at least in Visual Studio 2017, it will default to an old
typeHandlerVersion of 2.9. For most, this isn’t a problem, especially if
autoUpgradeMinorVersion is set to
I do want to point out that versions 2.4 up to 2.13 were retired in August 2016.
If you do need to handle sensitive data, then work with the release history for the PowerShell DSC extension, and the documentation on the syntax to ensure that you are working against the latest versions.
How to correctly handle sensitive data?
Note/Warning: At the time of writing, this all worked correctly for versions 2.24 to 2.26. It may not be correct for later versions.
So how do we correctly pass sensitive data between our template through to our virtual machine and DSC?
To begin with, you need to understand that within the properties for the DSC extension, we have two attributes that can be used to specify settings, the first, settings, which you are probably familiar with. There is another,
protectedSettings, you might already be familiar with this one, typically you will see
configurationUrlSasToken, but you can also specify sensitive data to be passed to the DSC configuration here as well.
So, I have a super simple DSC configuration, it will create a user account using the specified details. I have opted to provide the accounts username and password using a
PSCredential parameter, whilst I am also going to set a description for the account, specified as a
UserName = $Credential.UserName
Description = $AccountDescription
Disabled = $false
Ensure = 'Present'
Password = $Credential.Password
PasswordNeverExpires = $true
Obviously, I want to ensure that credential is kept securely, I don’t want it left in plaintext as part of the DSC compilation.
The ARM template looks like this:
"description": "Name of the virtual machine"
"description": "Account Username"
"description": "Account Password"
"description": "Path to DSC Package"
"description": "Sas Token"
"AccountDescription": "Account Created as part of DSC"
See the cool shortcut I have done in the
protectedSettings? See how the Credential is built within the template, I provide the username and password and the Azure takes care of the rest and DSC just gets a
PSCredential. Pretty neat!
It is worth highlighting that my non-sensitive data, like the
AccountDescription, are still found within the
settings attribute, I have only moved my sensitive data to
protectedSettings. Of course, this decision is entirely up to you.
Now, it might just have been my paranoia, but I found that things when much more smoothly if I placed the
settings. It shouldn’t matter, but things just seemed happier, and I know that doesn’t sound very logical.
If you have organisational privacy concerns
It is worth noting that if you have strict organisational privacy or data sharing controls, you should disable data collection,
The two examples show here are up on GitHub, I have also included a more detailed example as a Visual Studio 2017 project.
PS. I am still looking for talented individuals to join my team at Readify. I am after people with a passion for infrastructure, Azure, operations, and everything in between. Does that sound like you? Hit me up on Twitter or LinkedIn!