googleapis / googleapis/google-cloud-python

Support AIP-4114 GOOGLE_API_USE_MTLS_ENDPOINT overrides for IAM and STS credentials fetching

Aperta
#17,282 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
auth priority: p2 type: bug
Lingua principale
Python
Stelle
5.4k
Fork
1.8k
Merge medio
3g 4h
PR unite (30g)
122

Descrizione

### Summary of the issue
The `google-auth` library currently does not fully comply with [AIP-4114](https://google.aip.dev/auth/4114) regarding mTLS endpoint selection for its internal API calls, specifically for STS (Security Token Service) and IAM credentials fetching.

1. **IAM Credentials (`iam.py`)**: The domain hostname `_IAM_DOMAIN` is evaluated globally during module import using `_mtls_helper.check_use_client_cert()`. This static evaluation ignores the explicit overrides provided by the `GOOGLE_API_USE_MTLS_ENDPOINT` environment variable (`always`, `never`, `auto`). It also breaks late configuration bootstraps where certificates are provisioned dynamically after import.
2. **STS Endpoints (`external_account.py`, `identity_pool.py`, etc.)**: STS endpoint URLs (e.g., `https://sts.googleapis.com/v1/token`) are hardcoded or constructed without considering mTLS. In zero-trust environments where mTLS is strictly mandated (`GOOGLE_API_USE_MTLS_ENDPOINT=always`), these auth bootstrap requests must be routed to `sts.mtls.googleapis.com`. If they are not, the token exchange will be blocked by VPC Service Controls or BeyondCorp perimeters before any data-plane API calls can even be made.

### Proposed fix
Refactor `google/auth/iam.py` and the STS-related credential classes to resolve these endpoints dynamically per call or lazily upon instantiation, using the `should_use_mtls_endpoint()` helper from `google.auth.transport.mtls`.

To prevent excessive lookup overhead (such as synchronous disk I/O when verifying certificate paths on every token refresh), this runtime evaluation cloud be coupled with caching strategies (such as `@functools.lru_cache`) if the decision is decoupled from module load.

In addition, unit tests must be added to verify that the correct mTLS vs. standard endpoints (e.g., `sts.mtls.googleapis.com` and `iamcredentials.mtls.googleapis.com`) are triggered correctly based on the runtime `GOOGLE_API_USE_MTLS_ENDPOINT` environment variable and client certificate availability.

### Affected Files
* `google/auth/iam.py`
* `google/auth/impersonated_credentials.py`
* STS credential classes (e.g., `google/auth/external_account.py`, `google/auth/identity_pool.py`, `google/auth/aws.py`)

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.