Skip to content

Cross-platform SqlColumnEncryptionCertificateStoreProvider support #3014

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

Open
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

edwardneal
Copy link
Contributor

@edwardneal edwardneal commented Nov 18, 2024

Wider idea: #3048.

SqlColumnEncryptionCertificateStoreProvider is the only built-in SqlColumnEncryptionKeyStoreProvider implementation which needs no significant code changes to enable cross-platform support out of the box. I've removed the Unix-specific guards on this and widened the test coverage.

In the process, I've switched the Linux/macOS tests to use SqlColumnEncryptionCertificateStoreProvider rather than the Azure Key Vault provider. This doesn't seem to have had an impact on the code coverage statistics, but it's notable nonetheless.

* Only allow the use of the CurrentUser location for certificates.
* Changed the PublishTestResults@2 step to ensure that failed tests are also published.
…t to fix problems importing these files on MacOS
…t to fix problems importing these files on MacOS
Converting more unusually-embedded PFX files into embedded resources
@edwardneal edwardneal force-pushed the feat/xplat-column-encryption-certificate-store-provider branch from 6966d54 to c199ddd Compare November 19, 2024 20:37
Eliminating possibility of a certificate with a private key having been previously added to the user certificate store.
Copy link

codecov bot commented Nov 19, 2024

Codecov Report

Attention: Patch coverage is 65.38462% with 9 lines in your changes missing coverage. Please review.

Project coverage is 60.55%. Comparing base (12f742d) to head (de4bc32).

Files with missing lines Patch % Lines
....SqlClient/src/Microsoft/Data/SqlClient/SqlUtil.cs 57.14% 9 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3014      +/-   ##
==========================================
- Coverage   63.51%   60.55%   -2.96%     
==========================================
  Files         293      286       -7     
  Lines       63810    63511     -299     
==========================================
- Hits        40526    38461    -2065     
- Misses      23284    25050    +1766     
Flag Coverage Δ
addons ?
netcore 66.98% <65.38%> (+0.05%) ⬆️
netfx 58.80% <65.38%> (-7.42%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 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.

@edwardneal
Copy link
Contributor Author

This has reached the point where it needs a rough review by the SqlClient team.

We can make SqlColumnEncryptionCertificateStoreProvider work on Windows, Unix and macOS, as expected. There's one caveat to this though: Unix and macOS will only support certificates from the user store. I've proceeded with this, working from the starting point that this isn't a major barrier - a systemd unit will often run as a user and be able to access a user-level certificate store anyway. Does that caveat make this non-viable?

Secondly: around half of this PR's additions and a quarter of its file changes are because I moved the certificates which were previously static byte arrays in the code to .pfx files which are included within the test assembly as embedded resources. In the process, I also added a password to these .pfx files - macOS didn't seem to import them as certificates without one. Is this going to fall afoul of any security policies against checking private keys into the repo, or is it not a problem if they're only used for the tests?

If all's well, I'll likely cherry-pick some of the test changes included in this PR into a new one; it should clean up the diff a little and make it easier to review.

@edwardneal edwardneal changed the title WIP | Cross-platform SqlColumnEncryptionCertificateStoreProvider support Cross-platform SqlColumnEncryptionCertificateStoreProvider support Nov 19, 2024
@David-Engel
Copy link
Contributor

I'm pretty sure the private keys and password won't pass credential scanners. Can the files be generated on the fly with random passwords?

@edwardneal
Copy link
Contributor Author

edwardneal commented Nov 20, 2024

They can, and in some places they are - some of the tests launch a PowerShell script to do that. I'll work out a way to make this consistent across the tests...

Edit: it looks like there are three or four different sets of hardcoded certificates; some have hardcoded passwords, some don't have passwords at all. I'm working on a common approach which does as you've suggested; that'll be a separate PR for ease of review.

Second edit: I've decided to split this prerequisite into two PRs, the first of which is #3034.

@cheenamalhotra cheenamalhotra added the Partner Approval Needed 🤝 Issues/PRs that require approval/feedback from partner teams label Nov 23, 2024
paulmedynski
paulmedynski previously approved these changes Mar 21, 2025
Debug.Assert(validLocations.Length == 2);
if (isSystemOp)
{
return ADP.ArgumentNull(TdsEnums.TCE_PARAM_MASTERKEY_PATH, StringsHelper.GetString(Strings.TCE_NullCertificatePathSysErr, validLocations[0], validLocations[1], @"/"));
Copy link
Contributor

Choose a reason for hiding this comment

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

The Assert() won't save us if validLocations has fewer than 2 elements. An existing problem, but FYI.

@paulmedynski paulmedynski self-assigned this Mar 21, 2025
@mdaigle mdaigle self-assigned this Apr 2, 2025
@benrr101 benrr101 removed the Partner Approval Needed 🤝 Issues/PRs that require approval/feedback from partner teams label Apr 29, 2025
@benrr101
Copy link
Contributor

@edwardneal Sorry this one got lost, we had reached out to another team to see if this was something that aligns with their ideals. There doesn't seem to be any objections, so we are moving forward to try and take these changes (thank you for them, btw). However, there's some merge conflicts - if you can get those addressed, we can get the builds running :)

@cheenamalhotra cheenamalhotra added this to the 6.1-preview2 milestone Apr 30, 2025
@benrr101
Copy link
Contributor

@edwardneal I took a crack at resolving the merge conflicts myself, but it turns out most of the conflicts are due to your own efforts to remove hard-coded certificates from the tests. As such, it seems like you might be the best person to resolve those conflicts :)

