Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
5e76ca50
Unverified
Commit
5e76ca50
authored
Jan 10, 2020
by
Andreas Braun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-512: Expose ClientEncryption API in MongoDB\Client
parent
2fb70273
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
73 additions
and
0 deletions
+73
-0
Client.php
src/Client.php
+21
-0
ClientTest.php
tests/ClientTest.php
+52
-0
No files found.
src/Client.php
View file @
5e76ca50
...
@@ -17,6 +17,7 @@
...
@@ -17,6 +17,7 @@
namespace
MongoDB
;
namespace
MongoDB
;
use
MongoDB\Driver\ClientEncryption
;
use
MongoDB\Driver\Exception\InvalidArgumentException
as
DriverInvalidArgumentException
;
use
MongoDB\Driver\Exception\InvalidArgumentException
as
DriverInvalidArgumentException
;
use
MongoDB\Driver\Exception\RuntimeException
as
DriverRuntimeException
;
use
MongoDB\Driver\Exception\RuntimeException
as
DriverRuntimeException
;
use
MongoDB\Driver\Manager
;
use
MongoDB\Driver\Manager
;
...
@@ -161,6 +162,26 @@ class Client
...
@@ -161,6 +162,26 @@ class Client
return
$this
->
uri
;
return
$this
->
uri
;
}
}
/**
* Returns a ClientEncryption instance for explicit encryption and decryption
*
* @param array $options Encryption options
*
* @return ClientEncryption
*/
public
function
createClientEncryption
(
array
$options
)
{
if
(
isset
(
$options
[
'keyVaultClient'
]))
{
if
(
$options
[
'keyVaultClient'
]
instanceof
self
)
{
$options
[
'keyVaultClient'
]
=
$options
[
'keyVaultClient'
]
->
manager
;
}
elseif
(
!
$options
[
'keyVaultClient'
]
instanceof
Manager
)
{
throw
InvalidArgumentException
::
invalidType
(
'"keyVaultClient" option'
,
$options
[
'keyVaultClient'
],
[
self
::
class
,
Manager
::
class
]);
}
}
return
$this
->
manager
->
createClientEncryption
(
$options
);
}
/**
/**
* Drop a database.
* Drop a database.
*
*
...
...
tests/ClientTest.php
View file @
5e76ca50
...
@@ -3,6 +3,7 @@
...
@@ -3,6 +3,7 @@
namespace
MongoDB\Tests
;
namespace
MongoDB\Tests
;
use
MongoDB\Client
;
use
MongoDB\Client
;
use
MongoDB\Driver\ClientEncryption
;
use
MongoDB\Driver\ReadConcern
;
use
MongoDB\Driver\ReadConcern
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\ReadPreference
;
use
MongoDB\Driver\WriteConcern
;
use
MongoDB\Driver\WriteConcern
;
...
@@ -173,4 +174,55 @@ class ClientTest extends TestCase
...
@@ -173,4 +174,55 @@ class ClientTest extends TestCase
$this
->
assertInstanceOf
(
WriteConcern
::
class
,
$debug
[
'writeConcern'
]);
$this
->
assertInstanceOf
(
WriteConcern
::
class
,
$debug
[
'writeConcern'
]);
$this
->
assertSame
(
WriteConcern
::
MAJORITY
,
$debug
[
'writeConcern'
]
->
getW
());
$this
->
assertSame
(
WriteConcern
::
MAJORITY
,
$debug
[
'writeConcern'
]
->
getW
());
}
}
public
function
testCreateClientEncryption
()
{
$client
=
new
Client
(
static
::
getUri
());
$options
=
[
'keyVaultNamespace'
=>
'default.keys'
,
'kmsProviders'
=>
[
'aws'
=>
[
'accessKeyId'
=>
'abc'
,
'secretAccessKey'
=>
'def'
]],
];
$clientEncryption
=
$client
->
createClientEncryption
(
$options
);
$this
->
assertInstanceOf
(
ClientEncryption
::
class
,
$clientEncryption
);
}
public
function
testCreateClientEncryptionWithKeyVaultClient
()
{
$client
=
new
Client
(
static
::
getUri
());
$options
=
[
'keyVaultClient'
=>
$client
,
'keyVaultNamespace'
=>
'default.keys'
,
'kmsProviders'
=>
[
'aws'
=>
[
'accessKeyId'
=>
'abc'
,
'secretAccessKey'
=>
'def'
]],
];
$clientEncryption
=
$client
->
createClientEncryption
(
$options
);
$this
->
assertInstanceOf
(
ClientEncryption
::
class
,
$clientEncryption
);
}
public
function
testCreateClientEncryptionWithManager
()
{
$client
=
new
Client
(
static
::
getUri
());
$options
=
[
'keyVaultClient'
=>
$client
->
getManager
(),
'keyVaultNamespace'
=>
'default.keys'
,
'kmsProviders'
=>
[
'aws'
=>
[
'accessKeyId'
=>
'abc'
,
'secretAccessKey'
=>
'def'
]],
];
$clientEncryption
=
$client
->
createClientEncryption
(
$options
);
$this
->
assertInstanceOf
(
ClientEncryption
::
class
,
$clientEncryption
);
}
public
function
testCreateClientEncryptionWithInvalidKeyVaultClient
()
{
$client
=
new
Client
(
static
::
getUri
());
$this
->
expectException
(
InvalidArgumentException
::
class
);
$this
->
expectExceptionMessage
(
'Expected "keyVaultClient" option to have type "MongoDB\Client" or "MongoDB\Driver\Manager" but found "string"'
);
$client
->
createClientEncryption
([
'keyVaultClient'
=>
'foo'
]);
}
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment