Always encrypted: 401 errors from Azure Key Vault is cached forever #3212
Labels
🐛 Bug!
Issues that are bugs in the drivers we maintain.
🆕 Triage Needed
For new issues, not triaged yet.
Describe the bug
We use Azure Key Vault to store the necessary keys to decrypt data from Azure SQL (Always Encrypted).
We had a network configuration issue several days ago in Azure making requests to our Azure Key Vault fail with an HTTP 401 error response.
Even more than 24h after the incident was resolved, our application still couldn't read encrypted columns from Azure SQL.
Looking at the
Date
HTTP header shown in the exception message, we noticed that the Key Vault error response had been cached for more than 24h.Here is the details of an error that occurred on the 2025-03-04 around 12:00. Notice the date in the error (2025-03-03 around 09:00 AM).
After a quick review of the AzureSqlKeyCryptographer class, it seems that the response is cached forever.
The only way we found to reset the cache was to restart the entire application, which is clearly not ideal.
To reproduce
I expect that this could be reproduced by configuring an Azure SQL instance with Always Encrypted and storing keys in a publicly available Azure Key Vault and creating a simple app that reads encrypted data from it.
Then, disable "public access" in the Key Vault config.
As a result, a previously started app would still work and a newly started app won't.
If the public access is restored in the Key Vault configuration, the applications' behaviours won't change.
Expected behavior
Microsoft.Data.SqlClient
should not keep error responses in cache, or at least for a very limited period.Further technical details
Microsoft.Data.SqlClient version: 5.2.2
.NET target: .NET 8.0
SQL Server version: Azure SQL
Operating system: Docker container
The text was updated successfully, but these errors were encountered: