Skip to content

Commit 4aec9ee

Browse files
CSHARP-4335: Do not connect to mongocryptd if shared library is loaded. (#969)
1 parent f75c1fb commit 4aec9ee

File tree

1 file changed

+69
-1
lines changed

1 file changed

+69
-1
lines changed

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

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
using MongoDB.Driver.Core.Authentication.External;
3535
using MongoDB.Driver.Core.Bindings;
3636
using MongoDB.Driver.Core.Clusters;
37-
using MongoDB.Driver.Core.Configuration;
3837
using MongoDB.Driver.Core.Events;
3938
using MongoDB.Driver.Core.Misc;
4039
using MongoDB.Driver.Core.Operations;
@@ -250,6 +249,75 @@ public void BsonSizeLimitAndBatchSizeSplittingTest(
250249
}
251250
}
252251

252+
[SkippableTheory]
253+
[ParameterAttributeData]
254+
public void BypassMongocryptdClientWhenSharedLibraryTest(
255+
[Values(false, true)] bool async)
256+
{
257+
RequireServer.Check().Supports(Feature.ClientSideEncryption);
258+
RequireEnvironment.Check().EnvironmentVariable("CRYPT_SHARED_LIB_PATH", isDefined: true, allowEmpty: false);
259+
// socket.Close can hang on non windows OS. Might be related to this issue: https://github.com/dotnet/runtime/issues/47342
260+
RequirePlatform
261+
.Check()
262+
.SkipWhen(SupportedOperatingSystem.Linux)
263+
.SkipWhen(SupportedOperatingSystem.MacOS);
264+
265+
const int mongocryptPort = 27030;
266+
var timeout = TimeSpan.FromSeconds(3);
267+
var extraOptions = new Dictionary<string, object>
268+
{
269+
{ "mongocryptdURI", $"mongodb://localhost:{mongocryptPort}" }
270+
};
271+
272+
var mongocryptdIpAddress = IPAddress.Parse("127.0.0.1");
273+
TcpListener tcpListener = null;
274+
try
275+
{
276+
tcpListener = new TcpListener(mongocryptdIpAddress, port: mongocryptPort);
277+
var listenerThread = new Thread(new ParameterizedThreadStart(ThreadStart)) { IsBackground = true };
278+
279+
using (var clientEncrypted = ConfigureClientEncrypted(kmsProviderFilter: "local", extraOptions: extraOptions))
280+
{
281+
var coll = GetCollection(clientEncrypted, __collCollectionNamespace);
282+
283+
listenerThread.Start(tcpListener);
284+
285+
_ = Record.Exception(() => Insert(coll, async, new BsonDocument("unencrypted", "test")));
286+
287+
if (listenerThread.Join(timeout))
288+
{
289+
// This exception is never thrown when mognocryptd mongoClient is not spawned which is expected behavior.
290+
// However, if we intentionally break that logic to spawn mongocryptd mongoClient regardless of shared library,
291+
// this exception sometimes won't be thrown. In all such cases the spent time in listenerThread.Join is higher
292+
// or really close to timeout. So it's unclear why Join doesn't throw in that cases, but that logic is unrelated
293+
// to the driver and csfle in particular. We rely on the fact that even if we break this logic,
294+
// we run this test more than once.
295+
throw new Exception($"Listener accepted a tcp call for moncgocryptd during {timeout}.");
296+
}
297+
}
298+
}
299+
finally
300+
{
301+
tcpListener?.Stop();
302+
}
303+
304+
void ThreadStart(object param)
305+
{
306+
try
307+
{
308+
var tcpListener = (TcpListener)param;
309+
tcpListener.Start();
310+
using var client = tcpListener.AcceptTcpClient();
311+
// Perform a blocking call to accept requests.
312+
// if we're here, then something queries port 27030.
313+
}
314+
catch (SocketException)
315+
{
316+
// listener stopped outside thread
317+
}
318+
}
319+
}
320+
253321
[SkippableTheory]
254322
[ParameterAttributeData]
255323
public void BypassSpawningMongocryptdViaMongocryptdBypassSpawnTest(

0 commit comments

Comments
 (0)