|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2026 Google LLC |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +# |
| 16 | + |
| 17 | +"""Helpers for client certificate handling and mTLS authentication.""" |
| 18 | + |
| 19 | +import os |
| 20 | +from typing import Any, Optional |
| 21 | + |
| 22 | +from google.auth.transport import mtls # type: ignore |
| 23 | + |
| 24 | + |
| 25 | +def use_client_cert_effective() -> bool: |
| 26 | + """Returns whether client certificate should be used for mTLS if the |
| 27 | + google-auth version supports should_use_client_cert automatic mTLS |
| 28 | + enablement. |
| 29 | +
|
| 30 | + Alternatively, read from the GOOGLE_API_USE_CLIENT_CERTIFICATE env var. |
| 31 | +
|
| 32 | + Returns: |
| 33 | + bool: whether client certificate should be used for mTLS |
| 34 | + Raises: |
| 35 | + ValueError: (If using a version of google-auth without |
| 36 | + should_use_client_cert and GOOGLE_API_USE_CLIENT_CERTIFICATE is |
| 37 | + set to an unexpected value.) |
| 38 | + """ |
| 39 | + # check if google-auth version supports should_use_client_cert for |
| 40 | + # automatic mTLS enablement |
| 41 | + if hasattr(mtls, "should_use_client_cert"): # pragma: NO COVER |
| 42 | + return mtls.should_use_client_cert() |
| 43 | + else: # pragma: NO COVER |
| 44 | + # if unsupported, fallback to reading from env var |
| 45 | + use_client_cert_str = os.getenv( |
| 46 | + "GOOGLE_API_USE_CLIENT_CERTIFICATE", "false" |
| 47 | + ).lower() |
| 48 | + if use_client_cert_str not in ("true", "false"): |
| 49 | + raise ValueError( |
| 50 | + "Environment variable `GOOGLE_API_USE_CLIENT_CERTIFICATE` " |
| 51 | + "must be either `true` or `false`" |
| 52 | + ) |
| 53 | + return use_client_cert_str == "true" |
| 54 | + |
| 55 | + |
| 56 | +def get_client_cert_source( |
| 57 | + provided_cert_source: Optional[Any], use_cert_flag: bool |
| 58 | +) -> Optional[Any]: |
| 59 | + """Return the client cert source to be used by the client. |
| 60 | +
|
| 61 | + Args: |
| 62 | + provided_cert_source (bytes): The client certificate source provided. |
| 63 | + use_cert_flag (bool): A flag indicating whether to use the |
| 64 | + client certificate. |
| 65 | +
|
| 66 | + Returns: |
| 67 | + bytes or None: The client cert source to be used by the client. |
| 68 | + """ |
| 69 | + client_cert_source = None |
| 70 | + if use_cert_flag: |
| 71 | + if provided_cert_source: |
| 72 | + client_cert_source = provided_cert_source |
| 73 | + elif ( |
| 74 | + hasattr(mtls, "has_default_client_cert_source") |
| 75 | + and mtls.has_default_client_cert_source() |
| 76 | + ): |
| 77 | + client_cert_source = mtls.default_client_cert_source() |
| 78 | + return client_cert_source |
| 79 | + |
| 80 | + |
| 81 | +# Backward compatibility aliases for private methods |
| 82 | +# Previously, gapic-generator-python generated clients used these methods |
| 83 | +_use_client_cert_effective = use_client_cert_effective |
| 84 | +_get_client_cert_source = get_client_cert_source |
0 commit comments