-
Notifications
You must be signed in to change notification settings - Fork 314
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
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
# Copyright 2015 Google Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
"""Helpers for reading the Google Cloud SDK's configuration.""" | ||
|
||
import configparser | ||
import os | ||
|
||
import six | ||
|
||
from google.auth import environment_vars | ||
import google.oauth2.credentials | ||
|
||
# The Google OAuth 2.0 token endpoint. Used for authorized user credentials. | ||
_GOOGLE_OAUTH2_TOKEN_ENDPOINT = 'https://accounts.google.com/o/oauth2/token' | ||
|
||
# The ~/.config subdirectory containing gcloud credentials. | ||
_CONFIG_DIRECTORY = 'gcloud' | ||
# Windows systems store config at %APPDATA%\gcloud | ||
_WINDOWS_CONFIG_ROOT_ENV_VAR = 'APPDATA' | ||
# The name of the file in the Cloud SDK config that contains default | ||
# credentials. | ||
_CREDENTIALS_FILENAME = 'application_default_credentials.json' | ||
# The name of the file in the Cloud SDK config that contains the | ||
# active configuration. | ||
_ACTIVE_CONFIG_FILENAME = os.path.join( | ||
'configurations', 'config_default') | ||
# The config section and key for the project ID in the cloud SDK config. | ||
_PROJECT_CONFIG_SECTION = 'core' | ||
_PROJECT_CONFIG_KEY = 'project' | ||
|
||
|
||
def get_config_path(): | ||
"""Returns the absolute path the the Cloud SDK's configuration directory. | ||
|
||
Returns: | ||
str: The Cloud SDK config path. | ||
""" | ||
# If the path is explicitly set, return that. | ||
try: | ||
return os.environ[environment_vars.CLOUD_SDK_CONFIG_DIR] | ||
except KeyError: | ||
pass | ||
|
||
# Non-windows systems store this at ~/.config/gcloud | ||
if os.name != 'nt': | ||
return os.path.join( | ||
os.path.expanduser('~'), '.config', _CONFIG_DIRECTORY) | ||
# Windows systems store config at %APPDATA%\gcloud | ||
else: | ||
try: | ||
return os.path.join( | ||
os.environ[_WINDOWS_CONFIG_ROOT_ENV_VAR], | ||
_CONFIG_DIRECTORY) | ||
except KeyError: | ||
# This should never happen unless someone is really | ||
# messing with things, but we'll cover the case anyway. | ||
drive = os.environ.get('SystemDrive', 'C:') | ||
return os.path.join( | ||
drive, '\\', _CONFIG_DIRECTORY) | ||
|
||
|
||
def get_application_default_credentials_path(): | ||
"""Gets the path to the application default credentials file. | ||
|
||
The path may or may not exist. | ||
|
||
Returns: | ||
str: The full path to application default credentials. | ||
""" | ||
config_path = get_config_path() | ||
return os.path.join(config_path, _CREDENTIALS_FILENAME) | ||
|
||
|
||
def get_project_id(): | ||
"""Gets the project ID from the Cloud SDK's configuration. | ||
|
||
Returns: | ||
Optional[str]: The project ID. | ||
""" | ||
config_path = get_config_path() | ||
config_file = os.path.join(config_path, _ACTIVE_CONFIG_FILENAME) | ||
|
||
if not os.path.isfile(config_file): | ||
return None | ||
|
||
config = configparser.RawConfigParser() | ||
|
||
try: | ||
config.read(config_file) | ||
except configparser.Error: | ||
return None | ||
|
||
if config.has_section(_PROJECT_CONFIG_SECTION): | ||
return config.get( | ||
_PROJECT_CONFIG_SECTION, _PROJECT_CONFIG_KEY) | ||
|
||
|
||
def load_authorized_user_credentials(info): | ||
"""Loads an authorized user credential. | ||
|
||
Args: | ||
info (Mapping[str, str]): The loaded file's data. | ||
|
||
Returns: | ||
google.oauth2.credentials.Credentials: The constructed credentials. | ||
|
||
Raises: | ||
ValueError: if the info is in the wrong format or missing data. | ||
""" | ||
keys_needed = set(('refresh_token', 'client_id', 'client_secret')) | ||
missing = keys_needed.difference(six.iterkeys(info)) | ||
|
||
if missing: | ||
raise ValueError( | ||
'Authorized user info was not in the expected format, missing ' | ||
'fields {}.'.format(', '.join(missing))) | ||
|
||
return google.oauth2.credentials.Credentials( | ||
None, # No access token, must be refreshed. | ||
refresh_token=info['refresh_token'], | ||
token_uri=_GOOGLE_OAUTH2_TOKEN_ENDPOINT, | ||
client_id=info['client_id'], | ||
client_secret=info['client_secret']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
Sorry, something went wrong.