Skip to content

Commit cd834e9

Browse files
author
Mehdi BEN ABDALLAH
committed
expose the PEM encoded self signed server certificate
1 parent 6c2ff07 commit cd834e9

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

modules/cosmosdb/testcontainers/cosmosdb/__init__.py

+29-13
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class CosmosDBEmulatorContainer(DockerContainer):
4141
.. doctest::
4242
>>> from testcontainers.cosmosdb import CosmosDBEmulatorContainer
4343
>>> with CosmosDBEmulatorContainer() as cosmosdb:
44-
... db = cosmosdb.sync_client().create_database_if_not_exists("test")
44+
... db = cosmosdb.insecure_sync_client().create_database_if_not_exists("test")
4545
4646
.. doctest::
4747
>>> from testcontainers.cosmosdb import CosmosDBEmulatorContainer
@@ -84,39 +84,51 @@ def __init__(
8484
), "A MongoDB version is required to use the MongoDB Endpoint"
8585
self.mongodb_version = mongodb_version
8686

87-
def start(self) -> Self:
88-
self._configure()
89-
super().start()
90-
self._wait_until_ready()
91-
return self
92-
9387
@property
9488
def url(self) -> str:
9589
"""
96-
Returns the url to interact with the emulator
90+
The url to the CosmosDB server
9791
"""
9892
return f"https://{self.host}:{self.get_exposed_port(EMULATOR_PORT)}"
9993

10094
@property
10195
def host(self) -> str:
10296
return self.get_container_host_ip()
10397

98+
@property
99+
def certificate_pem(self) -> bytes:
100+
"""
101+
PEM-encoded certificate of the CosmosDB server
102+
"""
103+
return self._cert_pem_bytes
104+
104105
def ports(self, endpoint: Endpoints) -> Iterable[int]:
106+
"""
107+
Returns the set of exposed ports for a given endpoint.
108+
If bind_ports is True, the returned ports will be the NAT-ed ports reachable from the host.
109+
"""
105110
assert endpoint in self.endpoints, f"Endpoint {endpoint} is not exposed"
106111
return {self.get_exposed_port(p) for p in endpoint_ports[endpoint]}
107112

108-
def async_client(self) -> AsyncCosmosClient:
113+
def insecure_async_client(self) -> AsyncCosmosClient:
109114
"""
110-
Returns an asynchronous CosmosClient instance to interact with the CosmosDB server
115+
Returns an asynchronous CosmosClient instance
111116
"""
112117
return AsyncCosmosClient(url=self.url, credential=self.key, connection_verify=False)
113118

114-
def sync_client(self) -> SyncCosmosClient:
119+
def insecure_sync_client(self) -> SyncCosmosClient:
115120
"""
116-
Returns a synchronous CosmosClient instance to interact with the CosmosDB server
121+
Returns a synchronous CosmosClient instance
117122
"""
118123
return SyncCosmosClient(url=self.url, credential=self.key, connection_verify=False)
119124

125+
def start(self) -> Self:
126+
self._configure()
127+
super().start()
128+
self._wait_until_ready()
129+
self._cert_pem_bytes = self._download_cert()
130+
return self
131+
120132
def _configure(self) -> None:
121133
self.with_bind_ports(EMULATOR_PORT, EMULATOR_PORT)
122134

@@ -162,6 +174,10 @@ def _wait_for_logs(self, *args, **kwargs) -> Self:
162174

163175
@wait_container_is_ready(ServiceRequestError)
164176
def _wait_for_query_success(self, query: Callable[[SyncCosmosClient], None]) -> Self:
165-
with self.sync_client() as c:
177+
with self.insecure_sync_client() as c:
166178
query(c)
167179
return self
180+
181+
def _download_cert(self) -> bytes:
182+
with urlopen(f"{self.url}/_explorer/emulator.pem", context=ssl._create_unverified_context()) as response:
183+
return response.read()

modules/cosmosdb/tests/test_cosmosdb.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
def test_docker_run():
66
with CosmosDBEmulatorContainer(partition_count=1) as cosmosdb:
7-
list(cosmosdb.sync_client().list_databases())
7+
list(cosmosdb.insecure_sync_client().list_databases())
8+
assert cosmosdb.certificate_pem is not None
89

910

1011
def test_enabling_mondogb_endpoint_requires_a_version():

0 commit comments

Comments
 (0)