-
Notifications
You must be signed in to change notification settings - Fork 266
PHPLIB-1702: Always consult server encryptedFieldsMap when dropping collections with autoEncryption
enabled
#1745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
97284ec
8ef5fe6
878e5cd
bbc1700
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,8 @@ class Client | |
|
||
private WriteConcern $writeConcern; | ||
|
||
private bool $autoEncryptionEnabled; | ||
|
||
/** | ||
* Constructs a new Client instance. | ||
* | ||
|
@@ -134,6 +136,7 @@ public function __construct(?string $uri = null, array $uriOptions = [], array $ | |
$this->uri = $uri ?? self::DEFAULT_URI; | ||
$this->builderEncoder = $driverOptions['builderEncoder'] ?? new BuilderEncoder(); | ||
$this->typeMap = $driverOptions['typeMap']; | ||
$this->autoEncryptionEnabled = isset($driverOptions['autoEncryption']['keyVaultNamespace']); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Noted that
|
||
|
||
$driverOptions = array_diff_key($driverOptions, ['builderEncoder' => 1, 'typeMap' => 1]); | ||
|
||
|
@@ -258,7 +261,7 @@ public function dropDatabase(string $databaseName, array $options = []) | |
*/ | ||
public function getCollection(string $databaseName, string $collectionName, array $options = []): Collection | ||
{ | ||
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder]; | ||
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder, 'autoEncryptionEnabled' => $this->autoEncryptionEnabled]; | ||
|
||
return new Collection($this->manager, $databaseName, $collectionName, $options); | ||
} | ||
|
@@ -273,7 +276,7 @@ public function getCollection(string $databaseName, string $collectionName, arra | |
*/ | ||
public function getDatabase(string $databaseName, array $options = []): Database | ||
{ | ||
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder]; | ||
$options += ['typeMap' => $this->typeMap, 'builderEncoder' => $this->builderEncoder, 'autoEncryptionEnabled' => $this->autoEncryptionEnabled]; | ||
|
||
return new Database($this->manager, $databaseName, $options); | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,13 +201,8 @@ function get_encrypted_fields_from_driver(string $databaseName, string $collecti | |
* @see Database::dropCollection() | ||
* @return array|object|null | ||
*/ | ||
function get_encrypted_fields_from_server(string $databaseName, string $collectionName, Manager $manager, Server $server) | ||
function get_encrypted_fields_from_server(string $databaseName, string $collectionName, Server $server) | ||
{ | ||
// No-op if the encryptedFieldsMap autoEncryption driver option was omitted | ||
if ($manager->getEncryptedFieldsMap() === null) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ideally, it seems like we'd want the Manager to be able to report whether autoEncryption is enabled. |
||
return null; | ||
} | ||
|
||
$collectionInfoIterator = (new ListCollections($databaseName, ['filter' => ['name' => $collectionName]]))->execute($server); | ||
|
||
foreach ($collectionInfoIterator as $collectionInfo) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace MongoDB\Tests\Functions; | ||
|
||
use MongoDB\BSON\Binary; | ||
use MongoDB\BSON\Regex; | ||
use MongoDB\Collection; | ||
use MongoDB\Database; | ||
use MongoDB\Driver\ClientEncryption; | ||
use MongoDB\Driver\WriteConcern; | ||
use MongoDB\Tests\FunctionalTestCase; | ||
|
||
use function preg_quote; | ||
use function str_repeat; | ||
|
||
class GetEncryptedFieldsFromServerFunctionalTest extends FunctionalTestCase | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
private ClientEncryption $clientEncryption; | ||
private Collection $keyVaultCollection; | ||
private Database $database; | ||
|
||
public function setUp(): void | ||
{ | ||
parent::setUp(); | ||
|
||
$this->skipIfClientSideEncryptionIsNotSupported(); | ||
|
||
if ($this->isStandalone()) { | ||
$this->markTestSkipped('Queryable encryption requires replica sets'); | ||
} | ||
|
||
$this->skipIfServerVersion('<', '7.0.0', 'Queryable encryption requires MongoDB 7.0 or later'); | ||
|
||
$encryptionOptions = [ | ||
'keyVaultNamespace' => 'keyvault.datakeys', | ||
'kmsProviders' => [ | ||
'local' => [ | ||
'key' => new Binary(str_repeat("\0", 96)), // 96-byte local master key | ||
], | ||
], | ||
]; | ||
$client = static::createTestClient(driverOptions: ['autoEncryption' => $encryptionOptions]); | ||
|
||
// Ensure the key vault collection is dropped before each test | ||
$this->keyVaultCollection = $client->getCollection('keyvault', 'datakeys', ['writeConcern' => new WriteConcern(WriteConcern::MAJORITY)]); | ||
$this->keyVaultCollection->drop(); | ||
|
||
$this->clientEncryption = $client->createClientEncryption($encryptionOptions); | ||
|
||
$this->database = $client->getDatabase($this->getDatabaseName()); | ||
} | ||
|
||
public function tearDown(): void | ||
{ | ||
$this->keyVaultCollection?->drop(); | ||
} | ||
|
||
/** @see https://jira.mongodb.org/browse/PHPLIB-1702 */ | ||
public function testDatabaseDropCollectionConsultsEncryptedFieldsFromServer(): void | ||
{ | ||
$this->database->createEncryptedCollection( | ||
$this->getCollectionName(), | ||
$this->clientEncryption, | ||
'local', | ||
null, | ||
['encryptedFields' => ['fields' => []]], | ||
); | ||
|
||
$this->assertCountCollections(3, $this->getCollectionName(), 'createEncryptedCollection should create three collections'); | ||
|
||
$this->database->dropCollection($this->getCollectionName()); | ||
|
||
$this->assertCountCollections(0, $this->getCollectionName()); | ||
} | ||
|
||
/** @see https://jira.mongodb.org/browse/PHPLIB-1702 */ | ||
public function testCollectionDropConsultsEncryptedFieldsFromServer(): void | ||
{ | ||
$this->database->createEncryptedCollection( | ||
$this->getCollectionName(), | ||
$this->clientEncryption, | ||
'local', | ||
null, | ||
['encryptedFields' => ['fields' => []]], | ||
); | ||
|
||
$this->assertCountCollections(3, $this->getCollectionName(), 'createEncryptedCollection should create three collections'); | ||
|
||
$this->database->getCollection($this->getCollectionName())->drop(); | ||
|
||
$this->assertCountCollections(0, $this->getCollectionName()); | ||
} | ||
|
||
private function assertCountCollections(int $expected, $collectionName, string $message = ''): void | ||
{ | ||
$collectionNames = $this->database->listCollectionNames([ | ||
'filter' => ['name' => new Regex(preg_quote($collectionName))], | ||
]); | ||
$this->assertCount($expected, $collectionNames, $message); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@alcaeus @jmikola This new property will not be documented in Database and Collection options.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created PHPC-2615 to allow us to fetch this from the Manager directly.