|
1 | 1 | ---
|
2 |
| -title: Run Azure CLI or PowerShell commands under an Azure AD identity to access Azure Storage (Preview) | Microsoft Docs |
| 2 | +title: Run Azure CLI or PowerShell commands under an Azure AD identity to access Azure Storage | Microsoft Docs |
3 | 3 | description: Azure CLI and PowerShell support logging in with an Azure AD identity to run commands on Azure Storage containers and queues and their data. An access token is provided for the session and used to authorize calling operations. Permissions depend on the role assigned to the Azure AD identity.
|
4 | 4 | services: storage
|
5 | 5 | author: tamram
|
6 | 6 |
|
7 | 7 | ms.service: storage
|
8 | 8 | ms.topic: article
|
9 |
| -ms.date: 10/15/2018 |
| 9 | +ms.date: 03/06/2019 |
10 | 10 | ms.author: tamram
|
11 | 11 | ms.subservice: common
|
12 | 12 | ---
|
13 | 13 |
|
14 |
| -# Use an Azure AD identity to access Azure Storage with CLI or PowerShell (Preview) |
| 14 | +# Use an Azure AD identity to access Azure Storage with CLI or PowerShell |
15 | 15 |
|
16 |
| -Azure Storage provides preview extensions for Azure CLI and PowerShell that enable you to log in and run scripting commands under an Azure Active Directory (Azure AD) identity. The Azure AD identity can be a user, group, or application service principal, or it can be a [managed identity for Azure resources](../../active-directory/managed-identities-azure-resources/overview.md). You can assign permissions to access storage resources to the Azure AD identity via role-based access control (RBAC). For more information about RBAC roles in Azure Storage, see [Manage access rights to Azure Storage data with RBAC (Preview)](storage-auth-aad-rbac.md). |
| 16 | +Azure Storage provides extensions for Azure CLI and PowerShell that enable you to log in and run scripting commands under an Azure Active Directory (Azure AD) identity. The Azure AD identity can be a user, group, or application service principal, or it can be a [managed identity for Azure resources](../../active-directory/managed-identities-azure-resources/overview.md). You can assign permissions to access storage resources to the Azure AD identity via role-based access control (RBAC). For more information about RBAC roles in Azure Storage, see [Manage access rights to Azure Storage data with RBAC (Preview)](storage-auth-aad-rbac.md). |
17 | 17 |
|
18 | 18 | When you log in to Azure CLI or PowerShell with an Azure AD identity, an access token is returned for accessing Azure Storage under that identity. That token is then automatically used by CLI or PowerShell to authorize operations against Azure Storage. For supported operations, you no longer need to pass an account key or SAS token with the command.
|
19 | 19 |
|
20 |
| -[!INCLUDE [storage-auth-aad-note-include](../../../includes/storage-auth-aad-note-include.md)] |
21 |
| - |
22 | 20 | ## Supported operations
|
23 | 21 |
|
24 |
| -The preview extensions are supported for operations on containers and queues. Which operations you may call depends on the permissions granted to the Azure AD identity with which you log in to Azure CLI or PowerShell. Permissions to Azure Storage containers or queues are assigned via role-based access control (RBAC). For example, if a Data Reader role is assigned to the identity, then you can run scripting commands that read data from a container or queue. If a Data Contributor role is assigned to the identity, then you can run scripting commands that read, write, or delete a container or queue or the data they contain. |
| 22 | +The extensions are supported for operations on containers and queues. Which operations you may call depends on the permissions granted to the Azure AD identity with which you log in to Azure CLI or PowerShell. Permissions to Azure Storage containers or queues are assigned via role-based access control (RBAC). For example, if a Data Reader role is assigned to the identity, then you can run scripting commands that read data from a container or queue. If a Data Contributor role is assigned to the identity, then you can run scripting commands that read, write, or delete a container or queue or the data they contain. |
25 | 23 |
|
26 | 24 | For details about the permissions required for each Azure Storage operation on a container or queue, see [Permissions for calling REST operations](https://docs.microsoft.com/rest/api/storageservices/authenticate-with-azure-active-directory#permissions-for-calling-rest-operations).
|
27 | 25 |
|
28 |
| -## Call CLI commands with an Azure AD identity |
| 26 | +## Call CLI commands using Azure AD credentials |
| 27 | + |
| 28 | +Azure CLI supports the `--auth-mode` parameter for data operations against Azure Storage: |
| 29 | + |
| 30 | +- Set the `--auth-mode` parameter to `login` to sign in using an Azure AD security principal. |
| 31 | +- Set the `--auth-mode` parameter to the legacy `key` value to attempt to query for an account key if no authentication parameters for the account are provided. |
| 32 | + |
| 33 | +The following example shows how to create a container in a new storage account from Azure CLI using your Azure AD credentials. Remember to replace placeholder values in angle brackets with your own values: |
29 | 34 |
|
30 |
| -To install the preview extension for Azure CLI: |
| 35 | +1. Make sure that you have installed Azure CLI version 2.0.46 or later. Run `az --version` to check your installed version. |
31 | 36 |
|
32 |
| -1. Make sure that you have installed Azure CLI version 2.0.32 or later. Run `az --version` to check your installed version. |
33 |
| -2. Run the following command to install the preview extension: |
| 37 | +1. Run `az login` and authenticate in the browser window: |
34 | 38 |
|
35 | 39 | ```azurecli
|
36 |
| - az extension add -n storage-preview |
| 40 | + az login |
37 | 41 | ```
|
| 42 | + |
| 43 | +1. Specify your desired subscription. Create a resource group using [az group create](https://docs.microsoft.com/cli/azure/group?view=azure-cli-latest#az-group-create). Create a storage account within that resource group using [az storage account create](https://docs.microsoft.com/cli/azure/storage/account?view=azure-cli-latest#az-storage-account-create): |
38 | 44 |
|
39 |
| -The preview extension adds a new `--auth-mode` parameter to supported commands: |
40 |
| -
|
41 |
| -- Set the `--auth-mode` parameter to `login` to sign in using an Azure AD identity. |
42 |
| -- Set the `--auth-mode` parameter to the legacy `key` value to attempt to query for an account key if no authentication parameters for the account are provided. |
| 45 | + ```azurecli |
| 46 | + az account set --subscription <subscription-id> |
| 47 | +
|
| 48 | + az group create \ |
| 49 | + --name sample-resource-group-cli \ |
| 50 | + --location eastus |
| 51 | +
|
| 52 | + az storage account create \ |
| 53 | + --name <storage-account> \ |
| 54 | + --resource-group sample-resource-group-cli \ |
| 55 | + --location eastus \ |
| 56 | + --sku Standard_LRS \ |
| 57 | + --encryption-services blob |
| 58 | + ``` |
| 59 | + |
| 60 | +1. Before you create the container, assign the [Storage Blob Data Contributor (preview)](../../role-based-access-control/built-in-roles.md#storage-blob-data-contributor-preview) role to yourself. Even though you are the account owner, you need explicit permissions to perform data operations against the storage account. For more information about assigning RBAC roles, see [Grant access to Azure containers and queues with RBAC in the Azure portal (preview)](storage-auth-aad-rbac.md). |
43 | 61 |
|
44 |
| -For example, to download a blob in Azure CLI using an Azure AD identity, first run `az login`, then call the command with `--auth-mode` set to `login`: |
| 62 | + > [!IMPORTANT] |
| 63 | + > During the preview of Azure AD support for blobs and queues, RBAC role assignments may take up to 5 minutes to propagate. |
| 64 | + |
| 65 | +1. Call the [az storage container create](https://docs.microsoft.com/cli/azure/storage/container?view=azure-cli-latest#az-storage-container-create) command with the `--auth-mode` parameter set to `login` to create the container using your Azure AD credentials: |
45 | 66 |
|
46 |
| -```azurecli |
47 |
| -az login |
48 |
| -az storage blob download --account-name storagesamples --container sample-container --name myblob.txt --file myfile.txt --auth-mode login |
49 |
| -``` |
| 67 | + ```azurecli |
| 68 | + az storage container create \ |
| 69 | + --account-name <storage-account> \ |
| 70 | + --name sample-container \ |
| 71 | + --auth-mode login |
| 72 | + ``` |
50 | 73 |
|
51 |
| -The environment variable associated with the `--auth-mode` parameter is `AZURE_STORAGE_AUTH_MODE`. |
| 74 | +The environment variable associated with the `--auth-mode` parameter is `AZURE_STORAGE_AUTH_MODE`. You can specify the appropriate value in the environment variable to avoid including it on every call to an Azure Storage data operation. |
52 | 75 |
|
53 |
| -## Call PowerShell commands with an Azure AD identity |
| 76 | +## Call PowerShell commands using Azure AD credentials |
54 | 77 |
|
55 | 78 | [!INCLUDE [updated-for-az](../../../includes/updated-for-az.md)]
|
56 | 79 |
|
57 |
| -To use Azure PowerShell to sign in with an Azure AD identity: |
| 80 | +To use Azure PowerShell to sign in and run subsequent operations against Azure Storage using Azure AD credentials, create a storage context to reference the storage account, and including the `-UseConnectedAccount` parameter. |
58 | 81 |
|
59 |
| -1. Uninstall any previous installations of Azure PowerShell: |
| 82 | +The following example shows how to create a container in a new storage account from Azure PowerShell using your Azure AD credentials. Remember to replace placeholder values in angle brackets with your own values: |
60 | 83 |
|
61 |
| - - Remove any previous installations of Azure PowerShell from Windows using the **Apps & features** setting under **Settings**. |
62 |
| - - Remove all **Azure*** modules from `%Program Files%\WindowsPowerShell\Modules`. |
| 84 | +1. Sign in to your Azure subscription with the `Connect-AzAccount` command and follow the on-screen directions to enter your Azure AD credentials: |
63 | 85 |
|
64 |
| -1. Make sure that you have the latest version of PowerShellGet installed. Open a Windows PowerShell window, and run the following command to install the latest version: |
65 |
| - |
66 | 86 | ```powershell
|
67 |
| - Install-Module PowerShellGet –Repository PSGallery –Force |
| 87 | + Connect-AzAccount |
68 | 88 | ```
|
69 |
| -1. Close and reopen the PowerShell window after installing PowerShellGet. |
| 89 | + |
| 90 | +1. Create an Azure resource group by calling [New-AzResourceGroup](/powershell/module/az.resources/new-azresourcegroup). |
70 | 91 |
|
71 |
| -1. Install the latest version of Azure PowerShell: |
| 92 | + ```powershell |
| 93 | + $resourceGroup = "sample-resource-group-ps" |
| 94 | + $location = "eastus" |
| 95 | + New-AzResourceGroup -Name $resourceGroup -Location $location |
| 96 | + ``` |
| 97 | +
|
| 98 | +1. Create a storage account by calling [New-AzStorageAccount](/powershell/module/az.storage/new-azstorageaccount). |
| 99 | +
|
| 100 | + ```powershell |
| 101 | + $storageAccount = New-AzStorageAccount -ResourceGroupName $resourceGroup ` |
| 102 | + -Name "<storage-account>" ` |
| 103 | + -SkuName Standard_LRS ` |
| 104 | + -Location $location ` |
| 105 | + ``` |
| 106 | +
|
| 107 | +1. Get the storage account context that specifies the new storage account by calling [New-AzStorageContext](/powershell/module/az.storage/new-azstoragecontext). When acting on a storage account, you can reference the context instead of repeatedly passing in the credentials. Include the `-UseConnectedAccount` parameter to call any subsequent data operations using your Azure AD credentials: |
72 | 108 |
|
73 | 109 | ```powershell
|
74 |
| - Install-Module Az –Repository PSGallery –AllowClobber |
| 110 | + $ctx = New-AzStorageContext -StorageAccountName "<storage-account>" -UseConnectedAccount |
75 | 111 | ```
|
76 | 112 |
|
77 |
| -1. Install an Azure Storage preview module that supports Azure AD: |
78 |
| - |
79 |
| - ```powershell |
80 |
| - Install-Module Az.Storage -Repository PSGallery -AllowPrerelease -AllowClobber -Force |
81 |
| - ``` |
82 |
| -1. Close and reopen the PowerShell window. |
83 |
| -1. Call the [New-AzStorageContext](https://docs.microsoft.com/powershell/module/az.storage/new-azstoragecontext) cmdlet to create a context, and include the `-UseConnectedAccount` parameter. |
84 |
| -1. To call a cmdlet with an Azure AD identity, pass the newly created context to the cmdlet. |
| 113 | +1. Before you create the container, assign the [Storage Blob Data Contributor (preview)](../../role-based-access-control/built-in-roles.md#storage-blob-data-contributor-preview) role to yourself. Even though you are the account owner, you need explicit permissions to perform data operations against the storage account. For more information about assigning RBAC roles, see [Grant access to Azure containers and queues with RBAC in the Azure portal (preview)](storage-auth-aad-rbac.md). |
| 114 | +
|
| 115 | + > [!IMPORTANT] |
| 116 | + > During the preview of Azure AD support for blobs and queues, RBAC role assignments may take up to 5 minutes to propagate. |
85 | 117 |
|
86 |
| -The following example shows how to list the blobs in a container from Azure PowerShell using an Azure AD identity. Be sure to replace the placeholder account and container names with your own values: |
| 118 | +1. Create a container by calling [New-AzStorageContainer](/powershell/module/az.storage/new-azstoragecontainer). Because this call uses the context created in the previous steps, the container is created using your Azure AD credentials. |
87 | 119 |
|
88 |
| -```powershell |
89 |
| -$ctx = New-AzStorageContext -StorageAccountName storagesamples -UseConnectedAccount |
90 |
| -Get-AzStorageBlob -Container sample-container -Context $ctx |
91 |
| -``` |
| 120 | + ```powershell |
| 121 | + $containerName = "sample-container" |
| 122 | + New-AzStorageContainer -Name $containerName -Context $ctx |
| 123 | + ``` |
92 | 124 |
|
93 | 125 | ## Next steps
|
94 | 126 |
|
95 | 127 | - To learn more about RBAC roles for Azure storage, see [Manage access rights to storage data with RBAC (Preview)](storage-auth-aad-rbac.md).
|
96 | 128 | - To learn about using managed identities for Azure resources with Azure Storage, see [Authenticate access to blobs and queues with Azure managed identities for Azure Resources (Preview)](storage-auth-aad-msi.md).
|
97 |
| -- To learn how to authorize access to containers and queues from within your storage applications, see [Use Azure AD with storage applications](storage-auth-aad-app.md). |
98 |
| -- For additional information about Azure AD integration for Azure Blobs and Queues, see the Azure Storage team blog post, [Announcing the Preview of Azure AD Authentication for Azure Storage](https://azure.microsoft.com/blog/announcing-the-preview-of-aad-authentication-for-storage/). |
| 129 | +- To learn how to authorize access to containers and queues from within your storage applications, see [Use Azure AD with storage applications](storage-auth-aad-app.md). |
0 commit comments