Repository navigation
fix(auth): add startup polling for Cloud Run SPIFFE mTLS certificates #19161
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |
| import enum | ||
| import logging | ||
| import ssl | ||
| from os import getenv | ||
| from os import getenv, path | ||
| from typing import Optional | ||
|
|
||
| from google.auth import environment_vars, exceptions | ||
|
|
@@ -42,12 +42,18 @@ def has_default_client_cert_source(include_context_aware=True): | |
| Returns: | ||
| bool: indicating if the default client cert source exists. | ||
| """ | ||
| spiffe_config_path = _mtls_helper._get_spiffe_polling_config_path() | ||
| # Before the SPIFFE config is mounted, assume it will provide a cert and let | ||
| # the loader poll for it. Once it exists, inspect it like any other config. | ||
| if spiffe_config_path is not None and not path.exists(spiffe_config_path): | ||
| return True | ||
|
|
||
| try: | ||
| ( | ||
| cert_path, | ||
| key_path, | ||
| config_file_path, | ||
| ) = _mtls_helper._get_workload_cert_and_key_paths( | ||
| ) = _mtls_helper._get_non_spiffe_workload_cert_and_key_paths( | ||
| None, include_context_aware=include_context_aware | ||
| ) | ||
| except (exceptions.ClientCertError, OSError): | ||
|
|
@@ -86,10 +92,19 @@ def default_client_cert_source(): | |
|
|
||
| def callback(): | ||
| try: | ||
| _, cert_bytes, key_bytes = _mtls_helper.get_client_cert_and_key() | ||
| except (OSError, RuntimeError, ValueError) as caught_exc: | ||
| has_cert, cert_bytes, key_bytes = _mtls_helper.get_client_cert_and_key() | ||
| except ( | ||
| exceptions.ClientCertError, | ||
| OSError, | ||
| RuntimeError, | ||
| ValueError, | ||
| ) as caught_exc: | ||
| new_exc = exceptions.MutualTLSChannelError(caught_exc) | ||
| raise new_exc from caught_exc | ||
| if not has_cert: | ||
| raise exceptions.MutualTLSChannelError( | ||
| "Default client cert source did not provide a certificate" | ||
| ) | ||
|
|
||
| return cert_bytes, key_bytes | ||
|
|
||
|
|
@@ -123,11 +138,15 @@ def default_client_encrypted_cert_source(cert_path, key_path): | |
| def callback(): | ||
| 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" | ||
| ) | ||
|
Comment on lines
139
to
+149
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In Please update the except (
exceptions.ClientCertError,
OSError,
RuntimeError,
ValueError,
) as caught_exc:
new_exc = exceptions.MutualTLSChannelError(caught_exc)
raise new_exc from caught_exc |
||
| with open(cert_path, "wb") as cert_file: | ||
| cert_file.write(cert_bytes) | ||
| with open(key_path, "wb") as key_file: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Once the SPIFFE credentials are successfully loaded and validated, we should reset
_spiffe_poll_timed_outtoFalse. 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 intest__mtls_helper.py) to expect_spiffe_poll_timed_outto beFalseinstead ofTrue.