From 1219ed7946c32ddcf8d06e002c004c9ab4440cd2 Mon Sep 17 00:00:00 2001 From: Hannes Magnusson Date: Mon, 21 Apr 2014 23:23:50 +0000 Subject: [PATCH] Document how to connect to MongoDB over SSL using stream context option for verifications and authentication Fixes https://jira.mongodb.org/browse/PHP-935 git-svn-id: https://svn.php.net/repository/phpdoc/en/trunk@333392 c90b9560-bf6c-de11-be94-00142212c4b1 --- reference/mongo/connecting.xml | 134 ++++++++++++++++++++++ reference/mongo/mongoclient/construct.xml | 39 +------ 2 files changed, 140 insertions(+), 33 deletions(-) diff --git a/reference/mongo/connecting.xml b/reference/mongo/connecting.xml index ef1c366c25..d1e3be7172 100644 --- a/reference/mongo/connecting.xml +++ b/reference/mongo/connecting.xml @@ -12,6 +12,140 @@ cases. +
+ Connecting over SSL + + The driver supports connecting to MongoDB over SSL + and can optionally use SSL Stream Context options to provide more details, + such as verifying certificates against specific certificate chain, or authenticate to + MongoDB using X509 certificates. + + + + Connect to MongoDB Instance with SSL Encryption + + true)); +?> +]]> + + + + + Connect to MongoDB Instance with SSL Encryption, verifying it is who we think it is + + array( + /* Optionally verify the server is who he says he is, and has been certified by CA we trust */ + "verify_peer" => true, + "allow_self_signed" => false, + "cafile" => "/vagrant/certs/ca.pem", + ), +)); + +$mc = new MongoClient( + "mongodb://server1", + array("ssl" => true), + array("context" => $ctx) +); +?> +]]> + + + + + + Connect to MongoDB Instance that Requires Client Certificates + + array( + "local_cert" => "/vagrant/certs/client.pem", + /* If the certificate we are providing was passphrase encoded, we need to set it here */ + "passphrase" => "My Passphrase for the local_cert", + + /* Optionally verify the server is who he says he is */ + "verify_peer" => true, + "cafile" => "/vagrant/certs/ca.pem", + ), +)); + +$mc = new MongoClient( + "mongodb://server1/?ssl=true", + array(), + array("context" => $ctx) +); +?> +]]> + + + + + Authenticating with X.509 certificates + + The username is the certificate subject from the X509, which can be extracted like this: + + + + + + array( + "local_cert" => "/vagrant/certs/ca-signed-client.pem", + ) +) ); + +$mc = new MongoClient( + 'mongodb://username@server1/?authSource=$external&authMechanism=MONGODB-X509&ssl=true', + array(), + array("context" => $ctx) +); +?> +]]> + + + Where username is the certificate subject. + + + + + &reftitle.changelog; + + + + + &Version; + &Description; + + + + + 1.5.0 + + Added support for X509 authentication. + + + + 1.4.0 + + Added support for connecting to SSL enabled MongoDB. + + + + + + + +
+
Authentication diff --git a/reference/mongo/mongoclient/construct.xml b/reference/mongo/mongoclient/construct.xml index 1904696231..c1676a72ef 100644 --- a/reference/mongo/mongoclient/construct.xml +++ b/reference/mongo/mongoclient/construct.xml @@ -410,18 +410,18 @@ mongodb://[username:password@]host1[:port1][,host2[:port2:],...]/db An array of options for the MongoDB driver. Options include setting - connection context options for SSL or logging callbacks. + connection context options for SSL + or logging callbacks. "context" - A way to pass in context options. Context options allow you to - configure SSL certificates and are described at SSL context options. There is an example further - down that shows you how to use this. + The Stream Context to attach to all new connections. This allows you + for example to configure SSL certificates and are described at + SSL context options. See the + Connecting over SSL tutorial. @@ -623,33 +623,6 @@ $uri .= '?readPreference=nearest'; $uri .= '&readPreferenceTags=dc:east'; $m = new MongoClient($uri, array('replicaSet' => 'rs')); ?> -]]> - - - See the read preferences - section of this manual for further information. - - - - - <function>MongoClient::__construct</function> connecting with SSL - certifications example - - array( - 'local_cert' => '/vagrant/certs/client.pem', - 'cafile' => '/vagrant/certs/ca.pem', - ) -) ); - -$m = new MongoClient( - "mongodb://mongod/?ssl=true", - array(), - array('context' => $ctx) -); -?> ]]>