Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
37 changes: 19 additions & 18 deletions src/Command/DumpEncryptedFieldsMapCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
use Doctrine\ODM\MongoDB\Utility\EncryptedFieldsMapGenerator;
use MongoDB\BSON\PackedArray;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
Expand All @@ -16,10 +16,8 @@
use Symfony\Component\Yaml\Dumper;
use Symfony\Contracts\Service\ServiceCollectionInterface;

use function array_combine;
use function array_keys;
use function array_map;
use function array_values;
use function assert;
use function json_decode;
use function json_encode;
use function sprintf;
use function var_export;
Expand Down Expand Up @@ -63,24 +61,27 @@
$dumper = new Dumper();

foreach ($this->documentManagers as $name => $documentManager) {
$generator = new EncryptedFieldsMapGenerator($documentManager->getMetadataFactory());
$encryptedFieldsMap = $generator->getEncryptedFieldsMap();
assert($documentManager instanceof DocumentManager);

Check failure on line 64 in src/Command/DumpEncryptedFieldsMapCommand.php

View workflow job for this annotation

GitHub Actions / static-analysis / PHPStan (PHP: 8.4)

Instanceof between Doctrine\ODM\MongoDB\DocumentManager and Doctrine\ODM\MongoDB\DocumentManager will always evaluate to true.

Check failure on line 64 in src/Command/DumpEncryptedFieldsMapCommand.php

View workflow job for this annotation

GitHub Actions / static-analysis / PHPStan (PHP: 8.4)

Call to function assert() with true will always evaluate to true.

$encryptedFieldsMap = [];
foreach ($documentManager->getMetadataFactory()->getAllMetadata() as $metadata) {
$database = $documentManager->getDocumentDatabase($metadata->getName());
$collectionInfoIterator = $database->listCollections(['filter' => ['name' => $metadata->getCollection()]]);
Copy link
Member Author

Choose a reason for hiding this comment

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


foreach ($collectionInfoIterator as $collectionInfo) {
if ($collectionInfo['options']['encryptedFields'] ?? null) {
$encryptedFieldsMap[$this->getDocumentNamespace($metadata, $database->getDatabaseName())] = $collectionInfo['options']['encryptedFields'];
}
}
}

if (empty($encryptedFieldsMap)) {
continue;
}

$encryptedFieldsMap = array_combine(
// Convert class names in keys to their full namespaces
array_map(
fn (string $fqcn): string => $this->getDocumentNamespace(
$documentManager->getClassMetadata($fqcn),
$documentManager->getConfiguration()->getDefaultDB(),
),
array_keys($encryptedFieldsMap),
),
array_values($encryptedFieldsMap),
);
foreach ($encryptedFieldsMap as $ns => $encryptedFields) {
$encryptedFieldsMap[$ns] = json_decode(PackedArray::fromPHP($encryptedFields['fields'])->toRelaxedExtendedJSON(), true);
}

$io->section(sprintf('Dumping encrypted fields map for document manager "%s"', $name));
switch ($format) {
Expand Down
7 changes: 5 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -424,13 +424,16 @@ private function addConnectionsSection(ArrayNodeDefinition $rootNode): void
->children()
->scalarNode('path')->isRequired()->cannotBeEmpty()->end()
->scalarNode('bsonType')->isRequired()->cannotBeEmpty()->end()
->variableNode('keyId')->defaultNull()->end()
->arrayNode('queries')
->children()
->scalarNode('queryType')->isRequired()->cannotBeEmpty()->end()
->integerNode('min')->end()
->integerNode('max')->end()
->variableNode('min')->end()
->variableNode('max')->end()
->integerNode('sparsity')->end()
->integerNode('precision')->end()
->integerNode('trimFactor')->end()
->integerNode('contention')->end()
->end()
->end()
->end()
Expand Down
9 changes: 9 additions & 0 deletions src/DependencyInjection/DoctrineMongoDBExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use Doctrine\Persistence\Proxy;
use InvalidArgumentException;
use MongoDB\BSON\Document as BsonDocument;
use MongoDB\Client;
use ProxyManager\Proxy\LazyLoadingInterface;
use Symfony\Bridge\Doctrine\DependencyInjection\AbstractDoctrineExtension;
Expand Down Expand Up @@ -55,6 +56,7 @@
use function in_array;
use function interface_exists;
use function is_dir;
use function json_encode;
use function method_exists;
use function sprintf;

Expand Down Expand Up @@ -538,6 +540,13 @@ private function normalizeAutoEncryption(array $autoEncryption, string $defaultD

$autoEncryption['keyVaultNamespace'] ??= $defaultDB . '.datakeys';

if (isset($autoEncryption['encryptedFieldsMap'])) {
foreach ($autoEncryption['encryptedFieldsMap'] as &$value) {
//$value = ['fields' => $value];
$value = (new Definition(BsonDocument::class))->setFactory([BsonDocument::class, 'fromJSON'])->setArguments([json_encode(['fields' => $value])]);
}
}

return $autoEncryption;
}

Expand Down
Loading