Skip to content

Fix #2957: route enclave AE CEK lookup through SQLServerSymmetricKeyCache - #2964

Merged
Muskan Gupta (muskan124947) merged 2 commits into
mainfrom
users/machavan/issue-2957-enclave-cek-cache
Jul 23, 2026
Merged

Muskan Gupta (muskan124947) merged 2 commits into
mainfrom
users/machavan/issue-2957-enclave-cek-cache

Conversation

@machavan

@machavan Mahendra Chavan (machavan) commented Jun 1, 2026 •

Copy link
Copy Markdown
Contributor

Problem description

Always Encrypted queries that target columns with ENCLAVE_COMPUTATIONS (AE v2) issue an Azure Key Vault (or other CMK store) round-trip on every execution, even when the same column encryption key (CEK) was just resolved milliseconds earlier. With AKV this adds ~150–400 ms per query and consumes vault throttling quota; under load it dominates query latency and surfaces as 429 Too Many Requests from AKV.

The non-enclave AE path does not have this problem because it goes through SQLServerSymmetricKeyCache via SQLServerSecurityUtility.decryptSymmetricKey. The enclave path in ISQLServerEnclaveProvider.processSDPEv1 instead resolves the enclave-requested CEK by calling provider.decryptColumnEncryptionKey(...) directly, bypassing the cache.

In addition, when a CMK provider is registered system-wide or globally (the recommended pattern for AKV), the provider's internal SimpleTtlCache is intentionally disabled — SQLServerSymmetricKeyCache is meant to be the single caching layer. With the enclave path bypassing that single layer, no cache is in effect for enclave queries at all.

Fixes existing GitHub issue

Fixes #2957

Fix Description

ISQLServerEnclaveProvider.processSDPEv1 ("Site 4") now resolves the enclave-requested CEK through the same path as the non-enclave code:

  • Build an EncryptionKeyInfo from the sp_describe_parameter_encryption row.
  • When connection.getServerColumnEncryptionVersion() indicates instance-level providers are in scope (shouldUseInstanceLevelProviderFlow), call SQLServerSecurityUtility.getKeyFromLocalProviders(...).
  • Otherwise, call SQLServerSymmetricKeyCache.getInstance().getKey(keyInfo, connection).
  • Pass symKey.getRootKey() into aev2CekEntry.put(...) for delivery to the enclave.

Effects:

  • Repeat enclave queries reuse the cached plaintext CEK; AKV is hit once per (server, encrypted-CEK, key-store) triple within the cache TTL.
  • The enclave path now also goes through setTrustedMasterKeyPaths enforcement, closing a minor inconsistency with the non-enclave path.
  • No public API change. No behavior change for callers that were not using AE v2.

Test coverage added:

  • src/test/java/com/microsoft/sqlserver/jdbc/EnclaveCacheUnitTest.java — pure unit test, no SQL Server required. Uses Mockito to drive processSDPEv1 with a fabricated sp_describe_parameter_encryption result set and a counting CMK provider.
    • nonEnclavePath_hitsCacheAfterFirstLookup: 50 lookups of the same wrapped CEK → asserts the provider's decryptColumnEncryptionKey is called exactly once.
    • enclavePath_hitsCacheAfterFirstLookup: 25 invocations of processSDPEv1 for the same enclave-requested CEK → asserts the provider's decryptColumnEncryptionKey is called exactly once. Without this fix the same test produces ~26 calls.

Verified locally with mvn -P jre25 -Dtest=EnclaveCacheUnitTest test — both tests pass.

New Public APIs

None.

Performance Validation

Tested against Azure SQL Database with VBS enclave, AKV-backed CMK, randomized encryption. Query: SELECT * FROM dbo.TestEnclave WHERE SSN LIKE ? (enclave-required LIKE on randomized column).

Run Without fix (ms) With fix (ms)
0 3426 1763
1 1992 415
2 2503 411
3 1953 413
4 2056 413
5 1926 413
6 2662 412
7 2458 411
8 1944 421
9 2380 412

Without the fix, every run pays the AKV round-trip (~2000+ ms). With the fix, only Run 0 hits AKV; Run 1+ serve from cache (~415 ms, matching non-enclave baseline).

Metric Without fix With fix Improvement
Avg time (Run 1–9) ~2200 ms ~413 ms ~5.3x faster
AKV calls per 10 queries 10 1 90% reduction
@codecov

codecov Bot commented Jun 1, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 66.66667% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 60.07%. Comparing base (854806d) to head (74b5ca2).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
...soft/sqlserver/jdbc/ISQLServerEnclaveProvider.java 66.66% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##               main    #2964      +/-   ##
============================================
+ Coverage     59.25%   60.07%   +0.82%     
- Complexity     5047     5131      +84     
============================================
  Files           153      153              
  Lines         36350    36353       +3     
  Branches       6650     6651       +1     
============================================
+ Hits          21538    21839     +301     
+ Misses        11056    10759     -297     
+ Partials       3756     3755       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
@divang
Divang Sharma (divang) marked this pull request as ready for review July 14, 2026 07:32
…ache

processSDPEv1 previously called the CMK provider directly for the enclave CEK, bypassing the driver-wide symmetric key cache. Combined with the provider-internal cache being disabled when registered system/globally, every enclave query hit the key store (e.g. Azure Key Vault) on every invocation.\n\nThe enclave CEK path now mirrors SQLServerSecurityUtility.decryptSymmetricKey: build an EncryptionKeyInfo and resolve via SQLServerSymmetricKeyCache (or instance-level providers when configured). This restores caching for AE v2 / enclave queries and also brings them under setTrustedMasterKeyPaths enforcement.\n\nAdds EnclaveCacheUnitTest (no SQL Server required) covering both the non-enclave and enclave paths; both confirm exactly one provider decrypt call across many lookups of the same wrapped CEK.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request fixes a performance issue in Always Encrypted (AE v2 / enclave computations) where enclave-requested CEKs were being decrypted via the CMK provider on every execution by routing the enclave CEK lookup through the driver’s existing symmetric key caching path.

Changes:

  • Updated ISQLServerEnclaveProvider.processSDPEv1 to resolve enclave-requested CEKs via SQLServerSymmetricKeyCache (or instance-level provider flow), aligning enclave and non-enclave CEK lookup behavior.
  • Added a new unit test (EnclaveCacheUnitTest) to assert that repeated non-enclave and enclave CEK lookups only invoke decryptColumnEncryptionKey once.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/main/java/com/microsoft/sqlserver/jdbc/ISQLServerEnclaveProvider.java Routes enclave-requested CEK resolution through the same cached path used by non-enclave AE to avoid repeated CMK provider calls.
src/test/java/com/microsoft/sqlserver/jdbc/EnclaveCacheUnitTest.java Adds regression coverage ensuring both paths hit the CEK cache after the first lookup.

@David-Engel David Engel (David-Engel) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I bet that hidden gem was fun to find. 🙂

@muskan124947 Muskan Gupta (muskan124947) changed the title Fix #2957: route enclave AE CEK lookup through SQLServerSymmetricKeyC… Jul 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

6 participants