Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement application default credentials #32

Merged
merged 6 commits into from Oct 19, 2016
Merged

Implement application default credentials #32

merged 6 commits into from Oct 19, 2016

Conversation

theacodes
Copy link
Contributor

No description provided.

@theacodes theacodes added this to the 1.0.0 milestone Oct 18, 2016


__all__ = [
'default'

This comment was marked as spam.

This comment was marked as spam.

@@ -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.


"""Application default credentials.

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

This comment was marked as spam.

This comment was marked as spam.

# 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.

# 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.


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.

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.


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.



@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.

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.

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.

@@ -285,7 +309,7 @@ def default():
_get_explicit_environ_credentials,
_get_gcloud_sdk_credentials,
_get_gae_credentials,
_get_gce_credentials)
lambda: _get_gce_credentials(request))

This comment was marked as spam.

@@ -0,0 +1,295 @@
# Copyright 2015 Google Inc.

This comment was marked as spam.

Copy link
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.

# 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.

# 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.

'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.

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.

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.


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.



@LOAD_FILE_PATCH
def test__get_explicit_environ_credentials(mock_load, monkeypatch):

This comment was marked as spam.

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.

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.

# 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.



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

This comment was marked as spam.

This comment was marked as spam.



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
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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants