-
Notifications
You must be signed in to change notification settings - Fork 314
Fix encryption key cache design for AKV provider #3464
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR refactors the Azure Key Vault (AKV) provider cache design to address cache TTL enforcement, remove sync-over-async calls, and prevent redundant cache fetches under concurrency.
- Modernized
SqlSymmetricKeyCache
to use target-typednew
, inlineTryGetValue
, and removed forced TTL reset. - Switched
SqlConnection
to C# 12 collection expressions and dropped global TTL zeroing. - Refactored
SqlColumnEncryptionAzureKeyVaultProvider
andAzureSqlKeyCryptographer
from async task-based caching to synchronous patterns with a semaphore guard.
Reviewed Changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
File | Description |
---|---|
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlSymmetricKeyCache.cs | Modernized cache lookup and entry options; removed TTL override |
src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs | Replaced new List<string>(…) with [ .. keys] ; removed TTL zeroing |
src/Microsoft.Data.SqlClient/add-ons/AzureKeyVaultProvider/SqlColumnEncryptionAzureKeyVaultProvider.cs | Added SemaphoreSlim around local cache access |
src/Microsoft.Data.SqlClient/add-ons/AzureKeyVaultProvider/LocalCache.cs | Adjusted using order; removed unused Contains method |
src/Microsoft.Data.SqlClient/add-ons/AzureKeyVaultProvider/AzureSqlKeyCryptographer.cs | Replaced async fetch/dictionaries with sync KeyVaultKey cache |
Comments suppressed due to low confidence (5)
src/Microsoft.Data.SqlClient/add-ons/AzureKeyVaultProvider/AzureSqlKeyCryptographer.cs:31
- [nitpick] The
_keyFetchTaskDictionary
now storesKeyVaultKey
instances rather than tasks. Consider renaming it to better reflect its contents, e.g._keyCacheDictionary
or_fetchedKeys
.
private readonly ConcurrentDictionary<string, KeyVaultKey> _keyFetchTaskDictionary = new();
src/Microsoft.Data.SqlClient/add-ons/AzureKeyVaultProvider/AzureSqlKeyCryptographer.cs:175
FetchKeyFromKeyVault
retrieveskeyClient
but never initializes it. You need to callCreateKeyClient(vaultUri)
before accessing_keyClientDictionary
or handle the case wherekeyClient
is null.
return keyClient?.GetKey(keyName, keyVersion);
src/Microsoft.Data.SqlClient/add-ons/AzureKeyVaultProvider/AzureSqlKeyCryptographer.cs:81
- With the async/task-based guard removed, parallel calls for a missing key will all invoke
FetchKeyFromKeyVault
. Consider adding a lock or double-checked locking to prevent N+1 fetches under concurrency.
if (_keyFetchTaskDictionary.TryGetValue(keyIdentifierUri, out KeyVaultKey keyVaultKey))
src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs:279
- The method signature returns
List<string>
, but the collection expression[...]
produces an array by default. This will cause a type mismatch. Either construct aList<string>
explicitly or verify that C# 12 collection expressions target the declared return type.
return [.. s_systemColumnEncryptionKeyStoreProviders.Keys];
src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlConnection.cs:294
- Same as above: returning a collection expression for a method declared to return
List<string>
may not compile. Considernew List<string>(...)
or confirm the collection expression creates aList<string>
.
return [.. _customColumnEncryptionKeyStoreProviders.Keys];
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlSymmetricKeyCache.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/add-ons/AzureKeyVaultProvider/Strings.resx
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/add-ons/AzureKeyVaultProvider/AzureSqlKeyCryptographer.cs
Outdated
Show resolved
Hide resolved
...oft.Data.SqlClient/add-ons/AzureKeyVaultProvider/SqlColumnEncryptionAzureKeyVaultProvider.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlSymmetricKeyCache.cs
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A variety of comments.
src/Microsoft.Data.SqlClient/add-ons/AzureKeyVaultProvider/AzureSqlKeyCryptographer.cs
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/add-ons/AzureKeyVaultProvider/AzureSqlKeyCryptographer.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/add-ons/AzureKeyVaultProvider/AzureSqlKeyCryptographer.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/add-ons/AzureKeyVaultProvider/AzureSqlKeyCryptographer.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/add-ons/AzureKeyVaultProvider/AzureSqlKeyCryptographer.cs
Outdated
Show resolved
Hide resolved
src/Microsoft.Data.SqlClient/add-ons/AzureKeyVaultProvider/AzureSqlKeyCryptographer.cs
Show resolved
Hide resolved
...oft.Data.SqlClient/add-ons/AzureKeyVaultProvider/SqlColumnEncryptionAzureKeyVaultProvider.cs
Outdated
Show resolved
Hide resolved
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #3464 +/- ##
==========================================
- Coverage 68.86% 66.86% -2.00%
==========================================
Files 280 276 -4
Lines 62417 62236 -181
==========================================
- Hits 42982 41615 -1367
- Misses 19435 20621 +1186
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed trace logs are lacking connection context. But it was already like that, so not going to hold this up for that.
* Fix AzureSqlKeyCryptographer to be pure sync * Fix Symmetric key cache design + Key vault cache * Fix tests * Add back multi-user cache scenario * Add cache lock in global CEK cache to prevent concurrency issues. * Touch-ups * More changes to streamline getKey calls * More updates * - Addressed review comments. * - Fixing tests that expect certain localized strings that have changed. --------- Co-authored-by: Paul Medynski <31868385+paulmedynski@users.noreply.github.com>
Description
This PR fixes a couple of issues:
Issues
Fixes above issues, and additionally:
Testing
Repro can be found here: https://gist.github.com/cheenamalhotra/79f8ae709147e0b52239fd54c3049932
When registering AKV provider globally:
Guidelines
Please review the contribution guidelines before submitting a pull request:
P.S. We might want to consider it for 6.1.0 as it's critically needed for a customer.