Skip to content

PHPC-2103: Report loaded crypt_shared version in phpinfo() #1351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions php_phongo.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,17 @@ PHP_MINFO_FUNCTION(mongodb) /* {{{ */
#else /* MONGOCRYPT_ENABLE_CRYPTO */
php_info_print_table_row(2, "libmongocrypt crypto", "disabled");
#endif

{
const char* crypt_shared_version = php_phongo_crypt_shared_version();

if (crypt_shared_version) {
php_info_print_table_row(2, "crypt_shared library version", crypt_shared_version);
} else {
php_info_print_table_row(2, "crypt_shared library version", "unknown");
}
}

#else /* MONGOC_ENABLE_CLIENT_SIDE_ENCRYPTION */
php_info_print_table_row(2, "libmongocrypt", "disabled");
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/LIBMONGOC_VERSION_CURRENT
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.22.1
1.11.1-20220829+git623d659f00
2 changes: 1 addition & 1 deletion src/libmongoc
Submodule libmongoc updated 235 files
41 changes: 41 additions & 0 deletions src/phongo_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,47 @@ typedef struct {
bool is_persistent;
} php_phongo_pclient_t;

static const mongoc_client_t* get_first_pclient_client(HashTable* ht)
{
if (ht) {
php_phongo_pclient_t* pclient = NULL;

zend_hash_internal_pointer_reset(ht);
pclient = zend_hash_get_current_data_ptr(ht);

if (pclient) {
return pclient->client;
}
}

return NULL;
}

/* Returns the version of the crypt_shared library, or NULL if it's unavailable.
* Querying the version requires a mongoc_client_t pointer. Since the shared
* library can only be loaded once, any client will return the same result so we
* consult the first persistent or request-scoped client we can find.
*
* Note: this may incorrectly return NULL if crypt_shared was loaded through a
* mongoc_client_t since destroyed (e.g. single requested-scoped client);
* however, that's the best can do with libmongoc's API. */
const char* php_phongo_crypt_shared_version()
{
const mongoc_client_t* client = NULL;

client = get_first_pclient_client(&MONGODB_G(persistent_clients));

if (!client) {
client = get_first_pclient_client(MONGODB_G(request_clients));
}

if (client) {
return mongoc_client_get_crypt_shared_version(client);
}

return NULL;
}

static mongoc_uri_t* php_phongo_make_uri(const char* uri_string)
{
mongoc_uri_t* uri;
Expand Down
2 changes: 2 additions & 0 deletions src/phongo_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#include "phongo_classes.h"

const char* php_phongo_crypt_shared_version();

void phongo_manager_init(php_phongo_manager_t* manager, const char* uri_string, zval* options, zval* driverOptions);

void php_phongo_client_reset_once(php_phongo_manager_t* manager, int pid);
Expand Down
5 changes: 4 additions & 1 deletion tests/manager/manager-ctor-auto_encryption-002.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ $autoEncryptionOptions = [

create_test_manager(null, [], ['autoEncryption' => $autoEncryptionOptions]);

var_dump(get_module_info('crypt_shared library version'));

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
--EXPECTF--
string(%d) "mongo_crypt%a"
===DONE===
7 changes: 5 additions & 2 deletions tests/manager/manager-ctor-auto_encryption-error-004.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,17 @@ foreach ($tests as $autoEncryptionOptions) {
}, MongoDB\Driver\Exception\EncryptionException::class), "\n\n";
}

var_dump(get_module_info('crypt_shared library version'));

?>
===DONE===
<?php exit(0); ?>
--EXPECT--
--EXPECTF--
OK: Got MongoDB\Driver\Exception\EncryptionException
A crypt_shared override path was specified [/not/found], but we failed to open a dynamic library at that location

OK: Got MongoDB\Driver\Exception\EncryptionException
Option 'cryptSharedLibRequired' is 'true', but failed to load the crypt_shared runtime libary
Option 'cryptSharedLibRequired' is 'true', but failed to load the crypt_shared runtime lib%r(r?)%rary
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice way to work around the typo being fixed eventually :)


string(7) "unknown"
===DONE===
2 changes: 1 addition & 1 deletion tests/utils/tools.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function get_module_info($row)
phpinfo(INFO_MODULES);
$info = ob_get_clean();

$pattern = sprintf('/^%s([\w ]+)$/m', preg_quote($row . ' => '));
$pattern = sprintf('/^%s(.*)$/m', preg_quote($row . ' => '));

if (preg_match($pattern, $info, $matches) !== 1) {
return null;
Expand Down