_metadata.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. # Copyright 2016 Google LLC
  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 at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # 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 and
  13. # limitations under the License.
  14. """Provides helper methods for talking to the Compute Engine metadata server.
  15. See https://cloud.google.com/compute/docs/metadata for more details.
  16. """
  17. import datetime
  18. import http.client as http_client
  19. import json
  20. import logging
  21. import os
  22. from urllib.parse import urljoin
  23. from google.auth import _helpers
  24. from google.auth import environment_vars
  25. from google.auth import exceptions
  26. from google.auth import metrics
  27. from google.auth import transport
  28. from google.auth._exponential_backoff import ExponentialBackoff
  29. _LOGGER = logging.getLogger(__name__)
  30. # Environment variable GCE_METADATA_HOST is originally named
  31. # GCE_METADATA_ROOT. For compatibility reasons, here it checks
  32. # the new variable first; if not set, the system falls back
  33. # to the old variable.
  34. _GCE_METADATA_HOST = os.getenv(environment_vars.GCE_METADATA_HOST, None)
  35. if not _GCE_METADATA_HOST:
  36. _GCE_METADATA_HOST = os.getenv(
  37. environment_vars.GCE_METADATA_ROOT, "metadata.google.internal"
  38. )
  39. _METADATA_ROOT = "http://{}/computeMetadata/v1/".format(_GCE_METADATA_HOST)
  40. # This is used to ping the metadata server, it avoids the cost of a DNS
  41. # lookup.
  42. _METADATA_IP_ROOT = "http://{}".format(
  43. os.getenv(environment_vars.GCE_METADATA_IP, "169.254.169.254")
  44. )
  45. _METADATA_FLAVOR_HEADER = "metadata-flavor"
  46. _METADATA_FLAVOR_VALUE = "Google"
  47. _METADATA_HEADERS = {_METADATA_FLAVOR_HEADER: _METADATA_FLAVOR_VALUE}
  48. # Timeout in seconds to wait for the GCE metadata server when detecting the
  49. # GCE environment.
  50. try:
  51. _METADATA_DEFAULT_TIMEOUT = int(os.getenv("GCE_METADATA_TIMEOUT", 3))
  52. except ValueError: # pragma: NO COVER
  53. _METADATA_DEFAULT_TIMEOUT = 3
  54. # Detect GCE Residency
  55. _GOOGLE = "Google"
  56. _GCE_PRODUCT_NAME_FILE = "/sys/class/dmi/id/product_name"
  57. def is_on_gce(request):
  58. """Checks to see if the code runs on Google Compute Engine
  59. Args:
  60. request (google.auth.transport.Request): A callable used to make
  61. HTTP requests.
  62. Returns:
  63. bool: True if the code runs on Google Compute Engine, False otherwise.
  64. """
  65. if ping(request):
  66. return True
  67. if os.name == "nt":
  68. # TODO: implement GCE residency detection on Windows
  69. return False
  70. # Detect GCE residency on Linux
  71. return detect_gce_residency_linux()
  72. def detect_gce_residency_linux():
  73. """Detect Google Compute Engine residency by smbios check on Linux
  74. Returns:
  75. bool: True if the GCE product name file is detected, False otherwise.
  76. """
  77. try:
  78. with open(_GCE_PRODUCT_NAME_FILE, "r") as file_obj:
  79. content = file_obj.read().strip()
  80. except Exception:
  81. return False
  82. return content.startswith(_GOOGLE)
  83. def ping(request, timeout=_METADATA_DEFAULT_TIMEOUT, retry_count=3):
  84. """Checks to see if the metadata server is available.
  85. Args:
  86. request (google.auth.transport.Request): A callable used to make
  87. HTTP requests.
  88. timeout (int): How long to wait for the metadata server to respond.
  89. retry_count (int): How many times to attempt connecting to metadata
  90. server using above timeout.
  91. Returns:
  92. bool: True if the metadata server is reachable, False otherwise.
  93. """
  94. # NOTE: The explicit ``timeout`` is a workaround. The underlying
  95. # issue is that resolving an unknown host on some networks will take
  96. # 20-30 seconds; making this timeout short fixes the issue, but
  97. # could lead to false negatives in the event that we are on GCE, but
  98. # the metadata resolution was particularly slow. The latter case is
  99. # "unlikely".
  100. headers = _METADATA_HEADERS.copy()
  101. headers[metrics.API_CLIENT_HEADER] = metrics.mds_ping()
  102. backoff = ExponentialBackoff(total_attempts=retry_count)
  103. for attempt in backoff:
  104. try:
  105. response = request(
  106. url=_METADATA_IP_ROOT, method="GET", headers=headers, timeout=timeout
  107. )
  108. metadata_flavor = response.headers.get(_METADATA_FLAVOR_HEADER)
  109. return (
  110. response.status == http_client.OK
  111. and metadata_flavor == _METADATA_FLAVOR_VALUE
  112. )
  113. except exceptions.TransportError as e:
  114. _LOGGER.warning(
  115. "Compute Engine Metadata server unavailable on "
  116. "attempt %s of %s. Reason: %s",
  117. attempt,
  118. retry_count,
  119. e,
  120. )
  121. return False
  122. def get(
  123. request,
  124. path,
  125. root=_METADATA_ROOT,
  126. params=None,
  127. recursive=False,
  128. retry_count=5,
  129. headers=None,
  130. return_none_for_not_found_error=False,
  131. ):
  132. """Fetch a resource from the metadata server.
  133. Args:
  134. request (google.auth.transport.Request): A callable used to make
  135. HTTP requests.
  136. path (str): The resource to retrieve. For example,
  137. ``'instance/service-accounts/default'``.
  138. root (str): The full path to the metadata server root.
  139. params (Optional[Mapping[str, str]]): A mapping of query parameter
  140. keys to values.
  141. recursive (bool): Whether to do a recursive query of metadata. See
  142. https://cloud.google.com/compute/docs/metadata#aggcontents for more
  143. details.
  144. retry_count (int): How many times to attempt connecting to metadata
  145. server using above timeout.
  146. headers (Optional[Mapping[str, str]]): Headers for the request.
  147. return_none_for_not_found_error (Optional[bool]): If True, returns None
  148. for 404 error instead of throwing an exception.
  149. Returns:
  150. Union[Mapping, str]: If the metadata server returns JSON, a mapping of
  151. the decoded JSON is returned. Otherwise, the response content is
  152. returned as a string.
  153. Raises:
  154. google.auth.exceptions.TransportError: if an error occurred while
  155. retrieving metadata.
  156. """
  157. base_url = urljoin(root, path)
  158. query_params = {} if params is None else params
  159. headers_to_use = _METADATA_HEADERS.copy()
  160. if headers:
  161. headers_to_use.update(headers)
  162. if recursive:
  163. query_params["recursive"] = "true"
  164. url = _helpers.update_query(base_url, query_params)
  165. backoff = ExponentialBackoff(total_attempts=retry_count)
  166. failure_reason = None
  167. for attempt in backoff:
  168. try:
  169. response = request(url=url, method="GET", headers=headers_to_use)
  170. if response.status in transport.DEFAULT_RETRYABLE_STATUS_CODES:
  171. _LOGGER.warning(
  172. "Compute Engine Metadata server unavailable on "
  173. "attempt %s of %s. Response status: %s",
  174. attempt,
  175. retry_count,
  176. response.status,
  177. )
  178. failure_reason = (
  179. response.data.decode("utf-8")
  180. if hasattr(response.data, "decode")
  181. else response.data
  182. )
  183. continue
  184. else:
  185. break
  186. except exceptions.TransportError as e:
  187. _LOGGER.warning(
  188. "Compute Engine Metadata server unavailable on "
  189. "attempt %s of %s. Reason: %s",
  190. attempt,
  191. retry_count,
  192. e,
  193. )
  194. failure_reason = e
  195. else:
  196. raise exceptions.TransportError(
  197. "Failed to retrieve {} from the Google Compute Engine "
  198. "metadata service. Compute Engine Metadata server unavailable due to {}".format(
  199. url, failure_reason
  200. )
  201. )
  202. content = _helpers.from_bytes(response.data)
  203. if response.status == http_client.NOT_FOUND and return_none_for_not_found_error:
  204. return None
  205. if response.status == http_client.OK:
  206. if (
  207. _helpers.parse_content_type(response.headers["content-type"])
  208. == "application/json"
  209. ):
  210. try:
  211. return json.loads(content)
  212. except ValueError as caught_exc:
  213. new_exc = exceptions.TransportError(
  214. "Received invalid JSON from the Google Compute Engine "
  215. "metadata service: {:.20}".format(content)
  216. )
  217. raise new_exc from caught_exc
  218. else:
  219. return content
  220. raise exceptions.TransportError(
  221. "Failed to retrieve {} from the Google Compute Engine "
  222. "metadata service. Status: {} Response:\n{}".format(
  223. url, response.status, response.data
  224. ),
  225. response,
  226. )
  227. def get_project_id(request):
  228. """Get the Google Cloud Project ID from the metadata server.
  229. Args:
  230. request (google.auth.transport.Request): A callable used to make
  231. HTTP requests.
  232. Returns:
  233. str: The project ID
  234. Raises:
  235. google.auth.exceptions.TransportError: if an error occurred while
  236. retrieving metadata.
  237. """
  238. return get(request, "project/project-id")
  239. def get_universe_domain(request):
  240. """Get the universe domain value from the metadata server.
  241. Args:
  242. request (google.auth.transport.Request): A callable used to make
  243. HTTP requests.
  244. Returns:
  245. str: The universe domain value. If the universe domain endpoint is not
  246. not found, return the default value, which is googleapis.com
  247. Raises:
  248. google.auth.exceptions.TransportError: if an error other than
  249. 404 occurs while retrieving metadata.
  250. """
  251. universe_domain = get(
  252. request, "universe/universe-domain", return_none_for_not_found_error=True
  253. )
  254. if not universe_domain:
  255. return "googleapis.com"
  256. return universe_domain
  257. def get_service_account_info(request, service_account="default"):
  258. """Get information about a service account from the metadata server.
  259. Args:
  260. request (google.auth.transport.Request): A callable used to make
  261. HTTP requests.
  262. service_account (str): The string 'default' or a service account email
  263. address. The determines which service account for which to acquire
  264. information.
  265. Returns:
  266. Mapping: The service account's information, for example::
  267. {
  268. 'email': '...',
  269. 'scopes': ['scope', ...],
  270. 'aliases': ['default', '...']
  271. }
  272. Raises:
  273. google.auth.exceptions.TransportError: if an error occurred while
  274. retrieving metadata.
  275. """
  276. path = "instance/service-accounts/{0}/".format(service_account)
  277. # See https://cloud.google.com/compute/docs/metadata#aggcontents
  278. # for more on the use of 'recursive'.
  279. return get(request, path, params={"recursive": "true"})
  280. def get_service_account_token(request, service_account="default", scopes=None):
  281. """Get the OAuth 2.0 access token for a service account.
  282. Args:
  283. request (google.auth.transport.Request): A callable used to make
  284. HTTP requests.
  285. service_account (str): The string 'default' or a service account email
  286. address. The determines which service account for which to acquire
  287. an access token.
  288. scopes (Optional[Union[str, List[str]]]): Optional string or list of
  289. strings with auth scopes.
  290. Returns:
  291. Tuple[str, datetime]: The access token and its expiration.
  292. Raises:
  293. google.auth.exceptions.TransportError: if an error occurred while
  294. retrieving metadata.
  295. """
  296. if scopes:
  297. if not isinstance(scopes, str):
  298. scopes = ",".join(scopes)
  299. params = {"scopes": scopes}
  300. else:
  301. params = None
  302. metrics_header = {
  303. metrics.API_CLIENT_HEADER: metrics.token_request_access_token_mds()
  304. }
  305. path = "instance/service-accounts/{0}/token".format(service_account)
  306. token_json = get(request, path, params=params, headers=metrics_header)
  307. token_expiry = _helpers.utcnow() + datetime.timedelta(
  308. seconds=token_json["expires_in"]
  309. )
  310. return token_json["access_token"], token_expiry