Skip to content

Commit 66fc493

Browse files
committed
PHPC-2096: Implement Manager::getEncryptedFieldsMap()
PHPLIB will need to access the encryptedFieldsMap autoEncryption option.
1 parent 1b8543b commit 66fc493

File tree

4 files changed

+106
-0
lines changed

4 files changed

+106
-0
lines changed

src/MongoDB/Manager.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,21 @@ static PHP_METHOD(Manager, executeBulkWrite)
608608
}
609609
} /* }}} */
610610

611+
/* {{{ proto array|object|null MongoDB\Driver\Manager::getEncryptedFieldsMap()
612+
Returns the autoEncryption.encryptedFieldsMap driver option */
613+
static PHP_METHOD(Manager, getEncryptedFieldsMap)
614+
{
615+
php_phongo_manager_t* intern;
616+
617+
intern = Z_MANAGER_OBJ_P(getThis());
618+
619+
PHONGO_PARSE_PARAMETERS_NONE();
620+
621+
if (!Z_ISUNDEF(intern->enc_fields_map)) {
622+
RETURN_ZVAL(&intern->enc_fields_map, 1, 0);
623+
}
624+
} /* }}} */
625+
611626
/* {{{ proto MongoDB\Driver\ReadConcern MongoDB\Driver\Manager::getReadConcern()
612627
Returns the ReadConcern associated with this Manager */
613628
static PHP_METHOD(Manager, getReadConcern)
@@ -894,6 +909,7 @@ static zend_function_entry php_phongo_manager_me[] = {
894909
PHP_ME(Manager, executeReadWriteCommand, ai_Manager_executeCommand, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
895910
PHP_ME(Manager, executeQuery, ai_Manager_executeQuery, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
896911
PHP_ME(Manager, executeBulkWrite, ai_Manager_executeBulkWrite, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
912+
PHP_ME(Manager, getEncryptedFieldsMap, ai_Manager_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
897913
PHP_ME(Manager, getReadConcern, ai_Manager_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
898914
PHP_ME(Manager, getReadPreference, ai_Manager_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
899915
PHP_ME(Manager, getServers, ai_Manager_void, ZEND_ACC_PUBLIC | ZEND_ACC_FINAL)
@@ -938,6 +954,10 @@ static void php_phongo_manager_free_object(zend_object* object) /* {{{ */
938954
efree(intern->client_hash);
939955
}
940956

957+
if (!Z_ISUNDEF(intern->enc_fields_map)) {
958+
zval_ptr_dtor(&intern->enc_fields_map);
959+
}
960+
941961
/* Free the keyVaultClient last to ensure that potential non-persistent
942962
* clients are destroyed in the correct order */
943963
if (!Z_ISUNDEF(intern->key_vault_client_manager)) {

src/phongo_client.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,6 +1303,10 @@ static bool phongo_manager_set_auto_encryption_opts(php_phongo_manager_t* manage
13031303
mongoc_auto_encryption_opts_set_encrypted_fields_map(auto_encryption_opts, &bson_map);
13041304

13051305
bson_destroy(&bson_map);
1306+
1307+
/* Copy the encryptedFieldsMap to the Manager since PHPLIB may need to
1308+
* access it for createCollection and dropCollection helpers. */
1309+
ZVAL_ZVAL(&manager->enc_fields_map, enc_fields_map, 1, 0);
13061310
}
13071311

13081312
if (php_array_existsc(zAutoEncryptionOpts, "keyVaultClient")) {

src/phongo_structs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ typedef struct {
8181
char* client_hash;
8282
size_t client_hash_len;
8383
bool use_persistent_client;
84+
zval enc_fields_map;
8485
zval key_vault_client_manager;
8586
HashTable* subscribers;
8687
zend_object std;
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
--TEST--
2+
MongoDB\Driver\Manager::getEncryptedFieldsMap()
3+
--FILE--
4+
<?php
5+
6+
/* autoEncryption requires keyVaultNamespace and a kmsProvider. Additionally,
7+
* disable mongocryptd spawning since it is not required for this test. */
8+
$baseOptions = [
9+
'keyVaultNamespace' => 'encryption.__keyVault',
10+
'kmsProviders' => ['local' => ['key' => new MongoDB\BSON\Binary(str_repeat('0', 96), 0)]],
11+
'extraOptions' => ['mongocryptdBypassSpawn' => true],
12+
];
13+
14+
$encryptedFields = [
15+
'escCollection' => 'escCollectionName',
16+
'eccCollection' => 'eccCollectionName',
17+
'ecocCollection' => 'ecocCollectionName',
18+
'fields' => [
19+
[
20+
'path' => 'foo',
21+
'keyId' => new MongoDB\BSON\Binary(str_repeat('0', 16), MongoDB\BSON\Binary::TYPE_UUID),
22+
'bsonType' => 'string',
23+
'queries' => ['queryType' => 'equality'],
24+
],
25+
],
26+
];
27+
28+
$tests = [
29+
[],
30+
['autoEncryption' => $baseOptions],
31+
['autoEncryption' => ['encryptedFieldsMap' => []] + $baseOptions],
32+
['autoEncryption' => ['encryptedFieldsMap' => ['db.coll' => $encryptedFields]] + $baseOptions],
33+
];
34+
35+
foreach ($tests as $i => $driverOptions) {
36+
$manager = new MongoDB\Driver\Manager(null, [], $driverOptions);
37+
var_dump($manager->getEncryptedFieldsMap());
38+
}
39+
40+
?>
41+
===DONE===
42+
<?php exit(0); ?>
43+
--EXPECTF--
44+
NULL
45+
NULL
46+
array(0) {
47+
}
48+
array(1) {
49+
["db.coll"]=>
50+
array(4) {
51+
["escCollection"]=>
52+
string(17) "escCollectionName"
53+
["eccCollection"]=>
54+
string(17) "eccCollectionName"
55+
["ecocCollection"]=>
56+
string(18) "ecocCollectionName"
57+
["fields"]=>
58+
array(1) {
59+
[0]=>
60+
array(4) {
61+
["path"]=>
62+
string(3) "foo"
63+
["keyId"]=>
64+
object(MongoDB\BSON\Binary)#%d (%d) {
65+
["data"]=>
66+
string(16) "0000000000000000"
67+
["type"]=>
68+
int(4)
69+
}
70+
["bsonType"]=>
71+
string(6) "string"
72+
["queries"]=>
73+
array(1) {
74+
["queryType"]=>
75+
string(8) "equality"
76+
}
77+
}
78+
}
79+
}
80+
}
81+
===DONE===

0 commit comments

Comments
 (0)