Skip to content

Commit a1c0b25

Browse files
committed
Merge branch 'master' of github.com:mongodb/mongo-python-driver
2 parents dd8c035 + 4cd8191 commit a1c0b25

File tree

6 files changed

+50
-37
lines changed

6 files changed

+50
-37
lines changed

doc/changelog.rst

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,26 @@ Changes in Version 4.7
66

77
PyMongo 4.7 brings a number of improvements including:
88

9-
- Added the :class:`pymongo.hello.Hello.connection_id`,
10-
:attr:`pymongo.monitoring.CommandStartedEvent.server_connection_id`,
11-
:attr:`pymongo.monitoring.CommandSucceededEvent.server_connection_id`, and
12-
:attr:`pymongo.monitoring.CommandFailedEvent.server_connection_id` properties.
13-
- Fixed a bug where inflating a :class:`~bson.raw_bson.RawBSONDocument` containing a :class:`~bson.code.Code` would cause an error.
9+
- Added support for ``MONGODB-OIDC`` authentication. The MONGODB-OIDC mechanism authenticates
10+
using an OpenID Connect (OIDC) access token.
11+
The driver supports OIDC for workload identity, defined as an identity you assign to a software workload
12+
(such as an application, service, script, or container) to authenticate and access other services and resources.
13+
Please see :doc:`examples/authentication` for more information.
14+
- Added support for Python's `native logging library <https://docs.python.org/3/howto/logging.html>`_,
15+
enabling developers to customize the verbosity of log messages for their applications.
16+
Please see :doc:`examples/logging` for more information.
1417
- Significantly improved the performance of encoding BSON documents to JSON.
15-
- Support for named KMS providers for client side field level encryption.
18+
- Added support for named KMS providers for client side field level encryption.
1619
Previously supported KMS providers were only: aws, azure, gcp, kmip, and local.
1720
The KMS provider is now expanded to support name suffixes (e.g. local:myname).
1821
Named KMS providers enables more than one of each KMS provider type to be configured.
1922
See the docstring for :class:`~pymongo.encryption_options.AutoEncryptionOpts`.
2023
Note that named KMS providers requires pymongocrypt >=1.9 and libmongocrypt >=1.9.
24+
- Added the :class:`pymongo.hello.Hello.connection_id`,
25+
:attr:`pymongo.monitoring.CommandStartedEvent.server_connection_id`,
26+
:attr:`pymongo.monitoring.CommandSucceededEvent.server_connection_id`, and
27+
:attr:`pymongo.monitoring.CommandFailedEvent.server_connection_id` properties.
28+
- Fixed a bug where inflating a :class:`~bson.raw_bson.RawBSONDocument` containing a :class:`~bson.code.Code` would cause an error.
2129
- :meth:`~pymongo.encryption.ClientEncryption.encrypt` and
2230
:meth:`~pymongo.encryption.ClientEncryption.encrypt_expression` now allow ``key_id``
2331
to be passed in as a :class:`uuid.UUID`.

doc/examples/authentication.rst

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -455,32 +455,6 @@ Custom Callbacks
455455
For environments that are not directly supported by the driver, you can use :class:`~pymongo.auth_oidc.OIDCCallback`.
456456
Some examples are given below.
457457

458-
AWS EKS
459-
^^^^^^^
460-
461-
For an EKS Cluster with a configured `IAM OIDC provider`_, the token can be read from a path given by
462-
the ``AWS_WEB_IDENTITY_TOKEN_FILE`` environment variable.
463-
464-
.. code-block:: python
465-
466-
import os
467-
from pymongo.auth_oidc import OIDCCallback, OIDCCallbackContext, OIDCCallbackResult
468-
469-
470-
class MyCallback(OIDCCallback):
471-
def fetch(self, context: OIDCCallbackContext) -> OIDCCallbackResult:
472-
with open(os.environ["AWS_WEB_IDENTITY_TOKEN_FILE"]) as fid:
473-
token = fid.read()
474-
return OIDCCallbackResult(access_token=token)
475-
476-
477-
uri = os.environ["MONGODB_URI"]
478-
props = {"OIDC_CALLBACK": MyCallback()}
479-
c = MongoClient(uri, authMechanism="MONGODB-OIDC", authMechanismProperties=props)
480-
c.test.test.insert_one({})
481-
c.close()
482-
483-
484458
Other Azure Environments
485459
^^^^^^^^^^^^^^^^^^^^^^^^
486460

