|
15 | 15 | use MongoDB\Driver\Exception\ConnectionTimeoutException;
|
16 | 16 | use MongoDB\Driver\Exception\EncryptionException;
|
17 | 17 | use MongoDB\Driver\Exception\RuntimeException;
|
| 18 | +use MongoDB\Driver\Exception\ServerException; |
18 | 19 | use MongoDB\Driver\Monitoring\CommandFailedEvent;
|
19 | 20 | use MongoDB\Driver\Monitoring\CommandStartedEvent;
|
20 | 21 | use MongoDB\Driver\Monitoring\CommandSubscriber;
|
@@ -1403,6 +1404,93 @@ static function (self $test, ClientEncryption $clientEncryption, Client $encrypt
|
1403 | 1404 | ];
|
1404 | 1405 | }
|
1405 | 1406 |
|
| 1407 | + /** |
| 1408 | + * Prose test 13: Unique Index on keyAltNames |
| 1409 | + * |
| 1410 | + * @see https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/tests/README.rst#unique-index-on-keyaltnames |
| 1411 | + * @dataProvider provideUniqueIndexOnKeyAltNamesTests |
| 1412 | + */ |
| 1413 | + public function testUniqueIndexOnKeyAltNames(Closure $test): void |
| 1414 | + { |
| 1415 | + // Test setup |
| 1416 | + $client = static::createTestClient(); |
| 1417 | + |
| 1418 | + // Ensure that the key vault is dropped with a majority write concern |
| 1419 | + self::insertKeyVaultData($client, []); |
| 1420 | + |
| 1421 | + $client->selectCollection('keyvault', 'datakeys')->createIndex( |
| 1422 | + ['keyAltNames' => 1], |
| 1423 | + [ |
| 1424 | + 'unique' => true, |
| 1425 | + 'partialFilterExpression' => ['keyAltNames' => ['$exists' => true]], |
| 1426 | + 'writeConcern' => new WriteConcern(WriteConcern::MAJORITY), |
| 1427 | + ], |
| 1428 | + ); |
| 1429 | + |
| 1430 | + $clientEncryption = new ClientEncryption([ |
| 1431 | + 'keyVaultClient' => $client->getManager(), |
| 1432 | + 'keyVaultNamespace' => 'keyvault.datakeys', |
| 1433 | + 'kmsProviders' => ['local' => ['key' => new Binary(base64_decode(self::LOCAL_MASTERKEY), 0)]], |
| 1434 | + ]); |
| 1435 | + |
| 1436 | + $clientEncryption->createDataKey('local', ['keyAltNames' => ['def']]); |
| 1437 | + |
| 1438 | + $test($this, $client, $clientEncryption); |
| 1439 | + } |
| 1440 | + |
| 1441 | + public static function provideUniqueIndexOnKeyAltNamesTests() |
| 1442 | + { |
| 1443 | + // See: https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/tests/README.rst#case-1-createdatakey |
| 1444 | + yield 'Case 1: createDataKey()' => [ |
| 1445 | + static function (self $test, Client $client, ClientEncryption $clientEncryption): void { |
| 1446 | + $clientEncryption->createDataKey('local', ['keyAltNames' => ['abc']]); |
| 1447 | + |
| 1448 | + try { |
| 1449 | + $clientEncryption->createDataKey('local', ['keyAltNames' => ['abc']]); |
| 1450 | + $test->fail('Expected exception to be thrown'); |
| 1451 | + } catch (ServerException $e) { |
| 1452 | + $test->assertSame(11000 /* DuplicateKey */, $e->getCode()); |
| 1453 | + } |
| 1454 | + |
| 1455 | + try { |
| 1456 | + $clientEncryption->createDataKey('local', ['keyAltNames' => ['def']]); |
| 1457 | + $test->fail('Expected exception to be thrown'); |
| 1458 | + } catch (ServerException $e) { |
| 1459 | + $test->assertSame(11000 /* DuplicateKey */, $e->getCode()); |
| 1460 | + } |
| 1461 | + }, |
| 1462 | + ]; |
| 1463 | + |
| 1464 | + // See: https://github.com/mongodb/specifications/blob/master/source/client-side-encryption/tests/README.rst#case-2-addkeyaltname |
| 1465 | + yield 'Case 2: addKeyAltName()' => [ |
| 1466 | + static function (self $test, Client $client, ClientEncryption $clientEncryption): void { |
| 1467 | + $keyId = $clientEncryption->createDataKey('local'); |
| 1468 | + |
| 1469 | + $keyBeforeUpdate = $clientEncryption->addKeyAltName($keyId, 'abc'); |
| 1470 | + $test->assertObjectNotHasAttribute('keyAltNames', $keyBeforeUpdate); |
| 1471 | + |
| 1472 | + $keyBeforeUpdate = $clientEncryption->addKeyAltName($keyId, 'abc'); |
| 1473 | + $test->assertObjectHasAttribute('keyAltNames', $keyBeforeUpdate); |
| 1474 | + $test->assertIsArray($keyBeforeUpdate->keyAltNames); |
| 1475 | + $test->assertContains('abc', $keyBeforeUpdate->keyAltNames); |
| 1476 | + |
| 1477 | + try { |
| 1478 | + $clientEncryption->addKeyAltName($keyId, 'def'); |
| 1479 | + $test->fail('Expected exception to be thrown'); |
| 1480 | + } catch (ServerException $e) { |
| 1481 | + $test->assertSame(11000 /* DuplicateKey */, $e->getCode()); |
| 1482 | + } |
| 1483 | + |
| 1484 | + $originalKeyId = $clientEncryption->getKeyByAltName('def')->_id; |
| 1485 | + |
| 1486 | + $originalKeyBeforeUpdate = $clientEncryption->addKeyAltName($originalKeyId, 'def'); |
| 1487 | + $test->assertObjectHasAttribute('keyAltNames', $originalKeyBeforeUpdate); |
| 1488 | + $test->assertIsArray($originalKeyBeforeUpdate->keyAltNames); |
| 1489 | + $test->assertContains('def', $originalKeyBeforeUpdate->keyAltNames); |
| 1490 | + }, |
| 1491 | + ]; |
| 1492 | + } |
| 1493 | + |
1406 | 1494 | /**
|
1407 | 1495 | * Prose test 14: Decryption Events
|
1408 | 1496 | *
|
|
0 commit comments