Skip to content

Commit e15bc11

Browse files
committed
createDataKeys Return the modifiedEncryptedFields instead of modifying the reference
1 parent 6f2085c commit e15bc11

File tree

3 files changed

+13
-18
lines changed

3 files changed

+13
-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: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,21 +97,21 @@ 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
104+
* @return array
105105
* @throws DriverRuntimeException for errors creating a data key
106106
*/
107-
public function createDataKeys(ClientEncryption $clientEncryption, string $kmsProvider, ?array $masterKey, ?array &$encryptedFields = null): void
107+
public function createDataKeys(ClientEncryption $clientEncryption, string $kmsProvider, ?array $masterKey): array
108108
{
109109
/** @psalm-var array{fields: list<array{keyId: ?Binary}|object{keyId: ?Binary}>|Serializable|PackedArray} */
110110
$encryptedFields = document_to_array($this->options['encryptedFields']);
111111

112112
// NOP if there are no fields to examine
113113
if (! isset($encryptedFields['fields'])) {
114-
return;
114+
return $encryptedFields;
115115
}
116116

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

129129
// Skip invalid types and defer to the server to raise an error
130130
if (! is_array($encryptedFields['fields'])) {
131-
return;
131+
return $encryptedFields;
132132
}
133133

134134
$createDataKeyArgs = [
@@ -152,6 +152,8 @@ public function createDataKeys(ClientEncryption $clientEncryption, string $kmsPr
152152

153153
$this->options['encryptedFields'] = $encryptedFields;
154154
$this->createCollection = new CreateCollection($this->databaseName, $this->collectionName, $this->options);
155+
156+
return $encryptedFields;
155157
}
156158

157159
/**

tests/Operation/CreateEncryptedCollectionFunctionalTest.php

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

66-
$operation->createDataKeys(
66+
$encryptedFieldsOutput = $operation->createDataKeys(
6767
$this->clientEncryption,
6868
'local',
6969
null,
70-
$encryptedFieldsOutput,
7170
);
7271

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

97-
$operation->createDataKeys(
96+
$encryptedFieldsOutput = $operation->createDataKeys(
9897
$this->clientEncryption,
9998
'local',
10099
null,
101-
$encryptedFieldsOutput,
102100
);
103101

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

128-
$operation->createDataKeys(
126+
$encryptedFieldsOutput = $operation->createDataKeys(
129127
$this->clientEncryption,
130128
'local',
131129
null,
132-
$encryptedFieldsOutput,
133130
);
134131

135132
$this->assertSame($expectedOutput, $encryptedFieldsOutput);
@@ -158,11 +155,10 @@ public function testCreateDataKeysDoesNotModifyOriginalEncryptedFieldsOption():
158155
['encryptedFields' => $originalEncryptedFields],
159156
);
160157

161-
$operation->createDataKeys(
158+
$modifiedEncryptedFields = $operation->createDataKeys(
162159
$this->clientEncryption,
163160
'local',
164161
null,
165-
$modifiedEncryptedFields,
166162
);
167163

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

183-
$operation->createDataKeys(
179+
$modifiedEncryptedFields = $operation->createDataKeys(
184180
$this->clientEncryption,
185181
'local',
186182
null,
187-
$modifiedEncryptedFields,
188183
);
189184

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

0 commit comments

Comments
 (0)