Skip to content

Commit 48cb9c8

Browse files
CSHARP-2896: Validate that mongocryptd is not spawned if bypassAutoEncryption=true
1 parent 7fc793b commit 48cb9c8

File tree

1 file changed

+78
-7
lines changed

1 file changed

+78
-7
lines changed

tests/MongoDB.Driver.Tests/Specifications/client-side-encryption/prose-tests/ClientEncryptionProseTests.cs

Lines changed: 78 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,67 @@ public void BsonSizeLimitAndBatchSizeSplittingTest(
234234
}
235235
}
236236

237+
[SkippableTheory]
238+
[ParameterAttributeData]
239+
public void BypassSpawningMongocryptdViaMongocryptdBypassSpawnTest(
240+
[Values(false, true)] bool async)
241+
{
242+
RequireServer.Check().Supports(Feature.ClientSideEncryption);
243+
244+
var extraOptions = new Dictionary<string, object>
245+
{
246+
{ "mongocryptdBypassSpawn", true },
247+
{ "mongocryptdURI", "mongodb://localhost:27021/db?serverSelectionTimeoutMS=1000" },
248+
{ "mongocryptdSpawnArgs", new [] { "--pidfilepath=bypass-spawning-mongocryptd.pid", "--port=27021" } },
249+
};
250+
var clientEncryptedSchema = new BsonDocument("db.coll", JsonFileReader.Instance.Documents["external.external-schema.json"]);
251+
using (var client = ConfigureClient())
252+
using (var clientEncrypted = ConfigureClientEncrypted(
253+
schemaMap: clientEncryptedSchema,
254+
kmsProviderFilter: "local",
255+
extraOptions: extraOptions))
256+
{
257+
var datakeys = GetCollection(client, __keyVaultCollectionNamespace);
258+
var externalKey = JsonFileReader.Instance.Documents["external.external-key.json"];
259+
Insert(datakeys, async, externalKey);
260+
261+
var coll = GetCollection(clientEncrypted, __collCollectionNamespace);
262+
var exception = Record.Exception(() => Insert(coll, async, new BsonDocument("encrypted", "test")));
263+
264+
exception.Should().BeOfType<MongoEncryptionException>();
265+
exception.Message.Should().Contain("A timeout occured after 1000ms selecting a server");
266+
}
267+
}
268+
269+
[SkippableTheory]
270+
[ParameterAttributeData]
271+
public void BypassSpawningMongocryptdViaBypassAutoEncryptionTest(
272+
[Values(false, true)] bool async)
273+
{
274+
RequireServer.Check().Supports(Feature.ClientSideEncryption);
275+
276+
var extraOptions = new Dictionary<string, object>
277+
{
278+
{ "mongocryptdSpawnArgs", new [] { "--pidfilepath=bypass-spawning-mongocryptd.pid", "--port=27021" } },
279+
};
280+
using (var mongocryptdClient = new DisposableMongoClient(new MongoClient("mongodb://localhost:27021/?serverSelectionTimeoutMS=1000")))
281+
using (var clientEncrypted = ConfigureClientEncrypted(
282+
kmsProviderFilter: "local",
283+
bypassAutoEncryption: true,
284+
extraOptions: extraOptions))
285+
{
286+
var coll = GetCollection(clientEncrypted, __collCollectionNamespace);
287+
Insert(coll, async, new BsonDocument("unencrypted", "test"));
288+
289+
var adminDatabase = mongocryptdClient.GetDatabase(DatabaseNamespace.Admin.DatabaseName);
290+
var isMasterCommand = new BsonDocument("ismaster", 1);
291+
var exception = Record.Exception(() => adminDatabase.RunCommand<BsonDocument>(isMasterCommand));
292+
293+
exception.Should().BeOfType<TimeoutException>();
294+
exception.Message.Should().Contain("A timeout occured after 1000ms selecting a server");
295+
}
296+
}
297+
237298
[SkippableTheory]
238299
[ParameterAttributeData]
239300
public void CorpusTest(
@@ -641,7 +702,9 @@ private DisposableMongoClient ConfigureClientEncrypted(
641702
BsonDocument schemaMap = null,
642703
bool withExternalKeyVault = false,
643704
string kmsProviderFilter = null,
644-
EventCapturer eventCapturer = null)
705+
EventCapturer eventCapturer = null,
706+
Dictionary<string, object> extraOptions = null,
707+
bool bypassAutoEncryption = false)
645708
{
646709
var kmsProviders = GetKmsProviders();
647710

@@ -659,7 +722,9 @@ private DisposableMongoClient ConfigureClientEncrypted(
659722
clusterConfigurator:
660723
eventCapturer != null
661724
? c => c.Subscribe(eventCapturer)
662-
: (Action<ClusterBuilder>)null);
725+
: (Action<ClusterBuilder>)null,
726+
extraOptions: extraOptions,
727+
bypassAutoEncryption: bypassAutoEncryption);
663728
return clientEncrypted;
664729
}
665730

@@ -730,7 +795,9 @@ private DisposableMongoClient CreateMongoClient(
730795
BsonDocument schemaMapDocument = null,
731796
IReadOnlyDictionary<string, IReadOnlyDictionary<string, object>> kmsProviders = null,
732797
bool withExternalKeyVault = false,
733-
Action<ClusterBuilder> clusterConfigurator = null)
798+
Action<ClusterBuilder> clusterConfigurator = null,
799+
Dictionary<string, object> extraOptions = null,
800+
bool bypassAutoEncryption = false)
734801
{
735802
var mongoClientSettings = DriverTestConfiguration.GetClientSettings().Clone();
736803
#pragma warning disable 618
@@ -743,10 +810,13 @@ private DisposableMongoClient CreateMongoClient(
743810

744811
if (keyVaultNamespace != null || schemaMapDocument != null || kmsProviders != null || withExternalKeyVault)
745812
{
746-
var extraOptions = new Dictionary<string, object>()
813+
if (extraOptions == null)
747814
{
748-
{ "mongocryptdSpawnPath", Environment.GetEnvironmentVariable("MONGODB_BINARIES") ?? string.Empty }
749-
};
815+
extraOptions = new Dictionary<string, object>()
816+
{
817+
{ "mongocryptdSpawnPath", Environment.GetEnvironmentVariable("MONGODB_BINARIES") ?? string.Empty }
818+
};
819+
}
750820

751821
var schemaMap = GetSchemaMapIfNotNull(schemaMapDocument);
752822

@@ -759,7 +829,8 @@ private DisposableMongoClient CreateMongoClient(
759829
keyVaultNamespace: keyVaultNamespace,
760830
kmsProviders: kmsProviders,
761831
schemaMap: schemaMap,
762-
extraOptions: extraOptions);
832+
extraOptions: extraOptions,
833+
bypassAutoEncryption: bypassAutoEncryption);
763834

764835
if (withExternalKeyVault)
765836
{

0 commit comments

Comments
 (0)