-
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
Conversation
|
||
|
||
__all__ = [ | ||
'default' |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -0,0 +1,295 @@ | |||
# Copyright 2015 Google Inc. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
"""Application default credentials. | ||
|
||
Implementes application default credentials and project ID detection.""" |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
# 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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
# 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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
|
||
@LOAD_FILE_PATCH | ||
def test__get_explicit_environ_credentials(mock_load, monkeypatch): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
@@ -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.
This comment was marked as spam.
Sorry, something went wrong.
@@ -0,0 +1,295 @@ | |||
# Copyright 2015 Google Inc. |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
There was a problem hiding this 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.
This comment was marked as spam.
Sorry, something went wrong.
# 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.
Sorry, something went wrong.
'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.
This comment was marked as spam.
Sorry, something went wrong.
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.
This comment was marked as spam.
Sorry, something went wrong.
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.
This comment was marked as spam.
Sorry, something went wrong.
|
||
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.
Sorry, something went wrong.
|
||
|
||
@LOAD_FILE_PATCH | ||
def test__get_explicit_environ_credentials(mock_load, monkeypatch): |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
Sorry, something went wrong.
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
# 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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
|
||
PROJECT = 'GOOGLE_CLOUD_PROJECT' | ||
"""Environment variable defining default project.""" |
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
|
||
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
LGTM pending my few tiny nits |
No description provided.