5454import random
5555try :
5656 import requests
57- except ImportError as err :
57+ except ImportError :
5858 requests = None
5959
6060from mig .shared import returnvalues
61- from mig .shared .base import client_id_dir , extract_field
61+ from mig .shared .base import client_id_dir , extract_field , force_native_str
6262from mig .shared .conf import get_configuration_object
6363from mig .shared .defaults import session_id_bytes
6464from mig .shared .fileio import make_symlink , pickle , unpickle , write_file , \
6868from mig .shared .init import initialize_main_variables
6969from mig .shared .pwcrypto import generate_random_ascii
7070from mig .shared .ssh import generate_ssh_rsa_key_pair , tighten_key_perms
71+ from mig .shared .url import urljoin
7172from mig .shared .workflows import create_workflow_session_id , \
7273 get_workflow_session_id
7374
@@ -281,6 +282,23 @@ def jupyter_host(configuration, output_objects, user, url):
281282 return (output_objects , returnvalues .OK )
282283
283284
285+ def jupyterhub_session_post_request (session , url , params = None , ** kwargs ):
286+ """
287+ Sends a post request to a URL
288+ :param session: the session object that can be used to conduct the post request
289+ :param url: the designated URL that the post request is sent to
290+ :param params: parameters to pass to the post request
291+ :return: the response object from the post request
292+ """
293+ if not params :
294+ params = {}
295+
296+ if "_xsrf" in session .cookies :
297+ params ["_xsrf" ] = session .cookies ['_xsrf' ]
298+
299+ return session .post (url , params = params , ** kwargs )
300+
301+
284302def reset (configuration ):
285303 """Helper function to clean up all jupyter directories and mounts
286304 :param configuration: the MiG Configuration object
@@ -445,10 +463,13 @@ def main(client_id, user_arguments_dict):
445463 # Make sure ssh daemon does not complain
446464 tighten_key_perms (configuration , client_id )
447465
448- url_base = '/' + service ['service_name' ]
449- url_home = url_base + '/home'
450- url_auth = host + url_base + '/hub/login'
451- url_data = host + url_base + '/hub/set-user-data'
466+ url_service = urljoin ('/' , service ['service_name' ])
467+ url_home = urljoin (url_service + "/" , 'home' )
468+
469+ url_base = urljoin (host , service ['service_name' ])
470+ url_hub = urljoin (url_base + "/" , 'hub' )
471+ url_auth = urljoin (url_hub + "/" , 'login' )
472+ url_data = urljoin (url_hub + "/" , 'set-user-data' )
452473
453474 # Does the client home dir contain an active mount key
454475 # If so just keep on using it.
@@ -520,15 +541,12 @@ def main(client_id, user_arguments_dict):
520541
521542 with requests .session () as session :
522543 # Refresh cookies
523- session .get (url_auth )
524- auth_params = {}
525- if "_xsrf" in session .cookies :
526- auth_params = {"_xsrf" : session .cookies ['_xsrf' ]}
544+ session .get (url_hub )
527545 # Authenticate and submit data
528- response = session . post ( url_auth , headers = auth_header , params = auth_params )
546+ response = jupyterhub_session_post_request ( session , url_auth , headers = auth_header )
529547 if response .status_code == 200 :
530548 for user_data_type , user_data in user_post_data .items ():
531- response = session . post ( url_data , json = {user_data_type : user_data }, params = auth_params )
549+ response = jupyterhub_session_post_request ( session , url_data , json = {user_data_type : user_data })
532550 if response .status_code != 200 :
533551 logger .error (
534552 "Jupyter: User %s failed to submit data %s to %s"
@@ -549,28 +567,36 @@ def main(client_id, user_arguments_dict):
549567
550568 # Generate private/public keys
551569 (mount_private_key , mount_public_key ) = generate_ssh_rsa_key_pair (
552- encode_utf8 = True )
570+ encode_utf8 = True
571+ )
572+
573+ logger .debug ("User: %s - Creating a new jupyter mount keyset - "
574+ "private_key: %s public_key: %s "
575+ % (client_id , mount_private_key , mount_public_key ))
553576
554577 # Known hosts
555578 sftp_addresses = socket .gethostbyname_ex (
556579 configuration .user_sftp_show_address or socket .getfqdn ())
557580
581+ # Write the authorization file
582+ auth_content = []
583+ str_mount_public_key = force_native_str (mount_public_key )
558584 # Subsys sftp support
559585 if configuration .site_enable_sftp_subsys :
560586 # Restrict possible mount agent
561- auth_content = []
562587 restrict_opts = 'no-agent-forwarding,no-port-forwarding,no-pty,'
563588 restrict_opts += 'no-user-rc,no-X11-forwarding'
564589 restrictions = '%s' % restrict_opts
565- auth_content .append ('%s %s\n ' % (restrictions , mount_public_key ))
566- # Write auth file
567- write_file ('\n ' .join (auth_content ),
568- os .path .join (subsys_path , session_id
569- + '.authorized_keys' ), logger , umask = 0o27 )
570-
571- logger .debug ("User: %s - Creating a new jupyter mount keyset - "
572- "private_key: %s public_key: %s "
573- % (client_id , mount_private_key , mount_public_key ))
590+ auth_content .append ('%s %s\n ' % (restrictions , str_mount_public_key ))
591+ else :
592+ auth_content .append ('%s\n ' % str_mount_public_key )
593+
594+ # Write auth file
595+ write_file (
596+ '\n ' .join (auth_content ),
597+ os .path .join (subsys_path , session_id + '.authorized_keys' ),
598+ logger , umask = 0o27
599+ )
574600
575601 jupyter_dict = {
576602 'MOUNT_HOST' : configuration .short_title ,
@@ -626,15 +652,12 @@ def main(client_id, user_arguments_dict):
626652 # First login
627653 with requests .session () as session :
628654 # Refresh cookies
629- session .get (url_auth )
630- auth_params = {}
631- if "_xsrf" in session .cookies :
632- auth_params = {"_xsrf" : session .cookies ['_xsrf' ]}
655+ session .get (url_hub )
633656 # Authenticate
634- response = session . post ( url_auth , headers = auth_header , params = auth_params )
657+ response = jupyterhub_session_post_request ( session , url_auth , headers = auth_header )
635658 if response .status_code == 200 :
636659 for user_data_type , user_data in user_post_data .items ():
637- response = session . post ( url_data , json = {user_data_type : user_data }, params = auth_params )
660+ response = jupyterhub_session_post_request ( session , url_data , json = {user_data_type : user_data })
638661 if response .status_code != 200 :
639662 logger .error ("Jupyter: User %s failed to submit data %s to %s"
640663 % (client_id , user_data , url_data ))
0 commit comments