Skip to content
This repository was archived by the owner on Mar 6, 2026. It is now read-only.

Implement application default credentials#32

Merged
theacodes merged 6 commits intomasterfrom
adc
Oct 19, 2016
Merged

Implement application default credentials#32
theacodes merged 6 commits intomasterfrom
adc

Conversation

@theacodes
Copy link
Copy Markdown
Contributor

No description provided.

@theacodes theacodes added this to the 1.0.0 milestone Oct 18, 2016
Comment thread google/auth/__init__.py Outdated


__all__ = [
'default'

This comment was marked as spam.

This comment was marked as spam.

Comment thread google/auth/_default.py
@@ -0,0 +1,295 @@
# Copyright 2015 Google Inc.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

Comment thread google/auth/_default.py Outdated

"""Application default credentials.

Implementes application default credentials and project ID detection."""

This comment was marked as spam.

This comment was marked as spam.

Comment thread google/auth/_default.py Outdated
# Environment variable for explicit application default credentials and project
# ID.
_CREDENTIALS_ENV = 'GOOGLE_APPLICATION_CREDENTIALS'
_PROJECT_ENV = 'GCLOUD_PROJECT'

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

Comment thread google/auth/_default.py Outdated
# The ~/.config subdirectory containing gcloud credentials.
_CLOUDSDK_CONFIG_DIRECTORY = 'gcloud'
# The environment variable name which can replace ~/.config if set.
_CLOUDSDK_CONFIG_ENV = 'CLOUDSDK_CONFIG'

This comment was marked as spam.

This comment was marked as spam.

Comment thread tests/test__default.py

def test__load_credentials_from_file_invalid_type(tmpdir):
jsonfile = tmpdir.join('invalid.json')
jsonfile.write(json.dumps({'type': 'not-a-real-type'}))

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

Comment thread tests/test__default.py
assert project_id == SERVICE_ACCOUNT_FILE_DATA['project_id']


@mock.patch.dict(os.environ, {}, clear=True)

This comment was marked as spam.

This comment was marked as spam.

Comment thread tests/test__default.py Outdated

LOAD_FILE_PATCH = mock.patch(
'google.auth._default._load_credentials_from_file', return_value=(
mock.sentinel.credentials, mock.sentinel.project_id))

This comment was marked as spam.

This comment was marked as spam.

Comment thread tests/test__default.py


@LOAD_FILE_PATCH
def test__get_explicit_environ_credentials(mock_load, monkeypatch):

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

Comment thread tests/test__default.py Outdated
config_path = _default._get_gcloud_sdk_config_path()

assert os.path.split(config_path) == (
'appdata', _default._CLOUDSDK_CONFIG_DIRECTORY)

This comment was marked as spam.

This comment was marked as spam.

Comment thread google/auth/_default.py
If the Cloud SDK has an active project, the project ID is returned. The
active project can be set using::

gcloud config set project

This comment was marked as spam.

This comment was marked as spam.

Comment thread google/auth/_default.py
_get_gcloud_sdk_credentials,
_get_gae_credentials,
_get_gce_credentials)
lambda: _get_gce_credentials(request))

This comment was marked as spam.

Comment thread google/auth/_default.py
@@ -0,0 +1,295 @@
# Copyright 2015 Google Inc.

This comment was marked as spam.

Copy link
Copy Markdown
Contributor Author

@theacodes theacodes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dhermes I think I got everything, but you might want to take a peak at the _cloud_sdk and test__cloud_sdk modules.

Comment thread google/auth/_default.py Outdated
# Environment variable for explicit application default credentials and project
# ID.
_CREDENTIALS_ENV = 'GOOGLE_APPLICATION_CREDENTIALS'
_PROJECT_ENV = 'GCLOUD_PROJECT'

This comment was marked as spam.

Comment thread google/auth/_default.py Outdated
# The ~/.config subdirectory containing gcloud credentials.
_CLOUDSDK_CONFIG_DIRECTORY = 'gcloud'
# The environment variable name which can replace ~/.config if set.
_CLOUDSDK_CONFIG_ENV = 'CLOUDSDK_CONFIG'

This comment was marked as spam.

Comment thread google/auth/_default.py Outdated
'Could not automatically determine credentials. Please set {env} or '
'explicitly create credential and re-run the application. For more '
'information, please see https://developers.google.com/accounts/docs'
'/application-default-credentials.'.format(env=_CREDENTIALS_ENV))

This comment was marked as spam.

Comment thread google/auth/_default.py Outdated
refresh_token=info['refresh_token'],
token_uri=_GOOGLE_OAUTH2_TOKEN_ENDPOINT,
client_id=info['client_id'],
client_secret=info['client_secret'])

This comment was marked as spam.

Comment thread google/auth/_default.py
if credential_type == _SERVICE_ACCOUNT_TYPE:
credentials = service_account.Credentials.from_service_account_info(
info)
return credentials, info.get('project_id')

This comment was marked as spam.

Comment thread tests/test__default.py

def test__load_credentials_from_file_invalid_type(tmpdir):
jsonfile = tmpdir.join('invalid.json')
jsonfile.write(json.dumps({'type': 'not-a-real-type'}))

This comment was marked as spam.

Comment thread tests/test__default.py


@LOAD_FILE_PATCH
def test__get_explicit_environ_credentials(mock_load, monkeypatch):

This comment was marked as spam.

Comment thread google/auth/_default.py
If the Cloud SDK has an active project, the project ID is returned. The
active project can be set using::

gcloud config set project

This comment was marked as spam.

Comment thread google/auth/_default.py Outdated
Could not automatically determine credentials. Please set {env} or '
explicitly create credential and re-run the application. For more '
information, please see '
'https://developers.google.com/accounts/docs/application-default-credentials.

This comment was marked as spam.

This comment was marked as spam.

Comment thread google/auth/environment_vars.py Outdated
# See the License for the specific language governing permissions and
# limitations under the License.

"""Environment variables used by google.auth."""

This comment was marked as spam.

This comment was marked as spam.

Comment thread google/auth/environment_vars.py Outdated


PROJECT = 'GOOGLE_CLOUD_PROJECT'
"""Environment variable defining default project."""

This comment was marked as spam.

This comment was marked as spam.

Comment thread tests/test__default.py Outdated


def test__load_credentials_from_file_authorized_user_bad_format(tmpdir):
filename = tmpdir.join('authoirzed_user_bad.json')

This comment was marked as spam.

This comment was marked as spam.

@dhermes
Copy link
Copy Markdown
Contributor

dhermes commented Oct 19, 2016

LGTM pending my few tiny nits

@theacodes theacodes merged commit aadb3de into master Oct 19, 2016
@theacodes theacodes deleted the adc branch October 19, 2016 16:34
@theacodes theacodes mentioned this pull request Oct 21, 2016
12 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants