def init_cgiscript_possibly_with_cert(print_header=True, content_type='text/html'): """Prepare for CGI script with optional client certificate. Only used from some of the cgi scripts still on the legacy-form like requestnewjob and put. I.e. scripts where certs are not required due to use of sessionid. """ # Always rely on os.environ here since only called from cgi scripts environ = os.environ if print_header: cgiscript_header(content_type=content_type) configuration = get_configuration_object() logger = configuration.logger out = CGIOutput(logger) # get DN of user currently logged in client_id = extract_client_id(configuration, environ) if not client_id: logger.debug('(No client ID available in SSL session)') logger.info('script: %s cert: %s' % (requested_page(), client_id)) return (logger, configuration, client_id, out)
def stub(function, user_arguments_dict): """Run backend function with supplied arguments""" before_time = time.time() environ = os.environ configuration = get_configuration_object() _logger = configuration.logger # get ID of user currently logged in main = id client_id = extract_client_id(configuration, environ) output_objects = [] _logger.debug("import main for function: %s" % function) try: exec 'from %s import main' % function except Exception, err: output_objects.extend([{ 'object_type': 'error_text', 'text': 'Could not import module! %s: %s' % (function, err) }]) return (output_objects, returnvalues.SYSTEM_ERROR)
def application(environ, start_response): """MiG app called automatically by wsgi""" # TODO: verify security of this environment exposure # pass environment on to sub handlers os.environ = environ # TODO: we should avoid print calls completely in backends # make sure print calls do not interfere with wsgi sys.stdout = sys.stderr configuration = get_configuration_object() # get and log ID of user currently logged in # We can't import helper before environ is ready because it indirectly # tries to use pre-mangled environ for conf loading from shared.httpsclient import extract_client_id client_id = extract_client_id(configuration, environ) fieldstorage = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ) user_arguments_dict = fieldstorage_to_dict(fieldstorage) # default to html output_format = 'html' if user_arguments_dict.has_key('output_format'): output_format = user_arguments_dict['output_format'][0] try: if not configuration.site_enable_wsgi: raise Exception("WSGI interface not enabled for this grid") # Environment contains python script _somewhere_ , try in turn # and fall back to dashboard if all fails script_path = requested_page(environ, 'dashboard.py') backend = os.path.basename(script_path).replace('.py' , '') module_path = 'shared.functionality.%s' % backend (output_objs, ret_val) = stub(module_path, configuration, client_id, user_arguments_dict, environ) status = '200 OK' except Exception, exc: status = '500 ERROR' (output_objs, ret_val) = ([{'object_type': 'title', 'text' : 'Unsupported Interface'}, {'object_type': 'error_text', 'text' : str(exc)}, # Enable next two lines only for debugging # {'object_type': 'text', 'text': # str(environ)} {'object_type': 'link', 'text': 'Go to default interface', 'destination': '/index.html'}, ], returnvalues.SYSTEM_ERROR)
def init_cgi_script(environ, delayed_input=None): """Shared init""" configuration = get_configuration_object() logger = configuration.logger # get and log ID of user currently logged in client_id = extract_client_id(configuration, environ) logger.info('script: %s cert: %s' % (requested_page(), client_id)) if not delayed_input: fieldstorage = cgi.FieldStorage() user_arguments_dict = fieldstorage_to_dict(fieldstorage) else: user_arguments_dict = {'__DELAYED_INPUT__': delayed_input} return (configuration, logger, client_id, user_arguments_dict)
def stub(function, user_arguments_dict): """Run backend function with supplied arguments""" before_time = time.time() environ = os.environ configuration = get_configuration_object() # get ID of user currently logged in main = id client_id = extract_client_id(configuration, environ) output_objects = [] try: exec 'from %s import main' % function except Exception, err: output_objects.extend([{'object_type': 'error_text', 'text' : 'Could not import module! %s: %s' % (function, err)}]) return (output_objects, returnvalues.SYSTEM_ERROR)
def application(environ, start_response): """MiG app called automatically by wsgi""" # TODO: verify security of this environment exposure # pass environment on to sub handlers os.environ = environ # TODO: we should avoid print calls completely in backends # make sure print calls do not interfere with wsgi sys.stdout = sys.stderr configuration = get_configuration_object() _logger = configuration.logger # get and log ID of user currently logged in # We can't import helper before environ is ready because it indirectly # tries to use pre-mangled environ for conf loading from shared.httpsclient import extract_client_id client_id = extract_client_id(configuration, environ) fieldstorage = cgi.FieldStorage(fp=environ['wsgi.input'], environ=environ) user_arguments_dict = fieldstorage_to_dict(fieldstorage) # default to html output_format = 'html' if user_arguments_dict.has_key('output_format'): output_format = user_arguments_dict['output_format'][0] backend = "UNKNOWN" output_objs = [] try: if not configuration.site_enable_wsgi: _logger.error("WSGI interface is disabled in configuration") raise Exception("WSGI interface not enabled for this site") # Environment contains python script _somewhere_ , try in turn # and fall back to dashboard if all fails script_path = requested_page(environ, configuration.site_landing_page) script_name = os.path.basename(script_path) backend = os.path.splitext(script_name)[0] module_path = 'shared.functionality.%s' % backend (allow, msg) = allow_script(configuration, script_name, client_id) if allow: (output_objs, ret_val) = stub(configuration, client_id, module_path, backend, user_arguments_dict, environ) else: (output_objs, ret_val) = reject_main(client_id, user_arguments_dict) status = '200 OK' except Exception, exc: _logger.error("handling of WSGI request for %s from %s failed: %s" % (backend, client_id, exc)) status = '500 ERROR' crash_helper(configuration, backend, output_objs) output_objs.append({ 'object_type': 'link', 'text': 'Go to default interface', 'destination': configuration.site_landing_page }) ret_val = returnvalues.SYSTEM_ERROR