Skip to content

Commit

Permalink
Fix check connection (#457)
Browse files Browse the repository at this point in the history
* deprecate check_credentials argument from GoogleCredentials constructor

* remove check_connection argument from guess_credentials function

* add comment explaining why runtime error should never occur
  • Loading branch information
janjagusch committed Mar 22, 2022
1 parent d956b35 commit 9119d3d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
16 changes: 8 additions & 8 deletions gcsfs/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,6 @@ class GCSFileSystem(AsyncFileSystem):
Cache expiration time in seconds for object metadata cache.
Set cache_timeout <= 0 for no caching, None for no cache expiration.
secure_serialize: bool (deprecated)
check_connection: bool
When token=None, gcsfs will attempt various methods of establishing
credentials, falling back to anon. It is possible for a method to
find credentials in the system that turn out not to be valid. Setting
this parameter to True will ensure that an actual operation is
attempted before deciding that credentials are valid.
requester_pays : bool, or str default False
Whether to use requester-pays requests. This will include your
project ID `project` in requests as the `userPorject`, and you'll be
Expand Down Expand Up @@ -239,7 +233,7 @@ def __init__(
consistency="none",
cache_timeout=None,
secure_serialize=True,
check_connection=False,
check_connection=None,
requests_timeout=None,
requester_pays=False,
asynchronous=False,
Expand Down Expand Up @@ -271,7 +265,13 @@ def __init__(
self._endpoint = endpoint_url
self.session_kwargs = session_kwargs or {}

self.credentials = GoogleCredentials(project, access, token, check_connection)
if check_connection:
warnings.warn(
"The `check_connection` argument is deprecated and will be removed in a future release.",
DeprecationWarning,
)

self.credentials = GoogleCredentials(project, access, token)

if not self.asynchronous:
self._session = sync(
Expand Down
15 changes: 11 additions & 4 deletions gcsfs/credentials.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,24 @@


class GoogleCredentials:
def __init__(self, project, access, token, check_credentials=False):
def __init__(self, project, access, token, check_credentials=None):
self.scope = "https://www.googleapis.com/auth/devstorage." + access
self.project = project
self.access = access
self.heads = {}

self.check_credentials = check_credentials
self.credentials = None
self.method = None
self.lock = threading.Lock()
self.token = token
self.connect(method=token)

if check_credentials:
warnings.warn(
"The `check_credentials` argument is deprecated and will be removed in a future release.",
DeprecationWarning,
)

@classmethod
def load_tokens(cls):
"""Get "browser" tokens from disc"""
Expand Down Expand Up @@ -213,8 +218,6 @@ def connect(self, method=None):
for meth in ["google_default", "cache", "cloud", "anon"]:
try:
self.connect(method=meth)
if self.check_credentials and meth != "anon":
self.ls("anaconda-public-data")
logger.debug("Connected with method %s", meth)
break
except google.auth.exceptions.GoogleAuthError as e:
Expand All @@ -223,6 +226,10 @@ def connect(self, method=None):
logger.debug(
'Connection with method "%s" failed' % meth, exc_info=e
)
else:
# Since the 'anon' connection method should always succeed,
# getting here means something has gone terribly wrong.
raise RuntimeError("All connection methods have failed!")
else:
self.__getattribute__("_connect_" + method)()
self.method = method

0 comments on commit 9119d3d

Please sign in to comment.