Skip to content

Merge v1.21 into v2.1 #1746

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

Merged
merged 2 commits into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ class Client

private WriteConcern $writeConcern;

private bool $autoEncryptionEnabled;

/**
* Constructs a new Client instance.
*
Expand Down Expand Up @@ -133,6 +135,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']);

$driverOptions = array_diff_key($driverOptions, ['builderEncoder' => 1, 'typeMap' => 1]);

Expand Down Expand Up @@ -270,7 +273,7 @@ public function dropDatabase(string $databaseName, array $options = []): void
*/
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);
}
Expand All @@ -285,7 +288,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);
}
Expand Down
12 changes: 10 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@
use function array_key_exists;
use function current;
use function is_array;
use function is_bool;
use function strlen;

class Collection
Expand All @@ -100,6 +101,8 @@ class Collection

private WriteConcern $writeConcern;

private bool $autoEncryptionEnabled;

/**
* Constructs new Collection instance.
*
Expand Down Expand Up @@ -167,12 +170,17 @@ public function __construct(private Manager $manager, private string $databaseNa
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class);
}

if (isset($options['autoEncryptionEnabled']) && ! is_bool($options['autoEncryptionEnabled'])) {
throw InvalidArgumentException::invalidType('"autoEncryptionEnabled" option', $options['autoEncryptionEnabled'], 'boolean');
}

$this->builderEncoder = $options['builderEncoder'] ?? new BuilderEncoder();
$this->codec = $options['codec'] ?? null;
$this->readConcern = $options['readConcern'] ?? $this->manager->getReadConcern();
$this->readPreference = $options['readPreference'] ?? $this->manager->getReadPreference();
$this->typeMap = $options['typeMap'] ?? self::DEFAULT_TYPE_MAP;
$this->writeConcern = $options['writeConcern'] ?? $this->manager->getWriteConcern();
$this->autoEncryptionEnabled = $options['autoEncryptionEnabled'] ?? false;
}

/**
Expand Down Expand Up @@ -511,9 +519,9 @@ public function drop(array $options = []): void

$server = select_server_for_write($this->manager, $options);

if (! isset($options['encryptedFields'])) {
if ($this->autoEncryptionEnabled && ! isset($options['encryptedFields'])) {
$options['encryptedFields'] = get_encrypted_fields_from_driver($this->databaseName, $this->collectionName, $this->manager)
?? get_encrypted_fields_from_server($this->databaseName, $this->collectionName, $this->manager, $server);
?? get_encrypted_fields_from_server($this->databaseName, $this->collectionName, $server);
}

$operation = isset($options['encryptedFields'])
Expand Down
13 changes: 11 additions & 2 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
use Throwable;

use function is_array;
use function is_bool;
use function strlen;

class Database
Expand All @@ -77,6 +78,8 @@ class Database

private WriteConcern $writeConcern;

private bool $autoEncryptionEnabled;

/**
* Constructs new Database instance.
*
Expand Down Expand Up @@ -133,11 +136,16 @@ public function __construct(private Manager $manager, private string $databaseNa
throw InvalidArgumentException::invalidType('"writeConcern" option', $options['writeConcern'], WriteConcern::class);
}

if (isset($options['autoEncryptionEnabled']) && ! is_bool($options['autoEncryptionEnabled'])) {
throw InvalidArgumentException::invalidType('"autoEncryptionEnabled" option', $options['autoEncryptionEnabled'], 'boolean');
}

$this->builderEncoder = $options['builderEncoder'] ?? new BuilderEncoder();
$this->readConcern = $options['readConcern'] ?? $this->manager->getReadConcern();
$this->readPreference = $options['readPreference'] ?? $this->manager->getReadPreference();
$this->typeMap = $options['typeMap'] ?? self::DEFAULT_TYPE_MAP;
$this->writeConcern = $options['writeConcern'] ?? $this->manager->getWriteConcern();
$this->autoEncryptionEnabled = $options['autoEncryptionEnabled'] ?? false;
}

/**
Expand Down Expand Up @@ -372,9 +380,9 @@ public function dropCollection(string $collectionName, array $options = []): voi
$options['writeConcern'] = $this->writeConcern;
}

if (! isset($options['encryptedFields'])) {
if ($this->autoEncryptionEnabled && ! isset($options['encryptedFields'])) {
$options['encryptedFields'] = get_encrypted_fields_from_driver($this->databaseName, $collectionName, $this->manager)
?? get_encrypted_fields_from_server($this->databaseName, $collectionName, $this->manager, $server);
?? get_encrypted_fields_from_server($this->databaseName, $collectionName, $server);
}

$operation = isset($options['encryptedFields'])
Expand All @@ -401,6 +409,7 @@ public function getCollection(string $collectionName, array $options = []): Coll
'readPreference' => $this->readPreference,
'typeMap' => $this->typeMap,
'writeConcern' => $this->writeConcern,
'autoEncryptionEnabled' => $this->autoEncryptionEnabled,
];

return new Collection($this->manager, $this->databaseName, $collectionName, $options);
Expand Down
7 changes: 1 addition & 6 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,13 +198,8 @@ function get_encrypted_fields_from_driver(string $databaseName, string $collecti
* @see Collection::drop()
* @see Database::dropCollection()
*/
function get_encrypted_fields_from_server(string $databaseName, string $collectionName, Manager $manager, Server $server): array|object|null
function get_encrypted_fields_from_server(string $databaseName, string $collectionName, Server $server): array|object|null
{
// No-op if the encryptedFieldsMap autoEncryption driver option was omitted
if ($manager->getEncryptedFieldsMap() === null) {
return null;
}

$collectionInfoIterator = (new ListCollections($databaseName, ['filter' => ['name' => $collectionName]]))->execute($server);

foreach ($collectionInfoIterator as $collectionInfo) {
Expand Down
101 changes: 101 additions & 0 deletions tests/Functions/GetEncryptedFieldsFromServerFunctionalTest.php
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
{
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);
}
}
2 changes: 1 addition & 1 deletion tests/drivers-evergreen-tools
Submodule drivers-evergreen-tools updated 72 files
+21 −3 .evergreen/auth_aws/README.md
+1 −7 .evergreen/auth_aws/activate-authawsvenv.sh
+15 −3 .evergreen/auth_aws/aws_setup.sh
+40 −4 .evergreen/auth_aws/aws_tester.py
+15 −0 .evergreen/auth_aws/lib/eks-pod-run-self-test.sh
+120 −0 .evergreen/auth_aws/lib/eks-pod-setup.sh
+11 −0 .evergreen/auth_aws/lib/eks-pod-teardown.sh
+12 −0 .evergreen/auth_aws/lib/eks_pod_self_test.py
+10 −0 .evergreen/auth_aws/lib/eks_pod_setup_server.js
+3 −0 .evergreen/auth_aws/requirements-in.txt
+91 −0 .evergreen/auth_aws/requirements.txt
+1 −7 .evergreen/auth_oidc/activate-authoidcvenv.sh
+2 −0 .evergreen/auth_oidc/azure/README.md
+2 −0 .evergreen/auth_oidc/gcp/README.md
+20 −1 .evergreen/auth_oidc/k8s/run-driver-test.sh
+5 −0 .evergreen/auth_oidc/requirements-in.txt
+116 −0 .evergreen/auth_oidc/requirements.txt
+7 −40 .evergreen/config.yml
+2 −10 .evergreen/csfle/activate-kmstlsvenv.sh
+2 −0 .evergreen/csfle/azurekms/README.md
+5 −0 .evergreen/csfle/azurekms/create-vm.sh
+1 −3 .evergreen/csfle/azurekms/delete_old_azure_resources.sh
+1 −0 .evergreen/csfle/azurekms/login.sh
+8 −2 .evergreen/csfle/azurekms/remote-scripts/setup-azure-vm.sh
+3 −0 .evergreen/csfle/azurekms/requirements-in.txt
+67 −0 .evergreen/csfle/azurekms/requirements.txt
+2 −0 .evergreen/csfle/gcpkms/README.md
+2 −2 .evergreen/csfle/gcpkms/remote-scripts/setup-gce-instance.sh
+2 −0 .evergreen/csfle/requirements-in-legacy.txt
+3 −0 .evergreen/csfle/requirements-in.txt
+53 −0 .evergreen/csfle/requirements-legacy.txt
+50 −0 .evergreen/csfle/requirements.txt
+1 −1 .evergreen/csfle/stop-servers.sh
+0 −1 .evergreen/csfle/teardown.sh
+1 −1 .evergreen/docker/login.py
+2 −0 .evergreen/docker/requirements-in.txt
+37 −2 .evergreen/docker/requirements.txt
+3 −3 .evergreen/docker/rhel8/Dockerfile
+5 −0 .evergreen/docker/run-server.sh
+5 −4 .evergreen/docker/ubuntu20.04/Dockerfile
+54 −49 .evergreen/ensure-binary.sh
+13 −12 .evergreen/github_app/package-lock.json
+16 −9 .evergreen/install-cli.sh
+8 −0 .evergreen/install-node.sh
+1 −1 .evergreen/k8s/gke/setup.sh
+28 −6 .evergreen/mongodl.py
+52 −0 .evergreen/mongoproxy/README.md
+6 −0 .evergreen/mongoproxy/build.sh
+42 −0 .evergreen/mongoproxy/cmd/mongoproxy/main.go
+71 −0 .evergreen/mongoproxy/go.mod
+213 −0 .evergreen/mongoproxy/go.sum
+135 −0 .evergreen/mongoproxy/options.go
+317 −0 .evergreen/mongoproxy/proxy.go
+179 −0 .evergreen/mongoproxy/proxy_test.go
+87 −0 .evergreen/mongoproxy/testdsl.go
+100 −0 .evergreen/mongoproxy/testdsl_test.go
+9 −3 .evergreen/mongosh_dl.py
+1 −1 .evergreen/ocsp/activate-ocspvenv.sh
+0 −0 .evergreen/ocsp/requirements-in.txt
+41 −0 .evergreen/ocsp/requirements.txt
+1 −1 .evergreen/orchestration/drivers_orchestration.py
+8 −0 .evergreen/requirements-cli.txt
+1 −0 .evergreen/requirements-in-cli.txt
+2 −0 .evergreen/serverless/README.md
+76 −0 .evergreen/start-mongoproxy.sh
+4 −0 .evergreen/tests/test-aws.sh
+1 −1 .evergreen/tests/test-csfle.sh
+15 −6 .evergreen/venv-utils.sh
+2 −1 .github/workflows/tests.yml
+2 −0 .gitignore
+4 −1 .pre-commit-config.yaml
+4 −0 action.yml