Skip to content

Commit 72ad6a8

Browse files
committed
PHPC-33: Add tests for X509
This test does not currently work due to bug in Mongo Orchestration: mongodb-labs/mongo-orchestration#159
1 parent f95bc6f commit 72ad6a8

File tree

5 files changed

+187
-2
lines changed

5 files changed

+187
-2
lines changed

Diff for: scripts/presets/standalone-ssl.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"setParameter": {"enableTestCommands": 1}
1212
},
1313
"sslParams": {
14+
"sslMode": "requireSSL",
1415
"sslCAFile": "/phongo/scripts/ssl/ca.pem",
15-
"sslOnNormalPorts": true,
1616
"sslPEMKeyFile": "/phongo/scripts/ssl/server.pem",
1717
"sslWeakCertificateValidation": true
1818
}

Diff for: scripts/presets/standalone-x509.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
"setParameter": {"enableTestCommands": 1}
1515
},
1616
"sslParams": {
17+
"clusterAuthMode": "x509",
18+
"sslMode": "requireSSL",
1719
"sslCAFile": "/phongo/scripts/ssl/ca.pem",
18-
"sslOnNormalPorts": true,
1920
"sslPEMKeyFile": "/phongo/scripts/ssl/server.pem",
2021
"sslWeakCertificateValidation": true
2122
}

Diff for: tests/connect/standalone-x509-0001.phpt

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
--TEST--
2+
Connect to MongoDB with using SSL and verify the stream
3+
--SKIPIF--
4+
<?php require "tests/utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/basic.inc";
8+
9+
$SSL_DIR = realpath(__DIR__ . "/" . "./../../scripts/ssl/");
10+
11+
$opts = array(
12+
"ssl" => array(
13+
"peer_name" => "MongoDB",
14+
"verify_peer" => true,
15+
"verify_peer_name" => true,
16+
"allow_self_signed" => false,
17+
"cafile" => $SSL_DIR . "/ca.pem", /* Defaults to openssl.cafile */
18+
"capath" => $SSL_DIR, /* Defaults to openssl.capath */
19+
"local_cert" => $SSL_DIR . "/client.pem",
20+
"passphrase" => "Very secretive client.pem passphrase",
21+
"CN_match" => "server",
22+
"verify_depth" => 5,
23+
"ciphers" => "HIGH:!EXPORT:!aNULL@STRENGTH",
24+
"capture_peer_cert" => true,
25+
"capture_peer_cert_chain" => true,
26+
"SNI_enabled" => true,
27+
"disable_compression" => false,
28+
"peer_fingerprint" => "0d6dbd95",
29+
),
30+
);
31+
$context = stream_context_create($opts);
32+
33+
$parsed = parse_url(MONGODB_STANDALONE_X509_URI);
34+
$adminuser = "root";
35+
$adminpass = "toor";
36+
$dsn = sprintf("mongodb://%s:%s@%s:%d/admin?ssl=true", $adminuser, $adminpass, $parsed["host"], $parsed["port"]);
37+
$adminmanager = new MongoDB\Manager($dsn, array(), array("context" => $context, "debug" => STDERR));
38+
39+
$certusername = "C=US,ST=New York,L=New York City,O=MongoDB,OU=KernelUser,CN=client";
40+
41+
42+
$cmd = array(
43+
"createUser" => $certusername,
44+
"roles" => [["role" => "readWrite", "db" => DATABASE_NAME]],
45+
);
46+
47+
try {
48+
echo "User Created\n";
49+
$command = new MongoDB\Command($cmd);
50+
$result = $adminmanager->executeCommand(DATABASE_NAME, $command);
51+
echo "User Created\n";
52+
} catch(Exception $e) {
53+
echo get_class($e), ": ", $e->getMessage(), "\n";
54+
}
55+
56+
try {
57+
$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);
59+
60+
$manager = new MongoDB\Manager($dsn, array(), array("context" => $context, "debug" => STDERR));
61+
62+
$batch = new MongoDB\WriteBatch();
63+
$batch->insert(array("very" => "important"));
64+
$manager->executeWriteBatch(NS, $batch);
65+
$query = new MongoDB\Query(array("very" => "important"));
66+
$cursor = $manager->executeQuery(NS, $query);
67+
foreach($cursor as $document) {
68+
var_dump($document["very"]);
69+
}
70+
} catch(Exception $e) {
71+
echo get_class($e), ": ", $e->getMessage(), "\n";
72+
}
73+
74+
try {
75+
echo "User dropped\n";
76+
$command = new MongoDB\Command(array("drop" => COLLECTION_NAME));
77+
$result = $adminmanager->executeCommand(DATABASE_NAME, $command);
78+
echo "User dropped\n";
79+
} catch(Exception $e) {
80+
echo get_class($e), ": ", $e->getMessage(), "\n";
81+
}
82+
83+
84+
?>
85+
===DONE===
86+
<?php exit(0); ?>
87+
--EXPECTF--
88+
User Created
89+
string(9) "important"
90+
User dropped
91+
===DONE===

