@@ -93,7 +93,14 @@ def _connect_google_default(self):
9393 def _connect_cloud (self ):
9494 self .credentials = gauth .compute_engine .Credentials ()
9595
96+ if not self .credentials .valid :
97+ raise ValueError ("Invalid gcloud credentials" )
98+
9699 def _connect_cache (self ):
100+
101+ if len (self .tokens ) == 0 :
102+ raise ValueError ("No cached tokens" )
103+
97104 project , access = self .project , self .access
98105 if (project , access ) in self .tokens :
99106 credentials = self .tokens [(project , access )]
@@ -220,12 +227,15 @@ def connect(self, method=None):
220227 self .connect (method = meth )
221228 logger .debug ("Connected with method %s" , meth )
222229 break
223- except google .auth .exceptions .GoogleAuthError as e :
230+ except ( google .auth .exceptions .GoogleAuthError , ValueError ) as e :
224231 # GoogleAuthError is the base class for all authentication
225232 # errors
226233 logger .debug (
227234 'Connection with method "%s" failed' % meth , exc_info = e
228235 )
236+ # Reset credentials if they were set but the authentication failed
237+ # (reverts to 'anon' behavior)
238+ self .credentials = None
229239 else :
230240 # Since the 'anon' connection method should always succeed,
231241 # getting here means something has gone terribly wrong.
0 commit comments