Skip to content

Commit 9d66a37

Browse files
committed
PHPLIB-1702: Always consult server encryptedFieldsMap when dropping collections
1 parent 888d383 commit 9d66a37

File tree

5 files changed

+127
-8
lines changed

5 files changed

+127
-8
lines changed

src/Collection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ public function drop(array $options = []): void
513513

514514
if (! isset($options['encryptedFields'])) {
515515
$options['encryptedFields'] = get_encrypted_fields_from_driver($this->databaseName, $this->collectionName, $this->manager)
516-
?? get_encrypted_fields_from_server($this->databaseName, $this->collectionName, $this->manager, $server);
516+
?? get_encrypted_fields_from_server($this->databaseName, $this->collectionName, $server);
517517
}
518518

519519
$operation = isset($options['encryptedFields'])

src/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ public function dropCollection(string $collectionName, array $options = []): voi
374374

375375
if (! isset($options['encryptedFields'])) {
376376
$options['encryptedFields'] = get_encrypted_fields_from_driver($this->databaseName, $collectionName, $this->manager)
377-
?? get_encrypted_fields_from_server($this->databaseName, $collectionName, $this->manager, $server);
377+
?? get_encrypted_fields_from_server($this->databaseName, $collectionName, $server);
378378
}
379379

380380
$operation = isset($options['encryptedFields'])

src/functions.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,8 @@ function get_encrypted_fields_from_driver(string $databaseName, string $collecti
198198
* @see Collection::drop()
199199
* @see Database::dropCollection()
200200
*/
201-
function get_encrypted_fields_from_server(string $databaseName, string $collectionName, Manager $manager, Server $server): array|object|null
201+
function get_encrypted_fields_from_server(string $databaseName, string $collectionName, Server $server): array|object|null
202202
{
203-
// No-op if the encryptedFieldsMap autoEncryption driver option was omitted
204-
if ($manager->getEncryptedFieldsMap() === null) {
205-
return null;
206-
}
207-
208203
$collectionInfoIterator = (new ListCollections($databaseName, ['filter' => ['name' => $collectionName]]))->execute($server);
209204

210205
foreach ($collectionInfoIterator as $collectionInfo) {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Collection;
4+
5+
use MongoDB\BSON\Binary;
6+
use MongoDB\Database;
7+
use MongoDB\Driver\ClientEncryption;
8+
use MongoDB\Driver\WriteConcern;
9+
10+
use function iterator_count;
11+
use function str_repeat;
12+
13+
class DropEncryptedCollectionFunctionalTest extends FunctionalTestCase
14+
{
15+
protected ClientEncryption $clientEncryption;
16+
protected Database $database;
17+
18+
public function setUp(): void
19+
{
20+
parent::setUp();
21+
22+
$this->skipIfClientSideEncryptionIsNotSupported();
23+
24+
if ($this->isStandalone()) {
25+
$this->markTestSkipped('Queryable encryption requires replica sets');
26+
}
27+
28+
$this->skipIfServerVersion('<', '7.0.0', 'Queryable encryption requires MongoDB 7.0 or later');
29+
30+
$client = static::createTestClient();
31+
32+
// Ensure the key vault collection is dropped before each test
33+
$collection = $client->selectCollection('keyvault', 'datakeys', ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)]);
34+
$collection->drop();
35+
36+
$this->clientEncryption = $client->createClientEncryption([
37+
'keyVaultNamespace' => 'keyvault.datakeys',
38+
'kmsProviders' => ['local' => ['key' => new Binary(str_repeat("\0", 96)) ]],
39+
]);
40+
41+
$this->database = $client->getDatabase($this->getDatabaseName());
42+
}
43+
44+
/** @see https://jira.mongodb.org/browse/PHPLIB-1702 */
45+
public function testDropConsultsEncryptedFieldsFromServer(): void
46+
{
47+
$originalNumCollections = iterator_count($this->database->listCollectionNames());
48+
49+
$this->database->createEncryptedCollection(
50+
$this->getCollectionName(),
51+
$this->clientEncryption,
52+
'local',
53+
null,
54+
['encryptedFields' => ['fields' => []]],
55+
);
56+
57+
// createEncryptedCollection should create three collections
58+
$this->assertCount($originalNumCollections + 3, $this->database->listCollectionNames());
59+
60+
$this->collection->drop();
61+
62+
$this->assertCount($originalNumCollections, $this->database->listCollectionNames());
63+
}
64+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace MongoDB\Tests\Database;
4+
5+
use MongoDB\BSON\Binary;
6+
use MongoDB\Driver\ClientEncryption;
7+
use MongoDB\Driver\WriteConcern;
8+
9+
use function iterator_count;
10+
use function str_repeat;
11+
12+
class DropEncryptedCollectionFunctionalTest extends FunctionalTestCase
13+
{
14+
protected ClientEncryption $clientEncryption;
15+
16+
public function setUp(): void
17+
{
18+
parent::setUp();
19+
20+
$this->skipIfClientSideEncryptionIsNotSupported();
21+
22+
if ($this->isStandalone()) {
23+
$this->markTestSkipped('Queryable encryption requires replica sets');
24+
}
25+
26+
$this->skipIfServerVersion('<', '7.0.0', 'Queryable encryption requires MongoDB 7.0 or later');
27+
28+
$client = static::createTestClient();
29+
30+
// Ensure the key vault collection is dropped before each test
31+
$collection = $client->selectCollection('keyvault', 'datakeys', ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)]);
32+
$collection->drop();
33+
34+
$this->clientEncryption = $client->createClientEncryption([
35+
'keyVaultNamespace' => 'keyvault.datakeys',
36+
'kmsProviders' => ['local' => ['key' => new Binary(str_repeat("\0", 96)) ]],
37+
]);
38+
}
39+
40+
/** @see https://jira.mongodb.org/browse/PHPLIB-1702 */
41+
public function testDropCollectionConsultsEncryptedFieldsFromServer(): void
42+
{
43+
$originalNumCollections = iterator_count($this->database->listCollectionNames());
44+
45+
$this->database->createEncryptedCollection(
46+
$this->getCollectionName(),
47+
$this->clientEncryption,
48+
'local',
49+
null,
50+
['encryptedFields' => ['fields' => []]],
51+
);
52+
53+
// createEncryptedCollection should create three collections
54+
$this->assertCount($originalNumCollections + 3, $this->database->listCollectionNames());
55+
56+
$this->database->dropCollection($this->getCollectionName());
57+
58+
$this->assertCount($originalNumCollections, $this->database->listCollectionNames());
59+
}
60+
}

0 commit comments

Comments
 (0)