From 42aa3ddeae19a029d1699ac5ac0b7fd33aa7d6a8 Mon Sep 17 00:00:00 2001 From: Noah Golmant Date: Thu, 23 Jun 2022 13:41:39 -0700 Subject: [PATCH] Fix to also try `cloud` authentication method when `method=None` (#481) * raise exception for empty cache * raise value error for invalid gcloud credentials --- gcsfs/credentials.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/gcsfs/credentials.py b/gcsfs/credentials.py index 074c7d5e..f368f90f 100644 --- a/gcsfs/credentials.py +++ b/gcsfs/credentials.py @@ -93,7 +93,14 @@ def _connect_google_default(self): def _connect_cloud(self): self.credentials = gauth.compute_engine.Credentials() + if not self.credentials.valid: + raise ValueError("Invalid gcloud credentials") + def _connect_cache(self): + + if len(self.tokens) == 0: + raise ValueError("No cached tokens") + project, access = self.project, self.access if (project, access) in self.tokens: credentials = self.tokens[(project, access)] @@ -220,12 +227,15 @@ def connect(self, method=None): self.connect(method=meth) logger.debug("Connected with method %s", meth) break - except google.auth.exceptions.GoogleAuthError as e: + except (google.auth.exceptions.GoogleAuthError, ValueError) as e: # GoogleAuthError is the base class for all authentication # errors logger.debug( 'Connection with method "%s" failed' % meth, exc_info=e ) + # Reset credentials if they were set but the authentication failed + # (reverts to 'anon' behavior) + self.credentials = None else: # Since the 'anon' connection method should always succeed, # getting here means something has gone terribly wrong.