Skip to content

Commit 953613d

Browse files
committed
Cast KMS provider to object to support AWS empty config
doctrine/mongodb-odm#2801
1 parent 23737b3 commit 953613d

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

src/DependencyInjection/DoctrineMongoDBExtension.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -528,10 +528,16 @@ private function normalizeAutoEncryption(array $autoEncryption, string $defaultD
528528
throw new InvalidArgumentException('The "kmsProvider" option must contain a "type" key.');
529529
}
530530

531-
$provider = $autoEncryption['kmsProvider']['type'];
532-
$autoEncryption['kmsProviders'] = [
533-
$provider => array_diff_key($autoEncryption['kmsProvider'], ['type' => true]),
534-
];
531+
$provider = $autoEncryption['kmsProvider']['type'];
532+
$providerOpts = array_diff_key($autoEncryption['kmsProvider'], ['type' => true]);
533+
// To use "Automatic Credentials", the provider options must be an empty document.
534+
// Fix the empty array to an empty stdClass object, as the driver expects it.
535+
if ($providerOpts === []) {
536+
$providerOpts = new Definition('stdClass');
537+
}
538+
539+
$autoEncryption['kmsProviders'] = [$provider => $providerOpts];
540+
535541
if (isset($autoEncryption['tlsOptions'])) {
536542
$autoEncryption['tlsOptions'] = [$provider => $autoEncryption['tlsOptions']];
537543
}

tests/DependencyInjection/DoctrineMongoDBExtensionTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use MongoDB\Client;
1717
use PHPUnit\Framework\Attributes\DataProvider;
1818
use PHPUnit\Framework\TestCase;
19+
use stdClass;
1920
use Symfony\Bridge\Doctrine\Messenger\DoctrineClearEntityManagerWorkerSubscriber;
2021
use Symfony\Component\DependencyInjection\Alias;
2122
use Symfony\Component\DependencyInjection\ChildDefinition;
@@ -635,4 +636,31 @@ public function testAutoEncryptionWithExtraOptions(): void
635636
// Ensure the driver option set in the client matches the ODM configuration
636637
self::assertEquals($driverOptions['autoEncryption'], $odmConfiguration->getDriverOptions()['autoEncryption']);
637638
}
639+
640+
public function testAutoEncryptionWithEmptyKmsProvider(): void
641+
{
642+
$container = $this->buildMinimalContainer();
643+
$loader = new DoctrineMongoDBExtension();
644+
645+
$config = [
646+
'connections' => [
647+
'default' => [
648+
'autoEncryption' => [
649+
'keyVaultNamespace' => 'db.vault',
650+
'kmsProvider' => ['type' => 'aws'],
651+
],
652+
],
653+
],
654+
'document_managers' => ['default' => []],
655+
];
656+
657+
$loader->load([$config], $container);
658+
(new ServiceRepositoryCompilerPass())->process($container);
659+
660+
$clientDef = $container->getDefinition('doctrine_mongodb.odm.default_connection');
661+
$driverOptions = $clientDef->getArgument(2);
662+
663+
self::assertArrayHasKey('autoEncryption', $driverOptions);
664+
self::assertEquals(['aws' => new Definition(stdClass::class)], $driverOptions['autoEncryption']['kmsProviders']);
665+
}
638666
}

0 commit comments

Comments
 (0)