Allowing custom directory for well-known file.
Also raising exception if the directory does not exist. Fixes #151.
This commit is contained in:
@@ -81,6 +81,8 @@ GOOGLE_APPLICATION_CREDENTIALS = 'GOOGLE_APPLICATION_CREDENTIALS'
|
|||||||
# The ~/.config subdirectory containing gcloud credentials. Intended
|
# The ~/.config subdirectory containing gcloud credentials. Intended
|
||||||
# to be swapped out in tests.
|
# to be swapped out in tests.
|
||||||
_CLOUDSDK_CONFIG_DIRECTORY = 'gcloud'
|
_CLOUDSDK_CONFIG_DIRECTORY = 'gcloud'
|
||||||
|
# The environment variable name which can replace ~/.config if set.
|
||||||
|
_CLOUDSDK_CONFIG_ENV_VAR = 'CLOUDSDK_CONFIG'
|
||||||
|
|
||||||
# The error message we show users when we can't find the Application
|
# The error message we show users when we can't find the Application
|
||||||
# Default Credentials.
|
# Default Credentials.
|
||||||
@@ -1268,24 +1270,26 @@ def _get_well_known_file():
|
|||||||
|
|
||||||
WELL_KNOWN_CREDENTIALS_FILE = 'application_default_credentials.json'
|
WELL_KNOWN_CREDENTIALS_FILE = 'application_default_credentials.json'
|
||||||
|
|
||||||
if os.name == 'nt':
|
default_config_dir = os.getenv(_CLOUDSDK_CONFIG_ENV_VAR)
|
||||||
try:
|
if default_config_dir is None:
|
||||||
default_config_path = os.path.join(os.environ['APPDATA'],
|
if os.name == 'nt':
|
||||||
_CLOUDSDK_CONFIG_DIRECTORY)
|
try:
|
||||||
except KeyError:
|
default_config_dir = os.path.join(os.environ['APPDATA'],
|
||||||
# This should never happen unless someone is really messing with things.
|
_CLOUDSDK_CONFIG_DIRECTORY)
|
||||||
drive = os.environ.get('SystemDrive', 'C:')
|
except KeyError:
|
||||||
default_config_path = os.path.join(drive, '\\',
|
# This should never happen unless someone is really messing with things.
|
||||||
_CLOUDSDK_CONFIG_DIRECTORY)
|
drive = os.environ.get('SystemDrive', 'C:')
|
||||||
else:
|
default_config_dir = os.path.join(drive, '\\',
|
||||||
default_config_path = os.path.join(os.path.expanduser('~'),
|
_CLOUDSDK_CONFIG_DIRECTORY)
|
||||||
'.config',
|
else:
|
||||||
_CLOUDSDK_CONFIG_DIRECTORY)
|
default_config_dir = os.path.join(os.path.expanduser('~'),
|
||||||
|
'.config',
|
||||||
|
_CLOUDSDK_CONFIG_DIRECTORY)
|
||||||
|
|
||||||
default_config_path = os.path.join(default_config_path,
|
if not os.path.isdir(default_config_dir):
|
||||||
WELL_KNOWN_CREDENTIALS_FILE)
|
raise OSError('Config directory does not exist', default_config_dir)
|
||||||
|
|
||||||
return default_config_path
|
return os.path.join(default_config_dir, WELL_KNOWN_CREDENTIALS_FILE)
|
||||||
|
|
||||||
|
|
||||||
def _get_application_default_credential_from_file(filename):
|
def _get_application_default_credential_from_file(filename):
|
||||||
|
|||||||
@@ -125,6 +125,11 @@ class DevshellCredentialsTests(unittest.TestCase):
|
|||||||
self.assertEqual('sometoken', creds.access_token)
|
self.assertEqual('sometoken', creds.access_token)
|
||||||
|
|
||||||
def test_refuses_to_save_to_well_known_file(self):
|
def test_refuses_to_save_to_well_known_file(self):
|
||||||
with _AuthReferenceServer():
|
ORIGINAL_ISDIR = os.path.isdir
|
||||||
creds = DevshellCredentials()
|
try:
|
||||||
self.assertRaises(NotImplementedError, save_to_well_known_file, creds)
|
os.path.isdir = lambda path: True
|
||||||
|
with _AuthReferenceServer():
|
||||||
|
creds = DevshellCredentials()
|
||||||
|
self.assertRaises(NotImplementedError, save_to_well_known_file, creds)
|
||||||
|
finally:
|
||||||
|
os.path.isdir = ORIGINAL_ISDIR
|
||||||
|
|||||||
@@ -97,5 +97,12 @@ class AssertionCredentialsTests(unittest.TestCase):
|
|||||||
'default/acquire?scope=dummy_scope')
|
'default/acquire?scope=dummy_scope')
|
||||||
|
|
||||||
def test_save_to_well_known_file(self):
|
def test_save_to_well_known_file(self):
|
||||||
credentials = AppAssertionCredentials([])
|
import os
|
||||||
self.assertRaises(NotImplementedError, save_to_well_known_file, credentials)
|
ORIGINAL_ISDIR = os.path.isdir
|
||||||
|
try:
|
||||||
|
os.path.isdir = lambda path: True
|
||||||
|
credentials = AppAssertionCredentials([])
|
||||||
|
self.assertRaises(NotImplementedError, save_to_well_known_file,
|
||||||
|
credentials)
|
||||||
|
finally:
|
||||||
|
os.path.isdir = ORIGINAL_ISDIR
|
||||||
|
|||||||
@@ -247,11 +247,40 @@ class GoogleCredentialsTests(unittest.TestCase):
|
|||||||
str(error))
|
str(error))
|
||||||
|
|
||||||
def test_get_well_known_file_on_windows(self):
|
def test_get_well_known_file_on_windows(self):
|
||||||
well_known_file = datafile(
|
ORIGINAL_ISDIR = os.path.isdir
|
||||||
os.path.join('gcloud', 'application_default_credentials.json'))
|
try:
|
||||||
os.name = 'nt'
|
os.path.isdir = lambda path: True
|
||||||
os.environ['APPDATA'] = DATA_DIR
|
well_known_file = datafile(
|
||||||
self.assertEqual(well_known_file, _get_well_known_file())
|
os.path.join(client._CLOUDSDK_CONFIG_DIRECTORY,
|
||||||
|
'application_default_credentials.json'))
|
||||||
|
os.name = 'nt'
|
||||||
|
os.environ['APPDATA'] = DATA_DIR
|
||||||
|
self.assertEqual(well_known_file, _get_well_known_file())
|
||||||
|
finally:
|
||||||
|
os.path.isdir = ORIGINAL_ISDIR
|
||||||
|
|
||||||
|
def test_get_well_known_file_with_custom_config_dir(self):
|
||||||
|
ORIGINAL_ENVIRON = os.environ
|
||||||
|
ORIGINAL_ISDIR = os.path.isdir
|
||||||
|
CUSTOM_DIR = 'CUSTOM_DIR'
|
||||||
|
EXPECTED_FILE = os.path.join(CUSTOM_DIR,
|
||||||
|
'application_default_credentials.json')
|
||||||
|
try:
|
||||||
|
os.environ = {client._CLOUDSDK_CONFIG_ENV_VAR: CUSTOM_DIR}
|
||||||
|
os.path.isdir = lambda path: True
|
||||||
|
well_known_file = _get_well_known_file()
|
||||||
|
self.assertEqual(well_known_file, EXPECTED_FILE)
|
||||||
|
finally:
|
||||||
|
os.environ = ORIGINAL_ENVIRON
|
||||||
|
os.path.isdir = ORIGINAL_ISDIR
|
||||||
|
|
||||||
|
def test_get_well_known_file_with_non_existent_config_dir(self):
|
||||||
|
ORIGINAL_ISDIR = os.path.isdir
|
||||||
|
try:
|
||||||
|
os.path.isdir = lambda path: False
|
||||||
|
self.assertRaises(OSError, _get_well_known_file)
|
||||||
|
finally:
|
||||||
|
os.path.isdir = ORIGINAL_ISDIR
|
||||||
|
|
||||||
def test_get_application_default_credential_from_file_service_account(self):
|
def test_get_application_default_credential_from_file_service_account(self):
|
||||||
credentials_file = datafile(
|
credentials_file = datafile(
|
||||||
@@ -410,13 +439,16 @@ class GoogleCredentialsTests(unittest.TestCase):
|
|||||||
os.environ['APPDATA'] = ''
|
os.environ['APPDATA'] = ''
|
||||||
# we can't use self.assertRaisesRegexp() because it is only in Python 2.7+
|
# we can't use self.assertRaisesRegexp() because it is only in Python 2.7+
|
||||||
VALID_CONFIG_DIR = client._CLOUDSDK_CONFIG_DIRECTORY
|
VALID_CONFIG_DIR = client._CLOUDSDK_CONFIG_DIRECTORY
|
||||||
|
ORIGINAL_ISDIR = os.path.isdir
|
||||||
try:
|
try:
|
||||||
|
os.path.isdir = lambda path: True
|
||||||
client._CLOUDSDK_CONFIG_DIRECTORY = 'BOGUS_CONFIG_DIR'
|
client._CLOUDSDK_CONFIG_DIRECTORY = 'BOGUS_CONFIG_DIR'
|
||||||
GoogleCredentials.get_application_default()
|
GoogleCredentials.get_application_default()
|
||||||
self.fail('An exception was expected!')
|
self.fail('An exception was expected!')
|
||||||
except ApplicationDefaultCredentialsError as error:
|
except ApplicationDefaultCredentialsError as error:
|
||||||
self.assertEqual(ADC_HELP_MSG, str(error))
|
self.assertEqual(ADC_HELP_MSG, str(error))
|
||||||
finally:
|
finally:
|
||||||
|
os.path.isdir = ORIGINAL_ISDIR
|
||||||
client._CLOUDSDK_CONFIG_DIRECTORY = VALID_CONFIG_DIR
|
client._CLOUDSDK_CONFIG_DIRECTORY = VALID_CONFIG_DIR
|
||||||
|
|
||||||
def test_from_stream_service_account(self):
|
def test_from_stream_service_account(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user