File accessed using keys and managed identities - Azure Files and Azure Blobs
Tags: azure, fileaccess, identity, keys
Published: 2024-05-26 02:28:15

### Create the storage account and managed identity
**Provide a storage account for the web app.**
_In the portal, search for and select Storage accounts.
Select + Create.
For Resource group select Create new. Give your resource group a name and select OK to save your changes.
Provide a Storage account name. Ensure the name is unique and meets the naming requirements._
Follow our previous [tutorial](https://dev.to/olawaleoloye/file-shares-with-limited-access-corporate-virtual-networks-azure-files-and-azure-blobs-4p3p) for the above 
**Select Review + Create.**
![Encryption](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qcz3uxvzfeq36iyvsw4a.png)

**Wait for the resource to deploy.**

_**Provide a managed identity for the web app to use.**_

**Search for and select Managed identities.
Select Create.
Select your resource group.**
![Create ManagedID](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n9kbogp0w32apguj6t8b.png)

**Give your managed identity a name.
Select Review and create, and then Create.**
![Review and Create](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ko12yy0oqy6r372gwxsc.png)

![Create](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5a5cgrwfxvrz5dacy91j.png)

_Assign the correct permissions to the managed identity. The identity only needs to read and list containers and blobs._ 

![ManagedID](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i661blt9ij1m1uez851j.png)

![ManagedID Overview](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/leh2x4ofnvnxuvt7lq2h.png)

**Search for and select your storage account.
Select the Access Control (IAM) blade.
Select Add role assignment (center of the page).**

![Storage Account](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cx5f15e3g1osz9j1pt8n.png)
**On the Job functions roles page, search for and select the Storage Blob Data Reader role.**

![Storage Blob](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4wwyujq758byf8nq111a.png)


![Storage Blod Selected](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bg63lo5qt7bkrjd05pjo.png)


**On the Job functions roles page, search for and select the Storage Blob Data Reader role.
On the Members page, select Managed identity.
Select Select members, in the Managed identity drop-down select User-assigned managed identity.
Select the managed identity you created in the previous step.**
![Create Link](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yhvhhvaltd74s83i31ey.png)

**Click Select and then Review + assign the role.
Select Review + assign a second time to add the role assignment.**
![Review + assign](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/49qs142j3if4sz2jh85c.png)
_Your storage account can now be accessed by a managed identity with the Storage Data Blob Reader permissions._


### Secure access to the storage account with a key vault and key

**To create the key vault and key needed for this part of the lab, your user account must have Key Vault Administrator permissions.**
**In the portal, search for and select Resource groups.
Select your resource group, and then the Access Control (IAM) blade.
Select Add role assignment (center of the page).**


![Add role assignment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bjtxxn7z0vgxuair2qya.png)

**On the Job functions roles page, search for and select the Key Vault Administrator role.**

![Search and select](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/43kfq7k656ydsx6n2ad0.png)

**On the Job functions roles page, search for and select the Key Vault Administrator role.
On the Members page, select User, group, or service principal.
Select Select members.
Search for and select your user account. Your user account is shown in the top right of the portal.
Click Select and then Review + assign.**
![Select](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ers36x1ftu98mzpwunoa.png)

![Review](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ms5598bqyy4oxocgqf1n.png)

**Select Review + assign a second time to add the role assignment.
You are now ready to continue with the lab.**
_**Create a key vault to store the access keys.**_


**In the portal, search for and select Key vaults.**

![search for and select Key vaults](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gumjtub8gi74mhyb2odu.png)


**Select Create.**

![Select Create.](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jkxvdx8syix9s1wge09t.png)


**Select your resource group.**

**Provide the name for the key vault. The name must be unique.**

![key vault](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7uaqglbiiplzxncb6uyz.png)


**Ensure on the Access configuration tab that Azure role-based access control (recommended) is selected.**
![Access configuration tab](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8hfiv17nxelobokt13jw.png)
**Select Review + create.**

![Review + Create](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/usox74jf2edlkeev0vq8.png)

**Wait for the validation checks to complete and then select Create.**

![Create](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1118fsj7lk9g6c1cnpr3.png)


**After the deployment, select Go to resource.**

**On the Overview blade ensure both Soft-delete and Purge protection are enabled.**

![Soft-delete and Purge protection](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8qkbj5stm9qhkbxspmu2.png)


_**Create a customer-managed key in the key vault.**_


**In your key vault, in the Objects section, select the Keys blade.**
![Keys blade](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s13u5tth6ztu9ynmmvl5.png)

**Select Generate/Import and Name the key.
Take the defaults for the rest of the parameters, and Create the key**
![Generate/Import](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4aqi1vnhi84ni9qk972k.png)

### Configure the storage account to use the customer managed key in the key vault
_**Before you can complete the next steps, you must assign the Key Vault Crypto Service Encryption User role to the managed identity.**_ 


**In the portal, search for and select Resource groups.
Select your resource group, and then the Access Control (IAM) blade.
Select Add role assignment (center of the page).**
![role assignment](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oywnti3d137tcwkeqr4t.png)
**On the Job functions roles page, search for and select the Key Vault Crypto Service Encryption User role.**

![Key Vault Crypto Service Encryption User](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mkk8a2qthvb7uu7x1qu.png)

**On the Members page, select Managed identity.**

**Select Select members, in the Managed identity drop-down select User-assigned managed identity.
Select your managed identity.**
![Managed identity](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uig2jp7flgjd72b3461u.png)
**Click Select and then Review + assign.
Select Review + assign a second time to add the role assignment.**
![Review + assign](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wqy3p9zd0t29bftqpbzq.png)


**Configure the storage account to use the customer managed key in your key vault. **


**In the Security + networking section, select the Encryption blade.**

**Select Customer-managed keys.
Select a key vault and key. Select your key vault and key.**


![key vault and key](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g717vmll5mmhh4sm9y8g.png)

**Select to confirm your choices.
Ensure the Identity type is User-assigned.
Select an identity.**

![identity](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kphbhl4vindcdtdvzi17.png)

**Select your managed identity then select Add.
Save your changes.**
![managed identity](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sexrobi3m63aclw20yk0.png)
_If you receive an error that your identity does not have the correct permissions, wait a minute and try again._

### Configure an time-based retention policy and an encryption scope.

**The developers require a storage container where files can’t be modified, even by the administrator**

**Navigate to your storage account.
In the Data storage section, select the Containers blade.**
![Containers](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pg8c6ailpv1zuzhwpme.png)
**Create a container called hold. Take the defaults. Be sure to Create the container.**
![hold](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6dscgzfjkn7l4izl7ugg.png)


**Upload a file to the container.**
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3ndaoa8o9o3u32yupy03.png)

**In the Settings section, select the Access policy blade.
In the Immutable blob storage section, select + Add policy.
For the Policy type, select time-based retention.
Set the Retention period to 5 days.
Be sure to Save your changes.**

![Retention period](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1fxuzcuvyhqk6koaonpq.png)

**Try to delete the file in the container.
Verify you are notified failed to delete blobs due to policy.
The developers require an encryption scope that enables infrastructure encryption.**

![delete blobs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0wxfa2h6i7tkblxof6it.png)

**Navigate back to your storage account.
In the Security + networking blade, select Encryption.
In the Encryption scopes tab, select Add.
Give your encryption scope a name.
The Encryption type is Microsoft-managed key.
Set Infrastructure encryption to Enable.
Create the encryption scope.**

![encryption scope](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/99athr9wjlhk58k6kk86.png)