@@ -510,7 +484,7 @@ managed identity.
510484
511485
512486
props = {"OIDC_CALLBACK": MyCallback()}
513-
c = MongoClient(uri, authMechanismProperties=props)
487+
c = MongoClient(uri, authMechanism="MONGODB-OIDC", authMechanismProperties=props)
514488
c.test.test.insert_one({})
515489
c.close()
516490
@@ -543,6 +517,5 @@ service account token file location.
543517
.. _Azure Internal Metadata Service: https://learn.microsoft.com/en-us/azure/virtual-machines/instance-metadata-service
544518
.. _configured on your MongoDB deployment: https://www.mongodb.com/docs/manual/reference/parameters/#mongodb-parameter-param.oidcIdentityProviders
545519
.. _GCP Internal Metadata Service: https://cloud.google.com/compute/docs/metadata/querying-metadata
546-
.. _IAM OIDC provider: https://docs.aws.amazon.com/eks/latest/userguide/enable-iam-roles-for-service-accounts.html
547520
.. _azure-identity package: https://pypi.org/project/azure-identity/
548521
.. _configured service account: https://cloud.google.com/kubernetes-engine/docs/how-to/service-accounts

pymongo/_gcp_helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
def _get_gcp_response(resource: str, timeout: float = 5) -> dict[str, Any]:
2323
url = "http://metadata/computeMetadata/v1/instance/service-accounts/default/identity"
2424
url += f"?audience={resource}"
25-
headers = {"Metadata-Flavor": "Google", "Accept": "application/json"}
25+
headers = {"Metadata-Flavor": "Google"}
2626
request = Request(url, headers=headers) # noqa: S310
2727
try:
2828
with urlopen(request, timeout=timeout) as response: # noqa: S310

pymongo/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
from typing import Tuple, Union
1919

20-
version_tuple: Tuple[Union[int, str], ...] = (4, 7, 0, ".dev0")
20+
version_tuple: Tuple[Union[int, str], ...] = (4, 8, 0, '.dev0')
2121

2222

2323
def get_version_string() -> str:

pymongo/auth.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
Optional,
3434
cast,
3535
)
36-
from urllib.parse import quote
36+
from urllib.parse import quote, unquote
3737

3838
from bson.binary import Binary
3939
from pymongo.auth_aws import _authenticate_aws
@@ -173,6 +173,8 @@ def _build_credentials_tuple(
173173
human_callback = properties.get("OIDC_HUMAN_CALLBACK")
174174
environ = properties.get("ENVIRONMENT")
175175
token_resource = properties.get("TOKEN_RESOURCE", "")
176+
if unquote(token_resource) == token_resource:
177+
token_resource = quote(token_resource)
176178
default_allowed = [
177179
"*.mongodb.net",
178180
"*.mongodb-dev.net",

test/auth/legacy/connection-string.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,36 @@
539539
}
540540
}
541541
},
542+
{
543+
"description": "should accept a url-encoded TOKEN_RESOURCE (MONGODB-OIDC)",
544+
"uri": "mongodb://user@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:mongodb%253A//test-cluster",
545+
"valid": true,
546+
"credential": {
547+
"username": "user",
548+
"password": null,
549+
"source": "$external",
550+
"mechanism": "MONGODB-OIDC",
551+
"mechanism_properties": {
552+
"ENVIRONMENT": "azure",
553+
"TOKEN_RESOURCE": "mongodb%253A//test-cluster"
554+
}
555+
}
556+
},
557+
{
558+
"description": "should url-encode a TOKEN_RESOURCE (MONGODB-OIDC)",
559+
"uri": "mongodb://user@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:a$b",
560+
"valid": true,
561+
"credential": {
562+
"username": "user",
563+
"password": null,
564+
"source": "$external",
565+
"mechanism": "MONGODB-OIDC",
566+
"mechanism_properties": {
567+
"ENVIRONMENT": "azure",
568+
"TOKEN_RESOURCE": "a%24b"
569+
}
570+
}
571+
},
542572
{
543573
"description": "should accept a username and throw an error for a password with azure provider (MONGODB-OIDC)",
544574
"uri": "mongodb://user:pass@localhost/?authMechanism=MONGODB-OIDC&authMechanismProperties=ENVIRONMENT:azure,TOKEN_RESOURCE:foo",

0 commit comments

Comments
 (0)