def __init__(self, args): self._original_excepthook = sys.excepthook self.args = args if args.creds_file: try: with open(args.creds_file, 'r') as fp: creds = json.loads(fp.read()) username = creds.get('username') password = creds.get('password') session_cookie = login_user(CONF.getServerURI(), username, password) if session_cookie: logger.info('Login successful') CONF.setDBUser(username) CONF.setDBSessionCookies(session_cookie) else: logger.error('Login failed') except (IOError, ValueError): logger.error("Credentials file couldn't be loaded") self._mappers_manager = MapperManager() pending_actions = Queue() self._model_controller = ModelController(self._mappers_manager, pending_actions) self._plugin_manager = PluginManager( os.path.join(CONF.getConfigPath(), "plugins"), pending_actions=pending_actions, ) self._workspace_manager = WorkspaceManager( self._mappers_manager) # Create a PluginController and send this to UI selected. self._plugin_controller = PluginController( 'PluginController', self._plugin_manager, self._mappers_manager, pending_actions ) if self.args.cli: self.app = CliApp(self._workspace_manager, self._plugin_controller) if self.args.keep_old: CONF.setMergeStrategy("old") else: CONF.setMergeStrategy("new") else: self.app = UiFactory.create(self._model_controller, self._plugin_manager, self._workspace_manager, self._plugin_controller, self.args.gui) self.timer = TimerClass() self.timer.start()
def try_login_user(server_uri, api_username, api_password): try: session_cookie = login_user(server_uri, api_username, api_password) return session_cookie except requests.exceptions.SSLError: print("SSL certificate validation failed.\nYou can use the --cert option in Faraday to set the path of the cert") sys.exit(-1) except requests.exceptions.MissingSchema: print("The Faraday Server URL is incorrect, please try again.") sys.exit(-2)
def dispatch(args, unknown, user_help, username, password): session_cookie = login_user(args.url, username, password) if not session_cookie: raise UserWarning('Invalid credentials!') CONF.setDBUser(username) CONF.setDBSessionCookies(session_cookie) if '--' in unknown: unknown.remove('--') # We need the ModelController to register all available models mappers_manager = MapperManager() pending_actions = Queue() model_controller = ModelController(mappers_manager, pending_actions) if not args.command: print(user_help) if not args.interactive: sys.exit(1) if args.command not in list(plugins.keys()): sys.stderr.write(Fore.RED + ("ERROR: Plugin %s not found.\n" % args.command) + Fore.RESET) if args.interactive: return None else: sys.exit(1) from faraday import client # pylint:disable=import-outside-toplevel faraday_directory = os.path.dirname( os.path.realpath(os.path.join(client.__file__))) plugin_path = os.path.join(faraday_directory, "bin/", args.command + '.py') # Get filename and import this module_fplugin = imp.load_source('module_fplugin', plugin_path) module_fplugin.models.server.FARADAY_UP = False module_fplugin.models.server.SERVER_URL = args.url module_fplugin.models.server.AUTH_USER = username module_fplugin.models.server.AUTH_PASS = password call_main = getattr(module_fplugin, 'main', None) if call_main is None: sys.stderr.write(Fore.RED + "ERROR: Main function not found.\n" + Fore.RESET) if args.interactive: return None else: sys.exit(1) # Inspect the main function imported from the plugin and decide the best calling option main_argspec = inspect.getargspec(call_main) if main_argspec != CURRENT_MAIN_ARGSPEC: # Function argspec does not match current API. # Warn the user and call with original parameteres. sys.stderr.write( Fore.YELLOW + "WARNING: Plugin does not follow current call signature. Please update it! [%s.py]\n" % args.command + Fore.RESET) obj_id = None if {'args', 'parser'} <= set(main_argspec.args): # Function accepts args and parser arguments new_parser = argparse.ArgumentParser( description=plugins[args.command]['description'], prog="fplugin %s" % args.command, formatter_class=RawDescriptionAndDefaultsHelpFormatter) ret, obj_id = call_main(workspace=args.workspace, args=unknown, parser=new_parser) if obj_id is not None: print(obj_id) else: # Use old API to call plugin sys.stderr.write( Fore.YELLOW + "WARNING: Call with arguments and parser not supported.\n" + Fore.RESET) ret = call_main(workspace=args.workspace) if ret is None: ret = 0 if args.interactive: # print ('code = %d' % ret) return obj_id else: sys.exit(ret)