Repository navigation
fix(auth): add startup polling for Cloud Run SPIFFE mTLS certificates - #19161
daniel-sanche wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces SPIFFE workload polling for mutual TLS (mTLS) configurations, allowing up to a 30-second wait for SPIFFE configurations, certificates, and keys to become ready, and adds comprehensive tests for these scenarios. The review feedback recommends catching exceptions.ClientCertError in default_client_encrypted_cert_source to ensure consistent exception wrapping, and resetting _spiffe_poll_timed_out to False upon successful credential loading to support future certificate rotations.
| try: | ||
| ( | ||
| _, | ||
| has_cert, | ||
| cert_bytes, | ||
| key_bytes, | ||
| passphrase_bytes, | ||
| ) = _mtls_helper.get_client_ssl_credentials(generate_encrypted_key=True) | ||
| if not has_cert: | ||
| raise exceptions.MutualTLSChannelError( | ||
| "Default client cert source did not provide a certificate" | ||
| ) |
There was a problem hiding this comment.
In default_client_encrypted_cert_source, the except block (which is just below this hunk) is not updated to catch exceptions.ClientCertError. This is inconsistent with default_client_cert_source (lines 96-101) and will cause ClientCertError (e.g., from a SPIFFE polling timeout) to propagate directly instead of being wrapped in MutualTLSChannelError as expected.
Please update the except block of default_client_encrypted_cert_source to catch exceptions.ClientCertError as well:
except (
exceptions.ClientCertError,
OSError,
RuntimeError,
ValueError,
) as caught_exc:
new_exc = exceptions.MutualTLSChannelError(caught_exc)
raise new_exc from caught_exc| try: | ||
| _check_spiffe_file_ready(cert_path) | ||
| _check_spiffe_file_ready(key_path) | ||
| return cert_path, key_path, spiffe_config_path |
There was a problem hiding this comment.
Once the SPIFFE credentials are successfully loaded and validated, we should reset _spiffe_poll_timed_out to False. This ensures that if a temporary timeout occurs during startup but eventually recovers, any future certificate rotations can still be polled normally instead of failing immediately.
Note: If you apply this change, you will also need to update the assertion in test_poll_after_timeout_succeeds_once_files_appear (line 2743 in test__mtls_helper.py) to expect _spiffe_poll_timed_out to be False instead of True.
| try: | |
| _check_spiffe_file_ready(cert_path) | |
| _check_spiffe_file_ready(key_path) | |
| return cert_path, key_path, spiffe_config_path | |
| try: | |
| _check_spiffe_file_ready(cert_path) | |
| _check_spiffe_file_ready(key_path) | |
| _spiffe_poll_timed_out = False | |
| return cert_path, key_path, spiffe_config_path |
Rolls back #19159 to restore #18924: