Skip to content

Commit 55ec999

Browse files
committed
PHPLIB-1702: Always consult server encryptedFieldsMap when dropping collections
1 parent ae3821a commit 55ec999

File tree

4 files changed

+97
-8
lines changed

4 files changed

+97
-8
lines changed

src/Collection.php

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

531531
if (! isset($options['encryptedFields'])) {
532532
$options['encryptedFields'] = get_encrypted_fields_from_driver($this->databaseName, $this->collectionName, $this->manager)
533-
?? get_encrypted_fields_from_server($this->databaseName, $this->collectionName, $this->manager, $server);
533+
?? get_encrypted_fields_from_server($this->databaseName, $this->collectionName, $server);
534534
}
535535

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

src/Database.php

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

413413
if (! isset($options['encryptedFields'])) {
414414
$options['encryptedFields'] = get_encrypted_fields_from_driver($this->databaseName, $collectionName, $this->manager)
415-
?? get_encrypted_fields_from_server($this->databaseName, $collectionName, $this->manager, $server);
415+
?? get_encrypted_fields_from_server($this->databaseName, $collectionName, $server);
416416
}
417417

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

src/functions.php

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

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

0 commit comments

Comments
 (0)