Skip to content

Extract DropEncryptedCollection operation from helper methods #1055

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 4 commits into from
Apr 12, 2023
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
21 changes: 7 additions & 14 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use MongoDB\Operation\DeleteOne;
use MongoDB\Operation\Distinct;
use MongoDB\Operation\DropCollection;
use MongoDB\Operation\DropEncryptedCollection;
use MongoDB\Operation\DropIndexes;
use MongoDB\Operation\EstimatedDocumentCount;
use MongoDB\Operation\Explain;
Expand Down Expand Up @@ -495,22 +496,14 @@ public function drop(array $options = [])
$options['writeConcern'] = $this->writeConcern;
}

$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)
?? null;

if ($encryptedFields !== null) {
// encryptedFields is not passed to the drop command
unset($options['encryptedFields']);

$encryptedFields = (array) $encryptedFields;
(new DropCollection($this->databaseName, $encryptedFields['escCollection'] ?? 'enxcol_.' . $this->collectionName . '.esc'))->execute($server);
(new DropCollection($this->databaseName, $encryptedFields['eccCollection'] ?? 'enxcol_.' . $this->collectionName . '.ecc'))->execute($server);
(new DropCollection($this->databaseName, $encryptedFields['ecocCollection'] ?? 'enxcol_.' . $this->collectionName . '.ecoc'))->execute($server);
if (! 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);
}

$operation = new DropCollection($this->databaseName, $this->collectionName, $options);
$operation = isset($options['encryptedFields'])
? new DropEncryptedCollection($this->databaseName, $this->collectionName, $options)
: new DropCollection($this->databaseName, $this->collectionName, $options);

return $operation->execute($server);
}
Expand Down
21 changes: 7 additions & 14 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use MongoDB\Operation\DatabaseCommand;
use MongoDB\Operation\DropCollection;
use MongoDB\Operation\DropDatabase;
use MongoDB\Operation\DropEncryptedCollection;
use MongoDB\Operation\ListCollectionNames;
use MongoDB\Operation\ListCollections;
use MongoDB\Operation\ModifyCollection;
Expand Down Expand Up @@ -386,22 +387,14 @@ public function dropCollection(string $collectionName, array $options = [])
$options['writeConcern'] = $this->writeConcern;
}

$encryptedFields = $options['encryptedFields']
?? get_encrypted_fields_from_driver($this->databaseName, $collectionName, $this->manager)
?? get_encrypted_fields_from_server($this->databaseName, $collectionName, $this->manager, $server)
?? null;

if ($encryptedFields !== null) {
// encryptedFields is not passed to the drop command
unset($options['encryptedFields']);

$encryptedFields = (array) $encryptedFields;
(new DropCollection($this->databaseName, $encryptedFields['escCollection'] ?? 'enxcol_.' . $collectionName . '.esc'))->execute($server);
(new DropCollection($this->databaseName, $encryptedFields['eccCollection'] ?? 'enxcol_.' . $collectionName . '.ecc'))->execute($server);
(new DropCollection($this->databaseName, $encryptedFields['ecocCollection'] ?? 'enxcol_.' . $collectionName . '.ecoc'))->execute($server);
if (! 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);
}

$operation = new DropCollection($this->databaseName, $collectionName, $options);
$operation = isset($options['encryptedFields'])
? new DropEncryptedCollection($this->databaseName, $collectionName, $options)
: new DropCollection($this->databaseName, $collectionName, $options);

return $operation->execute($server);
}
Expand Down
104 changes: 104 additions & 0 deletions src/Operation/DropEncryptedCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php
/*
* Copyright 2023-present MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace MongoDB\Operation;

// phpcs:ignore SlevomatCodingStandard.Namespaces.UnusedUses.UnusedUse
use MongoDB\BSON\Binary;
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
use MongoDB\Driver\Server;
use MongoDB\Exception\InvalidArgumentException;

use function is_array;
use function is_object;

/**
* Drop an encrypted collection.
*
* The "encryptedFields" option is required.
*
* This operation additionally drops related metadata collections.
*
* @internal
* @see \MongoDB\Database::dropCollection()
* @see \MongoDB\Collection::drop()
* @see https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/client-side-encryption.rst#drop-collection-helper
* @see https://www.mongodb.com/docs/manual/core/queryable-encryption/fundamentals/manage-collections/
*/
class DropEncryptedCollection implements Executable
{
/** @var DropCollection */
private $dropCollection;

/** @var DropCollection[] */
private $dropMetadataCollections;

/**
* Constructs an operation to drop an encrypted collection and its related
* metadata collections.
*
* The following option is supported in addition to the options for
* DropCollection:
*
* * encryptedFields (document): Configuration for encrypted fields.
* See: https://www.mongodb.com/docs/manual/core/queryable-encryption/fundamentals/encrypt-and-query/
*
* @see DropCollection::__construct() for supported options
* @param string $databaseName Database name
* @param string $collectionName Collection name
* @param array $options DropCollection options
* @throws InvalidArgumentException for parameter/option parsing errors
*/
public function __construct(string $databaseName, string $collectionName, array $options)
{
if (! isset($options['encryptedFields'])) {
throw new InvalidArgumentException('"encryptedFields" option is required');
}

if (! is_array($options['encryptedFields']) && ! is_object($options['encryptedFields'])) {
throw InvalidArgumentException::invalidType('"encryptedFields" option', $options['encryptedFields'], ['array', 'object']);
}

/** @psalm-var array{eccCollection?: ?string, ecocCollection?: ?string, escCollection?: ?string} */
$encryptedFields = (array) $options['encryptedFields'];

$this->dropMetadataCollections = [
new DropCollection($databaseName, $encryptedFields['escCollection'] ?? 'enxcol_.' . $collectionName . '.esc'),
new DropCollection($databaseName, $encryptedFields['eccCollection'] ?? 'enxcol_.' . $collectionName . '.ecc'),
new DropCollection($databaseName, $encryptedFields['ecocCollection'] ?? 'enxcol_.' . $collectionName . '.ecoc'),
];

// DropCollection does not use the "encryptedFields" option
unset($options['encryptedFields']);

$this->dropCollection = new DropCollection($databaseName, $collectionName, $options);
}

/**
* @see Executable::execute()
* @return array|object Command result document from dropping the encrypted collection
* @throws DriverRuntimeException for other driver errors (e.g. connection errors)
*/
public function execute(Server $server)
{
foreach ($this->dropMetadataCollections as $dropMetadataCollection) {
$dropMetadataCollection->execute($server);
}

return $this->dropCollection->execute($server);
}
}
34 changes: 34 additions & 0 deletions tests/Operation/DropEncryptedCollectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace MongoDB\Tests\Operation;

use Generator;
use MongoDB\Exception\InvalidArgumentException;
use MongoDB\Operation\DropEncryptedCollection;

use function get_debug_type;

class DropEncryptedCollectionTest extends TestCase
{
/**
* @dataProvider provideInvalidConstructorOptions
*/
public function testConstructorOptionTypeChecks(array $options): void
{
$this->expectException(InvalidArgumentException::class);
new DropEncryptedCollection($this->getDatabaseName(), $this->getCollectionName(), $options);
}

public function provideInvalidConstructorOptions(): Generator
{
yield 'encryptedFields is required' => [
[],
];

foreach ($this->getInvalidDocumentValues() as $value) {
yield 'encryptedFields type: ' . get_debug_type($value) => [
['encryptedFields' => $value],
];
}
}
}