def do_setup(config, all_params): print "Setting up syntool..." # if the config file already exists, then bail if os.path.exists(conf.CONFIG_FILENAME): raise Exception("Syntool is already set up (in %s)" % conf.CONFIG_DIR) # check args... for required_key in ['syndicate_host', 'syndicate_port', 'user_id']: if config.get(required_key, None) is None: print >> sys.stderr, "Missing argument: %s" % required_key sys.exit(1) key_dirs = {} write_config = {} for key_type, key_dirname in conf.KEY_DIR_NAMES.items(): key_dirs[key_type] = key_dirname + "/" # generate URL if not given already if not config.has_key('url'): config['url'] = conf.make_ms_url(config['syndicate_host'], config['syndicate_port'], config['no_tls']) write_config.update(key_dirs) for attr in ['user_id', 'url']: write_config[attr] = config[attr] config_str = conf.make_config_str(write_config) log.debug("Obtaining Syndicate public key...") syndicate_public_key = download_syndicate_public_key(config) if syndicate_public_key is None: raise Exception("Failed to obtain Syndicate public key") if not config['trust_public_key']: do_trust = ask_trust_public_key(config, syndicate_public_key) if not do_trust: log.error("Refusing to trust key.") sys.exit(0) # store this for activation try: config['syndicate_public_key'] = CryptoKey.importKey( syndicate_public_key) except Exception, e: # shouldn't fail--download_syndicate_public_key validates log.error("Invalid public key") log.exception(e) sys.exit(1)
def do_setup( config, all_params ): print "Setting up syntool..." # if the config file already exists, then bail if os.path.exists( conf.CONFIG_FILENAME ): raise Exception("Syntool is already set up (in %s)" % conf.CONFIG_DIR) # check args... for required_key in ['syndicate_host', 'syndicate_port', 'user_id']: if config.get(required_key, None) is None: print >> sys.stderr, "Missing argument: %s" % required_key sys.exit(1) key_dirs = {} write_config = {} for key_type, key_dirname in conf.KEY_DIR_NAMES.items(): key_dirs[key_type] = key_dirname + "/" # generate URL if not given already if not config.has_key('url'): config['url'] = conf.make_ms_url( config['syndicate_host'], config['syndicate_port'], config['no_tls'] ) write_config.update( key_dirs ) for attr in ['user_id', 'url']: write_config[attr] = config[attr] config_str = conf.make_config_str( write_config ) log.debug("Obtaining Syndicate public key...") syndicate_public_key = download_syndicate_public_key( config ) if syndicate_public_key is None: raise Exception("Failed to obtain Syndicate public key") if not config['trust_public_key']: do_trust = ask_trust_public_key( config, syndicate_public_key ) if not do_trust: log.error("Refusing to trust key.") sys.exit(0) # store this for activation try: config['syndicate_public_key'] = CryptoKey.importKey( syndicate_public_key ) except Exception, e: # shouldn't fail--download_syndicate_public_key validates log.error("Invalid public key") log.exception(e) sys.exit(1)
def load_options( argv, setup_methods=[], builtin_methods=[] ): parser = modconf.build_parser( argv[0], conf.CONFIG_DESCRIPTION, conf.CONFIG_OPTIONS ) opts = parser.parse_args( argv[1:] ) # load everything into a dictionary and return it config = None config_str = None config_file_path = None if hasattr( opts, "config" ) and opts.config != None: config_file_path = opts.config[0] else: config_file_path = conf.CONFIG_FILENAME config_str = storage.read_file( config_file_path ) config = {} method_name, args, kw = read_params( getattr(opts, 'params', [] ) ) if config_str is None: # possibly calling 'setup', so fill in empty information if method_name not in setup_methods: raise Exception("Failed to load configuration from %s" % config_file_path) # calling 'setup', so populate the config conf.fill_defaults( config ) if not hasattr(opts, 'user_id') and method_name in setup_methods: raise Exception("--user_id option is required for this method") config['user_id'] = opts.user_id config_file_path = conf.CONFIG_FILENAME config_str = conf.make_config_str( config ) config_with_opts = conf.load_config( config_file_path, config_str, opts ) if config_with_opts != None: config.update( config_with_opts ) else: raise Exception("Failed to parse command-line args") else: config = conf.load_config( config_file_path, config_str, opts ) if config is None: raise Exception("Failed to parse configuration from %s" % config_file_path) config['params'] = getattr( opts, 'params', [] ) # set up the key directories setup_key_directories( config ) # generate syndicate_host and syndicate_port from URL, if needed if config.get('url', None) != None: # use https if no :// is given url = config['url'] host, port, no_tls = parse_url( url ) config['syndicate_host'] = host config['syndicate_port'] = port config['no_tls'] = no_tls # what do we need for authentication? auth_opts = None if method_name in setup_methods: # setup methods need password auth_opts = [msconfig.AUTH_METHOD_PASSWORD] elif method_name in builtin_methods: # builtin methods that don't do setup need no password auth_opts = [msconfig.AUTH_METHOD_NONE] else: auth_opts = api.method_auth_options( method_name ) have_auth_tokens = False if msconfig.AUTH_METHOD_NONE in auth_opts: # no authentication needed config['user_pkey'] = None config['password'] = None have_auth_tokens = True else: # obtain the user's private key and/or password, if present if not config.has_key('user_pkey') and msconfig.AUTH_METHOD_PUBKEY in auth_opts: try: config['user_pkey'] = load_user_private_key( config, config['user_id'] ) have_auth_tokens = True except Exception, e: log.warning("No private key found.") config['user_pkey'] = None pass if not have_auth_tokens and not config.has_key('password') and msconfig.AUTH_METHOD_PASSWORD in auth_opts: config['password'] = getpass.getpass("Syndicate password: ") have_auth_tokens = True elif config.has_key('password') and msconfig.AUTH_METHOD_PASSWORD in auth_opts: have_auth_tokens = True
def load_options(argv, setup_methods=[], builtin_methods=[]): parser = modconf.build_parser(argv[0], conf.CONFIG_DESCRIPTION, conf.CONFIG_OPTIONS) opts = parser.parse_args(argv[1:]) # load everything into a dictionary and return it config = None config_str = None config_file_path = None if hasattr(opts, "config") and opts.config != None: config_file_path = opts.config[0] else: config_file_path = conf.CONFIG_FILENAME config_str = storage.read_file(config_file_path) config = {} method_name, args, kw = read_params(getattr(opts, 'params', [])) if config_str is None: # possibly calling 'setup', so fill in empty information if method_name not in setup_methods: raise Exception("Failed to load configuration from %s" % config_file_path) # calling 'setup', so populate the config conf.fill_defaults(config) if not hasattr(opts, 'user_id') and method_name in setup_methods: raise Exception("--user_id option is required for this method") config['user_id'] = opts.user_id config_file_path = conf.CONFIG_FILENAME config_str = conf.make_config_str(config) config_with_opts = conf.load_config(config_file_path, config_str, opts) if config_with_opts != None: config.update(config_with_opts) else: raise Exception("Failed to parse command-line args") else: config = conf.load_config(config_file_path, config_str, opts) if config is None: raise Exception("Failed to parse configuration from %s" % config_file_path) config['params'] = getattr(opts, 'params', []) # set up the key directories setup_key_directories(config) # generate syndicate_host and syndicate_port from URL, if needed if config.get('url', None) != None: # use https if no :// is given url = config['url'] host, port, no_tls = parse_url(url) config['syndicate_host'] = host config['syndicate_port'] = port config['no_tls'] = no_tls # what do we need for authentication? auth_opts = None if method_name in setup_methods: # setup methods need password auth_opts = [msconfig.AUTH_METHOD_PASSWORD] elif method_name in builtin_methods: # builtin methods that don't do setup need no password auth_opts = [msconfig.AUTH_METHOD_NONE] else: auth_opts = api.method_auth_options(method_name) have_auth_tokens = False if msconfig.AUTH_METHOD_NONE in auth_opts: # no authentication needed config['user_pkey'] = None config['password'] = None have_auth_tokens = True else: # obtain the user's private key and/or password, if present if not config.has_key( 'user_pkey') and msconfig.AUTH_METHOD_PUBKEY in auth_opts: try: config['user_pkey'] = load_user_private_key( config, config['user_id']) have_auth_tokens = True except Exception, e: log.warning("No private key found.") config['user_pkey'] = None pass if not have_auth_tokens and not config.has_key( 'password') and msconfig.AUTH_METHOD_PASSWORD in auth_opts: config['password'] = getpass.getpass("Syndicate password: ") have_auth_tokens = True elif config.has_key( 'password') and msconfig.AUTH_METHOD_PASSWORD in auth_opts: have_auth_tokens = True