Skip to content
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

CSHARP-4944: Enable use of native crypto in libmongocrypt bindings #796

Merged
merged 6 commits into from
May 23, 2024

Conversation

adelinowona
Copy link
Contributor

Description

Use libmongocrypt native crypto when available.

What's changing

  • Bundle crypto-enabled libmongocrypt on Mac.
  • On linux continue to bundle crypto-disabled libmongocrypt, but inform users to install the crypto-enabled libmongocrypt on their system and use the new environment variable LIBMONGOCRYPT_PATH to make it available to the driver. If LIBMONGOCRYPT_PATH is not set then we default to using the packaged crypto-disabled libmongocrypt.

Performance Results

Using crypto-disabled libmongocrypt:

Method ThreadsCount Mean Error StdDev Median
BulkDecryptionUsingBinding 1 50.83 ms 0.975 ms 2.279 ms 49.70 ms
BulkDecryptionUsingBinding 2 71.34 ms 1.422 ms 3.090 ms 71.32 ms
BulkDecryptionUsingBinding 8 766.59 ms 23.535 ms 69.393 ms 776.18 ms
BulkDecryptionUsingBinding 64 5,575.51 ms 91.840 ms 85.907 ms 5,581.51 ms

Using crypto-enabled libmongocrypt:

Method ThreadsCount Mean Error StdDev
BulkDecryptionUsingBinding 1 22.03 ms 0.103 ms 0.096 ms
BulkDecryptionUsingBinding 2 22.89 ms 0.117 ms 0.104 ms
BulkDecryptionUsingBinding 8 30.69 ms 0.564 ms 0.554 ms
BulkDecryptionUsingBinding 64 201.37 ms 1.997 ms 1.559 ms

Copy link
Contributor

@BorisDog BorisDog left a comment

Choose a reason for hiding this comment

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

Few minor questions.

}
catch (LibraryLoader.FunctionNotFoundException)
{
// mongocrypt_is_crypto_available is only available in libmongocrypt version >= 1.9
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this better than checking the Library.Version ?
In the case when the version is right, and mongocrypt_is_crypto_available throws, that might point to other problems, and then it's better to fail?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point! Done

@@ -186,10 +168,18 @@ private class LinuxLibrary : ISharedLibraryLoader
// #define RTLD_GLOBAL 0x100
public const int RTLD_GLOBAL = 0x100;
public const int RTLD_NOW = 0x2;

private static readonly string[] _suffixPaths =
Copy link
Contributor

Choose a reason for hiding this comment

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

Plz add static prefix.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

bindings/cs/README.md Show resolved Hide resolved
@adelinowona adelinowona requested a review from BorisDog May 2, 2024 21:20
Copy link
Contributor

@BorisDog BorisDog left a comment

Choose a reason for hiding this comment

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

minor comments

@@ -42,14 +42,15 @@ public static CryptClient Create(CryptOptions options)
MongoCryptSafeHandle handle = null;
Status status = null;

// mongocrypt_is_crypto_available is only available in libmongocrypt version >= 1.9
var cryptoAvailable = Version.Parse(Library.Version) >= Version.Parse("1.9") && Library.mongocrypt_is_crypto_available();
Copy link
Contributor

Choose a reason for hiding this comment

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

minor: let's safe Version.Parse("1.9") as static readonly var.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

@@ -54,6 +54,9 @@ If you see `Windows Error: 126` during tests, like the example below, it means t
2. cd <build>/bindings/cs
3. dotnet build cs.build
```
*Note*: You can use the ```LIBMONGOCRYPT_PATH``` environment variable to load a locally installed
libmongocrypt build. You should specify the absolute path to the libmongocrypt build itself not just the containing folder. For example on Linux:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
libmongocrypt build. You should specify the absolute path to the libmongocrypt build itself not just the containing folder. For example on Linux:
libmongocrypt build. You should specify the absolute path to the libmongocrypt library itself, not just the containing folder. For example on Linux:

@@ -150,9 +124,17 @@ private class DarwinLibraryLoader : ISharedLibraryLoader
public const int RTLD_GLOBAL = 0x8;
public const int RTLD_NOW = 0x2;

private static readonly string[] _s_suffixPaths =
Copy link
Contributor

Choose a reason for hiding this comment

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

I see that we are missing conventions for static vars, in libmongocrypt. Let's go with the usual ___s_suffixPaths convention here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@adelinowona adelinowona requested a review from BorisDog May 7, 2024 15:58
@@ -34,6 +34,9 @@ public class CryptClientFactory
private static Library.Delegates.RandomCallback __randomCallback = new Library.Delegates.RandomCallback(SecureRandomCallback.GenerateRandom);
private static Library.Delegates.CryptoHmacCallback __signRsaesPkcs1HmacCallback = new Library.Delegates.CryptoHmacCallback(SigningRSAESPKCSCallback.RsaSign);

// mongocrypt_is_crypto_available is only available in libmongocrypt version >= 1.9
private static readonly Version __s_libVersionWithMongocryptIsCryptoAvailable = Version.Parse("1.9");
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider:
private static readonly Version __mongocryptIsCryptoAvailableMinVersion = Version.Parse("1.9");

@@ -54,6 +54,9 @@ If you see `Windows Error: 126` during tests, like the example below, it means t
2. cd <build>/bindings/cs
3. dotnet build cs.build
```
*Note*: You can use the ```LIBMONGOCRYPT_PATH``` environment variable to load a locally installed
libmongocrypt build. You should specify the absolute path to the libmongocrypt library itself, not just the containing folder. For example on Linux:
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this comment got lost:
#796 (comment)

@@ -150,9 +124,17 @@ private class DarwinLibraryLoader : ISharedLibraryLoader
public const int RTLD_GLOBAL = 0x8;
public const int RTLD_NOW = 0x2;

private static readonly string[] __s_suffixPaths =
Copy link
Contributor

Choose a reason for hiding this comment

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

I am sorry for confusion, I wanted to write __suffixPaths instead ___s_suffixPaths

@adelinowona adelinowona requested a review from BorisDog May 7, 2024 18:44
Copy link
Contributor

@BorisDog BorisDog left a comment

Choose a reason for hiding this comment

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

LGTM

@adelinowona adelinowona merged commit 1a94335 into mongodb:master May 23, 2024
29 of 50 checks passed
@adelinowona adelinowona deleted the csharp4944 branch May 23, 2024 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants