Skip to content

Commit

Permalink
feat: Add debug logging that can help with diagnosing auth lib. path (#…
Browse files Browse the repository at this point in the history
…473)

Co-authored-by: Bu Sun Kim <8822365+busunkim96@users.noreply.github.com>
Co-authored-by: Tres Seaver <tseaver@palladion.com>
  • Loading branch information
3 people committed Jul 21, 2020
1 parent 7ada6b0 commit ecd88d4
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions google/auth/_default.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,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)
Expand All @@ -174,6 +177,10 @@ def _get_explicit_environ_credentials():
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]
Expand All @@ -190,15 +197,20 @@ 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:
credentials = app_engine.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


Expand All @@ -215,6 +227,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:
Expand All @@ -229,6 +242,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."
)
return None, None


Expand Down

0 comments on commit ecd88d4

Please sign in to comment.