Skip to content

Commit 764ee97

Browse files
committed
Check wire version for Queryable Encryption support in CreateEncryptedCollection
1 parent 66b7a7d commit 764ee97

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/Database.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ public function createCollection(string $collectionName, array $options = [])
313313
* @return array A tuple containing the command result document from creating the collection and the modified "encryptedFields" option
314314
* @throws InvalidArgumentException for parameter/option parsing errors
315315
* @throws CreateEncryptedCollectionException for any errors creating data keys or creating the collection
316+
* @throws UnsupportedException if Queryable Encryption is not supported by the selected server
316317
*/
317318
public function createEncryptedCollection(string $collectionName, ClientEncryption $clientEncryption, string $kmsProvider, ?array $masterKey, array $options): array
318319
{

src/Operation/CreateEncryptedCollection.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
2323
use MongoDB\Driver\Server;
2424
use MongoDB\Exception\InvalidArgumentException;
25+
use MongoDB\Exception\UnsupportedException;
2526

2627
use function array_key_exists;
2728
use function is_array;
2829
use function is_object;
30+
use function MongoDB\server_supports_feature;
2931

3032
/**
3133
* Create an encrypted collection.
@@ -44,6 +46,9 @@
4446
*/
4547
class CreateEncryptedCollection implements Executable
4648
{
49+
/** @var integer */
50+
private static $wireVersionForQueryableEncryptionV2 = 21;
51+
4752
/** @var CreateCollection */
4853
private $createCollection;
4954

@@ -149,9 +154,14 @@ public function createDataKeys(ClientEncryption $clientEncryption, string $kmsPr
149154
* @see Executable::execute()
150155
* @return array|object Command result document from creating the encrypted collection
151156
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
157+
* @throws UnsupportedException if the server does not support Queryable Encryption
152158
*/
153159
public function execute(Server $server)
154160
{
161+
if (! server_supports_feature($server, self::$wireVersionForQueryableEncryptionV2)) {
162+
throw new UnsupportedException('Driver support of Queryable Encryption is incompatible with server. Upgrade server to use Queryable Encryption.');
163+
}
164+
155165
foreach ($this->createMetadataCollections as $createMetadataCollection) {
156166
$createMetadataCollection->execute($server);
157167
}

0 commit comments

Comments
 (0)