Skip to content

Commit

Permalink
Make sure that writing auth token from a file and reading it from a f…
Browse files Browse the repository at this point in the history
…ile is not

fatal if a token file is corrupted or similar.
  • Loading branch information
Kami committed Jul 30, 2016
1 parent 43eeed6 commit 2f02aec
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions libcloud/common/google.py
Expand Up @@ -677,7 +677,9 @@ def _get_token_from_file(self):
with open(filename, 'r') as f:
data = f.read()
token = json.loads(data)
except IOError:
except (IOError, ValueError):
# Note: File related errors (IOError) and errors related to json
# parsing of the data (ValueError) are not fatal.
pass
return token

Expand All @@ -686,11 +688,19 @@ def _write_token_to_file(self):
Write token to credential file.
Mocked in libcloud.test.common.google.GoogleTestCase.
"""
filename = os.path.realpath(os.path.expanduser(self.credential_file))
data = json.dumps(self.token)
with os.fdopen(os.open(filename, os.O_CREAT | os.O_WRONLY | os.O_TRUNC,
int('600', 8)), 'w') as f:
f.write(data)
try:
filename = os.path.expanduser(self.credential_file)
filename = os.path.realpath(filename)
data = json.dumps(self.token)
write_flags = os.O_CREAT | os.O_WRONLY | os.O_TRUNC
with os.fdopen(os.open(filename, write_flags,
int('600', 8)), 'w') as f:
f.write(data)
except:
# Note: Failed to write (cache) token in a file is not fatal. It
# simply means degraded performance since we will need to acquire a
# new token each time script runs.
pass


class GoogleBaseConnection(ConnectionUserAndKey, PollingConnection):
Expand Down

0 comments on commit 2f02aec

Please sign in to comment.