Skip to content

fix(auth): align mTLS discovery and enforce fail-fast transport configuration.#17470

Merged
vverman merged 7 commits into
googleapis:mainfrom
vverman:agentic-identity-fix-mtls-gaps-2
Jun 26, 2026
Merged

fix(auth): align mTLS discovery and enforce fail-fast transport configuration.#17470
vverman merged 7 commits into
googleapis:mainfrom
vverman:agentic-identity-fix-mtls-gaps-2

Conversation

@vverman

@vverman vverman commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

This PR resolves two mTLS issues:

(1) it synchronizes the transport and token discovery paths to check for workload certs and library support, preventing mismatched bound tokens, and

(2) it enforces fail-fast error behavior in all transports if mTLS configuration fails, avoiding silent fallback to insecure TLS. These align token and transport states securely.

@vverman vverman requested review from a team as code owners June 16, 2026 00:46
@vverman vverman requested a review from nbayati June 16, 2026 00:46

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces auto-enablement of mTLS when workload certificates are discovered (including at a well-known SPIFFE path) and changes the library's behavior to fail fast with a MutualTLSChannelError instead of silently falling back to standard TLS when mTLS is expected but certificates cannot be loaded. The review feedback suggests several improvements: aligning the environment variable checks in _agent_identity_utils.py to treat any value other than 'true' as disabled, correcting a misleading exception message in sessions.py when a custom request handler is used, and making the SPIFFE certificate path check more robust by using path.isfile and handling potential OSError exceptions.

Comment thread packages/google-auth/google/auth/_agent_identity_utils.py Outdated
Comment thread packages/google-auth/google/auth/_agent_identity_utils.py Outdated
Comment thread packages/google-auth/google/auth/aio/transport/sessions.py Outdated
Comment thread packages/google-auth/google/auth/transport/_mtls_helper.py Outdated
Comment thread packages/google-auth/google/auth/aio/transport/sessions.py Outdated
Comment thread packages/google-auth/google/auth/aio/transport/sessions.py
Comment thread packages/google-auth/google/auth/aio/transport/sessions.py
Comment thread packages/google-auth/google/auth/transport/_mtls_helper.py Outdated
Comment thread packages/google-auth/google/auth/transport/requests.py Outdated
Comment thread packages/google-auth/google/auth/transport/requests.py Outdated
Comment thread packages/google-auth/google/auth/transport/urllib3.py Outdated
Comment thread packages/google-auth/google/auth/_agent_identity_utils.py Outdated
Comment thread packages/google-auth/google/auth/_agent_identity_utils.py Outdated
Comment thread packages/google-auth/google/auth/transport/_mtls_helper.py Outdated
@vverman

vverman commented Jun 24, 2026

Copy link
Copy Markdown
Contributor Author

Summary of Changes

Aligned with PR 17387:

  1. State Reset: Reset _is_mtls = False on exceptions in sessions.py, requests.py, and urllib3.py to ensure correct transport state.
  2. Custom Transport Fallback: Fallback to standard TLS (no crash) in sessions.py when custom request handler is provided.

New fixes addressing PR 17470 comments:

  1. Centralized Env Check: Created _check_use_client_cert_env() helper in _mtls_helper.py and reused it in _agent_identity_utils.py.
  2. AIP Env Alignment: Treat any value other than "true" (case-insensitive) as "false" (opt-out) when GOOGLE_API_USE_CLIENT_CERTIFICATE is set.
  3. Reverted Fail-Fast: Reverted crashes on missing certificates in all transports, returning to standard TLS fallback. Reverted corresponding docstrings.
  4. Warning Logs: Added warning in sessions.py when falling back due to custom transports.
  5. Removed SPIFFE Fallback: Removed _WELL_KNOWN_SPIFFE_CERT_PATH logic and tests to fix GKE regression.
  6. Cleaned Up Capability Checks: Removed obsolete is_transport_mtls_capable() and associated mock tests.

@vverman vverman requested a review from nbayati June 24, 2026 01:37

@nbayati nbayati left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just left a comment on the docstring update, otherwise LGTM!

Comment thread packages/google-auth/tests/transport/aio/test_sessions_mtls.py
@parthea

parthea commented Jun 25, 2026

Copy link
Copy Markdown
Contributor

/gemini review

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors mTLS configuration and client certificate checks across multiple transports (aio, requests, urllib3) to respect explicit opt-outs and handle custom transports gracefully by setting _is_mtls to False and logging warnings. It also improves error handling for missing workload certificate paths and introduces corresponding unit tests. The reviewer feedback recommends correcting a docstring mismatch in a new test that incorrectly states an exception is raised, and improving test isolation by clearing all relevant environment variables in the auto-enablement test.

Comment thread packages/google-auth/tests/transport/aio/test_sessions_mtls.py
Comment thread packages/google-auth/tests/test_agent_identity_utils.py Outdated
Comment thread packages/google-auth/google/auth/aio/transport/sessions.py Outdated
Comment thread packages/google-auth/google/auth/transport/_mtls_helper.py Outdated
Comment thread packages/google-auth/google/auth/transport/_mtls_helper.py Outdated
Comment thread packages/google-auth/google/auth/transport/_mtls_helper.py
@vverman vverman requested a review from parthea June 26, 2026 02:48
Comment thread packages/google-auth/google/auth/transport/_mtls_helper.py Outdated
@vverman vverman merged commit f492d3d into googleapis:main Jun 26, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants