optional_utils.py 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. """
  2. Optional utils module contains utility methods
  3. that require optional dependencies.
  4. """
  5. import asyncio
  6. import collections
  7. import functools
  8. import inspect
  9. import logging
  10. import os
  11. import time
  12. import traceback
  13. from collections import namedtuple
  14. from types import ModuleType
  15. from typing import Callable, List, Optional, Set, Union
  16. from aiohttp.web import Request, Response
  17. import ray
  18. import ray.dashboard.consts as dashboard_consts
  19. import ray.dashboard.utils as dashboard_utils
  20. from ray._private.ray_constants import env_bool
  21. from ray._raylet import RAY_INTERNAL_DASHBOARD_NAMESPACE
  22. # All third-party dependencies that are not included in the minimal Ray
  23. # installation must be included in this file. This allows us to determine if
  24. # the agent has the necessary dependencies to be started.
  25. from ray.dashboard.optional_deps import aiohttp, hdrs
  26. from ray.dashboard.routes import method_route_table_factory, rest_response
  27. from ray.dashboard.utils import (
  28. DashboardAgentModule,
  29. DashboardHeadModule,
  30. )
  31. try:
  32. create_task = asyncio.create_task
  33. except AttributeError:
  34. create_task = asyncio.ensure_future
  35. logger = logging.getLogger(__name__)
  36. DashboardHeadRouteTable = method_route_table_factory()
  37. DashboardAgentRouteTable = method_route_table_factory()
  38. # The cache value type used by aiohttp_cache.
  39. _AiohttpCacheValue = namedtuple("AiohttpCacheValue", ["data", "expiration", "task"])
  40. # The methods with no request body used by aiohttp_cache.
  41. _AIOHTTP_CACHE_NOBODY_METHODS = {hdrs.METH_GET, hdrs.METH_DELETE}
  42. def aiohttp_cache(
  43. ttl_seconds=dashboard_consts.AIOHTTP_CACHE_TTL_SECONDS,
  44. maxsize=dashboard_consts.AIOHTTP_CACHE_MAX_SIZE,
  45. enable=not env_bool(dashboard_consts.AIOHTTP_CACHE_DISABLE_ENVIRONMENT_KEY, False),
  46. ):
  47. assert maxsize > 0
  48. cache = collections.OrderedDict()
  49. def _wrapper(handler):
  50. if enable:
  51. @functools.wraps(handler)
  52. async def _cache_handler(*args) -> aiohttp.web.Response:
  53. # Make the route handler as a bound method.
  54. # The args may be:
  55. # * (Request, )
  56. # * (self, Request)
  57. req = args[-1]
  58. # If nocache=1 in query string, bypass cache.
  59. if req.query.get("nocache") == "1":
  60. return await handler(*args)
  61. # Make key.
  62. if req.method in _AIOHTTP_CACHE_NOBODY_METHODS:
  63. key = req.path_qs
  64. else:
  65. key = (req.path_qs, await req.read())
  66. # Query cache.
  67. value = cache.get(key)
  68. if value is not None:
  69. cache.move_to_end(key)
  70. if not value.task.done() or value.expiration >= time.time():
  71. # Update task not done or the data is not expired.
  72. return aiohttp.web.Response(**value.data)
  73. def _update_cache(task):
  74. try:
  75. response = task.result()
  76. except Exception:
  77. response = rest_response(
  78. status_code=dashboard_utils.HTTPStatusCode.INTERNAL_ERROR,
  79. message=traceback.format_exc(),
  80. )
  81. data = {
  82. "status": response.status,
  83. "headers": dict(response.headers),
  84. "body": response.body,
  85. }
  86. cache[key] = _AiohttpCacheValue(
  87. data, time.time() + ttl_seconds, task
  88. )
  89. cache.move_to_end(key)
  90. if len(cache) > maxsize:
  91. cache.popitem(last=False)
  92. return response
  93. task = create_task(handler(*args))
  94. task.add_done_callback(_update_cache)
  95. if value is None:
  96. return await task
  97. else:
  98. return aiohttp.web.Response(**value.data)
  99. suffix = f"[cache ttl={ttl_seconds}, max_size={maxsize}]"
  100. _cache_handler.__name__ += suffix
  101. _cache_handler.__qualname__ += suffix
  102. return _cache_handler
  103. else:
  104. return handler
  105. if inspect.iscoroutinefunction(ttl_seconds):
  106. target_func = ttl_seconds
  107. ttl_seconds = dashboard_consts.AIOHTTP_CACHE_TTL_SECONDS
  108. return _wrapper(target_func)
  109. else:
  110. return _wrapper
  111. def is_browser_request(req: Request) -> bool:
  112. """Best-effort detection if the request was made by a browser.
  113. Uses three heuristics:
  114. 1) If the `User-Agent` header starts with 'Mozilla'. This heuristic is weak,
  115. but hard for a browser to bypass e.g., fetch/xhr and friends cannot alter the
  116. user agent, but requests made with an HTTP library can stumble into this if
  117. they choose to user a browser-like user agent. At the time of writing, all
  118. common browsers' user agents start with 'Mozilla'.
  119. 2) If any of the `Sec-Fetch-*` headers are present.
  120. 3) If any of the various CORS headers are present
  121. """
  122. return req.headers.get("User-Agent", "").startswith("Mozilla") or any(
  123. h in req.headers
  124. for h in (
  125. # Origin and Referer are sent by browser user agents to give
  126. # information about the requesting origin
  127. "Referer",
  128. "Origin",
  129. # Sec-Fetch headers are sent with many but not all `fetch`
  130. # requests, and will eventually be sent on all requests.
  131. "Sec-Fetch-Mode",
  132. "Sec-Fetch-Dest",
  133. "Sec-Fetch-Site",
  134. "Sec-Fetch-User",
  135. # CORS headers specifying which other headers are modified
  136. "Access-Control-Request-Method",
  137. "Access-Control-Request-Headers",
  138. )
  139. )
  140. def get_browser_request_middleware(
  141. aiohttp_module: ModuleType,
  142. allowed_methods: Optional[Set[str]] = None,
  143. allowed_paths: Optional[List[str]] = None,
  144. ):
  145. """Create middleware that restricts browser access to specified HTTP methods.
  146. This middleware blocks browser requests to prevent DNS rebinding and CSRF
  147. attacks. Only explicitly allowed methods are permitted from browsers.
  148. Args:
  149. aiohttp_module: The aiohttp module to use
  150. allowed_methods: Set of HTTP methods browsers are allowed to use.
  151. allowed_paths: List of paths that bypass the method check entirely,
  152. allowing any method from browsers.
  153. Returns:
  154. An aiohttp middleware function
  155. """
  156. allowed_methods = allowed_methods or set()
  157. @aiohttp_module.web.middleware
  158. async def browser_request_middleware(request, handler):
  159. if not is_browser_request(request):
  160. return await handler(request)
  161. # Allow whitelisted paths to bypass the check
  162. if allowed_paths and request.path in allowed_paths:
  163. return await handler(request)
  164. # No methods allowed for browsers, return `403` status.
  165. if not allowed_methods:
  166. return aiohttp_module.web.Response(
  167. status=403, text="Browser requests not allowed."
  168. )
  169. # This specific method is not allowed, return `405` status.
  170. if request.method not in allowed_methods:
  171. return aiohttp_module.web.Response(
  172. status=405,
  173. text=f"'{request.method}' method not allowed for browser traffic.",
  174. )
  175. return await handler(request)
  176. return browser_request_middleware
  177. def init_ray_and_catch_exceptions() -> Callable:
  178. """Decorator to be used on methods that require being connected to Ray."""
  179. def decorator_factory(f: Callable) -> Callable:
  180. @functools.wraps(f)
  181. async def decorator(
  182. self: Union[DashboardAgentModule, DashboardHeadModule], *args, **kwargs
  183. ):
  184. try:
  185. if not ray.is_initialized():
  186. try:
  187. address = self.gcs_address
  188. logger.info(f"Connecting to ray with address={address}")
  189. # Set the gcs rpc timeout to shorter
  190. os.environ["RAY_gcs_server_request_timeout_seconds"] = str(
  191. dashboard_consts.GCS_RPC_TIMEOUT_SECONDS
  192. )
  193. # Init ray without logging to driver
  194. # to avoid infinite logging issue.
  195. ray.init(
  196. address=address,
  197. log_to_driver=False,
  198. configure_logging=False,
  199. namespace=RAY_INTERNAL_DASHBOARD_NAMESPACE,
  200. _skip_env_hook=True,
  201. )
  202. except Exception as e:
  203. ray.shutdown()
  204. raise e from None
  205. return await f(self, *args, **kwargs)
  206. except Exception as e:
  207. logger.exception(f"Unexpected error in handler: {e}")
  208. return Response(
  209. text=traceback.format_exc(),
  210. status=aiohttp.web.HTTPInternalServerError.status_code,
  211. )
  212. return decorator
  213. return decorator_factory