codekingpro/portable-devtools
114k
1# Copyright 2020 Google Inc.2#3# Licensed under the Apache License, Version 2.0 (the "License");4# you may not use this file except in compliance with the License.5# You may obtain a copy of the License at6#7# http://www.apache.org/licenses/LICENSE-2.08#9# Unless required by applicable law or agreed to in writing, software10# distributed under the License is distributed on an "AS IS" BASIS,11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.12# See the License for the specific language governing permissions and13# limitations under the License.14 15"""Application default credentials.16 17Implements application default credentials and project ID detection.18"""19 20import io21import json22import os23 24from google.auth import _default25from google.auth import environment_vars26from google.auth import exceptions27 28 29def load_credentials_from_file(filename, scopes=None, quota_project_id=None):30 """Loads Google credentials from a file.31 32 The credentials file must be a service account key or stored authorized33 user credentials.34 35 Args:36 filename (str): The full path to the credentials file.37 scopes (Optional[Sequence[str]]): The list of scopes for the credentials. If38 specified, the credentials will automatically be scoped if39 necessary40 quota_project_id (Optional[str]): The project ID used for41 quota and billing.42 43 Returns:44 Tuple[google.auth.credentials.Credentials, Optional[str]]: Loaded45 credentials and the project ID. Authorized user credentials do not46 have the project ID information.47 48 Raises:49 google.auth.exceptions.DefaultCredentialsError: if the file is in the50 wrong format or is missing.51 """52 if not os.path.exists(filename):53 raise exceptions.DefaultCredentialsError(54 "File {} was not found.".format(filename)55 )56 57 with io.open(filename, "r") as file_obj:58 try:59 info = json.load(file_obj)60 except ValueError as caught_exc:61 new_exc = exceptions.DefaultCredentialsError(62 "File {} is not a valid json file.".format(filename), caught_exc63 )64 raise new_exc from caught_exc65 66 # The type key should indicate that the file is either a service account67 # credentials file or an authorized user credentials file.68 credential_type = info.get("type")69 70 if credential_type == _default._AUTHORIZED_USER_TYPE:71 from google.oauth2 import _credentials_async as credentials72 73 try:74 credentials = credentials.Credentials.from_authorized_user_info(75 info, scopes=scopes76 )77 except ValueError as caught_exc:78 msg = "Failed to load authorized user credentials from {}".format(filename)79 new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)80 raise new_exc from caught_exc81 if quota_project_id:82 credentials = credentials.with_quota_project(quota_project_id)83 if not credentials.quota_project_id:84 _default._warn_about_problematic_credentials(credentials)85 return credentials, None86 87 elif credential_type == _default._SERVICE_ACCOUNT_TYPE:88 from google.oauth2 import _service_account_async as service_account89 90 try:91 credentials = service_account.Credentials.from_service_account_info(92 info, scopes=scopes93 ).with_quota_project(quota_project_id)94 except ValueError as caught_exc:95 msg = "Failed to load service account credentials from {}".format(filename)96 new_exc = exceptions.DefaultCredentialsError(msg, caught_exc)97 raise new_exc from caught_exc98 return credentials, info.get("project_id")99 100 else:101 raise exceptions.DefaultCredentialsError(102 "The file {file} does not have a valid type. "103 "Type is {type}, expected one of {valid_types}.".format(104 file=filename, type=credential_type, valid_types=_default._VALID_TYPES105 )106 )107 108 109def _get_gcloud_sdk_credentials(quota_project_id=None):110 """Gets the credentials and project ID from the Cloud SDK."""111 from google.auth import _cloud_sdk112 113 # Check if application default credentials exist.114 credentials_filename = _cloud_sdk.get_application_default_credentials_path()115 116 if not os.path.isfile(credentials_filename):117 return None, None118 119 credentials, project_id = load_credentials_from_file(120 credentials_filename, quota_project_id=quota_project_id121 )122 123 if not project_id:124 project_id = _cloud_sdk.get_project_id()125 126 return credentials, project_id127 128 129def _get_explicit_environ_credentials(quota_project_id=None):130 """Gets credentials from the GOOGLE_APPLICATION_CREDENTIALS environment131 variable."""132 from google.auth import _cloud_sdk133 134 cloud_sdk_adc_path = _cloud_sdk.get_application_default_credentials_path()135 explicit_file = os.environ.get(environment_vars.CREDENTIALS)136 137 if explicit_file is not None and explicit_file == cloud_sdk_adc_path:138 # Cloud sdk flow calls gcloud to fetch project id, so if the explicit139 # file path is cloud sdk credentials path, then we should fall back140 # to cloud sdk flow, otherwise project id cannot be obtained.141 return _get_gcloud_sdk_credentials(quota_project_id=quota_project_id)142 143 if explicit_file is not None:144 credentials, project_id = load_credentials_from_file(145 os.environ[environment_vars.CREDENTIALS], quota_project_id=quota_project_id146 )147 148 return credentials, project_id149 150 else:151 return None, None152 153 154def _get_gae_credentials():155 """Gets Google App Engine App Identity credentials and project ID."""156 # While this library is normally bundled with app_engine, there are157 # some cases where it's not available, so we tolerate ImportError.158 159 return _default._get_gae_credentials()160 161 162def _get_gce_credentials(request=None):163 """Gets credentials and project ID from the GCE Metadata Service."""164 # Ping requires a transport, but we want application default credentials165 # to require no arguments. So, we'll use the _http_client transport which166 # uses http.client. This is only acceptable because the metadata server167 # doesn't do SSL and never requires proxies.168 169 # While this library is normally bundled with compute_engine, there are170 # some cases where it's not available, so we tolerate ImportError.171 172 return _default._get_gce_credentials(request)173 174 175def default_async(scopes=None, request=None, quota_project_id=None):176 """Gets the default credentials for the current environment.177 178 `Application Default Credentials`_ provides an easy way to obtain179 credentials to call Google APIs for server-to-server or local applications.180 This function acquires credentials from the environment in the following181 order:182 183 1. If the environment variable ``GOOGLE_APPLICATION_CREDENTIALS`` is set184 to the path of a valid service account JSON private key file, then it is185 loaded and returned. The project ID returned is the project ID defined186 in the service account file if available (some older files do not187 contain project ID information).188 2. If the `Google Cloud SDK`_ is installed and has application default189 credentials set they are loaded and returned.190 191 To enable application default credentials with the Cloud SDK run::192 193 gcloud auth application-default login194 195 If the Cloud SDK has an active project, the project ID is returned. The196 active project can be set using::197 198 gcloud config set project199 200 3. If the application is running in the `App Engine standard environment`_201 (first generation) then the credentials and project ID from the202 `App Identity Service`_ are used.203 4. If the application is running in `Compute Engine`_ or `Cloud Run`_ or204 the `App Engine flexible environment`_ or the `App Engine standard205 environment`_ (second generation) then the credentials and project ID206 are obtained from the `Metadata Service`_.207 5. If no credentials are found,208 :class:`~google.auth.exceptions.DefaultCredentialsError` will be raised.209 210 .. _Application Default Credentials: https://developers.google.com\211 /identity/protocols/application-default-credentials212 .. _Google Cloud SDK: https://cloud.google.com/sdk213 .. _App Engine standard environment: https://cloud.google.com/appengine214 .. _App Identity Service: https://cloud.google.com/appengine/docs/python\215 /appidentity/216 .. _Compute Engine: https://cloud.google.com/compute217 .. _App Engine flexible environment: https://cloud.google.com\218 /appengine/flexible219 .. _Metadata Service: https://cloud.google.com/compute/docs\220 /storing-retrieving-metadata221 .. _Cloud Run: https://cloud.google.com/run222 223 Example::224 225 import google.auth226 227 credentials, project_id = google.auth.default()228 229 Args:230 scopes (Sequence[str]): The list of scopes for the credentials. If231 specified, the credentials will automatically be scoped if232 necessary.233 request (google.auth.transport.Request): An object used to make234 HTTP requests. This is used to detect whether the application235 is running on Compute Engine. If not specified, then it will236 use the standard library http client to make requests.237 quota_project_id (Optional[str]): The project ID used for238 quota and billing.239 Returns:240 Tuple[~google.auth.credentials.Credentials, Optional[str]]:241 the current environment's credentials and project ID. Project ID242 may be None, which indicates that the Project ID could not be243 ascertained from the environment.244 245 Raises:246 ~google.auth.exceptions.DefaultCredentialsError:247 If no credentials were found, or if the credentials found were248 invalid.249 """250 from google.auth._credentials_async import with_scopes_if_required251 from google.auth.credentials import CredentialsWithQuotaProject252 253 explicit_project_id = os.environ.get(254 environment_vars.PROJECT, os.environ.get(environment_vars.LEGACY_PROJECT)255 )256 257 checkers = (258 lambda: _get_explicit_environ_credentials(quota_project_id=quota_project_id),259 lambda: _get_gcloud_sdk_credentials(quota_project_id=quota_project_id),260 _get_gae_credentials,261 lambda: _get_gce_credentials(request),262 )263 264 for checker in checkers:265 credentials, project_id = checker()266 if credentials is not None:267 credentials = with_scopes_if_required(credentials, scopes)268 if quota_project_id and isinstance(269 credentials, CredentialsWithQuotaProject270 ):271 credentials = credentials.with_quota_project(quota_project_id)272 effective_project_id = explicit_project_id or project_id273 if not effective_project_id:274 _default._LOGGER.warning(275 "No project ID could be determined. Consider running "276 "`gcloud config set project` or setting the %s "277 "environment variable",278 environment_vars.PROJECT,279 )280 return credentials, effective_project_id281 282 raise exceptions.DefaultCredentialsError(_default._CLOUD_SDK_MISSING_CREDENTIALS)283 