Skip to content

Commit f866a64

Browse files
committed
Support clientEncryption entity and $$placeholder syntax
1 parent 98d318a commit f866a64

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

tests/UnifiedSpecTests/Context.php

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,19 @@
44

55
use LogicException;
66
use MongoDB\Client;
7+
use MongoDB\Driver\ClientEncryption;
78
use MongoDB\Driver\ServerApi;
89
use MongoDB\Model\BSONArray;
910
use MongoDB\Tests\FunctionalTestCase;
11+
use MongoDB\Tests\SpecTests\ClientSideEncryptionSpecTest;
12+
use PHPUnit\Framework\Assert;
1013
use stdClass;
1114

1215
use function array_key_exists;
1316
use function array_map;
1417
use function current;
1518
use function explode;
19+
use function getenv;
1620
use function key;
1721
use function PHPUnit\Framework\assertArrayHasKey;
1822
use function PHPUnit\Framework\assertCount;
@@ -24,6 +28,7 @@
2428
use function PHPUnit\Framework\assertNotEmpty;
2529
use function PHPUnit\Framework\assertNotSame;
2630
use function PHPUnit\Framework\assertSame;
31+
use function sprintf;
2732

2833
/**
2934
* Execution context for spec tests.
@@ -108,6 +113,10 @@ public function createEntities(array $entities): void
108113
$this->createClient($id, $def);
109114
break;
110115

116+
case 'clientEncryption':
117+
$this->createClientEncryption($id, $def);
118+
break;
119+
111120
case 'database':
112121
$this->createDatabase($id, $def);
113122
break;
@@ -316,6 +325,68 @@ private function createClient(string $id, stdClass $o): void
316325
$this->entityMap->set($id, FunctionalTestCase::createTestClient($uri, $uriOptions, $driverOptions));
317326
}
318327

328+
private function createClientEncryption(string $id, stdClass $o): void
329+
{
330+
Util::assertHasOnlyKeys($o, [
331+
'id',
332+
'clientEncryptionOpts',
333+
]);
334+
335+
$clientEncryptionOpts = [];
336+
337+
if (isset($o->clientEncryptionOpts)) {
338+
assertIsObject($o->clientEncryptionOpts);
339+
$clientEncryptionOpts = (array) $o->clientEncryptionOpts;
340+
}
341+
342+
if (isset($clientEncryptionOpts['keyVaultClient'])) {
343+
assertIsString($clientEncryptionOpts['keyVaultClient']);
344+
$clientEncryptionOpts['keyVaultClient'] = $this->entityMap->getClient($clientEncryptionOpts['keyVaultClient'])->getManager();
345+
}
346+
347+
if (isset($clientEncryptionOpts['kmsProviders'])) {
348+
assertIsObject($clientEncryptionOpts['kmsProviders']);
349+
350+
if (isset($clientEncryptionOpts['kmsProviders']->aws->accessKeyId->{'$$placeholder'})) {
351+
$clientEncryptionOpts['kmsProviders']->aws->accessKeyId = static::getEnv('AWS_ACCESS_KEY_ID');
352+
}
353+
354+
if (isset($clientEncryptionOpts['kmsProviders']->aws->secretAccessKey->{'$$placeholder'})) {
355+
$clientEncryptionOpts['kmsProviders']->aws->secretAccessKey = static::getEnv('AWS_SECRET_ACCESS_KEY');
356+
}
357+
358+
if (isset($clientEncryptionOpts['kmsProviders']->azure->clientId->{'$$placeholder'})) {
359+
$clientEncryptionOpts['kmsProviders']->azure->clientId = static::getEnv('AZURE_CLIENT_ID');
360+
}
361+
362+
if (isset($clientEncryptionOpts['kmsProviders']->azure->clientSecret->{'$$placeholder'})) {
363+
$clientEncryptionOpts['kmsProviders']->azure->clientSecret = static::getEnv('AZURE_CLIENT_SECRET');
364+
}
365+
366+
if (isset($clientEncryptionOpts['kmsProviders']->azure->tenantId->{'$$placeholder'})) {
367+
$clientEncryptionOpts['kmsProviders']->azure->tenantId = static::getEnv('AZURE_TENANT_ID');
368+
}
369+
370+
if (isset($clientEncryptionOpts['kmsProviders']->gcp->email->{'$$placeholder'})) {
371+
$clientEncryptionOpts['kmsProviders']->gcp->email = static::getEnv('GCP_EMAIL');
372+
}
373+
374+
if (isset($clientEncryptionOpts['kmsProviders']->gcp->privateKey->{'$$placeholder'})) {
375+
$clientEncryptionOpts['kmsProviders']->gcp->privateKey = static::getEnv('GCP_PRIVATE_KEY');
376+
}
377+
378+
if (isset($clientEncryptionOpts['kmsProviders']->kmip->endpoint->{'$$placeholder'})) {
379+
$clientEncryptionOpts['kmsProviders']->kmip->endpoint = static::getEnv('KMIP_ENDPOINT');
380+
}
381+
382+
if (isset($clientEncryptionOpts['kmsProviders']->local->key->{'$$placeholder'})) {
383+
$clientEncryptionOpts['kmsProviders']->local->key = ClientSideEncryptionSpecTest::LOCAL_MASTERKEY;
384+
}
385+
}
386+
387+
$this->entityMap->set($id, new ClientEncryption($clientEncryptionOpts));
388+
}
389+
319390
private function createEntityCollector(string $clientId, stdClass $o): void
320391
{
321392
Util::assertHasOnlyKeys($o, ['id', 'events']);
@@ -411,6 +482,17 @@ private function createBucket(string $id, stdClass $o): void
411482
$this->entityMap->set($id, $database->selectGridFSBucket($options), $databaseId);
412483
}
413484

485+
private static function getEnv(string $name): string
486+
{
487+
$value = getenv($name);
488+
489+
if ($value === false) {
490+
Assert::markTestSkipped(sprintf('Environment variable "%s" is not defined', $name));
491+
}
492+
493+
return $value;
494+
}
495+
414496
private static function prepareCollectionOrDatabaseOptions(array $options): array
415497
{
416498
Util::assertHasOnlyKeys($options, ['readConcern', 'readPreference', 'writeConcern']);

tests/UnifiedSpecTests/EntityMap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use MongoDB\Client;
88
use MongoDB\Collection;
99
use MongoDB\Database;
10+
use MongoDB\Driver\ClientEncryption;
1011
use MongoDB\Driver\Cursor;
1112
use MongoDB\Driver\Session;
1213
use MongoDB\GridFS\Bucket;
@@ -182,6 +183,7 @@ private static function isSupportedType(): Constraint
182183
if (self::$isSupportedType === null) {
183184
self::$isSupportedType = logicalOr(
184185
isInstanceOf(Client::class),
186+
isInstanceOf(ClientEncryption::class),
185187
isInstanceOf(Database::class),
186188
isInstanceOf(Collection::class),
187189
isInstanceOf(Session::class),

0 commit comments

Comments
 (0)