Skip to content

PHPLIB-1708 Cast empty KMS provider into an object #1757

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

Open
wants to merge 1 commit into
base: v1.21
Choose a base branch
from

Conversation

GromNaN
Copy link
Member

@GromNaN GromNaN commented Aug 19, 2025

Fix PHPLIB-1708

For some KMS providers, all options can be omitted. The authentication is done using the env var or the system.
https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/client-side-encryption.md#credentialproviders

The approach in doctrine/mongodb-odm#2801 is not sufficient because Symfony is not able to dump the empty object instance into the container.

Unable to dump a service container if a parameter is an object or a resource, got "stdClass".

@GromNaN GromNaN requested review from paulinevos and Copilot August 19, 2025 11:53
@GromNaN GromNaN requested a review from a team as a code owner August 19, 2025 11:53
Copilot

This comment was marked as outdated.

@GromNaN GromNaN force-pushed the PHPLIB-1708 branch 2 times, most recently from 1c52490 to d96aa71 Compare August 19, 2025 12:06

// The server requires an empty document for automatic credentials.
if (isset($options['kmsProviders']) && is_array($options['kmsProviders'])) {
foreach ($options['kmsProviders'] as $name => $provider) {

Check notice

Code scanning / Psalm

MixedAssignment Note

Unable to determine the type that $provider is being assigned to
@GromNaN
Copy link
Member Author

GromNaN commented Aug 19, 2025

Fixed in Doctrine MongoDB ODM Bundle by setting a DI Definition of stdClass in the parameters instead of an empty stdClass object. doctrine/DoctrineMongoDBBundle@953613d

src/Client.php Outdated
@@ -457,6 +447,28 @@ public function watch(array $pipeline = [], array $options = [])
return $operation->execute($server);
}

private function formatEncryptionOptions(array $options): array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private function formatEncryptionOptions(array $options): array
private function prepareAutoEncryptionOptions(array $options): array

I'm not sure if there's prior art in PHPLIB, but I use "prepare" extensively in PHPC for this sort of thing. Up to you, though.

I do like including "AutoEncryption" in the name here since it refers to that driver option.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok for "prepare", but no for "AutoEncryption": the method createClientEncryption isn't specific to auto encryption.

src/Client.php Outdated
if (isset($options['kmsProviders']) && is_array($options['kmsProviders'])) {
foreach ($options['kmsProviders'] as $name => $provider) {
if ($provider === []) {
$options['kmsProviders'][$name] = Document::fromPHP([]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to use Document::fromPHP([]) instead of (object) [] or new stdClass? PHPC is going to end up encoding this as BSON anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea what's the more efficient. Changed to new stdClass

@@ -37,6 +37,18 @@ public function testConstructorAutoEncryptionOpts(): void
new Client(static::getUri(), [], ['autoEncryption' => $autoEncryptionOpts]);
}

#[DoesNotPerformAssertions]
public function testConstructorEmptyKmsProvider(): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this test have failed without the above patch? If not, is anything being tested at all?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes:

MongoDB\Driver\Exception\EncryptionException: expected BSON document for field: gcp

@GromNaN GromNaN requested a review from jmikola August 19, 2025 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants