mirror of
https://github.com/jupyterhub/jupyterhub.git
synced 2025-10-08 02:24:08 +00:00
add metrics_authentication decorator to /metrics API
This commit is contained in:
@@ -618,6 +618,9 @@ class JupyterHub(Application):
|
||||
"""
|
||||
).tag(config=True)
|
||||
|
||||
authenticate_prometheus = Bool(True,
|
||||
help="Authentication for prometheus metrics").tag(config=True)
|
||||
|
||||
@observe('api_tokens')
|
||||
def _deprecate_api_tokens(self, change):
|
||||
self.log.warning("JupyterHub.api_tokens is pending deprecation"
|
||||
@@ -1621,6 +1624,7 @@ class JupyterHub(Application):
|
||||
concurrent_spawn_limit=self.concurrent_spawn_limit,
|
||||
spawn_throttle_retry_range=self.spawn_throttle_retry_range,
|
||||
active_server_limit=self.active_server_limit,
|
||||
authenticate_prometheus=self.authenticate_prometheus
|
||||
)
|
||||
# allow configured settings to have priority
|
||||
settings.update(self.tornado_settings)
|
||||
|
@@ -206,6 +206,10 @@ class BaseHandler(RequestHandler):
|
||||
def redirect_to_server(self):
|
||||
return self.settings.get('redirect_to_server', True)
|
||||
|
||||
@property
|
||||
def authenticate_prometheus(self):
|
||||
return self.settings.get('authenticate_prometheus', True)
|
||||
|
||||
def get_auth_token(self):
|
||||
"""Get the authorization token from Authorization header"""
|
||||
auth_header = self.request.headers.get('Authorization', '')
|
||||
|
@@ -2,11 +2,13 @@ from prometheus_client import REGISTRY, CONTENT_TYPE_LATEST, generate_latest
|
||||
from tornado import gen
|
||||
|
||||
from .base import BaseHandler
|
||||
from ..utils import metrics_authentication
|
||||
|
||||
class MetricsHandler(BaseHandler):
|
||||
"""
|
||||
Handler to serve Prometheus metrics
|
||||
"""
|
||||
@metrics_authentication
|
||||
async def get(self):
|
||||
self.set_header('Content-Type', CONTENT_TYPE_LATEST)
|
||||
self.write(generate_latest(REGISTRY))
|
||||
|
@@ -247,6 +247,14 @@ def admin_only(self):
|
||||
if user is None or not user.admin:
|
||||
raise web.HTTPError(403)
|
||||
|
||||
@auth_decorator
|
||||
def metrics_authentication(self):
|
||||
"""Decorator for restricting access to metrics"""
|
||||
user = self.get_current_user()
|
||||
authenticate_prometheus = self.authenticate_prometheus()
|
||||
if user is None and not user.authenticate_prometheus:
|
||||
raise web.HTTPError(403)
|
||||
|
||||
|
||||
# Token utilities
|
||||
|
||||
|
Reference in New Issue
Block a user