Skip to content

Commit a2e8eda

Browse files
committed
Merge branch 'main' into PYTHON-5090
2 parents 3fa1552 + 8f6249e commit a2e8eda

File tree

5 files changed

+253
-14
lines changed

5 files changed

+253
-14
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
# Copyright 2022-present MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Test client side encryption with on demand credentials."""
16+
from __future__ import annotations
17+
18+
import os
19+
import sys
20+
import unittest
21+
22+
import pytest
23+
24+
sys.path[0:0] = [""]
25+
26+
from test.asynchronous import AsyncIntegrationTest, async_client_context
27+
28+
from bson.codec_options import CodecOptions
29+
from pymongo.asynchronous.encryption import (
30+
_HAVE_PYMONGOCRYPT,
31+
AsyncClientEncryption,
32+
EncryptionError,
33+
)
34+
35+
_IS_SYNC = False
36+
37+
pytestmark = pytest.mark.csfle
38+
39+
40+
class TestonDemandGCPCredentials(AsyncIntegrationTest):
41+
@unittest.skipUnless(_HAVE_PYMONGOCRYPT, "pymongocrypt is not installed")
42+
@async_client_context.require_version_min(4, 2, -1)
43+
async def asyncSetUp(self):
44+
await super().asyncSetUp()
45+
self.master_key = {
46+
"projectId": "devprod-drivers",
47+
"location": "global",
48+
"keyRing": "key-ring-csfle",
49+
"keyName": "key-name-csfle",
50+
}
51+
52+
@unittest.skipIf(not os.getenv("TEST_FLE_GCP_AUTO"), "Not testing FLE GCP auto")
53+
async def test_01_failure(self):
54+
if os.environ["SUCCESS"].lower() == "true":
55+
self.skipTest("Expecting success")
56+
self.client_encryption = AsyncClientEncryption(
57+
kms_providers={"gcp": {}},
58+
key_vault_namespace="keyvault.datakeys",
59+
key_vault_client=async_client_context.client,
60+
codec_options=CodecOptions(),
61+
)
62+
with self.assertRaises(EncryptionError):
63+
await self.client_encryption.create_data_key("gcp", self.master_key)
64+
65+
@unittest.skipIf(not os.getenv("TEST_FLE_GCP_AUTO"), "Not testing FLE GCP auto")
66+
async def test_02_success(self):
67+
if os.environ["SUCCESS"].lower() == "false":
68+
self.skipTest("Expecting failure")
69+
self.client_encryption = AsyncClientEncryption(
70+
kms_providers={"gcp": {}},
71+
key_vault_namespace="keyvault.datakeys",
72+
key_vault_client=async_client_context.client,
73+
codec_options=CodecOptions(),
74+
)
75+
await self.client_encryption.create_data_key("gcp", self.master_key)
76+
77+
78+
class TestonDemandAzureCredentials(AsyncIntegrationTest):
79+
@unittest.skipUnless(_HAVE_PYMONGOCRYPT, "pymongocrypt is not installed")
80+
@async_client_context.require_version_min(4, 2, -1)
81+
async def asyncSetUp(self):
82+
await super().asyncSetUp()
83+
self.master_key = {
84+
"keyVaultEndpoint": os.environ["KEY_VAULT_ENDPOINT"],
85+
"keyName": os.environ["KEY_NAME"],
86+
}
87+
88+
@unittest.skipIf(not os.getenv("TEST_FLE_AZURE_AUTO"), "Not testing FLE Azure auto")
89+
async def test_01_failure(self):
90+
if os.environ["SUCCESS"].lower() == "true":
91+
self.skipTest("Expecting success")
92+
self.client_encryption = AsyncClientEncryption(
93+
kms_providers={"azure": {}},
94+
key_vault_namespace="keyvault.datakeys",
95+
key_vault_client=async_client_context.client,
96+
codec_options=CodecOptions(),
97+
)
98+
with self.assertRaises(EncryptionError):
99+
await self.client_encryption.create_data_key("azure", self.master_key)
100+
101+
@unittest.skipIf(not os.getenv("TEST_FLE_AZURE_AUTO"), "Not testing FLE Azure auto")
102+
async def test_02_success(self):
103+
if os.environ["SUCCESS"].lower() == "false":
104+
self.skipTest("Expecting failure")
105+
self.client_encryption = AsyncClientEncryption(
106+
kms_providers={"azure": {}},
107+
key_vault_namespace="keyvault.datakeys",
108+
key_vault_client=async_client_context.client,
109+
codec_options=CodecOptions(),
110+
)
111+
await self.client_encryption.create_data_key("azure", self.master_key)
112+
113+
114+
if __name__ == "__main__":
115+
unittest.main(verbosity=2)
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
# Copyright 2015-present MongoDB, Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""Test the read_concern module."""
16+
from __future__ import annotations
17+
18+
import sys
19+
import unittest
20+
21+
sys.path[0:0] = [""]
22+
23+
from test.asynchronous import AsyncIntegrationTest, async_client_context
24+
from test.utils import OvertCommandListener
25+
26+
from bson.son import SON
27+
from pymongo.errors import OperationFailure
28+
from pymongo.read_concern import ReadConcern
29+
30+
_IS_SYNC = False
31+
32+
33+
class TestReadConcern(AsyncIntegrationTest):
34+
listener: OvertCommandListener
35+
36+
@async_client_context.require_connection
37+
async def asyncSetUp(self):
38+
await super().asyncSetUp()
39+
self.listener = OvertCommandListener()
40+
self.client = await self.async_rs_or_single_client(event_listeners=[self.listener])
41+
self.db = self.client.pymongo_test
42+
await async_client_context.client.pymongo_test.create_collection("coll")
43+
44+
async def asyncTearDown(self):
45+
await async_client_context.client.pymongo_test.drop_collection("coll")
46+
47+
def test_read_concern(self):
48+
rc = ReadConcern()
49+
self.assertIsNone(rc.level)
50+
self.assertTrue(rc.ok_for_legacy)
51+
52+
rc = ReadConcern("majority")
53+
self.assertEqual("majority", rc.level)
54+
self.assertFalse(rc.ok_for_legacy)
55+
56+
rc = ReadConcern("local")
57+
self.assertEqual("local", rc.level)
58+
self.assertTrue(rc.ok_for_legacy)
59+
60+
self.assertRaises(TypeError, ReadConcern, 42)
61+
62+
async def test_read_concern_uri(self):
63+
uri = f"mongodb://{await async_client_context.pair}/?readConcernLevel=majority"
64+
client = await self.async_rs_or_single_client(uri, connect=False)
65+
self.assertEqual(ReadConcern("majority"), client.read_concern)
66+
67+
async def test_invalid_read_concern(self):
68+
coll = self.db.get_collection("coll", read_concern=ReadConcern("unknown"))
69+
# We rely on the server to validate read concern.
70+
with self.assertRaises(OperationFailure):
71+
await coll.find_one()
72+
73+
async def test_find_command(self):
74+
# readConcern not sent in command if not specified.
75+
coll = self.db.coll
76+
await coll.find({"field": "value"}).to_list()
77+
self.assertNotIn("readConcern", self.listener.started_events[0].command)
78+
79+
self.listener.reset()
80+
81+
# Explicitly set readConcern to 'local'.
82+
coll = self.db.get_collection("coll", read_concern=ReadConcern("local"))
83+
await coll.find({"field": "value"}).to_list()
84+
self.assertEqualCommand(
85+
SON(
86+
[
87+
("find", "coll"),
88+
("filter", {"field": "value"}),
89+
("readConcern", {"level": "local"}),
90+
]
91+
),
92+
self.listener.started_events[0].command,
93+
)
94+
95+
async def test_command_cursor(self):
96+
# readConcern not sent in command if not specified.
97+
coll = self.db.coll
98+
await (await coll.aggregate([{"$match": {"field": "value"}}])).to_list()
99+
self.assertNotIn("readConcern", self.listener.started_events[0].command)
100+
101+
self.listener.reset()
102+
103+
# Explicitly set readConcern to 'local'.
104+
coll = self.db.get_collection("coll", read_concern=ReadConcern("local"))
105+
await (await coll.aggregate([{"$match": {"field": "value"}}])).to_list()
106+
self.assertEqual({"level": "local"}, self.listener.started_events[0].command["readConcern"])
107+
108+
async def test_aggregate_out(self):
109+
coll = self.db.get_collection("coll", read_concern=ReadConcern("local"))
110+
await (
111+
await coll.aggregate([{"$match": {"field": "value"}}, {"$out": "output_collection"}])
112+
).to_list()
113+
114+
# Aggregate with $out supports readConcern MongoDB 4.2 onwards.
115+
if async_client_context.version >= (4, 1):
116+
self.assertIn("readConcern", self.listener.started_events[0].command)
117+
else:
118+
self.assertNotIn("readConcern", self.listener.started_events[0].command)
119+
120+
121+
if __name__ == "__main__":
122+
unittest.main()

