Allow toggling auth for prometheus metrics

Equivalent to https://github.com/jupyterhub/jupyterhub/pull/2224

Prometheus metrics can potentially leak information about
the user, so they should be kept behind auth by default.
However, for many JupyterHub deployments, they would need
to be scraped by a centralized Prometheus instance that can not
really authenticate separately to each user notebook without
a lot of work. Admins can use this setting to allow unauthenticated
access to the /metrics endpoint.
This commit is contained in:
YuviPanda 2020-11-17 23:14:54 +05:30
parent cd7a06ce88
commit c0ab18a534
2 changed files with 11 additions and 1 deletions

View File

@ -914,8 +914,10 @@ class PrometheusMetricsHandler(IPythonHandler):
"""
Return prometheus metrics for this notebook server
"""
@web.authenticated
def get(self):
if self.settings['authenticate_prometheus'] and not self.logged_in:
raise web.HTTPError(403)
self.set_header('Content-Type', prometheus_client.CONTENT_TYPE_LATEST)
self.write(prometheus_client.generate_latest(prometheus_client.REGISTRY))

View File

@ -283,6 +283,7 @@ class NotebookWebApplication(web.Application):
disable_check_xsrf=jupyter_app.disable_check_xsrf,
allow_remote_access=jupyter_app.allow_remote_access,
local_hostnames=jupyter_app.local_hostnames,
authenticate_prometheus=jupyter_app.authenticate_prometheus,
# managers
kernel_manager=kernel_manager,
@ -1551,6 +1552,13 @@ class NotebookApp(JupyterApp):
is not available.
"""))
authenticate_prometheus = Bool(
True,
help=""""
Require authentication to access prometheus metrics.
"""
).tag(config=True)
# Since use of terminals is also a function of whether the terminado package is
# available, this variable holds the "final indication" of whether terminal functionality
# should be considered (particularly during shutdown/cleanup). It is enabled only