title: Use a Linux VM system-assigned managed identity to access Azure Storage description: A tutorial that walks you through the process of using a Linux VM system-assigned managed identity to access Azure Storage. services: active-directory documentationcenter: '' author: daveba manager: mtillman editor: daveba
ms.service: active-directory ms.component: msi ms.devlang: na ms.topic: tutorial ms.tgt_pltfrm: na ms.workload: identity ms.date: 11/20/2017 ms.author: daveba
[!INCLUDE preview-notice]
This tutorial shows you how to use a system-assigned managed identity for a Linux virtual machine (VM) to retrieve storage account access keys. You can use a storage access key as usual when doing storage operations, for example when using the Storage SDK. For this tutorial, we upload and download blobs using Azure CLI. You will learn how to:
[!div class="checklist"]
- Grant your VM access to storage account access keys in Resource Manager
- Get an access token using your VM's identity, and use it to retrieve the storage access keys from Resource Manager
[!INCLUDE msi-tut-prereqs]
If you don't already have one, you will now create a storage account. You can also skip this step and grant your VM system-assigned managed identity access to the keys of an existing storage account.
-
Click the +/Create new service button found on the upper left-hand corner of the Azure portal.
-
Click Storage, then Storage Account, and a new "Create storage account" panel will display.
-
Enter a Name for the storage account, which you will use later.
-
Deployment model and Account kind should be set to "Resource manager" and "General purpose", respectively.
-
Ensure the Subscription and Resource Group match the ones you specified when you created your VM in the previous step.
-
Click Create.
Later we will upload and download a file to the new storage account. Because files require blob storage, we need to create a blob container in which to store the file.
-
Navigate back to your newly created storage account.
-
Click the Containers link in the left, under "Blob service."
-
Click + Container on the top of the page, and a "New container" panel slides out.
-
Give the container a name, select an access level, then click OK. The name you specified will be used later in the tutorial.
Azure Storage does not natively support Azure AD authentication. However, you can use managed identities for Azure resources to retrieve storage account access keys from the Resource Manager, then use a key to access storage. In this step, you grant your VM's system-assigned managed identity access to the keys to your storage account.
-
Navigate back to your newly created storage account.
-
Click the Access control (IAM) link in the left panel.
-
Click + Add role assignment on top of the page to add a new role assignment for your VM
-
Set Role to "Storage Account Key Operator Service Role", on the right side of the page.
-
In the next dropdown, set Assign access to the resource "Virtual Machine".
-
Next, ensure the proper subscription is listed in Subscription dropdown, then set Resource Group to "All resource groups".
-
Finally, under Select choose your Linux Virtual Machine in the dropdown, then click Save.
For the remainder of the tutorial, we will work from the VM we created earlier.
To complete these steps, you will need an SSH client. If you are using Windows, you can use the SSH client in the Windows Subsystem for Linux. If you need assistance configuring your SSH client's keys, see How to Use SSH keys with Windows on Azure, or How to create and use an SSH public and private key pair for Linux VMs in Azure.
-
In the Azure portal, navigate to Virtual Machines, go to your Linux virtual machine, then from the Overview page click Connect at the top. Copy the string to connect to your VM.
-
Connect to your VM using your SSH client.
-
Next, you will be prompted to enter in your Password you added when creating the Linux VM. You should then be successfully signed in.
-
Use CURL to get an access token for Azure Resource Manager.
The CURL request and response for the access token is below:
curl 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fmanagement.azure.com%2F' -H Metadata:true
[!NOTE] In the previous request, the value of the "resource" parameter must be an exact match for what is expected by Azure AD. When using the Azure Resource Manager resource ID, you must include the trailing slash on the URI. In the following response, the access_token element as been shortened for brevity.
{"access_token":"eyJ0eXAiOiJ...", "refresh_token":"", "expires_in":"3599", "expires_on":"1504130527", "not_before":"1504126627", "resource":"https://management.azure.com", "token_type":"Bearer"}
Now use CURL to call Resource Manager using the access token we retrieved in the previous section, to retrieve the storage access key. Once we have the storage access key, we can call storage upload/download operations. Be sure to replace the <SUBSCRIPTION ID>
, <RESOURCE GROUP>
, and <STORAGE ACCOUNT NAME>
parameter values with your own values. Replace the <ACCESS TOKEN>
value with the access token you retrieved earlier:
curl https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>/providers/Microsoft.Storage/storageAccounts/<STORAGE ACCOUNT NAME>/listKeys?api-version=2016-12-01 --request POST -d "" -H "Authorization: Bearer <ACCESS TOKEN>"
Note
The text in the prior URL is case sensitive, so ensure if you are using upper-lowercase for your Resource Groups to reflect it accordingly. Additionally, it’s important to know that this is a POST request not a GET request and ensure you pass a value to capture a length limit with -d that can be NULL.
The CURL response gives you the list of Keys:
{"keys":[{"keyName":"key1","permissions":"Full","value":"iqDPNt..."},{"keyName":"key2","permissions":"Full","value":"U+uI0B..."}]}
Create a sample blob file to upload to your blob storage container. On a Linux VM you can do this with the following command.
echo "This is a test file." > test.txt
Next, authenticate with the CLI az storage
command using the storage access key, and upload the file to the blob container. For this step, you will need to install the latest Azure CLI on your VM, if you haven't already.
az storage blob upload -c <CONTAINER NAME> -n test.txt -f test.txt --account-name <STORAGE ACCOUNT NAME> --account-key <STORAGE ACCOUNT KEY>
Response:
Finished[#############################################################] 100.0000%
{
"etag": "\"0x8D4F9929765C139\"",
"lastModified": "2017-09-12T03:58:56+00:00"
}
Additionally, you can download the file using the Azure CLI and authenticating with the storage access key.
Request:
az storage blob download -c <CONTAINER NAME> -n test.txt -f test-download.txt --account-name <STORAGE ACCOUNT NAME> --account-key <STORAGE ACCOUNT KEY>
Response:
{
"content": null,
"metadata": {},
"name": "test.txt",
"properties": {
"appendBlobCommittedBlockCount": null,
"blobType": "BlockBlob",
"contentLength": 21,
"contentRange": "bytes 0-20/21",
"contentSettings": {
"cacheControl": null,
"contentDisposition": null,
"contentEncoding": null,
"contentLanguage": null,
"contentMd5": "LSghAvpnElYyfUdn7CO8aw==",
"contentType": "text/plain"
},
"copy": {
"completionTime": null,
"id": null,
"progress": null,
"source": null,
"status": null,
"statusDescription": null
},
"etag": "\"0x8D5067F30D0C283\"",
"lastModified": "2017-09-28T14:42:49+00:00",
"lease": {
"duration": null,
"state": "available",
"status": "unlocked"
},
"pageBlobSequenceNumber": null,
"serverEncrypted": false
},
"snapshot": null
}
In this tutorial, you learned how to use a Linux VM system-assigned managed identity to access Azure Storage using an access key. To learn more about Azure Storage access keys see:
[!div class="nextstepaction"] Manage your storage access keys