Skip to content
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
4 changes: 4 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,12 @@
<code><![CDATA[$driverOptions['driver'] ?? []]]></code>
<code><![CDATA[$pipeline]]></code>
</MixedArgument>
<MixedArrayAssignment>
<code><![CDATA[$options['kmsProviders'][$name]]]></code>
</MixedArrayAssignment>
<MixedAssignment>
<code><![CDATA[$mergedDriver['platform']]]></code>
<code><![CDATA[$provider]]></code>
</MixedAssignment>
<MixedPropertyTypeCoercion>
<code><![CDATA[$driverOptions['builderEncoder'] ?? new BuilderEncoder()]]></code>
Expand Down
38 changes: 25 additions & 13 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,8 @@
throw InvalidArgumentException::invalidType('"typeMap" driver option', $driverOptions['typeMap'], 'array');
}

if (isset($driverOptions['autoEncryption']['keyVaultClient'])) {
if ($driverOptions['autoEncryption']['keyVaultClient'] instanceof self) {
$driverOptions['autoEncryption']['keyVaultClient'] = $driverOptions['autoEncryption']['keyVaultClient']->manager;
} elseif (! $driverOptions['autoEncryption']['keyVaultClient'] instanceof Manager) {
throw InvalidArgumentException::invalidType('"keyVaultClient" autoEncryption option', $driverOptions['autoEncryption']['keyVaultClient'], [self::class, Manager::class]);
}
if (isset($driverOptions['autoEncryption']) && is_array($driverOptions['autoEncryption'])) {
$driverOptions['autoEncryption'] = $this->prepareEncryptionOptions($driverOptions['autoEncryption']);
}

if (isset($driverOptions['builderEncoder']) && ! $driverOptions['builderEncoder'] instanceof Encoder) {
Expand Down Expand Up @@ -213,13 +209,7 @@
*/
public function createClientEncryption(array $options)
{
if (isset($options['keyVaultClient'])) {
if ($options['keyVaultClient'] instanceof self) {
$options['keyVaultClient'] = $options['keyVaultClient']->manager;
} elseif (! $options['keyVaultClient'] instanceof Manager) {
throw InvalidArgumentException::invalidType('"keyVaultClient" option', $options['keyVaultClient'], [self::class, Manager::class]);
}
}
$options = $this->prepareEncryptionOptions($options);

return $this->manager->createClientEncryption($options);
}
Expand Down Expand Up @@ -499,4 +489,26 @@

return $mergedDriver;
}

private function prepareEncryptionOptions(array $options): array
{
if (isset($options['keyVaultClient'])) {
if ($options['keyVaultClient'] instanceof self) {
$options['keyVaultClient'] = $options['keyVaultClient']->manager;
} elseif (! $options['keyVaultClient'] instanceof Manager) {
throw InvalidArgumentException::invalidType('"keyVaultClient" option', $options['keyVaultClient'], [self::class, Manager::class]);
}
}

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

Check notice

Code scanning / Psalm

MixedAssignment Note

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

Choose a reason for hiding this comment

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

I assume the Psalm invocation here is failing to observe the MixedArrayAssignment addition to the baseline.

@GromNaN: If so, feel free to dismiss and merge.

$options['kmsProviders'][$name] = new stdClass();
}
}
}

return $options;
}
}
12 changes: 12 additions & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ public function testConstructorAutoEncryptionOpts(): void
new Client(static::getUri(), [], ['autoEncryption' => $autoEncryptionOpts]);
}

#[DoesNotPerformAssertions]
public function testConstructorEmptyKmsProvider(): void
{
$autoEncryptionOpts = [
'keyVaultClient' => new Client(static::getUri()),
'keyVaultNamespace' => 'default.keys',
'kmsProviders' => ['gcp' => []],
];

new Client(static::getUri(), [], ['autoEncryption' => $autoEncryptionOpts]);
}

#[DataProvider('provideInvalidConstructorDriverOptions')]
public function testConstructorDriverOptionTypeChecks(array $driverOptions, string $exception = InvalidArgumentException::class): void
{
Expand Down
2 changes: 0 additions & 2 deletions tests/Collection/CollectionFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,6 @@ public function testWithOptionsInheritsOptions(): void

$rc = new ReflectionClass($clone);
$rp = $rc->getProperty('autoEncryptionEnabled');
$rp->setAccessible(true);

$this->assertSame(true, $rp->getValue($clone));
}
Expand Down Expand Up @@ -432,7 +431,6 @@ public function testWithOptionsPassesOptions(): void

$rc = new ReflectionClass($clone);
$rp = $rc->getProperty('autoEncryptionEnabled');
$rp->setAccessible(true);

$this->assertSame(true, $rp->getValue($clone));
}
Expand Down
2 changes: 0 additions & 2 deletions tests/Database/DatabaseFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ public function testWithOptionsInheritsOptions(): void

$rc = new ReflectionClass($clone);
$rp = $rc->getProperty('autoEncryptionEnabled');
$rp->setAccessible(true);

$this->assertSame(true, $rp->getValue($clone));
}
Expand All @@ -424,7 +423,6 @@ public function testWithOptionsPassesOptions(): void

$rc = new ReflectionClass($clone);
$rp = $rc->getProperty('autoEncryptionEnabled');
$rp->setAccessible(true);

$this->assertSame(true, $rp->getValue($clone));
}
Expand Down