test/test_on_demand_csfle.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,20 @@
2626
from test import IntegrationTest, client_context
2727

2828
from bson.codec_options import CodecOptions
29-
from pymongo.synchronous.encryption import _HAVE_PYMONGOCRYPT, ClientEncryption, EncryptionError
29+
from pymongo.synchronous.encryption import (
30+
_HAVE_PYMONGOCRYPT,
31+
ClientEncryption,
32+
EncryptionError,
33+
)
34+
35+
_IS_SYNC = True
3036

3137
pytestmark = pytest.mark.csfle
3238

3339

3440
class TestonDemandGCPCredentials(IntegrationTest):
35-
@classmethod
3641
@unittest.skipUnless(_HAVE_PYMONGOCRYPT, "pymongocrypt is not installed")
3742
@client_context.require_version_min(4, 2, -1)
38-
def setUpClass(cls):
39-
super().setUpClass()
40-
4143
def setUp(self):
4244
super().setUp()
4345
self.master_key = {
@@ -74,12 +76,8 @@ def test_02_success(self):
7476

7577

7678
class TestonDemandAzureCredentials(IntegrationTest):
77-
@classmethod
7879
@unittest.skipUnless(_HAVE_PYMONGOCRYPT, "pymongocrypt is not installed")
7980
@client_context.require_version_min(4, 2, -1)
80-
def setUpClass(cls):
81-
super().setUpClass()
82-
8381
def setUp(self):
8482
super().setUp()
8583
self.master_key = {

test/test_read_concern.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
from pymongo.errors import OperationFailure
2828
from pymongo.read_concern import ReadConcern
2929

30+
_IS_SYNC = True
31+
3032

3133
class TestReadConcern(IntegrationTest):
3234
listener: OvertCommandListener
@@ -71,14 +73,14 @@ def test_invalid_read_concern(self):
7173
def test_find_command(self):
7274
# readConcern not sent in command if not specified.
7375
coll = self.db.coll
74-
tuple(coll.find({"field": "value"}))
76+
coll.find({"field": "value"}).to_list()
7577
self.assertNotIn("readConcern", self.listener.started_events[0].command)
7678

7779
self.listener.reset()
7880

7981
# Explicitly set readConcern to 'local'.
8082
coll = self.db.get_collection("coll", read_concern=ReadConcern("local"))
81-
tuple(coll.find({"field": "value"}))
83+
coll.find({"field": "value"}).to_list()
8284
self.assertEqualCommand(
8385
SON(
8486
[
@@ -93,19 +95,19 @@ def test_find_command(self):
9395
def test_command_cursor(self):
9496
# readConcern not sent in command if not specified.
9597
coll = self.db.coll
96-
tuple(coll.aggregate([{"$match": {"field": "value"}}]))
98+
(coll.aggregate([{"$match": {"field": "value"}}])).to_list()
9799
self.assertNotIn("readConcern", self.listener.started_events[0].command)
98100

99101
self.listener.reset()
100102

101103
# Explicitly set readConcern to 'local'.
102104
coll = self.db.get_collection("coll", read_concern=ReadConcern("local"))
103-
tuple(coll.aggregate([{"$match": {"field": "value"}}]))
105+
(coll.aggregate([{"$match": {"field": "value"}}])).to_list()
104106
self.assertEqual({"level": "local"}, self.listener.started_events[0].command["readConcern"])
105107

106108
def test_aggregate_out(self):
107109
coll = self.db.get_collection("coll", read_concern=ReadConcern("local"))
108-
tuple(coll.aggregate([{"$match": {"field": "value"}}, {"$out": "output_collection"}]))
110+
(coll.aggregate([{"$match": {"field": "value"}}, {"$out": "output_collection"}])).to_list()
109111

110112
# Aggregate with $out supports readConcern MongoDB 4.2 onwards.
111113
if client_context.version >= (4, 1):

tools/synchro.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,9 @@ def async_only_test(f: str) -> bool:
216216
"test_logger.py",
217217
"test_monitor.py",
218218
"test_monitoring.py",
219+
"test_on_demand_csfle.py",
219220
"test_raw_bson.py",
221+
"test_read_concern.py",
220222
"test_retryable_reads.py",
221223
"test_retryable_writes.py",
222224
"test_session.py",

0 commit comments

Comments
 (0)