/
pages.py
691 lines (586 loc) · 25.3 KB
/
pages.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
"""Basic html-rendering handlers."""
# Copyright (c) Jupyter Development Team.
# Distributed under the terms of the Modified BSD License.
import asyncio
import time
from collections import defaultdict
from datetime import datetime
from http.client import responses
from jinja2 import TemplateNotFound
from tornado import web
from tornado.httputil import url_concat
from .. import __version__, orm
from ..metrics import SERVER_POLL_DURATION_SECONDS, ServerPollStatus
from ..scopes import describe_raw_scopes, needs_scope
from ..utils import maybe_future, url_escape_path, url_path_join, utcnow
from .base import BaseHandler
class RootHandler(BaseHandler):
"""Render the Hub root page.
If next argument is passed by single-user server,
redirect to base_url + single-user page.
If logged in, redirects to:
- single-user server if settings.redirect_to_server (default)
- hub home, otherwise
Otherwise, renders login page.
"""
def get(self):
user = self.current_user
if self.default_url:
# As set in jupyterhub_config.py
if callable(self.default_url):
url = self.default_url(self)
else:
url = self.default_url
elif user:
url = self.get_next_url(user)
else:
url = url_concat(self.settings["login_url"], dict(next=self.request.uri))
self.redirect(url)
class HomeHandler(BaseHandler):
"""Render the user's home page."""
@web.authenticated
async def get(self):
user = self.current_user
if user.running:
# trigger poll_and_notify event in case of a server that died
await user.spawner.poll_and_notify()
# send the user to /spawn if they have no active servers,
# to establish that this is an explicit spawn request rather
# than an implicit one, which can be caused by any link to `/user/:name(/:server_name)`
if user.active:
url = url_path_join(self.base_url, 'user', user.escaped_name)
else:
url = url_path_join(self.hub.base_url, 'spawn', user.escaped_name)
auth_state = await user.get_auth_state()
html = await self.render_template(
'home.html',
auth_state=auth_state,
user=user,
url=url,
allow_named_servers=self.allow_named_servers,
named_server_limit_per_user=await self.get_current_user_named_server_limit(),
url_path_join=url_path_join,
# can't use user.spawners because the stop method of User pops named servers from user.spawners when they're stopped
spawners=user.orm_user._orm_spawners,
default_server=user.spawner,
)
self.finish(html)
class SpawnHandler(BaseHandler):
"""Handle spawning of single-user servers
GET renders the form, POST handles form submission.
If no `options_form` is enabled, GET triggers spawn directly.
"""
default_url = None
async def _render_form(self, for_user, spawner_options_form, message=''):
auth_state = await for_user.get_auth_state()
return await self.render_template(
'spawn.html',
for_user=for_user,
auth_state=auth_state,
spawner_options_form=spawner_options_form,
error_message=message,
url=url_concat(
self.request.uri, {"_xsrf": self.xsrf_token.decode('ascii')}
),
spawner=for_user.spawner,
)
@web.authenticated
def get(self, user_name=None, server_name=''):
"""GET renders form for spawning with user-specified options
or triggers spawn via redirect if there is no form.
"""
# two-stage to get the right signature for @require_scopes filter on user_name
if user_name is None:
user_name = self.current_user.name
if server_name is None:
server_name = ""
return self._get(user_name=user_name, server_name=server_name)
@needs_scope("servers")
async def _get(self, user_name, server_name):
for_user = user_name
user = current_user = self.current_user
if for_user != user.name:
user = self.find_user(for_user)
if user is None:
raise web.HTTPError(404, f"No such user: {for_user}")
if server_name:
if not self.allow_named_servers:
raise web.HTTPError(400, "Named servers are not enabled.")
named_server_limit_per_user = (
await self.get_current_user_named_server_limit()
)
if named_server_limit_per_user > 0 and server_name not in user.orm_spawners:
named_spawners = list(user.all_spawners(include_default=False))
if named_server_limit_per_user <= len(named_spawners):
raise web.HTTPError(
400,
f"User {user.name} already has the maximum of {named_server_limit_per_user} named servers."
" One must be deleted before a new server can be created",
)
if not self.allow_named_servers and user.running:
url = self.get_next_url(user, default=user.server_url(""))
self.log.info("User is running: %s", user.name)
self.redirect(url)
return
spawner = user.get_spawner(server_name, replace_failed=True)
pending_url = self._get_pending_url(user, server_name)
# spawner is active, redirect back to get progress, etc.
if spawner.ready:
self.log.info("Server %s is already running", spawner._log_name)
next_url = self.get_next_url(user, default=user.server_url(server_name))
self.redirect(next_url)
return
elif spawner.active:
self.log.info("Server %s is already active", spawner._log_name)
self.redirect(pending_url)
return
# Add handler to spawner here so you can access query params in form rendering.
spawner.handler = self
# auth_state may be an input to options form,
# so resolve the auth state hook here
auth_state = await user.get_auth_state()
await spawner.run_auth_state_hook(auth_state)
# Try to start server directly when query arguments are passed.
error_message = ''
query_options = {}
for key, byte_list in self.request.query_arguments.items():
query_options[key] = [bs.decode('utf8') for bs in byte_list]
# 'next' is reserved argument for redirect after spawn
query_options.pop('next', None)
if len(query_options) > 0:
try:
self.log.debug(
"Triggering spawn with supplied query arguments for %s",
spawner._log_name,
)
options = await maybe_future(spawner.options_from_query(query_options))
return await self._wrap_spawn_single_user(
user, server_name, spawner, pending_url, options
)
except Exception as e:
self.log.error(
"Failed to spawn single-user server with query arguments",
exc_info=True,
)
error_message = str(e)
# fallback to behavior without failing query arguments
spawner_options_form = await spawner.get_options_form()
if spawner_options_form:
self.log.debug("Serving options form for %s", spawner._log_name)
form = await self._render_form(
for_user=user,
spawner_options_form=spawner_options_form,
message=error_message,
)
self.finish(form)
else:
self.log.debug(
"Triggering spawn with default options for %s", spawner._log_name
)
return await self._wrap_spawn_single_user(
user, server_name, spawner, pending_url
)
@web.authenticated
def post(self, user_name=None, server_name=''):
"""POST spawns with user-specified options"""
if user_name is None:
user_name = self.current_user.name
if server_name is None:
server_name = ""
return self._post(user_name=user_name, server_name=server_name)
@needs_scope("servers")
async def _post(self, user_name, server_name):
for_user = user_name
user = current_user = self.current_user
if for_user != user.name:
user = self.find_user(for_user)
if user is None:
raise web.HTTPError(404, f"No such user: {for_user}")
spawner = user.get_spawner(server_name, replace_failed=True)
if spawner.ready:
raise web.HTTPError(400, f"{spawner._log_name} is already running")
elif spawner.pending:
raise web.HTTPError(
400, f"{spawner._log_name} is pending {spawner.pending}"
)
form_options = {}
for key, byte_list in self.request.body_arguments.items():
form_options[key] = [bs.decode('utf8') for bs in byte_list]
for key, byte_list in self.request.files.items():
form_options[f"{key}_file"] = byte_list
try:
self.log.debug(
"Triggering spawn with supplied form options for %s", spawner._log_name
)
options = await maybe_future(spawner.run_options_from_form(form_options))
pending_url = self._get_pending_url(user, server_name)
return await self._wrap_spawn_single_user(
user, server_name, spawner, pending_url, options
)
except Exception as e:
self.log.error(
"Failed to spawn single-user server with form", exc_info=True
)
spawner_options_form = await user.spawner.get_options_form()
form = await self._render_form(
for_user=user, spawner_options_form=spawner_options_form, message=str(e)
)
self.finish(form)
return
def _get_pending_url(self, user, server_name):
# resolve `?next=...`, falling back on the spawn-pending url
# must not be /user/server for named servers,
# which may get handled by the default server if they aren't ready yet
pending_url = url_path_join(
self.hub.base_url,
"spawn-pending",
user.escaped_name,
url_escape_path(server_name),
)
pending_url = self.append_query_parameters(pending_url, exclude=['next'])
if self.get_argument('next', None):
# preserve `?next=...` through spawn-pending
pending_url = url_concat(pending_url, {'next': self.get_argument('next')})
return pending_url
async def _wrap_spawn_single_user(
self, user, server_name, spawner, pending_url, options=None
):
# Explicit spawn request: clear _spawn_future
# which may have been saved to prevent implicit spawns
# after a failure.
if spawner._spawn_future and spawner._spawn_future.done():
spawner._spawn_future = None
# not running, no form. Trigger spawn and redirect back to /user/:name
f = asyncio.ensure_future(
self.spawn_single_user(user, server_name, options=options)
)
done, pending = await asyncio.wait([f], timeout=1)
# If spawn_single_user throws an exception, raise a 500 error
# otherwise it may cause a redirect loop
if f.done() and f.exception():
exc = f.exception()
self.log.exception(f"Error starting server {spawner._log_name}: {exc}")
if isinstance(exc, web.HTTPError):
# allow custom HTTPErrors to pass through
raise exc
raise web.HTTPError(
500,
f"Unhandled error starting server {spawner._log_name}",
)
return self.redirect(pending_url)
class SpawnPendingHandler(BaseHandler):
"""Handle /hub/spawn-pending/:user/:server
One and only purpose:
- wait for pending spawn
- serve progress bar
- redirect to /user/:name when ready
- show error if spawn failed
Functionality split out of /user/:name handler to
have clearer behavior at the right time.
Requests for this URL will never trigger any actions
such as spawning new servers.
"""
@web.authenticated
@needs_scope("servers")
async def get(self, user_name, server_name=''):
for_user = user_name
user = current_user = self.current_user
if for_user != current_user.name:
user = self.find_user(for_user)
if user is None:
raise web.HTTPError(404, f"No such user: {for_user}")
if server_name and server_name not in user.spawners:
raise web.HTTPError(404, f"{user.name} has no such server {server_name}")
escaped_server_name = url_escape_path(server_name)
spawner = user.spawners[server_name]
if spawner.ready:
# spawner is ready and waiting. Redirect to it.
next_url = self.get_next_url(default=user.server_url(server_name))
self.redirect(next_url)
return
# if spawning fails for any reason, point users to /hub/home to retry
self.extra_error_html = self.spawn_home_error
auth_state = await user.get_auth_state()
# First, check for previous failure.
if not spawner.active and spawner._failed:
# Condition: spawner not active and last spawn failed
# (failure is available as spawner._spawn_future.exception()).
# Implicit spawn on /user/:name is not allowed if the user's last spawn failed.
# We should point the user to Home if the most recent spawn failed.
exc = spawner._spawn_future.exception()
self.log.error("Previous spawn for %s failed: %s", spawner._log_name, exc)
spawn_url = url_path_join(
self.hub.base_url, "spawn", user.escaped_name, escaped_server_name
)
self.set_status(500)
html = await self.render_template(
"not_running.html",
user=user,
auth_state=auth_state,
server_name=server_name,
spawn_url=spawn_url,
failed=True,
failed_html_message=getattr(exc, 'jupyterhub_html_message', ''),
failed_message=getattr(exc, 'jupyterhub_message', ''),
exception=exc,
)
self.finish(html)
return
# Check for pending events. This should usually be the case
# when we are on this page.
# page could be pending spawn *or* stop
if spawner.pending:
self.log.info("%s is pending %s", spawner._log_name, spawner.pending)
# spawn has started, but not finished
url_parts = []
if spawner.pending == "stop":
page = "stop_pending.html"
else:
page = "spawn_pending.html"
html = await self.render_template(
page,
user=user,
spawner=spawner,
progress_url=url_concat(
spawner._progress_url, {"_xsrf": self.xsrf_token.decode('ascii')}
),
auth_state=auth_state,
)
self.finish(html)
return
# spawn is supposedly ready, check on the status
if spawner.ready:
poll_start_time = time.perf_counter()
status = await spawner.poll()
SERVER_POLL_DURATION_SECONDS.labels(
status=ServerPollStatus.from_status(status)
).observe(time.perf_counter() - poll_start_time)
else:
status = 0
# server is not running, render "not running" page
# further, set status to 404 because this is not
# serving the expected page
if status is not None:
spawn_url = url_path_join(
self.hub.base_url, "spawn", user.escaped_name, escaped_server_name
)
html = await self.render_template(
"not_running.html",
user=user,
auth_state=auth_state,
server_name=server_name,
spawn_url=spawn_url,
)
self.finish(html)
return
# we got here, server appears to be ready and running,
# no longer pending.
# redirect to the running server.
next_url = self.get_next_url(default=user.server_url(server_name))
self.redirect(next_url)
class AdminHandler(BaseHandler):
"""Render the admin page."""
@web.authenticated
# stacked decorators: all scopes must be present
# note: keep in sync with admin link condition in page.html
@needs_scope('admin-ui')
async def get(self):
auth_state = await self.current_user.get_auth_state()
html = await self.render_template(
'admin.html',
current_user=self.current_user,
auth_state=auth_state,
admin_access=True,
allow_named_servers=self.allow_named_servers,
named_server_limit_per_user=await self.get_current_user_named_server_limit(),
server_version=f'{__version__} {self.version_hash}',
api_page_limit=self.settings["api_page_default_limit"],
base_url=self.settings["base_url"],
)
self.finish(html)
class TokenPageHandler(BaseHandler):
"""Handler for page requesting new API tokens"""
@web.authenticated
async def get(self):
never = datetime(1900, 1, 1)
user = self.current_user
def sort_key(token):
return (token.last_activity or never, token.created or never)
now = utcnow(with_tz=False)
# group oauth client tokens by client id
all_tokens = defaultdict(list)
for token in sorted(user.api_tokens, key=sort_key, reverse=True):
if token.expires_at and token.expires_at < now:
self.log.warning(f"Deleting expired token {token}")
self.db.delete(token)
self.db.commit()
continue
if not token.client_id:
# token should have been deleted when client was deleted
self.log.warning(f"Deleting stale oauth token {token}")
self.db.delete(token)
self.db.commit()
continue
all_tokens[token.client_id].append(token)
# individually list tokens issued by jupyterhub itself
api_tokens = all_tokens.pop("jupyterhub", [])
# group all other tokens issued under their owners
# get the earliest created and latest last_activity
# timestamp for a given oauth client
oauth_clients = []
for client_id, tokens in all_tokens.items():
created = tokens[0].created
last_activity = tokens[0].last_activity
for token in tokens[1:]:
if token.created < created:
created = token.created
if last_activity is None or (
token.last_activity and token.last_activity > last_activity
):
last_activity = token.last_activity
token = tokens[0]
oauth_clients.append(
{
'client': token.oauth_client,
'description': token.oauth_client.description
or token.oauth_client.identifier,
'created': created,
'last_activity': last_activity,
'tokens': tokens,
# only need one token id because
# revoking one oauth token revokes all oauth tokens for that client
'token_id': tokens[0].api_id,
'token_count': len(tokens),
}
)
# sort oauth clients by last activity, created
def sort_key(client):
return (client['last_activity'] or never, client['created'] or never)
oauth_clients = sorted(oauth_clients, key=sort_key, reverse=True)
auth_state = await self.current_user.get_auth_state()
html = await self.render_template(
'token.html',
api_tokens=api_tokens,
oauth_clients=oauth_clients,
auth_state=auth_state,
)
self.finish(html)
class AcceptShareHandler(BaseHandler):
def _get_next_url(self, owner, spawner):
"""Get next_url for a given owner/spawner"""
next_url = self.get_argument("next", "")
next_url = self._validate_next_url(next_url)
if next_url:
return next_url
# default behavior:
# if it's active, redirect to server URL
if spawner.name in owner.spawners:
spawner = owner.spawners[spawner.name]
if spawner.active:
# redirect to spawner url
next_url = owner.server_url(spawner.name)
if not next_url:
# spawner not active
# TODO: next_url not specified and not running, what do we do?
# for now, redirect as if it's running,
# but that's very likely to fail on "You can't launch this server"
# is there a better experience for this?
next_url = owner.server_url(spawner.name)
# validate again, which strips away host to just absolute path
return self._validate_next_url(next_url)
@web.authenticated
async def get(self):
code = self.get_argument("code")
share_code = orm.ShareCode.find(self.db, code=code)
if share_code is None:
raise web.HTTPError(404, "Share not found or expired")
if share_code.owner == self.current_user.orm_user:
raise web.HTTPError(403, "You can't share with yourself!")
scope_descriptions = describe_raw_scopes(
share_code.scopes,
username=self.current_user.name,
)
owner = self._user_from_orm(share_code.owner)
spawner = share_code.spawner
if spawner.name in owner.spawners:
spawner = owner.spawners[spawner.name]
spawner_ready = spawner.ready
else:
spawner_ready = False
html = await self.render_template(
'accept-share.html',
code=code,
owner=owner,
spawner=spawner,
spawner_ready=spawner_ready,
spawner_url=owner.server_url(spawner.name),
scope_descriptions=scope_descriptions,
next_url=self._get_next_url(owner, spawner),
)
self.finish(html)
@web.authenticated
def post(self):
code = self.get_argument("code")
self.log.debug("Looking up %s", code)
share_code = orm.ShareCode.find(self.db, code=code)
if share_code is None:
raise web.HTTPError(400, f"Invalid share code: {code}")
if share_code.owner == self.current_user.orm_user:
raise web.HTTPError(400, "You can't share with yourself!")
user = self.current_user
share = share_code.exchange(user.orm_user)
owner = self._user_from_orm(share.owner)
spawner = share.spawner
next_url = self._get_next_url(owner, spawner)
self.redirect(next_url)
class ProxyErrorHandler(BaseHandler):
"""Handler for rendering proxy error pages"""
async def get(self, status_code_s):
status_code = int(status_code_s)
status_message = responses.get(status_code, 'Unknown HTTP Error')
# build template namespace
hub_home = url_path_join(self.hub.base_url, 'home')
message_html = ''
if status_code == 503:
message_html = ' '.join(
[
"Your server appears to be down.",
f"Try restarting it <a href='{hub_home}'>from the hub</a>",
]
)
ns = dict(
status_code=status_code,
status_message=status_message,
message_html=message_html,
logo_url=hub_home,
)
self.set_header('Content-Type', 'text/html')
# render the template
try:
html = await self.render_template(f'{status_code}.html', **ns)
except TemplateNotFound:
self.log.debug("Using default error template for %d", status_code)
html = await self.render_template('error.html', **ns)
self.write(html)
class HealthCheckHandler(BaseHandler):
"""Serve health check probes as quickly as possible"""
# There is nothing for us to do other than return a positive
# HTTP status code as quickly as possible for GET or HEAD requests
def get(self):
pass
head = get
default_handlers = [
(r'/', RootHandler),
(r'/home', HomeHandler),
(r'/admin', AdminHandler),
(r'/spawn-pending/([^/]+)', SpawnPendingHandler),
(r'/spawn-pending/([^/]+)/([^/]+)', SpawnPendingHandler),
(r'/spawn', SpawnHandler),
(r'/spawn/([^/]+)', SpawnHandler),
(r'/spawn/([^/]+)/([^/]+)', SpawnHandler),
(r'/token', TokenPageHandler),
(r'/accept-share', AcceptShareHandler),
(r'/error/(\d+)', ProxyErrorHandler),
(r'/health$', HealthCheckHandler),
(r'/api/health$', HealthCheckHandler),
]