Skip to content

Commit 5e76ca5

Browse files
committed
PHPLIB-512: Expose ClientEncryption API in MongoDB\Client
1 parent 2fb7027 commit 5e76ca5

File tree

2 files changed

+73
-0
lines changed

2 files changed

+73
-0
lines changed

src/Client.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace MongoDB;
1919

20+
use MongoDB\Driver\ClientEncryption;
2021
use MongoDB\Driver\Exception\InvalidArgumentException as DriverInvalidArgumentException;
2122
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
2223
use MongoDB\Driver\Manager;
@@ -161,6 +162,26 @@ public function __toString()
161162
return $this->uri;
162163
}
163164

165+
/**
166+
* Returns a ClientEncryption instance for explicit encryption and decryption
167+
*
168+
* @param array $options Encryption options
169+
*
170+
* @return ClientEncryption
171+
*/
172+
public function createClientEncryption(array $options)
173+
{
174+
if (isset($options['keyVaultClient'])) {
175+
if ($options['keyVaultClient'] instanceof self) {
176+
$options['keyVaultClient'] = $options['keyVaultClient']->manager;
177+
} elseif (! $options['keyVaultClient'] instanceof Manager) {
178+
throw InvalidArgumentException::invalidType('"keyVaultClient" option', $options['keyVaultClient'], [self::class, Manager::class]);
179+
}
180+
}
181+
182+
return $this->manager->createClientEncryption($options);
183+
}
184+
164185
/**
165186
* Drop a database.
166187
*

tests/ClientTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace MongoDB\Tests;
44

55
use MongoDB\Client;
6+
use MongoDB\Driver\ClientEncryption;
67
use MongoDB\Driver\ReadConcern;
78
use MongoDB\Driver\ReadPreference;
89
use MongoDB\Driver\WriteConcern;
@@ -173,4 +174,55 @@ public function testSelectDatabasePassesOptions()
173174
$this->assertInstanceOf(WriteConcern::class, $debug['writeConcern']);
174175
$this->assertSame(WriteConcern::MAJORITY, $debug['writeConcern']->getW());
175176
}
177+
178+
public function testCreateClientEncryption()
179+
{
180+
$client = new Client(static::getUri());
181+
182+
$options = [
183+
'keyVaultNamespace' => 'default.keys',
184+
'kmsProviders' => ['aws' => ['accessKeyId' => 'abc', 'secretAccessKey' => 'def']],
185+
];
186+
187+
$clientEncryption = $client->createClientEncryption($options);
188+
$this->assertInstanceOf(ClientEncryption::class, $clientEncryption);
189+
}
190+
191+
public function testCreateClientEncryptionWithKeyVaultClient()
192+
{
193+
$client = new Client(static::getUri());
194+
195+
$options = [
196+
'keyVaultClient' => $client,
197+
'keyVaultNamespace' => 'default.keys',
198+
'kmsProviders' => ['aws' => ['accessKeyId' => 'abc', 'secretAccessKey' => 'def']],
199+
];
200+
201+
$clientEncryption = $client->createClientEncryption($options);
202+
$this->assertInstanceOf(ClientEncryption::class, $clientEncryption);
203+
}
204+
205+
public function testCreateClientEncryptionWithManager()
206+
{
207+
$client = new Client(static::getUri());
208+
209+
$options = [
210+
'keyVaultClient' => $client->getManager(),
211+
'keyVaultNamespace' => 'default.keys',
212+
'kmsProviders' => ['aws' => ['accessKeyId' => 'abc', 'secretAccessKey' => 'def']],
213+
];
214+
215+
$clientEncryption = $client->createClientEncryption($options);
216+
$this->assertInstanceOf(ClientEncryption::class, $clientEncryption);
217+
}
218+
219+
public function testCreateClientEncryptionWithInvalidKeyVaultClient()
220+
{
221+
$client = new Client(static::getUri());
222+
223+
$this->expectException(InvalidArgumentException::class);
224+
$this->expectExceptionMessage('Expected "keyVaultClient" option to have type "MongoDB\Client" or "MongoDB\Driver\Manager" but found "string"');
225+
226+
$client->createClientEncryption(['keyVaultClient' => 'foo']);
227+
}
176228
}

0 commit comments

Comments
 (0)