@@ -167,7 +167,7 @@ public function provideTests()
167
167
*
168
168
* @dataProvider dataKeyProvider
169
169
*/
170
- public function testDataKeyAndDoubleEncryption (Closure $ test )
170
+ public function testDataKeyAndDoubleEncryption (string $ providerName , $ masterKey )
171
171
{
172
172
$ client = new Client (static ::getUri ());
173
173
@@ -203,104 +203,70 @@ public function testDataKeyAndDoubleEncryption(Closure $test)
203
203
$ clientEncrypted = new Client (static ::getUri (), [], ['autoEncryption ' => $ autoEncryptionOpts ]);
204
204
$ clientEncryption = $ clientEncrypted ->createClientEncryption ($ encryptionOpts );
205
205
206
- $ test ($ clientEncryption , $ client , $ clientEncrypted , $ this );
206
+ $ commands = [];
207
+
208
+ $ dataKeyId = null ;
209
+ $ keyAltName = $ providerName . '_altname ' ;
210
+
211
+ (new CommandObserver ())->observe (
212
+ function () use ($ clientEncryption , &$ dataKeyId , $ keyAltName , $ providerName , $ masterKey ) {
213
+ $ keyData = ['keyAltNames ' => [$ keyAltName ]];
214
+ if ($ masterKey !== null ) {
215
+ $ keyData ['masterKey ' ] = $ masterKey ;
216
+ }
217
+
218
+ $ dataKeyId = $ clientEncryption ->createDataKey ($ providerName , $ keyData );
219
+ },
220
+ function ($ command ) use (&$ commands ) {
221
+ $ commands [] = $ command ;
222
+ }
223
+ );
224
+
225
+ $ this ->assertInstanceOf (Binary::class, $ dataKeyId );
226
+ $ this ->assertSame (Binary::TYPE_UUID , $ dataKeyId ->getType ());
227
+
228
+ $ this ->assertCount (2 , $ commands );
229
+ $ insert = $ commands [1 ]['started ' ];
230
+ $ this ->assertSame ('insert ' , $ insert ->getCommandName ());
231
+ $ this ->assertSame (WriteConcern::MAJORITY , $ insert ->getCommand ()->writeConcern ->w );
232
+
233
+ $ keys = $ client ->selectCollection ('keyvault ' , 'datakeys ' )->find (['_id ' => $ dataKeyId ]);
234
+ $ keys = iterator_to_array ($ keys );
235
+ $ this ->assertCount (1 , $ keys );
236
+
237
+ $ key = $ keys [0 ];
238
+ $ this ->assertNotNull ($ key );
239
+ $ this ->assertSame ($ providerName , $ key ['masterKey ' ]['provider ' ]);
240
+
241
+ $ encrypted = $ clientEncryption ->encrypt ('hello ' . $ providerName , ['algorithm ' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC , 'keyId ' => $ dataKeyId ]);
242
+ $ this ->assertInstanceOf (Binary::class, $ encrypted );
243
+ $ this ->assertSame (Binary::TYPE_ENCRYPTED , $ encrypted ->getType ());
244
+
245
+ $ clientEncrypted ->selectCollection ('db ' , 'coll ' )->insertOne (['_id ' => 'local ' , 'value ' => $ encrypted ]);
246
+ $ hello = $ clientEncrypted ->selectCollection ('db ' , 'coll ' )->findOne (['_id ' => 'local ' ]);
247
+ $ this ->assertNotNull ($ hello );
248
+ $ this ->assertSame ('hello ' . $ providerName , $ hello ['value ' ]);
249
+
250
+ $ encryptedAltName = $ clientEncryption ->encrypt ('hello ' . $ providerName , ['algorithm ' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC , 'keyAltName ' => $ keyAltName ]);
251
+ $ this ->assertEquals ($ encrypted , $ encryptedAltName );
252
+
253
+ $ this ->expectException (BulkWriteException::class);
254
+ $ clientEncrypted ->selectCollection ('db ' , 'coll ' )->insertOne (['encrypted_placeholder ' => $ encrypted ]);
207
255
}
208
256
209
257
public static function dataKeyProvider ()
210
258
{
211
259
return [
212
260
'local ' => [
213
- static function (ClientEncryption $ clientEncryption , Client $ client , Client $ clientEncrypted , self $ test ) {
214
- $ commands = [];
215
-
216
- $ localDatakeyId = null ;
217
-
218
- (new CommandObserver ())->observe (
219
- function () use ($ clientEncryption , &$ localDatakeyId ) {
220
- $ localDatakeyId = $ clientEncryption ->createDataKey ('local ' , ['keyAltNames ' => ['local_altname ' ]]);
221
- },
222
- function ($ command ) use (&$ commands ) {
223
- $ commands [] = $ command ;
224
- }
225
- );
226
-
227
- $ test ->assertInstanceOf (Binary::class, $ localDatakeyId );
228
- $ test ->assertSame (Binary::TYPE_UUID , $ localDatakeyId ->getType ());
229
-
230
- $ test ->assertCount (2 , $ commands );
231
- $ insert = $ commands [1 ]['started ' ];
232
- $ test ->assertSame ('insert ' , $ insert ->getCommandName ());
233
- $ test ->assertSame (WriteConcern::MAJORITY , $ insert ->getCommand ()->writeConcern ->w );
234
-
235
- $ keys = $ client ->selectCollection ('keyvault ' , 'datakeys ' )->find (['_id ' => $ localDatakeyId ]);
236
- $ keys = iterator_to_array ($ keys );
237
- $ test ->assertCount (1 , $ keys );
238
-
239
- $ key = $ keys [0 ];
240
- $ test ->assertNotNull ($ key );
241
- $ test ->assertSame ('local ' , $ key ['masterKey ' ]['provider ' ]);
242
-
243
- $ localEncrypted = $ clientEncryption ->encrypt ('hello local ' , ['algorithm ' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC , 'keyId ' => $ localDatakeyId ]);
244
- $ test ->assertInstanceOf (Binary::class, $ localEncrypted );
245
- $ test ->assertSame (Binary::TYPE_ENCRYPTED , $ localEncrypted ->getType ());
246
-
247
- $ clientEncrypted ->selectCollection ('db ' , 'coll ' )->insertOne (['_id ' => 'local ' , 'value ' => $ localEncrypted ]);
248
- $ helloLocal = $ clientEncrypted ->selectCollection ('db ' , 'coll ' )->findOne (['_id ' => 'local ' ]);
249
- $ test ->assertNotNull ($ helloLocal );
250
- $ test ->assertSame ('hello local ' , $ helloLocal ['value ' ]);
251
-
252
- $ localEncryptedAltName = $ clientEncryption ->encrypt ('hello local ' , ['algorithm ' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC , 'keyAltName ' => 'local_altname ' ]);
253
- $ test ->assertEquals ($ localEncrypted , $ localEncryptedAltName );
254
-
255
- $ test ->expectException (BulkWriteException::class);
256
- $ clientEncrypted ->selectCollection ('db ' , 'coll ' )->insertOne (['encrypted_placeholder ' => $ localEncrypted ]);
257
- },
261
+ 'providerName ' => 'local ' ,
262
+ 'masterKey ' => null ,
258
263
],
259
264
'aws ' => [
260
- static function (ClientEncryption $ clientEncryption , Client $ client , Client $ clientEncrypted , self $ test ) {
261
- $ commands = [];
262
- $ awsDatakeyId = null ;
263
-
264
- (new CommandObserver ())->observe (
265
- function () use ($ clientEncryption , &$ awsDatakeyId ) {
266
- $ awsDatakeyId = $ clientEncryption ->createDataKey ('aws ' , ['keyAltNames ' => ['aws_altname ' ], 'masterKey ' => ['region ' => 'us-east-1 ' , 'key ' => 'arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0 ' ]]);
267
- },
268
- function ($ command ) use (&$ commands ) {
269
- $ commands [] = $ command ;
270
- }
271
- );
272
-
273
- $ test ->assertInstanceOf (Binary::class, $ awsDatakeyId );
274
- $ test ->assertSame (Binary::TYPE_UUID , $ awsDatakeyId ->getType ());
275
-
276
- $ test ->assertCount (2 , $ commands );
277
- $ insert = $ commands [1 ]['started ' ];
278
- $ test ->assertSame ('insert ' , $ insert ->getCommandName ());
279
- $ test ->assertSame (WriteConcern::MAJORITY , $ insert ->getCommand ()->writeConcern ->w );
280
-
281
- $ keys = $ client ->selectCollection ('keyvault ' , 'datakeys ' )->find (['_id ' => $ awsDatakeyId ]);
282
- $ keys = iterator_to_array ($ keys );
283
- $ test ->assertCount (1 , $ keys );
284
-
285
- $ key = $ keys [0 ];
286
- $ test ->assertNotNull ($ key );
287
- $ test ->assertSame ('aws ' , $ key ['masterKey ' ]['provider ' ]);
288
-
289
- $ awsEncrypted = $ clientEncryption ->encrypt ('hello aws ' , ['algorithm ' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC , 'keyId ' => $ awsDatakeyId ]);
290
- $ test ->assertInstanceOf (Binary::class, $ awsEncrypted );
291
- $ test ->assertSame (Binary::TYPE_ENCRYPTED , $ awsEncrypted ->getType ());
292
-
293
- $ clientEncrypted ->selectCollection ('db ' , 'coll ' )->insertOne (['_id ' => 'aws ' , 'value ' => $ awsEncrypted ]);
294
- $ helloAws = $ clientEncrypted ->selectCollection ('db ' , 'coll ' )->findOne (['_id ' => 'aws ' ]);
295
- $ test ->assertNotNull ($ helloAws );
296
- $ test ->assertSame ('hello aws ' , $ helloAws ['value ' ]);
297
-
298
- $ awsEncryptedAltName = $ clientEncryption ->encrypt ('hello aws ' , ['algorithm ' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC , 'keyAltName ' => 'aws_altname ' ]);
299
- $ test ->assertEquals ($ awsEncrypted , $ awsEncryptedAltName );
300
-
301
- $ test ->expectException (BulkWriteException::class);
302
- $ clientEncrypted ->selectCollection ('db ' , 'coll ' )->insertOne (['encrypted_placeholder ' => $ awsEncrypted ]);
303
- },
265
+ 'providerName ' => 'aws ' ,
266
+ 'masterKey ' => [
267
+ 'region ' => 'us-east-1 ' ,
268
+ 'key ' => 'arn:aws:kms:us-east-1:579766882180:key/89fcc2c4-08b0-4bd9-9f25-e30687b580d0 ' ,
269
+ ],
304
270
],
305
271
];
306
272
}
0 commit comments