Access to storage: keys, SAS, stored access policies, data roles and firewalls
There are four ways into a storage account's data, and an on-call engineer needs to know all four, because every leak and every 403 involves one of them: the account keys, a shared access signature (SAS), Microsoft Entra ID with a data role, and (for some cases) anonymous public access. On top of all four sits the network: the storage firewall decides which addresses may try at all. This lesson is about granting exactly the access a job needs, and taking it back.
Need to know:
| Way in | Who holds it | Scope | How you revoke it |
|---|---|---|---|
| Account key (key1, key2) | whoever has listKeys (Contributor and up) | everything in the account, forever | rotate the key (breaks every client using it) |
| SAS signed with a key (account or service SAS) | anyone with the URL | what it says: services, containers, permissions, expiry, IPs | expiry, or rotate the signing key; a stored access policy makes it revocable on its own |
| User delegation SAS | anyone with the URL | max 7 days, and never more than the signer's data role | revoke the user delegation keys, or remove the signer's role |
| Entra ID + data role | a user, group or managed identity | role at account, container scope | remove the role assignment |
Control-plane roles (Owner, Contributor) do not read data by themselves. They read it by fetching the keys. The data roles are separate: Storage Blob Data Reader / Contributor / Owner, Storage File Data SMB Share Reader / Contributor, Storage Queue Data .... For people and apps, Entra ID plus a data role is the recommended way. A SAS is for handing limited, time-boxed access to someone outside your directory.
Account keys
$ az group create -n rg-try-data -l westeurope -o none
$ az storage account create -n sttrydata05 -g rg-try-data -o none
$ az storage account keys list -n sttrydata05 -g rg-try-data --query "[].{key:keyName, permissions:permissions}" -o table
Key Permissions
---- -----------
key1 FULL
key2 FULL
$ KEY=$(az storage account keys list -n sttrydata05 -g rg-try-data --query "[0].value" -o tsv)
$ az storage container create --account-name sttrydata05 -n invoices --account-key "$KEY" -o none
$ echo inv-001 > a.csv
$ az storage blob upload --account-name sttrydata05 -c invoices -n 2026/09/a.csv -f a.csv --account-key "$KEY" -o none 2>/dev/null
There are two keys so you can rotate without downtime: move the clients to key2, renew key1, move them back later, renew key2. Keep keys in Key Vault, rotate them on a schedule (Key Vault can remind you or rotate them), and where possible turn them off: --allow-shared-key-access false makes the account accept only Entra ID (and user delegation SAS). That is the strongest setting, and it breaks every tool that still uses keys, so take inventory first.
Shared access signatures
A SAS is a set of query parameters signed with a key: what it grants (sp permissions, sr resource, ss/srt for account SAS), until when (se), optionally from when (st), from which IPs (sip) and over which protocol (spr), plus the signature (sig). Anyone holding the URL has that access, no login needed.
A service SAS for one blob, read-only, HTTPS only, for 12 hours:
$ SAS=$(az storage blob generate-sas --account-name sttrydata05 -c invoices -n 2026/09/a.csv --permissions r --expiry 2026-09-23T08:00Z --https-only --account-key "$KEY" -o tsv)
$ echo "$SAS"
se=2026-09-23T08%3A00Z&sp=r&sv=2025-07-05&sr=b&spr=https&sig=sxRwGMBGwx8qBWdDtn3E8bocNioB5ZJNQPXhqOd2zsk%3D
$ curl -s "https://sttrydata05.blob.core.windows.net/invoices/2026/09/a.csv?$SAS"
inv-001
$ curl -s -X PUT -H "x-ms-blob-type: BlockBlob" -d tampered "https://sttrydata05.blob.core.windows.net/invoices/2026/09/a.csv?$SAS"
<?xml version="1.0" encoding="utf-8"?><Error><Code>AuthorizationPermissionMismatch</Code><Message>This request is not authorized to perform this operation using this permission.
RequestId:80ac8744-06a0-4b9e-8ffd-26f3b5ad0f2b
Time:2026-09-22T20:00:05.2700000Z</Message></Error>
The write was refused with AuthorizationPermissionMismatch: the token says sp=r. (A VNet rule for a subnet in another resource group takes the subnet's full id, as below.) An account SAS covers more (services b blob, f file, q queue, t table; resource types s service, c container, o object) and is the one to avoid handing out:
$ az storage account generate-sas --account-name sttrydata05 --services b --resource-types sco --permissions rl --expiry 2026-09-23 --account-key "$KEY" -o tsv
se=2026-09-23&sp=rl&sv=2025-07-05&ss=b&srt=sco&sig=1Vv5DFzVHiKhwminI1yDC5lKuUHRG1Tmk67NWp%2BuBqU%3D
Revoking a key-signed SAS is the problem: there is no list of issued tokens. The only switches are its own expiry and renewing the key that signed it, which also breaks every other client using that key:
$ az storage account keys renew -n sttrydata05 -g rg-try-data --key key1 -o none
$ curl -s "https://sttrydata05.blob.core.windows.net/invoices/2026/09/a.csv?$SAS" | grep -o "<AuthenticationErrorDetail>[^<]*" | head -1
<AuthenticationErrorDetail>Signature did not match. String to sign used was r
Stored access policies: a SAS you can take back
A stored access policy lives on a container (up to 5 per container) and holds the permissions and expiry. A SAS that names it (si=) carries no permissions or expiry of its own: change the policy and every SAS using it changes; delete it and they all stop working, without touching the keys.
$ KEY=$(az storage account keys list -n sttrydata05 -g rg-try-data --query "[0].value" -o tsv)
$ az storage container policy create --account-name sttrydata05 -c invoices -n partner-read --permissions rl --expiry 2026-10-22 --account-key "$KEY" -o none
$ PSAS=$(az storage container generate-sas --account-name sttrydata05 -n invoices --policy-name partner-read --account-key "$KEY" -o tsv)
$ echo "$PSAS"
sv=2025-07-05&sr=c&si=partner-read&sig=GW24Ufa8Ftq%2BihVQr7Gobn1Wjs%2FOAx3MvhyYTUlxdCA%3D
$ curl -s "https://sttrydata05.blob.core.windows.net/invoices/2026/09/a.csv?$PSAS"
inv-001
The partner is gone a week early. Delete the policy, and their SAS dies with it, while the keys and every other client stay untouched:
$ az storage container policy delete --account-name sttrydata05 -c invoices -n partner-read --account-key "$KEY" -o none
$ curl -s "https://sttrydata05.blob.core.windows.net/invoices/2026/09/a.csv?$PSAS" | grep -o "<AuthenticationErrorDetail>[^<]*"
<AuthenticationErrorDetail>The specified signed identifier 'partner-read' does not exist on container 'invoices' (the stored access policy was deleted or renamed, which revokes every SAS that used it).
(Changes to a stored access policy can take up to 30 seconds to take effect.)
User delegation SAS
Signed with a user delegation key that Entra ID issues to a signed-in user (or managed identity) holding the right to request one, instead of an account key. Microsoft's recommended kind:
- valid for at most 7 days;
- grants at most what the signer's data role allows. An Owner without a data role can create one, and it is worthless;
- recognisable by
skoid/sktid(the signer's object and tenant) in the token; - revoked by revoking the user's delegation keys, or by removing their role.
$ az role assignment create --assignee [email protected] --role "Storage Blob Data Reader" --scope $(az storage account show -n sttrydata05 -g rg-try-data --query id -o tsv) -o none
$ URL=$(az storage blob generate-sas --account-name sttrydata05 -c invoices -n 2026/09/a.csv --permissions r --expiry 2026-09-29 --as-user --auth-mode login --full-uri -o tsv)
$ echo "$URL" | grep -o "skoid=[^&]*"
skoid=99999999-8888-7777-6666-555555555555
$ curl -s "$URL"
inv-001
Entra ID and data roles
For your own scripts and apps, skip SAS entirely: --auth-mode login uses your Entra identity and its data role. Setting AZURE_STORAGE_AUTH_MODE=login makes it the default for the session.
$ export AZURE_STORAGE_AUTH_MODE=login
$ az storage blob list --account-name sttrydata05 -c invoices --query "[].name" -o tsv
2026/09/a.csv
$ echo inv-002 > b.csv
$ az storage blob upload --account-name sttrydata05 -c invoices -n 2026/09/b.csv -f b.csv -o none
ERROR: You do not have the required permissions needed to perform this operation.
Depending on your operation, you may need to be assigned one of the following roles:
"Storage Blob Data Owner"
"Storage Blob Data Contributor"
"Storage Blob Data Reader"
"Storage Queue Data Contributor"
"Storage Queue Data Reader"
"Storage Table Data Contributor"
"Storage Table Data Reader"
If you want to use the old authentication method and allow querying for the right account key, please use the "--auth-mode" parameter and "key" value.
The upload is refused: Storage Blob Data Reader reads, it doesn't write. The fix is a role, not a key. Data roles can be scoped to a single container (.../blobServices/default/containers/invoices), which a key never can.
Network: the storage firewall
By default an account accepts requests from any network (still authenticated). The firewall changes that:
--default-action Deny: only allowed networks get in. Keep--bypass AzureServicesso trusted Azure services (Backup, Monitor, Event Grid...) still reach it.- IP rules: public IPs or ranges (
198.51.100.24is this box's egress address). Private ranges (10.x, 172.16-31.x, 192.168.x) are refused: inside Azure, use VNet rules. - VNet rules: a subnet with the
Microsoft.Storageservice endpoint. Traffic from that subnet reaches the account over the Azure backbone with the subnet's identity. The account still has a public endpoint. - Private endpoint (Azure II): the account gets a private IP in your VNet, and public access can be turned off completely (
--public-network-access Disabled).
$ az storage account update -n sttrydata05 -g rg-try-data --default-action Deny --bypass AzureServices -o none
$ az storage blob list --account-name sttrydata05 -c invoices --query "[].name" -o tsv 2>&1 | grep ErrorCode
ErrorCode:AuthorizationFailure
$ az storage account network-rule add -n sttrydata05 -g rg-try-data --ip-address 10.20.3.0/24 2>&1 | head -1
ERROR: (NetworkAclsValidationFailure) Validation of network acls failure: IpAddressOrRangeIsInvalid:The IP address range 10.20.3.0/24 is a private range. Private IP ranges are not allowed in storage account IP rules; use virtual network rules or private endpoints.
$ az storage account network-rule add -n sttrydata05 -g rg-try-data --ip-address 198.51.100.24 -o none
$ az network vnet subnet update -g rg-oncall-lab --vnet-name vnet-sysop -n snet-app --service-endpoints Microsoft.Storage -o none
$ az storage account network-rule add -n sttrydata05 -g rg-try-data --subnet $(az network vnet subnet show -g rg-oncall-lab --vnet-name vnet-sysop -n snet-app --query id -o tsv) -o none
$ az storage account network-rule list -n sttrydata05 -g rg-try-data --query "{ips:ipRules[].ipAddressOrRange, subnets:virtualNetworkRules[].virtualNetworkResourceId}"
{
"ips": [
"198.51.100.24"
],
"subnets": [
"/subscriptions/00000000-1111-2222-3333-444444444444/resourceGroups/rg-oncall-lab/providers/Microsoft.Network/virtualNetworks/vnet-sysop/subnets/snet-app"
]
}
$ az storage blob list --account-name sttrydata05 -c invoices --query "[].name" -o tsv
2026/09/a.csv
A firewall block says AuthorizationFailure (this request is not authorized), not "permission denied". When a working pipeline suddenly gets 403s from storage, check the firewall and the source IP before the roles.
On the exam (AZ-104 2.1.1-2.1.4): the SAS types (account, service, user delegation) and their parameters, stored access policies (5 per container, revoke by deleting), key rotation with two keys, disabling shared key access, data roles vs control-plane roles, firewall default action, IP rules (public only), VNet rules with service endpoints, trusted services bypass, private endpoints.
In an interview: "A partner needs to download files from one container for two weeks. How?" - "A service SAS on that container with read and list only, HTTPS only, ideally limited to their IP range, tied to a stored access policy that expires in two weeks. Then I can revoke it early by deleting the policy without rotating the account keys. If they were in our directory I'd give them Storage Blob Data Reader on the container instead, and a user delegation SAS if a URL is needed. Never an account SAS or the keys."
What you can now do
- Use keys deliberately, rotate them with two keys, and know when to turn shared key access off.
- Build the smallest SAS for a job, and revoke it: expiry, stored access policy, key rotation.
- Use data roles and
--auth-mode logininstead of keys. - Lock an account to known IPs and subnets, and recognise a firewall 403.