Skip to content

Commit f8ee3ad

Browse files
committed
Rewrite tests for SSL with X509 auth
1 parent c5dda25 commit f8ee3ad

9 files changed

+165
-238
lines changed

tests/connect/standalone-x509-0001.phpt

Lines changed: 0 additions & 57 deletions
This file was deleted.

tests/connect/standalone-x509-0002.phpt

Lines changed: 0 additions & 58 deletions
This file was deleted.

tests/connect/standalone-x509-0003.phpt

Lines changed: 0 additions & 45 deletions
This file was deleted.

tests/connect/standalone-x509-0004.phpt

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
Connect to MongoDB with SSL and X509 auth
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; NEEDS("STANDALONE_X509"); ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$SSL_DIR = realpath(__DIR__ . '/../../scripts/ssl/');
10+
11+
$driverOptions = [
12+
// libmongoc does not allow the hostname to be overridden as "server"
13+
'allow_invalid_hostname' => true,
14+
'weak_cert_validation' => false,
15+
'ca_file' => $SSL_DIR . '/ca.pem',
16+
'pem_file' => $SSL_DIR . '/client.pem',
17+
// TODO: this doesn't appear to have any effect. Does the PEM file not have a password?
18+
'pem_pwd' => 'qwerty',
19+
];
20+
21+
$manager = new MongoDB\Driver\Manager(STANDALONE_X509, ['ssl' => true], $driverOptions);
22+
$cursor = $manager->executeCommand(DATABASE_NAME, new MongoDB\Driver\Command(['ping' => 1]));
23+
var_dump($cursor->toArray()[0]);
24+
25+
?>
26+
===DONE===
27+
<?php exit(0); ?>
28+
--EXPECTF--
29+
object(stdClass)#%d (%d) {
30+
["ok"]=>
31+
float(1)
32+
}
33+
===DONE===
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
--TEST--
2+
Connect to MongoDB with SSL and X509 auth (stream context)
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; NEEDS("STANDALONE_X509"); ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$SSL_DIR = realpath(__DIR__ . '/../../scripts/ssl/');
10+
11+
$driverOptions = [
12+
'context' => stream_context_create([
13+
'ssl' => [
14+
// libmongoc does not allow the hostname to be overridden as "server"
15+
'allow_invalid_hostname' => true,
16+
'allow_self_signed' => false, // "weak_cert_validation" alias
17+
'cafile' => $SSL_DIR . '/ca.pem', // "ca_file" alias
18+
'local_cert' => $SSL_DIR . '/client.pem', // "pem_file" alias
19+
// TODO: this doesn't appear to have any effect. Does the PEM file not have a password?
20+
'passphrase' => 'qwerty', // "pem_pwd" alias
21+
],
22+
]),
23+
];
24+
25+
$manager = new MongoDB\Driver\Manager(STANDALONE_X509, ['ssl' => true], $driverOptions);
26+
$cursor = $manager->executeCommand(DATABASE_NAME, new MongoDB\Driver\Command(['ping' => 1]));
27+
var_dump($cursor->toArray()[0]);
28+
29+
?>
30+
===DONE===
31+
<?php exit(0); ?>
32+
--EXPECTF--
33+
object(stdClass)#%d (%d) {
34+
["ok"]=>
35+
float(1)
36+
}
37+
===DONE===

tests/connect/standalone-x509-error-0001.phpt

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -6,44 +6,34 @@ X509 connection should not reuse previous stream after an auth failure
66
<?php
77
require_once __DIR__ . "/../utils/basic.inc";
88

