Metadata collections not dropped when an encrypted collection is dropped

XMLWordPrintableJSON

    • None
    • PHP Drivers
    • Not Needed
    • None
    • None
    • None
    • None
    • None
    • None

       

      use MongoDB\BSON\Binary;
      use MongoDB\Client;
      
      class TestDropEncryptedCollection extends TestCase
      {
          public function testDropEncryptedCollection(): void
          {
              $collectionName = $this->getCollectionName();
              $encryptedClient = new Client(
                  $this->getUri(),
                  [],
                  [
                      'autoEncryption' => $autoEncryptionOptions = [
                          'keyVaultNamespace' => 'encryption.datakeys',
                          'kmsProviders' => [
                              'local' => [
                                  'key' => new Binary(random_bytes(96)),
                              ],
                          ],
                          //'encryptedFieldsMap' => [],
                       ],
                  ],
              );
              $clientEncryption = $encryptedClient->createClientEncryption($autoEncryptionOptions);
              $database = $encryptedClient->getDatabase($this->getDatabaseName());
              $database->drop();
              $database->createEncryptedCollection($collectionName, $clientEncryption, 'local', null, [
                  'encryptedFields' => [
                      'fields' => [
                          [
                              'keyId' => null,
                              'path' => 'ssn',
                              'bsonType' => 'string',
                          ],
                      ],
                  ],
              ]);
              $collectionNames = iterator_to_array($database->listCollectionNames());
              self::assertCount(3, $collectionNames);
              $database->dropCollection($collectionName);
              //$database->dropCollection($collectionName, ['encryptedFields' => []]);
              $collectionNames = iterator_to_array($database->listCollectionNames());
              self::assertCount(0, $collectionNames);
          }
      } 

       

       

      When an encrypted collection is dropped, and autoEncryption is enabled, the metadata collections (.esc and .ecoc) must be automatically dropped.

      https://github.com/mongodb/mongo-php-library/blob/888d383b6cf5ea25eba8adce4ad6474fe703dbb6/src/Collection.php#L514-L517

      They are removed only in one of the conditions:

      • The autoEncryption.encryptedFieldsMap is set in the client driver options. With any value (even an empty array)
      • The option encryptedFields is set drop options

      $database->dropCollection($collectionName, ['encryptedFields' => []]);

       

       

            Assignee:
            Jeremy Mikola
            Reporter:
            Jérôme Tamarelle
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: