Skip to content

Commit c57450e

Browse files
committed
Merge pull request #709
2 parents 2fb7027 + d777f5f commit c57450e

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
source:
2+
file: apiargs-common-param.yaml
3+
ref: $options
4+
...

docs/reference/class/MongoDBClient.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Methods
3030

3131
/reference/method/MongoDBClient__construct
3232
/reference/method/MongoDBClient__get
33+
/reference/method/MongoDBClient-createClientEncryption
3334
/reference/method/MongoDBClient-dropDatabase
3435
/reference/method/MongoDBClient-getManager
3536
/reference/method/MongoDBClient-getReadConcern
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
=========================================
2+
MongoDB\\Client::createClientEncryption()
3+
=========================================
4+
5+
.. default-domain:: mongodb
6+
7+
.. contents:: On this page
8+
:local:
9+
:backlinks: none
10+
:depth: 1
11+
:class: singlecol
12+
13+
Definition
14+
----------
15+
16+
.. phpmethod:: MongoDB\\Client::createClientEncryption()
17+
18+
Returns a :php:`MongoDB\\Driver\\ClientEncryption <class.mongodb-driver-clientencryption>`
19+
object for manual encryption and decryption of values.
20+
21+
.. code-block:: php
22+
23+
function createClientEncryption(array $options): MongoDB\Driver\ClientEncryption
24+
25+
This method has the following parameters:
26+
27+
.. include:: /includes/apiargs/MongoDBClient-method-createClientEncryption-param.rst
28+
29+
The ``$options`` parameter supports all options documented in the
30+
:php:`extension manual <manual/en/mongodb-driver-manager.createclientencryption.php>`.
31+
For the ``keyVaultClient`` option, an instance of :phpclass:`MongoDB\\Client`
32+
is automatically unwrapped and the :php:`MongoDB\\Driver\\Manager <class.mongodb-driver-manager>`
33+
instance is passed to the extension.
34+
35+
Return Values
36+
-------------
37+
38+
A :php:`MongoDB\\Driver\\ClientEncryption <class.mongodb-driver-clientencryption>`
39+
instance which can be used to encrypt and decrypt values.
40+
41+
Errors/Exceptions
42+
-----------------
43+
44+
.. include:: /includes/extracts/error-invalidargumentexception.rst
45+
.. include:: /includes/extracts/error-driver-invalidargumentexception.rst
46+
47+
See Also
48+
--------
49+
50+
- :php:`MongoDB\\Driver\\Manager::createClientEncryption()
51+
<manual/en/mongodb-driver-manager.createclientencryption.php>`

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)