Skip to content

Commit 8a2b956

Browse files
committed
example fixes
1 parent f75f041 commit 8a2b956

15 files changed

+89
-67
lines changed

source/includes/authentication/azure-envs-mongoclient.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
# define callback, properties, and MongoClient
66
audience = "<audience>"
7-
client_id = "<Azure client ID>"
7+
client_id = "<Azure ID>"
88
class MyCallback(OIDCCallback):
99
def fetch(self, context: OIDCCallbackContext) -> OIDCCallbackResult:
1010
credential = DefaultAzureCredential(managed_identity_client_id=client_id)
1111
token = credential.get_token(f"{audience}/.default").token
1212
return OIDCCallbackResult(access_token=token)
1313
properties = {"OIDC_CALLBACK": MyCallback()}
1414
client = MongoClient(
15-
"mongodb://<hostname>:<port>",
15+
"mongodb[+srv]://<hostname>:<port>",
1616
authMechanism="MONGODB-OIDC",
1717
authMechanismProperties=properties
1818
)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
from pymongo import MongoClient
22

33
# define URI and MongoClient
4-
uri = ("mongodb://<hostname>:<port>/?"
5-
"username=<Azure client ID or application ID>"
4+
uri = ("mongodb[+srv]://<hostname>:<port>/?"
5+
"username=<username>"
66
"&authMechanism=MONGODB-OIDC"
77
"&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:<percent-encoded audience>")
88
client = MongoClient(uri)

source/includes/authentication/azure-imds-mongoclient.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
# define properties and MongoClient
44
properties = {"ENVIRONMENT": "azure", "TOKEN_RESOURCE": "<audience>"}
55
client = MongoClient(
6-
"mongodb://<hostname>:<port>",
7-
username="<Azure client ID or application ID>",
6+
"mongodb[+srv]://<hostname>:<port>",
7+
username="<Azure ID>",
88
authMechanism="MONGODB-OIDC",
99
authMechanismProperties=properties
1010
)

source/includes/authentication/gcp-gke-mongoclient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ def fetch(self, context: OIDCCallbackContext) -> OIDCCallbackResult:
99
return OIDCCallbackResult(access_token=token)
1010
properties = {"OIDC_CALLBACK": MyCallback()}
1111
client = MongoClient(
12-
"mongodb://<hostname>:<port>",
12+
"mongodb[+srv]://<hostname>:<port>",
1313
authMechanism="MONGODB-OIDC",
1414
authMechanismProperties=properties
1515
)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pymongo import MongoClient
22

33
# define URI and MongoClient
4-
uri = ("mongodb://<hostname>:<port>/?"
4+
uri = ("mongodb[+srv]://<hostname>:<port>/?"
55
"&authMechanism=MONGODB-OIDC"
66
"&authMechanismProperties=ENVIRONMENT:gcp,TOKEN_RESOURCE:<percent-encoded audience>")
77
client = MongoClient(uri)

source/includes/authentication/gcp-imds-mongoclient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# define properties and MongoClient
44
properties = {"ENVIRONMENT": "gcp", "TOKEN_RESOURCE": "<audience>"}
55
client = MongoClient(
6-
"mongodb://<hostname>:<port>",
6+
"mongodb[+srv]://<hostname>:<port>",
77
authMechanism="MONGODB-OIDC",
88
authMechanismProperties=properties
99
)
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.. important:: Percent-Encoding
2+
3+
You must :wikipedia:`percent-encode <Percent-encoding>` a username and password before
4+
you include them in a MongoDB URI. The ``quote_plus()`` method, available in the
5+
`urllib.parse <https://docs.python.org/3/library/urllib.parse.html#urllib.parse.quote_plus>`__
6+
module, is one way to perform this task. For example, calling ``quote_plus("and / or")``
7+
returns the string ``and+%2F+or``.
8+
9+
Don't percent-encode the username or password when passing them as arguments to
10+
``MongoClient``.

source/security.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ Secure Your Data
2323
:maxdepth: 1
2424

2525
Authentication </security/authentication>
26-
Enterprise Authentication </security/enterprise-authentication>
2726
In-Use Encryption </security/in-use-encryption>
2827

2928
Overview

source/security/authentication.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
.. _pymongo-authentication-mechanisms:
2+
.. _pymongo-auth:
3+
.. _pymongo-enterprise-auth:
24

35
=========================
46
Authentication Mechanisms
@@ -37,7 +39,7 @@ the identity of a client to ensure security before connecting.
3739

3840
.. tip:: Connecting to MongoDB
3941

40-
To learn how to establish a connection to your MongoDB deployment, see the
42+
To learn how to establish a connection to your MongoDB deployment, see
4143
:ref:`pymongo-get-started`.
4244

4345
MongoDB Edition Compatibility

source/security/authentication/aws-iam.txt

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,14 @@ Code Placeholders
3636
The code examples on this page use the following placeholders:
3737

3838
- ``<hostname>``: The network address of your MongoDB Atlas deployment
39-
- ``<awsKeyId>``: Your AWS access key ID
40-
- ``<awsSecretKey>``: Your AWS secret access key
41-
- ``<awsSessionToken>``: Your AWS session token
39+
- ``<AWS IAM access key ID>``: Your AWS access key ID
40+
- ``<AWS IAM secret access key>``: Your AWS secret access key
41+
- ``<AWS session token>``: Your AWS session token
4242

4343
To use the code examples on this page, replace these placeholders with your own values.
4444

45+
.. include:: /includes/authentication/percent-encoding.rst
46+
4547
Using AWS IAM Authentication in Your Application
4648
------------------------------------------------
4749

@@ -56,7 +58,7 @@ To use AWS IAM authentication, you must install {+driver-short+} with the
5658
tries to retrieve AWS credentials from the following sources, in the order listed:
5759

5860
1. Named arguments passed to the ``MongoClient`` constructor or parameters in the
59-
connection URI
61+
connection string
6062
#. Environment variables
6163
#. Shared credentials file
6264
#. AWS config file
@@ -74,13 +76,13 @@ these sources and use them to authenticate your application.
7476

7577
First, {+driver-short+} checks whether you passed AWS credentials
7678
to the ``MongoClient`` constructor, either as a named argument or as part of the
77-
connection URI. To pass your credentials to ``MongoClient``,
79+
connection string. To pass your credentials to ``MongoClient``,
7880
set the following connection options:
7981

8082
- ``username``: The AWS IAM access key ID to authenticate. Percent-encode this value
81-
before including it in a connection URI.
83+
before including it in a connection string.
8284
- ``password``: The AWS IAM secret access key. Percent-encode this value before including
83-
it in a connection URI.
85+
it in a connection string.
8486
- ``authMechanism``: Set to ``"MONGODB-AWS"``.
8587

8688
You can set these options in two ways: by passing arguments to the
@@ -93,7 +95,7 @@ You can set these options in two ways: by passing arguments to the
9395

9496
.. code-block:: python
9597

96-
client = pymongo.MongoClient("mongodb://@<hostname>:<port>",
98+
client = pymongo.MongoClient("mongodb+srv://<hostname>",
9799
username="<AWS IAM access key ID>",
98100
password="<AWS IAM secret access key>",
99101
authMechanism="MONGODB-AWS")
@@ -103,9 +105,9 @@ You can set these options in two ways: by passing arguments to the
103105

104106
.. code-block:: python
105107

106-
uri = ("mongodb://<percent-encoded AWS IAM access key ID>:"
108+
uri = ("mongodb+srv://<percent-encoded AWS IAM access key ID>:"
107109
"<percent-encoded AWS IAM secret access key>"
108-
"@<hostname>:<port>/?"
110+
"@<hostname>/?"
109111
"&authMechanism=MONGODB-AWS")
110112
client = pymongo.MongoClient(uri)
111113

@@ -148,15 +150,15 @@ You can set this option in two ways: by passing an argument to the
148150

149151
.. code-block:: python
150152

151-
client = pymongo.MongoClient("mongodb://<hostname>:<port>",
153+
client = pymongo.MongoClient("mongodb+srv://<hostname>",
152154
authMechanism="MONGODB-AWS")
153155

154156
.. tab:: Connection String
155157
:tabid: connectionstring
156158

157159
.. code-block:: python
158160

159-
uri = "mongodb://<hostname>:<port>/?&authMechanism=MONGODB-AWS"
161+
uri = "mongodb+srv://<hostname>/?&authMechanism=MONGODB-AWS"
160162
client = pymongo.MongoClient(uri)
161163

162164
.. tip:: AWS Lambda
@@ -194,15 +196,15 @@ You can set this option in two ways: by passing an argument to the
194196

195197
.. code-block:: python
196198

197-
client = pymongo.MongoClient("mongodb://<hostname>:<port>",
199+
client = pymongo.MongoClient("mongodb+srv://<hostname>",
198200
authMechanism="MONGODB-AWS")
199201

200202
.. tab:: Connection String
201203
:tabid: connectionstring
202204

203205
.. code-block:: python
204206

205-
uri = "mongodb://<hostname>:<port>/?&authMechanism=MONGODB-AWS"
207+
uri = "mongodb+srv://<hostname>/?&authMechanism=MONGODB-AWS"
206208
client = pymongo.MongoClient(uri)
207209

208210
.. tip::
@@ -244,15 +246,15 @@ You can set this option in two ways: by passing an argument to the
244246

245247
.. code-block:: python
246248

247-
client = pymongo.MongoClient("mongodb://<hostname>:<port>",
249+
client = pymongo.MongoClient("mongodb+srv://<hostname>",
248250
authMechanism="MONGODB-AWS")
249251

250252
.. tab:: Connection String
251253
:tabid: connectionstring
252254

253255
.. code-block:: python
254256

255-
uri = "mongodb://<hostname>:<port>/?&authMechanism=MONGODB-AWS"
257+
uri = "mongodb+srv://<hostname>/?&authMechanism=MONGODB-AWS"
256258
client = pymongo.MongoClient(uri)
257259

258260
.. _pymongo-mongodb-aws-assume-role:
@@ -275,11 +277,11 @@ in the AWS documentation.
275277
After you create the config file, set the following connection options:
276278

277279
- ``username``: The AWS IAM access key ID to authenticate returned by the ``AssumeRole``
278-
request. Percent-encode this value before including it in a connection URI.
280+
request. Percent-encode this value before including it in a connection string.
279281
- ``password``: The AWS IAM secret access key returned by the ``AssumeRole`` request.
280-
Percent-encode this value before including it in a connection URI..
282+
Percent-encode this value before including it in a connection string.
281283
- ``authMechanismProperties``: Set to ``AWS_SESSION_TOKEN:`` and the
282-
AWS session token returned by the ``AssumeRole`` request.
284+
AWS session token returned by the ``AssumeRole`` request.
283285
- ``authMechanism``: Set to ``"MONGODB-AWS"``.
284286

285287
You can set these options in two ways: by passing arguments to the
@@ -294,7 +296,7 @@ You can set these options in two ways: by passing arguments to the
294296

295297
.. code-block:: python
296298

297-
client = pymongo.MongoClient("mongodb://@<hostname>:<port>",
299+
client = pymongo.MongoClient("mongodb+srv://@<hostname>",
298300
username="<AWS IAM access key ID>",
299301
password="<AWS IAM secret access key>",
300302
authMechanismProperties="AWS_SESSION_TOKEN:<AWS session token>",
@@ -305,9 +307,9 @@ You can set these options in two ways: by passing arguments to the
305307

306308
.. code-block:: python
307309

308-
uri = ("mongodb://<percent-encoded AWS IAM access key ID>:"
310+
uri = ("mongodb+srv://<percent-encoded AWS IAM access key ID>:"
309311
"<percent-encoded AWS IAM secret access key>"
310-
"@<hostname>:<port>/?"
312+
"@<hostname>/?"
311313
"authMechanismProperties=AWS_SESSION_TOKEN:<AWS session token>"
312314
"&authMechanism=MONGODB-AWS")
313315
client = pymongo.MongoClient(uri)
@@ -353,15 +355,15 @@ You can set this option in two ways: by passing an argument to the
353355

354356
.. code-block:: python
355357

356-
client = pymongo.MongoClient("mongodb://<hostname>:<port>",
358+
client = pymongo.MongoClient("mongodb+srv://<hostname>",
357359
authMechanism="MONGODB-AWS")
358360

359361
.. tab:: Connection String
360362
:tabid: connectionstring
361363

362364
.. code-block:: python
363365

364-
uri = "mongodb://<hostname>:<port>/?&authMechanism=MONGODB-AWS"
366+
uri = "mongodb+srv://<hostname>/?&authMechanism=MONGODB-AWS"
365367
client = pymongo.MongoClient(uri)
366368

367369
For more information about using an ``AssumeRoleWithWebIdentity`` request to
@@ -391,15 +393,15 @@ You can set this option in two ways: by passing an argument to the
391393

392394
.. code-block:: python
393395

394-
client = pymongo.MongoClient("mongodb://<hostname>:<port>",
396+
client = pymongo.MongoClient("mongodb+srv://<hostname>",
395397
authMechanism="MONGODB-AWS")
396398

397399
.. tab:: Connection String
398400
:tabid: connectionstring
399401

400402
.. code-block:: python
401403

402-
uri = "mongodb://<hostname>:<port>/?&authMechanism=MONGODB-AWS"
404+
uri = "mongodb+srv://<hostname>/?&authMechanism=MONGODB-AWS"
403405
client = pymongo.MongoClient(uri)
404406

405407
API Documentation

0 commit comments

Comments
 (0)