Skip to content

Commit 97284ec

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

File tree

4 files changed

+96
-8
lines changed

4 files changed

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

0 commit comments

Comments
 (0)