Skip to content

Commit ed07710

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

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

tests/UnifiedSpecTests/Context.php

Lines changed: 86 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,72 @@ 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']->kmip->endpoint->{'$$placeholder'})) {
383+
$clientEncryptionOpts['kmsProviders']->kmip->endpoint = static::getEnv('KMIP_ENDPOINT');
384+
}
385+
386+
if (isset($clientEncryptionOpts['kmsProviders']->local->key->{'$$placeholder'})) {
387+
$clientEncryptionOpts['kmsProviders']->local->key = ClientSideEncryptionSpecTest::LOCAL_MASTERKEY;
388+
}
389+
}
390+
391+
$this->entityMap->set($id, new ClientEncryption($clientEncryptionOpts));
392+
}
393+
319394
private function createEntityCollector(string $clientId, stdClass $o): void
320395
{
321396
Util::assertHasOnlyKeys($o, ['id', 'events']);
@@ -411,6 +486,17 @@ private function createBucket(string $id, stdClass $o): void
411486
$this->entityMap->set($id, $database->selectGridFSBucket($options), $databaseId);
412487
}
413488

489+
private static function getEnv(string $name): string
490+
{
491+
$value = getenv($name);
492+
493+
if ($value === false) {
494+
Assert::markTestSkipped(sprintf('Environment variable "%s" is not defined', $name));
495+
}
496+
497+
return $value;
498+
}
499+
414500
private static function prepareCollectionOrDatabaseOptions(array $options): array
415501
{
416502
Util::assertHasOnlyKeys($options, ['readConcern', 'readPreference', 'writeConcern']);

0 commit comments

Comments
 (0)