@edwardneal
Copy link
Contributor Author

Thanks @benrr101, sorry for the radio silence here. You're right - I need to rework the certificate generation to take advantage of the new fixtures, then drop the existing certificates. I'm planning to wait until the final hard-coded certificates are removed in #3204, then merge.

@paulmedynski
Copy link
Contributor

Hi @edwardneal - this PR has conflicts that need to be resolved. We are hoping to complete all 6.1-preview2 PRs this week. Will you have time to resovle the conflicts?

@edwardneal edwardneal requested a review from a team as a code owner May 29, 2025 19:20
@edwardneal edwardneal force-pushed the feat/xplat-column-encryption-certificate-store-provider branch from 7903126 to 4c9e6d4 Compare May 29, 2025 19:21
@edwardneal
Copy link
Contributor Author

Thanks @paulmedynski, I've just resolved the conflicts - it looks like the initial certificate generation changes were sufficient to remove the hardcoded credentials.

Would you mind running CI please, so we can make sure the Linux tests are working properly post-merge?

@paulmedynski
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@edwardneal
Copy link
Contributor Author

Thanks. It looks like the .netcore.Unix.cs file was retained during the merge. I've just removed it, can you please retrigger CI?

@benrr101
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 2 pipeline(s).

paulmedynski
paulmedynski previously approved these changes May 30, 2025
@edwardneal
Copy link
Contributor Author

Test failures should hopefully be addressed, can you re-run CI please?

It's not a problem if this falls into the next 6.1 preview.

@benrr101
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 2 pipeline(s).

@edwardneal
Copy link
Contributor Author

Thanks. I'm happy with the Windows & Linux implementations, but there's clearly extra requirements when adding the certificates to the current user's certificate store in MacOS. I think the problem might be an absent Subject Alternate Name, but if it isn't then I'll need to test further.

I've made the adjustment and would appreciate a final CI run. If this doesn't work, I think it'd be better to leave this out of the current preview; it'll take a week or two before I can start testing on MacOS in earnest.

For reference, the failing stack trace is below.

