Complete a KMS request. :param kms_context: A :class:`MongoCryptKmsContext`. :return: None
(self, kms_context: MongoCryptKmsContext)
| 165 | self._kms_ssl_contexts = opts._kms_ssl_contexts(_IS_SYNC) |
| 166 | |
| 167 | async def kms_request(self, kms_context: MongoCryptKmsContext) -> None: |
| 168 | """Complete a KMS request. |
| 169 | |
| 170 | :param kms_context: A :class:`MongoCryptKmsContext`. |
| 171 | |
| 172 | :return: None |
| 173 | """ |
| 174 | endpoint = kms_context.endpoint |
| 175 | message = kms_context.message |
| 176 | provider = kms_context.kms_provider |
| 177 | ctx = self._kms_ssl_contexts.get(provider) |
| 178 | if ctx is None: |
| 179 | # Enable strict certificate verification, OCSP, match hostname, and |
| 180 | # SNI using the system default CA certificates. |
| 181 | ctx = get_ssl_context( |
| 182 | None, # certfile |
| 183 | None, # passphrase |
| 184 | None, # ca_certs |
| 185 | None, # crlfile |
| 186 | False, # allow_invalid_certificates |
| 187 | False, # allow_invalid_hostnames |
| 188 | False, # disable_ocsp_endpoint_check |
| 189 | _IS_SYNC, |
| 190 | ) |
| 191 | # CSOT: set timeout for socket creation. |
| 192 | connect_timeout = max(_csot.clamp_remaining(_KMS_CONNECT_TIMEOUT), 0.001) |
| 193 | opts = PoolOptions( |
| 194 | connect_timeout=connect_timeout, |
| 195 | socket_timeout=connect_timeout, |
| 196 | ssl_context=ctx, |
| 197 | ) |
| 198 | address = parse_host(endpoint, _HTTPS_PORT) |
| 199 | sleep_u = kms_context.usleep |
| 200 | if sleep_u: |
| 201 | sleep_sec = float(sleep_u) / 1e6 |
| 202 | await asyncio.sleep(sleep_sec) |
| 203 | try: |
| 204 | conn = await _connect_kms(address, opts) |
| 205 | try: |
| 206 | await async_socket_sendall(conn, message) |
| 207 | while kms_context.bytes_needed > 0: |
| 208 | # CSOT: update timeout. |
| 209 | conn.settimeout(max(_csot.clamp_remaining(_KMS_CONNECT_TIMEOUT), 0)) |
| 210 | data: memoryview | bytes |
| 211 | if _IS_SYNC: |
| 212 | data = conn.recv(kms_context.bytes_needed) |
| 213 | else: |
| 214 | from pymongo.network_layer import ( # type: ignore[attr-defined] |
| 215 | async_receive_data_socket, |
| 216 | ) |
| 217 | |
| 218 | data = await async_receive_data_socket(conn, kms_context.bytes_needed) |
| 219 | if not data: |
| 220 | raise OSError("KMS connection closed") |
| 221 | kms_context.feed(data) |
| 222 | except MongoCryptError: |
| 223 | raise # Propagate MongoCryptError errors directly. |
| 224 | except Exception as exc: |
nothing calls this directly
no test coverage detected