This article is contributed. See the original author and article here.
In this blog we will look at using service principals with AzCopy and Azure CLI to connect to storage accounts and manage blob data.
An Azure service principal is an identity created for use with applications, hosted services, and automated tools to access Azure resources. This access is restricted by the roles assigned to the service principal, giving you control over which resources can be accessed and at which level. For security reasons, it’s always recommended to use service principals with automated tools rather than a user identity.
1. Creating a service principal
To create a service principal we will use Cloud Shell on Azure Portal using the az ad sp create-for-rbac command. The below command will provide an Azure Storage data access role to assign to the new service principal. Additionally, provide the scope for the role assignment. For more information about the built-in roles provided for Azure Storage, see Azure built-in roles. Note: Save the output of the create SPN command.
Assigning roles to service principal
Once the Service Principal is created, we also need to grant ‘Reader’ role on the storage account to the service principal. This will grant the SPN read access to storage resource at subscription level. Please refer to our documentation on assigning roles for access to blob. https://docs.microsoft.com/en-us/azure/storage/common/storage-auth-aad-rbac-portal
2. Using service principal with AzCopy
AzCopy is a command-line tool that moves data into and out of Azure Storage. To learn more about AzCopy please refer the official documentation.
Login as service principal
Next we will login as the service principal in AzCopy using the azcopy login command. The values for options application-id, tenant-id and AZ_COPY_CLIENT_SECRET, will be available on step 1 after creating the service principal.
Performing copy operations
Once sucessfully logged in, we can upload and download files using OAuth authentication of the service principal with azcopy copy command.
3. Using service principal with Azure CLI
The Azure command-line interface (Azure CLI) is a set of commands used to create and manage Azure resources. The Azure CLI is available across Azure services and is designed to get you working quickly with Azure, with an emphasis on automation. To learn more about Azure CLI and how to install Azure CLI please refer the official documentation.
Login as service principal in Azure CLI
Once we have installed Azure CLI we can use the az login command to login with our service principal.
Performing Azure CLI storage & blob operations
We can now perform various operations/commands on the storage accounts that the service principal has access to.
List storage accounts
Brought to you by Dr. Ware, Microsoft Office 365 Silver Partner, Charleston SC.