Skip to content

Commit 54aa80a

Browse files
committed
JM feedback
1 parent 67a3b4f commit 54aa80a

File tree

3 files changed

+63
-37
lines changed

3 files changed

+63
-37
lines changed

source/connect.txt

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ Make sure to replace all placeholders in the code examples, such as
5656
:linenos:
5757
:emphasize-lines: 5-7
5858

59+
.. important:: Percent-Encoding
60+
61+
You must :wikipedia:`percent-encode <Percent-encoding>` a username and password before
62+
you include them in a MongoDB URI. You can use the ``rawurlencode()`` method to encode
63+
these values according to the URI syntax specified in `RFC 3986 <http://www.faqs.org/rfcs/rfc3986.html>`__.
64+
Don't percent-encode the username or password when passing them in an options array
65+
parameter to the ``MongoDB\Client`` constructor.
66+
5967
Connection
6068
----------
6169

@@ -84,6 +92,11 @@ The following code shows how to connect to a local MongoDB deployment:
8492
:start-after: start-local
8593
:end-before: end-local
8694

95+
.. note::
96+
97+
If you don't specify the ``$uri`` parameter, the connection URI defaults to
98+
``'mongodb://127.0.0.1:27017'``.
99+
87100
To learn more about connecting to a local deployment, see :ref:`php-connection-local`
88101
in the Connection Targets guide.
89102

@@ -261,13 +274,20 @@ client certificate:
261274
:start-after: start-key-file-uri
262275
:end-before: end-key-file-uri
263276

277+
.. important::
278+
279+
When replacing the ``<password>`` placeholder, ensure that you :wikipedia:`percent-encode
280+
<Percent-encoding>` the value.
281+
264282
To learn more about providing a key file password, see :ref:`php-key-file-password` in
265283
the TLS Configuration guide.
266284

267285
Allow Insecure TLS
268286
~~~~~~~~~~~~~~~~~~
269287

270-
The following code shows how to enable insecure TLS:
288+
The following code shows how to relax TLS constraints, which has the same
289+
effect as disabling both :ref:`certificate validation <php-connect-disable-cert>`
290+
and :ref:`hostname verification <php-connect-disable-hostname>`:
271291

272292
.. tabs::
273293

@@ -292,6 +312,15 @@ The following code shows how to enable insecure TLS:
292312
To learn more about allowing insecure TLS, see :ref:`php-insecure-tls` in
293313
the TLS Configuration guide.
294314

315+
.. warning::
316+
317+
Setting the ``tlsInsecure`` option to ``true`` might expose your application
318+
to security risks. Enabling this option makes your application insecure and
319+
potentially vulnerable to expired certificates and to foreign processes posing
320+
as valid client instances.
321+
322+
.. _php-connect-disable-cert:
323+
295324
Disable Certificate Validation
296325
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
297326

@@ -320,6 +349,8 @@ The following code shows how to disable certificate validation:
320349
To learn more about disabling certificate validation, see :ref:`php-insecure-tls` in
321350
the TLS Configuration guide.
322351

352+
.. _php-connect-disable-hostname:
353+
323354
Disable Hostname Verification
324355
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
325356

source/includes/usage-examples/connect-code-examples.php

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,30 +2,28 @@
22

33
require 'vendor/autoload.php';
44

5-
use MongoDB\Client;
6-
75
// Connects to a local MongoDB deployment
86
// start-local
97
$uri = 'mongodb://localhost:27017/';
10-
$client = new Client($uri);
8+
$client = new MongoDB\Client($uri);
119
// end-local
1210

1311
// Connects to a MongoDB Atlas deployment
1412
// start-atlas
1513
$uri = '<Atlas connection string>';
16-
$client = new Client($uri);
14+
$client = new MongoDB\Client($uri);
1715
// end-atlas
1816

1917
// Connects to a replica set
2018
// start-replica-set
2119
$uri = 'mongodb://<replica set member>:<port>/?replicaSet=<replica set name>';
22-
$client = new Client($uri);
20+
$client = new MongoDB\Client($uri);
2321
// end-replica-set
2422

