Skip to content

CSHARP-4386: Test mongocryptd is not spawned when shared library is loaded. #965

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

Merged
merged 5 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions evergreen/evergreen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2063,22 +2063,22 @@ buildvariants:
- name: test-gssapi-netstandard21

- matrix_name: "csfle-with-mocked-kms-tests-windows"
matrix_spec: { os: "windows-64", ssl: "nossl", version: [ "5.0", "6.0", "rapid", "latest" ], topology: ["standalone"] }
matrix_spec: { os: "windows-64", ssl: "nossl", version: [ "5.0", "6.0", "rapid", "latest" ], topology: ["replicaset"] }
display_name: "CSFLE Mocked KMS ${version} ${os}"
tasks:
- name: test-csfle-with-mocked-kms-tls-net472
- name: test-csfle-with-mocked-kms-tls-netstandard20
- name: test-csfle-with-mocked-kms-tls-netstandard21

- matrix_name: "csfle-with-mocked-kms-tests-linux"
matrix_spec: { os: "ubuntu-1804", ssl: "nossl", version: [ "5.0", "6.0", "rapid", "latest" ], topology: ["standalone"] }
matrix_spec: { os: "ubuntu-1804", ssl: "nossl", version: [ "5.0", "6.0", "rapid", "latest" ], topology: ["replicaset"] }
display_name: "CSFLE Mocked KMS ${version} ${os}"
tasks:
- name: test-csfle-with-mocked-kms-tls-netstandard20
- name: test-csfle-with-mocked-kms-tls-netstandard21

