Skip to content

Commit b6c4601

Browse files
committed
Address code review feedback
1 parent 6082a83 commit b6c4601

File tree

2 files changed

+170
-181
lines changed

2 files changed

+170
-181
lines changed

tests/SpecTests/ClientSideEncryptionSpecTest.php

Lines changed: 163 additions & 159 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use function basename;
2727
use function file_get_contents;
2828
use function glob;
29+
use function in_array;
2930
use function iterator_to_array;
3031
use function json_decode;
3132
use function sprintf;
@@ -355,85 +356,85 @@ public function testExternalKeyVault($withExternalKeyVault)
355356

356357
public static function provideBSONSizeLimitsAndBatchSplittingTests()
357358
{
358-
yield [static function (self $test, Collection $collection) {
359-
// Test 1
360-
$collection->insertOne(['_id' => 'over_2mib_under_16mib', 'unencrypted' => str_repeat('a', 2097152)]);
361-
$test->assertCollectionCount($collection->getNamespace(), 1);
362-
},
359+
yield 'Test 1' => [
360+
static function (self $test, Collection $collection) {
361+
$collection->insertOne(['_id' => 'over_2mib_under_16mib', 'unencrypted' => str_repeat('a', 2097152)]);
362+
$test->assertCollectionCount($collection->getNamespace(), 1);
363+
},
363364
];
364365

365-
yield [static function (self $test, Collection $collection, array $document) {
366-
// Test 2
367-
$collection->insertOne(
368-
['_id' => 'encryption_exceeds_2mib', 'unencrypted' => str_repeat('a', 2097152 - 2000)] + $document
369-
);
370-
$test->assertCollectionCount($collection->getNamespace(), 1);
371-
},
366+
yield 'Test 2' => [
367+
static function (self $test, Collection $collection, array $document) {
368+
$collection->insertOne(
369+
['_id' => 'encryption_exceeds_2mib', 'unencrypted' => str_repeat('a', 2097152 - 2000)] + $document
370+
);
371+
$test->assertCollectionCount($collection->getNamespace(), 1);
372+
},
372373
];
373374

374-
yield [static function (self $test, Collection $collection) {
375-
// Test 3
376-
$commands = [];
377-
(new CommandObserver())->observe(
378-
function () use ($collection) {
379-
$collection->insertMany([
380-
['_id' => 'over_2mib_1', 'unencrypted' => str_repeat('a', 2097152)],
381-
['_id' => 'over_2mib_2', 'unencrypted' => str_repeat('a', 2097152)],
382-
]);
383-
},
384-
function ($command) use (&$commands) {
385-
$commands[] = $command;
386-
}
387-
);
375+
yield 'Test 3' => [
376+
static function (self $test, Collection $collection) {
377+
$commands = [];
378+
(new CommandObserver())->observe(
379+
function () use ($collection) {
380+
$collection->insertMany([
381+
['_id' => 'over_2mib_1', 'unencrypted' => str_repeat('a', 2097152)],
382+
['_id' => 'over_2mib_2', 'unencrypted' => str_repeat('a', 2097152)],
383+
]);
384+
},
385+
function ($command) use (&$commands) {
386+
$commands[] = $command;
387+
}
388+
);
388389

389-
$test->assertCount(2, $commands);
390-
foreach ($commands as $command) {
391-
$test->assertSame('insert', $command['started']->getCommandName());
392-
}
393-
},
390+
$test->assertCount(2, $commands);
391+
foreach ($commands as $command) {
392+
$test->assertSame('insert', $command['started']->getCommandName());
393+
}
394+
},
394395
];
395396

396-
yield [static function (self $test, Collection $collection, array $document) {
397-
// Test 4
398-
$commands = [];
399-
(new CommandObserver())->observe(
400-
function () use ($collection, $document) {
401-
$collection->insertMany([
402-
[
403-
'_id' => 'encryption_exceeds_2mib_1',
404-
'unencrypted' => str_repeat('a', 2097152 - 2000),
405-
] + $document,
406-
[
407-
'_id' => 'encryption_exceeds_2mib_2',
408-
'unencrypted' => str_repeat('a', 2097152 - 2000),
409-
] + $document,
410-
]);
411-
},
412-
function ($command) use (&$commands) {
413-
$commands[] = $command;
414-
}
415-
);
397+
yield 'Test 4' => [
398+
static function (self $test, Collection $collection, array $document) {
399+
$commands = [];
400+
(new CommandObserver())->observe(
401+
function () use ($collection, $document) {
402+
$collection->insertMany([
403+
[
404+
'_id' => 'encryption_exceeds_2mib_1',
405+
'unencrypted' => str_repeat('a', 2097152 - 2000),
406+
] + $document,
407+
[
408+
'_id' => 'encryption_exceeds_2mib_2',
409+
'unencrypted' => str_repeat('a', 2097152 - 2000),
410+
] + $document,
411+
]);
412+
},
413+
function ($command) use (&$commands) {
414+
$commands[] = $command;
415+
}
416+
);
416417

417-
$test->assertCount(2, $commands);
418-
foreach ($commands as $command) {
419-
$test->assertSame('insert', $command['started']->getCommandName());
420-
}
421-
},
418+
$test->assertCount(2, $commands);
419+
foreach ($commands as $command) {
420+
$test->assertSame('insert', $command['started']->getCommandName());
421+
}
422+
},
422423
];
423424

424-
yield [static function (self $test, Collection $collection) {
425-
// Test 5
426-
$collection->insertOne(['_id' => 'under_16mib', 'unencrypted' => str_repeat('a', 16777216 - 2000)]);
427-
$test->assertCollectionCount($collection->getNamespace(), 1);
428-
},
425+
yield 'Test 5' => [
426+
static function (self $test, Collection $collection) {
427+
$collection->insertOne(['_id' => 'under_16mib', 'unencrypted' => str_repeat('a', 16777216 - 2000)]);
428+
$test->assertCollectionCount($collection->getNamespace(), 1);
429+
},
429430
];
430431

431-
yield [static function (self $test, Collection $collection, array $document) {
432-
// Test 6
433-
$test->expectException(BulkWriteException::class);
434-
$test->expectExceptionMessageMatches('#object to insert too large#');
435-
$collection->insertOne(['_id' => 'encryption_exceeds_16mib', 'unencrypted' => str_repeat('a', 16777216 - 2000)] + $document);
436-
},
432+
yield 'Test 6' => [
433+
static function (self $test, Collection $collection, array $document) {
434+
$test->expectException(BulkWriteException::class);
435+
$test->expectExceptionMessageMatches('#object to insert too large#');
436+
$collection->insertOne(['_id' => 'encryption_exceeds_16mib', 'unencrypted' => str_repeat('a', 16777216 - 2000)] + $document);
437+
},
437438
];
438439
}
439440

@@ -553,19 +554,21 @@ public function testCorpus($schemaMap = true)
553554

554555
$collection = $clientEncrypted->selectCollection('db', 'coll');
555556

557+
$unpreparedFieldNames = [
558+
'_id',
559+
'altname_aws',
560+
'altname_azure',
561+
'altname_gcp',
562+
'altname_local',
563+
];
564+
556565
foreach ($corpus as $fieldName => $data) {
557-
switch ($fieldName) {
558-
case '_id':
559-
case 'altname_aws':
560-
case 'altname_azure':
561-
case 'altname_gcp':
562-
case 'altname_local':
563-
$corpusCopied[$fieldName] = $data;
564-
break;
565-
566-
default:
567-
$corpusCopied[$fieldName] = $this->prepareCorpusData($fieldName, $data, $clientEncryption);
566+
if (in_array($fieldName, $unpreparedFieldNames, true)) {
567+
$corpusCopied[$fieldName] = $data;
568+
continue;
568569
}
570+
571+
$corpusCopied[$fieldName] = $this->prepareCorpusData($fieldName, $data, $clientEncryption);
569572
}
570573

571574
$collection->insertOne($corpusCopied);
@@ -577,13 +580,8 @@ public function testCorpus($schemaMap = true)
577580
$corpusEncryptedActual = $client->selectCollection('db', 'coll')->findOne(['_id' => 'client_side_encryption_corpus'], ['typeMap' => ['root' => 'array', 'document' => stdClass::class, 'array' => 'array']]);
578581

579582
foreach ($corpusEncryptedExpected as $fieldName => $expectedData) {
580-
switch ($fieldName) {
581-
case '_id':
582-
case 'altname_aws':
583-
case 'altname_azure':
584-
case 'altname_gcp':
585-
case 'altname_local':
586-
continue 2;
583+
if (in_array($fieldName, $unpreparedFieldNames, true)) {
584+
continue;
587585
}
588586

589587
$actualData = $corpusEncryptedActual[$fieldName];
@@ -649,80 +647,86 @@ public static function customEndpointProvider()
649647
'endpoint' => 'cloudkms.googleapis.com:443',
650648
];
651649

652-
return [
653-
'Test 1' => [
654-
static function (self $test, ClientEncryption $clientEncryption, ClientEncryption $clientEncryptionInvalid) use ($awsMasterKey) {
655-
$keyId = $clientEncryption->createDataKey('aws', ['masterKey' => $awsMasterKey]);
656-
$encrypted = $clientEncryption->encrypt('test', ['algorithm' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC, 'keyId' => $keyId]);
657-
$test->assertSame('test', $clientEncryption->decrypt($encrypted));
658-
},
659-
],
660-
'Test 2' => [
661-
static function (self $test, ClientEncryption $clientEncryption, ClientEncryption $clientEncryptionInvalid) use ($awsMasterKey) {
662-
$keyId = $clientEncryption->createDataKey('aws', ['masterKey' => $awsMasterKey + ['endpoint' => 'kms.us-east-1.amazonaws.com']]);
663-
$encrypted = $clientEncryption->encrypt('test', ['algorithm' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC, 'keyId' => $keyId]);
664-
$test->assertSame('test', $clientEncryption->decrypt($encrypted));
665-
},
666-
],
667-
'Test 3' => [
668-
static function (self $test, ClientEncryption $clientEncryption, ClientEncryption $clientEncryptionInvalid) use ($awsMasterKey) {
669-
$keyId = $clientEncryption->createDataKey('aws', ['masterKey' => $awsMasterKey + [ 'endpoint' => 'kms.us-east-1.amazonaws.com:443']]);
670-
$encrypted = $clientEncryption->encrypt('test', ['algorithm' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC, 'keyId' => $keyId]);
671-
$test->assertSame('test', $clientEncryption->decrypt($encrypted));
672-
},
673-
],
674-
'Test 4' => [
675-
static function (self $test, ClientEncryption $clientEncryption, ClientEncryption $clientEncryptionInvalid) use ($awsMasterKey) {
676-
$test->expectException(ConnectionException::class);
677-
$clientEncryption->createDataKey('aws', ['masterKey' => $awsMasterKey + ['endpoint' => 'kms.us-east-1.amazonaws.com:12345']]);
678-
},
679-
],
680-
'Test 5' => [
681-
static function (self $test, ClientEncryption $clientEncryption, ClientEncryption $clientEncryptionInvalid) use ($awsMasterKey) {
682-
$test->expectException(RuntimeException::class);
683-
$test->expectExceptionMessageMatches('#us-east-1#');
684-
$clientEncryption->createDataKey('aws', ['masterKey' => $awsMasterKey + ['endpoint' => 'kms.us-east-2.amazonaws.com']]);
685-
},
686-
],
687-
'Test 6' => [
688-
static function (self $test, ClientEncryption $clientEncryption, ClientEncryption $clientEncryptionInvalid) use ($awsMasterKey) {
689-
$test->expectException(RuntimeException::class);
690-
$test->expectExceptionMessageMatches('#parse error#');
691-
$clientEncryption->createDataKey('aws', ['masterKey' => $awsMasterKey + ['endpoint' => 'example.com']]);
692-
},
693-
],
694-
'Test 7' => [
695-
static function (self $test, ClientEncryption $clientEncryption, ClientEncryption $clientEncryptionInvalid) use ($azureMasterKey) {
696-
$keyId = $clientEncryption->createDataKey('azure', ['masterKey' => $azureMasterKey]);
697-
$encrypted = $clientEncryption->encrypt('test', ['algorithm' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC, 'keyId' => $keyId]);
698-
$test->assertSame('test', $clientEncryption->decrypt($encrypted));
699-
700-
$test->expectException(RuntimeException::class);
701-
$test->expectExceptionMessageMatches('#parse error#');
702-
$clientEncryptionInvalid->createDataKey('azure', ['masterKey' => $azureMasterKey]);
703-
},
704-
],
705-
'Test 8' => [
706-
static function (self $test, ClientEncryption $clientEncryption, ClientEncryption $clientEncryptionInvalid) use ($gcpMasterKey) {
707-
$keyId = $clientEncryption->createDataKey('gcp', ['masterKey' => $gcpMasterKey]);
708-
$encrypted = $clientEncryption->encrypt('test', ['algorithm' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC, 'keyId' => $keyId]);
709-
$test->assertSame('test', $clientEncryption->decrypt($encrypted));
710-
711-
$test->expectException(RuntimeException::class);
712-
$test->expectExceptionMessageMatches('#parse error#');
713-
$clientEncryptionInvalid->createDataKey('gcp', ['masterKey' => $gcpMasterKey]);
714-
},
715-
],
716-
'Test 9' => [
717-
static function (self $test, ClientEncryption $clientEncryption, ClientEncryption $clientEncryptionInvalid) use ($gcpMasterKey) {
718-
$masterKey = $gcpMasterKey;
719-
$masterKey['endpoint'] = 'example.com:443';
720-
721-
$test->expectException(RuntimeException::class);
722-
$test->expectExceptionMessageMatches('#Invalid KMS response#');
723-
$clientEncryption->createDataKey('gcp', ['masterKey' => $masterKey]);
724-
},
725-
],
650+
yield 'Test 1' => [
651+
static function (self $test, ClientEncryption $clientEncryption, ClientEncryption $clientEncryptionInvalid) use ($awsMasterKey) {
652+
$keyId = $clientEncryption->createDataKey('aws', ['masterKey' => $awsMasterKey]);
653+
$encrypted = $clientEncryption->encrypt('test', ['algorithm' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC, 'keyId' => $keyId]);
654+
$test->assertSame('test', $clientEncryption->decrypt($encrypted));
655+
},
656+
];
657+
658+
yield 'Test 2' => [
659+
static function (self $test, ClientEncryption $clientEncryption, ClientEncryption $clientEncryptionInvalid) use ($awsMasterKey) {
660+
$keyId = $clientEncryption->createDataKey('aws', ['masterKey' => $awsMasterKey + ['endpoint' => 'kms.us-east-1.amazonaws.com']]);
661+
$encrypted = $clientEncryption->encrypt('test', ['algorithm' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC, 'keyId' => $keyId]);
662+
$test->assertSame('test', $clientEncryption->decrypt($encrypted));
663+
},
664+
];
665+
666+
yield 'Test 3' => [
667+
static function (self $test, ClientEncryption $clientEncryption, ClientEncryption $clientEncryptionInvalid) use ($awsMasterKey) {
668+
$keyId = $clientEncryption->createDataKey('aws', ['masterKey' => $awsMasterKey + [ 'endpoint' => 'kms.us-east-1.amazonaws.com:443']]);
669+
$encrypted = $clientEncryption->encrypt('test', ['algorithm' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC, 'keyId' => $keyId]);
670+
$test->assertSame('test', $clientEncryption->decrypt($encrypted));
671+
},
672+
];
673+
674+
yield 'Test 4' => [
675+
static function (self $test, ClientEncryption $clientEncryption, ClientEncryption $clientEncryptionInvalid) use ($awsMasterKey) {
676+
$test->expectException(ConnectionException::class);
677+
$clientEncryption->createDataKey('aws', ['masterKey' => $awsMasterKey + ['endpoint' => 'kms.us-east-1.amazonaws.com:12345']]);
678+
},
679+
];
680+
681+
yield 'Test 5' => [
682+
static function (self $test, ClientEncryption $clientEncryption, ClientEncryption $clientEncryptionInvalid) use ($awsMasterKey) {
683+
$test->expectException(RuntimeException::class);
684+
$test->expectExceptionMessageMatches('#us-east-1#');
685+
$clientEncryption->createDataKey('aws', ['masterKey' => $awsMasterKey + ['endpoint' => 'kms.us-east-2.amazonaws.com']]);
686+
},
687+
];
688+
689+
yield 'Test 6' => [
690+
static function (self $test, ClientEncryption $clientEncryption, ClientEncryption $clientEncryptionInvalid) use ($awsMasterKey) {
691+
$test->expectException(RuntimeException::class);
692+
$test->expectExceptionMessageMatches('#parse error#');
693+
$clientEncryption->createDataKey('aws', ['masterKey' => $awsMasterKey + ['endpoint' => 'example.com']]);
694+
},
695+
];
696+
697+
yield 'Test 7' => [
698+
static function (self $test, ClientEncryption $clientEncryption, ClientEncryption $clientEncryptionInvalid) use ($azureMasterKey) {
699+
$keyId = $clientEncryption->createDataKey('azure', ['masterKey' => $azureMasterKey]);
700+
$encrypted = $clientEncryption->encrypt('test', ['algorithm' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC, 'keyId' => $keyId]);
701+
$test->assertSame('test', $clientEncryption->decrypt($encrypted));
702+
703+
$test->expectException(RuntimeException::class);
704+
$test->expectExceptionMessageMatches('#parse error#');
705+
$clientEncryptionInvalid->createDataKey('azure', ['masterKey' => $azureMasterKey]);
706+
},
707+
];
708+
709+
yield 'Test 8' => [
710+
static function (self $test, ClientEncryption $clientEncryption, ClientEncryption $clientEncryptionInvalid) use ($gcpMasterKey) {
711+
$keyId = $clientEncryption->createDataKey('gcp', ['masterKey' => $gcpMasterKey]);
712+
$encrypted = $clientEncryption->encrypt('test', ['algorithm' => ClientEncryption::AEAD_AES_256_CBC_HMAC_SHA_512_DETERMINISTIC, 'keyId' => $keyId]);
713+
$test->assertSame('test', $clientEncryption->decrypt($encrypted));
714+
715+
$test->expectException(RuntimeException::class);
716+
$test->expectExceptionMessageMatches('#parse error#');
717+
$clientEncryptionInvalid->createDataKey('gcp', ['masterKey' => $gcpMasterKey]);
718+
},
719+
];
720+
721+
yield 'Test 9' => [
722+
static function (self $test, ClientEncryption $clientEncryption, ClientEncryption $clientEncryptionInvalid) use ($gcpMasterKey) {
723+
$masterKey = $gcpMasterKey;
724+
$masterKey['endpoint'] = 'example.com:443';
725+
726+
$test->expectException(RuntimeException::class);
727+
$test->expectExceptionMessageMatches('#Invalid KMS response#');
728+
$clientEncryption->createDataKey('gcp', ['masterKey' => $masterKey]);
729+
},
726730
];
727731
}
728732

0 commit comments

Comments
 (0)