2523
// Connects to a MongoDB deployment and enables TLS using client
2624
// options
2725
// start-enable-tls-client
28-
$client = new Client(
26+
$client = new MongoDB\Client(
2927
'mongodb://<hostname>:<port>/',
3028
['tls' => true],
3129
);
@@ -35,13 +33,13 @@
3533
// parameters
3634
// start-enable-tls-uri
3735
$uri = 'mongodb://<hostname>:<port>/?tls=true';
38-
$client = new Client($uri);
36+
$client = new MongoDB\Client($uri);
3937
// end-enable-tls-uri
4038

4139
// Connects to a MongoDB deployment, enables TLS, and specifies the path to
4240
// a CA file using client options
4341
// start-ca-file-client
44-
$client = new Client(
42+
$client = new MongoDB\Client(
4543
'mongodb://<hostname>:<port>/',
4644
['tls' => true, 'tlsCAFile' => '/path/to/ca.pem'],
4745
);
@@ -51,13 +49,13 @@
5149
// a CA file using connection URI parameters
5250
// start-ca-file-uri
5351
$uri = 'mongodb://<hostname>:<port>/?tls=true&tlsCAFile=/path/to/ca.pem';
54-
$client = new Client($uri);
52+
$client = new MongoDB\Client($uri);
5553
// end-ca-file-uri
5654

5755
// Connects to a MongoDB deployment, enables TLS, and prevents OCSP endpoint checks
5856
// using client options
5957
// start-disable-ocsp-client
60-
$client = new Client(
58+
$client = new MongoDB\Client(
6159
'mongodb://<hostname>:<port>/',
6260
['tls' => true, 'tlsDisableOCSPEndpointCheck' => true],
6361
);
@@ -67,13 +65,13 @@
6765
// using connection URI parameters
6866
// start-disable-ocsp-uri
6967
$uri = 'mongodb://<hostname>:<port>/?tls=true&tlsDisableOCSPEndpointCheck=true';
70-
$client = new Client($uri);
68+
$client = new MongoDB\Client($uri);
7169
// end-disable-ocsp-uri
7270

7371
// Connects to a TLS-enabled deployment and instructs the driver to check the
7472
// server certificate against a CRL
7573
// start-crl
76-
$client = new Client(
74+
$client = new MongoDB\Client(
7775
'mongodb://<hostname>:<port>/',
7876
['tls' => true],
7977
['crl_file' => '/path/to/file.pem'],
@@ -83,7 +81,7 @@
8381
// Presents a client certificate to prove identity
8482
// using client options
8583
// start-client-cert-client
86-
$client = new Client(
84+
$client = new MongoDB\Client(
8785
'mongodb://<hostname>:<port>/',
8886
['tls' => true, 'tlsCertificateKeyFile' => '/path/to/client.pem'],
8987
);
@@ -93,12 +91,12 @@
9391
// using connection URI parameters
9492
// start-client-cert-uri
9593
$uri = 'mongodb://<hostname>:<port>/?tls=true&tlsCertificateKeyFile=/path/to/client.pem';
96-
$client = new Client($uri);
94+
$client = new MongoDB\Client($uri);
9795
// end-client-cert-uri
9896

9997
// Specifies the password for a client certificate using client options
10098
// start-key-file-client
101-
$client = new Client(
99+
$client = new MongoDB\Client(
102100
'mongodb://<hostname>:<port>/',
103101
[
104102
'tls' => true,
@@ -111,13 +109,13 @@
111109
// Specifies the password for a client certificate using connection URI parameters
112110
// start-key-file-uri
113111
$uri = 'mongodb://<hostname>:<port>/?tls=true&tlsCertificateKeyFile=/path/to/client.pem&tlsCertificateKeyFilePassword=<password>';
114-
$client = new Client($uri);
112+
$client = new MongoDB\Client($uri);
115113
// end-key-file-uri
116114

117115
// Connects to a TLS-enabled deployment and disables server certificate verification
118116
// using client options
119117
// start-insecure-tls-client
120-
$client = new Client(
118+
$client = new MongoDB\Client(
121119
'mongodb://<hostname>:<port>/',
122120
['tls' => true, 'tlsInsecure' => true],
123121
);
@@ -127,12 +125,12 @@
127125
// using connection URI parameters
128126
// start-insecure-tls-uri
129127
$uri = 'mongodb://<hostname>:<port>/?tls=true&tlsInsecure=true';
130-
$client = new Client($uri);
128+
$client = new MongoDB\Client($uri);
131129
// end-insecure-tls-uri
132130

133131
// Disables certificate validation using client options
134132
// start-disable-cert-client
135-
$client = new Client(
133+
$client = new MongoDB\Client(
136134
'mongodb://<hostname>:<port>/',
137135
['tls' => true, 'tlsAllowInvalidCertificates' => true],
138136
);
@@ -141,13 +139,13 @@
141139
// Disables certificate validation using connection URI parameters
142140
// start-disable-cert-uri
143141
$uri = 'mongodb://<hostname>:<port>/?tls=true&tlsAllowInvalidCertificates=true';
144-
$client = new Client($uri);
142+
$client = new MongoDB\Client($uri);
145143
// end-disable-cert-uri
146144

147145
// Connects to a TLS-enabled deployment and disables hostname verification
148146
// using client options
149147
// start-disable-hostname-client
150-
$client = new Client(
148+
$client = new MongoDB\Client(
151149
'mongodb://<hostname>:<port>/',
152150
['tls' => true, 'tlsAllowInvalidHostnames' => true],
153151
);
@@ -157,18 +155,17 @@
157155
// using connection URI parameters
158156
// start-disable-hostname-uri
159157
$uri = 'mongodb://<hostname>:<port>/?tls=true&tlsAllowInvalidHostnames=true';
160-
$client = new Client($uri);
158+
$client = new MongoDB\Client($uri);
161159
// end-disable-hostname-uri
162160

163161
// Connects to a MongoDB deployment and enables the stable API
164162
// start-stable-api
165-
$uri = '<connection string>';
166-
$clientOptions = [
167-
'serverApi' => [
168-
'version' => '1',
169-
],
170-
];
171-
$client = new Client($uri, $clientOptions);
163+
$driverOptions = ['serverApi' => new MongoDB\Driver\ServerApi(ServerApi::V1)];
164+
$client = new MongoDB\Client(
165+
'mongodb://<hostname>:<port>/',
166+
[],
167+
$driverOptions,
168+
);
172169
// end-stable-api
173170

174171
?>

source/includes/usage-examples/connect-sample-app.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@
66

77
// End example code here
88

9-
$admin = $client->admin;
10-
$result = $admin->command(['ping' => 1]);
11-
12-
if ($result) {
9+
try {
10+
$client->test->command(['ping' => 1]);
1311
echo 'Successfully pinged the MongoDB server.', PHP_EOL;
14-
} else {
15-
echo 'Ping to MongoDB server failed.', PHP_EOL;
16-
}
12+
} catch (MongoDB\Driver\Exception\RuntimeException $e) {
13+
printf("Failed to ping the MongoDB server: %s\n", $e->getMessage());
14+
}

0 commit comments

Comments
 (0)