-
Notifications
You must be signed in to change notification settings - Fork 266
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
jmikola
merged 4 commits into
mongodb:master
from
jmikola:dropencryptedcollection-operation
Apr 12, 2023
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
89bd6ac
Extract DropEncryptedCollection operation from helper methods
jmikola 5ac1830
Apply suggestions from code review
jmikola 8b64a9c
Merge branch 'master' into dropencryptedcollection-operation
jmikola 43c9293
Merge branch 'master' into dropencryptedcollection-operation
jmikola File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
<?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:disable SlevomatCodingStandard.Namespaces.UnusedUses.UnusedUse | ||
use MongoDB\BSON\Binary; | ||
// phpcs:enable | ||
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 or object'); | ||
} | ||
jmikola marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** @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); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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], | ||
]; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.