Skip to content

Instantly share code, notes, and snippets.

@noelbundick
Created December 15, 2018 23:18
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noelbundick/7378efa4b34206f6e32f39b1297dad35 to your computer and use it in GitHub Desktop.
Save noelbundick/7378efa4b34206f6e32f39b1297dad35 to your computer and use it in GitHub Desktop.
Secure code execution via ARM template and Azure Container Instances

Secure code execution via ARM template and Azure Container Instances

What is this?

It's a template to execute authenticated az commands from an ARM template deployment, without storing or passing credentials of any kind

Why did you make it?

I was recently looking to move my blog from Azure Web Apps to a static site hosted on Azure Storage.

I wanted to have an ARM template so I can stand up other sites / use as a reference, etc. Unfortunately, enabling the static website feature on Azure Storage is currently a data-plane operation, and ARM templates only execute against the data plane.

How does it work?

The template creates

  • A Managed Identity, which I can use with some services to eliminate the need for Service Principals & passwords
  • A Role Assignment so that the Identity can take actions in my resource group
  • An Azure Storage account that I'll use to deploy a static website on
  • An Azure Container Instance that runs my bootstrap commands - here using the azure-cli

To deploy, run the following 2 commands

az group create -n website -l westus
az group deployment create -g website --template-file azuredeploy.json --parameters baseName=mystorageaccount

Next steps

It seems to make sense to move the script out of the inline template - it's hard to read - might eventually hit some string lenght limits (I haven't checked or tested this yet). I could mount a GitRepo volume and pin to a specific commit here

To use with other non-Azure services, I'd replace the az storage blob commands and instead use az keyvault secret show to download a secret from a preexisting Key Vault. There's a little bit more work in precreating the Identity outside of this template, making sure it's in the KV accessPolicies, and then pass the Idenitity as a parameter to the template - but the flow is logically the same. Once you have a secret from Key Vault, use it to run whatever script(s) you need, and then when the container is finished, the secret vanishes with it!

{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"baseName": {
"type": "string",
"defaultValue": "mystorage"
}
},
"variables": {
"aciName": "[concat(parameters('baseName'), '-bootstrap')]",
"identityName": "[concat(parameters('baseName'), '-bootstrap')]",
"storageName": "[toLower(parameters('baseName'))]",
"bootstrapRoleAssignmentId": "[guid(concat(resourceGroup().id, 'contributor'))]",
"contributorRoleDefinitionId": "[concat('/subscriptions/', subscription().subscriptionId, '/providers/Microsoft.Authorization/roleDefinitions/', 'b24988ac-6180-42a0-ab88-20f7382dd24c')]"
},
"resources": [
{
"type": "Microsoft.ManagedIdentity/userAssignedIdentities",
"name": "[variables('identityName')]",
"apiVersion": "2015-08-31-preview",
"location": "[resourceGroup().location]"
},
{
"type": "Microsoft.Authorization/roleAssignments",
"apiVersion": "2017-05-01",
"name": "[variables('bootstrapRoleAssignmentId')]",
"dependsOn": [
"[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', variables('identityName'))]"
],
"properties": {
"roleDefinitionId": "[variables('contributorRoleDefinitionId')]",
"principalId": "[reference(resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', variables('identityName')), '2015-08-31-preview').principalId]",
"scope": "[resourceGroup().id]"
}
},
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2018-07-01",
"name": "[variables('storageName')]",
"location": "[resourceGroup().location]",
"kind": "StorageV2",
"sku": {
"name": "Standard_LRS"
},
"properties": {
"encryption": {
"keySource": "Microsoft.Storage",
"services": {
"blob": {
"enabled": true
},
"file": {
"enabled": true
}
}
},
"supportsHttpsTrafficOnly": true
}
},
{
"type": "Microsoft.ContainerInstance/containerGroups",
"apiVersion": "2018-10-01",
"name": "[variables('aciName')]",
"location": "[resourceGroup().location]",
"dependsOn": [
"[resourceId('Microsoft.Storage/storageAccounts', variables('storageName'))]",
"[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', variables('identityName'))]",
"[resourceId('Microsoft.Authorization/roleAssignments', variables('bootstrapRoleAssignmentId'))]"
],
"identity": {
"type": "UserAssigned",
"userAssignedIdentities": {
"[resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', variables('identityName'))]": {}
}
},
"properties": {
"osType": "Linux",
"restartPolicy": "OnFailure",
"containers": [
{
"name": "azure-cli",
"properties": {
"image": "microsoft/azure-cli",
"command": [
"/bin/sh",
"-c",
"[concat('az login --identity -u ', resourceId('Microsoft.ManagedIdentity/userAssignedIdentities', variables('identityName')), ' && az extension add -n storage-preview -y && az storage blob service-properties update --account-name ', variables('storageName'), ' --static-website --404-document 404.html --index-document index.html')]"
],
"resources": {
"requests": {
"cpu": 1,
"memoryInGB": 1
}
}
}
}
]
}
}
],
"outputs": {}
}
MIT License
Copyright (c) 2018 Noel Bundick
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment