Skip to content

Commit 2fd54d0

Browse files
author
Jon Wayne Parrott
authored
Read cloud sdk active_config file to determine the right config file to read (#57)
1 parent 78791dd commit 2fd54d0

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

packages/google-auth/google/auth/_cloud_sdk.py

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
"""Helpers for reading the Google Cloud SDK's configuration."""
1616

17+
import io
1718
import os
1819

1920
import six
@@ -32,10 +33,6 @@
3233
# The name of the file in the Cloud SDK config that contains default
3334
# credentials.
3435
_CREDENTIALS_FILENAME = 'application_default_credentials.json'
35-
# The name of the file in the Cloud SDK config that contains the
36-
# active configuration.
37-
_ACTIVE_CONFIG_FILENAME = os.path.join(
38-
'configurations', 'config_default')
3936
# The config section and key for the project ID in the cloud SDK config.
4037
_PROJECT_CONFIG_SECTION = 'core'
4138
_PROJECT_CONFIG_KEY = 'project'
@@ -83,14 +80,49 @@ def get_application_default_credentials_path():
8380
return os.path.join(config_path, _CREDENTIALS_FILENAME)
8481

8582

83+
def _get_active_config(config_path):
84+
"""Gets the active config for the Cloud SDK.
85+
86+
Args:
87+
config_path (str): The Cloud SDK's config path.
88+
89+
Returns:
90+
str: The active configuration name.
91+
"""
92+
active_config_filename = os.path.join(config_path, 'active_config')
93+
94+
if not os.path.isfile(active_config_filename):
95+
return 'default'
96+
97+
with io.open(active_config_filename, 'r', encoding='utf-8') as file_obj:
98+
active_config_name = file_obj.read().strip()
99+
100+
return active_config_name
101+
102+
103+
def _get_config_file(config_path, config_name):
104+
"""Returns the full path to a configuration's config file.
105+
106+
Args:
107+
config_path (str): The Cloud SDK's config path.
108+
config_name (str): The configuration name.
109+
110+
Returns:
111+
str: The config file path.
112+
"""
113+
return os.path.join(
114+
config_path, 'configurations', 'config_{}'.format(config_name))
115+
116+
86117
def get_project_id():
87118
"""Gets the project ID from the Cloud SDK's configuration.
88119
89120
Returns:
90121
Optional[str]: The project ID.
91122
"""
92123
config_path = get_config_path()
93-
config_file = os.path.join(config_path, _ACTIVE_CONFIG_FILENAME)
124+
active_config = _get_active_config(config_path)
125+
config_file = _get_config_file(config_path, active_config)
94126

95127
if not os.path.isfile(config_file):
96128
return None

packages/google-auth/tests/test__cloud_sdk.py

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import os
1717

1818
import mock
19+
import py
1920
import pytest
2021

2122
from google.auth import _cloud_sdk
@@ -41,15 +42,20 @@
4142

4243

4344
@pytest.fixture
44-
def config_file(tmpdir):
45+
def config_dir(tmpdir):
4546
config_dir = tmpdir.join(
4647
'.config', _cloud_sdk._CONFIG_DIRECTORY)
47-
config_file = config_dir.join(
48-
_cloud_sdk._ACTIVE_CONFIG_FILENAME)
4948

5049
with CONFIG_PATH_PATCH as mock_get_config_dir:
5150
mock_get_config_dir.return_value = str(config_dir)
52-
yield config_file
51+
yield config_dir
52+
53+
54+
@pytest.fixture
55+
def config_file(config_dir):
56+
config_file = py.path.local(_cloud_sdk._get_config_file(
57+
str(config_dir), 'default'))
58+
yield config_file
5359

5460

5561
def test_get_project_id(config_file):
@@ -75,6 +81,20 @@ def test_get_project_id_no_section(config_file):
7581
assert project_id is None
7682

7783

84+
def test_get_project_id_non_default_config(config_dir):
85+
active_config = config_dir.join('active_config')
86+
test_config = py.path.local(_cloud_sdk._get_config_file(
87+
str(config_dir), 'test'))
88+
89+
# Create an active config file that points to the 'test' config.
90+
active_config.write('test', ensure=True)
91+
test_config.write(CLOUD_SDK_CONFIG_DATA, ensure=True)
92+
93+
project_id = _cloud_sdk.get_project_id()
94+
95+
assert project_id == 'example-project'
96+
97+
7898
@CONFIG_PATH_PATCH
7999
def test_get_application_default_credentials_path(mock_get_config_dir):
80100
config_path = 'config_path'

0 commit comments

Comments
 (0)