- matrix_name: "csfle-with-mocked-kms-tests-macOS"
matrix_spec: { os: [ "macos-1100", "macos-1100-arm64" ], ssl: "nossl", version: [ "5.0", "6.0", "rapid", "latest" ], topology: ["standalone"] }
matrix_spec: { os: [ "macos-1100", "macos-1100-arm64" ], ssl: "nossl", version: [ "5.0", "6.0", "rapid", "latest" ], topology: ["replicaset"] }
display_name: "CSFLE Mocked KMS ${version} ${os}"
tasks:
- name: test-csfle-with-mocked-kms-tls-netstandard21
Expand Down
6 changes: 3 additions & 3 deletions src/MongoDB.Driver/Encryption/AutoEncryptionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public AutoEncryptionOptions(
_tlsOptions = tlsOptions.WithDefault(new Dictionary<string, SslSettings>());
_encryptedFieldsMap = encryptedFieldsMap.WithDefault(null);

EncryptionExtraOptionsValidator.EnsureThatExtraOptionsAreValid(_extraOptions);
EncryptionExtraOptionsHelper.EnsureThatExtraOptionsAreValid(_extraOptions);
KmsProvidersHelper.EnsureKmsProvidersAreValid(_kmsProviders);
KmsProvidersHelper.EnsureKmsProvidersTlsSettingsAreValid(_tlsOptions);
EncryptedCollectionHelper.EnsureCollectionsValid(_schemaMap, _encryptedFieldsMap);
Expand Down Expand Up @@ -273,10 +273,10 @@ public override string ToString()
internal CryptClientSettings ToCryptClientSettings() =>
new CryptClientSettings(
_bypassQueryAnalysis,
ExtraOptions.GetValueOrDefault<string, string, object>("cryptSharedLibPath"),
EncryptionExtraOptionsHelper.ExtractCryptSharedLibPath(ExtraOptions),
cryptSharedLibSearchPath: _bypassAutoEncryption ? null : "$SYSTEM",
_encryptedFieldsMap,
ExtraOptions.GetValueOrDefault<bool?, string, object>("cryptSharedLibRequired"),
EncryptionExtraOptionsHelper.ExtractCryptSharedLibRequired(ExtraOptions),
_kmsProviders,
_schemaMap);

Expand Down
12 changes: 9 additions & 3 deletions src/MongoDB.Driver/Encryption/MongocryptdFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
using System.IO;
using System.Linq;
using System.Reflection;
using MongoDB.Driver.Core;
using MongoDB.Driver.Core.Misc;

namespace MongoDB.Driver.Encryption
{
internal static class EncryptionExtraOptionsValidator
internal static class EncryptionExtraOptionsHelper
{
#region static
private static readonly Dictionary<string, Type[]> __supportedExtraOptions = new Dictionary<string, Type[]>
{
{ "cryptSharedLibPath", new [] { typeof(string) } },
Expand All @@ -36,7 +36,6 @@ internal static class EncryptionExtraOptionsValidator
{ "mongocryptdSpawnPath", new [] { typeof(string) } },
{ "mongocryptdSpawnArgs", new [] { typeof(string), typeof(IEnumerable<string>) } }
};
#endregion

public static void EnsureThatExtraOptionsAreValid(IReadOnlyDictionary<string, object> extraOptions)
{
Expand All @@ -63,6 +62,13 @@ public static void EnsureThatExtraOptionsAreValid(IReadOnlyDictionary<string, ob
}
}
}

public static string ExtractCryptSharedLibPath(IReadOnlyDictionary<string, object> dict) =>
dict.GetValueOrDefault<string, string, object>("cryptSharedLibPath");

public static bool? ExtractCryptSharedLibRequired(IReadOnlyDictionary<string, object> dict) =>
dict.GetValueOrDefault<bool?, string, object>("cryptSharedLibRequired");

}

internal class MongocryptdFactory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
Expand All @@ -34,7 +35,6 @@
using MongoDB.Driver.Core.Authentication.External;
using MongoDB.Driver.Core.Bindings;
using MongoDB.Driver.Core.Clusters;
using MongoDB.Driver.Core.Configuration;
using MongoDB.Driver.Core.Events;
using MongoDB.Driver.Core.Misc;
using MongoDB.Driver.Core.Operations;
Expand Down Expand Up @@ -271,36 +271,28 @@ public void BypassSpawningMongocryptdViaMongocryptdBypassSpawnTest(
kmsProviderFilter: "local",
extraOptions: extraOptions))
{
var datakeys = GetCollection(client, __keyVaultCollectionNamespace);
var externalKey = JsonFileReader.Instance.Documents["external.external-key.json"];
Insert(datakeys, async, externalKey);

var coll = GetCollection(clientEncrypted, __collCollectionNamespace);
var exception = Record.Exception(() => Insert(coll, async, new BsonDocument("encrypted", "test")));

AssertInnerEncryptionException<TimeoutException>(exception, "A timeout occurred after 10000ms selecting a server");
}
}

public enum BypassSpawningMongocryptd
{
BypassAutoEncryption,
BypassQueryAnalysis,
SharedLibrary
}

