From 1a9b58f842cac7a5774f28a9cf5a4732c492e30b Mon Sep 17 00:00:00 2001 From: Vaughan Hilts Date: Thu, 26 Mar 2020 13:01:36 -0400 Subject: [PATCH 1/2] feat: Add debug logging that can help with diagnosing break downs in the auth process for support --- google/auth/_default.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/google/auth/_default.py b/google/auth/_default.py index 32e81ba5f..de4d639ac 100644 --- a/google/auth/_default.py +++ b/google/auth/_default.py @@ -142,10 +142,13 @@ def _get_gcloud_sdk_credentials(): """Gets the credentials and project ID from the Cloud SDK.""" from google.auth import _cloud_sdk + _LOGGER.debug("Checking Cloud SDK credentials as part of auth process...") + # Check if application default credentials exist. credentials_filename = _cloud_sdk.get_application_default_credentials_path() if not os.path.isfile(credentials_filename): + _LOGGER.debug("Cloud SDK credentials not found on disk; not using them") return None, None credentials, project_id = _load_credentials_from_file(credentials_filename) @@ -160,6 +163,8 @@ def _get_explicit_environ_credentials(): """Gets credentials from the GOOGLE_APPLICATION_CREDENTIALS environment variable.""" explicit_file = os.environ.get(environment_vars.CREDENTIALS) + + _LOGGER.debug("Checking %s for explicit credentials as part of auth process...", explicit_file) if explicit_file is not None: credentials, project_id = _load_credentials_from_file( @@ -177,8 +182,10 @@ def _get_gae_credentials(): # While this library is normally bundled with app_engine, there are # some cases where it's not available, so we tolerate ImportError. try: + _LOGGER.debug("Checking for App Engine runtime as part of auth process...") import google.auth.app_engine as app_engine except ImportError: + _LOGGER.warning("Import of App Engine auth library failed.") return None, None try: @@ -186,6 +193,7 @@ def _get_gae_credentials(): project_id = app_engine.get_project_id() return credentials, project_id except EnvironmentError: + _LOGGER.debug("No App Engine library was found so cannot authentication via App Engine Identity Credentials.") return None, None @@ -202,6 +210,7 @@ def _get_gce_credentials(request=None): from google.auth import compute_engine from google.auth.compute_engine import _metadata except ImportError: + _LOGGER.warning("Import of Compute Engine auth library failed.") return None, None if request is None: @@ -216,6 +225,7 @@ def _get_gce_credentials(request=None): return compute_engine.Credentials(), project_id else: + _LOGGER.warning("Authentication failed using Compute Engine authentication due to unavailable metadata server.") return None, None From 5bf12d9ba0f337b7ce2413c7d3e6ab02650f0023 Mon Sep 17 00:00:00 2001 From: Tres Seaver Date: Tue, 21 Jul 2020 14:32:35 -0400 Subject: [PATCH 2/2] fix: blacken to appease lint --- google/auth/_default.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/google/auth/_default.py b/google/auth/_default.py index a37b3a1b0..2130a84b3 100644 --- a/google/auth/_default.py +++ b/google/auth/_default.py @@ -163,8 +163,10 @@ def _get_explicit_environ_credentials(): """Gets credentials from the GOOGLE_APPLICATION_CREDENTIALS environment variable.""" explicit_file = os.environ.get(environment_vars.CREDENTIALS) - - _LOGGER.debug("Checking %s for explicit credentials as part of auth process...", explicit_file) + + _LOGGER.debug( + "Checking %s for explicit credentials as part of auth process...", explicit_file + ) if explicit_file is not None: credentials, project_id = _load_credentials_from_file( @@ -193,7 +195,9 @@ def _get_gae_credentials(): project_id = app_engine.get_project_id() return credentials, project_id except EnvironmentError: - _LOGGER.debug("No App Engine library was found so cannot authentication via App Engine Identity Credentials.") + _LOGGER.debug( + "No App Engine library was found so cannot authentication via App Engine Identity Credentials." + ) return None, None @@ -225,7 +229,9 @@ def _get_gce_credentials(request=None): return compute_engine.Credentials(), project_id else: - _LOGGER.warning("Authentication failed using Compute Engine authentication due to unavailable metadata server.") + _LOGGER.warning( + "Authentication failed using Compute Engine authentication due to unavailable metadata server." + ) return None, None