9-
function connect($dsn, $opts) {
10-
try {
11-
$manager = new MongoDB\Driver\Manager($dsn, array(), $opts);
9+
$SSL_DIR = realpath(__DIR__ . '/../../scripts/ssl/');
1210

13-
$bulk = new MongoDB\Driver\BulkWrite();
14-
$bulk->insert(array("very" => "important"));
15-
$manager->executeBulkWrite(NS, $bulk);
16-
echo "Connected\n";
17-
} catch(Exception $e) {
18-
echo get_class($e), ": ", $e->getMessage(), "\n";
19-
}
20-
return $manager;
21-
}
22-
23-
$SSL_DIR = realpath(__DIR__ . "/" . "./../../scripts/ssl/");
24-
$opts = array(
25-
"peer_name" => "server",
26-
"verify_peer" => true,
27-
"verify_peer_name" => true,
28-
"allow_self_signed" => false,
29-
"cafile" => $SSL_DIR . "/ca.pem", /* Defaults to openssl.cafile */
30-
"capath" => $SSL_DIR, /* Defaults to openssl.capath */
31-
"local_cert" => $SSL_DIR . "/client.pem",
32-
);
11+
$driverOptions = [
12+
// libmongoc does not allow the hostname to be overridden as "server"
13+
'allow_invalid_hostname' => true,
14+
'ca_file' => $SSL_DIR . '/ca.pem',
15+
'pem_file' => $SSL_DIR . '/client.pem',
16+
];
3317

34-
/* Wrong username */
18+
// Wrong username for X509 authentication
3519
$parsed = parse_url(STANDALONE_X509);
36-
$dsn = sprintf("mongodb://username@%s:%d/%s?ssl=true&authMechanism=MONGODB-X509", $parsed["host"], $parsed["port"], DATABASE_NAME);
20+
$dsn = sprintf('mongodb://username@%s:%d/?ssl=true&authMechanism=MONGODB-X509', $parsed['host'], $parsed['port']);
3721

38-
$m1 = connect($dsn, $opts);
39-
$m2 = connect($dsn, $opts);
22+
// Both should fail with auth failure, without reusing the previous stream
23+
for ($i = 0; $i < 2; $i++) {
24+
echo throws(function() use ($dsn, $driverOptions) {
25+
$manager = new MongoDB\Driver\Manager($dsn, [], $driverOptions);
26+
$cursor = $manager->executeCommand(DATABASE_NAME, new MongoDB\Driver\Command(['ping' => 1]));
27+
var_dump($cursor->toArray()[0]);
28+
}, 'MongoDB\Driver\Exception\AuthenticationException', 'executeCommand'), "\n";
29+
}
4030

41-
echo "Both should have failed with auth failure - without reusing previous stream\n";
4231
?>
4332
===DONE===
4433
<?php exit(0); ?>
4534
--EXPECTF--
46-
MongoDB\Driver\Exception\AuthenticationException: auth failed
47-
MongoDB\Driver\Exception\AuthenticationException: auth failed
48-
Both should have failed with auth failure - without reusing previous stream
35+
OK: Got MongoDB\Driver\Exception\AuthenticationException thrown from executeCommand
36+
auth failed
37+
OK: Got MongoDB\Driver\Exception\AuthenticationException thrown from executeCommand
38+
auth failed
4939
===DONE===
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
Connect to MongoDB with SSL and X509 auth and username retrieved from cert
3+
--SKIPIF--
4+
<?php require __DIR__ . "/../utils/basic-skipif.inc"; NEEDS("STANDALONE_X509"); ?>
5+
--FILE--
6+
<?php
7+
require_once __DIR__ . "/../utils/basic.inc";
8+
9+
$SSL_DIR = realpath(__DIR__ . '/../../scripts/ssl/');
10+
11+
$driverOptions = [
12+
// libmongoc does not allow the hostname to be overridden as "server"
13+
'allow_invalid_hostname' => true,
14+
'weak_cert_validation' => false,
15+
'ca_file' => $SSL_DIR . '/ca.pem',
16+
'pem_file' => $SSL_DIR . '/client.pem',
17+
];
18+
19+
$parsed = parse_url(STANDALONE_X509);
20+
// TODO: authMechanism cannot be parsed from URI options array (PHPC-772)
21+
$uri = sprintf('mongodb://%s:%d/?ssl=true&authMechanism=MONGODB-X509', $parsed['host'], $parsed['port']);
22+
23+
$manager = new MongoDB\Driver\Manager($uri, [], $driverOptions);
24+
$cursor = $manager->executeCommand(DATABASE_NAME, new MongoDB\Driver\Command(['ping' => 1]));
25+
var_dump($cursor->toArray()[0]);
26+
27+
?>
28+
===DONE===
29+
<?php exit(0); ?>
30+
--EXPECTF--
31+
object(stdClass)#%d (%d) {
32+
["ok"]=>
33+
float(1)
34+
}
35+
===DONE===

0 commit comments

Comments
 (0)