Skip to content

Commit 021b422

Browse files
committed
createDataKeys Return the modifiedEncryptedFields instead of modifying the reference
1 parent 6cb7682 commit 021b422

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

src/Database.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,9 @@ public function createEncryptedCollection(string $collectionName, ClientEncrypti
324324
$server = select_server_for_write($this->manager, $options);
325325

326326
try {
327-
$operation->createDataKeys($clientEncryption, $kmsProvider, $masterKey, $encryptedFields);
327+
$encryptedFields = $operation->createDataKeys($clientEncryption, $kmsProvider, $masterKey);
328328
$operation->execute($server);
329329

330-
assert(is_array($encryptedFields), '$encryptedFields is set');
331-
332330
return $encryptedFields;
333331
} catch (Throwable $e) {
334332
throw new CreateEncryptedCollectionException($e, $encryptedFields ?? []);

src/Operation/CreateEncryptedCollection.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,20 @@ public function __construct(private string $databaseName, private string $collec
9797
* "encryptedFields" option and reconstruct the internal CreateCollection
9898
* operation used for creating the encrypted collection.
9999
*
100-
* The $encryptedFields reference parameter may be used to determine which
101-
* data keys have been created.
100+
* Returns the data keys that have been created.
102101
*
103102
* @see \MongoDB\Database::createEncryptedCollection()
104103
* @see https://www.php.net/manual/en/mongodb-driver-clientencryption.createdatakey.php
105104
* @throws DriverRuntimeException for errors creating a data key
106105
*/
107-
public function createDataKeys(ClientEncryption $clientEncryption, string $kmsProvider, ?array $masterKey, ?array &$encryptedFields = null): void
106+
public function createDataKeys(ClientEncryption $clientEncryption, string $kmsProvider, ?array $masterKey): array
108107
{
109108
/** @psalm-var array{fields: list<array{keyId: ?Binary}|object{keyId: ?Binary}>|Serializable|PackedArray} */
110109
$encryptedFields = document_to_array($this->options['encryptedFields']);
111110

112111
// NOP if there are no fields to examine
113112
if (! isset($encryptedFields['fields'])) {
114-
return;
113+
return $encryptedFields;
115114
}
116115

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

129128
// Skip invalid types and defer to the server to raise an error
130129
if (! is_array($encryptedFields['fields'])) {
131-
return;
130+
return $encryptedFields;
132131
}
133132

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

153152
$this->options['encryptedFields'] = $encryptedFields;
154153
$this->createCollection = new CreateCollection($this->databaseName, $this->collectionName, $this->options);
154+
155+
return $encryptedFields;
155156
}
156157

157158
/**

tests/Operation/CreateEncryptedCollectionFunctionalTest.php

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,10 @@ public function testCreateDataKeysNopIfFieldsIsMissing($input, array $expectedOu
6464
['encryptedFields' => $input],
6565
);
6666

67-
$operation->createDataKeys(
67+
$encryptedFieldsOutput = $operation->createDataKeys(
6868
$this->clientEncryption,
6969
'local',
7070
null,
71-
$encryptedFieldsOutput,
7271
);
7372

7473
$this->assertSame($expectedOutput, $encryptedFieldsOutput);
@@ -95,11 +94,10 @@ public function testCreateDataKeysNopIfFieldsHasInvalidType($input, array $expec
9594
['encryptedFields' => $input],
9695
);
9796

98-
$operation->createDataKeys(
97+
$encryptedFieldsOutput = $operation->createDataKeys(
9998
$this->clientEncryption,
10099
'local',
101100
null,
102-
$encryptedFieldsOutput,
103101
);
104102

105103
$this->assertSame($expectedOutput, $encryptedFieldsOutput);
@@ -126,11 +124,10 @@ public function testCreateDataKeysSkipsNonDocumentFields($input, array $expected
126124
['encryptedFields' => $input],
127125
);
128126

129-
$operation->createDataKeys(
127+
$encryptedFieldsOutput = $operation->createDataKeys(
130128
$this->clientEncryption,
131129
'local',
132130
null,
133-
$encryptedFieldsOutput,
134131
);
135132

136133
$this->assertSame($expectedOutput, $encryptedFieldsOutput);
@@ -159,11 +156,10 @@ public function testCreateDataKeysDoesNotModifyOriginalEncryptedFieldsOption():
159156
['encryptedFields' => $originalEncryptedFields],
160157
);
161158

162-
$operation->createDataKeys(
159+
$modifiedEncryptedFields = $operation->createDataKeys(
163160
$this->clientEncryption,
164161
'local',
165162
null,
166-
$modifiedEncryptedFields,
167163
);
168164

169165
$this->assertSame($originalField, $originalEncryptedFields->fields[0]);
@@ -181,11 +177,10 @@ public function testEncryptedFieldsDocuments($input): void
181177
['encryptedFields' => $input],
182178
);
183179

184-
$operation->createDataKeys(
180+
$modifiedEncryptedFields = $operation->createDataKeys(
185181
$this->clientEncryption,
186182
'local',
187183
null,
188-
$modifiedEncryptedFields,
189184
);
190185

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

0 commit comments

Comments
 (0)