Diff for: tests/connect/standalone-x509-0002.phpt

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
--TEST--
2+
Connect to MongoDB with using X509 retrieving username from certificate #002
3+
--SKIPIF--
4+
<?php require "tests/utils/basic-skipif.inc"?>
5+
--FILE--
6+
<?php
7+
require_once "tests/utils/basic.inc";
8+
9+
$SSL_DIR = realpath(__DIR__ . "/" . "./../../scripts/ssl/");
10+
11+
$opts = array(
12+
"ssl" => array(
13+
"peer_name" => "MongoDB",
14+
"verify_peer" => true,
15+
"verify_peer_name" => true,
16+
"allow_self_signed" => false,
17+
"cafile" => $SSL_DIR . "/ca.pem", /* Defaults to openssl.cafile */
18+
"capath" => $SSL_DIR, /* Defaults to openssl.capath */
19+
"local_cert" => $SSL_DIR . "/client.pem",
20+
"passphrase" => "Very secretive client.pem passphrase",
21+
"CN_match" => "server",
22+
"verify_depth" => 5,
23+
"ciphers" => "HIGH:!EXPORT:!aNULL@STRENGTH",
24+
"capture_peer_cert" => true,
25+
"capture_peer_cert_chain" => true,
26+
"SNI_enabled" => true,
27+
"disable_compression" => false,
28+
"peer_fingerprint" => "0d6dbd95",
29+
),
30+
);
31+
$context = stream_context_create($opts);
32+
33+
$parsed = parse_url(MONGODB_STANDALONE_X509_URI);
34+
$adminuser = "root";
35+
$adminpass = "toor";
36+
$dsn = sprintf("mongodb://%s:%s@%s:%d/admin?ssl=true", $adminuser, $adminpass, $parsed["host"], $parsed["port"]);
37+
$adminmanager = new MongoDB\Manager($dsn, array(), array("context" => $context, "debug" => STDERR));
38+
39+
$certusername = "C=US,ST=New York,L=New York City,O=MongoDB,OU=KernelUser,CN=client";
40+
41+
42+
$cmd = array(
43+
"createUser" => $certusername,
44+
"roles" => [["role" => "readWrite", "db" => DATABASE_NAME]],
45+
);
46+
47+
try {
48+
echo "User Created\n";
49+
$command = new MongoDB\Command($cmd);
50+
$result = $adminmanager->executeCommand(DATABASE_NAME, $command);
51+
echo "User Created\n";
52+
} catch(Exception $e) {
53+
echo get_class($e), ": ", $e->getMessage(), "\n";
54+
}
55+
56+
try {
57+
/* mongoc will pull the username of the certificate */
58+
$parsed = parse_url(MONGODB_STANDALONE_X509_URI);
59+
$dsn = sprintf("mongodb://%s:%d/%s?ssl=true&authMechanism=MONGODB-X509", $parsed["host"], $parsed["port"], DATABASE_NAME);
60+
61+
$manager = new MongoDB\Manager($dsn, array(), array("context" => $context, "debug" => STDERR));
62+
63+
$batch = new MongoDB\WriteBatch();
64+
$batch->insert(array("very" => "important"));
65+
$manager->executeWriteBatch(NS, $batch);
66+
$query = new MongoDB\Query(array("very" => "important"));
67+
$cursor = $manager->executeQuery(NS, $query);
68+
foreach($cursor as $document) {
69+
var_dump($document["very"]);
70+
}
71+
} catch(Exception $e) {
72+
echo get_class($e), ": ", $e->getMessage(), "\n";
73+
}
74+
75+
try {
76+
echo "User dropped\n";
77+
$command = new MongoDB\Command(array("drop" => COLLECTION_NAME));
78+
$result = $adminmanager->executeCommand(DATABASE_NAME, $command);
79+
echo "User dropped\n";
80+
} catch(Exception $e) {
81+
echo get_class($e), ": ", $e->getMessage(), "\n";
82+
}
83+
84+
85+
?>
86+
===DONE===
87+
<?php exit(0); ?>
88+
--EXPECTF--
89+
User Created
90+
string(9) "important"
91+
User dropped
92+
===DONE===

Diff for: tests/utils/basic.inc

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ $consts = array(
2121
"MONGODB_STANDALONE_AUTH_URI" => $orch->getURI("standalone-auth.json"),
2222
"MONGODB_STANDALONE_SSL_URI" => $orch->getURI("standalone-ssl.json"),
2323
"MONGODB_STANDALONE_PLAIN_URI" => $orch->getURI("standalone-plain.json"),
24+
"MONGODB_STANDALONE_X509_URI" => $orch->getURI("standalone-x509.json"),
2425
"DATABASE_NAME" => "phongo",
2526
"COLLECTION_NAME" => makeCollectionNameFromFilename($_SERVER["SCRIPT_FILENAME"]),
2627
"DEBUG_DIR" => sys_get_temp_dir() . "/PHONGO-TESTS/",

0 commit comments

Comments
 (0)