[SkippableTheory]
[ParameterAttributeData]
public void BypassSpawningMongocryptdTest(
[Values(false, true)] bool bypassAutoEncryption, // true - BypassAutoEncryption, false - BypassQueryAnalysis
[Values(BypassSpawningMongocryptd.BypassQueryAnalysis, BypassSpawningMongocryptd.BypassAutoEncryption, BypassSpawningMongocryptd.SharedLibrary)] BypassSpawningMongocryptd bypassSpawning,
[Values(false, true)] bool async)
{
RequireServer.Check().Supports(Feature.ClientSideEncryption);
RequireEnvironment.Check().EnvironmentVariable("CRYPT_SHARED_LIB_PATH", isDefined: false);

var extraOptions = new Dictionary<string, object>
{
{ "mongocryptdSpawnArgs", new [] { "--pidfilepath=bypass-spawning-mongocryptd.pid", "--port=27021" } },
};
using (var mongocryptdClient = new DisposableMongoClient(new MongoClient("mongodb://localhost:27021/?serverSelectionTimeoutMS=10000"), CreateLogger<DisposableMongoClient>()))
using (var clientEncrypted = ConfigureClientEncrypted(
kmsProviderFilter: "local",
bypassAutoEncryption: bypassAutoEncryption, // bypass options are mutually exclusive for this test
bypassQueryAnalysis: !bypassAutoEncryption,
extraOptions: extraOptions))
using (var clientEncrypted = EnsureEnvironmentAndConfigureTestClientEncrypted())
using (var mongocryptdClient = new DisposableMongoClient(new MongoClient("mongodb://localhost:27021/?serverSelectionTimeoutMS=1000"), CreateLogger<DisposableMongoClient>()))
{
var coll = GetCollection(clientEncrypted, __collCollectionNamespace);
Insert(coll, async, new BsonDocument("unencrypted", "test"));
Expand All @@ -310,7 +302,43 @@ public void BypassSpawningMongocryptdTest(
var exception = Record.Exception(() => adminDatabase.RunCommand<BsonDocument>(legacyHelloCommand));

exception.Should().BeOfType<TimeoutException>();
exception.Message.Should().Contain("A timeout occurred after 10000ms selecting a server");
exception.Message.Should().Contain("A timeout occurred after 1000ms selecting a server").And.Contain("localhost:27021");
}

DisposableMongoClient EnsureEnvironmentAndConfigureTestClientEncrypted()
{
var extraOptions = new Dictionary<string, object>
{
{ "mongocryptdSpawnArgs", new [] { "--pidfilepath=bypass-spawning-mongocryptd.pid", "--port=27021" } },
};
var kmsProvider = "local";
switch (bypassSpawning)
{
case BypassSpawningMongocryptd.BypassAutoEncryption:
RequireServer.Check().Supports(Feature.ClientSideEncryption);
RequireEnvironment.Check().EnvironmentVariable("CRYPT_SHARED_LIB_PATH", isDefined: false);
return ConfigureClientEncrypted(kmsProviderFilter: kmsProvider, bypassAutoEncryption: true, extraOptions: extraOptions);
case BypassSpawningMongocryptd.BypassQueryAnalysis:
RequireServer.Check().Supports(Feature.ClientSideEncryption);
RequireEnvironment.Check().EnvironmentVariable("CRYPT_SHARED_LIB_PATH", isDefined: false);
return ConfigureClientEncrypted(kmsProviderFilter: kmsProvider, bypassQueryAnalysis: true, extraOptions: extraOptions);
case BypassSpawningMongocryptd.SharedLibrary:
{
RequireServer.Check().Supports(Feature.Csfle2).ClusterTypes(ClusterType.ReplicaSet, ClusterType.Sharded, ClusterType.LoadBalanced);
RequireEnvironment.Check().EnvironmentVariable("CRYPT_SHARED_LIB_PATH", isDefined: true, allowEmpty: false);
var clientEncryptedSchema = new BsonDocument("db.coll", JsonFileReader.Instance.Documents["external.external-schema.json"]);
var cryptSharedPath = CoreTestConfiguration.GetCryptSharedLibPath();
Ensure.That(File.Exists(cryptSharedPath), $"Shared library path {cryptSharedPath} is not valid.");
var effectiveExtraOptions = new Dictionary<string, object>(extraOptions)
Copy link
Contributor

Choose a reason for hiding this comment

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

Where effectiveExtraOptions is used?

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, fixed. The test actually behaves almost the same even without this configuration because a shared library is configured by default, but it's definitely better to have it configured explicitly.

{
{ "mongocryptdURI", "mongodb://localhost:27021/db?serverSelectionTimeoutMS=1000" },
{ "cryptSharedLibPath", cryptSharedPath },
{ "cryptSharedRequired", true }
};
return ConfigureClientEncrypted(kmsProviderFilter: kmsProvider, schemaMap: clientEncryptedSchema, extraOptions: effectiveExtraOptions);
}
default: throw new Exception($"Invalid bypass mongocryptd {bypassSpawning} option.");
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: ArgumentOutOfRangeException?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this file, we use Exception in other places. I don't have strong preference in general what exception better to use, but mostly in tests we use just Exception

}
}
}

Expand Down