Skip to content

Commit

Permalink
createDataKeys Return the modifiedEncryptedFields instead of modifyin…
Browse files Browse the repository at this point in the history
…g the reference
  • Loading branch information
GromNaN committed Sep 26, 2024
1 parent 6f2085c commit c6772de
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 18 deletions.
4 changes: 1 addition & 3 deletions src/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -324,11 +324,9 @@ public function createEncryptedCollection(string $collectionName, ClientEncrypti
$server = select_server_for_write($this->manager, $options);

try {
$operation->createDataKeys($clientEncryption, $kmsProvider, $masterKey, $encryptedFields);
$encryptedFields = $operation->createDataKeys($clientEncryption, $kmsProvider, $masterKey);
$operation->execute($server);

assert(is_array($encryptedFields), '$encryptedFields is set');

return $encryptedFields;
} catch (Throwable $e) {
throw new CreateEncryptedCollectionException($e, $encryptedFields ?? []);
Expand Down
11 changes: 6 additions & 5 deletions src/Operation/CreateEncryptedCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,20 @@ public function __construct(private string $databaseName, private string $collec
* "encryptedFields" option and reconstruct the internal CreateCollection
* operation used for creating the encrypted collection.
*
* The $encryptedFields reference parameter may be used to determine which
* data keys have been created.
* Returns the data keys that have been created.
*
* @see \MongoDB\Database::createEncryptedCollection()
* @see https://www.php.net/manual/en/mongodb-driver-clientencryption.createdatakey.php
* @throws DriverRuntimeException for errors creating a data key
*/
public function createDataKeys(ClientEncryption $clientEncryption, string $kmsProvider, ?array $masterKey, ?array &$encryptedFields = null): void
public function createDataKeys(ClientEncryption $clientEncryption, string $kmsProvider, ?array $masterKey): array
{
/** @psalm-var array{fields: list<array{keyId: ?Binary}|object{keyId: ?Binary}>|Serializable|PackedArray} */
$encryptedFields = document_to_array($this->options['encryptedFields']);

// NOP if there are no fields to examine
if (! isset($encryptedFields['fields'])) {
return;
return $encryptedFields;
}

// Allow PackedArray or Serializable object for the fields array
Expand All @@ -128,7 +127,7 @@ public function createDataKeys(ClientEncryption $clientEncryption, string $kmsPr

// Skip invalid types and defer to the server to raise an error
if (! is_array($encryptedFields['fields'])) {
return;
return $encryptedFields;
}

$createDataKeyArgs = [
Expand All @@ -152,6 +151,8 @@ public function createDataKeys(ClientEncryption $clientEncryption, string $kmsPr

$this->options['encryptedFields'] = $encryptedFields;
$this->createCollection = new CreateCollection($this->databaseName, $this->collectionName, $this->options);

return $encryptedFields;
}

/**
Expand Down
15 changes: 5 additions & 10 deletions tests/Operation/CreateEncryptedCollectionFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,10 @@ public function testCreateDataKeysNopIfFieldsIsMissing($input, array $expectedOu
['encryptedFields' => $input],
);

$operation->createDataKeys(
$encryptedFieldsOutput = $operation->createDataKeys(
$this->clientEncryption,
'local',
null,
$encryptedFieldsOutput,
);

$this->assertSame($expectedOutput, $encryptedFieldsOutput);
Expand All @@ -94,11 +93,10 @@ public function testCreateDataKeysNopIfFieldsHasInvalidType($input, array $expec
['encryptedFields' => $input],
);

$operation->createDataKeys(
$encryptedFieldsOutput = $operation->createDataKeys(
$this->clientEncryption,
'local',
null,
$encryptedFieldsOutput,
);

$this->assertSame($expectedOutput, $encryptedFieldsOutput);
Expand All @@ -125,11 +123,10 @@ public function testCreateDataKeysSkipsNonDocumentFields($input, array $expected
['encryptedFields' => $input],
);

$operation->createDataKeys(
$encryptedFieldsOutput = $operation->createDataKeys(
$this->clientEncryption,
'local',
null,
$encryptedFieldsOutput,
);

$this->assertSame($expectedOutput, $encryptedFieldsOutput);
Expand Down Expand Up @@ -158,11 +155,10 @@ public function testCreateDataKeysDoesNotModifyOriginalEncryptedFieldsOption():
['encryptedFields' => $originalEncryptedFields],
);

$operation->createDataKeys(
$modifiedEncryptedFields = $operation->createDataKeys(
$this->clientEncryption,
'local',
null,
$modifiedEncryptedFields,
);

$this->assertSame($originalField, $originalEncryptedFields->fields[0]);
Expand All @@ -180,11 +176,10 @@ public function testEncryptedFieldsDocuments($input): void
['encryptedFields' => $input],
);

$operation->createDataKeys(
$modifiedEncryptedFields = $operation->createDataKeys(
$this->clientEncryption,
'local',
null,
$modifiedEncryptedFields,
);

$this->assertInstanceOf(Binary::class, $modifiedEncryptedFields['fields'][0]['keyId'] ?? null);
Expand Down

0 comments on commit c6772de

Please sign in to comment.