feat: Add debug logging that can help with diagnosing auth lib. path (#473)
Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com>
Co-authored-by: Tres Seaver <tseaver@palladion.com>
diff --git a/google/auth/_default.py b/google/auth/_default.py
index 694033f..de81c5b 100644
--- a/google/auth/_default.py
+++ b/google/auth/_default.py
@@ -155,10 +155,13 @@
"""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)
@@ -174,6 +177,10 @@
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(
os.environ[environment_vars.CREDENTIALS]
@@ -190,8 +197,10 @@
# 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:
@@ -199,6 +208,9 @@
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
@@ -215,6 +227,7 @@
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:
@@ -229,6 +242,9 @@
return compute_engine.Credentials(), project_id
else:
+ _LOGGER.warning(
+ "Authentication failed using Compute Engine authentication due to unavailable metadata server."
+ )
return None, None