|
| 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) |
0 commit comments