Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions 201-vm-msi-linux/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Deploy A Linux VM with MSI

This shows how to use Managed Service Idenity from within a Linux VM to access azure resources, in particular it shows how to:

- Create a VM with a system assigned idenity
- Install the MSI extension on the VM to allow OAuth tokens to be issued for Azure resources
- Assign RBAC permissions to the Managed Identity
- Run a script that uses azure cli 2 to login using the MSI

This template creates a new Linux VM with a MSI and deploys the MSI extension to the VM. The MSI associated with the VM is given owner permission on a storage account that is created by the template. A shell script is then run on the VM using the customscript extension , this script installs Docker and then creates a container with the Azure CLI 2, it runs a script in this container that logs in to the CLI using the token issuing endpoint installed in the VM by the MSI extension. It then uses the cli to retrieve the keys for the storage account and writes a blob with a name matching the VM name into the storage account.

<a href="https://portal.azure.com/#create/Microsoft.Template/uri/https%3A%2F%2Fraw.githubusercontent.com%2FAzure%2Fazure-quickstart-templates%2Fmaster%2F201-vm-msi-linux%2Fazuredeploy.json" target="_blank">
<img src="http://azuredeploy.net/deploybutton.png"/>
</a>
330 changes: 330 additions & 0 deletions 201-vm-msi-linux/azuredeploy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,330 @@
{
"$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"adminUsername": {
"type": "string",
"metadata": {
"description": "Username for the Virtual Machine."
}
},
"authenticationType": {
"type": "string",
"defaultValue": "password",
"allowedValues": [
"password",
"sshPublicKey"
],
"metadata": {
"description": "Authentication type"
}
},
"adminPasswordorSSHKey": {
"type": "securestring",
"metadata": {
"description": "OS Admin password or SSH Key depending on value of authentication type"
}
},
"dnsLabelPrefix": {
"type": "string",
"metadata": {
"description": "Unique DNS Name for the Public IP used to access the Virtual Machine."
},
"defaultValue": "[concat('msi',uniquestring(resourceGroup().id,deployment().name))]"
},
"location": {
"type": "string",
"metadata": {
"description": "The Location For the resources"
},
"defaultValue": "[resourceGroup().location]"
},
"vmSize": {
"type": "string",
"metadata": {
"description": "The size of the VM to create"
},
"defaultValue": "Standard_DS1_V2"
},
"azureCLI2DockerImage": {
"type": "string",
"metadata": {
"description": "The Docker image to rin the azure CLI from"
},
"defaultValue": "azuresdk/azure-cli-python:latest"
},
"_artifactsLocation": {
"type": "string",
"metadata": {
"description": "The base URI where artifacts required by this template are located. When the template is deployed using the accompanying scripts, a private location in the subscription will be used and this value will be automatically generated."
},
"defaultValue": "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/201-vm-msi-linux"
},
"_artifactsLocationSasToken": {
"type": "securestring",
"metadata": {
"description": "The sasToken required to access _artifactsLocation."
},
"defaultValue": ""
},
"publisher": {
"type": "string",
"metadata": {
"description": "The VM Publisher"
},
"defaultValue": "Canonical"
},
"offer": {
"type": "string",
"metadata": {
"description": "The VM Offer"
},
"defaultValue": "UbuntuServer"
},
"SKU": {
"type": "string",
"metadata": {
"description": "The VM SKU"
},
"defaultValue": "16.04-LTS"
}
},
"variables": {
"storageAccountName": "[take(concat(uniquestring(resourceGroup().id), parameters('dnsLabelPrefix')),24)]",
"nicName": "[concat('nic',uniquestring(resourceGroup().id,deployment().name))]",
"networkSecurityGroupName": "[concat('nsg',uniquestring(resourceGroup().id,deployment().name))]",
"addressPrefix": "10.0.0.0/16",
"subnetName": "Subnet",
"subnetPrefix": "10.0.0.0/24",
"publicIPAddressName": "[concat('pip',uniquestring(resourceGroup().id,deployment().name))]",
"vmName": "[concat('vm',uniquestring(resourceGroup().id,deployment().name))]",
"virtualNetworkName": "[concat('vnet',uniquestring(resourceGroup().id,deployment().name))]",
"subnetRef": "[resourceId('Microsoft.Network/virtualNetworks/subnets', variables('virtualNetworkName'), variables('subnetName'))]",
"linuxConfigurationChosen": "[concat('linuxConfiguration', parameters('authenticationType'))]",
"linuxConfigurationsshPublicKey": {
"disablePasswordAuthentication": true,
"ssh": {
"publicKeys": [
{
"path": "[concat('/home/',parameters('adminUsername'),'/.ssh/authorized_keys')]",
"keyData": "[parameters('adminPasswordorSSHKey')]"
}
]
}
},
"linuxConfigurationpassword": {
"disablePasswordAuthentication": false
},
"adminPasswordChosen": "[concat('adminPassword', parameters('authenticationType'))]",
"adminPasswordsshPublicKey": "",
"adminPasswordpassword": "[parameters('adminPasswordorSSHKey')]",
"containerName": "msi",
"createVMUrl": "[concat(parameters('_artifactsLocation'), '/nestedtemplates/createVM.json', parameters('_artifactsLocationSasToken'))]",
"createRBACUrl": "[concat(parameters('_artifactsLocation'), '/nestedtemplates/setUpRBAC.json', parameters('_artifactsLocationSasToken'))]"
},
"resources": [
{
"type": "Microsoft.Storage/storageAccounts",
"name": "[variables('storageAccountName')]",
"apiVersion": "2016-12-01",
"location": "[parameters('Location')]",
"sku": {
"name": "Standard_LRS"
},
"kind": "Storage",
"properties": {}
},
{
"apiVersion": "2016-09-01",
"type": "Microsoft.Network/publicIPAddresses",
"name": "[variables('publicIPAddressName')]",
"location": "[parameters('Location')]",
"properties": {
"publicIPAllocationMethod": "Dynamic",
"dnsSettings": {
"domainNameLabel": "[parameters('dnsLabelPrefix')]"
}
}
},
{
"apiVersion": "2016-09-01",
"type": "Microsoft.Network/virtualNetworks",
"name": "[variables('virtualNetworkName')]",
"location": "[parameters('Location')]",
"properties": {
"addressSpace": {
"addressPrefixes": [
"[variables('addressPrefix')]"
]
},
"subnets": [
{
"name": "[variables('subnetName')]",
"properties": {
"addressPrefix": "[variables('subnetPrefix')]"
}
}
]
}
},
{
"name": "[variables('networkSecurityGroupName')]",
"type": "Microsoft.Network/networkSecurityGroups",
"apiVersion": "2016-09-01",
"location": "[parameters('location')]",
"properties": {
"securityRules": [
{
"name": "default-allow-ssh",
"properties": {
"priority": 1000,
"sourceAddressPrefix": "*",
"protocol": "TCP",
"destinationPortRange": "22",
"access": "Allow",
"direction": "Inbound",
"sourcePortRange": "*",
"destinationAddressPrefix": "*"
}
}
]
}
},
{
"apiVersion": "2016-09-01",
"type": "Microsoft.Network/networkInterfaces",
"name": "[variables('nicName')]",
"location": "[parameters('Location')]",
"dependsOn": [
"[resourceId('Microsoft.Network/publicIPAddresses/', variables('publicIPAddressName'))]",
"[resourceId('Microsoft.Network/virtualNetworks/', variables('virtualNetworkName'))]"
],
"properties": {
"ipConfigurations": [
{
"name": "ipconfig1",
"properties": {
"privateIPAllocationMethod": "Dynamic",
"publicIPAddress": {
"id": "[resourceId('Microsoft.Network/publicIPAddresses',variables('publicIPAddressName'))]"
},
"subnet": {
"id": "[variables('subnetRef')]"
}
}
}
],
"networkSecurityGroup": {
"id": "[resourceId('Microsoft.Network/networkSecurityGroups', variables('networkSecurityGroupName'))]"
}
}
},
{
"name": "creatingVM",
"type": "Microsoft.Resources/deployments",
"apiVersion": "2016-09-01",
"dependsOn": [
"[resourceId('Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
"[resourceId('Microsoft.Network/networkInterfaces/', variables('nicName'))]"
],
"properties": {
"mode": "Incremental",
"templateLink": {
"uri": "[variables('createVMUrl')]"
},
"parameters": {
"adminUsername": {
"value": "[parameters('adminUsername')]"
},
"adminPassword": {
"value": "[variables(variables('adminPasswordChosen'))]"
},
"location": {
"value": "[parameters('location')]"
},
"vmSize": {
"value": "[parameters('vmSize')]"
},
"vmName": {
"value": "[variables('VMName')]"
},
"nicName": {
"value": "[variables('nicName')]"
},
"storageAccountName": {
"value": "[variables('storageAccountName')]"
},
"linuxConfiguration": {
"value": "[variables(variables('linuxConfigurationChosen'))]"
},
"publisher": {
"value": "[parameters('publisher')]"
},
"offer": {
"value": "[parameters('offer')]"
},
"SKU": {
"value": "[parameters('SKU')]"
}
}
}
},
{
"name": "creatingRBAC",
"type": "Microsoft.Resources/deployments",
"apiVersion": "2016-09-01",
"dependsOn": [
"Microsoft.Resources/deployments/creatingVM"
],
"properties": {
"mode": "Incremental",
"templateLink": {
"uri": "[variables('createRBACUrl')]",
"contentVersion": "1.0.0.0"
},
"parameters": {
"principalId": {
"value": "[reference('Microsoft.Resources/deployments/creatingVM', '2016-09-01').outputs.principalId.value]"
},
"storageAccountName": {
"value": "[variables('storageAccountName')]"
}
}
}
},
{
"name": "[concat(variables('vmName'),'/customscriptextension')]",
"type": "Microsoft.Compute/virtualMachines/extensions",
"apiVersion": "2017-03-30",
"location": "[parameters('Location')]",
"dependsOn": [
"Microsoft.Resources/deployments/creatingRBAC"
],
"properties": {
"publisher": "Microsoft.Azure.Extensions",
"type": "CustomScript",
"typeHandlerVersion": "2.0",
"autoUpgradeMinorVersion": true,
"settings": {
"fileUris": [
"[concat(parameters('_artifactsLocation'), '/scripts/writeblob.sh', parameters('_artifactsLocationSasToken'))]",
"[concat(parameters('_artifactsLocation'), '/scripts/install-and-run-cli-2.sh', parameters('_artifactsLocationSasToken'))]"
]
},
"protectedSettings": {
"commandToExecute": "[concat('./install-and-run-cli-2.sh -i \"', parameters('azureCLI2DockerImage'),'\" -a \"', variables('storageAccountName'), '\" -c \"', variables('containerName'), '\" -r \"', resourceGroup().Name,'\"')]"
}
}
}
],
"outputs": {
"hostname": {
"type": "string",
"value": "[reference(variables('publicIPAddressName')).dnsSettings.fqdn]"
},
"principalId": {
"type": "string",
"value": "[reference('Microsoft.Resources/deployments/creatingVM', '2016-09-01').outputs.principalId.value]"
}
}
}
15 changes: 15 additions & 0 deletions 201-vm-msi-linux/azuredeploy.parameters.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"adminUsername": {
"value": "GEN-UNIQUE"
},
"authenticationType": {
"value": "sshPublicKey"
},
"adminPasswordorSSHKey" : {
"value": "GEN-SSH-PUB-KEY"
}
}
}
Loading