Skip to content

Commit 0ecfd24

Browse files
CSHARP-3065: Don't use admin database for FLE tests.
1 parent 8fb8c57 commit 0ecfd24

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+629
-570
lines changed

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

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ namespace MongoDB.Driver.Tests.Specifications.client_side_encryption
2828
{
2929
public class ClientSideEncryptionTestRunner : MongoClientJsonDrivenTestRunnerBase
3030
{
31+
#region static
32+
private static readonly CollectionNamespace __keyVaultCollectionNamespace = CollectionNamespace.FromFullName("keyvault.datakeys");
33+
#endregion
34+
3135
[SkippableTheory]
3236
[ClassData(typeof(TestCaseFactory))]
3337
public void Run(JsonDrivenTestCase testCase)
@@ -104,8 +108,8 @@ protected override void DropCollection(MongoClient client, string databaseName,
104108

105109
if (shared.Contains("key_vault_data"))
106110
{
107-
var adminDatabase = client.GetDatabase("admin").WithWriteConcern(WriteConcern.WMajority);
108-
adminDatabase.DropCollection("datakeys");
111+
var keyVaultDatabase = client.GetDatabase(__keyVaultCollectionNamespace.DatabaseNamespace.DatabaseName).WithWriteConcern(WriteConcern.WMajority);
112+
keyVaultDatabase.DropCollection(__keyVaultCollectionNamespace.CollectionName);
109113
}
110114
}
111115

@@ -115,14 +119,14 @@ protected override void InsertData(IMongoClient client, string databaseName, str
115119

116120
if (shared.TryGetValue("key_vault_data", out var keyVaultData))
117121
{
118-
var adminDatabase = client.GetDatabase("admin");
122+
var keyVaultDatabase = client.GetDatabase(__keyVaultCollectionNamespace.DatabaseNamespace.DatabaseName);
119123
var collectionSettings = new MongoCollectionSettings
120124
{
121125
AssignIdOnInsert = false,
122126
ReadConcern = ReadConcern.Majority,
123127
WriteConcern = WriteConcern.WMajority
124128
};
125-
var keyVaultCollection = adminDatabase.GetCollection<BsonDocument>("datakeys", collectionSettings);
129+
var keyVaultCollection = keyVaultDatabase.GetCollection<BsonDocument>(__keyVaultCollectionNamespace.CollectionName, collectionSettings);
126130
var keyVaultDocuments = keyVaultData.AsBsonArray.Select(c => c.AsBsonDocument);
127131
keyVaultCollection.InsertMany(keyVaultDocuments);
128132
}
@@ -159,15 +163,14 @@ protected override void VerifyCollectionData(IEnumerable<BsonDocument> expectedD
159163
// private methods
160164
private AutoEncryptionOptions ConfigureAutoEncryptionOptions(BsonDocument autoEncryptOpts)
161165
{
162-
var keyVaultCollectionNamespace = new CollectionNamespace("admin", "datakeys");
163166
var extraOptions = new Dictionary<string, object>()
164167
{
165168
{ "mongocryptdSpawnPath", Environment.GetEnvironmentVariable("MONGODB_BINARIES") ?? string.Empty }
166169
};
167170

168171
var kmsProviders = new ReadOnlyDictionary<string, IReadOnlyDictionary<string, object>>(new Dictionary<string, IReadOnlyDictionary<string, object>>());
169172
var autoEncryptionOptions = new AutoEncryptionOptions(
170-
keyVaultNamespace: keyVaultCollectionNamespace,
173+
keyVaultNamespace: __keyVaultCollectionNamespace,
171174
kmsProviders: kmsProviders,
172175
extraOptions: extraOptions);
173176

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class ClientEncryptionProseTests
4242
{
4343
#region static
4444
private static readonly CollectionNamespace __collCollectionNamespace = CollectionNamespace.FromFullName("db.coll");
45-
private static readonly CollectionNamespace __keyVaultCollectionNamespace = CollectionNamespace.FromFullName("admin.datakeys");
45+
private static readonly CollectionNamespace __keyVaultCollectionNamespace = CollectionNamespace.FromFullName("keyvault.datakeys");
4646
#endregion
4747

4848
private const string LocalMasterKey = "Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk";
@@ -629,8 +629,8 @@ private DisposableMongoClient ConfigureClient(bool clearCollections = true)
629629
var client = CreateMongoClient();
630630
if (clearCollections)
631631
{
632-
var clientAdminDatabase = client.GetDatabase(__keyVaultCollectionNamespace.DatabaseNamespace.DatabaseName);
633-
clientAdminDatabase.DropCollection(__keyVaultCollectionNamespace.CollectionName);
632+
var clientKeyVaultDatabase = client.GetDatabase(__keyVaultCollectionNamespace.DatabaseNamespace.DatabaseName);
633+
clientKeyVaultDatabase.DropCollection(__keyVaultCollectionNamespace.CollectionName);
634634
var clientDbDatabase = client.GetDatabase(__collCollectionNamespace.DatabaseNamespace.DatabaseName);
635635
clientDbDatabase.DropCollection(__collCollectionNamespace.CollectionName);
636636
}

tests/MongoDB.Driver.Tests/Specifications/client-side-encryption/tests/README.rst

Lines changed: 85 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ Each YAML file has the following keys:
8383

8484
- ``schemaMap``: Optional, a map from namespaces to local JSON schemas.
8585

86-
- ``keyVaultNamespace``: Optional, a namespace to the key vault collection. Defaults to "admin.datakeys".
86+
- ``keyVaultNamespace``: Optional, a namespace to the key vault collection. Defaults to "keyvault.datakeys".
8787

8888
- ``bypassAutoEncryption``: Optional, a boolean to indicate whether or not auto encryption should be bypassed. Defaults to ``false``.
8989

@@ -124,8 +124,8 @@ Then for each element in ``tests``:
124124
#. If the ``skipReason`` field is present, skip this test completely.
125125
#. If the ``key_vault_data`` field is present:
126126

127-
#. Drop the ``admin.datakeys`` collection using writeConcern "majority".
128-
#. Insert the data specified into the ``admin.datakeys`` with write concern "majority".
127+
#. Drop the ``keyvault.datakeys`` collection using writeConcern "majority".
128+
#. Insert the data specified into the ``keyvault.datakeys`` with write concern "majority".
129129

130130
#. Create a MongoClient.
131131

@@ -144,7 +144,7 @@ Then for each element in ``tests``:
144144
#. Create a **new** MongoClient using ``clientOptions``.
145145

146146
#. If ``autoEncryptOpts`` includes ``aws`` as a KMS provider, pass in AWS credentials from the environment.
147-
#. If ``autoEncryptOpts`` does not include ``keyVaultNamespace``, default it to ``admin.datakeys``.
147+
#. If ``autoEncryptOpts`` does not include ``keyVaultNamespace``, default it to ``keyvault.datakeys``.
148148

149149
#. For each element in ``operations``:
150150

@@ -208,14 +208,16 @@ In the prose tests LOCAL_MASTERKEY refers to the following base64:
208208
209209
Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk
210210
211+
Perform all applicable operations on key vault collections (e.g. inserting an example data key, or running a find command) with readConcern/writeConcern "majority".
212+
211213
Data key and double encryption
212214
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
213215

214216
First, perform the setup.
215217

216218
#. Create a MongoClient without encryption enabled (referred to as ``client``). Enable command monitoring to listen for command_started events.
217219

218-
#. Using ``client``, drop the collections ``admin.datakeys`` and ``db.coll``.
220+
#. Using ``client``, drop the collections ``keyvault.datakeys`` and ``db.coll``.
219221

220222
#. Create the following:
221223

@@ -231,7 +233,7 @@ First, perform the setup.
231233
"local": { "key": <base64 decoding of LOCAL_MASTERKEY> }
232234
}
233235
234-
Configure both objects with ``keyVaultNamespace`` set to ``admin.datakeys``.
236+
Configure both objects with ``keyVaultNamespace`` set to ``keyvault.datakeys``.
235237

236238
Configure the ``MongoClient`` with the following ``schema_map``:
237239

@@ -259,7 +261,7 @@ Then, test creating and using data keys from a ``local`` KMS provider:
259261
#. Call ``client_encryption.createDataKey()`` with the ``local`` KMS provider and keyAltNames set to ``["local_altname"]``.
260262

261263
- Expect a BSON binary with subtype 4 to be returned, referred to as ``local_datakey_id``.
262-
- Use ``client`` to run a ``find`` on ``admin.datakeys`` by querying with the ``_id`` set to the ``local_datakey_id``.
264+
- Use ``client`` to run a ``find`` on ``keyvault.datakeys`` by querying with the ``_id`` set to the ``local_datakey_id``.
263265
- Expect that exactly one document is returned with the "masterKey.provider" equal to "local".
264266
- Check that ``client`` captured a command_started event for the ``insert`` command containing a majority writeConcern.
265267

@@ -286,7 +288,7 @@ Then, repeat the above tests with the ``aws`` KMS provider:
286288
287289
288290
- Expect a BSON binary with subtype 4 to be returned, referred to as ``aws_datakey_id``.
289-
- Use ``client`` to run a ``find`` on ``admin.datakeys`` by querying with the ``_id`` set to the ``aws_datakey_id``.
291+
- Use ``client`` to run a ``find`` on ``keyvault.datakeys`` by querying with the ``_id`` set to the ``aws_datakey_id``.
290292
- Expect that exactly one document is returned with the "masterKey.provider" equal to "aws".
291293
- Check that ``client`` captured a command_started event for the ``insert`` command containing a majority writeConcern.
292294

@@ -317,8 +319,8 @@ Run the following tests twice, parameterized by a boolean ``withExternalKeyVault
317319

318320
#. Create a MongoClient without encryption enabled (referred to as ``client``).
319321

320-
#. Using ``client``, drop the collections ``admin.datakeys`` and ``db.coll``.
321-
Insert the document `external/external-key.json <../external/external-key.json>`_ into ``admin.datakeys``.
322+
#. Using ``client``, drop the collections ``keyvault.datakeys`` and ``db.coll``.
323+
Insert the document `external/external-key.json <../external/external-key.json>`_ into ``keyvault.datakeys``.
322324

323325
#. Create the following:
324326

@@ -331,7 +333,7 @@ Run the following tests twice, parameterized by a boolean ``withExternalKeyVault
331333
332334
{ "local": { "key": <base64 decoding of LOCAL_MASTERKEY> } }
333335
334-
Configure both objects with ``keyVaultNamespace`` set to ``admin.datakeys``.
336+
Configure both objects with ``keyVaultNamespace`` set to ``keyvault.datakeys``.
335337

336338
Configure ``client_encrypted`` to use the schema `external/external-schema.json <../external/external-schema.json>`_ for ``db.coll`` by setting a schema map like: ``{ "db.coll": <contents of external-schema.json>}``
337339

@@ -354,7 +356,7 @@ First, perform the setup.
354356

355357
#. Using ``client``, drop and create the collection ``db.coll`` configured with the included JSON schema `limits/limits-schema.json <../limits/limits-schema.json>`_.
356358

357-
#. Using ``client``, drop the collection ``admin.datakeys``. Insert the document `limits/limits-key.json <../limits/limits-key.json>`_
359+
#. Using ``client``, drop the collection ``keyvault.datakeys``. Insert the document `limits/limits-key.json <../limits/limits-key.json>`_
358360

359361
#. Create a MongoClient configured with auto encryption (referred to as ``client_encrypted``)
360362

@@ -364,7 +366,7 @@ First, perform the setup.
364366
365367
{ "local": { "key": <base64 decoding of LOCAL_MASTERKEY> } }
366368
367-
Configure with the ``keyVaultNamespace`` set to ``admin.datakeys``.
369+
Configure with the ``keyVaultNamespace`` set to ``keyvault.datakeys``.
368370

369371
Using ``client_encrypted`` perform the following operations:
370372

@@ -420,21 +422,21 @@ Views are prohibited
420422
421423
{ "local": { "key": <base64 decoding of LOCAL_MASTERKEY> } }
422424
423-
Configure with the ``keyVaultNamespace`` set to ``admin.datakeys``.
425+
Configure with the ``keyVaultNamespace`` set to ``keyvault.datakeys``.
424426

425427
#. Using ``client_encrypted``, attempt to insert a document into ``db.view``. Expect an exception to be thrown containing the message: "cannot auto encrypt a view".
426428

427429

428430
Corpus Test
429-
===========
431+
~~~~~~~~~~~
430432

431433
The corpus test exhaustively enumerates all ways to encrypt all BSON value types. Note, the test data includes BSON binary subtype 4 (or standard UUID), which MUST be decoded and encoded as subtype 4. Run the test as follows.
432434

433435
1. Create a MongoClient without encryption enabled (referred to as ``client``).
434436

435437
2. Using ``client``, drop and create the collection ``db.coll`` configured with the included JSON schema `corpus/corpus-schema.json <../corpus/corpus-schema.json>`_.
436438

437-
3. Using ``client``, drop the collection ``admin.datakeys``. Insert the documents `corpus/corpus-key-local.json <../corpus/corpus-key-local.json>`_ and `corpus/corpus-key-aws.json <../corpus/corpus-key-aws.json>`_.
439+
3. Using ``client``, drop the collection ``keyvault.datakeys``. Insert the documents `corpus/corpus-key-local.json <../corpus/corpus-key-local.json>`_ and `corpus/corpus-key-aws.json <../corpus/corpus-key-aws.json>`_.
438440

439441
4. Create the following:
440442

@@ -456,7 +458,7 @@ The corpus test exhaustively enumerates all ways to encrypt all BSON value types
456458
457459
Mng0NCt4ZHVUYUJCa1kxNkVyNUR1QURhZ2h2UzR2d2RrZzh0cFBwM3R6NmdWMDFBMUN3YkQ5aXRRMkhGRGdQV09wOGVNYUMxT2k3NjZKelhaQmRCZGJkTXVyZG9uSjFk
458460
459-
Configure both objects with ``keyVaultNamespace`` set to ``admin.datakeys``.
461+
Configure both objects with ``keyVaultNamespace`` set to ``keyvault.datakeys``.
460462

461463
5. Load `corpus/corpus.json <../corpus/corpus.json>`_ to a variable named ``corpus``. The corpus contains subdocuments with the following fields:
462464

@@ -507,7 +509,7 @@ The corpus test exhaustively enumerates all ways to encrypt all BSON value types
507509
9. Repeat steps 1-8 with a local JSON schema. I.e. amend step 4 to configure the schema on ``client_encrypted`` with the ``schema_map`` option.
508510

509511
Custom Endpoint Test
510-
====================
512+
~~~~~~~~~~~~~~~~~~~~
511513

512514
Data keys created with AWS KMS may specify a custom endpoint to contact (instead of the default endpoint derived from the AWS region).
513515

@@ -521,7 +523,7 @@ Data keys created with AWS KMS may specify a custom endpoint to contact (instead
521523
"aws": { <AWS credentials> }
522524
}
523525
524-
Configure with ``keyVaultNamespace`` set to ``admin.datakeys``, and a default MongoClient as the ``keyVaultClient``.
526+
Configure with ``keyVaultNamespace`` set to ``keyvault.datakeys``, and a default MongoClient as the ``keyVaultClient``.
525527

526528
2. Call `client_encryption.createDataKey()` with "aws" as the provider and the following masterKey:
527529

@@ -594,3 +596,67 @@ Data keys created with AWS KMS may specify a custom endpoint to contact (instead
594596
595597
Expect this to fail with an exception with a message containing the string: "parse error"
596598

599+
Bypass spawning mongocryptd
600+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
601+
602+
Via mongocryptdBypassSpawn
603+
``````````````````````````
604+
605+
The following tests that setting ``mongocryptdBypassSpawn=true`` really does bypass spawning mongocryptd.
606+
607+
#. Create a MongoClient configured with auto encryption (referred to as ``client_encrypted``)
608+
609+
Configure the required options. Use the ``local`` KMS provider as follows:
610+
611+
.. code:: javascript
612+
613+
{ "local": { "key": <base64 decoding of LOCAL_MASTERKEY> } }
614+
615+
Configure with the ``keyVaultNamespace`` set to ``keyvault.datakeys``.
616+
617+
Configure ``client_encrypted`` to use the schema `external/external-schema.json <../external/external-schema.json>`_ for ``db.coll`` by setting a schema map like: ``{ "db.coll": <contents of external-schema.json>}``
618+
619+
Configure the following ``extraOptions``:
620+
621+
.. code:: javascript
622+
623+
{
624+
"mongocryptdBypassSpawn": true
625+
"mongocryptdURI": "mongodb://localhost:27021/db?serverSelectionTimeoutMS=1000",
626+
"mongocryptdSpawnArgs": [ "--pidfilepath=bypass-spawning-mongocryptd.pid", "--port=27021"]
627+
}
628+
629+
Drivers MAY pass a different port if they expect their testing infrastructure to be using port 27021. Pass a port that should be free.
630+
631+
#. Use ``client_encrypted`` to insert the document ``{"encrypted": "test"}`` into ``db.coll``. Expect a server selection error propagated from the internal MongoClient failing to connect to mongocryptd on port 27021.
632+
633+
Via bypassAutoEncryption
634+
````````````````````````
635+
636+
The following tests that setting ``bypassAutoEncryption=true`` really does bypass spawning mongocryptd.
637+
638+
#. Create a MongoClient configured with auto encryption (referred to as ``client_encrypted``)
639+
640+
Configure the required options. Use the ``local`` KMS provider as follows:
641+
642+
.. code:: javascript
643+
644+
{ "local": { "key": <base64 decoding of LOCAL_MASTERKEY> } }
645+
646+
Configure with the ``keyVaultNamespace`` set to ``keyvault.datakeys``.
647+
648+
Configure with ``bypassAutoEncryption=true``.
649+
650+
Configure the following ``extraOptions``:
651+
652+
.. code:: javascript
653+
654+
{
655+
"mongocryptdSpawnArgs": [ "--pidfilepath=bypass-spawning-mongocryptd.pid", "--port=27021"]
656+
}
657+
658+
Drivers MAY pass a different value to ``--port`` if they expect their testing infrastructure to be using port 27021. Pass a port that should be free.
659+
660+
#. Use ``client_encrypted`` to insert the document ``{"unencrypted": "test"}`` into ``db.coll``. Expect this to succeed.
661+
662+
#. Validate that mongocryptd was not spawned. Create a MongoClient to localhost:27021 (or whatever was passed via ``--port``) with serverSelectionTimeoutMS=1000. Run an ``isMaster`` command and ensure it fails with a server selection timeout.

0 commit comments

Comments
 (0)