2025-05-30T21:47:34.3782040Z   [xUnit.net 00:00:15.63]     Microsoft.Data.SqlClient.Tests.AlwaysEncryptedTests.SqlColumnEncryptionCertificateStoreProviderShould.TestAeadEncryptionReversal(dataType: "int", data: 2147483647, encType: Deterministic) [FAIL]
2025-05-30T21:47:34.3801200Z   [xUnit.net 00:00:15.63]       Class fixture type 'Microsoft.Data.SqlClient.TestUtilities.Fixtures.ColumnEncryptionCertificateFixture' threw in its constructor
2025-05-30T21:47:34.3802060Z   [xUnit.net 00:00:15.63]       ---- Interop+AppleCrypto+AppleCommonCryptoCryptographicException : An invalid record was encountered.
2025-05-30T21:47:34.3803700Z   [xUnit.net 00:00:15.63]       Stack Trace:
2025-05-30T21:47:34.3805040Z   [xUnit.net 00:00:15.63]         
2025-05-30T21:47:34.3806220Z   [xUnit.net 00:00:15.63]         ----- Inner Stack Trace -----
2025-05-30T21:47:34.3806970Z   [xUnit.net 00:00:15.63]            at Interop.AppleCrypto.X509StoreAddCertificate(SafeKeychainItemHandle certOrIdentity, SafeKeychainHandle keychain)
2025-05-30T21:47:34.3808030Z   [xUnit.net 00:00:15.63]         /_/src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/Fixtures/CertificateFixtureBase.cs(241,0): at Microsoft.Data.SqlClient.TestUtilities.Fixtures.CertificateFixtureBase.AddToStore(X509Certificate2 cert, StoreLocation storeLocation, StoreName storeName)
2025-05-30T21:47:34.3809240Z   [xUnit.net 00:00:15.63]         /_/src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/Fixtures/ColumnEncryptionCertificateFixture.cs(38,0): at Microsoft.Data.SqlClient.TestUtilities.Fixtures.ColumnEncryptionCertificateFixture..ctor()
2025-05-30T21:47:34.3810600Z   [xUnit.net 00:00:15.63]            at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
2025-05-30T21:47:34.3823680Z   [xUnit.net 00:00:15.63]            at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
2025-05-30T21:47:34.3831300Z   [xUnit.net 00:00:15.64]     Microsoft.Data.SqlClient.Tests.AlwaysEncryptedTests.SqlColumnEncryptionCertificateStoreProviderShould.TestAeadEncryptionReversal(dataType: "string", data: "Transparent Column Encryption", encType: Randomized) [FAIL]
2025-05-30T21:47:34.3853300Z   [xUnit.net 00:00:15.64]     Microsoft.Data.SqlClient.Tests.AlwaysEncryptedTests.SqlColumnEncryptionCertificateStoreProviderShould.TestAeadEncryptionReversal(dataType: "int", data: 0, encType: Randomized) [FAIL]
2025-05-30T21:47:34.3923540Z   [xUnit.net 00:00:15.64]       Class fixture type 'Microsoft.Data.SqlClient.TestUtilities.Fixtures.ColumnEncryptionCertificateFixture' threw in its constructor
2025-05-30T21:47:34.3974820Z   [xUnit.net 00:00:15.64]       ---- Interop+AppleCrypto+AppleCommonCryptoCryptographicException : An invalid record was encountered.
2025-05-30T21:47:34.3989890Z   [xUnit.net 00:00:15.64]       Stack Trace:
2025-05-30T21:47:34.3993810Z   [xUnit.net 00:00:15.64]         
2025-05-30T21:47:34.3997450Z   [xUnit.net 00:00:15.64]         ----- Inner Stack Trace -----
2025-05-30T21:47:34.4000570Z   [xUnit.net 00:00:15.64]            at Interop.AppleCrypto.X509StoreAddCertificate(SafeKeychainItemHandle certOrIdentity, SafeKeychainHandle keychain)
2025-05-30T21:47:34.4013060Z   [xUnit.net 00:00:15.64]         /_/src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/Fixtures/CertificateFixtureBase.cs(241,0): at Microsoft.Data.SqlClient.TestUtilities.Fixtures.CertificateFixtureBase.AddToStore(X509Certificate2 cert, StoreLocation storeLocation, StoreName storeName)
2025-05-30T21:47:34.4063330Z   [xUnit.net 00:00:15.64]         /_/src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/Fixtures/ColumnEncryptionCertificateFixture.cs(38,0): at Microsoft.Data.SqlClient.TestUtilities.Fixtures.ColumnEncryptionCertificateFixture..ctor()
2025-05-30T21:47:34.4151730Z   [xUnit.net 00:00:15.64]            at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
2025-05-30T21:47:34.4168730Z   [xUnit.net 00:00:15.64]            at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
2025-05-30T21:47:34.4180560Z   [xUnit.net 00:00:15.64]       Class fixture type 'Microsoft.Data.SqlClient.TestUtilities.Fixtures.ColumnEncryptionCertificateFixture' threw in its constructor
2025-05-30T21:47:34.4202700Z   [xUnit.net 00:00:15.64]       ---- Interop+AppleCrypto+AppleCommonCryptoCryptographicException : An invalid record was encountered.
2025-05-30T21:47:34.4213020Z   [xUnit.net 00:00:15.64]       Stack Trace:
2025-05-30T21:47:34.4224910Z   [xUnit.net 00:00:15.64]         
2025-05-30T21:47:34.4226020Z   [xUnit.net 00:00:15.64]         ----- Inner Stack Trace -----
2025-05-30T21:47:34.4226820Z   [xUnit.net 00:00:15.64]            at Interop.AppleCrypto.X509StoreAddCertificate(SafeKeychainItemHandle certOrIdentity, SafeKeychainHandle keychain)
2025-05-30T21:47:34.4227850Z   [xUnit.net 00:00:15.64]         /_/src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/Fixtures/CertificateFixtureBase.cs(241,0): at Microsoft.Data.SqlClient.TestUtilities.Fixtures.CertificateFixtureBase.AddToStore(X509Certificate2 cert, StoreLocation storeLocation, StoreName storeName)
2025-05-30T21:47:34.4229010Z   [xUnit.net 00:00:15.64]         /_/src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/Fixtures/ColumnEncryptionCertificateFixture.cs(38,0): at Microsoft.Data.SqlClient.TestUtilities.Fixtures.ColumnEncryptionCertificateFixture..ctor()
2025-05-30T21:47:34.4230420Z   [xUnit.net 00:00:15.64]            at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
2025-05-30T21:47:34.4230990Z   [xUnit.net 00:00:15.64]            at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
2025-05-30T21:47:34.4231760Z   [xUnit.net 00:00:15.64]     Microsoft.Data.SqlClient.Tests.AlwaysEncryptedTests.SqlColumnEncryptionCertificateStoreProviderShould.TestAeadEncryptionReversal(dataType: "int", data: -2147483648, encType: Deterministic) [FAIL]
2025-05-30T21:47:34.4260360Z   [xUnit.net 00:00:15.64]       Class fixture type 'Microsoft.Data.SqlClient.TestUtilities.Fixtures.ColumnEncryptionCertificateFixture' threw in its constructor
2025-05-30T21:47:34.4274550Z   [xUnit.net 00:00:15.64]       ---- Interop+AppleCrypto+AppleCommonCryptoCryptographicException : An invalid record was encountered.
2025-05-30T21:47:34.4307190Z   [xUnit.net 00:00:15.64]       Stack Trace:
2025-05-30T21:47:34.4310410Z   [xUnit.net 00:00:15.64]         
2025-05-30T21:47:34.4314440Z   [xUnit.net 00:00:15.64]         ----- Inner Stack Trace -----
2025-05-30T21:47:34.4317710Z   [xUnit.net 00:00:15.64]            at Interop.AppleCrypto.X509StoreAddCertificate(SafeKeychainItemHandle certOrIdentity, SafeKeychainHandle keychain)
2025-05-30T21:47:34.4321850Z   [xUnit.net 00:00:15.64]         /_/src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/Fixtures/CertificateFixtureBase.cs(241,0): at Microsoft.Data.SqlClient.TestUtilities.Fixtures.CertificateFixtureBase.AddToStore(X509Certificate2 cert, StoreLocation storeLocation, StoreName storeName)
2025-05-30T21:47:34.4323500Z   [xUnit.net 00:00:15.64]         /_/src/Microsoft.Data.SqlClient/tests/tools/Microsoft.Data.SqlClient.TestUtilities/Fixtures/ColumnEncryptionCertificateFixture.cs(38,0): at Microsoft.Data.SqlClient.TestUtilities.Fixtures.ColumnEncryptionCertificateFixture..ctor()
2025-05-30T21:47:34.4330240Z   [xUnit.net 00:00:15.64]            at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
2025-05-30T21:47:34.4337400Z   [xUnit.net 00:00:15.64]            at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

@paulmedynski
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 2 pipeline(s).

paulmedynski
paulmedynski previously approved these changes Jun 2, 2025
@mdaigle
Copy link
Contributor

mdaigle commented Jun 3, 2025

@edwardneal it looks like the CI is still failing on mac, so I'm moving this out of 6.1-preview2.

@mdaigle mdaigle removed this from the 6.1-preview2 milestone Jun 3, 2025
MacOS will throw an exception when adding a certificate to the user store if the PersistKeySet key storage flag is set. Instead, use the UserKeySet flag.
@edwardneal
Copy link
Contributor Author

I've got local tests passing on a MacOS environment - it looks like the test certificates were being created with invalid KeyStorageFlags. I'd appreciate a CI run.

@paulmedynski
Copy link
Contributor

/azp run

Copy link

Azure Pipelines successfully started running 2 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants