Your archive automation failure involves all three authentication areas. Here’s the comprehensive solution to transition from SAS tokens to managed identity:
SAS Token Expiry Management:
SAS tokens with stored access policies provide centralized control but require active management. Your current issue stems from hardcoded SAS tokens in automation code. Best practice for SAS management:
- Never hardcode SAS tokens - store in Key Vault if SAS is required
- Set reasonable expiry (90-180 days for automation scenarios)
- Implement token refresh logic before expiry
- Use stored access policies to enable revocation without code changes
However, for long-term automation, managed identity is superior and eliminates token management entirely.
RBAC Policy Update Process:
Your managed identity has correct role but insufficient scope. Verify and update:
az role assignment create \
--assignee <function-managed-identity-id> \
--role "Storage Blob Data Contributor" \
--scope /subscriptions/{sub}/resourceGroups/{rg}/providers/Microsoft.Storage/storageAccounts/erparchive
Storage Blob Data Contributor provides read, write, and delete permissions on blob data. Assign at storage account level to cover all containers used by automation.
Managed Identity Authentication Implementation:
Update your Azure Function code to eliminate SAS dependency. Here’s the transition:
Old code (SAS-based):
BlobServiceClient client = new BlobServiceClient(
"DefaultEndpointsProtocol=https;AccountName=...;SharedAccessSignature=..."
);
New code (Managed Identity):
using Azure.Identity;
using Azure.Storage.Blobs;
var credential = new DefaultAzureCredential();
var serviceUri = new Uri("https://erparchive.blob.core.windows.net");
BlobServiceClient client = new BlobServiceClient(serviceUri, credential);
Critical steps for successful transition:
-
Enable system-assigned managed identity on Azure Function:
- Portal: Function App → Identity → System assigned → On
- CLI: az functionapp identity assign --name YourFunction --resource-group YourRG
-
Update function application settings:
-
Update NuGet packages:
- Azure.Storage.Blobs (v12+)
- Azure.Identity (v1.5+)
-
Modify code to use DefaultAzureCredential (shown above)
-
Test authentication:
- Deploy updated function
- Trigger manually to verify blob access
- Monitor function logs for authentication errors
-
Handle firewall rules:
- If storage has firewall enabled, add Function App outbound IPs to allowed list
- Or use VNet integration with service endpoints
RBAC role assignments can take 5-10 minutes to propagate. After assignment, the DefaultAzureCredential will automatically discover and use the function’s managed identity for authentication.
Benefits of this approach:
- No token expiry management required
- Automatic credential rotation by Azure
- Centralized access control via RBAC
- Audit trail through Azure Activity Log
- No secrets stored in code or configuration
Your archive automation will resume once the function code is updated to use managed identity authentication and RBAC permissions have propagated.