Skip to content

Commit 9c06e70

Browse files
committed
PHPC-33: Implement X509 support via PHP streams
1 parent 79d9bd5 commit 9c06e70

File tree

3 files changed

+32
-11
lines changed

3 files changed

+32
-11
lines changed

src/MongoDB/Manager.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
# include "config.h"
2525
#endif
2626

27+
/* YCM */
28+
#include <strings.h>
2729
/* External libs */
2830
#include <bson.h>
2931
#include <mongoc.h>
@@ -78,7 +80,26 @@ PHP_METHOD(Manager, __construct)
7880
zval **tmp;
7981

8082
if (zend_hash_find(Z_ARRVAL_P(driverOptions), "context", strlen("context") + 1, (void**)&tmp) == SUCCESS) {
83+
const mongoc_uri_t *muri = mongoc_client_get_uri(intern->client);
84+
const char *mech = mongoc_uri_get_auth_mechanism(muri);
8185
ctx = php_stream_context_from_zval(*tmp, PHP_FILE_NO_DEFAULT_CONTEXT);
86+
87+
/* Check if we are doing X509 auth, in which case extract the username (subject) from the cert if no username is provided */
88+
if (mech && !strcasecmp(mech, "MONGODB-X509") && !mongoc_uri_get_username(muri)) {
89+
zval **pem;
90+
91+
if (SUCCESS == php_stream_context_get_option(ctx, "ssl", "local_cert", &pem)) {
92+
char filename[MAXPATHLEN];
93+
94+
convert_to_string_ex(pem);
95+
if (VCWD_REALPATH(Z_STRVAL_PP(pem), filename)) {
96+
mongoc_ssl_opt_t ssl_options;
97+
98+
ssl_options.pem_file = filename;
99+
mongoc_client_set_ssl_opts(intern->client, &ssl_options);
100+
}
101+
}
102+
}
82103
}
83104

84105
if (zend_hash_find(Z_ARRVAL_P(driverOptions), "debug", strlen("debug") + 1, (void**)&tmp) == SUCCESS) {

tests/connect/standalone-x509-0001.phpt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,17 +45,16 @@ $cmd = array(
4545
);
4646

4747
try {
48-
echo "User Created\n";
4948
$command = new MongoDB\Command($cmd);
50-
$result = $adminmanager->executeCommand(DATABASE_NAME, $command);
49+
$result = $adminmanager->executeCommand('$external', $command);
5150
echo "User Created\n";
5251
} catch(Exception $e) {
5352
echo get_class($e), ": ", $e->getMessage(), "\n";
5453
}
5554

5655
try {
5756
$parsed = parse_url(MONGODB_STANDALONE_X509_URI);
58-
$dsn = sprintf("mongodb://%s@%s:%d/%s?ssl=true&authMechanism=MONGODB-X509", urlencode($certusername), $parsed["host"], $parsed["port"], DATABASE_NAME);
57+
$dsn = sprintf("mongodb://%s@%s:%d/%s?ssl=true&authMechanism=MONGODB-X509", $certusername, $parsed["host"], $parsed["port"], DATABASE_NAME);
5958

6059
$manager = new MongoDB\Manager($dsn, array(), array("context" => $context, "debug" => STDERR));
6160

@@ -67,14 +66,15 @@ try {
6766
foreach($cursor as $document) {
6867
var_dump($document["very"]);
6968
}
69+
$command = new MongoDB\Command(array("drop" => COLLECTION_NAME));
70+
$result = $manager->executeCommand(DATABASE_NAME, $command);
7071
} catch(Exception $e) {
7172
echo get_class($e), ": ", $e->getMessage(), "\n";
7273
}
7374

7475
try {
75-
echo "User dropped\n";
76-
$command = new MongoDB\Command(array("drop" => COLLECTION_NAME));
77-
$result = $adminmanager->executeCommand(DATABASE_NAME, $command);
76+
$command = new MongoDB\Command(array("dropUser" => $certusername));
77+
$result = $adminmanager->executeCommand('$external', $command);
7878
echo "User dropped\n";
7979
} catch(Exception $e) {
8080
echo get_class($e), ": ", $e->getMessage(), "\n";

tests/connect/standalone-x509-0002.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ $cmd = array(
4545
);
4646

4747
try {
48-
echo "User Created\n";
4948
$command = new MongoDB\Command($cmd);
50-
$result = $adminmanager->executeCommand(DATABASE_NAME, $command);
49+
$result = $adminmanager->executeCommand('$external', $command);
5150
echo "User Created\n";
5251
} catch(Exception $e) {
5352
echo get_class($e), ": ", $e->getMessage(), "\n";
@@ -68,14 +67,15 @@ try {
6867
foreach($cursor as $document) {
6968
var_dump($document["very"]);
7069
}
70+
$command = new MongoDB\Command(array("drop" => COLLECTION_NAME));
71+
$result = $manager->executeCommand(DATABASE_NAME, $command);
7172
} catch(Exception $e) {
7273
echo get_class($e), ": ", $e->getMessage(), "\n";
7374
}
7475

7576
try {
76-
echo "User dropped\n";
77-
$command = new MongoDB\Command(array("drop" => COLLECTION_NAME));
78-
$result = $adminmanager->executeCommand(DATABASE_NAME, $command);
77+
$command = new MongoDB\Command(array("dropUser" => $certusername));
78+
$result = $adminmanager->executeCommand('$external', $command);
7979
echo "User dropped\n";
8080
} catch(Exception $e) {
8181
echo get_class($e), ": ", $e->getMessage(), "\n";

0 commit comments

Comments
 (0)